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>
30 #include <sys/queue.h>
35 #define MEMIF_NAME_LEN 32
36 _Static_assert (strlen (MEMIF_DEFAULT_APP_NAME) <= MEMIF_NAME_LEN,
37 "MEMIF_DEFAULT_APP_NAME max length is 32");
39 #define MEMIF_DEFAULT_SOCKET_PATH "/run/vpp/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 #define MEMIF_DEFAULT_RECONNECT_PERIOD_SEC 2
46 #define MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC 0
48 #define MEMIF_MAX_M2S_RING 255
49 #define MEMIF_MAX_S2M_RING 255
50 #define MEMIF_MAX_REGION 255
51 #define MEMIF_MAX_LOG2_RING_SIZE 14
53 #define MEMIF_MAX_FDS 512
55 #define memif_min(a,b) (((a) < (b)) ? (a) : (b))
57 #define EXPECT_TRUE(x) __builtin_expect((x),1)
58 #define EXPECT_FALSE(x) __builtin_expect((x),0)
61 #define DBG(...) do { \
62 printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
63 printf(__VA_ARGS__); \
68 #endif /* MEMIF_DBG */
72 strlcpy (char *dest, const char *src, size_t len)
79 if ((*dest++ = *s++) == '\0')
94 #include <bsd/string.h>
99 MEMIF_SOCKET_TYPE_NONE = 0, /* unassigned, not used by any interface */
100 MEMIF_SOCKET_TYPE_LISTENER, /* listener socket, master interface assigned */
101 MEMIF_SOCKET_TYPE_CLIENT /* client socket, slave interface assigned */
102 } memif_socket_type_t;
107 memif_region_size_t region_size;
108 uint32_t buffer_offset;
116 uint8_t log2_ring_size;
126 uint32_t next_buf; /* points to next free buffer */
129 struct memif_connection;
131 typedef struct memif_connection memif_connection_t;
135 uint8_t num_s2m_rings;
136 uint8_t num_m2s_rings;
137 uint16_t buffer_size;
138 memif_log2_ring_size_t log2_ring_size;
139 } memif_conn_run_args_t;
141 struct memif_control_channel;
143 typedef struct memif_connection
146 memif_conn_args_t args;
147 memif_conn_run_args_t run_args;
149 struct memif_control_channel *control_channel;
151 memif_connection_update_t *on_connect, *on_disconnect;
152 memif_on_interrupt_t *on_interrupt;
155 uint8_t remote_if_name[MEMIF_NAME_LEN];
156 uint8_t remote_name[MEMIF_NAME_LEN];
157 uint8_t remote_disconnect_string[96];
160 memif_region_t *regions;
162 uint8_t rx_queues_num;
163 uint8_t tx_queues_num;
164 memif_queue_t *rx_queues;
165 memif_queue_t *tx_queues;
168 #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
170 TAILQ_ENTRY (memif_connection) next;
171 } memif_connection_t;
173 /** \brief Memif message queue element
174 * @param msg - memif control message (defined in memif.h)
175 * @param nex - tailq entry
176 * @param fd - File descriptor to be shared with peer endpoint
178 typedef struct memif_msg_queue_elt
181 TAILQ_ENTRY (memif_msg_queue_elt) next;
183 } memif_msg_queue_elt_t;
187 /** \brief Memif control channel
188 * @param fd - fd used for communbication
189 * @param msg_queue - message queue
190 * @param conn - memif connection using this control channel
191 * @param sock - socket this control channel belongs to
193 * Memif controll channel represents one end of communication between two memif
194 * endpoints. The controll channel is responsible for receiving and
195 * transmitting memif control messages via UNIX domain socket.
197 typedef struct memif_control_channel
200 TAILQ_HEAD (, memif_msg_queue_elt) msg_queue;
201 memif_connection_t *conn;
202 struct memif_socket *sock;
203 } memif_control_channel_t;
205 /** \brief Memif socket
206 * @param args - memif socket arguments (from libmemif.h)
207 * @param epfd - epoll fd, used for internal fd polling
208 * @param poll_cancel_fd - if event is received on this fd, interrupt polling
209 * @param listener_fd - listener fd if this socket is listener else -1
210 * @param private_ctx - private context
211 * @param master_interfaces - master interface queue
212 * @param slave_interfaces - slave interface queue
213 * @param control_channels - controll channel queue
215 typedef struct memif_socket
217 memif_socket_args_t args;
222 struct itimerspec timer;
224 TAILQ_HEAD (, memif_connection) master_interfaces;
225 TAILQ_HEAD (, memif_connection) slave_interfaces;
227 /* External region callbacks */
228 memif_add_external_region_t *add_external_region;
229 memif_get_external_region_addr_t *get_external_region_addr;
230 memif_del_external_region_t *del_external_region;
231 memif_get_external_buffer_offset_t *get_external_buffer_offset;
234 typedef int (memif_fd_event_handler_t) (memif_fd_event_type_t type,
237 typedef struct memif_fd_event_data
239 memif_fd_event_handler_t *event_handler;
241 } memif_fd_event_data_t;
243 typedef struct memif_interrupt
245 memif_connection_t *c;
251 /* if region doesn't contain shared memory, mmap region, check ring cookie */
252 int memif_connect1 (memif_connection_t * c);
254 /* memory map region, initialize rings and queues */
255 int memif_init_regions_and_queues (memif_connection_t * c);
257 int memif_disconnect_internal (memif_connection_t * c);
259 int memif_interrupt_handler (memif_fd_event_type_t type, void *private_ctx);
261 /* map errno to memif error code */
262 int memif_syscall_error_handler (int err_code);
264 #ifndef __NR_memfd_create
265 #if defined __x86_64__
266 #define __NR_memfd_create 319
267 #elif defined __arm__
268 #define __NR_memfd_create 385
269 #elif defined __aarch64__
270 #define __NR_memfd_create 279
272 #error "__NR_memfd_create unknown for this architecture"
276 #ifndef HAVE_MEMFD_CREATE
278 memfd_create (const char *name, unsigned int flags)
280 return syscall (__NR_memfd_create, name, flags);
285 memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring,
288 return (conn->regions[ring->desc[index].region].addr +
289 ring->desc[index].offset);
292 #ifndef F_LINUX_SPECIFIC_BASE
293 #define F_LINUX_SPECIFIC_BASE 1024
296 #ifndef MFD_ALLOW_SEALING
297 #define MFD_ALLOW_SEALING 0x0002U
301 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
302 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
304 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
305 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
306 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
307 #define F_SEAL_WRITE 0x0008 /* prevent writes */
310 #endif /* _MEMIF_PRIVATE_H_ */