Add API calls for packet generator
[vpp.git] / svm / svm.h
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 */
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 */
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;
74   uword baseva;
75   uword size;
76   uword flags;
77   char *backing_file;
78   uword backing_mmap_size;
79   /* uid, gid to own the svm region(s) */
80   int uid;
81   int gid;
82 } svm_map_region_args_t;
83
84
85 /*
86  * Memory shared across all router instances. Packet buffers, etc
87  * Base should be "out of the way," and size should be big enough to
88  * cover everything we plan to put here.
89  */
90 #define SVM_GLOBAL_REGION_BASEVA  0x30000000
91 #define SVM_GLOBAL_REGION_SIZE    (64<<20)
92 #define SVM_GLOBAL_REGION_NAME "/global_vm"
93
94 /*
95  * Memory shared across individual router instances.
96  */
97 #define SVM_OVERLAY_REGION_BASEVA \
98                (SVM_GLOBAL_REGION_BASEVA + SVM_GLOBAL_REGION_SIZE)
99 #define SVM_OVERLAY_REGION_SIZE   (1<<20)
100 #define SVM_OVERLAY_REGION_BASENAME "/overlay_vm"
101
102 typedef struct
103 {
104   u8 *subregion_name;
105 } svm_subregion_t;
106
107 typedef struct
108 {
109   svm_subregion_t *subregions;  /* subregion pool */
110   uword *name_hash;
111   u8 *root_path;
112 } svm_main_region_t;
113
114
115 void *svm_region_find_or_create (svm_map_region_args_t * a);
116 void svm_region_init (void);
117 void svm_region_init_chroot (char *root_path);
118 void svm_region_init_chroot_uid_gid (char *root_path, int uid, int gid);
119 void svm_region_exit (void);
120 void svm_region_unmap (void *rp_arg);
121 void svm_client_scan (char *root_path);
122 void svm_client_scan_this_region_nolock (svm_region_t * rp);
123 u8 *shm_name_from_svm_map_region_args (svm_map_region_args_t * a);
124
125 static inline void *
126 svm_mem_alloc (svm_region_t * rp, uword size)
127 {
128   u8 *oldheap;
129   ASSERT (rp->flags & SVM_FLAGS_MHEAP);
130   u8 *rv;
131
132   pthread_mutex_lock (&rp->mutex);
133   oldheap = clib_mem_set_heap (rp->data_heap);
134   rv = clib_mem_alloc (size);
135   clib_mem_set_heap (oldheap);
136   pthread_mutex_unlock (&rp->mutex);
137   return (rv);
138 }
139
140 static inline void *
141 svm_mem_alloc_aligned_at_offset (svm_region_t * rp,
142                                  uword size, uword align, uword offset)
143 {
144   u8 *oldheap;
145   ASSERT (rp->flags & SVM_FLAGS_MHEAP);
146   u8 *rv;
147
148   pthread_mutex_lock (&rp->mutex);
149   oldheap = clib_mem_set_heap (rp->data_heap);
150   rv = clib_mem_alloc_aligned_at_offset (size, align, offset);
151   clib_mem_set_heap (oldheap);
152   pthread_mutex_unlock (&rp->mutex);
153   return (rv);
154 }
155
156 static inline void
157 svm_mem_free (svm_region_t * rp, void *ptr)
158 {
159   u8 *oldheap;
160   ASSERT (rp->flags & SVM_FLAGS_MHEAP);
161
162   pthread_mutex_lock (&rp->mutex);
163   oldheap = clib_mem_set_heap (rp->data_heap);
164   clib_mem_free (ptr);
165   clib_mem_set_heap (oldheap);
166   pthread_mutex_unlock (&rp->mutex);
167
168 }
169
170 static inline void *
171 svm_push_pvt_heap (svm_region_t * rp)
172 {
173   u8 *oldheap;
174   oldheap = clib_mem_set_heap (rp->region_heap);
175   return ((void *) oldheap);
176 }
177
178 static inline void *
179 svm_push_data_heap (svm_region_t * rp)
180 {
181   u8 *oldheap;
182   oldheap = clib_mem_set_heap (rp->data_heap);
183   return ((void *) oldheap);
184 }
185
186 static inline void
187 svm_pop_heap (void *oldheap)
188 {
189   clib_mem_set_heap (oldheap);
190 }
191
192 u8 *format_svm_region (u8 * s, va_list * args);
193
194 svm_region_t *svm_get_root_rp (void);
195
196 #endif /* __included_svm_h__ */
197
198 /*
199  * fd.io coding-style-patch-verification: ON
200  *
201  * Local Variables:
202  * eval: (c-set-style "gnu")
203  * End:
204  */