Shared memory packet interface (memif) library
[vpp.git] / extras / libmemif / src / memif_private.h
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        14
44
45 #define MEMIF_MAX_FDS 512
46
47
48 #ifdef MEMIF_DBG
49 #define DBG(...) do {                                                             \
50                         printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__);  \
51                         printf(__VA_ARGS__);                                            \
52                         printf("\n");                                                   \
53                         } while (0)
54
55 #define DBG_UNIX(...) do {                                                        \
56                       printf("MEMIF_DEBUG_UNIX:%s:%s:%d: ", __FILE__, __func__, __LINE__);  \
57                       printf(__VA_ARGS__);                                    \
58                       printf("\n");                                           \
59                       } while (0)
60
61 #define error_return_unix(...) do {                                             \
62                                 DBG_UNIX(__VA_ARGS__);                          \
63                                 return -1;                                      \
64                                 } while (0)
65 #define error_return(...) do {                                                  \
66                             DBG(__VA_ARGS__);                                   \
67                             return -1;                                          \
68                             } while (0)
69 #else
70 #define DBG(...)
71 #define DBG_UNIX(...)
72 #define error_return_unix(...) do {                                             \
73                                 return -1;                                      \
74                                 } while (0)
75 #define error_return(...) do {                                                  \
76                             return -1;                                          \
77                             } while (0)
78
79 #endif /* MEMIF_DBG */
80
81 typedef struct
82 {
83   void *shm;
84   uint32_t region_size;
85   int fd;
86 } memif_region_t;
87
88 typedef struct
89 {
90   memif_ring_t *ring;
91   uint8_t log2_ring_size;
92   uint8_t region;
93   uint32_t offset;
94
95   uint16_t last_head;
96   uint16_t last_tail;
97
98   int int_fd;
99
100   uint64_t int_count;
101   uint32_t alloc_bufs;
102 } memif_queue_t;
103
104 typedef struct memif_msg_queue_elt
105 {
106   memif_msg_t msg;
107   int fd;
108   struct memif_msg_queue_elt *next;
109 } memif_msg_queue_elt_t;
110
111 struct memif_connection;
112
113 typedef struct memif_connection memif_connection_t;
114
115 /* functions called by memif_control_fd_handler */
116 typedef int (memif_fn) (memif_connection_t * conn);
117
118 typedef struct
119 {
120   uint8_t num_s2m_rings;
121   uint8_t num_m2s_rings;
122   uint16_t buffer_size;
123   memif_log2_ring_size_t log2_ring_size;
124 } memif_conn_run_args_t;
125
126 typedef struct memif_connection
127 {
128   uint16_t index;
129   memif_conn_args_t args;
130   memif_conn_run_args_t run_args;
131
132   int fd;
133   int listener_fd;
134
135   memif_fn *write_fn, *read_fn, *error_fn;
136
137   memif_connection_update_t *on_connect, *on_disconnect;
138   memif_interrupt_t *on_interrupt;
139   void *private_ctx;
140
141   /* connection message queue */
142   memif_msg_queue_elt_t *msg_queue;
143
144   uint8_t remote_if_name[32];
145   uint8_t remote_name[32];
146   uint8_t remote_disconnect_string[96];
147
148   memif_region_t *regions;
149
150   memif_queue_t *rx_queues;
151   memif_queue_t *tx_queues;
152
153   uint16_t flags;
154 #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
155 } memif_connection_t;
156
157 /*
158  * WIP
159  */
160 typedef struct
161 {
162   int key;                      /* fd or id */
163   void *data_struct;
164 } memif_list_elt_t;
165
166 /*
167  * WIP
168  */
169 typedef struct
170 {
171   int fd;
172   uint16_t use_count;
173   uint8_t *filename;
174   uint16_t interface_list_len;
175   memif_list_elt_t *interface_list;     /* memif master interfaces listening on this socket */
176 } memif_socket_t;
177
178 /*
179  * WIP
180  */
181 /* probably function like memif_cleanup () will need to be called to close timerfd */
182 typedef struct
183 {
184   memif_control_fd_update_t *control_fd_update;
185   int timerfd;
186   struct itimerspec arm, disarm;
187   uint16_t disconn_slaves;
188   uint8_t *app_name;
189
190   /* master implementation... */
191   memif_socket_t ms;
192
193   uint16_t control_list_len;
194   uint16_t interrupt_list_len;
195   uint16_t listener_list_len;
196   uint16_t pending_list_len;
197   memif_list_elt_t *control_list;
198   memif_list_elt_t *interrupt_list;
199   memif_list_elt_t *listener_list;
200   memif_list_elt_t *pending_list;
201 } libmemif_main_t;
202
203 extern libmemif_main_t libmemif_main;
204 extern int memif_epfd;
205
206 /* main.c */
207
208 /* if region doesn't contain shared memory, mmap region, check ring cookie */
209 int memif_connect1 (memif_connection_t * c);
210
211 /* memory map region, initalize rings and queues */
212 int memif_init_regions_and_queues (memif_connection_t * c);
213
214 int memif_disconnect_internal (memif_connection_t * c);
215
216 /* map errno to memif error code */
217 int memif_syscall_error_handler (int err_code);
218
219 int add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list,
220                   uint16_t * len);
221
222 int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list,
223                   uint16_t len, int key);
224
225 int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
226
227 #ifndef __NR_memfd_create
228 #if defined __x86_64__
229 #define __NR_memfd_create 319
230 #elif defined __arm__
231 #define __NR_memfd_create 385
232 #elif defined __aarch64__
233 #define __NR_memfd_create 279
234 #else
235 #error "__NR_memfd_create unknown for this architecture"
236 #endif
237 #endif
238
239 static inline int
240 memfd_create (const char *name, unsigned int flags)
241 {
242   return syscall (__NR_memfd_create, name, flags);
243 }
244
245 static inline void *
246 memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring,
247                   uint16_t index)
248 {
249   return (conn->regions[ring->desc[index].region].shm +
250           ring->desc[index].offset);
251 }
252
253 #ifndef F_LINUX_SPECIFIC_BASE
254 #define F_LINUX_SPECIFIC_BASE 1024
255 #endif
256 #define MFD_ALLOW_SEALING       0x0002U
257 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
258 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
259
260 #define F_SEAL_SEAL     0x0001  /* prevent further seals from being set */
261 #define F_SEAL_SHRINK   0x0002  /* prevent file from shrinking */
262 #define F_SEAL_GROW     0x0004  /* prevent file from growing */
263 #define F_SEAL_WRITE    0x0008  /* prevent writes */
264
265 #endif /* _MEMIF_PRIVATE_H_ */