Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1 | /* |
| 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 Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 29 | #include <string.h> |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 30 | #include <sys/queue.h> |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 31 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 32 | #include <memif.h> |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 33 | #include <libmemif.h> |
| 34 | |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 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"); |
| 38 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 39 | #define MEMIF_DEFAULT_SOCKET_PATH "/run/vpp/memif.sock" |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 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 |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 45 | #define MEMIF_DEFAULT_RECONNECT_PERIOD_SEC 2 |
| 46 | #define MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC 0 |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 47 | |
| 48 | #define MEMIF_MAX_M2S_RING 255 |
| 49 | #define MEMIF_MAX_S2M_RING 255 |
| 50 | #define MEMIF_MAX_REGION 255 |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 51 | #define MEMIF_MAX_LOG2_RING_SIZE 14 |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 52 | |
| 53 | #define MEMIF_MAX_FDS 512 |
| 54 | |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 55 | #define memif_min(a,b) (((a) < (b)) ? (a) : (b)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 56 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 57 | #define EXPECT_TRUE(x) __builtin_expect((x),1) |
| 58 | #define EXPECT_FALSE(x) __builtin_expect((x),0) |
| 59 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 60 | #ifdef MEMIF_DBG |
| 61 | #define DBG(...) do { \ |
| 62 | printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \ |
| 63 | printf(__VA_ARGS__); \ |
| 64 | printf("\n"); \ |
| 65 | } while (0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 66 | #else |
| 67 | #define DBG(...) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 68 | #endif /* MEMIF_DBG */ |
| 69 | |
Andrew Yourtchenko | e5b7ca4 | 2021-01-29 14:18:12 +0000 | [diff] [blame] | 70 | #ifndef HAS_LIB_BSD |
| 71 | static inline size_t |
| 72 | strlcpy (char *dest, const char *src, size_t len) |
| 73 | { |
| 74 | const char *s = src; |
| 75 | size_t n = len; |
| 76 | |
| 77 | while (--n > 0) |
| 78 | { |
| 79 | if ((*dest++ = *s++) == '\0') |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | if (n == 0) |
| 84 | { |
| 85 | if (len != 0) |
| 86 | *dest = '\0'; |
| 87 | while (*s++) |
| 88 | ; |
| 89 | } |
| 90 | |
| 91 | return (s - src - 1); |
| 92 | } |
| 93 | #else |
| 94 | #include <bsd/string.h> |
| 95 | #endif |
| 96 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 97 | typedef enum |
| 98 | { |
| 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; |
| 103 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 104 | typedef struct |
| 105 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 106 | void *addr; |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 107 | memif_region_size_t region_size; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 108 | uint32_t buffer_offset; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 109 | int fd; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 110 | uint8_t is_external; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 111 | } memif_region_t; |
| 112 | |
| 113 | typedef struct |
| 114 | { |
| 115 | memif_ring_t *ring; |
| 116 | uint8_t log2_ring_size; |
| 117 | uint8_t region; |
| 118 | uint32_t offset; |
| 119 | |
| 120 | uint16_t last_head; |
| 121 | uint16_t last_tail; |
| 122 | |
| 123 | int int_fd; |
| 124 | |
| 125 | uint64_t int_count; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 126 | uint32_t next_buf; /* points to next free buffer */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 127 | } memif_queue_t; |
| 128 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 129 | struct memif_connection; |
| 130 | |
| 131 | typedef struct memif_connection memif_connection_t; |
| 132 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 133 | typedef struct |
| 134 | { |
| 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; |
| 140 | |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 141 | struct memif_control_channel; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 142 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 143 | typedef struct memif_connection |
| 144 | { |
| 145 | uint16_t index; |
| 146 | memif_conn_args_t args; |
| 147 | memif_conn_run_args_t run_args; |
| 148 | |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 149 | struct memif_control_channel *control_channel; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 150 | |
| 151 | memif_connection_update_t *on_connect, *on_disconnect; |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 152 | memif_on_interrupt_t *on_interrupt; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 153 | void *private_ctx; |
| 154 | |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 155 | uint8_t remote_if_name[MEMIF_NAME_LEN]; |
| 156 | uint8_t remote_name[MEMIF_NAME_LEN]; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 157 | uint8_t remote_disconnect_string[96]; |
| 158 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 159 | uint8_t regions_num; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 160 | memif_region_t *regions; |
| 161 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 162 | uint8_t rx_queues_num; |
| 163 | uint8_t tx_queues_num; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 164 | memif_queue_t *rx_queues; |
| 165 | memif_queue_t *tx_queues; |
| 166 | |
| 167 | uint16_t flags; |
| 168 | #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0) |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 169 | |
| 170 | TAILQ_ENTRY (memif_connection) next; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 171 | } memif_connection_t; |
| 172 | |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 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 |
| 177 | */ |
| 178 | typedef struct memif_msg_queue_elt |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 179 | { |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 180 | memif_msg_t msg; |
| 181 | TAILQ_ENTRY (memif_msg_queue_elt) next; |
| 182 | int fd; |
| 183 | } memif_msg_queue_elt_t; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 184 | |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 185 | struct memif_socket; |
| 186 | |
| 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 |
| 192 | * |
| 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. |
| 196 | */ |
| 197 | typedef struct memif_control_channel |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 198 | { |
| 199 | int fd; |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 200 | TAILQ_HEAD (, memif_msg_queue_elt) msg_queue; |
| 201 | memif_connection_t *conn; |
| 202 | struct memif_socket *sock; |
| 203 | } memif_control_channel_t; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 204 | |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 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 |
| 214 | */ |
| 215 | typedef struct memif_socket |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 216 | { |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 217 | memif_socket_args_t args; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 218 | int epfd; |
| 219 | int poll_cancel_fd; |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 220 | int listener_fd; |
| 221 | int timer_fd; |
| 222 | struct itimerspec timer; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 223 | void *private_ctx; |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 224 | TAILQ_HEAD (, memif_connection) master_interfaces; |
| 225 | TAILQ_HEAD (, memif_connection) slave_interfaces; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 226 | |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 227 | /* External region callbacks */ |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 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; |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 232 | } memif_socket_t; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 233 | |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 234 | typedef int (memif_fd_event_handler_t) (memif_fd_event_type_t type, |
| 235 | void *private_ctx); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 236 | |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 237 | typedef struct memif_fd_event_data |
| 238 | { |
| 239 | memif_fd_event_handler_t *event_handler; |
| 240 | void *private_ctx; |
| 241 | } memif_fd_event_data_t; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 242 | |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 243 | typedef struct memif_interrupt |
| 244 | { |
| 245 | memif_connection_t *c; |
| 246 | uint16_t qid; |
| 247 | } memif_interrupt_t; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 248 | |
| 249 | /* main.c */ |
| 250 | |
| 251 | /* if region doesn't contain shared memory, mmap region, check ring cookie */ |
| 252 | int memif_connect1 (memif_connection_t * c); |
| 253 | |
Paul Vinciguerra | 6223766 | 2020-05-29 23:03:06 -0400 | [diff] [blame] | 254 | /* memory map region, initialize rings and queues */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 255 | int memif_init_regions_and_queues (memif_connection_t * c); |
| 256 | |
| 257 | int memif_disconnect_internal (memif_connection_t * c); |
| 258 | |
Jakub Grajciar | 134f1e0 | 2021-01-04 11:10:42 +0100 | [diff] [blame] | 259 | int memif_interrupt_handler (memif_fd_event_type_t type, void *private_ctx); |
| 260 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 261 | /* map errno to memif error code */ |
| 262 | int memif_syscall_error_handler (int err_code); |
| 263 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 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 |
| 271 | #else |
| 272 | #error "__NR_memfd_create unknown for this architecture" |
| 273 | #endif |
| 274 | #endif |
| 275 | |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 276 | #ifndef HAVE_MEMFD_CREATE |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 277 | static inline int |
Damjan Marion | 23d4e8a | 2018-03-26 14:09:38 +0200 | [diff] [blame] | 278 | memfd_create (const char *name, unsigned int flags) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 279 | { |
| 280 | return syscall (__NR_memfd_create, name, flags); |
| 281 | } |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 282 | #endif |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 283 | |
| 284 | static inline void * |
| 285 | memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring, |
| 286 | uint16_t index) |
| 287 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 288 | return (conn->regions[ring->desc[index].region].addr + |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 289 | ring->desc[index].offset); |
| 290 | } |
| 291 | |
| 292 | #ifndef F_LINUX_SPECIFIC_BASE |
| 293 | #define F_LINUX_SPECIFIC_BASE 1024 |
| 294 | #endif |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 295 | |
| 296 | #ifndef MFD_ALLOW_SEALING |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 297 | #define MFD_ALLOW_SEALING 0x0002U |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 298 | #endif |
| 299 | |
| 300 | #ifndef F_ADD_SEALS |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 301 | #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) |
| 302 | #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10) |
| 303 | |
| 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 */ |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 308 | #endif |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 309 | |
| 310 | #endif /* _MEMIF_PRIVATE_H_ */ |