blob: 71a4bc879f4d00e9f7b56e4ec854822299513450 [file] [log] [blame]
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001/*
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>
Jakub Grajciar19418712018-03-13 13:57:50 +010029#include <string.h>
Jakub Grajciar134f1e02021-01-04 11:10:42 +010030#include <sys/queue.h>
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020031
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020032#include <memif.h>
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020033#include <libmemif.h>
34
Jakub Grajciar19418712018-03-13 13:57:50 +010035#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
Jakub Grajciar12df4972019-07-01 14:24:48 +020039#define MEMIF_DEFAULT_SOCKET_PATH "/run/vpp/memif.sock"
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020040#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
Jakub Grajciar84b83772019-03-04 12:42:19 +010045#define MEMIF_DEFAULT_RECONNECT_PERIOD_SEC 2
46#define MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC 0
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020047
48#define MEMIF_MAX_M2S_RING 255
49#define MEMIF_MAX_S2M_RING 255
50#define MEMIF_MAX_REGION 255
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020051#define MEMIF_MAX_LOG2_RING_SIZE 14
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020052
53#define MEMIF_MAX_FDS 512
54
Jakub Grajciarb467b2a2017-09-14 14:12:10 +020055#define memif_min(a,b) (((a) < (b)) ? (a) : (b))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020056
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020057#define EXPECT_TRUE(x) __builtin_expect((x),1)
58#define EXPECT_FALSE(x) __builtin_expect((x),0)
59
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020060#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)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020066#else
67#define DBG(...)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020068#endif /* MEMIF_DBG */
69
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +000070#ifndef HAS_LIB_BSD
71static inline size_t
72strlcpy (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
Jakub Grajciar12df4972019-07-01 14:24:48 +020097typedef 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
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200104typedef struct
105{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200106 void *addr;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100107 memif_region_size_t region_size;
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200108 uint32_t buffer_offset;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200109 int fd;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200110 uint8_t is_external;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200111} memif_region_t;
112
113typedef 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;
Jakub Grajciarf35fef22021-01-08 15:01:13 +0100126 uint32_t next_buf; /* points to next free buffer */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200127} memif_queue_t;
128
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200129struct memif_connection;
130
131typedef struct memif_connection memif_connection_t;
132
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200133typedef 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
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100141struct memif_control_channel;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200142
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200143typedef struct memif_connection
144{
145 uint16_t index;
146 memif_conn_args_t args;
147 memif_conn_run_args_t run_args;
148
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100149 struct memif_control_channel *control_channel;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200150
151 memif_connection_update_t *on_connect, *on_disconnect;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100152 memif_on_interrupt_t *on_interrupt;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200153 void *private_ctx;
154
Jakub Grajciar19418712018-03-13 13:57:50 +0100155 uint8_t remote_if_name[MEMIF_NAME_LEN];
156 uint8_t remote_name[MEMIF_NAME_LEN];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200157 uint8_t remote_disconnect_string[96];
158
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200159 uint8_t regions_num;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200160 memif_region_t *regions;
161
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200162 uint8_t rx_queues_num;
163 uint8_t tx_queues_num;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200164 memif_queue_t *rx_queues;
165 memif_queue_t *tx_queues;
166
167 uint16_t flags;
168#define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100169
170 TAILQ_ENTRY (memif_connection) next;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200171} memif_connection_t;
172
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100173/** \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 */
178typedef struct memif_msg_queue_elt
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200179{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100180 memif_msg_t msg;
181 TAILQ_ENTRY (memif_msg_queue_elt) next;
182 int fd;
183} memif_msg_queue_elt_t;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200184
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100185struct 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 */
197typedef struct memif_control_channel
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200198{
199 int fd;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100200 TAILQ_HEAD (, memif_msg_queue_elt) msg_queue;
201 memif_connection_t *conn;
202 struct memif_socket *sock;
203} memif_control_channel_t;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200204
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100205/** \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 */
215typedef struct memif_socket
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200216{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100217 memif_socket_args_t args;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200218 int epfd;
219 int poll_cancel_fd;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100220 int listener_fd;
221 int timer_fd;
222 struct itimerspec timer;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200223 void *private_ctx;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100224 TAILQ_HEAD (, memif_connection) master_interfaces;
225 TAILQ_HEAD (, memif_connection) slave_interfaces;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200226
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100227 /* External region callbacks */
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200228 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;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100232} memif_socket_t;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200233
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100234typedef int (memif_fd_event_handler_t) (memif_fd_event_type_t type,
235 void *private_ctx);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200236
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100237typedef struct memif_fd_event_data
238{
239 memif_fd_event_handler_t *event_handler;
240 void *private_ctx;
241} memif_fd_event_data_t;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200242
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100243typedef struct memif_interrupt
244{
245 memif_connection_t *c;
246 uint16_t qid;
247} memif_interrupt_t;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200248
249/* main.c */
250
251/* if region doesn't contain shared memory, mmap region, check ring cookie */
252int memif_connect1 (memif_connection_t * c);
253
Paul Vinciguerra62237662020-05-29 23:03:06 -0400254/* memory map region, initialize rings and queues */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200255int memif_init_regions_and_queues (memif_connection_t * c);
256
257int memif_disconnect_internal (memif_connection_t * c);
258
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100259int memif_interrupt_handler (memif_fd_event_type_t type, void *private_ctx);
260
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200261/* map errno to memif error code */
262int memif_syscall_error_handler (int err_code);
263
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200264#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
Jakub Grajciar19418712018-03-13 13:57:50 +0100276#ifndef HAVE_MEMFD_CREATE
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200277static inline int
Damjan Marion23d4e8a2018-03-26 14:09:38 +0200278memfd_create (const char *name, unsigned int flags)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200279{
280 return syscall (__NR_memfd_create, name, flags);
281}
Jakub Grajciar19418712018-03-13 13:57:50 +0100282#endif
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200283
284static inline void *
285memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring,
286 uint16_t index)
287{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200288 return (conn->regions[ring->desc[index].region].addr +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200289 ring->desc[index].offset);
290}
291
292#ifndef F_LINUX_SPECIFIC_BASE
293#define F_LINUX_SPECIFIC_BASE 1024
294#endif
Jakub Grajciar19418712018-03-13 13:57:50 +0100295
296#ifndef MFD_ALLOW_SEALING
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200297#define MFD_ALLOW_SEALING 0x0002U
Jakub Grajciar19418712018-03-13 13:57:50 +0100298#endif
299
300#ifndef F_ADD_SEALS
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200301#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 */
Jakub Grajciar19418712018-03-13 13:57:50 +0100308#endif
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200309
310#endif /* _MEMIF_PRIVATE_H_ */