FD.io VPP  v19.04.4-rc0-5-ge88582fac
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_DIR "/run/vpp"
39 #define MEMIF_DEFAULT_SOCKET_FILENAME "memif.sock"
40 #define MEMIF_DEFAULT_RING_SIZE 1024
41 #define MEMIF_DEFAULT_LOG2_RING_SIZE 10
42 #define MEMIF_DEFAULT_RX_QUEUES 1
43 #define MEMIF_DEFAULT_TX_QUEUES 1
44 #define MEMIF_DEFAULT_BUFFER_SIZE 2048
45 #define MEMIF_DEFAULT_RECONNECT_PERIOD_SEC 2
46 #define MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC 0
47 
48 #define MEMIF_MAX_M2S_RING 255
49 #define MEMIF_MAX_S2M_RING 255
50 #define MEMIF_MAX_REGION 255
51 #define MEMIF_MAX_LOG2_RING_SIZE 14
52 
53 #define MEMIF_MAX_FDS 512
54 
55 #define memif_min(a,b) (((a) < (b)) ? (a) : (b))
56 
57 #define EXPECT_TRUE(x) __builtin_expect((x),1)
58 #define EXPECT_FALSE(x) __builtin_expect((x),0)
59 
60 #ifdef MEMIF_DBG
61 #define DBG(...) do { \
62  printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
63  printf(__VA_ARGS__); \
64  printf("\n"); \
65  } while (0)
66 #else
67 #define DBG(...)
68 #endif /* MEMIF_DBG */
69 
70 typedef struct
71 {
72  void *addr;
73  uint32_t region_size;
74  uint32_t buffer_offset;
75  int fd;
76  uint8_t is_external;
78 
79 typedef struct
80 {
81  memif_ring_t *ring;
82  uint8_t log2_ring_size;
83  uint8_t region;
84  uint32_t offset;
85 
86  uint16_t last_head;
87  uint16_t last_tail;
88 
89  int int_fd;
90 
91  uint64_t int_count;
92  uint32_t alloc_bufs;
94 
95 typedef struct memif_msg_queue_elt
96 {
98  int fd;
101 
102 struct memif_connection;
103 
105 
106 /* functions called by memif_control_fd_handler */
107 typedef int (memif_fn) (memif_connection_t * conn);
108 
109 typedef struct
110 {
111  uint8_t num_s2m_rings;
112  uint8_t num_m2s_rings;
113  uint16_t buffer_size;
116 
117 typedef struct memif_connection
118 {
119  uint16_t index;
122 
123  int fd;
125 
126  memif_fn *write_fn, *read_fn, *error_fn;
127 
130  void *private_ctx;
131 
132  /* connection message queue */
134 
135  uint8_t remote_if_name[MEMIF_NAME_LEN];
136  uint8_t remote_name[MEMIF_NAME_LEN];
137  uint8_t remote_disconnect_string[96];
138 
139  uint8_t regions_num;
141 
142  uint8_t rx_queues_num;
143  uint8_t tx_queues_num;
146 
147  uint16_t flags;
148 #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
150 
151 typedef struct
152 {
153  int key;
154  void *data_struct;
156 
157 typedef struct
158 {
159  int fd;
160  uint16_t use_count;
161  uint8_t *filename;
163  memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
165 
166 typedef struct
167 {
169  int timerfd;
170  struct itimerspec arm, disarm;
171  uint16_t disconn_slaves;
172  uint8_t app_name[MEMIF_NAME_LEN];
173 
178 
182 
192 
194 extern int memif_epfd;
195 
196 /* main.c */
197 
198 /* if region doesn't contain shared memory, mmap region, check ring cookie */
200 
201 /* memory map region, initalize rings and queues */
203 
205 
206 /* map errno to memif error code */
207 int memif_syscall_error_handler (int err_code);
208 
210  uint16_t * len);
211 
213  uint16_t len, int key);
214 
215 int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
216 
217 #ifndef __NR_memfd_create
218 #if defined __x86_64__
219 #define __NR_memfd_create 319
220 #elif defined __arm__
221 #define __NR_memfd_create 385
222 #elif defined __aarch64__
223 #define __NR_memfd_create 279
224 #else
225 #error "__NR_memfd_create unknown for this architecture"
226 #endif
227 #endif
228 
229 #ifndef HAVE_MEMFD_CREATE
230 static inline int
231 memfd_create (const char *name, unsigned int flags)
232 {
233  return syscall (__NR_memfd_create, name, flags);
234 }
235 #endif
236 
237 static inline void *
239  uint16_t index)
240 {
241  return (conn->regions[ring->desc[index].region].addr +
242  ring->desc[index].offset);
243 }
244 
245 #ifndef F_LINUX_SPECIFIC_BASE
246 #define F_LINUX_SPECIFIC_BASE 1024
247 #endif
248 
249 #ifndef MFD_ALLOW_SEALING
250 #define MFD_ALLOW_SEALING 0x0002U
251 #endif
252 
253 #ifndef F_ADD_SEALS
254 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
255 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
256 
257 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
258 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
259 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
260 #define F_SEAL_WRITE 0x0008 /* prevent writes */
261 #endif
262 
263 #endif /* _MEMIF_PRIVATE_H_ */
uint8_t log2_ring_size
Definition: memif_private.h:82
int memif_init_regions_and_queues(memif_connection_t *c)
Definition: main.c:1576
u32 flags
Definition: vhost_user.h:115
memif_queue_t * rx_queues
uint8_t * filename
Optimized string handling code, including c11-compliant "safe C library" variants.
memif_list_elt_t * pending_list
int get_list_elt(memif_list_elt_t **e, memif_list_elt_t *list, uint16_t len, int key)
Definition: main.c:354
#define MEMIF_NAME_LEN
Definition: memif_private.h:34
uint32_t alloc_bufs
Definition: memif_private.h:92
memif_control_fd_update_t * control_fd_update
memif_conn_args_t args
uint64_t int_count
Definition: memif_private.h:91
memif_fn * write_fn
uint16_t last_tail
Definition: memif_private.h:87
int() memif_control_fd_update_t(int fd, uint8_t events)
Memif control file descriptor update (callback function)
Definition: libmemif.h:141
memif_get_external_buffer_offset_t * get_external_buffer_offset
#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:84
memif_list_elt_t * interface_list
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
memif_list_elt_t * listener_list
memif_realloc_t * realloc
uint16_t disconn_slaves
memif_free_t * free
int free_list_elt(memif_list_elt_t *list, uint16_t len, int key)
Definition: main.c:377
int add_list_elt(memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len)
Definition: main.c:319
uint8_t region
Definition: memif_private.h:83
memif_list_elt_t * interrupt_list
memif_region_index_t region
Definition: memif.h:151
struct memif_msg_queue_elt memif_msg_queue_elt_t
memif_desc_t desc[0]
Definition: memif.h:173
uint8_t is_external
Definition: memif_private.h:76
int() memif_add_external_region_t(void **addr, uint32_t size, int *fd, void *private_ctx)
Add external region.
Definition: libmemif.h:187
struct memif_connection memif_connection_t
uint8_t memif_log2_ring_size_t
Definition: memif.h:66
int memif_connect1(memif_connection_t *c)
Definition: main.c:1351
int() memif_del_external_region_t(void *addr, uint32_t size, int fd, void *private_ctx)
Delete external region.
Definition: libmemif.h:210
memif_queue_t * tx_queues
struct memif_msg_queue_elt * next
Definition: memif_private.h:99
int() memif_interrupt_t(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
Memif interrupt occured (callback function)
Definition: libmemif.h:160
int() memif_connection_update_t(memif_conn_handle_t conn, void *private_ctx)
Memif connection status update (callback function)
Definition: libmemif.h:150
u8 name[64]
Definition: memclnt.api:152
uint32_t buffer_offset
Definition: memif_private.h:74
u8 len
Definition: ip_types.api:49
uint16_t use_count
memif_add_external_region_t * add_external_region
svmdb_client_t * c
void() memif_free_t(void *ptr)
Memif allocator free.
Definition: libmemif.h:125
int memif_disconnect_internal(memif_connection_t *c)
Definition: main.c:1157
memif_alloc_t * alloc
int memif_syscall_error_handler(int err_code)
Definition: main.c:204
memif_msg_queue_elt_t * msg_queue
void *() memif_realloc_t(void *ptr, size_t size)
Memif realloc.
Definition: libmemif.h:118
static int memfd_create(const char *name, unsigned int flags)
memif_region_t * regions
uint32_t region_size
Definition: memif_private.h:73
uint32_t() memif_get_external_buffer_offset_t(void *private_ctx)
Get external buffer offset (optional)
Definition: libmemif.h:177
static void * memif_get_buffer(memif_connection_t *conn, memif_ring_t *ring, uint16_t index)
int memif_epfd
Definition: main.c:69
uint16_t interrupt_list_len
memif_conn_run_args_t run_args
memif_region_offset_t offset
Definition: memif.h:153
memif_interrupt_t * on_interrupt
uint16_t last_head
Definition: memif_private.h:86
memif_del_external_region_t * del_external_region
typedef key
Definition: ipsec.api:244
uint16_t pending_list_len
memif_list_elt_t * control_list
void *() memif_alloc_t(size_t size)
Memif allocator alloc.
Definition: libmemif.h:109
void *() memif_get_external_region_addr_t(uint32_t size, int fd, void *private_ctx)
Get external region address.
Definition: libmemif.h:199
memif_log2_ring_size_t log2_ring_size
uint16_t control_list_len
memif_get_external_region_addr_t * get_external_region_addr
uint16_t listener_list_len
Memif connection arguments.
Definition: libmemif.h:254
uint16_t interface_list_len