blob: 0e19ffd3f761f7707314d4d213fd091567c1eb01 [file] [log] [blame]
Klement Sekera58eb8662017-06-09 06:06:49 +02001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2009 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
18#ifndef __included_svm_common_h__
19#define __included_svm_common_h__
20
21#include <stdarg.h>
Tom Jonescaf9d4f2024-01-26 17:32:02 +000022#ifdef __FreeBSD__
23#include <stdint.h>
24#endif /* __FreeBSD__ */
Klement Sekera58eb8662017-06-09 06:06:49 +020025#include <pthread.h>
Tom Jones9a97fb82024-01-30 09:19:13 +000026#ifdef __linux__
Nathan Mooscf5ba262021-01-15 14:03:01 -080027#include <sys/user.h>
Tom Jones9a97fb82024-01-30 09:19:13 +000028#endif /* __linux__ */
Benoît Ganne6531cf52022-09-30 17:13:33 +020029#include <vppinfra/clib.h>
Klement Sekera58eb8662017-06-09 06:06:49 +020030#include <vppinfra/types.h>
31
32#define SVM_VERSION ((1<<16) | 1) /* set to declare region ready. */
33
34#define SVM_FLAGS_MHEAP (1<<0) /* region contains an mheap */
35#define SVM_FLAGS_FILE (1<<1) /* region backed by one or more files */
36#define SVM_FLAGS_NODATA (1<<2) /* region will be further subdivided */
37#define SVM_FLAGS_NEED_DATA_INIT (1<<3)
38
39#define SVM_PVT_MHEAP_SIZE (128<<10) /* region's private mheap (128k) */
40
41typedef struct svm_region_
42{
43 volatile uword version;
44 pthread_mutex_t mutex;
45 pthread_cond_t condvar;
46 int mutex_owner_pid; /* in case of trouble */
47 int mutex_owner_tag;
48 uword flags;
49 uword virtual_base; /* base of the region object */
50 uword virtual_size;
51 void *region_heap;
52 void *data_base; /* data portion base address */
53 void *data_heap; /* data heap, if any */
54 volatile void *user_ctx; /* user context pointer */
55 /* stuff allocated in the region's heap */
56 uword bitmap_size; /* nbits in virtual alloc bitmap */
57 uword *bitmap; /* the bitmap */
58 char *region_name;
59 char *backing_file;
60 char **filenames;
61 uword *client_pids;
62 /* pad */
63
64 /* next page:
65 * (64K) clib heap for the region itself
66 *
67 * data_base -> whatever is in this region
68 */
69
70} svm_region_t;
71
72typedef struct svm_map_region_args_
73{
74 const char *root_path; /* NULL means use the truly global arena */
75 const char *name;
David Johnsond9818dd2018-12-14 14:53:41 -050076 uword baseva;
Klement Sekera58eb8662017-06-09 06:06:49 +020077 u64 size;
78 u64 pvt_heap_size;
79 uword flags;
80 char *backing_file;
81 uword backing_mmap_size;
82 /* uid, gid to own the svm region(s) */
83 int uid;
84 int gid;
85} svm_map_region_args_t;
86
David Johnsond9818dd2018-12-14 14:53:41 -050087/*
88 * Memory mapped to high addresses for session/vppcom/vcl/etc...
89 */
90#if __WORDSIZE == 64
Benoît Ganne9fb6d402019-04-15 15:28:21 +020091#ifdef CLIB_SANITIZE_ADDR
Benoît Gannee3e9d8a2020-11-17 17:58:59 +010092#define HIGH_SEGMENT_BASEVA 0x300000000000 /* DO NOT CHANGE THIS: YOU'LL BREAK ASAN */
Benoît Ganne9fb6d402019-04-15 15:28:21 +020093#else /* CLIB_SANITIZE_ADDR */
Nathan Skrzypczakf260eb92020-11-10 16:57:49 +010094#define HIGH_SEGMENT_BASEVA (128ULL << 30) /* 128GB */
Benoît Ganne9fb6d402019-04-15 15:28:21 +020095#endif /* CLIB_SANITIZE_ADDR */
David Johnsond9818dd2018-12-14 14:53:41 -050096#elif __WORDSIZE == 32
97#define HIGH_SEGMENT_BASEVA (3584UL << 20) /* 3.5GB */
98#else
99#error "unknown __WORDSIZE"
100#endif
Klement Sekera58eb8662017-06-09 06:06:49 +0200101
102/*
103 * Memory shared across all router instances. Packet buffers, etc
104 * Base should be "out of the way," and size should be big enough to
105 * cover everything we plan to put here.
106 */
Klement Sekera58eb8662017-06-09 06:06:49 +0200107#define SVM_GLOBAL_REGION_SIZE (64<<20)
108#define SVM_GLOBAL_REGION_NAME "/global_vm"
Damjan Marionaec8f892018-01-08 16:35:35 +0100109u64 svm_get_global_region_base_va ();
Klement Sekera58eb8662017-06-09 06:06:49 +0200110
111/*
112 * Memory shared across individual router instances.
113 */
114#define SVM_OVERLAY_REGION_BASEVA \
115 (SVM_GLOBAL_REGION_BASEVA + SVM_GLOBAL_REGION_SIZE)
116#define SVM_OVERLAY_REGION_SIZE (1<<20)
117#define SVM_OVERLAY_REGION_BASENAME "/overlay_vm"
118
119typedef struct
120{
121 u8 *subregion_name;
122} svm_subregion_t;
123
124typedef struct
125{
126 svm_subregion_t *subregions; /* subregion pool */
127 uword *name_hash;
128 u8 *root_path;
Dave Wallace19296112017-08-31 15:54:11 -0400129 int uid;
130 int gid;
Klement Sekera58eb8662017-06-09 06:06:49 +0200131} svm_main_region_t;
132
133
134void *svm_region_find_or_create (svm_map_region_args_t * a);
135void svm_region_init (void);
Dave Barach59b25652017-09-10 15:04:27 -0400136void svm_region_init_mapped_region (svm_map_region_args_t * a,
137 svm_region_t * rp);
Ole Troan3cdc25f2017-08-17 11:07:33 +0200138int svm_region_init_chroot (const char *root_path);
Klement Sekera58eb8662017-06-09 06:06:49 +0200139void svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid);
140void svm_region_init_args (svm_map_region_args_t * a);
141void svm_region_exit (void);
Florin Corasd6c30d92018-01-29 05:11:24 -0800142void svm_region_exit_client (void);
Klement Sekera58eb8662017-06-09 06:06:49 +0200143void svm_region_unmap (void *rp_arg);
Florin Corasd6c30d92018-01-29 05:11:24 -0800144void svm_region_unmap_client (void *rp_arg);
Klement Sekera58eb8662017-06-09 06:06:49 +0200145void svm_client_scan (const char *root_path);
146void svm_client_scan_this_region_nolock (svm_region_t * rp);
147u8 *shm_name_from_svm_map_region_args (svm_map_region_args_t * a);
148u8 *format_svm_region (u8 * s, va_list * args);
149
150svm_region_t *svm_get_root_rp (void);
151
152#endif /* __included_svm_common_h__ */
153
154/*
155 * fd.io coding-style-patch-verification: ON
156 *
157 * Local Variables:
158 * eval: (c-set-style "gnu")
159 * End:
160 */