FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
memif_private.h
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 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 
19 #ifndef _MEMIF_PRIVATE_H_
20 #define _MEMIF_PRIVATE_H_
21 
22 #define _GNU_SOURCE
23 #include <unistd.h>
24 #include <sys/syscall.h>
25 #include <stdint.h>
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <sys/timerfd.h>
29 #include <string.h>
30 
31 #include <memif.h>
32 #include <libmemif.h>
33 
34 #define MEMIF_NAME_LEN 32
35 _Static_assert (strlen (MEMIF_DEFAULT_APP_NAME) <= MEMIF_NAME_LEN,
36  "MEMIF_DEFAULT_APP_NAME max length is 32");
37 
38 #define MEMIF_DEFAULT_SOCKET_PATH "/run/vpp/memif.sock"
39 #define MEMIF_DEFAULT_RING_SIZE 1024
40 #define MEMIF_DEFAULT_LOG2_RING_SIZE 10
41 #define MEMIF_DEFAULT_RX_QUEUES 1
42 #define MEMIF_DEFAULT_TX_QUEUES 1
43 #define MEMIF_DEFAULT_BUFFER_SIZE 2048
44 #define MEMIF_DEFAULT_RECONNECT_PERIOD_SEC 2
45 #define MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC 0
46 
47 #define MEMIF_MAX_M2S_RING 255
48 #define MEMIF_MAX_S2M_RING 255
49 #define MEMIF_MAX_REGION 255
50 #define MEMIF_MAX_LOG2_RING_SIZE 14
51 
52 #define MEMIF_MAX_FDS 512
53 
54 #define memif_min(a,b) (((a) < (b)) ? (a) : (b))
55 
56 #define EXPECT_TRUE(x) __builtin_expect((x),1)
57 #define EXPECT_FALSE(x) __builtin_expect((x),0)
58 
59 #ifdef MEMIF_DBG
60 #define DBG(...) do { \
61  printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
62  printf(__VA_ARGS__); \
63  printf("\n"); \
64  } while (0)
65 #else
66 #define DBG(...)
67 #endif /* MEMIF_DBG */
68 
69 #ifndef HAS_LIB_BSD
70 static inline size_t
71 strlcpy (char *dest, const char *src, size_t len)
72 {
73  const char *s = src;
74  size_t n = len;
75 
76  while (--n > 0)
77  {
78  if ((*dest++ = *s++) == '\0')
79  break;
80  }
81 
82  if (n == 0)
83  {
84  if (len != 0)
85  *dest = '\0';
86  while (*s++)
87  ;
88  }
89 
90  return (s - src - 1);
91 }
92 #else
93 #include <bsd/string.h>
94 #endif
95 
96 typedef enum
97 {
98  MEMIF_SOCKET_TYPE_NONE = 0, /* unassigned, not used by any interface */
99  MEMIF_SOCKET_TYPE_LISTENER, /* listener socket, master interface assigned */
100  MEMIF_SOCKET_TYPE_CLIENT /* client socket, slave interface assigned */
102 
103 typedef struct
104 {
105  void *addr;
106  uint32_t region_size;
107  uint32_t buffer_offset;
108  int fd;
109  uint8_t is_external;
111 
112 typedef struct
113 {
114  memif_ring_t *ring;
115  uint8_t log2_ring_size;
116  uint8_t region;
117  uint32_t offset;
118 
119  uint16_t last_head;
120  uint16_t last_tail;
121 
122  int int_fd;
123 
124  uint64_t int_count;
125  uint32_t next_buf; /* points to next free buffer */
126 } memif_queue_t;
127 
128 typedef struct memif_msg_queue_elt
129 {
131  int fd;
134 
135 struct memif_connection;
136 
138 
139 /* functions called by memif_control_fd_handler */
140 typedef int (memif_fn) (memif_connection_t * conn);
141 
142 typedef struct
143 {
144  uint8_t num_s2m_rings;
145  uint8_t num_m2s_rings;
146  uint16_t buffer_size;
149 
150 struct libmemif_main;
151 
152 typedef struct memif_connection
153 {
154  uint16_t index;
157 
158  int fd;
159 
161 
164  void *private_ctx;
165 
166  /* connection message queue */
168 
172 
173  uint8_t regions_num;
175 
176  uint8_t rx_queues_num;
177  uint8_t tx_queues_num;
180 
181  uint16_t flags;
182 #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
184 
185 typedef struct
186 {
187  int key;
188  void *data_struct;
190 
191 typedef struct
192 {
193  int fd;
194  uint16_t use_count;
196  uint8_t filename[108];
197  /* unique database */
198  struct libmemif_main *lm;
200  void *private_ctx;
201  memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
203 
204 typedef struct libmemif_main
205 {
207  int timerfd;
208  int epfd;
210  struct itimerspec arm, disarm;
211  uint16_t disconn_slaves;
213 
214  void *private_ctx;
215 
217 
222 
226 
229  uint16_t socket_list_len;
236 
238 
239 /* main.c */
240 
241 /* if region doesn't contain shared memory, mmap region, check ring cookie */
243 
244 /* memory map region, initialize rings and queues */
246 
248 
249 /* map errno to memif error code */
250 int memif_syscall_error_handler (int err_code);
251 
253  uint16_t * len);
254 
256  uint16_t len, int key);
257 
258 int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
259 
261 
262 #ifndef __NR_memfd_create
263 #if defined __x86_64__
264 #define __NR_memfd_create 319
265 #elif defined __arm__
266 #define __NR_memfd_create 385
267 #elif defined __aarch64__
268 #define __NR_memfd_create 279
269 #else
270 #error "__NR_memfd_create unknown for this architecture"
271 #endif
272 #endif
273 
274 #ifndef HAVE_MEMFD_CREATE
275 static inline int
276 memfd_create (const char *name, unsigned int flags)
277 {
278  return syscall (__NR_memfd_create, name, flags);
279 }
280 #endif
281 
282 static inline void *
284  uint16_t index)
285 {
286  return (conn->regions[ring->desc[index].region].addr +
287  ring->desc[index].offset);
288 }
289 
290 #ifndef F_LINUX_SPECIFIC_BASE
291 #define F_LINUX_SPECIFIC_BASE 1024
292 #endif
293 
294 #ifndef MFD_ALLOW_SEALING
295 #define MFD_ALLOW_SEALING 0x0002U
296 #endif
297 
298 #ifndef F_ADD_SEALS
299 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
300 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
301 
302 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
303 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
304 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
305 #define F_SEAL_WRITE 0x0008 /* prevent writes */
306 #endif
307 
308 #endif /* _MEMIF_PRIVATE_H_ */
MEMIF_DEFAULT_APP_NAME
#define MEMIF_DEFAULT_APP_NAME
Default name of application using libmemif.
Definition: libmemif.h:28
MEMIF_SOCKET_TYPE_LISTENER
@ MEMIF_SOCKET_TYPE_LISTENER
Definition: memif_private.h:99
libmemif_main::default_socket
memif_socket_handle_t default_socket
Definition: memif_private.h:216
libmemif_main::epfd
int epfd
Definition: memif_private.h:208
memif_socket_t::type
memif_socket_type_t type
Definition: memif_private.h:195
libmemif_main::socket_list_len
uint16_t socket_list_len
Definition: memif_private.h:229
memif_connect1
int memif_connect1(memif_connection_t *c)
Definition: main.c:1848
memif_disconnect_internal
int memif_disconnect_internal(memif_connection_t *c)
Definition: main.c:1635
memif_connection::regions_num
uint8_t regions_num
Definition: memif_private.h:173
libmemif_main::private_ctx
void * private_ctx
Definition: memif_private.h:214
memif_msg_queue_elt
Definition: memif_private.h:128
memif_socket_t::fd
int fd
Definition: memif_private.h:193
libmemif_main::add_external_region
memif_add_external_region_t * add_external_region
Definition: memif_private.h:218
memif_connection::index
uint16_t index
Definition: memif_private.h:154
libmemif_main
Definition: memif_private.h:204
libmemif_main::del_external_region
memif_del_external_region_t * del_external_region
Definition: memif_private.h:220
memif_list_elt_t
Definition: memif_private.h:185
libmemif_main_t
struct libmemif_main libmemif_main_t
memif_list_elt_t::data_struct
void * data_struct
Definition: memif_private.h:188
memif_ring_t::desc
memif_desc_t desc[]
Definition: memif.h:175
libmemif_main::pending_list_len
uint16_t pending_list_len
Definition: memif_private.h:230
memif_free_t
void() memif_free_t(void *ptr)
Memif allocator free.
Definition: libmemif.h:138
memif_connection::fd
int fd
Definition: memif_private.h:158
name
string name[64]
Definition: fib.api:25
libmemif_main::app_name
uint8_t app_name[MEMIF_NAME_LEN]
Definition: memif_private.h:212
string.h
memif_alloc_t
void *() memif_alloc_t(size_t size)
Memif allocator alloc.
Definition: libmemif.h:122
memif_get_external_buffer_offset_t
uint32_t() memif_get_external_buffer_offset_t(void *private_ctx)
Get external buffer offset (optional)
Definition: libmemif.h:196
memif_connection::tx_queues
memif_queue_t * tx_queues
Definition: memif_private.h:179
libmemif_main::poll_cancel_fd
int poll_cancel_fd
Definition: memif_private.h:209
libmemif.h
memif_conn_args_t
Memif connection arguments.
Definition: libmemif.h:295
memif_syscall_error_handler
int memif_syscall_error_handler(int err_code)
Definition: main.c:199
memif_get_buffer
static void * memif_get_buffer(memif_connection_t *conn, memif_ring_t *ring, uint16_t index)
Definition: memif_private.h:283
libmemif_main::get_external_buffer_offset
memif_get_external_buffer_offset_t * get_external_buffer_offset
Definition: memif_private.h:221
memif_conn_run_args_t::log2_ring_size
memif_log2_ring_size_t log2_ring_size
Definition: memif_private.h:147
memif_socket_type_t
memif_socket_type_t
Definition: memif_private.h:96
get_libmemif_main
libmemif_main_t * get_libmemif_main(memif_socket_t *ms)
Definition: main.c:236
memif_msg_queue_elt::fd
int fd
Definition: memif_private.h:131
memif_connection::tx_queues_num
uint8_t tx_queues_num
Definition: memif_private.h:177
memif_list_elt_t::key
int key
Definition: memif_private.h:187
memif_connection::flags
uint16_t flags
Definition: memif_private.h:181
libmemif_main::get_external_region_addr
memif_get_external_region_addr_t * get_external_region_addr
Definition: memif_private.h:219
libmemif_main::alloc
memif_alloc_t * alloc
Definition: memif_private.h:223
strlcpy
static size_t strlcpy(char *dest, const char *src, size_t len)
Definition: memif_private.h:71
memif_region_t::is_external
uint8_t is_external
Definition: memif_private.h:109
memif_connection::remote_disconnect_string
uint8_t remote_disconnect_string[96]
Definition: memif_private.h:171
memif_conn_run_args_t
Definition: memif_private.h:142
memif_desc_t::region
memif_region_index_t region
Definition: memif.h:153
memif_socket_t::use_count
uint16_t use_count
Definition: memif_private.h:194
key
typedef key
Definition: ipsec_types.api:91
MEMIF_SOCKET_TYPE_CLIENT
@ MEMIF_SOCKET_TYPE_CLIENT
Definition: memif_private.h:100
memif_conn_run_args_t::num_m2s_rings
uint8_t num_m2s_rings
Definition: memif_private.h:145
memif_connection::read_fn
memif_fn * read_fn
Definition: memif_private.h:160
memif_queue_t::last_head
uint16_t last_head
Definition: memif_private.h:119
memif_connection::error_fn
memif_fn * error_fn
Definition: memif_private.h:160
memif_connection::on_connect
memif_connection_update_t * on_connect
Definition: memif_private.h:162
memif_region_t::buffer_offset
uint32_t buffer_offset
Definition: memif_private.h:107
libmemif_main::realloc
memif_realloc_t * realloc
Definition: memif_private.h:224
len
u8 len
Definition: ip_types.api:103
memif_add_external_region_t
int() memif_add_external_region_t(void **addr, uint32_t size, int *fd, void *private_ctx)
Add external region.
Definition: libmemif.h:206
memif_region_t
Definition: private.h:107
memif_connection::write_fn
memif_fn * write_fn
Definition: memif_private.h:160
memif_socket_handle_t
void * memif_socket_handle_t
Memif socket handle pointer of type void, pointing to internal structure.
Definition: libmemif.h:115
memif_msg_queue_elt::msg
memif_msg_t msg
Definition: memif_private.h:130
memif_connection_t
struct memif_connection memif_connection_t
Definition: memif_private.h:137
memif_socket_t::interface_list
memif_list_elt_t * interface_list
Definition: memif_private.h:201
libmemif_main::pending_list
memif_list_elt_t * pending_list
Definition: memif_private.h:234
libmemif_main::socket_list
memif_list_elt_t * socket_list
Definition: memif_private.h:233
c
svmdb_client_t * c
Definition: vpp_get_metrics.c:48
memif_connection_update_t
int() memif_connection_update_t(memif_conn_handle_t conn, void *private_ctx)
Memif connection status update (callback function)
Definition: libmemif.h:169
get_list_elt
int get_list_elt(memif_list_elt_t **e, memif_list_elt_t *list, uint16_t len, int key)
Definition: main.c:363
memif_realloc_t
void *() memif_realloc_t(void *ptr, size_t size)
Memif realloc.
Definition: libmemif.h:131
memif_connection::on_interrupt
memif_interrupt_t * on_interrupt
Definition: memif_private.h:163
memif_msg_queue_elt_t
struct memif_msg_queue_elt memif_msg_queue_elt_t
memif_socket_t::lm
struct libmemif_main * lm
Definition: memif_private.h:198
memif_region_t::region_size
uint32_t region_size
Definition: memif_private.h:106
MEMIF_NAME_LEN
#define MEMIF_NAME_LEN
Definition: memif_private.h:34
src
vl_api_address_t src
Definition: gre.api:54
add_list_elt
int add_list_elt(libmemif_main_t *lm, memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len)
Definition: main.c:327
libmemif_main::free
memif_free_t * free
Definition: memif_private.h:225
memif_queue_t::log2_ring_size
uint8_t log2_ring_size
Definition: memif_private.h:115
memif_del_external_region_t
int() memif_del_external_region_t(void *addr, uint32_t size, int fd, void *private_ctx)
Delete external region.
Definition: libmemif.h:229
libmemif_main::control_fd_update
memif_control_fd_update_t * control_fd_update
Definition: memif_private.h:206
memif_connection
Definition: main.c:57
memif_connection::run_args
memif_conn_run_args_t run_args
Definition: memif_private.h:156
libmemif_main
libmemif_main_t libmemif_main
Definition: main.c:68
memif_connection::remote_name
uint8_t remote_name[MEMIF_NAME_LEN]
Definition: memif_private.h:170
libmemif_main::disarm
struct itimerspec arm disarm
Definition: memif_private.h:210
libmemif_main::control_list_len
uint16_t control_list_len
Definition: memif_private.h:227
memif_queue_t
Definition: private.h:123
memif_log2_ring_size_t
uint8_t memif_log2_ring_size_t
Definition: memif.h:68
memif_msg_queue_elt::next
struct memif_msg_queue_elt * next
Definition: memif_private.h:132
memif_control_fd_update_t
int() memif_control_fd_update_t(int fd, uint8_t events, void *private_ctx)
Memif control file descriptor update (callback function)
Definition: libmemif.h:159
libmemif_main::timerfd
int timerfd
Definition: memif_private.h:207
memif_msg_t
Definition: memif.h:127
index
u32 index
Definition: flow_types.api:221
memif_socket_t
Definition: memif_private.h:191
memif_conn_run_args_t::buffer_size
uint16_t buffer_size
Definition: memif_private.h:146
memif_queue_t::int_count
uint64_t int_count
Definition: memif_private.h:124
memif_queue_t::last_tail
uint16_t last_tail
Definition: memif_private.h:120
memif_socket_t::interface_list_len
uint16_t interface_list_len
Definition: memif_private.h:199
memif_get_external_region_addr_t
void *() memif_get_external_region_addr_t(uint32_t size, int fd, void *private_ctx)
Get external region address.
Definition: libmemif.h:218
memif_conn_run_args_t::num_s2m_rings
uint8_t num_s2m_rings
Definition: memif_private.h:144
memif_connection::rx_queues_num
uint8_t rx_queues_num
Definition: memif_private.h:176
memif_connection::regions
memif_region_t * regions
Definition: memif_private.h:174
libmemif_main::disconn_slaves
uint16_t disconn_slaves
Definition: memif_private.h:211
memif_connection_t
Definition: main.c:72
memif_queue_t::offset
uint32_t offset
Definition: memif_private.h:117
memif_fn
int() memif_fn(memif_connection_t *conn)
Definition: memif_private.h:140
memif_interrupt_t
int() memif_interrupt_t(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
Memif interrupt occurred (callback function)
Definition: libmemif.h:179
memif.h
libmemif_main::interrupt_list
memif_list_elt_t * interrupt_list
Definition: memif_private.h:232
memif_connection::rx_queues
memif_queue_t * rx_queues
Definition: memif_private.h:178
memif_connection::msg_queue
memif_msg_queue_elt_t * msg_queue
Definition: memif_private.h:167
libmemif_main::interrupt_list_len
uint16_t interrupt_list_len
Definition: memif_private.h:228
memif_connection::on_disconnect
memif_connection_update_t * on_disconnect
Definition: memif_private.h:162
memif_socket_t::private_ctx
void * private_ctx
Definition: memif_private.h:200
memif_connection::private_ctx
void * private_ctx
Definition: memif_private.h:164
memfd_create
static int memfd_create(const char *name, unsigned int flags)
Definition: memif_private.h:276
memif_region_t::addr
void * addr
Definition: memif_private.h:105
memif_queue_t::region
uint8_t region
Definition: memif_private.h:116
memif_desc_t::offset
memif_region_offset_t offset
Definition: memif.h:155
memif_init_regions_and_queues
int memif_init_regions_and_queues(memif_connection_t *c)
Definition: main.c:2079
libmemif_main::control_list
memif_list_elt_t * control_list
Definition: memif_private.h:231
memif_ring_t
Definition: memif.h:165
memif_connection::remote_if_name
uint8_t remote_if_name[MEMIF_NAME_LEN]
Definition: memif_private.h:169
MEMIF_SOCKET_TYPE_NONE
@ MEMIF_SOCKET_TYPE_NONE
Definition: memif_private.h:98
memif_connection::args
memif_conn_args_t args
Definition: memif_private.h:155
free_list_elt
int free_list_elt(memif_list_elt_t *list, uint16_t len, int key)
Definition: main.c:387
memif_queue_t::next_buf
uint32_t next_buf
Definition: memif_private.h:125
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105