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 | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 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 |
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 | |
| 70 | typedef struct |
| 71 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 72 | void *addr; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 73 | uint32_t region_size; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 74 | uint32_t buffer_offset; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 75 | int fd; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 76 | uint8_t is_external; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 77 | } memif_region_t; |
| 78 | |
| 79 | typedef struct |
| 80 | { |
| 81 | memif_ring_t *ring; |
| 82 | uint8_t log2_ring_size; |
| 83 | uint8_t region; |
| 84 | uint32_t offset; |
| 85 | |
| 86 | uint16_t last_head; |
| 87 | uint16_t last_tail; |
| 88 | |
| 89 | int int_fd; |
| 90 | |
| 91 | uint64_t int_count; |
| 92 | uint32_t alloc_bufs; |
| 93 | } memif_queue_t; |
| 94 | |
| 95 | typedef struct memif_msg_queue_elt |
| 96 | { |
| 97 | memif_msg_t msg; |
| 98 | int fd; |
| 99 | struct memif_msg_queue_elt *next; |
| 100 | } memif_msg_queue_elt_t; |
| 101 | |
| 102 | struct memif_connection; |
| 103 | |
| 104 | typedef struct memif_connection memif_connection_t; |
| 105 | |
| 106 | /* functions called by memif_control_fd_handler */ |
| 107 | typedef int (memif_fn) (memif_connection_t * conn); |
| 108 | |
| 109 | typedef struct |
| 110 | { |
| 111 | uint8_t num_s2m_rings; |
| 112 | uint8_t num_m2s_rings; |
| 113 | uint16_t buffer_size; |
| 114 | memif_log2_ring_size_t log2_ring_size; |
| 115 | } memif_conn_run_args_t; |
| 116 | |
| 117 | typedef struct memif_connection |
| 118 | { |
| 119 | uint16_t index; |
| 120 | memif_conn_args_t args; |
| 121 | memif_conn_run_args_t run_args; |
| 122 | |
| 123 | int fd; |
| 124 | int listener_fd; |
| 125 | |
| 126 | memif_fn *write_fn, *read_fn, *error_fn; |
| 127 | |
| 128 | memif_connection_update_t *on_connect, *on_disconnect; |
| 129 | memif_interrupt_t *on_interrupt; |
| 130 | void *private_ctx; |
| 131 | |
| 132 | /* connection message queue */ |
| 133 | memif_msg_queue_elt_t *msg_queue; |
| 134 | |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 135 | uint8_t remote_if_name[MEMIF_NAME_LEN]; |
| 136 | uint8_t remote_name[MEMIF_NAME_LEN]; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 137 | uint8_t remote_disconnect_string[96]; |
| 138 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 139 | uint8_t regions_num; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 140 | memif_region_t *regions; |
| 141 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 142 | uint8_t rx_queues_num; |
| 143 | uint8_t tx_queues_num; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 144 | memif_queue_t *rx_queues; |
| 145 | memif_queue_t *tx_queues; |
| 146 | |
| 147 | uint16_t flags; |
| 148 | #define MEMIF_CONNECTION_FLAG_WRITE (1 << 0) |
| 149 | } memif_connection_t; |
| 150 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 151 | typedef struct |
| 152 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 153 | int key; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 154 | void *data_struct; |
| 155 | } memif_list_elt_t; |
| 156 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 157 | typedef struct |
| 158 | { |
| 159 | int fd; |
| 160 | uint16_t use_count; |
| 161 | uint8_t *filename; |
| 162 | uint16_t interface_list_len; |
| 163 | memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */ |
| 164 | } memif_socket_t; |
| 165 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 166 | typedef struct |
| 167 | { |
| 168 | memif_control_fd_update_t *control_fd_update; |
| 169 | int timerfd; |
| 170 | struct itimerspec arm, disarm; |
| 171 | uint16_t disconn_slaves; |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 172 | uint8_t app_name[MEMIF_NAME_LEN]; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 173 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 174 | memif_add_external_region_t *add_external_region; |
| 175 | memif_get_external_region_addr_t *get_external_region_addr; |
| 176 | memif_del_external_region_t *del_external_region; |
| 177 | memif_get_external_buffer_offset_t *get_external_buffer_offset; |
| 178 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 179 | memif_alloc_t *alloc; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 180 | memif_realloc_t *realloc; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 181 | memif_free_t *free; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 182 | |
| 183 | uint16_t control_list_len; |
| 184 | uint16_t interrupt_list_len; |
| 185 | uint16_t listener_list_len; |
| 186 | uint16_t pending_list_len; |
| 187 | memif_list_elt_t *control_list; |
| 188 | memif_list_elt_t *interrupt_list; |
| 189 | memif_list_elt_t *listener_list; |
| 190 | memif_list_elt_t *pending_list; |
| 191 | } libmemif_main_t; |
| 192 | |
| 193 | extern libmemif_main_t libmemif_main; |
| 194 | extern int memif_epfd; |
| 195 | |
| 196 | /* main.c */ |
| 197 | |
| 198 | /* if region doesn't contain shared memory, mmap region, check ring cookie */ |
| 199 | int memif_connect1 (memif_connection_t * c); |
| 200 | |
| 201 | /* memory map region, initalize rings and queues */ |
| 202 | int memif_init_regions_and_queues (memif_connection_t * c); |
| 203 | |
| 204 | int memif_disconnect_internal (memif_connection_t * c); |
| 205 | |
| 206 | /* map errno to memif error code */ |
| 207 | int memif_syscall_error_handler (int err_code); |
| 208 | |
| 209 | int add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list, |
| 210 | uint16_t * len); |
| 211 | |
| 212 | int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list, |
| 213 | uint16_t len, int key); |
| 214 | |
| 215 | int free_list_elt (memif_list_elt_t * list, uint16_t len, int key); |
| 216 | |
| 217 | #ifndef __NR_memfd_create |
| 218 | #if defined __x86_64__ |
| 219 | #define __NR_memfd_create 319 |
| 220 | #elif defined __arm__ |
| 221 | #define __NR_memfd_create 385 |
| 222 | #elif defined __aarch64__ |
| 223 | #define __NR_memfd_create 279 |
| 224 | #else |
| 225 | #error "__NR_memfd_create unknown for this architecture" |
| 226 | #endif |
| 227 | #endif |
| 228 | |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 229 | #ifndef HAVE_MEMFD_CREATE |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 230 | static inline int |
Damjan Marion | 23d4e8a | 2018-03-26 14:09:38 +0200 | [diff] [blame] | 231 | memfd_create (const char *name, unsigned int flags) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 232 | { |
| 233 | return syscall (__NR_memfd_create, name, flags); |
| 234 | } |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 235 | #endif |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 236 | |
| 237 | static inline void * |
| 238 | memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring, |
| 239 | uint16_t index) |
| 240 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 241 | return (conn->regions[ring->desc[index].region].addr + |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 242 | ring->desc[index].offset); |
| 243 | } |
| 244 | |
| 245 | #ifndef F_LINUX_SPECIFIC_BASE |
| 246 | #define F_LINUX_SPECIFIC_BASE 1024 |
| 247 | #endif |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 248 | |
| 249 | #ifndef MFD_ALLOW_SEALING |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 250 | #define MFD_ALLOW_SEALING 0x0002U |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 251 | #endif |
| 252 | |
| 253 | #ifndef F_ADD_SEALS |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 254 | #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) |
| 255 | #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10) |
| 256 | |
| 257 | #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */ |
| 258 | #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */ |
| 259 | #define F_SEAL_GROW 0x0004 /* prevent file from growing */ |
| 260 | #define F_SEAL_WRITE 0x0008 /* prevent writes */ |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 261 | #endif |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 262 | |
| 263 | #endif /* _MEMIF_PRIVATE_H_ */ |