FD.io VPP  v20.05.1-6-gf53edbc3b
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/mheap.h>
37 #include <vppinfra/heap.h>
38 #include <vppinfra/pool.h>
39 #include <vppinfra/format.h>
40 #include <vppinfra/lock.h>
41 
42 #ifndef MMAP_PAGESIZE
43 #define MMAP_PAGESIZE (clib_mem_get_page_size())
44 #endif
45 
46 #define SSVM_N_OPAQUE 7
47 
48 typedef enum ssvm_segment_type_
49 {
53  SSVM_N_SEGMENT_TYPES /**< Private segments */
55 
56 typedef struct
57 {
58  /* Spin-lock */
59  volatile u32 lock;
60  volatile u32 owner_pid;
62  u32 tag; /* for debugging */
63 
64  /* The allocation arena */
65  void *heap;
66 
67  /* Segment must be mapped at this address, or no supper */
69  /* The actual mmap size */
73  u8 *name;
74  void *opaque[SSVM_N_OPAQUE];
75 
76  /* Set when the master application thinks it's time to make the donuts */
77  volatile u32 ready;
78 
81 
82 typedef struct
83 {
88  u8 *name;
89  u8 numa; /**< Numa requested at alloc time */
91 
92  union
93  {
94  int fd; /**< memfd segments */
95  int attach_timeout; /**< shm segments attach timeout (sec) */
96  };
98 
99 always_inline void
101 {
102  if (h->owner_pid == my_pid)
103  {
104  h->recursion_count++;
105  return;
106  }
107 
108  while (clib_atomic_test_and_set (&h->lock))
109  CLIB_PAUSE ();
110 
111  h->owner_pid = my_pid;
112  h->recursion_count = 1;
113  h->tag = tag;
114 }
115 
116 always_inline void
118 {
119  while (clib_atomic_test_and_set (&h->lock))
120  CLIB_PAUSE ();
121 
122  h->tag = tag;
123 }
124 
125 always_inline void
127 {
128  if (--h->recursion_count == 0)
129  {
130  h->owner_pid = 0;
131  h->tag = 0;
133  }
134 }
135 
136 always_inline void
138 {
139  h->tag = 0;
141 }
142 
143 static inline void *
145 {
146  u8 *oldheap;
147  oldheap = clib_mem_set_heap (sh->heap);
148  return ((void *) oldheap);
149 }
150 
151 static inline void
152 ssvm_pop_heap (void *oldheap)
153 {
154  clib_mem_set_heap (oldheap);
155 }
156 
157 static inline void *
159 {
160  u8 *oldheap;
161  void *rv;
162 
163  oldheap = clib_mem_set_heap (ssvm->sh->heap);
164  rv = clib_mem_alloc (size);
165  clib_mem_set_heap (oldheap);
166  return (rv);
167 }
168 
169 #define foreach_ssvm_api_error \
170 _(NO_NAME, "No shared segment name", -100) \
171 _(NO_SIZE, "Size not set (master)", -101) \
172 _(CREATE_FAILURE, "Create failed", -102) \
173 _(SET_SIZE, "Set size failed", -103) \
174 _(MMAP, "mmap failed", -104) \
175 _(SLAVE_TIMEOUT, "Slave map timeout", -105)
176 
177 typedef enum
178 {
179 #define _(n,s,c) SSVM_API_ERROR_##n = c,
181 #undef _
183 
184 #define SSVM_API_ERROR_NO_NAME (-10)
185 
188 void ssvm_delete (ssvm_private_t * ssvm);
189 
192 void ssvm_delete_shm (ssvm_private_t * ssvm);
193 
196 void ssvm_delete_memfd (ssvm_private_t * memfd);
197 
200 void ssvm_delete_private (ssvm_private_t * ssvm);
201 
203 u8 *ssvm_name (const ssvm_private_t * ssvm);
204 
205 #endif /* __included_ssvm_h__ */
206 
207 /*
208  * fd.io coding-style-patch-verification: ON
209  *
210  * Local Variables:
211  * eval: (c-set-style "gnu")
212  * End:
213  */
#define CLIB_PAUSE()
Definition: lock.h:23
int ssvm_master_init(ssvm_private_t *ssvm, ssvm_segment_type_t type)
Definition: ssvm.c:415
uword requested_va
Definition: ssvm.h:86
uword ssvm_size
Definition: ssvm.h:85
Optimized string handling code, including c11-compliant "safe C library" variants.
volatile u32 ready
Definition: ssvm.h:77
ssvm_api_error_enum_t
Definition: ssvm.h:177
Fixed length block allocator.
ssvm_segment_type_t type
Definition: ssvm.h:79
volatile u32 lock
Definition: ssvm.h:59
int ssvm_master_init_shm(ssvm_private_t *ssvm)
Definition: ssvm.c:29
ssvm_shared_header_t * sh
Definition: ssvm.h:84
int ssvm_slave_init_private(ssvm_private_t *ssvm)
Definition: ssvm.c:400
unsigned char u8
Definition: types.h:56
ssvm_segment_type_
Definition: ssvm.h:48
enum ssvm_segment_type_ ssvm_segment_type_t
int ssvm_slave_init(ssvm_private_t *ssvm, ssvm_segment_type_t type)
Definition: ssvm.c:421
static void * ssvm_push_heap(ssvm_shared_header_t *sh)
Definition: ssvm.h:144
unsigned int u32
Definition: types.h:88
int ssvm_master_init_private(ssvm_private_t *ssvm)
Initialize segment in a private heap.
Definition: ssvm.c:340
int attach_timeout
shm segments attach timeout (sec)
Definition: ssvm.h:95
static void ssvm_pop_heap(void *oldheap)
Definition: ssvm.h:152
#define clib_atomic_test_and_set(a)
Definition: atomics.h:42
void ssvm_delete_private(ssvm_private_t *ssvm)
Definition: ssvm.c:407
vl_api_fib_path_type_t type
Definition: fib_types.api:123
#define foreach_ssvm_api_error
Definition: ssvm.h:169
static void ssvm_unlock(ssvm_shared_header_t *h)
Definition: ssvm.h:126
static void * ssvm_mem_alloc(ssvm_private_t *ssvm, uword size)
Definition: ssvm.h:158
#define clib_atomic_release(a)
Definition: atomics.h:43
u64 size
Definition: vhost_user.h:150
#define SSVM_N_OPAQUE
Definition: ssvm.h:46
vec_header_t h
Definition: buffer.c:322
#define always_inline
Definition: ipsec.h:28
ssvm_segment_type_t ssvm_type(const ssvm_private_t *ssvm)
Definition: ssvm.c:433
u8 numa
Numa requested at alloc time.
Definition: ssvm.h:89
void ssvm_delete_shm(ssvm_private_t *ssvm)
Definition: ssvm.c:193
static void * clib_mem_set_heap(void *heap)
Definition: mem.h:268
u32 my_pid
Definition: ssvm.h:87
int ssvm_slave_init_memfd(ssvm_private_t *memfd)
Initialize memfd segment slave.
Definition: ssvm.c:277
int fd
memfd segments
Definition: ssvm.h:94
void ssvm_delete_memfd(ssvm_private_t *memfd)
Definition: ssvm.c:329
Bitmaps built as vectors of machine words.
static void * clib_mem_alloc(uword size)
Definition: mem.h:157
void ssvm_delete(ssvm_private_t *ssvm)
Definition: ssvm.c:427
u8 * name
Definition: ssvm.h:88
u8 * ssvm_name(const ssvm_private_t *ssvm)
Definition: ssvm.c:439
uword ssvm_size
Definition: ssvm.h:70
u64 uword
Definition: types.h:112
int ssvm_master_init_memfd(ssvm_private_t *memfd)
Initialize memfd segment master.
Definition: ssvm.c:217
volatile u32 owner_pid
Definition: ssvm.h:60
int ssvm_slave_init_shm(ssvm_private_t *ssvm)
Definition: ssvm.c:123
static void ssvm_unlock_non_recursive(ssvm_shared_header_t *h)
Definition: ssvm.h:137
Private segments.
Definition: ssvm.h:53
int recursion_count
Definition: ssvm.h:61
int i_am_master
Definition: ssvm.h:90
static void ssvm_lock(ssvm_shared_header_t *h, u32 my_pid, u32 tag)
Definition: ssvm.h:100
static void ssvm_lock_non_recursive(ssvm_shared_header_t *h, u32 tag)
Definition: ssvm.h:117
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".