FD.io VPP  v18.07.1-19-g511ce25
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 
46 #define MEMIF_MAX_M2S_RING 255
47 #define MEMIF_MAX_S2M_RING 255
48 #define MEMIF_MAX_REGION 255
49 #define MEMIF_MAX_LOG2_RING_SIZE 14
50 
51 #define MEMIF_MAX_FDS 512
52 
53 #define memif_min(a,b) (((a) < (b)) ? (a) : (b))
54 
55 #define EXPECT_TRUE(x) __builtin_expect((x),1)
56 #define EXPECT_FALSE(x) __builtin_expect((x),0)
57 
58 #ifdef MEMIF_DBG
59 #define DBG(...) do { \
60  printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
61  printf(__VA_ARGS__); \
62  printf("\n"); \
63  } while (0)
64 #else
65 #define DBG(...)
66 #endif /* MEMIF_DBG */
67 
68 typedef struct
69 {
70  void *shm;
71  uint32_t region_size;
72  uint32_t buffer_offset;
73  int fd;
75 
76 typedef struct
77 {
78  memif_ring_t *ring;
79  uint8_t log2_ring_size;
80  uint8_t region;
81  uint32_t offset;
82 
83  uint16_t last_head;
84  uint16_t last_tail;
85 
86  int int_fd;
87 
88  uint64_t int_count;
89  uint32_t alloc_bufs;
91 
92 typedef struct memif_msg_queue_elt
93 {
95  int fd;
98 
99 struct memif_connection;
100 
102 
103 /* functions called by memif_control_fd_handler */
104 typedef int (memif_fn) (memif_connection_t * conn);
105 
106 typedef struct
107 {
108  uint8_t num_s2m_rings;
109  uint8_t num_m2s_rings;
110  uint16_t buffer_size;
113 
114 typedef struct memif_connection
115 {
116  uint16_t index;
119 
120  int fd;
122 
123  memif_fn *write_fn, *read_fn, *error_fn;
124 
127  void *private_ctx;
128 
129  /* connection message queue */
131 
132  uint8_t remote_if_name[MEMIF_NAME_LEN];
133  uint8_t remote_name[MEMIF_NAME_LEN];
134  uint8_t remote_disconnect_string[96];
135 
137 
140 
141  uint16_t flags;
142 #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
144 
145 typedef struct
146 {
147  int key;
148  void *data_struct;
150 
151 typedef struct
152 {
153  int fd;
154  uint16_t use_count;
155  uint8_t *filename;
157  memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
159 
160 typedef struct
161 {
163  int timerfd;
164  struct itimerspec arm, disarm;
165  uint16_t disconn_slaves;
166  uint8_t app_name[MEMIF_NAME_LEN];
167 
170 
180 
182 extern int memif_epfd;
183 
184 /* main.c */
185 
186 /* if region doesn't contain shared memory, mmap region, check ring cookie */
188 
189 /* memory map region, initalize rings and queues */
191 
193 
194 /* map errno to memif error code */
195 int memif_syscall_error_handler (int err_code);
196 
198  uint16_t * len);
199 
201  uint16_t len, int key);
202 
203 int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
204 
205 #ifndef __NR_memfd_create
206 #if defined __x86_64__
207 #define __NR_memfd_create 319
208 #elif defined __arm__
209 #define __NR_memfd_create 385
210 #elif defined __aarch64__
211 #define __NR_memfd_create 279
212 #else
213 #error "__NR_memfd_create unknown for this architecture"
214 #endif
215 #endif
216 
217 #ifndef HAVE_MEMFD_CREATE
218 static inline int
219 memfd_create (const char *name, unsigned int flags)
220 {
221  return syscall (__NR_memfd_create, name, flags);
222 }
223 #endif
224 
225 static inline void *
227  uint16_t index)
228 {
229  return (conn->regions[ring->desc[index].region].shm +
230  ring->desc[index].offset);
231 }
232 
233 #ifndef F_LINUX_SPECIFIC_BASE
234 #define F_LINUX_SPECIFIC_BASE 1024
235 #endif
236 
237 #ifndef MFD_ALLOW_SEALING
238 #define MFD_ALLOW_SEALING 0x0002U
239 #endif
240 
241 #ifndef F_ADD_SEALS
242 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
243 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
244 
245 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
246 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
247 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
248 #define F_SEAL_WRITE 0x0008 /* prevent writes */
249 #endif
250 
251 #endif /* _MEMIF_PRIVATE_H_ */
uint8_t log2_ring_size
Definition: memif_private.h:79
int memif_init_regions_and_queues(memif_connection_t *c)
Definition: main.c:1325
memif_queue_t * rx_queues
uint8_t * filename
int( memif_fn)(memif_connection_t *conn)
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:348
#define MEMIF_NAME_LEN
Definition: memif_private.h:34
uint32_t alloc_bufs
Definition: memif_private.h:89
int( memif_control_fd_update_t)(int fd, uint8_t events)
Memif control file descriptor update (callback function)
Definition: libmemif.h:130
memif_control_fd_update_t * control_fd_update
memif_conn_args_t args
uint64_t int_count
Definition: memif_private.h:88
memif_fn * write_fn
uint16_t last_tail
Definition: memif_private.h:84
#define MEMIF_DEFAULT_APP_NAME
Default name of application using libmemif.
Definition: libmemif.h:28
uint32_t offset
Definition: memif_private.h:81
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
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:371
int add_list_elt(memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len)
Definition: main.c:313
uint8_t region
Definition: memif_private.h:80
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
int( memif_interrupt_t)(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
Memif interrupt occured (callback function)
Definition: libmemif.h:149
struct memif_connection memif_connection_t
uint8_t memif_log2_ring_size_t
Definition: memif.h:66
int( memif_connection_update_t)(memif_conn_handle_t conn, void *private_ctx)
Memif connection status update (callback function)
Definition: libmemif.h:139
int memif_connect1(memif_connection_t *c)
Definition: main.c:1259
memif_queue_t * tx_queues
struct memif_msg_queue_elt * next
Definition: memif_private.h:96
uint32_t buffer_offset
Definition: memif_private.h:72
void( memif_free_t)(void *ptr)
Memif allocator free.
Definition: libmemif.h:114
u32 flags
Definition: vhost_user.h:110
uint16_t use_count
svmdb_client_t * c
int memif_disconnect_internal(memif_connection_t *c)
Definition: main.c:1077
memif_alloc_t * alloc
int memif_syscall_error_handler(int err_code)
Definition: main.c:202
memif_msg_queue_elt_t * msg_queue
static int memfd_create(const char *name, unsigned int flags)
memif_region_t * regions
uint32_t region_size
Definition: memif_private.h:71
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:83
uint16_t pending_list_len
memif_list_elt_t * control_list
memif_log2_ring_size_t log2_ring_size
uint16_t control_list_len
uint16_t listener_list_len
Memif connection arguments.
Definition: libmemif.h:181
uint16_t interface_list_len
void *( memif_alloc_t)(size_t size)
Memif allocator alloc.
Definition: libmemif.h:107