blob: eceac6777526ac37affdea67f74a504d61396aee [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 Grajciar7c5c40d2017-08-30 10:13:25 +020030
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020031#include <memif.h>
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020032#include <libmemif.h>
33
Jakub Grajciar19418712018-03-13 13:57:50 +010034#define MEMIF_NAME_LEN 32
35_Static_assert (strlen (MEMIF_DEFAULT_APP_NAME) <= MEMIF_NAME_LEN,
36 "MEMIF_DEFAULT_APP_NAME max length is 32");
37
Jakub Grajciar12df4972019-07-01 14:24:48 +020038#define MEMIF_DEFAULT_SOCKET_PATH "/run/vpp/memif.sock"
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020039#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
Jakub Grajciar84b83772019-03-04 12:42:19 +010044#define MEMIF_DEFAULT_RECONNECT_PERIOD_SEC 2
45#define MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC 0
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020046
47#define MEMIF_MAX_M2S_RING 255
48#define MEMIF_MAX_S2M_RING 255
49#define MEMIF_MAX_REGION 255
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020050#define MEMIF_MAX_LOG2_RING_SIZE 14
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020051
52#define MEMIF_MAX_FDS 512
53
Jakub Grajciarb467b2a2017-09-14 14:12:10 +020054#define memif_min(a,b) (((a) < (b)) ? (a) : (b))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020055
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020056#define EXPECT_TRUE(x) __builtin_expect((x),1)
57#define EXPECT_FALSE(x) __builtin_expect((x),0)
58
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020059#ifdef MEMIF_DBG
60#define DBG(...) do { \
61 printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
62 printf(__VA_ARGS__); \
63 printf("\n"); \
64 } while (0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020065#else
66#define DBG(...)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020067#endif /* MEMIF_DBG */
68
Jakub Grajciar12df4972019-07-01 14:24:48 +020069typedef enum
70{
71 MEMIF_SOCKET_TYPE_NONE = 0, /* unassigned, not used by any interface */
72 MEMIF_SOCKET_TYPE_LISTENER, /* listener socket, master interface assigned */
73 MEMIF_SOCKET_TYPE_CLIENT /* client socket, slave interface assigned */
74} memif_socket_type_t;
75
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020076typedef struct
77{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +020078 void *addr;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020079 uint32_t region_size;
Jakub Grajciar3744fc72018-03-29 13:15:10 +020080 uint32_t buffer_offset;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020081 int fd;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +020082 uint8_t is_external;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020083} memif_region_t;
84
85typedef struct
86{
87 memif_ring_t *ring;
88 uint8_t log2_ring_size;
89 uint8_t region;
90 uint32_t offset;
91
92 uint16_t last_head;
93 uint16_t last_tail;
94
95 int int_fd;
96
97 uint64_t int_count;
98 uint32_t alloc_bufs;
99} memif_queue_t;
100
101typedef struct memif_msg_queue_elt
102{
103 memif_msg_t msg;
104 int fd;
105 struct memif_msg_queue_elt *next;
106} memif_msg_queue_elt_t;
107
108struct memif_connection;
109
110typedef struct memif_connection memif_connection_t;
111
112/* functions called by memif_control_fd_handler */
113typedef int (memif_fn) (memif_connection_t * conn);
114
115typedef struct
116{
117 uint8_t num_s2m_rings;
118 uint8_t num_m2s_rings;
119 uint16_t buffer_size;
120 memif_log2_ring_size_t log2_ring_size;
121} memif_conn_run_args_t;
122
123typedef struct memif_connection
124{
125 uint16_t index;
126 memif_conn_args_t args;
127 memif_conn_run_args_t run_args;
128
129 int fd;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200130
131 memif_fn *write_fn, *read_fn, *error_fn;
132
133 memif_connection_update_t *on_connect, *on_disconnect;
134 memif_interrupt_t *on_interrupt;
135 void *private_ctx;
136
137 /* connection message queue */
138 memif_msg_queue_elt_t *msg_queue;
139
Jakub Grajciar19418712018-03-13 13:57:50 +0100140 uint8_t remote_if_name[MEMIF_NAME_LEN];
141 uint8_t remote_name[MEMIF_NAME_LEN];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200142 uint8_t remote_disconnect_string[96];
143
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200144 uint8_t regions_num;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200145 memif_region_t *regions;
146
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200147 uint8_t rx_queues_num;
148 uint8_t tx_queues_num;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200149 memif_queue_t *rx_queues;
150 memif_queue_t *tx_queues;
151
152 uint16_t flags;
153#define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
154} memif_connection_t;
155
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200156typedef struct
157{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200158 int key;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200159 void *data_struct;
160} memif_list_elt_t;
161
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200162typedef struct
163{
164 int fd;
165 uint16_t use_count;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200166 memif_socket_type_t type;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200167 uint8_t *filename;
168 uint16_t interface_list_len;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200169 void *private_ctx;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200170 memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
171} memif_socket_t;
172
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200173typedef struct
174{
175 memif_control_fd_update_t *control_fd_update;
176 int timerfd;
177 struct itimerspec arm, disarm;
178 uint16_t disconn_slaves;
Jakub Grajciar19418712018-03-13 13:57:50 +0100179 uint8_t app_name[MEMIF_NAME_LEN];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200180
Jakub Grajciar12df4972019-07-01 14:24:48 +0200181 memif_socket_handle_t default_socket;
182
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200183 memif_add_external_region_t *add_external_region;
184 memif_get_external_region_addr_t *get_external_region_addr;
185 memif_del_external_region_t *del_external_region;
186 memif_get_external_buffer_offset_t *get_external_buffer_offset;
187
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200188 memif_alloc_t *alloc;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200189 memif_realloc_t *realloc;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200190 memif_free_t *free;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200191
192 uint16_t control_list_len;
193 uint16_t interrupt_list_len;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200194 uint16_t socket_list_len;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200195 uint16_t pending_list_len;
196 memif_list_elt_t *control_list;
197 memif_list_elt_t *interrupt_list;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200198 memif_list_elt_t *socket_list;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200199 memif_list_elt_t *pending_list;
200} libmemif_main_t;
201
202extern libmemif_main_t libmemif_main;
203extern int memif_epfd;
204
205/* main.c */
206
207/* if region doesn't contain shared memory, mmap region, check ring cookie */
208int memif_connect1 (memif_connection_t * c);
209
210/* memory map region, initalize rings and queues */
211int memif_init_regions_and_queues (memif_connection_t * c);
212
213int memif_disconnect_internal (memif_connection_t * c);
214
215/* map errno to memif error code */
216int memif_syscall_error_handler (int err_code);
217
218int add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list,
219 uint16_t * len);
220
221int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list,
222 uint16_t len, int key);
223
224int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
225
226#ifndef __NR_memfd_create
227#if defined __x86_64__
228#define __NR_memfd_create 319
229#elif defined __arm__
230#define __NR_memfd_create 385
231#elif defined __aarch64__
232#define __NR_memfd_create 279
233#else
234#error "__NR_memfd_create unknown for this architecture"
235#endif
236#endif
237
Jakub Grajciar19418712018-03-13 13:57:50 +0100238#ifndef HAVE_MEMFD_CREATE
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200239static inline int
Damjan Marion23d4e8a2018-03-26 14:09:38 +0200240memfd_create (const char *name, unsigned int flags)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200241{
242 return syscall (__NR_memfd_create, name, flags);
243}
Jakub Grajciar19418712018-03-13 13:57:50 +0100244#endif
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200245
246static inline void *
247memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring,
248 uint16_t index)
249{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200250 return (conn->regions[ring->desc[index].region].addr +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200251 ring->desc[index].offset);
252}
253
254#ifndef F_LINUX_SPECIFIC_BASE
255#define F_LINUX_SPECIFIC_BASE 1024
256#endif
Jakub Grajciar19418712018-03-13 13:57:50 +0100257
258#ifndef MFD_ALLOW_SEALING
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200259#define MFD_ALLOW_SEALING 0x0002U
Jakub Grajciar19418712018-03-13 13:57:50 +0100260#endif
261
262#ifndef F_ADD_SEALS
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200263#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
264#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
265
266#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
267#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
268#define F_SEAL_GROW 0x0004 /* prevent file from growing */
269#define F_SEAL_WRITE 0x0008 /* prevent writes */
Jakub Grajciar19418712018-03-13 13:57:50 +0100270#endif
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200271
272#endif /* _MEMIF_PRIVATE_H_ */