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>
32 #define MEMIF_DEFAULT_SOCKET_DIR "/run/vpp"
33 #define MEMIF_DEFAULT_SOCKET_FILENAME "memif.sock"
34 #define MEMIF_DEFAULT_RING_SIZE 1024
35 #define MEMIF_DEFAULT_LOG2_RING_SIZE 10
36 #define MEMIF_DEFAULT_RX_QUEUES 1
37 #define MEMIF_DEFAULT_TX_QUEUES 1
38 #define MEMIF_DEFAULT_BUFFER_SIZE 2048
40 #define MEMIF_MAX_M2S_RING 255
41 #define MEMIF_MAX_S2M_RING 255
42 #define MEMIF_MAX_REGION 255
43 #define MEMIF_MAX_LOG2_RING_SIZE 15
45 #define MEMIF_MAX_FDS 512
47 #define memif_min(a,b) (((a) < (b)) ? (a) : (b))
50 #define DBG(...) do { \
51 printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
52 printf(__VA_ARGS__); \
56 #define DBG_UNIX(...) do { \
57 printf("MEMIF_DEBUG_UNIX:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
58 printf(__VA_ARGS__); \
62 #define error_return_unix(...) do { \
63 DBG_UNIX(__VA_ARGS__); \
66 #define error_return(...) do { \
73 #define error_return_unix(...) do { \
76 #define error_return(...) do { \
80 #endif /* MEMIF_DBG */
92 uint8_t log2_ring_size;
105 typedef struct memif_msg_queue_elt
109 struct memif_msg_queue_elt *next;
110 } memif_msg_queue_elt_t;
112 struct memif_connection;
114 typedef struct memif_connection memif_connection_t;
116 /* functions called by memif_control_fd_handler */
117 typedef int (memif_fn) (memif_connection_t * conn);
121 uint8_t num_s2m_rings;
122 uint8_t num_m2s_rings;
123 uint16_t buffer_size;
124 memif_log2_ring_size_t log2_ring_size;
125 } memif_conn_run_args_t;
127 typedef struct memif_connection
130 memif_conn_args_t args;
131 memif_conn_run_args_t run_args;
136 memif_fn *write_fn, *read_fn, *error_fn;
138 memif_connection_update_t *on_connect, *on_disconnect;
139 memif_interrupt_t *on_interrupt;
142 /* connection message queue */
143 memif_msg_queue_elt_t *msg_queue;
145 uint8_t remote_if_name[32];
146 uint8_t remote_name[32];
147 uint8_t remote_disconnect_string[96];
149 memif_region_t *regions;
151 memif_queue_t *rx_queues;
152 memif_queue_t *tx_queues;
155 #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
156 } memif_connection_t;
163 int key; /* fd or id */
175 uint16_t interface_list_len;
176 memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
182 /* probably function like memif_cleanup () will need to be called to close timerfd */
185 memif_control_fd_update_t *control_fd_update;
187 struct itimerspec arm, disarm;
188 uint16_t disconn_slaves;
191 /* master implementation... */
194 uint16_t control_list_len;
195 uint16_t interrupt_list_len;
196 uint16_t listener_list_len;
197 uint16_t pending_list_len;
198 memif_list_elt_t *control_list;
199 memif_list_elt_t *interrupt_list;
200 memif_list_elt_t *listener_list;
201 memif_list_elt_t *pending_list;
204 extern libmemif_main_t libmemif_main;
205 extern int memif_epfd;
209 /* if region doesn't contain shared memory, mmap region, check ring cookie */
210 int memif_connect1 (memif_connection_t * c);
212 /* memory map region, initalize rings and queues */
213 int memif_init_regions_and_queues (memif_connection_t * c);
215 int memif_disconnect_internal (memif_connection_t * c);
217 /* map errno to memif error code */
218 int memif_syscall_error_handler (int err_code);
220 int add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list,
223 int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list,
224 uint16_t len, int key);
226 int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
228 #ifndef __NR_memfd_create
229 #if defined __x86_64__
230 #define __NR_memfd_create 319
231 #elif defined __arm__
232 #define __NR_memfd_create 385
233 #elif defined __aarch64__
234 #define __NR_memfd_create 279
236 #error "__NR_memfd_create unknown for this architecture"
241 memfd_create (const char *name, unsigned int flags)
243 return syscall (__NR_memfd_create, name, flags);
247 memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring,
250 return (conn->regions[ring->desc[index].region].shm +
251 ring->desc[index].offset);
254 #ifndef F_LINUX_SPECIFIC_BASE
255 #define F_LINUX_SPECIFIC_BASE 1024
257 #define MFD_ALLOW_SEALING 0x0002U
258 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
259 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
261 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
262 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
263 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
264 #define F_SEAL_WRITE 0x0008 /* prevent writes */
266 #endif /* _MEMIF_PRIVATE_H_ */