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