hs-test: more debug output in http3 test
[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 #include <sys/queue.h>
31
32 #include <memif.h>
33 #include <libmemif.h>
34
35 #define MEMIF_NAME_LEN 32
36 _Static_assert (strlen (MEMIF_DEFAULT_APP_NAME) <= MEMIF_NAME_LEN,
37                 "MEMIF_DEFAULT_APP_NAME max length is 32");
38
39 #define MEMIF_DEFAULT_SOCKET_PATH "/run/vpp/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 #ifndef HAS_LIB_BSD
71 static inline size_t
72 strlcpy (char *dest, const char *src, size_t len)
73 {
74   const char *s = src;
75   size_t n = len;
76
77   while (--n > 0)
78     {
79       if ((*dest++ = *s++) == '\0')
80         break;
81     }
82
83   if (n == 0)
84     {
85       if (len != 0)
86         *dest = '\0';
87       while (*s++)
88         ;
89     }
90
91   return (s - src - 1);
92 }
93 #else
94 #include <bsd/string.h>
95 #endif
96
97 typedef enum
98 {
99   MEMIF_SOCKET_TYPE_NONE = 0,   /* unassigned, not used by any interface */
100   MEMIF_SOCKET_TYPE_LISTENER,   /* listener socket, master interface assigned */
101   MEMIF_SOCKET_TYPE_CLIENT      /* client socket, slave interface assigned */
102 } memif_socket_type_t;
103
104 typedef struct
105 {
106   void *addr;
107   memif_region_size_t region_size;
108   uint32_t buffer_offset;
109   int fd;
110   uint8_t is_external;
111 } memif_region_t;
112
113 typedef struct
114 {
115   memif_ring_t *ring;
116   uint8_t log2_ring_size;
117   uint8_t region;
118   uint32_t offset;
119
120   uint16_t last_head;
121   uint16_t last_tail;
122
123   int int_fd;
124
125   uint64_t int_count;
126   uint32_t next_buf; /* points to next free buffer */
127 } memif_queue_t;
128
129 struct memif_connection;
130
131 typedef struct memif_connection memif_connection_t;
132
133 typedef struct
134 {
135   uint8_t num_s2m_rings;
136   uint8_t num_m2s_rings;
137   uint16_t buffer_size;
138   memif_log2_ring_size_t log2_ring_size;
139 } memif_conn_run_args_t;
140
141 struct memif_control_channel;
142
143 typedef struct memif_connection
144 {
145   uint16_t index;
146   memif_conn_args_t args;
147   memif_conn_run_args_t run_args;
148
149   struct memif_control_channel *control_channel;
150
151   memif_connection_update_t *on_connect, *on_disconnect;
152   memif_on_interrupt_t *on_interrupt;
153   void *private_ctx;
154
155   uint8_t remote_if_name[MEMIF_NAME_LEN];
156   uint8_t remote_name[MEMIF_NAME_LEN];
157   uint8_t remote_disconnect_string[96];
158
159   uint8_t regions_num;
160   memif_region_t *regions;
161
162   uint8_t rx_queues_num;
163   uint8_t tx_queues_num;
164   memif_queue_t *rx_queues;
165   memif_queue_t *tx_queues;
166
167   uint16_t flags;
168 #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
169
170   TAILQ_ENTRY (memif_connection) next;
171 } memif_connection_t;
172
173 /** \brief Memif message queue element
174  * @param msg - memif control message (defined in memif.h)
175  * @param nex - tailq entry
176  * @param fd - File descriptor to be shared with peer endpoint
177  */
178 typedef struct memif_msg_queue_elt
179 {
180   memif_msg_t msg;
181   TAILQ_ENTRY (memif_msg_queue_elt) next;
182   int fd;
183 } memif_msg_queue_elt_t;
184
185 struct memif_socket;
186
187 /** \brief Memif control channel
188  * @param fd - fd used for communbication
189  * @param msg_queue - message queue
190  * @param conn - memif connection using this control channel
191  * @param sock - socket this control channel belongs to
192  *
193  * Memif controll channel represents one end of communication between two memif
194  * endpoints. The controll channel is responsible for receiving and
195  * transmitting memif control messages via UNIX domain socket.
196  */
197 typedef struct memif_control_channel
198 {
199   int fd;
200   TAILQ_HEAD (, memif_msg_queue_elt) msg_queue;
201   memif_connection_t *conn;
202   struct memif_socket *sock;
203 } memif_control_channel_t;
204
205 /** \brief Memif socket
206  * @param args - memif socket arguments (from libmemif.h)
207  * @param epfd - epoll fd, used for internal fd polling
208  * @param poll_cancel_fd - if event is received on this fd, interrupt polling
209  * @param listener_fd - listener fd if this socket is listener else -1
210  * @param private_ctx - private context
211  * @param master_interfaces - master interface queue
212  * @param slave_interfaces - slave interface queue
213  * @param control_channels - controll channel queue
214  */
215 typedef struct memif_socket
216 {
217   memif_socket_args_t args;
218   int epfd;
219   int poll_cancel_fd;
220   int listener_fd;
221   int timer_fd;
222   struct itimerspec timer;
223   void *private_ctx;
224   TAILQ_HEAD (, memif_connection) master_interfaces;
225   TAILQ_HEAD (, memif_connection) slave_interfaces;
226
227   /* External region callbacks */
228   memif_add_external_region_t *add_external_region;
229   memif_get_external_region_addr_t *get_external_region_addr;
230   memif_del_external_region_t *del_external_region;
231   memif_get_external_buffer_offset_t *get_external_buffer_offset;
232 } memif_socket_t;
233
234 typedef int (memif_fd_event_handler_t) (memif_fd_event_type_t type,
235                                         void *private_ctx);
236
237 typedef struct memif_fd_event_data
238 {
239   memif_fd_event_handler_t *event_handler;
240   void *private_ctx;
241 } memif_fd_event_data_t;
242
243 typedef struct memif_interrupt
244 {
245   memif_connection_t *c;
246   uint16_t qid;
247 } memif_interrupt_t;
248
249 /* main.c */
250
251 /* if region doesn't contain shared memory, mmap region, check ring cookie */
252 int memif_connect1 (memif_connection_t * c);
253
254 /* memory map region, initialize rings and queues */
255 int memif_init_regions_and_queues (memif_connection_t * c);
256
257 int memif_disconnect_internal (memif_connection_t * c);
258
259 int memif_interrupt_handler (memif_fd_event_type_t type, void *private_ctx);
260
261 /* map errno to memif error code */
262 int memif_syscall_error_handler (int err_code);
263
264 #ifndef __NR_memfd_create
265 #if defined __x86_64__
266 #define __NR_memfd_create 319
267 #elif defined __arm__
268 #define __NR_memfd_create 385
269 #elif defined __aarch64__
270 #define __NR_memfd_create 279
271 #else
272 #error "__NR_memfd_create unknown for this architecture"
273 #endif
274 #endif
275
276 #ifndef HAVE_MEMFD_CREATE
277 static inline int
278 memfd_create (const char *name, unsigned int flags)
279 {
280   return syscall (__NR_memfd_create, name, flags);
281 }
282 #endif
283
284 static inline void *
285 memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring,
286                   uint16_t index)
287 {
288   return (conn->regions[ring->desc[index].region].addr +
289           ring->desc[index].offset);
290 }
291
292 #ifndef F_LINUX_SPECIFIC_BASE
293 #define F_LINUX_SPECIFIC_BASE 1024
294 #endif
295
296 #ifndef MFD_ALLOW_SEALING
297 #define MFD_ALLOW_SEALING       0x0002U
298 #endif
299
300 #ifndef F_ADD_SEALS
301 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
302 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
303
304 #define F_SEAL_SEAL     0x0001  /* prevent further seals from being set */
305 #define F_SEAL_SHRINK   0x0002  /* prevent file from shrinking */
306 #define F_SEAL_GROW     0x0004  /* prevent file from growing */
307 #define F_SEAL_WRITE    0x0008  /* prevent writes */
308 #endif
309
310 #endif /* _MEMIF_PRIVATE_H_ */