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