FD.io VPP  v16.09
Vector Packet Processing
svm.h
Go to the documentation of this file.
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 (clib_mem_get_page_size())
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 (128<<10) /* region's private mheap (128k) */
38 
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 */
47  uword virtual_base; /* base of the region object */
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;
60  /* pad */
61 
62  /* next page:
63  * (64K) clib heap for the region itself
64  *
65  * data_base -> whatever is in this region
66  */
67 
68 } svm_region_t;
69 
70 typedef struct svm_map_region_args_
71 {
72  char *root_path; /* NULL means use the truly global arena */
73  char *name;
78  char *backing_file;
80  /* uid, gid to own the svm region(s) */
81  int uid;
82  int gid;
84 
85 
86 /*
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
89  * cover everything we plan to put here.
90  */
91 #define SVM_GLOBAL_REGION_BASEVA 0x30000000
92 #define SVM_GLOBAL_REGION_SIZE (64<<20)
93 #define SVM_GLOBAL_REGION_NAME "/global_vm"
94 
95 /*
96  * Memory shared across individual router instances.
97  */
98 #define SVM_OVERLAY_REGION_BASEVA \
99  (SVM_GLOBAL_REGION_BASEVA + SVM_GLOBAL_REGION_SIZE)
100 #define SVM_OVERLAY_REGION_SIZE (1<<20)
101 #define SVM_OVERLAY_REGION_BASENAME "/overlay_vm"
102 
103 typedef struct
104 {
107 
108 typedef struct
109 {
110  svm_subregion_t *subregions; /* subregion pool */
114 
115 
117 void svm_region_init (void);
118 void svm_region_init_chroot (char *root_path);
119 void svm_region_init_chroot_uid_gid (char *root_path, int uid, int gid);
121 void svm_region_exit (void);
122 void svm_region_unmap (void *rp_arg);
123 void svm_client_scan (char *root_path);
126 
127 static inline void *
129 {
130  u8 *oldheap;
131  ASSERT (rp->flags & SVM_FLAGS_MHEAP);
132  u8 *rv;
133 
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);
140 }
141 
142 static inline void *
144  uword size, uword align, uword offset)
145 {
146  u8 *oldheap;
147  ASSERT (rp->flags & SVM_FLAGS_MHEAP);
148  u8 *rv;
149 
150  pthread_mutex_lock (&rp->mutex);
151  oldheap = clib_mem_set_heap (rp->data_heap);
152  rv = clib_mem_alloc_aligned_at_offset (size, align, offset);
153  clib_mem_set_heap (oldheap);
154  pthread_mutex_unlock (&rp->mutex);
155  return (rv);
156 }
157 
158 static inline void
159 svm_mem_free (svm_region_t * rp, void *ptr)
160 {
161  u8 *oldheap;
162  ASSERT (rp->flags & SVM_FLAGS_MHEAP);
163 
164  pthread_mutex_lock (&rp->mutex);
165  oldheap = clib_mem_set_heap (rp->data_heap);
166  clib_mem_free (ptr);
167  clib_mem_set_heap (oldheap);
168  pthread_mutex_unlock (&rp->mutex);
169 
170 }
171 
172 static inline void *
174 {
175  u8 *oldheap;
176  oldheap = clib_mem_set_heap (rp->region_heap);
177  return ((void *) oldheap);
178 }
179 
180 static inline void *
182 {
183  u8 *oldheap;
184  oldheap = clib_mem_set_heap (rp->data_heap);
185  return ((void *) oldheap);
186 }
187 
188 static inline void
189 svm_pop_heap (void *oldheap)
190 {
191  clib_mem_set_heap (oldheap);
192 }
193 
194 u8 *format_svm_region (u8 * s, va_list * args);
195 
197 
198 #endif /* __included_svm_h__ */
199 
200 /*
201  * fd.io coding-style-patch-verification: ON
202  *
203  * Local Variables:
204  * eval: (c-set-style "gnu")
205  * End:
206  */
char * root_path
Definition: svm.h:72
void svm_region_exit(void)
Definition: svm.c:1078
u8 * root_path
Definition: svm.h:112
u64 pvt_heap_size
Definition: svm.h:76
static void svm_pop_heap(void *oldheap)
Definition: svm.h:189
void svm_region_init_chroot_uid_gid(char *root_path, int uid, int gid)
Definition: svm.c:816
a
Definition: bitmap.h:516
uword virtual_base
Definition: svm.h:47
void * svm_region_find_or_create(svm_map_region_args_t *a)
Definition: svm.c:839
#define SVM_FLAGS_MHEAP
Definition: svm.h:32
static void svm_mem_free(svm_region_t *rp, void *ptr)
Definition: svm.h:159
void svm_region_init_args(svm_map_region_args_t *a)
Definition: svm.c:833
static void * svm_push_data_heap(svm_region_t *rp)
Definition: svm.h:181
uword * client_pids
Definition: svm.h:59
volatile void * user_ctx
Definition: svm.h:52
u8 * shm_name_from_svm_map_region_args(svm_map_region_args_t *a)
Definition: svm.c:388
char * name
Definition: svm.h:73
unsigned long u64
Definition: types.h:89
svm_region_t * svm_get_root_rp(void)
Definition: svm.c:54
pthread_cond_t condvar
Definition: svm.h:43
void * data_base
Definition: svm.h:50
u8 * subregion_name
Definition: svm.h:105
uword * name_hash
Definition: svm.h:111
static void * svm_mem_alloc_aligned_at_offset(svm_region_t *rp, uword size, uword align, uword offset)
Definition: svm.h:143
static void * svm_mem_alloc(svm_region_t *rp, uword size)
Definition: svm.h:128
svm_subregion_t * subregions
Definition: svm.h:110
char * backing_file
Definition: svm.h:57
uword virtual_size
Definition: svm.h:48
char * backing_file
Definition: svm.h:78
char * region_name
Definition: svm.h:56
static void * clib_mem_alloc_aligned_at_offset(uword size, uword align, uword align_offset)
Definition: mem.h:72
static void * svm_push_pvt_heap(svm_region_t *rp)
Definition: svm.h:173
static void * clib_mem_set_heap(void *heap)
Definition: mem.h:201
void svm_client_scan(char *root_path)
Definition: svm.c:1154
uword bitmap_size
Definition: svm.h:54
void svm_region_init_chroot(char *root_path)
Definition: svm.c:799
#define ASSERT(truth)
struct svm_map_region_args_ svm_map_region_args_t
u32 size
Definition: vhost-user.h:77
static void clib_mem_free(void *p)
Definition: mem.h:154
volatile uword version
Definition: svm.h:41
int mutex_owner_tag
Definition: svm.h:45
static void * clib_mem_alloc(uword size)
Definition: mem.h:107
void svm_region_init(void)
Definition: svm.c:782
u64 uword
Definition: types.h:112
template key/value backing page structure
Definition: bihash_doc.h:44
uword backing_mmap_size
Definition: svm.h:79
unsigned char u8
Definition: types.h:56
void * region_heap
Definition: svm.h:49
uword * bitmap
Definition: svm.h:55
u8 * format_svm_region(u8 *s, va_list *args)
Definition: svm.c:143
char ** filenames
Definition: svm.h:58
void * data_heap
Definition: svm.h:51
void svm_client_scan_this_region_nolock(svm_region_t *rp)
Definition: svm.c:1126
int mutex_owner_pid
Definition: svm.h:44
struct svm_region_ svm_region_t
uword flags
Definition: svm.h:46
pthread_mutex_t mutex
Definition: svm.h:42
void svm_region_unmap(void *rp_arg)
Definition: svm.c:958