blob: deca80c3350af7b26d5a3fdc645367628650960b [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 Grajciar7c5c40d2017-08-30 10:13:25 +020038#define MEMIF_DEFAULT_SOCKET_DIR "/run/vpp"
39#define MEMIF_DEFAULT_SOCKET_FILENAME "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
46#define MEMIF_MAX_M2S_RING 255
47#define MEMIF_MAX_S2M_RING 255
48#define MEMIF_MAX_REGION 255
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020049#define MEMIF_MAX_LOG2_RING_SIZE 14
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020050
51#define MEMIF_MAX_FDS 512
52
Jakub Grajciarb467b2a2017-09-14 14:12:10 +020053#define memif_min(a,b) (((a) < (b)) ? (a) : (b))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020054
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020055#define EXPECT_TRUE(x) __builtin_expect((x),1)
56#define EXPECT_FALSE(x) __builtin_expect((x),0)
57
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020058#ifdef MEMIF_DBG
59#define DBG(...) do { \
60 printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
61 printf(__VA_ARGS__); \
62 printf("\n"); \
63 } while (0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020064#else
65#define DBG(...)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020066#endif /* MEMIF_DBG */
67
68typedef struct
69{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +020070 void *addr;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020071 uint32_t region_size;
Jakub Grajciar3744fc72018-03-29 13:15:10 +020072 uint32_t buffer_offset;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020073 int fd;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +020074 uint8_t is_external;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020075} memif_region_t;
76
77typedef struct
78{
79 memif_ring_t *ring;
80 uint8_t log2_ring_size;
81 uint8_t region;
82 uint32_t offset;
83
84 uint16_t last_head;
85 uint16_t last_tail;
86
87 int int_fd;
88
89 uint64_t int_count;
90 uint32_t alloc_bufs;
91} memif_queue_t;
92
93typedef struct memif_msg_queue_elt
94{
95 memif_msg_t msg;
96 int fd;
97 struct memif_msg_queue_elt *next;
98} memif_msg_queue_elt_t;
99
100struct memif_connection;
101
102typedef struct memif_connection memif_connection_t;
103
104/* functions called by memif_control_fd_handler */
105typedef int (memif_fn) (memif_connection_t * conn);
106
107typedef struct
108{
109 uint8_t num_s2m_rings;
110 uint8_t num_m2s_rings;
111 uint16_t buffer_size;
112 memif_log2_ring_size_t log2_ring_size;
113} memif_conn_run_args_t;
114
115typedef struct memif_connection
116{
117 uint16_t index;
118 memif_conn_args_t args;
119 memif_conn_run_args_t run_args;
120
121 int fd;
122 int listener_fd;
123
124 memif_fn *write_fn, *read_fn, *error_fn;
125
126 memif_connection_update_t *on_connect, *on_disconnect;
127 memif_interrupt_t *on_interrupt;
128 void *private_ctx;
129
130 /* connection message queue */
131 memif_msg_queue_elt_t *msg_queue;
132
Jakub Grajciar19418712018-03-13 13:57:50 +0100133 uint8_t remote_if_name[MEMIF_NAME_LEN];
134 uint8_t remote_name[MEMIF_NAME_LEN];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200135 uint8_t remote_disconnect_string[96];
136
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200137 uint8_t regions_num;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200138 memif_region_t *regions;
139
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200140 uint8_t rx_queues_num;
141 uint8_t tx_queues_num;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200142 memif_queue_t *rx_queues;
143 memif_queue_t *tx_queues;
144
145 uint16_t flags;
146#define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
147} memif_connection_t;
148
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200149typedef struct
150{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200151 int key;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200152 void *data_struct;
153} memif_list_elt_t;
154
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200155typedef struct
156{
157 int fd;
158 uint16_t use_count;
159 uint8_t *filename;
160 uint16_t interface_list_len;
161 memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
162} memif_socket_t;
163
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200164typedef struct
165{
166 memif_control_fd_update_t *control_fd_update;
167 int timerfd;
168 struct itimerspec arm, disarm;
169 uint16_t disconn_slaves;
Jakub Grajciar19418712018-03-13 13:57:50 +0100170 uint8_t app_name[MEMIF_NAME_LEN];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200171
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200172 memif_add_external_region_t *add_external_region;
173 memif_get_external_region_addr_t *get_external_region_addr;
174 memif_del_external_region_t *del_external_region;
175 memif_get_external_buffer_offset_t *get_external_buffer_offset;
176
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200177 memif_alloc_t *alloc;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200178 memif_realloc_t *realloc;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200179 memif_free_t *free;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200180
181 uint16_t control_list_len;
182 uint16_t interrupt_list_len;
183 uint16_t listener_list_len;
184 uint16_t pending_list_len;
185 memif_list_elt_t *control_list;
186 memif_list_elt_t *interrupt_list;
187 memif_list_elt_t *listener_list;
188 memif_list_elt_t *pending_list;
189} libmemif_main_t;
190
191extern libmemif_main_t libmemif_main;
192extern int memif_epfd;
193
194/* main.c */
195
196/* if region doesn't contain shared memory, mmap region, check ring cookie */
197int memif_connect1 (memif_connection_t * c);
198
199/* memory map region, initalize rings and queues */
200int memif_init_regions_and_queues (memif_connection_t * c);
201
202int memif_disconnect_internal (memif_connection_t * c);
203
204/* map errno to memif error code */
205int memif_syscall_error_handler (int err_code);
206
207int add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list,
208 uint16_t * len);
209
210int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list,
211 uint16_t len, int key);
212
213int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
214
215#ifndef __NR_memfd_create
216#if defined __x86_64__
217#define __NR_memfd_create 319
218#elif defined __arm__
219#define __NR_memfd_create 385
220#elif defined __aarch64__
221#define __NR_memfd_create 279
222#else
223#error "__NR_memfd_create unknown for this architecture"
224#endif
225#endif
226
Jakub Grajciar19418712018-03-13 13:57:50 +0100227#ifndef HAVE_MEMFD_CREATE
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200228static inline int
Damjan Marion23d4e8a2018-03-26 14:09:38 +0200229memfd_create (const char *name, unsigned int flags)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200230{
231 return syscall (__NR_memfd_create, name, flags);
232}
Jakub Grajciar19418712018-03-13 13:57:50 +0100233#endif
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200234
235static inline void *
236memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring,
237 uint16_t index)
238{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200239 return (conn->regions[ring->desc[index].region].addr +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200240 ring->desc[index].offset);
241}
242
243#ifndef F_LINUX_SPECIFIC_BASE
244#define F_LINUX_SPECIFIC_BASE 1024
245#endif
Jakub Grajciar19418712018-03-13 13:57:50 +0100246
247#ifndef MFD_ALLOW_SEALING
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200248#define MFD_ALLOW_SEALING 0x0002U
Jakub Grajciar19418712018-03-13 13:57:50 +0100249#endif
250
251#ifndef F_ADD_SEALS
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200252#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
253#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
254
255#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
256#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
257#define F_SEAL_GROW 0x0004 /* prevent file from growing */
258#define F_SEAL_WRITE 0x0008 /* prevent writes */
Jakub Grajciar19418712018-03-13 13:57:50 +0100259#endif
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200260
261#endif /* _MEMIF_PRIVATE_H_ */