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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
19 #ifndef _MEMIF_PRIVATE_H_
20 #define _MEMIF_PRIVATE_H_
24 #include <sys/syscall.h>
28 #include <sys/timerfd.h>
34 #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");
38 #define MEMIF_DEFAULT_SOCKET_PATH "/run/vpp/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 #define MEMIF_DEFAULT_RECONNECT_PERIOD_SEC 2
45 #define MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC 0
47 #define MEMIF_MAX_M2S_RING 255
48 #define MEMIF_MAX_S2M_RING 255
49 #define MEMIF_MAX_REGION 255
50 #define MEMIF_MAX_LOG2_RING_SIZE 14
52 #define MEMIF_MAX_FDS 512
54 #define memif_min(a,b) (((a) < (b)) ? (a) : (b))
56 #define EXPECT_TRUE(x) __builtin_expect((x),1)
57 #define EXPECT_FALSE(x) __builtin_expect((x),0)
60 #define DBG(...) do { \
61 printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
62 printf(__VA_ARGS__); \
67 #endif /* MEMIF_DBG */
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;
80 uint32_t buffer_offset;
88 uint8_t log2_ring_size;
101 typedef struct memif_msg_queue_elt
105 struct memif_msg_queue_elt *next;
106 } memif_msg_queue_elt_t;
108 struct memif_connection;
110 typedef struct memif_connection memif_connection_t;
112 /* functions called by memif_control_fd_handler */
113 typedef int (memif_fn) (memif_connection_t * conn);
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;
123 struct libmemif_main;
125 typedef struct memif_connection
128 memif_conn_args_t args;
129 memif_conn_run_args_t run_args;
133 memif_fn *write_fn, *read_fn, *error_fn;
135 memif_connection_update_t *on_connect, *on_disconnect;
136 memif_interrupt_t *on_interrupt;
139 /* connection message queue */
140 memif_msg_queue_elt_t *msg_queue;
142 uint8_t remote_if_name[MEMIF_NAME_LEN];
143 uint8_t remote_name[MEMIF_NAME_LEN];
144 uint8_t remote_disconnect_string[96];
147 memif_region_t *regions;
149 uint8_t rx_queues_num;
150 uint8_t tx_queues_num;
151 memif_queue_t *rx_queues;
152 memif_queue_t *tx_queues;
155 #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
156 } memif_connection_t;
168 memif_socket_type_t type;
170 /* unique database */
171 struct libmemif_main *lm;
172 uint16_t interface_list_len;
174 memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
177 typedef struct libmemif_main
179 memif_control_fd_update_t *control_fd_update;
183 struct itimerspec arm, disarm;
184 uint16_t disconn_slaves;
185 uint8_t app_name[MEMIF_NAME_LEN];
189 memif_socket_handle_t default_socket;
191 memif_add_external_region_t *add_external_region;
192 memif_get_external_region_addr_t *get_external_region_addr;
193 memif_del_external_region_t *del_external_region;
194 memif_get_external_buffer_offset_t *get_external_buffer_offset;
196 memif_alloc_t *alloc;
197 memif_realloc_t *realloc;
200 uint16_t control_list_len;
201 uint16_t interrupt_list_len;
202 uint16_t socket_list_len;
203 uint16_t pending_list_len;
204 memif_list_elt_t *control_list;
205 memif_list_elt_t *interrupt_list;
206 memif_list_elt_t *socket_list;
207 memif_list_elt_t *pending_list;
210 extern libmemif_main_t libmemif_main;
214 /* if region doesn't contain shared memory, mmap region, check ring cookie */
215 int memif_connect1 (memif_connection_t * c);
217 /* memory map region, initalize rings and queues */
218 int memif_init_regions_and_queues (memif_connection_t * c);
220 int memif_disconnect_internal (memif_connection_t * c);
222 /* map errno to memif error code */
223 int memif_syscall_error_handler (int err_code);
225 int add_list_elt (libmemif_main_t *lm, memif_list_elt_t * e, memif_list_elt_t ** list,
228 int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list,
229 uint16_t len, int key);
231 int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
233 libmemif_main_t *get_libmemif_main (memif_socket_t * ms);
235 #ifndef __NR_memfd_create
236 #if defined __x86_64__
237 #define __NR_memfd_create 319
238 #elif defined __arm__
239 #define __NR_memfd_create 385
240 #elif defined __aarch64__
241 #define __NR_memfd_create 279
243 #error "__NR_memfd_create unknown for this architecture"
247 #ifndef HAVE_MEMFD_CREATE
249 memfd_create (const char *name, unsigned int flags)
251 return syscall (__NR_memfd_create, name, flags);
256 memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring,
259 return (conn->regions[ring->desc[index].region].addr +
260 ring->desc[index].offset);
263 #ifndef F_LINUX_SPECIFIC_BASE
264 #define F_LINUX_SPECIFIC_BASE 1024
267 #ifndef MFD_ALLOW_SEALING
268 #define MFD_ALLOW_SEALING 0x0002U
272 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
273 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
275 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
276 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
277 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
278 #define F_SEAL_WRITE 0x0008 /* prevent writes */
281 #endif /* _MEMIF_PRIVATE_H_ */