FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
ssvm.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __included_ssvm_h__
16 #define __included_ssvm_h__
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <sys/mman.h>
22 #include <sys/stat.h>
23 #include <netinet/in.h>
24 #include <signal.h>
25 #include <pthread.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <vppinfra/clib.h>
31 #include <vppinfra/vec.h>
32 #include <vppinfra/hash.h>
33 #include <vppinfra/bitmap.h>
34 #include <vppinfra/fifo.h>
35 #include <vppinfra/time.h>
36 #include <vppinfra/heap.h>
37 #include <vppinfra/pool.h>
38 #include <vppinfra/format.h>
39 #include <vppinfra/lock.h>
40 
41 #ifndef MMAP_PAGESIZE
42 #define MMAP_PAGESIZE (clib_mem_get_page_size())
43 #endif
44 
45 #define SSVM_N_OPAQUE 7
46 
47 typedef enum ssvm_segment_type_
48 {
52  SSVM_N_SEGMENT_TYPES /**< Private segments */
54 
55 typedef struct
56 {
57  /* Spin-lock */
58  volatile u32 lock;
59  volatile u32 owner_pid;
61  u32 tag; /* for debugging */
62 
63  /* The allocation arena */
64  void *heap;
65 
66  /* Segment must be mapped at this address, or no supper */
68  /* The actual mmap size */
72  u8 *name;
73  void *opaque[SSVM_N_OPAQUE];
74 
75  /* Set when server init done */
76  volatile u32 ready;
77 
80 
81 typedef struct
82 {
87  u8 *name;
88  u8 numa; /**< UNUSED: numa requested at alloc time */
89  int is_server;
90 
91  union
92  {
93  int fd; /**< memfd segments */
94  int attach_timeout; /**< shm segments attach timeout (sec) */
95  };
97 
98 always_inline void
100 {
101  if (h->owner_pid == my_pid)
102  {
103  h->recursion_count++;
104  return;
105  }
106 
107  while (clib_atomic_test_and_set (&h->lock))
108  CLIB_PAUSE ();
109 
110  h->owner_pid = my_pid;
111  h->recursion_count = 1;
112  h->tag = tag;
113 }
114 
115 always_inline void
117 {
118  while (clib_atomic_test_and_set (&h->lock))
119  CLIB_PAUSE ();
120 
121  h->tag = tag;
122 }
123 
124 always_inline void
126 {
127  if (--h->recursion_count == 0)
128  {
129  h->owner_pid = 0;
130  h->tag = 0;
131  clib_atomic_release (&h->lock);
132  }
133 }
134 
135 always_inline void
137 {
138  h->tag = 0;
139  clib_atomic_release (&h->lock);
140 }
141 
142 static inline void *
144 {
145  clib_mem_heap_t *oldheap;
146  oldheap = clib_mem_set_heap (sh->heap);
147  return ((void *) oldheap);
148 }
149 
150 static inline void
151 ssvm_pop_heap (void *oldheap)
152 {
153  clib_mem_set_heap (oldheap);
154 }
155 
156 static inline void *
158 {
159  clib_mem_heap_t *oldheap;
160  void *rv;
161 
162  oldheap = clib_mem_set_heap (ssvm->sh->heap);
163  rv = clib_mem_alloc (size);
164  clib_mem_set_heap (oldheap);
165  return (rv);
166 }
167 
168 #define foreach_ssvm_api_error \
169 _(NO_NAME, "No shared segment name", -100) \
170 _(NO_SIZE, "Size not set (server)", -101) \
171 _(CREATE_FAILURE, "Create failed", -102) \
172 _(SET_SIZE, "Set size failed", -103) \
173 _(MMAP, "mmap failed", -104) \
174 _(CLIENT_TIMEOUT, "Client map timeout", -105)
175 
176 typedef enum
177 {
178 #define _(n,s,c) SSVM_API_ERROR_##n = c,
180 #undef _
182 
183 #define SSVM_API_ERROR_NO_NAME (-10)
184 
187 void ssvm_delete (ssvm_private_t * ssvm);
188 
191 void ssvm_delete_shm (ssvm_private_t * ssvm);
192 
195 void ssvm_delete_memfd (ssvm_private_t * memfd);
196 
199 void ssvm_delete_private (ssvm_private_t * ssvm);
200 
202 u8 *ssvm_name (const ssvm_private_t * ssvm);
203 
204 #endif /* __included_ssvm_h__ */
205 
206 /*
207  * fd.io coding-style-patch-verification: ON
208  *
209  * Local Variables:
210  * eval: (c-set-style "gnu")
211  * End:
212  */
ssvm_private_t::requested_va
uword requested_va
Definition: ssvm.h:85
ssvm_server_init_private
int ssvm_server_init_private(ssvm_private_t *ssvm)
Initialize segment in a private heap.
Definition: ssvm.c:361
ssvm_shared_header_t::tag
u32 tag
Definition: ssvm.h:61
ssvm_shared_header_t::name
u8 * name
Definition: ssvm.h:72
ssvm_delete_memfd
void ssvm_delete_memfd(ssvm_private_t *memfd)
Definition: ssvm.c:347
ssvm_shared_header_t::ssvm_size
uword ssvm_size
Definition: ssvm.h:69
ssvm_pop_heap
static void ssvm_pop_heap(void *oldheap)
Definition: ssvm.h:151
ssvm_private_t::ssvm_size
uword ssvm_size
Definition: ssvm.h:84
bitmap.h
clib.h
string.h
ssvm_shared_header_t::heap
void * heap
Definition: ssvm.h:64
ssvm_push_heap
static void * ssvm_push_heap(ssvm_shared_header_t *sh)
Definition: ssvm.h:143
SSVM_N_SEGMENT_TYPES
@ SSVM_N_SEGMENT_TYPES
Private segments.
Definition: ssvm.h:52
ssvm_type
ssvm_segment_type_t ssvm_type(const ssvm_private_t *ssvm)
Definition: ssvm.c:451
fifo.h
ssvm_lock_non_recursive
static void ssvm_lock_non_recursive(ssvm_shared_header_t *h, u32 tag)
Definition: ssvm.h:116
ssvm_client_init_private
int ssvm_client_init_private(ssvm_private_t *ssvm)
Definition: ssvm.c:418
foreach_ssvm_api_error
#define foreach_ssvm_api_error
Definition: ssvm.h:168
ssvm_private_t::sh
ssvm_shared_header_t * sh
Definition: ssvm.h:83
ssvm_shared_header_t::ssvm_va
uword ssvm_va
Definition: ssvm.h:67
ssvm_shared_header_t::recursion_count
int recursion_count
Definition: ssvm.h:60
ssvm_unlock
static void ssvm_unlock(ssvm_shared_header_t *h)
Definition: ssvm.h:125
ssvm_shared_header_t::owner_pid
volatile u32 owner_pid
Definition: ssvm.h:59
clib_atomic_release
#define clib_atomic_release(a)
Definition: atomics.h:46
SSVM_SEGMENT_MEMFD
@ SSVM_SEGMENT_MEMFD
Definition: ssvm.h:50
h
h
Definition: flowhash_template.h:372
ssvm_segment_type_t
enum ssvm_segment_type_ ssvm_segment_type_t
ssvm_unlock_non_recursive
static void ssvm_unlock_non_recursive(ssvm_shared_header_t *h)
Definition: ssvm.h:136
ssvm_server_init
int ssvm_server_init(ssvm_private_t *ssvm, ssvm_segment_type_t type)
Definition: ssvm.c:433
ssvm_name
u8 * ssvm_name(const ssvm_private_t *ssvm)
Definition: ssvm.c:457
ssvm_api_error_enum_t
ssvm_api_error_enum_t
Definition: ssvm.h:176
lock.h
clib_atomic_test_and_set
#define clib_atomic_test_and_set(a)
Definition: atomics.h:45
ssvm_client_init
int ssvm_client_init(ssvm_private_t *ssvm, ssvm_segment_type_t type)
Definition: ssvm.c:439
uword
u64 uword
Definition: types.h:112
pool.h
Fixed length block allocator. Pools are built from clib vectors and bitmaps. Use pools when repeatedl...
ssvm_server_init_memfd
int ssvm_server_init_memfd(ssvm_private_t *memfd)
Initialize memfd segment server.
Definition: ssvm.c:217
time.h
ssvm_private_t::attach_timeout
int attach_timeout
shm segments attach timeout (sec)
Definition: ssvm.h:94
format.h
ssvm_segment_type_
ssvm_segment_type_
Definition: ssvm.h:47
ssvm_shared_header_t::client_pid
u32 client_pid
Definition: ssvm.h:71
SSVM_SEGMENT_PRIVATE
@ SSVM_SEGMENT_PRIVATE
Definition: ssvm.h:51
size
u32 size
Definition: vhost_user.h:125
SSVM_SEGMENT_SHM
@ SSVM_SEGMENT_SHM
Definition: ssvm.h:49
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
ssvm_shared_header_t::server_pid
u32 server_pid
Definition: ssvm.h:70
ssvm_private_t::numa
u8 numa
UNUSED: numa requested at alloc time.
Definition: ssvm.h:88
ssvm_server_init_shm
int ssvm_server_init_shm(ssvm_private_t *ssvm)
Definition: ssvm.c:29
ssvm_private_t::my_pid
u32 my_pid
Definition: ssvm.h:86
u32
unsigned int u32
Definition: types.h:88
SSVM_N_OPAQUE
#define SSVM_N_OPAQUE
Definition: ssvm.h:45
ssvm_shared_header_t::type
ssvm_segment_type_t type
Definition: ssvm.h:78
CLIB_PAUSE
#define CLIB_PAUSE()
Definition: lock.h:23
ssvm_mem_alloc
static void * ssvm_mem_alloc(ssvm_private_t *ssvm, uword size)
Definition: ssvm.h:157
ssvm_private_t::is_server
int is_server
Definition: ssvm.h:89
ssvm_client_init_memfd
int ssvm_client_init_memfd(ssvm_private_t *memfd)
Initialize memfd segment client.
Definition: ssvm.c:292
vec.h
ssvm_shared_header_t::ready
volatile u32 ready
Definition: ssvm.h:76
hash.h
u8
unsigned char u8
Definition: types.h:56
ssvm_private_t::name
u8 * name
Definition: ssvm.h:87
ssvm_private_t::fd
int fd
memfd segments
Definition: ssvm.h:93
ssvm_lock
static void ssvm_lock(ssvm_shared_header_t *h, u32 my_pid, u32 tag)
Definition: ssvm.h:99
rv
int __clib_unused rv
Definition: application.c:491
ssvm_private_t
Definition: ssvm.h:81
ssvm_client_init_shm
int ssvm_client_init_shm(ssvm_private_t *ssvm)
Definition: ssvm.c:120
ssvm_shared_header_t::lock
volatile u32 lock
Definition: ssvm.h:58
ssvm_delete
void ssvm_delete(ssvm_private_t *ssvm)
Definition: ssvm.c:445
clib_mem_set_heap
static clib_mem_heap_t * clib_mem_set_heap(clib_mem_heap_t *heap)
Definition: mem.h:365
clib_mem_heap_t
Definition: mem.h:107
clib_mem_alloc
static void * clib_mem_alloc(uword size)
Definition: mem.h:253
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
ssvm_delete_shm
void ssvm_delete_shm(ssvm_private_t *ssvm)
Definition: ssvm.c:190
heap.h
ssvm_shared_header_t
Definition: ssvm.h:55
ssvm_delete_private
void ssvm_delete_private(ssvm_private_t *ssvm)
Definition: ssvm.c:425