Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * svm.h - shared VM allocation, mmap(...MAP_FIXED...) |
| 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 | |
| 28 | #define MMAP_PAGESIZE (4<<10) |
| 29 | |
| 30 | #define SVM_VERSION ((1<<16) | 1) /* set to declare region ready. */ |
| 31 | |
| 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) |
| 36 | |
| 37 | #define SVM_PVT_MHEAP_SIZE (32<<10) /* region's private mheap (32k) */ |
| 38 | |
| 39 | typedef struct svm_region_ { |
| 40 | volatile uword version; |
| 41 | pthread_mutex_t mutex; |
| 42 | pthread_cond_t condvar; |
| 43 | int mutex_owner_pid; /* in case of trouble */ |
| 44 | int mutex_owner_tag; |
| 45 | uword flags; |
| 46 | uword virtual_base; /* base of the region object */ |
| 47 | uword virtual_size; |
| 48 | void *region_heap; |
| 49 | void *data_base; /* data portion base address */ |
| 50 | void *data_heap; /* data heap, if any */ |
| 51 | volatile void *user_ctx; /* user context pointer */ |
| 52 | /* stuff allocated in the region's heap */ |
| 53 | uword bitmap_size; /* nbits in virtual alloc bitmap */ |
| 54 | uword *bitmap; /* the bitmap */ |
| 55 | char *region_name; |
| 56 | char *backing_file; |
| 57 | char **filenames; |
| 58 | uword *client_pids; |
| 59 | /* pad */ |
| 60 | |
| 61 | /* next page: |
| 62 | * (64K) clib heap for the region itself |
| 63 | * |
| 64 | * data_base -> whatever is in this region |
| 65 | */ |
| 66 | |
| 67 | } svm_region_t; |
| 68 | |
| 69 | typedef struct svm_map_region_args_ { |
| 70 | char *root_path; /* NULL means use the truly global arena */ |
| 71 | char *name; |
| 72 | uword baseva; |
| 73 | uword size; |
| 74 | uword flags; |
| 75 | char *backing_file; |
| 76 | uword backing_mmap_size; |
| 77 | } svm_map_region_args_t; |
| 78 | |
| 79 | |
| 80 | /* |
| 81 | * Memory shared across all router instances. Packet buffers, etc |
| 82 | * Base should be "out of the way," and size should be big enough to |
| 83 | * cover everything we plan to put here. |
| 84 | */ |
| 85 | #define SVM_GLOBAL_REGION_BASEVA 0x30000000 |
| 86 | #define SVM_GLOBAL_REGION_SIZE (64<<20) |
| 87 | #define SVM_GLOBAL_REGION_NAME "/global_vm" |
| 88 | |
| 89 | /* |
| 90 | * Memory shared across individual router instances. |
| 91 | */ |
| 92 | #define SVM_OVERLAY_REGION_BASEVA \ |
| 93 | (SVM_GLOBAL_REGION_BASEVA + SVM_GLOBAL_REGION_SIZE) |
| 94 | #define SVM_OVERLAY_REGION_SIZE (1<<20) |
| 95 | #define SVM_OVERLAY_REGION_BASENAME "/overlay_vm" |
| 96 | |
| 97 | typedef struct { |
| 98 | u8 *subregion_name; |
| 99 | } svm_subregion_t; |
| 100 | |
| 101 | typedef struct { |
| 102 | svm_subregion_t *subregions; /* subregion pool */ |
| 103 | uword *name_hash; |
| 104 | u8 *root_path; |
| 105 | } svm_main_region_t; |
| 106 | |
| 107 | |
| 108 | void *svm_region_find_or_create (svm_map_region_args_t *a); |
| 109 | void svm_region_init(void); |
| 110 | void svm_region_init_chroot(char *root_path); |
| 111 | void svm_region_exit (void); |
| 112 | void svm_region_unmap(void *rp_arg); |
| 113 | void svm_client_scan (char *root_path); |
| 114 | void svm_client_scan_this_region_nolock (svm_region_t *rp); |
| 115 | u8 *shm_name_from_svm_map_region_args (svm_map_region_args_t *a); |
| 116 | |
| 117 | static inline void *svm_mem_alloc (svm_region_t *rp, uword size) |
| 118 | { |
| 119 | u8 *oldheap; |
| 120 | ASSERT(rp->flags & SVM_FLAGS_MHEAP); |
| 121 | u8 *rv; |
| 122 | |
| 123 | pthread_mutex_lock(&rp->mutex); |
| 124 | oldheap = clib_mem_set_heap(rp->data_heap); |
| 125 | rv = clib_mem_alloc (size); |
| 126 | clib_mem_set_heap(oldheap); |
| 127 | pthread_mutex_unlock(&rp->mutex); |
| 128 | return (rv); |
| 129 | } |
| 130 | |
| 131 | static inline void *svm_mem_alloc_aligned_at_offset (svm_region_t *rp, |
| 132 | uword size, |
| 133 | uword align, |
| 134 | uword offset) |
| 135 | { |
| 136 | u8 *oldheap; |
| 137 | ASSERT(rp->flags & SVM_FLAGS_MHEAP); |
| 138 | u8 *rv; |
| 139 | |
| 140 | pthread_mutex_lock(&rp->mutex); |
| 141 | oldheap = clib_mem_set_heap(rp->data_heap); |
| 142 | rv = clib_mem_alloc_aligned_at_offset (size, align, offset); |
| 143 | clib_mem_set_heap(oldheap); |
| 144 | pthread_mutex_unlock(&rp->mutex); |
| 145 | return (rv); |
| 146 | } |
| 147 | |
| 148 | static inline void svm_mem_free (svm_region_t *rp, void *ptr) |
| 149 | { |
| 150 | u8 *oldheap; |
| 151 | ASSERT(rp->flags & SVM_FLAGS_MHEAP); |
| 152 | |
| 153 | pthread_mutex_lock(&rp->mutex); |
| 154 | oldheap = clib_mem_set_heap(rp->data_heap); |
| 155 | clib_mem_free (ptr); |
| 156 | clib_mem_set_heap(oldheap); |
| 157 | pthread_mutex_unlock(&rp->mutex); |
| 158 | |
| 159 | } |
| 160 | |
| 161 | static inline void *svm_push_pvt_heap (svm_region_t *rp) |
| 162 | { |
| 163 | u8 *oldheap; |
| 164 | oldheap = clib_mem_set_heap(rp->region_heap); |
| 165 | return ((void *) oldheap); |
| 166 | } |
| 167 | |
| 168 | static inline void *svm_push_data_heap (svm_region_t *rp) |
| 169 | { |
| 170 | u8 *oldheap; |
| 171 | oldheap = clib_mem_set_heap(rp->data_heap); |
| 172 | return ((void *) oldheap); |
| 173 | } |
| 174 | |
| 175 | static inline void svm_pop_heap (void *oldheap) |
| 176 | { |
| 177 | clib_mem_set_heap(oldheap); |
| 178 | } |
| 179 | |
| 180 | u8 * format_svm_region (u8 * s, va_list * args); |
| 181 | |
| 182 | svm_region_t *svm_get_root_rp (void); |
| 183 | |
| 184 | #endif /* __included_svm_h__ */ |