FD.io VPP  v21.01.1
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 typedef enum
70 {
71  MEMIF_SOCKET_TYPE_NONE = 0, /* unassigned, not used by any interface */
72  MEMIF_SOCKET_TYPE_LISTENER, /* listener socket, master interface assigned */
73  MEMIF_SOCKET_TYPE_CLIENT /* client socket, slave interface assigned */
75 
76 typedef struct
77 {
78  void *addr;
79  uint32_t region_size;
80  uint32_t buffer_offset;
81  int fd;
82  uint8_t is_external;
84 
85 typedef struct
86 {
87  memif_ring_t *ring;
88  uint8_t log2_ring_size;
89  uint8_t region;
90  uint32_t offset;
91 
92  uint16_t last_head;
93  uint16_t last_tail;
94 
95  int int_fd;
96 
97  uint64_t int_count;
98  uint32_t alloc_bufs;
100 
101 typedef struct memif_msg_queue_elt
102 {
104  int fd;
107 
108 struct memif_connection;
109 
111 
112 /* functions called by memif_control_fd_handler */
113 typedef int (memif_fn) (memif_connection_t * conn);
114 
115 typedef struct
116 {
117  uint8_t num_s2m_rings;
118  uint8_t num_m2s_rings;
119  uint16_t buffer_size;
122 
123 struct libmemif_main;
124 
125 typedef struct memif_connection
126 {
127  uint16_t index;
130 
131  int fd;
132 
133  memif_fn *write_fn, *read_fn, *error_fn;
134 
137  void *private_ctx;
138 
139  /* connection message queue */
141 
142  uint8_t remote_if_name[MEMIF_NAME_LEN];
143  uint8_t remote_name[MEMIF_NAME_LEN];
144  uint8_t remote_disconnect_string[96];
145 
146  uint8_t regions_num;
148 
149  uint8_t rx_queues_num;
150  uint8_t tx_queues_num;
153 
154  uint16_t flags;
155 #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
157 
158 typedef struct
159 {
160  int key;
161  void *data_struct;
163 
164 typedef struct
165 {
166  int fd;
167  uint16_t use_count;
168  memif_socket_type_t type;
169  uint8_t *filename;
170  /* unique database */
171  struct libmemif_main *lm;
173  void *private_ctx;
174  memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
176 
177 typedef struct libmemif_main
178 {
180  int timerfd;
181  int epfd;
183  struct itimerspec arm, disarm;
184  uint16_t disconn_slaves;
185  uint8_t app_name[MEMIF_NAME_LEN];
186 
187  void *private_ctx;
188 
190 
195 
199 
202  uint16_t socket_list_len;
209 
211 
212 /* main.c */
213 
214 /* if region doesn't contain shared memory, mmap region, check ring cookie */
216 
217 /* memory map region, initialize rings and queues */
219 
221 
222 /* map errno to memif error code */
223 int memif_syscall_error_handler (int err_code);
224 
226  uint16_t * len);
227 
229  uint16_t len, int key);
230 
231 int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
232 
234 
235 #ifndef __NR_memfd_create
236 #if defined __x86_64__
237 #define __NR_memfd_create 319
238 #elif defined __arm__
239 #define __NR_memfd_create 385
240 #elif defined __aarch64__
241 #define __NR_memfd_create 279
242 #else
243 #error "__NR_memfd_create unknown for this architecture"
244 #endif
245 #endif
246 
247 #ifndef HAVE_MEMFD_CREATE
248 static inline int
249 memfd_create (const char *name, unsigned int flags)
250 {
251  return syscall (__NR_memfd_create, name, flags);
252 }
253 #endif
254 
255 static inline void *
257  uint16_t index)
258 {
259  return (conn->regions[ring->desc[index].region].addr +
260  ring->desc[index].offset);
261 }
262 
263 #ifndef F_LINUX_SPECIFIC_BASE
264 #define F_LINUX_SPECIFIC_BASE 1024
265 #endif
266 
267 #ifndef MFD_ALLOW_SEALING
268 #define MFD_ALLOW_SEALING 0x0002U
269 #endif
270 
271 #ifndef F_ADD_SEALS
272 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
273 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
274 
275 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
276 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
277 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
278 #define F_SEAL_WRITE 0x0008 /* prevent writes */
279 #endif
280 
281 #endif /* _MEMIF_PRIVATE_H_ */
uint8_t log2_ring_size
Definition: memif_private.h:88
int memif_init_regions_and_queues(memif_connection_t *c)
Definition: main.c:2113
memif_queue_t * rx_queues
uint8_t * filename
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105
Optimized string handling code, including c11-compliant "safe C library" variants.
memif_alloc_t * alloc
int get_list_elt(memif_list_elt_t **e, memif_list_elt_t *list, uint16_t len, int key)
Definition: main.c:366
#define MEMIF_NAME_LEN
Definition: memif_private.h:34
uint32_t alloc_bufs
Definition: memif_private.h:98
memif_conn_args_t args
uint64_t int_count
Definition: memif_private.h:97
memif_del_external_region_t * del_external_region
memif_fn * write_fn
uint16_t last_tail
Definition: memif_private.h:93
#define MEMIF_DEFAULT_APP_NAME
Default name of application using libmemif.
Definition: libmemif.h:28
int() memif_fn(memif_connection_t *conn)
uint32_t offset
Definition: memif_private.h:90
memif_list_elt_t * interface_list
memif_socket_type_t type
int on_connect(memif_conn_handle_t conn, void *private_ctx)
Definition: main.c:177
memif_connection_update_t * on_disconnect
libmemif_main_t libmemif_main
Definition: main.c:68
int free_list_elt(memif_list_elt_t *list, uint16_t len, int key)
Definition: main.c:390
struct libmemif_main * lm
uint8_t region
Definition: memif_private.h:89
int add_list_elt(libmemif_main_t *lm, memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len)
Definition: main.c:330
libmemif_main_t * get_libmemif_main(memif_socket_t *ms)
Definition: main.c:239
memif_region_index_t region
Definition: memif.h:153
struct memif_msg_queue_elt memif_msg_queue_elt_t
memif_desc_t desc[0]
Definition: memif.h:175
uint8_t is_external
Definition: memif_private.h:82
memif_list_elt_t * pending_list
int() memif_add_external_region_t(void **addr, uint32_t size, int *fd, void *private_ctx)
Add external region.
Definition: libmemif.h:204
struct memif_connection memif_connection_t
uint8_t memif_log2_ring_size_t
Definition: memif.h:68
void * memif_socket_handle_t
Memif socket handle pointer of type void, pointing to internal structure.
Definition: libmemif.h:113
int memif_connect1(memif_connection_t *c)
Definition: main.c:1880
int() memif_del_external_region_t(void *addr, uint32_t size, int fd, void *private_ctx)
Delete external region.
Definition: libmemif.h:227
memif_queue_t * tx_queues
struct memif_msg_queue_elt * next
int() memif_interrupt_t(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
Memif interrupt occurred (callback function)
Definition: libmemif.h:177
int() memif_connection_update_t(memif_conn_handle_t conn, void *private_ctx)
Memif connection status update (callback function)
Definition: libmemif.h:167
memif_list_elt_t * interrupt_list
uint16_t pending_list_len
uint32_t buffer_offset
Definition: memif_private.h:80
u8 len
Definition: ip_types.api:103
uint16_t use_count
svmdb_client_t * c
void() memif_free_t(void *ptr)
Memif allocator free.
Definition: libmemif.h:136
int memif_disconnect_internal(memif_connection_t *c)
Definition: main.c:1665
int memif_syscall_error_handler(int err_code)
Definition: main.c:202
struct libmemif_main libmemif_main_t
memif_list_elt_t * control_list
memif_socket_handle_t default_socket
memif_msg_queue_elt_t * msg_queue
memif_free_t * free
uint16_t interrupt_list_len
string name[64]
Definition: ip.api:44
uint16_t control_list_len
void *() memif_realloc_t(void *ptr, size_t size)
Memif realloc.
Definition: libmemif.h:129
static int memfd_create(const char *name, unsigned int flags)
memif_realloc_t * realloc
memif_region_t * regions
uint32_t region_size
Definition: memif_private.h:79
uint32_t() memif_get_external_buffer_offset_t(void *private_ctx)
Get external buffer offset (optional)
Definition: libmemif.h:194
static void * memif_get_buffer(memif_connection_t *conn, memif_ring_t *ring, uint16_t index)
memif_get_external_region_addr_t * get_external_region_addr
memif_conn_run_args_t run_args
memif_region_offset_t offset
Definition: memif.h:155
memif_interrupt_t * on_interrupt
typedef key
Definition: ipsec_types.api:86
memif_add_external_region_t * add_external_region
memif_get_external_buffer_offset_t * get_external_buffer_offset
uint16_t last_head
Definition: memif_private.h:92
memif_socket_type_t
Definition: memif_private.h:69
u32 index
Definition: flow_types.api:221
void *() memif_alloc_t(size_t size)
Memif allocator alloc.
Definition: libmemif.h:120
void *() memif_get_external_region_addr_t(uint32_t size, int fd, void *private_ctx)
Get external region address.
Definition: libmemif.h:216
memif_log2_ring_size_t log2_ring_size
memif_control_fd_update_t * control_fd_update
memif_list_elt_t * socket_list
uint16_t socket_list_len
uint16_t disconn_slaves
void * private_ctx
Memif connection arguments.
Definition: libmemif.h:293
uint16_t interface_list_len
int() memif_control_fd_update_t(int fd, uint8_t events, void *private_ctx)
Memif control file descriptor update (callback function)
Definition: libmemif.h:157