blob: 5b95abb66ba3c098a4ff59d1e830ff6a07ccf1eb [file] [log] [blame]
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
Dave Barach8a7fb0c2016-07-08 14:44:23 -04003 * svm.h - shared VM allocation, mmap(...MAP_FIXED...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004 * 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 Barach95bb8832015-12-12 10:37:00 -050028#define MMAP_PAGESIZE (clib_mem_get_page_size())
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Dave Barach8a7fb0c2016-07-08 14:44:23 -040030#define SVM_VERSION ((1<<16) | 1) /* set to declare region ready. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
Dave Barach8a7fb0c2016-07-08 14:44:23 -040032#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 Warnickecb9cada2015-12-08 15:45:58 -070036
Dave Barach8a7fb0c2016-07-08 14:44:23 -040037#define SVM_PVT_MHEAP_SIZE (128<<10) /* region's private mheap (128k) */
Ed Warnickecb9cada2015-12-08 15:45:58 -070038
Dave Barach8a7fb0c2016-07-08 14:44:23 -040039typedef 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 Warnickecb9cada2015-12-08 15:45:58 -070061
Dave Barach8a7fb0c2016-07-08 14:44:23 -040062 /* next page:
63 * (64K) clib heap for the region itself
64 *
65 * data_base -> whatever is in this region
66 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070067
68} svm_region_t;
69
Dave Barach8a7fb0c2016-07-08 14:44:23 -040070typedef struct svm_map_region_args_
71{
72 char *root_path; /* NULL means use the truly global arena */
73 char *name;
Dave Barachb3d93da2016-08-03 14:34:38 -040074 u64 baseva;
75 u64 size;
76 u64 pvt_heap_size;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040077 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 Warnickecb9cada2015-12-08 15:45:58 -070083} svm_map_region_args_t;
84
85
86/*
Dave Barach8a7fb0c2016-07-08 14:44:23 -040087 * 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 Warnickecb9cada2015-12-08 15:45:58 -070089 * cover everything we plan to put here.
90 */
91#define SVM_GLOBAL_REGION_BASEVA 0x30000000
Dave Barach8a7fb0c2016-07-08 14:44:23 -040092#define SVM_GLOBAL_REGION_SIZE (64<<20)
Ed Warnickecb9cada2015-12-08 15:45:58 -070093#define SVM_GLOBAL_REGION_NAME "/global_vm"
94
95/*
Dave Barach8a7fb0c2016-07-08 14:44:23 -040096 * Memory shared across individual router instances.
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 */
98#define SVM_OVERLAY_REGION_BASEVA \
99 (SVM_GLOBAL_REGION_BASEVA + SVM_GLOBAL_REGION_SIZE)
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400100#define SVM_OVERLAY_REGION_SIZE (1<<20)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101#define SVM_OVERLAY_REGION_BASENAME "/overlay_vm"
102
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400103typedef struct
104{
105 u8 *subregion_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106} svm_subregion_t;
107
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400108typedef struct
109{
110 svm_subregion_t *subregions; /* subregion pool */
111 uword *name_hash;
112 u8 *root_path;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113} svm_main_region_t;
114
115
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400116void *svm_region_find_or_create (svm_map_region_args_t * a);
117void svm_region_init (void);
118void svm_region_init_chroot (char *root_path);
119void svm_region_init_chroot_uid_gid (char *root_path, int uid, int gid);
Dave Barachc3799992016-08-15 11:12:27 -0400120void svm_region_init_args (svm_map_region_args_t * a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121void svm_region_exit (void);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400122void svm_region_unmap (void *rp_arg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123void svm_client_scan (char *root_path);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400124void svm_client_scan_this_region_nolock (svm_region_t * rp);
125u8 *shm_name_from_svm_map_region_args (svm_map_region_args_t * a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400127static inline void *
128svm_mem_alloc (svm_region_t * rp, uword size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400130 u8 *oldheap;
131 ASSERT (rp->flags & SVM_FLAGS_MHEAP);
132 u8 *rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400134 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 Warnickecb9cada2015-12-08 15:45:58 -0700140}
141
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400142static inline void *
143svm_mem_alloc_aligned_at_offset (svm_region_t * rp,
144 uword size, uword align, uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400146 u8 *oldheap;
147 ASSERT (rp->flags & SVM_FLAGS_MHEAP);
148 u8 *rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400150 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156}
157
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400158static inline void
159svm_mem_free (svm_region_t * rp, void *ptr)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400161 u8 *oldheap;
162 ASSERT (rp->flags & SVM_FLAGS_MHEAP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400164 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169
170}
171
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400172static inline void *
173svm_push_pvt_heap (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400175 u8 *oldheap;
176 oldheap = clib_mem_set_heap (rp->region_heap);
177 return ((void *) oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178}
179
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400180static inline void *
181svm_push_data_heap (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400183 u8 *oldheap;
184 oldheap = clib_mem_set_heap (rp->data_heap);
185 return ((void *) oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186}
187
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400188static inline void
189svm_pop_heap (void *oldheap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400191 clib_mem_set_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192}
193
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400194u8 *format_svm_region (u8 * s, va_list * args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195
196svm_region_t *svm_get_root_rp (void);
197
198#endif /* __included_svm_h__ */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400199
200/*
201 * fd.io coding-style-patch-verification: ON
202 *
203 * Local Variables:
204 * eval: (c-set-style "gnu")
205 * End:
206 */