Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 3 | * svm.h - shared VM allocation, mmap(...MAP_FIXED...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 4 | * brain police |
| 5 | * |
| 6 | * Copyright (c) 2009 Cisco and/or its affiliates. |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at: |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | *------------------------------------------------------------------ |
| 19 | */ |
| 20 | |
| 21 | #ifndef __included_svm_h__ |
| 22 | #define __included_svm_h__ |
| 23 | |
| 24 | #include <pthread.h> |
| 25 | #include <vppinfra/clib.h> |
| 26 | #include <vppinfra/mem.h> |
| 27 | |
Dave Barach | 95bb883 | 2015-12-12 10:37:00 -0500 | [diff] [blame] | 28 | #define MMAP_PAGESIZE (clib_mem_get_page_size()) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 30 | #define SVM_VERSION ((1<<16) | 1) /* set to declare region ready. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 32 | #define SVM_FLAGS_MHEAP (1<<0) /* region contains an mheap */ |
| 33 | #define SVM_FLAGS_FILE (1<<1) /* region backed by one or more files */ |
| 34 | #define SVM_FLAGS_NODATA (1<<2) /* region will be further subdivided */ |
| 35 | #define SVM_FLAGS_NEED_DATA_INIT (1<<3) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 37 | #define SVM_PVT_MHEAP_SIZE (128<<10) /* region's private mheap (128k) */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 38 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 39 | typedef struct svm_region_ |
| 40 | { |
| 41 | volatile uword version; |
| 42 | pthread_mutex_t mutex; |
| 43 | pthread_cond_t condvar; |
| 44 | int mutex_owner_pid; /* in case of trouble */ |
| 45 | int mutex_owner_tag; |
| 46 | uword flags; |
| 47 | uword virtual_base; /* base of the region object */ |
| 48 | uword virtual_size; |
| 49 | void *region_heap; |
| 50 | void *data_base; /* data portion base address */ |
| 51 | void *data_heap; /* data heap, if any */ |
| 52 | volatile void *user_ctx; /* user context pointer */ |
| 53 | /* stuff allocated in the region's heap */ |
| 54 | uword bitmap_size; /* nbits in virtual alloc bitmap */ |
| 55 | uword *bitmap; /* the bitmap */ |
| 56 | char *region_name; |
| 57 | char *backing_file; |
| 58 | char **filenames; |
| 59 | uword *client_pids; |
| 60 | /* pad */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 62 | /* next page: |
| 63 | * (64K) clib heap for the region itself |
| 64 | * |
| 65 | * data_base -> whatever is in this region |
| 66 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
| 68 | } svm_region_t; |
| 69 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 70 | typedef struct svm_map_region_args_ |
| 71 | { |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 72 | const char *root_path; /* NULL means use the truly global arena */ |
| 73 | const char *name; |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 74 | u64 baseva; |
| 75 | u64 size; |
| 76 | u64 pvt_heap_size; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 77 | uword flags; |
| 78 | char *backing_file; |
| 79 | uword backing_mmap_size; |
| 80 | /* uid, gid to own the svm region(s) */ |
| 81 | int uid; |
| 82 | int gid; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | } svm_map_region_args_t; |
| 84 | |
| 85 | |
| 86 | /* |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 87 | * Memory shared across all router instances. Packet buffers, etc |
| 88 | * Base should be "out of the way," and size should be big enough to |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | * cover everything we plan to put here. |
| 90 | */ |
| 91 | #define SVM_GLOBAL_REGION_BASEVA 0x30000000 |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 92 | #define SVM_GLOBAL_REGION_SIZE (64<<20) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | #define SVM_GLOBAL_REGION_NAME "/global_vm" |
| 94 | |
| 95 | /* |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 96 | * Memory shared across individual router instances. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | */ |
| 98 | #define SVM_OVERLAY_REGION_BASEVA \ |
| 99 | (SVM_GLOBAL_REGION_BASEVA + SVM_GLOBAL_REGION_SIZE) |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 100 | #define SVM_OVERLAY_REGION_SIZE (1<<20) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | #define SVM_OVERLAY_REGION_BASENAME "/overlay_vm" |
| 102 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 103 | typedef struct |
| 104 | { |
| 105 | u8 *subregion_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | } svm_subregion_t; |
| 107 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 108 | typedef struct |
| 109 | { |
| 110 | svm_subregion_t *subregions; /* subregion pool */ |
| 111 | uword *name_hash; |
| 112 | u8 *root_path; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | } svm_main_region_t; |
| 114 | |
| 115 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 116 | void *svm_region_find_or_create (svm_map_region_args_t * a); |
| 117 | void svm_region_init (void); |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 118 | void svm_region_init_chroot (const char *root_path); |
| 119 | void svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 120 | void svm_region_init_args (svm_map_region_args_t * a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | void svm_region_exit (void); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 122 | void svm_region_unmap (void *rp_arg); |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 123 | void svm_client_scan (const char *root_path); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 124 | void svm_client_scan_this_region_nolock (svm_region_t * rp); |
| 125 | u8 *shm_name_from_svm_map_region_args (svm_map_region_args_t * a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 127 | static inline void * |
| 128 | svm_mem_alloc (svm_region_t * rp, uword size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 130 | u8 *oldheap; |
| 131 | ASSERT (rp->flags & SVM_FLAGS_MHEAP); |
| 132 | u8 *rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 134 | pthread_mutex_lock (&rp->mutex); |
| 135 | oldheap = clib_mem_set_heap (rp->data_heap); |
| 136 | rv = clib_mem_alloc (size); |
| 137 | clib_mem_set_heap (oldheap); |
| 138 | pthread_mutex_unlock (&rp->mutex); |
| 139 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 142 | static inline void * |
| 143 | svm_mem_alloc_aligned_at_offset (svm_region_t * rp, |
| 144 | uword size, uword align, uword offset) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 146 | u8 *oldheap; |
| 147 | ASSERT (rp->flags & SVM_FLAGS_MHEAP); |
| 148 | u8 *rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 150 | pthread_mutex_lock (&rp->mutex); |
| 151 | oldheap = clib_mem_set_heap (rp->data_heap); |
Dave Barach | 241e522 | 2016-10-13 10:53:26 -0400 | [diff] [blame] | 152 | rv = clib_mem_alloc_aligned_at_offset (size, align, offset, |
| 153 | 1 /* yes, call os_out_of_memory */ ); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 154 | clib_mem_set_heap (oldheap); |
| 155 | pthread_mutex_unlock (&rp->mutex); |
| 156 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 159 | static inline void |
| 160 | svm_mem_free (svm_region_t * rp, void *ptr) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 162 | u8 *oldheap; |
| 163 | ASSERT (rp->flags & SVM_FLAGS_MHEAP); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 165 | pthread_mutex_lock (&rp->mutex); |
| 166 | oldheap = clib_mem_set_heap (rp->data_heap); |
| 167 | clib_mem_free (ptr); |
| 168 | clib_mem_set_heap (oldheap); |
| 169 | pthread_mutex_unlock (&rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
| 171 | } |
| 172 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 173 | static inline void * |
| 174 | svm_push_pvt_heap (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 176 | u8 *oldheap; |
| 177 | oldheap = clib_mem_set_heap (rp->region_heap); |
| 178 | return ((void *) oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 181 | static inline void * |
| 182 | svm_push_data_heap (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 184 | u8 *oldheap; |
| 185 | oldheap = clib_mem_set_heap (rp->data_heap); |
| 186 | return ((void *) oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 189 | static inline void |
| 190 | svm_pop_heap (void *oldheap) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 192 | clib_mem_set_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 195 | u8 *format_svm_region (u8 * s, va_list * args); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | |
| 197 | svm_region_t *svm_get_root_rp (void); |
| 198 | |
| 199 | #endif /* __included_svm_h__ */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 200 | |
| 201 | /* |
| 202 | * fd.io coding-style-patch-verification: ON |
| 203 | * |
| 204 | * Local Variables: |
| 205 | * eval: (c-set-style "gnu") |
| 206 | * End: |
| 207 | */ |