FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
ssvm.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 
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 struct
48 {
49  /* Spin-lock */
50  volatile u32 lock;
51  volatile u32 owner_pid;
53  u32 tag; /* for debugging */
54 
55  /* The allocation arena */
56  void *heap;
57 
58  /* Segment must be mapped at this address, or no supper */
60  /* The actual mmap size */
64  u8 *name;
65  void *opaque[SSVM_N_OPAQUE];
66 
67  /* Set when the master application thinks it's time to make the donuts */
68  volatile u32 ready;
69 
70  /* Needed to make unique MAC addresses, etc. */
73 
74 typedef struct
75 {
80  u8 *name;
86 
87 always_inline void
89 {
90  if (h->owner_pid == my_pid)
91  {
92  h->recursion_count++;
93  return;
94  }
95 
96  while (__sync_lock_test_and_set (&h->lock, 1))
97  ;
98 
99  h->owner_pid = my_pid;
100  h->recursion_count = 1;
101  h->tag = tag;
102 }
103 
104 always_inline void
106 {
107  while (__sync_lock_test_and_set (&h->lock, 1))
108  ;
109 
110  h->tag = tag;
111 }
112 
113 always_inline void
115 {
116  if (--h->recursion_count == 0)
117  {
118  h->owner_pid = 0;
119  h->tag = 0;
121  h->lock = 0;
122  }
123 }
124 
125 always_inline void
127 {
128  h->tag = 0;
130  h->lock = 0;
131 }
132 
133 static inline void *
135 {
136  u8 *oldheap;
137  oldheap = clib_mem_set_heap (sh->heap);
138  return ((void *) oldheap);
139 }
140 
141 static inline void
142 ssvm_pop_heap (void *oldheap)
143 {
144  clib_mem_set_heap (oldheap);
145 }
146 
147 #define foreach_ssvm_api_error \
148 _(NO_NAME, "No shared segment name", -100) \
149 _(NO_SIZE, "Size not set (master)", -101) \
150 _(CREATE_FAILURE, "Create failed", -102) \
151 _(SET_SIZE, "Set size failed", -103) \
152 _(MMAP, "mmap failed", -104) \
153 _(SLAVE_TIMEOUT, "Slave map timeout", -105)
154 
155 typedef enum
156 {
157 #define _(n,s,c) SSVM_API_ERROR_##n = c,
159 #undef _
161 
162 #define SSVM_API_ERROR_NO_NAME (-10)
163 
164 int ssvm_master_init (ssvm_private_t * ssvm, u32 master_index);
165 int ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds);
166 void ssvm_delete (ssvm_private_t * ssvm);
167 
168 #endif /* __included_ssvm_h__ */
169 
170 /*
171  * fd.io coding-style-patch-verification: ON
172  *
173  * Local Variables:
174  * eval: (c-set-style "gnu")
175  * End:
176  */
u64 ssvm_size
Definition: ssvm.h:77
int ssvm_master_init(ssvm_private_t *ssvm, u32 master_index)
Definition: ssvm.c:19
uword requested_va
Definition: ssvm.h:81
u32 vlib_hw_if_index
Definition: ssvm.h:79
volatile u32 ready
Definition: ssvm.h:68
ssvm_api_error_enum_t
Definition: ssvm.h:155
Fixed length block allocator.
volatile u32 lock
Definition: ssvm.h:50
int ssvm_slave_init(ssvm_private_t *ssvm, int timeout_in_seconds)
Definition: ssvm.c:115
ssvm_shared_header_t * sh
Definition: ssvm.h:76
#define always_inline
Definition: clib.h:92
static void * ssvm_push_heap(ssvm_shared_header_t *sh)
Definition: ssvm.h:134
unsigned long u64
Definition: types.h:89
static void ssvm_pop_heap(void *oldheap)
Definition: ssvm.h:142
#define foreach_ssvm_api_error
Definition: ssvm.h:147
static void ssvm_unlock(ssvm_shared_header_t *h)
Definition: ssvm.h:114
#define SSVM_N_OPAQUE
Definition: ssvm.h:45
u32 per_interface_next_index
Definition: ssvm.h:83
u32 * rx_queue
Definition: ssvm.h:84
vec_header_t h
Definition: buffer.c:282
static void * clib_mem_set_heap(void *heap)
Definition: mem.h:226
u32 my_pid
Definition: ssvm.h:78
unsigned int u32
Definition: types.h:88
Bitmaps built as vectors of machine words.
void ssvm_delete(ssvm_private_t *ssvm)
Definition: ssvm.c:185
u8 * name
Definition: ssvm.h:80
u64 uword
Definition: types.h:112
unsigned char u8
Definition: types.h:56
volatile u32 owner_pid
Definition: ssvm.h:51
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:109
static void ssvm_unlock_non_recursive(ssvm_shared_header_t *h)
Definition: ssvm.h:126
int recursion_count
Definition: ssvm.h:52
int i_am_master
Definition: ssvm.h:82
static void ssvm_lock(ssvm_shared_header_t *h, u32 my_pid, u32 tag)
Definition: ssvm.h:88
static void ssvm_lock_non_recursive(ssvm_shared_header_t *h, u32 tag)
Definition: ssvm.h:105
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".