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_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
46 #define MEMIF_MAX_M2S_RING 255
47 #define MEMIF_MAX_S2M_RING 255
48 #define MEMIF_MAX_REGION 255
49 #define MEMIF_MAX_LOG2_RING_SIZE 14
51 #define MEMIF_MAX_FDS 512
53 #define memif_min(a,b) (((a) < (b)) ? (a) : (b))
55 #define EXPECT_TRUE(x) __builtin_expect((x),1)
56 #define EXPECT_FALSE(x) __builtin_expect((x),0)
59 #define DBG(...) do { \
60 printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
61 printf(__VA_ARGS__); \
66 #endif /* MEMIF_DBG */
72 uint32_t buffer_offset;
79 uint8_t log2_ring_size;
92 typedef struct memif_msg_queue_elt
96 struct memif_msg_queue_elt *next;
97 } memif_msg_queue_elt_t;
99 struct memif_connection;
101 typedef struct memif_connection memif_connection_t;
103 /* functions called by memif_control_fd_handler */
104 typedef int (memif_fn) (memif_connection_t * conn);
108 uint8_t num_s2m_rings;
109 uint8_t num_m2s_rings;
110 uint16_t buffer_size;
111 memif_log2_ring_size_t log2_ring_size;
112 } memif_conn_run_args_t;
114 typedef struct memif_connection
117 memif_conn_args_t args;
118 memif_conn_run_args_t run_args;
123 memif_fn *write_fn, *read_fn, *error_fn;
125 memif_connection_update_t *on_connect, *on_disconnect;
126 memif_interrupt_t *on_interrupt;
129 /* connection message queue */
130 memif_msg_queue_elt_t *msg_queue;
132 uint8_t remote_if_name[MEMIF_NAME_LEN];
133 uint8_t remote_name[MEMIF_NAME_LEN];
134 uint8_t remote_disconnect_string[96];
136 memif_region_t *regions;
138 memif_queue_t *rx_queues;
139 memif_queue_t *tx_queues;
142 #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
143 } memif_connection_t;
156 uint16_t interface_list_len;
157 memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
162 memif_control_fd_update_t *control_fd_update;
164 struct itimerspec arm, disarm;
165 uint16_t disconn_slaves;
166 uint8_t app_name[MEMIF_NAME_LEN];
168 memif_alloc_t *alloc;
171 uint16_t control_list_len;
172 uint16_t interrupt_list_len;
173 uint16_t listener_list_len;
174 uint16_t pending_list_len;
175 memif_list_elt_t *control_list;
176 memif_list_elt_t *interrupt_list;
177 memif_list_elt_t *listener_list;
178 memif_list_elt_t *pending_list;
181 extern libmemif_main_t libmemif_main;
182 extern int memif_epfd;
186 /* if region doesn't contain shared memory, mmap region, check ring cookie */
187 int memif_connect1 (memif_connection_t * c);
189 /* memory map region, initalize rings and queues */
190 int memif_init_regions_and_queues (memif_connection_t * c);
192 int memif_disconnect_internal (memif_connection_t * c);
194 /* map errno to memif error code */
195 int memif_syscall_error_handler (int err_code);
197 int add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list,
200 int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list,
201 uint16_t len, int key);
203 int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
205 #ifndef __NR_memfd_create
206 #if defined __x86_64__
207 #define __NR_memfd_create 319
208 #elif defined __arm__
209 #define __NR_memfd_create 385
210 #elif defined __aarch64__
211 #define __NR_memfd_create 279
213 #error "__NR_memfd_create unknown for this architecture"
217 #ifndef HAVE_MEMFD_CREATE
219 memfd_create (const char *name, unsigned int flags)
221 return syscall (__NR_memfd_create, name, flags);
226 memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring,
229 return (conn->regions[ring->desc[index].region].shm +
230 ring->desc[index].offset);
233 #ifndef F_LINUX_SPECIFIC_BASE
234 #define F_LINUX_SPECIFIC_BASE 1024
237 #ifndef MFD_ALLOW_SEALING
238 #define MFD_ALLOW_SEALING 0x0002U
242 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
243 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
245 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
246 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
247 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
248 #define F_SEAL_WRITE 0x0008 /* prevent writes */
251 #endif /* _MEMIF_PRIVATE_H_ */