FD.io VPP  v18.01-8-g0eacf49
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 
30 #include <libmemif.h>
31 
32 #define MEMIF_DEFAULT_SOCKET_DIR "/run/vpp"
33 #define MEMIF_DEFAULT_SOCKET_FILENAME "memif.sock"
34 #define MEMIF_DEFAULT_RING_SIZE 1024
35 #define MEMIF_DEFAULT_LOG2_RING_SIZE 10
36 #define MEMIF_DEFAULT_RX_QUEUES 1
37 #define MEMIF_DEFAULT_TX_QUEUES 1
38 #define MEMIF_DEFAULT_BUFFER_SIZE 2048
39 
40 #define MEMIF_MAX_M2S_RING 255
41 #define MEMIF_MAX_S2M_RING 255
42 #define MEMIF_MAX_REGION 255
43 #define MEMIF_MAX_LOG2_RING_SIZE 15
44 
45 #define MEMIF_MAX_FDS 512
46 
47 #define memif_min(a,b) (((a) < (b)) ? (a) : (b))
48 
49 #ifdef MEMIF_DBG
50 #define DBG(...) do { \
51  printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
52  printf(__VA_ARGS__); \
53  printf("\n"); \
54  } while (0)
55 
56 #define DBG_UNIX(...) do { \
57  printf("MEMIF_DEBUG_UNIX:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
58  printf(__VA_ARGS__); \
59  printf("\n"); \
60  } while (0)
61 
62 #define error_return_unix(...) do { \
63  DBG_UNIX(__VA_ARGS__); \
64  return -1; \
65  } while (0)
66 #define error_return(...) do { \
67  DBG(__VA_ARGS__); \
68  return -1; \
69  } while (0)
70 #else
71 #define DBG(...)
72 #define DBG_UNIX(...)
73 #define error_return_unix(...) do { \
74  return -1; \
75  } while (0)
76 #define error_return(...) do { \
77  return -1; \
78  } while (0)
79 
80 #endif /* MEMIF_DBG */
81 
82 typedef struct
83 {
84  void *shm;
85  uint32_t region_size;
86  int fd;
88 
89 typedef struct
90 {
91  memif_ring_t *ring;
92  uint8_t log2_ring_size;
93  uint8_t region;
94  uint32_t offset;
95 
96  uint16_t last_head;
97  uint16_t last_tail;
98 
99  int int_fd;
100 
101  uint64_t int_count;
102  uint32_t alloc_bufs;
103 } memif_queue_t;
104 
105 typedef struct memif_msg_queue_elt
106 {
108  int fd;
111 
112 struct memif_connection;
113 
115 
116 /* functions called by memif_control_fd_handler */
117 typedef int (memif_fn) (memif_connection_t * conn);
118 
119 typedef struct
120 {
121  uint8_t num_s2m_rings;
122  uint8_t num_m2s_rings;
123  uint16_t buffer_size;
126 
127 typedef struct memif_connection
128 {
129  uint16_t index;
132 
133  int fd;
135 
136  memif_fn *write_fn, *read_fn, *error_fn;
137 
140  void *private_ctx;
141 
142  /* connection message queue */
144 
145  uint8_t remote_if_name[32];
146  uint8_t remote_name[32];
147  uint8_t remote_disconnect_string[96];
148 
150 
153 
154  uint16_t flags;
155 #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
157 
158 /*
159  * WIP
160  */
161 typedef struct
162 {
163  int key; /* fd or id */
164  void *data_struct;
166 
167 /*
168  * WIP
169  */
170 typedef struct
171 {
172  int fd;
173  uint16_t use_count;
174  uint8_t *filename;
176  memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
178 
179 /*
180  * WIP
181  */
182 /* probably function like memif_cleanup () will need to be called to close timerfd */
183 typedef struct
184 {
186  int timerfd;
187  struct itimerspec arm, disarm;
188  uint16_t disconn_slaves;
189  uint8_t *app_name;
190 
191  /* master implementation... */
193 
203 
205 extern int memif_epfd;
206 
207 /* main.c */
208 
209 /* if region doesn't contain shared memory, mmap region, check ring cookie */
211 
212 /* memory map region, initalize rings and queues */
214 
216 
217 /* map errno to memif error code */
218 int memif_syscall_error_handler (int err_code);
219 
221  uint16_t * len);
222 
224  uint16_t len, int key);
225 
226 int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
227 
228 #ifndef __NR_memfd_create
229 #if defined __x86_64__
230 #define __NR_memfd_create 319
231 #elif defined __arm__
232 #define __NR_memfd_create 385
233 #elif defined __aarch64__
234 #define __NR_memfd_create 279
235 #else
236 #error "__NR_memfd_create unknown for this architecture"
237 #endif
238 #endif
239 
240 static inline int
241 memfd_create (const char *name, unsigned int flags)
242 {
243  return syscall (__NR_memfd_create, name, flags);
244 }
245 
246 static inline void *
248  uint16_t index)
249 {
250  return (conn->regions[ring->desc[index].region].shm +
251  ring->desc[index].offset);
252 }
253 
254 #ifndef F_LINUX_SPECIFIC_BASE
255 #define F_LINUX_SPECIFIC_BASE 1024
256 #endif
257 #define MFD_ALLOW_SEALING 0x0002U
258 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
259 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
260 
261 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
262 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
263 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
264 #define F_SEAL_WRITE 0x0008 /* prevent writes */
265 
266 #endif /* _MEMIF_PRIVATE_H_ */
uint8_t log2_ring_size
Definition: memif_private.h:92
int memif_init_regions_and_queues(memif_connection_t *c)
Definition: main.c:1252
memif_socket_t ms
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:338
uint32_t alloc_bufs
int( memif_control_fd_update_t)(int fd, uint8_t events)
Memif control file descriptor update (callback function)
Definition: libmemif.h:116
memif_control_fd_update_t * control_fd_update
memif_conn_args_t args
uint64_t int_count
memif_fn * write_fn
uint16_t last_tail
Definition: memif_private.h:97
uint32_t offset
Definition: memif_private.h:94
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:66
memif_list_elt_t * listener_list
uint16_t disconn_slaves
int free_list_elt(memif_list_elt_t *list, uint16_t len, int key)
Definition: main.c:361
int add_list_elt(memif_list_elt_t *e, memif_list_elt_t **list, uint16_t *len)
Definition: main.c:303
uint8_t region
Definition: memif_private.h:93
memif_list_elt_t * interrupt_list
memif_region_index_t region
Definition: memif.h:150
struct memif_msg_queue_elt memif_msg_queue_elt_t
memif_desc_t desc[0]
Definition: memif.h:174
int( memif_interrupt_t)(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
Memif interrupt occured (callback function)
Definition: libmemif.h:135
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:125
int memif_connect1(memif_connection_t *c)
Definition: main.c:1186
uint8_t * app_name
memif_queue_t * tx_queues
struct memif_msg_queue_elt * next
uint16_t use_count
svmdb_client_t * c
int memif_disconnect_internal(memif_connection_t *c)
Definition: main.c:1004
int memif_syscall_error_handler(int err_code)
Definition: main.c:192
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:85
static void * memif_get_buffer(memif_connection_t *conn, memif_ring_t *ring, uint16_t index)
int memif_epfd
Definition: main.c:67
uint16_t interrupt_list_len
memif_conn_run_args_t run_args
memif_region_offset_t offset
Definition: memif.h:154
memif_interrupt_t * on_interrupt
uint16_t last_head
Definition: memif_private.h:96
uint16_t pending_list_len
memif_list_elt_t * control_list
memif_log2_ring_size_t log2_ring_size
uint16_t control_list_len
u32 flags
Definition: vhost-user.h:77
uint16_t listener_list_len
Memif connection arguments.
Definition: libmemif.h:159
uint16_t interface_list_len