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 | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 30 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 31 | #include <memif.h> |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 32 | #include <libmemif.h> |
| 33 | |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 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"); |
| 37 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 38 | #define MEMIF_DEFAULT_SOCKET_PATH "/run/vpp/memif.sock" |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 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 |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 44 | #define MEMIF_DEFAULT_RECONNECT_PERIOD_SEC 2 |
| 45 | #define MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC 0 |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 46 | |
| 47 | #define MEMIF_MAX_M2S_RING 255 |
| 48 | #define MEMIF_MAX_S2M_RING 255 |
| 49 | #define MEMIF_MAX_REGION 255 |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 50 | #define MEMIF_MAX_LOG2_RING_SIZE 14 |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 51 | |
| 52 | #define MEMIF_MAX_FDS 512 |
| 53 | |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 54 | #define memif_min(a,b) (((a) < (b)) ? (a) : (b)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 55 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 56 | #define EXPECT_TRUE(x) __builtin_expect((x),1) |
| 57 | #define EXPECT_FALSE(x) __builtin_expect((x),0) |
| 58 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 59 | #ifdef MEMIF_DBG |
| 60 | #define DBG(...) do { \ |
| 61 | printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \ |
| 62 | printf(__VA_ARGS__); \ |
| 63 | printf("\n"); \ |
| 64 | } while (0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 65 | #else |
| 66 | #define DBG(...) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 67 | #endif /* MEMIF_DBG */ |
| 68 | |
Andrew Yourtchenko | e5b7ca4 | 2021-01-29 14:18:12 +0000 | [diff] [blame] | 69 | #ifndef HAS_LIB_BSD |
| 70 | static inline size_t |
| 71 | strlcpy (char *dest, const char *src, size_t len) |
| 72 | { |
| 73 | const char *s = src; |
| 74 | size_t n = len; |
| 75 | |
| 76 | while (--n > 0) |
| 77 | { |
| 78 | if ((*dest++ = *s++) == '\0') |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | if (n == 0) |
| 83 | { |
| 84 | if (len != 0) |
| 85 | *dest = '\0'; |
| 86 | while (*s++) |
| 87 | ; |
| 88 | } |
| 89 | |
| 90 | return (s - src - 1); |
| 91 | } |
| 92 | #else |
| 93 | #include <bsd/string.h> |
| 94 | #endif |
| 95 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 96 | typedef enum |
| 97 | { |
| 98 | MEMIF_SOCKET_TYPE_NONE = 0, /* unassigned, not used by any interface */ |
| 99 | MEMIF_SOCKET_TYPE_LISTENER, /* listener socket, master interface assigned */ |
| 100 | MEMIF_SOCKET_TYPE_CLIENT /* client socket, slave interface assigned */ |
| 101 | } memif_socket_type_t; |
| 102 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 103 | typedef struct |
| 104 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 105 | void *addr; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 106 | uint32_t region_size; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 107 | uint32_t buffer_offset; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 108 | int fd; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 109 | uint8_t is_external; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 110 | } memif_region_t; |
| 111 | |
| 112 | typedef struct |
| 113 | { |
| 114 | memif_ring_t *ring; |
| 115 | uint8_t log2_ring_size; |
| 116 | uint8_t region; |
| 117 | uint32_t offset; |
| 118 | |
| 119 | uint16_t last_head; |
| 120 | uint16_t last_tail; |
| 121 | |
| 122 | int int_fd; |
| 123 | |
| 124 | uint64_t int_count; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 125 | uint32_t next_buf; /* points to next free buffer */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 126 | } memif_queue_t; |
| 127 | |
| 128 | typedef struct memif_msg_queue_elt |
| 129 | { |
| 130 | memif_msg_t msg; |
| 131 | int fd; |
| 132 | struct memif_msg_queue_elt *next; |
| 133 | } memif_msg_queue_elt_t; |
| 134 | |
| 135 | struct memif_connection; |
| 136 | |
| 137 | typedef struct memif_connection memif_connection_t; |
| 138 | |
| 139 | /* functions called by memif_control_fd_handler */ |
| 140 | typedef int (memif_fn) (memif_connection_t * conn); |
| 141 | |
| 142 | typedef struct |
| 143 | { |
| 144 | uint8_t num_s2m_rings; |
| 145 | uint8_t num_m2s_rings; |
| 146 | uint16_t buffer_size; |
| 147 | memif_log2_ring_size_t log2_ring_size; |
| 148 | } memif_conn_run_args_t; |
| 149 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 150 | struct libmemif_main; |
| 151 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 152 | typedef struct memif_connection |
| 153 | { |
| 154 | uint16_t index; |
| 155 | memif_conn_args_t args; |
| 156 | memif_conn_run_args_t run_args; |
| 157 | |
| 158 | int fd; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 159 | |
| 160 | memif_fn *write_fn, *read_fn, *error_fn; |
| 161 | |
| 162 | memif_connection_update_t *on_connect, *on_disconnect; |
| 163 | memif_interrupt_t *on_interrupt; |
| 164 | void *private_ctx; |
| 165 | |
| 166 | /* connection message queue */ |
| 167 | memif_msg_queue_elt_t *msg_queue; |
| 168 | |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 169 | uint8_t remote_if_name[MEMIF_NAME_LEN]; |
| 170 | uint8_t remote_name[MEMIF_NAME_LEN]; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 171 | uint8_t remote_disconnect_string[96]; |
| 172 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 173 | uint8_t regions_num; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 174 | memif_region_t *regions; |
| 175 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 176 | uint8_t rx_queues_num; |
| 177 | uint8_t tx_queues_num; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 178 | memif_queue_t *rx_queues; |
| 179 | memif_queue_t *tx_queues; |
| 180 | |
| 181 | uint16_t flags; |
| 182 | #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0) |
| 183 | } memif_connection_t; |
| 184 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 185 | typedef struct |
| 186 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 187 | int key; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 188 | void *data_struct; |
| 189 | } memif_list_elt_t; |
| 190 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 191 | typedef struct |
| 192 | { |
| 193 | int fd; |
| 194 | uint16_t use_count; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 195 | memif_socket_type_t type; |
Jakub Grajciar | 57084e5 | 2021-03-01 08:45:17 +0100 | [diff] [blame] | 196 | uint8_t filename[108]; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 197 | /* unique database */ |
| 198 | struct libmemif_main *lm; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 199 | uint16_t interface_list_len; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 200 | void *private_ctx; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 201 | memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */ |
| 202 | } memif_socket_t; |
| 203 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 204 | typedef struct libmemif_main |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 205 | { |
| 206 | memif_control_fd_update_t *control_fd_update; |
| 207 | int timerfd; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 208 | int epfd; |
| 209 | int poll_cancel_fd; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 210 | struct itimerspec arm, disarm; |
| 211 | uint16_t disconn_slaves; |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 212 | uint8_t app_name[MEMIF_NAME_LEN]; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 213 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 214 | void *private_ctx; |
| 215 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 216 | memif_socket_handle_t default_socket; |
| 217 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 218 | memif_add_external_region_t *add_external_region; |
| 219 | memif_get_external_region_addr_t *get_external_region_addr; |
| 220 | memif_del_external_region_t *del_external_region; |
| 221 | memif_get_external_buffer_offset_t *get_external_buffer_offset; |
| 222 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 223 | memif_alloc_t *alloc; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 224 | memif_realloc_t *realloc; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 225 | memif_free_t *free; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 226 | |
| 227 | uint16_t control_list_len; |
| 228 | uint16_t interrupt_list_len; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 229 | uint16_t socket_list_len; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 230 | uint16_t pending_list_len; |
| 231 | memif_list_elt_t *control_list; |
| 232 | memif_list_elt_t *interrupt_list; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 233 | memif_list_elt_t *socket_list; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 234 | memif_list_elt_t *pending_list; |
| 235 | } libmemif_main_t; |
| 236 | |
| 237 | extern libmemif_main_t libmemif_main; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 238 | |
| 239 | /* main.c */ |
| 240 | |
| 241 | /* if region doesn't contain shared memory, mmap region, check ring cookie */ |
| 242 | int memif_connect1 (memif_connection_t * c); |
| 243 | |
Paul Vinciguerra | 6223766 | 2020-05-29 23:03:06 -0400 | [diff] [blame] | 244 | /* memory map region, initialize rings and queues */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 245 | int memif_init_regions_and_queues (memif_connection_t * c); |
| 246 | |
| 247 | int memif_disconnect_internal (memif_connection_t * c); |
| 248 | |
| 249 | /* map errno to memif error code */ |
| 250 | int memif_syscall_error_handler (int err_code); |
| 251 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 252 | int add_list_elt (libmemif_main_t *lm, memif_list_elt_t * e, memif_list_elt_t ** list, |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 253 | uint16_t * len); |
| 254 | |
| 255 | int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list, |
| 256 | uint16_t len, int key); |
| 257 | |
| 258 | int free_list_elt (memif_list_elt_t * list, uint16_t len, int key); |
| 259 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 260 | libmemif_main_t *get_libmemif_main (memif_socket_t * ms); |
| 261 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 262 | #ifndef __NR_memfd_create |
| 263 | #if defined __x86_64__ |
| 264 | #define __NR_memfd_create 319 |
| 265 | #elif defined __arm__ |
| 266 | #define __NR_memfd_create 385 |
| 267 | #elif defined __aarch64__ |
| 268 | #define __NR_memfd_create 279 |
| 269 | #else |
| 270 | #error "__NR_memfd_create unknown for this architecture" |
| 271 | #endif |
| 272 | #endif |
| 273 | |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 274 | #ifndef HAVE_MEMFD_CREATE |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 275 | static inline int |
Damjan Marion | 23d4e8a | 2018-03-26 14:09:38 +0200 | [diff] [blame] | 276 | memfd_create (const char *name, unsigned int flags) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 277 | { |
| 278 | return syscall (__NR_memfd_create, name, flags); |
| 279 | } |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 280 | #endif |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 281 | |
| 282 | static inline void * |
| 283 | memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring, |
| 284 | uint16_t index) |
| 285 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 286 | return (conn->regions[ring->desc[index].region].addr + |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 287 | ring->desc[index].offset); |
| 288 | } |
| 289 | |
| 290 | #ifndef F_LINUX_SPECIFIC_BASE |
| 291 | #define F_LINUX_SPECIFIC_BASE 1024 |
| 292 | #endif |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 293 | |
| 294 | #ifndef MFD_ALLOW_SEALING |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 295 | #define MFD_ALLOW_SEALING 0x0002U |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 296 | #endif |
| 297 | |
| 298 | #ifndef F_ADD_SEALS |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 299 | #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) |
| 300 | #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10) |
| 301 | |
| 302 | #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */ |
| 303 | #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */ |
| 304 | #define F_SEAL_GROW 0x0004 /* prevent file from growing */ |
| 305 | #define F_SEAL_WRITE 0x0008 /* prevent writes */ |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 306 | #endif |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 307 | |
| 308 | #endif /* _MEMIF_PRIVATE_H_ */ |