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 | |
Damjan Marion | 2cdc2bd | 2018-04-26 18:39:56 +0200 | [diff] [blame] | 18 | #define _GNU_SOURCE |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <net/if.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <sys/un.h> |
| 26 | #include <sys/uio.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <sys/prctl.h> |
| 29 | #include <inttypes.h> |
| 30 | #include <string.h> |
| 31 | #include <stdio.h> |
| 32 | #include <netdb.h> |
| 33 | #include <linux/ip.h> |
| 34 | #include <linux/icmp.h> |
| 35 | #include <arpa/inet.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <netinet/if_ether.h> |
| 38 | #include <net/if_arp.h> |
| 39 | #include <asm/byteorder.h> |
| 40 | #include <byteswap.h> |
| 41 | #include <string.h> |
| 42 | #include <errno.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <sys/eventfd.h> |
| 45 | #include <sys/timerfd.h> |
| 46 | #include <sys/epoll.h> |
| 47 | #include <signal.h> |
Jakub Grajciar | 435f500 | 2018-04-20 09:20:50 +0200 | [diff] [blame] | 48 | #include <linux/memfd.h> |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 49 | |
| 50 | /* memif protocol msg, ring and descriptor definitions */ |
| 51 | #include <memif.h> |
| 52 | /* memif api */ |
| 53 | #include <libmemif.h> |
| 54 | /* socket messaging functions */ |
| 55 | #include <socket.h> |
| 56 | /* private structs and functions */ |
| 57 | #include <memif_private.h> |
| 58 | |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 59 | #define ERRLIST_LEN 40 |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 60 | #define MAX_ERRBUF_LEN 256 |
| 61 | |
| 62 | #if __x86_x64__ |
| 63 | #define MEMIF_MEMORY_BARRIER() __builtin_ia32_sfence () |
| 64 | #else |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 65 | #define MEMIF_MEMORY_BARRIER() __sync_synchronize () |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 66 | #endif /* __x86_x64__ */ |
| 67 | |
| 68 | libmemif_main_t libmemif_main; |
| 69 | int memif_epfd; |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 70 | int poll_cancel_fd = -1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 71 | |
| 72 | static char memif_buf[MAX_ERRBUF_LEN]; |
| 73 | |
| 74 | const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ |
| 75 | "Success.", |
| 76 | /* MEMIF_ERR_SYSCALL */ |
| 77 | "Unspecified syscall error (build with -DMEMIF_DBG or make debug).", |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 78 | /* MEMIF_ERR_CONNREFUSED */ |
| 79 | "Connection refused", |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 80 | /* MEMIF_ERR_ACCES */ |
| 81 | "Permission to resoure denied.", |
| 82 | /* MEMIF_ERR_NO_FILE */ |
| 83 | "Socket file does not exist", |
| 84 | /* MEMIF_ERR_FILE_LIMIT */ |
| 85 | "System limit on total numer of open files reached.", |
| 86 | /* MEMIF_ERR_PROC_FILE_LIMIT */ |
| 87 | "Per-process limit on total number of open files reached.", |
| 88 | /* MEMIF_ERR_ALREADY */ |
| 89 | "Connection already requested.", |
| 90 | /* MEMIF_ERR_AGAIN */ |
| 91 | "File descriptor refers to file other than socket, or operation would block.", |
| 92 | /* MEMIF_ERR_BAD_FD */ |
| 93 | "Bad file descriptor.", |
| 94 | /* MEMIF_ERR_NOMEM */ |
| 95 | "Out of memory.", |
| 96 | /* MEMIF_ERR_INVAL_ARG */ |
| 97 | "Invalid argument.", |
| 98 | /* MEMIF_ERR_NOCONN */ |
| 99 | "Memif connection handle does not point to existing conenction", |
| 100 | /* MEMIF_ERR_CONN */ |
| 101 | "Memif connection handle points to existing connection", |
| 102 | /* MEMIF_ERR_CB_FDUPDATE */ |
| 103 | "Callback memif_control_fd_update_t returned error", |
| 104 | /* MEMIF_ERR_FILE_NOT_SOCK */ |
| 105 | "File specified by socket filename exists and is not socket.", |
| 106 | /* MEMIF_ERR_NO_SHMFD */ |
| 107 | "Missing shared memory file descriptor. (internal error)", |
| 108 | /* MEMIF_ERR_COOKIE */ |
| 109 | "Invalid cookie on ring. (internal error)", |
| 110 | /* MEMIF_ERR_NOBUF_RING */ |
| 111 | "Ring buffer full.", |
| 112 | /* MEMIF_ERR_NOBUF */ |
| 113 | "Not enough memif buffers. There are unreceived data in shared memory.", |
| 114 | /* MEMIF_ERR_NOBUF_DET */ |
| 115 | "Not enough space for memif details in suplied buffer. String data might be malformed.", |
| 116 | /* MEMIF_ERR_INT_WRITE */ |
| 117 | "Send interrupt error.", |
| 118 | /* MEMIF_ERR_MFMSG */ |
| 119 | "Malformed message received on control channel.", |
| 120 | /* MEMIF_ERR_QID */ |
| 121 | "Invalid queue id", |
| 122 | /* MEMIF_ERR_PROTO */ |
| 123 | "Incompatible memory interface protocol version.", |
| 124 | /* MEMIF_ERR_ID */ |
| 125 | "Unmatched interface id.", |
| 126 | /* MEMIF_ERR_ACCSLAVE */ |
| 127 | "Slave cannot accept connection reqest.", |
| 128 | /* MEMIF_ERR_ALRCONN */ |
| 129 | "Interface is already connected.", |
| 130 | /* MEMIF_ERR_MODE */ |
| 131 | "Mode mismatch.", |
| 132 | /* MEMIF_ERR_SECRET */ |
| 133 | "Secret mismatch.", |
| 134 | /* MEMIF_ERR_NOSECRET */ |
| 135 | "Secret required.", |
| 136 | /* MEMIF_ERR_MAXREG */ |
| 137 | "Limit on total number of regions reached.", |
| 138 | /* MEMIF_ERR_MAXRING */ |
| 139 | "Limit on total number of ring reached.", |
| 140 | /* MEMIF_ERR_NO_INTFD */ |
| 141 | "Missing interrupt file descriptor. (internal error)", |
| 142 | /* MEMIF_ERR_DISCONNECT */ |
| 143 | "Interface received disconnect request.", |
| 144 | /* MEMIF_ERR_DISCONNECTED */ |
| 145 | "Interface is disconnected.", |
| 146 | /* MEMIF_ERR_UNKNOWN_MSG */ |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 147 | "Unknown message type received on control channel. (internal error)", |
| 148 | /* MEMIF_ERR_POLL_CANCEL */ |
Damjan Marion | 6d56fa4 | 2017-11-03 12:24:37 +0100 | [diff] [blame] | 149 | "Memif event polling was canceled.", |
| 150 | /* MEMIF_ERR_MAX_RING */ |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 151 | "Maximum log2 ring size is 15", |
Jakub Grajciar | ab7c2b0 | 2018-03-28 10:21:05 +0200 | [diff] [blame] | 152 | /* MEMIF_ERR_PRIVHDR */ |
| 153 | "Private headers not supported." |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | #define MEMIF_ERR_UNDEFINED "undefined error" |
| 157 | |
| 158 | char * |
| 159 | memif_strerror (int err_code) |
| 160 | { |
| 161 | if (err_code >= ERRLIST_LEN) |
| 162 | { |
| 163 | strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED)); |
| 164 | memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0'; |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | strncpy (memif_buf, memif_errlist[err_code], |
| 169 | strlen (memif_errlist[err_code])); |
| 170 | memif_buf[strlen (memif_errlist[err_code])] = '\0'; |
| 171 | } |
| 172 | return memif_buf; |
| 173 | } |
| 174 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 175 | uint16_t |
| 176 | memif_get_version () |
| 177 | { |
| 178 | return MEMIF_VERSION; |
| 179 | } |
| 180 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 181 | #define DBG_TX_BUF (0) |
| 182 | #define DBG_RX_BUF (1) |
| 183 | |
| 184 | #ifdef MEMIF_DBG_SHM |
| 185 | static void |
| 186 | print_bytes (void *data, uint16_t len, uint8_t q) |
| 187 | { |
| 188 | if (q == DBG_TX_BUF) |
| 189 | printf ("\nTX:\n\t"); |
| 190 | else |
| 191 | printf ("\nRX:\n\t"); |
| 192 | int i; |
| 193 | for (i = 0; i < len; i++) |
| 194 | { |
| 195 | if (i % 8 == 0) |
| 196 | printf ("\n%d:\t", i); |
| 197 | printf ("%02X ", ((uint8_t *) (data))[i]); |
| 198 | } |
| 199 | printf ("\n\n"); |
| 200 | } |
Jakub Grajciar | ba3c4e8 | 2017-09-18 11:21:40 +0200 | [diff] [blame] | 201 | #endif /* MEMIF_DBG_SHM */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 202 | |
| 203 | int |
| 204 | memif_syscall_error_handler (int err_code) |
| 205 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 206 | DBG ("%s", strerror (err_code)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 207 | |
| 208 | if (err_code == 0) |
| 209 | return MEMIF_ERR_SUCCESS; |
| 210 | if (err_code == EACCES) |
| 211 | return MEMIF_ERR_ACCES; |
| 212 | if (err_code == ENFILE) |
| 213 | return MEMIF_ERR_FILE_LIMIT; |
| 214 | if (err_code == EMFILE) |
| 215 | return MEMIF_ERR_PROC_FILE_LIMIT; |
| 216 | if (err_code == ENOMEM) |
| 217 | return MEMIF_ERR_NOMEM; |
Damjan Marion | 6d56fa4 | 2017-11-03 12:24:37 +0100 | [diff] [blame] | 218 | /* connection refused if master does not exist |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 219 | this error would spam the user until master was created */ |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 220 | /* |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 221 | if (err_code == ECONNREFUSED) |
| 222 | return MEMIF_ERR_SUCCESS; |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 223 | */ |
| 224 | if (err_code == ECONNREFUSED) |
| 225 | return MEMIF_ERR_CONNREFUSED; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 226 | if (err_code == EALREADY) |
| 227 | return MEMIF_ERR_ALREADY; |
| 228 | if (err_code == EAGAIN) |
| 229 | return MEMIF_ERR_AGAIN; |
| 230 | if (err_code == EBADF) |
| 231 | return MEMIF_ERR_BAD_FD; |
| 232 | if (err_code == ENOENT) |
| 233 | return MEMIF_ERR_NO_FILE; |
| 234 | |
| 235 | /* other syscall errors */ |
| 236 | return MEMIF_ERR_SYSCALL; |
| 237 | } |
| 238 | |
| 239 | static int |
| 240 | memif_add_epoll_fd (int fd, uint32_t events) |
| 241 | { |
| 242 | if (fd < 0) |
| 243 | { |
| 244 | DBG ("invalid fd %d", fd); |
| 245 | return -1; |
| 246 | } |
| 247 | struct epoll_event evt; |
| 248 | memset (&evt, 0, sizeof (evt)); |
| 249 | evt.events = events; |
| 250 | evt.data.fd = fd; |
| 251 | if (epoll_ctl (memif_epfd, EPOLL_CTL_ADD, fd, &evt) < 0) |
| 252 | { |
| 253 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 254 | return -1; |
| 255 | } |
| 256 | DBG ("fd %d added to epoll", fd); |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int |
| 261 | memif_mod_epoll_fd (int fd, uint32_t events) |
| 262 | { |
| 263 | if (fd < 0) |
| 264 | { |
| 265 | DBG ("invalid fd %d", fd); |
| 266 | return -1; |
| 267 | } |
| 268 | struct epoll_event evt; |
| 269 | memset (&evt, 0, sizeof (evt)); |
| 270 | evt.events = events; |
| 271 | evt.data.fd = fd; |
| 272 | if (epoll_ctl (memif_epfd, EPOLL_CTL_MOD, fd, &evt) < 0) |
| 273 | { |
| 274 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 275 | return -1; |
| 276 | } |
| 277 | DBG ("fd %d moddified on epoll", fd); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int |
| 282 | memif_del_epoll_fd (int fd) |
| 283 | { |
| 284 | if (fd < 0) |
| 285 | { |
| 286 | DBG ("invalid fd %d", fd); |
| 287 | return -1; |
| 288 | } |
| 289 | struct epoll_event evt; |
| 290 | memset (&evt, 0, sizeof (evt)); |
| 291 | if (epoll_ctl (memif_epfd, EPOLL_CTL_DEL, fd, &evt) < 0) |
| 292 | { |
| 293 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 294 | return -1; |
| 295 | } |
| 296 | DBG ("fd %d removed from epoll", fd); |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | int |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 301 | memif_control_fd_update (int fd, uint8_t events, void *private_ctx) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 302 | { |
| 303 | if (events & MEMIF_FD_EVENT_DEL) |
| 304 | return memif_del_epoll_fd (fd); |
| 305 | |
| 306 | uint32_t evt = 0; |
| 307 | if (events & MEMIF_FD_EVENT_READ) |
| 308 | evt |= EPOLLIN; |
| 309 | if (events & MEMIF_FD_EVENT_WRITE) |
| 310 | evt |= EPOLLOUT; |
| 311 | |
| 312 | if (events & MEMIF_FD_EVENT_MOD) |
| 313 | return memif_mod_epoll_fd (fd, evt); |
| 314 | |
| 315 | return memif_add_epoll_fd (fd, evt); |
| 316 | } |
| 317 | |
| 318 | int |
| 319 | add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list, uint16_t * len) |
| 320 | { |
| 321 | libmemif_main_t *lm = &libmemif_main; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 322 | memif_list_elt_t *tmp; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 323 | int i; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 324 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 325 | for (i = 0; i < *len; i++) |
| 326 | { |
| 327 | if ((*list)[i].data_struct == NULL) |
| 328 | { |
| 329 | (*list)[i].key = e->key; |
| 330 | (*list)[i].data_struct = e->data_struct; |
| 331 | return i; |
| 332 | } |
| 333 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 334 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 335 | tmp = lm->realloc (*list, sizeof (memif_list_elt_t) * *len * 2); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 336 | if (tmp == NULL) |
| 337 | return -1; |
| 338 | |
| 339 | for (i = *len; i < *len * 2; i++) |
| 340 | { |
| 341 | tmp[i].key = -1; |
| 342 | tmp[i].data_struct = NULL; |
| 343 | } |
| 344 | |
| 345 | tmp[*len].key = e->key; |
| 346 | tmp[*len].data_struct = e->data_struct; |
| 347 | i = *len; |
| 348 | *len = *len * 2; |
| 349 | *list = tmp; |
| 350 | |
| 351 | return i; |
| 352 | } |
| 353 | |
| 354 | int |
| 355 | get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list, uint16_t len, |
| 356 | int key) |
| 357 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 358 | int i; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 359 | if (key == -1) |
| 360 | { |
| 361 | *e = NULL; |
| 362 | return -1; |
| 363 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 364 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 365 | for (i = 0; i < len; i++) |
| 366 | { |
| 367 | if (list[i].key == key) |
| 368 | { |
| 369 | *e = &list[i]; |
| 370 | return 0; |
| 371 | } |
| 372 | } |
| 373 | *e = NULL; |
| 374 | return -1; |
| 375 | } |
| 376 | |
| 377 | /* does not free memory, only marks element as free */ |
| 378 | int |
| 379 | free_list_elt (memif_list_elt_t * list, uint16_t len, int key) |
| 380 | { |
| 381 | int i; |
| 382 | for (i = 0; i < len; i++) |
| 383 | { |
| 384 | if (list[i].key == key) |
| 385 | { |
| 386 | list[i].key = -1; |
| 387 | list[i].data_struct = NULL; |
| 388 | return 0; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | int |
| 396 | free_list_elt_ctx (memif_list_elt_t * list, uint16_t len, |
| 397 | memif_connection_t * ctx) |
| 398 | { |
| 399 | int i; |
| 400 | for (i = 0; i < len; i++) |
| 401 | { |
| 402 | if (list[i].key == -1) |
| 403 | { |
| 404 | if (list[i].data_struct == ctx) |
| 405 | { |
| 406 | list[i].data_struct = NULL; |
| 407 | return 0; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return -1; |
| 413 | } |
| 414 | |
| 415 | static void |
| 416 | memif_control_fd_update_register (memif_control_fd_update_t * cb) |
| 417 | { |
| 418 | libmemif_main_t *lm = &libmemif_main; |
| 419 | lm->control_fd_update = cb; |
| 420 | } |
| 421 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 422 | void |
| 423 | memif_register_external_region (memif_add_external_region_t * ar, |
| 424 | memif_get_external_region_addr_t * gr, |
| 425 | memif_del_external_region_t * dr, |
| 426 | memif_get_external_buffer_offset_t * go) |
| 427 | { |
| 428 | libmemif_main_t *lm = &libmemif_main; |
| 429 | lm->add_external_region = ar; |
| 430 | lm->get_external_region_addr = gr; |
| 431 | lm->del_external_region = dr; |
| 432 | lm->get_external_buffer_offset = go; |
| 433 | } |
| 434 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 435 | static void |
| 436 | memif_alloc_register (memif_alloc_t * ma) |
| 437 | { |
| 438 | libmemif_main_t *lm = &libmemif_main; |
| 439 | lm->alloc = ma; |
| 440 | } |
| 441 | |
| 442 | static void |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 443 | memif_realloc_register (memif_realloc_t * mr) |
| 444 | { |
| 445 | libmemif_main_t *lm = &libmemif_main; |
| 446 | lm->realloc = mr; |
| 447 | } |
| 448 | |
| 449 | static void |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 450 | memif_free_register (memif_free_t * mf) |
| 451 | { |
| 452 | libmemif_main_t *lm = &libmemif_main; |
| 453 | lm->free = mf; |
| 454 | } |
| 455 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 456 | int |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 457 | memif_set_connection_request_timer (struct itimerspec timer) |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 458 | { |
| 459 | libmemif_main_t *lm = &libmemif_main; |
| 460 | int err = MEMIF_ERR_SUCCESS; |
| 461 | |
| 462 | lm->arm = timer; |
| 463 | |
| 464 | /* overwrite timer, if already armed */ |
| 465 | if (lm->disconn_slaves != 0) |
| 466 | { |
| 467 | if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) |
| 468 | { |
| 469 | err = memif_syscall_error_handler (errno); |
| 470 | } |
| 471 | } |
| 472 | return err; |
| 473 | } |
| 474 | |
| 475 | int |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 476 | memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name, |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 477 | memif_alloc_t * memif_alloc, memif_realloc_t * memif_realloc, |
| 478 | memif_free_t * memif_free) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 479 | { |
| 480 | int err = MEMIF_ERR_SUCCESS; /* 0 */ |
| 481 | libmemif_main_t *lm = &libmemif_main; |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 482 | memset (lm, 0, sizeof (libmemif_main_t)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 483 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 484 | if (memif_alloc != NULL) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 485 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 486 | memif_alloc_register (memif_alloc); |
| 487 | } |
| 488 | else |
| 489 | memif_alloc_register (malloc); |
| 490 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 491 | if (memif_realloc != NULL) |
| 492 | { |
| 493 | memif_realloc_register (memif_realloc); |
| 494 | } |
| 495 | else |
| 496 | memif_realloc_register (realloc); |
| 497 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 498 | if (memif_free != NULL) |
| 499 | memif_free_register (memif_free); |
| 500 | else |
| 501 | memif_free_register (free); |
| 502 | |
| 503 | if (app_name != NULL) |
| 504 | { |
| 505 | uint8_t len = (strlen (app_name) > MEMIF_NAME_LEN) |
| 506 | ? strlen (app_name) : MEMIF_NAME_LEN; |
| 507 | strncpy ((char *) lm->app_name, app_name, len); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 508 | } |
| 509 | else |
| 510 | { |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 511 | strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME, |
| 512 | strlen (MEMIF_DEFAULT_APP_NAME)); |
| 513 | } |
| 514 | |
| 515 | /* register control fd update callback */ |
| 516 | if (on_control_fd_update != NULL) |
| 517 | memif_control_fd_update_register (on_control_fd_update); |
| 518 | else |
| 519 | { |
| 520 | memif_epfd = epoll_create (1); |
| 521 | memif_control_fd_update_register (memif_control_fd_update); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 522 | if ((poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0) |
| 523 | { |
| 524 | err = errno; |
| 525 | DBG ("eventfd: %s", strerror (err)); |
| 526 | return memif_syscall_error_handler (err); |
| 527 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 528 | lm->control_fd_update (poll_cancel_fd, MEMIF_FD_EVENT_READ, NULL); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 529 | DBG ("libmemif event polling initialized"); |
| 530 | } |
| 531 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 532 | lm->control_list_len = 2; |
| 533 | lm->interrupt_list_len = 2; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 534 | lm->socket_list_len = 1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 535 | lm->pending_list_len = 1; |
| 536 | |
| 537 | lm->control_list = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 538 | lm->alloc (sizeof (memif_list_elt_t) * lm->control_list_len); |
| 539 | if (lm->control_list == NULL) |
| 540 | { |
| 541 | err = MEMIF_ERR_NOMEM; |
| 542 | goto error; |
| 543 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 544 | lm->interrupt_list = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 545 | lm->alloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len); |
| 546 | if (lm->interrupt_list == NULL) |
| 547 | { |
| 548 | err = MEMIF_ERR_NOMEM; |
| 549 | goto error; |
| 550 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 551 | lm->socket_list = |
| 552 | lm->alloc (sizeof (memif_list_elt_t) * lm->socket_list_len); |
| 553 | if (lm->socket_list == NULL) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 554 | { |
| 555 | err = MEMIF_ERR_NOMEM; |
| 556 | goto error; |
| 557 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 558 | lm->pending_list = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 559 | lm->alloc (sizeof (memif_list_elt_t) * lm->pending_list_len); |
| 560 | if (lm->pending_list == NULL) |
| 561 | { |
| 562 | err = MEMIF_ERR_NOMEM; |
| 563 | goto error; |
| 564 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 565 | |
| 566 | int i; |
| 567 | for (i = 0; i < lm->control_list_len; i++) |
| 568 | { |
| 569 | lm->control_list[i].key = -1; |
| 570 | lm->control_list[i].data_struct = NULL; |
| 571 | } |
| 572 | for (i = 0; i < lm->interrupt_list_len; i++) |
| 573 | { |
| 574 | lm->interrupt_list[i].key = -1; |
| 575 | lm->interrupt_list[i].data_struct = NULL; |
| 576 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 577 | for (i = 0; i < lm->socket_list_len; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 578 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 579 | lm->socket_list[i].key = -1; |
| 580 | lm->socket_list[i].data_struct = NULL; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 581 | } |
| 582 | for (i = 0; i < lm->pending_list_len; i++) |
| 583 | { |
| 584 | lm->pending_list[i].key = -1; |
| 585 | lm->pending_list[i].data_struct = NULL; |
| 586 | } |
| 587 | |
| 588 | lm->disconn_slaves = 0; |
| 589 | |
| 590 | lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK); |
| 591 | if (lm->timerfd < 0) |
| 592 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 593 | err = memif_syscall_error_handler (errno); |
| 594 | goto error; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 597 | lm->arm.it_value.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC; |
| 598 | lm->arm.it_value.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC; |
| 599 | lm->arm.it_interval.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC; |
| 600 | lm->arm.it_interval.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 601 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 602 | if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ, NULL) < 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 603 | { |
| 604 | DBG ("callback type memif_control_fd_update_t error!"); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 605 | err = MEMIF_ERR_CB_FDUPDATE; |
| 606 | goto error; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 607 | } |
| 608 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 609 | return err; |
| 610 | |
| 611 | error: |
| 612 | memif_cleanup (); |
| 613 | return err; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | static inline memif_ring_t * |
| 617 | memif_get_ring (memif_connection_t * conn, memif_ring_type_t type, |
| 618 | uint16_t ring_num) |
| 619 | { |
| 620 | if (&conn->regions[0] == NULL) |
| 621 | return NULL; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 622 | void *p = conn->regions[0].addr; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 623 | int ring_size = |
| 624 | sizeof (memif_ring_t) + |
| 625 | sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size); |
| 626 | p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size; |
| 627 | |
| 628 | return (memif_ring_t *) p; |
| 629 | } |
| 630 | |
| 631 | int |
| 632 | memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode, |
| 633 | uint16_t qid) |
| 634 | { |
| 635 | memif_connection_t *conn = (memif_connection_t *) c; |
| 636 | if (conn == NULL) |
| 637 | return MEMIF_ERR_NOCONN; |
| 638 | uint8_t num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 639 | (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn-> |
| 640 | run_args.num_m2s_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 641 | if (qid >= num) |
| 642 | return MEMIF_ERR_QID; |
| 643 | |
| 644 | conn->rx_queues[qid].ring->flags = rx_mode; |
| 645 | DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags); |
| 646 | return MEMIF_ERR_SUCCESS; |
| 647 | } |
| 648 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 649 | static int |
| 650 | memif_socket_start_listening (memif_socket_t * ms) |
| 651 | { |
| 652 | libmemif_main_t *lm = &libmemif_main; |
| 653 | memif_list_elt_t elt; |
| 654 | struct stat file_stat; |
| 655 | struct sockaddr_un un = { 0 }; |
| 656 | int on = 1; |
| 657 | int err = MEMIF_ERR_SUCCESS; |
| 658 | |
| 659 | if (ms->type == MEMIF_SOCKET_TYPE_CLIENT) |
| 660 | return MEMIF_ERR_INVAL_ARG; |
| 661 | |
| 662 | /* check if file exists */ |
| 663 | if (stat ((char *) ms->filename, &file_stat) == 0) |
| 664 | { |
| 665 | if (S_ISSOCK (file_stat.st_mode)) |
| 666 | unlink ((char *) ms->filename); |
| 667 | else |
| 668 | return memif_syscall_error_handler (errno); |
| 669 | } |
| 670 | |
| 671 | ms->fd = socket (AF_UNIX, SOCK_SEQPACKET, 0); |
| 672 | if (ms->fd < 0) |
| 673 | { |
| 674 | err = memif_syscall_error_handler (errno); |
| 675 | goto error; |
| 676 | } |
| 677 | |
| 678 | DBG ("socket %d created", ms->fd); |
| 679 | un.sun_family = AF_UNIX; |
| 680 | strncpy ((char *) un.sun_path, (char *) ms->filename, |
| 681 | sizeof (un.sun_path) - 1); |
| 682 | if (setsockopt (ms->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0) |
| 683 | { |
| 684 | err = memif_syscall_error_handler (errno); |
| 685 | goto error; |
| 686 | } |
| 687 | if (bind (ms->fd, (struct sockaddr *) &un, sizeof (un)) < 0) |
| 688 | { |
| 689 | err = memif_syscall_error_handler (errno); |
| 690 | goto error; |
| 691 | } |
| 692 | if (listen (ms->fd, 1) < 0) |
| 693 | { |
| 694 | err = memif_syscall_error_handler (errno); |
| 695 | goto error; |
| 696 | } |
| 697 | if (stat ((char *) ms->filename, &file_stat) < 0) |
| 698 | { |
| 699 | err = memif_syscall_error_handler (errno); |
| 700 | goto error; |
| 701 | } |
| 702 | |
| 703 | /* add socket to libmemif main */ |
| 704 | elt.key = ms->fd; |
| 705 | elt.data_struct = ms; |
| 706 | add_list_elt (&elt, &lm->socket_list, &lm->socket_list_len); |
| 707 | lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ, ms->private_ctx); |
| 708 | |
| 709 | ms->type = MEMIF_SOCKET_TYPE_LISTENER; |
| 710 | |
| 711 | return err; |
| 712 | |
| 713 | error: |
| 714 | if (ms->fd > 0) |
| 715 | { |
| 716 | close (ms->fd); |
| 717 | ms->fd = -1; |
| 718 | } |
| 719 | return err; |
| 720 | } |
| 721 | |
| 722 | int |
| 723 | memif_create_socket (memif_socket_handle_t * sock, const char *filename, |
| 724 | void *private_ctx) |
| 725 | { |
| 726 | libmemif_main_t *lm = &libmemif_main; |
| 727 | memif_socket_t *ms = (memif_socket_t *) * sock; |
| 728 | int i, err = MEMIF_ERR_SUCCESS; |
| 729 | |
| 730 | for (i = 0; i < lm->socket_list_len; i++) |
| 731 | { |
| 732 | if ((ms = (memif_socket_t *) lm->socket_list[i].data_struct) != NULL) |
| 733 | { |
| 734 | if (strncmp ((char *) ms->filename, filename, |
| 735 | strlen ((char *) ms->filename)) == 0) |
| 736 | return MEMIF_ERR_INVAL_ARG; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /* allocate memif_socket_t */ |
| 741 | ms = NULL; |
| 742 | ms = lm->alloc (sizeof (memif_socket_t)); |
| 743 | if (ms == NULL) |
| 744 | { |
| 745 | err = MEMIF_ERR_NOMEM; |
| 746 | goto error; |
| 747 | } |
| 748 | memset (ms, 0, sizeof (memif_socket_t)); |
| 749 | /* set filename */ |
| 750 | ms->filename = lm->alloc (strlen (filename) + sizeof (char)); |
| 751 | if (ms->filename == NULL) |
| 752 | { |
| 753 | err = MEMIF_ERR_NOMEM; |
| 754 | goto error; |
| 755 | } |
| 756 | memset (ms->filename, 0, strlen (filename) + sizeof (char)); |
| 757 | strncpy ((char *) ms->filename, filename, strlen (filename)); |
| 758 | |
| 759 | ms->type = MEMIF_SOCKET_TYPE_NONE; |
| 760 | |
| 761 | ms->interface_list_len = 1; |
| 762 | ms->interface_list = |
| 763 | lm->alloc (sizeof (memif_list_elt_t) * ms->interface_list_len); |
| 764 | if (ms->interface_list == NULL) |
| 765 | { |
| 766 | err = MEMIF_ERR_NOMEM; |
| 767 | goto error; |
| 768 | } |
| 769 | ms->interface_list[0].key = -1; |
| 770 | ms->interface_list[0].data_struct = NULL; |
| 771 | |
| 772 | *sock = ms; |
| 773 | |
| 774 | return err; |
| 775 | |
| 776 | error: |
| 777 | if (ms != NULL) |
| 778 | { |
| 779 | if (ms->filename != NULL) |
| 780 | { |
| 781 | lm->free (ms->filename); |
| 782 | ms->filename = NULL; |
| 783 | } |
| 784 | if (ms->fd > 0) |
| 785 | { |
| 786 | close (ms->fd); |
| 787 | ms->fd = -1; |
| 788 | } |
| 789 | if (ms->interface_list != NULL) |
| 790 | { |
| 791 | lm->free (ms->interface_list); |
| 792 | ms->interface_list = NULL; |
| 793 | ms->interface_list_len = 0; |
| 794 | } |
| 795 | lm->free (ms); |
| 796 | *sock = ms = NULL; |
| 797 | } |
| 798 | return err; |
| 799 | } |
| 800 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 801 | int |
| 802 | memif_create (memif_conn_handle_t * c, memif_conn_args_t * args, |
| 803 | memif_connection_update_t * on_connect, |
| 804 | memif_connection_update_t * on_disconnect, |
| 805 | memif_interrupt_t * on_interrupt, void *private_ctx) |
| 806 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 807 | libmemif_main_t *lm = &libmemif_main; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 808 | int err, index = 0; |
| 809 | memif_list_elt_t elt; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 810 | memif_connection_t *conn = (memif_connection_t *) * c; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 811 | memif_socket_t *ms; |
| 812 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 813 | if (conn != NULL) |
| 814 | { |
| 815 | DBG ("This handle already points to existing memif."); |
| 816 | return MEMIF_ERR_CONN; |
| 817 | } |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 818 | conn = (memif_connection_t *) lm->alloc (sizeof (memif_connection_t)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 819 | if (conn == NULL) |
| 820 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 821 | err = MEMIF_ERR_NOMEM; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 822 | goto error; |
| 823 | } |
| 824 | memset (conn, 0, sizeof (memif_connection_t)); |
| 825 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 826 | conn->args.interface_id = args->interface_id; |
| 827 | |
| 828 | if (args->log2_ring_size == 0) |
| 829 | args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE; |
Damjan Marion | 6d56fa4 | 2017-11-03 12:24:37 +0100 | [diff] [blame] | 830 | else if (args->log2_ring_size > MEMIF_MAX_LOG2_RING_SIZE) |
| 831 | { |
| 832 | err = MEMIF_ERR_MAX_RING; |
| 833 | goto error; |
| 834 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 835 | if (args->buffer_size == 0) |
| 836 | args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE; |
| 837 | if (args->num_s2m_rings == 0) |
| 838 | args->num_s2m_rings = MEMIF_DEFAULT_TX_QUEUES; |
| 839 | if (args->num_m2s_rings == 0) |
| 840 | args->num_m2s_rings = MEMIF_DEFAULT_RX_QUEUES; |
| 841 | |
| 842 | conn->args.num_s2m_rings = args->num_s2m_rings; |
| 843 | conn->args.num_m2s_rings = args->num_m2s_rings; |
| 844 | conn->args.buffer_size = args->buffer_size; |
| 845 | conn->args.log2_ring_size = args->log2_ring_size; |
| 846 | conn->args.is_master = args->is_master; |
| 847 | conn->args.mode = args->mode; |
| 848 | conn->msg_queue = NULL; |
| 849 | conn->regions = NULL; |
| 850 | conn->tx_queues = NULL; |
| 851 | conn->rx_queues = NULL; |
| 852 | conn->fd = -1; |
| 853 | conn->on_connect = on_connect; |
| 854 | conn->on_disconnect = on_disconnect; |
| 855 | conn->on_interrupt = on_interrupt; |
| 856 | conn->private_ctx = private_ctx; |
| 857 | memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t)); |
| 858 | |
| 859 | uint8_t l = strlen ((char *) args->interface_name); |
| 860 | strncpy ((char *) conn->args.interface_name, (char *) args->interface_name, |
| 861 | l); |
| 862 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 863 | if ((l = strlen ((char *) args->secret)) > 0) |
| 864 | strncpy ((char *) conn->args.secret, (char *) args->secret, l); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 865 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 866 | if (args->socket != NULL) |
| 867 | conn->args.socket = args->socket; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 868 | else |
| 869 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 870 | if (lm->default_socket == NULL) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 871 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 872 | err = |
| 873 | memif_create_socket ((memif_socket_handle_t *) & |
| 874 | lm->default_socket, |
| 875 | MEMIF_DEFAULT_SOCKET_PATH, NULL); |
| 876 | if (err != MEMIF_ERR_SUCCESS) |
| 877 | goto error; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 878 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 879 | conn->args.socket = lm->default_socket; |
| 880 | } |
| 881 | ms = (memif_socket_t *) conn->args.socket; |
| 882 | |
| 883 | if ((conn->args.is_master && ms->type == MEMIF_SOCKET_TYPE_CLIENT) || |
| 884 | (!conn->args.is_master && ms->type == MEMIF_SOCKET_TYPE_LISTENER)) |
| 885 | { |
| 886 | err = MEMIF_ERR_INVAL_ARG; |
| 887 | goto error; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 888 | } |
| 889 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 890 | elt.key = conn->args.interface_id; |
| 891 | elt.data_struct = conn; |
| 892 | add_list_elt (&elt, &ms->interface_list, &ms->interface_list_len); |
| 893 | ms->use_count++; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 894 | |
| 895 | if (conn->args.is_master) |
| 896 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 897 | if (ms->type == MEMIF_SOCKET_TYPE_NONE) |
| 898 | err = memif_socket_start_listening (ms); |
| 899 | if (err != MEMIF_ERR_SUCCESS) |
| 900 | goto error; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 901 | } |
| 902 | else |
| 903 | { |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 904 | lm->disconn_slaves++; |
| 905 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 906 | elt.key = -1; |
| 907 | elt.data_struct = conn; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 908 | if ((index = |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 909 | add_list_elt (&elt, &lm->control_list, &lm->control_list_len)) < 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 910 | { |
| 911 | err = MEMIF_ERR_NOMEM; |
| 912 | goto error; |
| 913 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 914 | |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 915 | conn->index = index; |
| 916 | |
| 917 | /* try connectiong to master */ |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 918 | err = memif_request_connection (conn); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 919 | if ((err < 0) && (lm->disconn_slaves == 1)) |
| 920 | { |
| 921 | /* connection failed, arm reconnect timer (if not armed) */ |
| 922 | if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) |
| 923 | { |
| 924 | err = memif_syscall_error_handler (errno); |
| 925 | goto error; |
| 926 | } |
| 927 | } |
| 928 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 929 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 930 | *c = conn; |
| 931 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 932 | return 0; |
| 933 | |
| 934 | error: |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 935 | if (conn != NULL) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 936 | lm->free (conn); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 937 | *c = conn = NULL; |
| 938 | return err; |
| 939 | } |
| 940 | |
| 941 | int |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 942 | memif_request_connection (memif_conn_handle_t c) |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 943 | { |
| 944 | libmemif_main_t *lm = &libmemif_main; |
| 945 | memif_connection_t *conn = (memif_connection_t *) c; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 946 | memif_socket_t *ms; |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 947 | int err = MEMIF_ERR_SUCCESS; |
| 948 | int sockfd = -1; |
| 949 | struct sockaddr_un sun; |
| 950 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 951 | if (conn == NULL) |
| 952 | return MEMIF_ERR_NOCONN; |
| 953 | |
| 954 | ms = (memif_socket_t *) conn->args.socket; |
| 955 | |
| 956 | if (conn->args.is_master || ms->type == MEMIF_SOCKET_TYPE_LISTENER) |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 957 | return MEMIF_ERR_INVAL_ARG; |
| 958 | if (conn->fd > 0) |
| 959 | return MEMIF_ERR_ALRCONN; |
| 960 | |
| 961 | sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); |
| 962 | if (sockfd < 0) |
| 963 | { |
| 964 | err = memif_syscall_error_handler (errno); |
| 965 | goto error; |
| 966 | } |
| 967 | |
| 968 | sun.sun_family = AF_UNIX; |
| 969 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 970 | strncpy (sun.sun_path, (char *) ms->filename, sizeof (sun.sun_path) - 1); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 971 | |
| 972 | if (connect (sockfd, (struct sockaddr *) &sun, |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 973 | sizeof (struct sockaddr_un)) == 0) |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 974 | { |
| 975 | conn->fd = sockfd; |
| 976 | conn->read_fn = memif_conn_fd_read_ready; |
| 977 | conn->write_fn = memif_conn_fd_write_ready; |
| 978 | conn->error_fn = memif_conn_fd_error; |
| 979 | |
| 980 | lm->control_list[conn->index].key = conn->fd; |
| 981 | |
| 982 | lm->control_fd_update (sockfd, |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 983 | MEMIF_FD_EVENT_READ | |
| 984 | MEMIF_FD_EVENT_WRITE, conn->private_ctx); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 985 | |
| 986 | lm->disconn_slaves--; |
| 987 | if (lm->disconn_slaves == 0) |
| 988 | { |
| 989 | if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) |
| 990 | { |
| 991 | err = memif_syscall_error_handler (errno); |
| 992 | return err; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | else |
| 997 | { |
| 998 | strcpy ((char *) conn->remote_disconnect_string, |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 999 | memif_strerror (memif_syscall_error_handler (errno))); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1000 | goto error; |
| 1001 | } |
| 1002 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1003 | ms->type = MEMIF_SOCKET_TYPE_CLIENT; |
| 1004 | |
| 1005 | return err; |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1006 | |
| 1007 | error: |
| 1008 | if (sockfd > 0) |
| 1009 | close (sockfd); |
| 1010 | sockfd = -1; |
| 1011 | return err; |
| 1012 | } |
| 1013 | |
| 1014 | int |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1015 | memif_control_fd_handler (int fd, uint8_t events) |
| 1016 | { |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1017 | int i, err = MEMIF_ERR_SUCCESS; /* 0 */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1018 | uint16_t num; |
| 1019 | memif_list_elt_t *e = NULL; |
| 1020 | memif_connection_t *conn; |
| 1021 | libmemif_main_t *lm = &libmemif_main; |
| 1022 | if (fd == lm->timerfd) |
| 1023 | { |
| 1024 | uint64_t b; |
| 1025 | ssize_t size; |
| 1026 | size = read (fd, &b, sizeof (b)); |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 1027 | |
| 1028 | if (size == -1) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1029 | goto error; |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 1030 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1031 | for (i = 0; i < lm->control_list_len; i++) |
| 1032 | { |
| 1033 | if ((lm->control_list[i].key < 0) |
| 1034 | && (lm->control_list[i].data_struct != NULL)) |
| 1035 | { |
| 1036 | conn = lm->control_list[i].data_struct; |
| 1037 | if (conn->args.is_master) |
| 1038 | continue; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1039 | err = memif_request_connection (conn); |
| 1040 | if (err != MEMIF_ERR_SUCCESS) |
| 1041 | DBG ("memif_request_connection: %s", memif_strerror (err)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | else |
| 1046 | { |
| 1047 | get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd); |
| 1048 | if (e != NULL) |
| 1049 | { |
| 1050 | if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL) |
| 1051 | { |
| 1052 | num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1053 | (((memif_connection_t *) e->data_struct)-> |
| 1054 | args.is_master) ? ((memif_connection_t *) e-> |
| 1055 | data_struct)->run_args. |
| 1056 | num_s2m_rings : ((memif_connection_t *) e->data_struct)-> |
| 1057 | run_args.num_m2s_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1058 | for (i = 0; i < num; i++) |
| 1059 | { |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1060 | if (((memif_connection_t *) e->data_struct)-> |
| 1061 | rx_queues[i].int_fd == fd) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1062 | { |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1063 | ((memif_connection_t *) e->data_struct)-> |
| 1064 | on_interrupt ((void *) e->data_struct, |
| 1065 | ((memif_connection_t *) e-> |
| 1066 | data_struct)->private_ctx, i); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1067 | return MEMIF_ERR_SUCCESS; |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | return MEMIF_ERR_SUCCESS; |
| 1072 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1073 | get_list_elt (&e, lm->socket_list, lm->socket_list_len, fd); |
| 1074 | if (e != NULL |
| 1075 | && ((memif_socket_t *) e->data_struct)->type == |
| 1076 | MEMIF_SOCKET_TYPE_LISTENER) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1077 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1078 | err = |
| 1079 | memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1080 | return err; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd); |
| 1084 | if (e != NULL) |
| 1085 | { |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1086 | err = memif_read_ready (fd); |
| 1087 | return err; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | get_list_elt (&e, lm->control_list, lm->control_list_len, fd); |
| 1091 | if (e != NULL) |
| 1092 | { |
| 1093 | if (events & MEMIF_FD_EVENT_READ) |
| 1094 | { |
| 1095 | err = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1096 | ((memif_connection_t *) e->data_struct)-> |
| 1097 | read_fn (e->data_struct); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1098 | if (err != MEMIF_ERR_SUCCESS) |
| 1099 | return err; |
| 1100 | } |
| 1101 | if (events & MEMIF_FD_EVENT_WRITE) |
| 1102 | { |
| 1103 | err = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1104 | ((memif_connection_t *) e->data_struct)-> |
| 1105 | write_fn (e->data_struct); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1106 | if (err != MEMIF_ERR_SUCCESS) |
| 1107 | return err; |
| 1108 | } |
| 1109 | if (events & MEMIF_FD_EVENT_ERROR) |
| 1110 | { |
| 1111 | err = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1112 | ((memif_connection_t *) e->data_struct)-> |
| 1113 | error_fn (e->data_struct); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1114 | if (err != MEMIF_ERR_SUCCESS) |
| 1115 | return err; |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | return MEMIF_ERR_SUCCESS; /* 0 */ |
| 1121 | |
| 1122 | error: |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1123 | return err; |
| 1124 | } |
| 1125 | |
| 1126 | int |
| 1127 | memif_poll_event (int timeout) |
| 1128 | { |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 1129 | struct epoll_event evt; |
| 1130 | int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1131 | uint32_t events = 0; |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1132 | uint64_t counter = 0; |
| 1133 | ssize_t r = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1134 | memset (&evt, 0, sizeof (evt)); |
| 1135 | evt.events = EPOLLIN | EPOLLOUT; |
| 1136 | sigset_t sigset; |
| 1137 | sigemptyset (&sigset); |
| 1138 | en = epoll_pwait (memif_epfd, &evt, 1, timeout, &sigset); |
| 1139 | if (en < 0) |
| 1140 | { |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1141 | err = errno; |
| 1142 | DBG ("epoll_pwait: %s", strerror (err)); |
| 1143 | return memif_syscall_error_handler (err); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1144 | } |
| 1145 | if (en > 0) |
| 1146 | { |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1147 | if (evt.data.fd == poll_cancel_fd) |
| 1148 | { |
| 1149 | r = read (evt.data.fd, &counter, sizeof (counter)); |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 1150 | if (r == -1) |
| 1151 | return MEMIF_ERR_DISCONNECTED; |
| 1152 | |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1153 | return MEMIF_ERR_POLL_CANCEL; |
| 1154 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1155 | if (evt.events & EPOLLIN) |
| 1156 | events |= MEMIF_FD_EVENT_READ; |
| 1157 | if (evt.events & EPOLLOUT) |
| 1158 | events |= MEMIF_FD_EVENT_WRITE; |
| 1159 | if (evt.events & EPOLLERR) |
| 1160 | events |= MEMIF_FD_EVENT_ERROR; |
| 1161 | err = memif_control_fd_handler (evt.data.fd, events); |
| 1162 | return err; |
| 1163 | } |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1167 | int |
| 1168 | memif_cancel_poll_event () |
| 1169 | { |
| 1170 | uint64_t counter = 1; |
| 1171 | ssize_t w = 0; |
| 1172 | |
| 1173 | if (poll_cancel_fd == -1) |
| 1174 | return 0; |
| 1175 | w = write (poll_cancel_fd, &counter, sizeof (counter)); |
| 1176 | if (w < sizeof (counter)) |
| 1177 | return MEMIF_ERR_INT_WRITE; |
| 1178 | |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1182 | static void |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1183 | memif_msg_queue_free (libmemif_main_t * lm, memif_msg_queue_elt_t ** e) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1184 | { |
| 1185 | if (*e == NULL) |
| 1186 | return; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1187 | memif_msg_queue_free (lm, &(*e)->next); |
| 1188 | lm->free (*e); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1189 | *e = NULL; |
| 1190 | return; |
| 1191 | } |
| 1192 | |
| 1193 | /* send disconnect msg and close interface */ |
| 1194 | int |
| 1195 | memif_disconnect_internal (memif_connection_t * c) |
| 1196 | { |
| 1197 | if (c == NULL) |
| 1198 | { |
| 1199 | DBG ("no connection"); |
| 1200 | return MEMIF_ERR_NOCONN; |
| 1201 | } |
| 1202 | uint16_t num; |
| 1203 | int err = MEMIF_ERR_SUCCESS, i; /* 0 */ |
| 1204 | memif_queue_t *mq; |
| 1205 | libmemif_main_t *lm = &libmemif_main; |
| 1206 | memif_list_elt_t *e; |
| 1207 | |
| 1208 | c->on_disconnect ((void *) c, c->private_ctx); |
| 1209 | |
| 1210 | if (c->fd > 0) |
| 1211 | { |
Jakub Grajciar | 5e1f69a | 2018-04-12 13:56:29 +0200 | [diff] [blame] | 1212 | memif_msg_send_disconnect (c->fd, (uint8_t *) "interface deleted", 0); |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1213 | lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL, c->private_ctx); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1214 | close (c->fd); |
| 1215 | } |
| 1216 | get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd); |
| 1217 | if (e != NULL) |
| 1218 | { |
| 1219 | if (c->args.is_master) |
| 1220 | free_list_elt (lm->control_list, lm->control_list_len, c->fd); |
| 1221 | e->key = c->fd = -1; |
| 1222 | } |
| 1223 | |
| 1224 | if (c->tx_queues != NULL) |
| 1225 | { |
| 1226 | num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1227 | (c->args.is_master) ? c->run_args.num_m2s_rings : c-> |
| 1228 | run_args.num_s2m_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1229 | for (i = 0; i < num; i++) |
| 1230 | { |
| 1231 | mq = &c->tx_queues[i]; |
| 1232 | if (mq != NULL) |
| 1233 | { |
| 1234 | if (mq->int_fd > 0) |
| 1235 | close (mq->int_fd); |
| 1236 | free_list_elt (lm->interrupt_list, lm->interrupt_list_len, |
| 1237 | mq->int_fd); |
| 1238 | mq->int_fd = -1; |
| 1239 | } |
| 1240 | } |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1241 | lm->free (c->tx_queues); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1242 | c->tx_queues = NULL; |
| 1243 | } |
| 1244 | |
| 1245 | if (c->rx_queues != NULL) |
| 1246 | { |
| 1247 | num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1248 | (c->args.is_master) ? c->run_args.num_s2m_rings : c-> |
| 1249 | run_args.num_m2s_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1250 | for (i = 0; i < num; i++) |
| 1251 | { |
| 1252 | mq = &c->rx_queues[i]; |
| 1253 | if (mq != NULL) |
| 1254 | { |
| 1255 | if (mq->int_fd > 0) |
| 1256 | { |
| 1257 | if (c->on_interrupt != NULL) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1258 | lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL, |
| 1259 | c->private_ctx); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1260 | close (mq->int_fd); |
| 1261 | } |
| 1262 | free_list_elt (lm->interrupt_list, lm->interrupt_list_len, |
| 1263 | mq->int_fd); |
| 1264 | mq->int_fd = -1; |
| 1265 | } |
| 1266 | } |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1267 | lm->free (c->rx_queues); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1268 | c->rx_queues = NULL; |
| 1269 | } |
| 1270 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1271 | for (i = 0; i < c->regions_num; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1272 | { |
Jakub Grajciar | 412e7b6 | 2018-09-27 10:26:35 +0200 | [diff] [blame] | 1273 | if (&c->regions[i] == NULL) |
| 1274 | continue; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1275 | if (c->regions[i].is_external != 0) |
| 1276 | { |
| 1277 | lm->del_external_region (c->regions[i].addr, |
| 1278 | c->regions[i].region_size, |
| 1279 | c->regions[i].fd, c->private_ctx); |
| 1280 | } |
| 1281 | else |
| 1282 | { |
| 1283 | if (munmap (c->regions[i].addr, c->regions[i].region_size) < 0) |
| 1284 | return memif_syscall_error_handler (errno); |
| 1285 | if (c->regions[i].fd > 0) |
| 1286 | close (c->regions[i].fd); |
| 1287 | c->regions[i].fd = -1; |
| 1288 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1289 | } |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1290 | lm->free (c->regions); |
| 1291 | c->regions = NULL; |
| 1292 | c->regions_num = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1293 | |
| 1294 | memset (&c->run_args, 0, sizeof (memif_conn_run_args_t)); |
| 1295 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1296 | memif_msg_queue_free (lm, &c->msg_queue); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1297 | |
| 1298 | if (!(c->args.is_master)) |
| 1299 | { |
| 1300 | if (lm->disconn_slaves == 0) |
| 1301 | { |
| 1302 | if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) |
| 1303 | { |
| 1304 | err = memif_syscall_error_handler (errno); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1305 | DBG ("timerfd_settime: arm"); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | lm->disconn_slaves++; |
| 1309 | } |
| 1310 | |
| 1311 | return err; |
| 1312 | } |
| 1313 | |
| 1314 | int |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1315 | memif_delete_socket (memif_socket_handle_t * sock) |
| 1316 | { |
| 1317 | memif_socket_t *ms = (memif_socket_t *) * sock; |
| 1318 | libmemif_main_t *lm = &libmemif_main; |
| 1319 | |
| 1320 | /* check if socket is in use */ |
| 1321 | if (ms == NULL || ms->type != MEMIF_SOCKET_TYPE_NONE) |
| 1322 | return MEMIF_ERR_INVAL_ARG; |
| 1323 | |
| 1324 | lm->free (ms->interface_list); |
| 1325 | ms->interface_list = NULL; |
| 1326 | lm->free (ms->filename); |
| 1327 | ms->filename = NULL; |
| 1328 | lm->free (ms); |
| 1329 | *sock = ms = NULL; |
| 1330 | |
| 1331 | return MEMIF_ERR_SUCCESS; |
| 1332 | } |
| 1333 | |
| 1334 | int |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1335 | memif_delete (memif_conn_handle_t * conn) |
| 1336 | { |
| 1337 | memif_connection_t *c = (memif_connection_t *) * conn; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1338 | libmemif_main_t *lm = &libmemif_main; |
| 1339 | memif_socket_t *ms = NULL; |
| 1340 | int err = MEMIF_ERR_SUCCESS; |
| 1341 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1342 | if (c == NULL) |
| 1343 | { |
| 1344 | DBG ("no connection"); |
| 1345 | return MEMIF_ERR_NOCONN; |
| 1346 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1347 | |
| 1348 | if (c->fd > 0) |
| 1349 | { |
| 1350 | DBG ("DISCONNECTING"); |
| 1351 | err = memif_disconnect_internal (c); |
| 1352 | if (err == MEMIF_ERR_NOCONN) |
| 1353 | return err; |
| 1354 | } |
| 1355 | |
| 1356 | free_list_elt_ctx (lm->control_list, lm->control_list_len, c); |
| 1357 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1358 | ms = (memif_socket_t *) c->args.socket; |
| 1359 | ms->use_count--; |
| 1360 | free_list_elt (ms->interface_list, ms->interface_list_len, |
| 1361 | c->args.interface_id); |
| 1362 | if (ms->use_count <= 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1363 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1364 | /* stop listening on this socket */ |
| 1365 | if (ms->type == MEMIF_SOCKET_TYPE_LISTENER) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1366 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1367 | lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_DEL, ms->private_ctx); |
| 1368 | free_list_elt (lm->socket_list, lm->socket_list_len, ms->fd); |
| 1369 | close (ms->fd); |
| 1370 | ms->fd = -1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1371 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1372 | /* socket not in use */ |
| 1373 | ms->type = MEMIF_SOCKET_TYPE_NONE; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1374 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1375 | |
| 1376 | if (!c->args.is_master) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1377 | { |
| 1378 | lm->disconn_slaves--; |
| 1379 | if (lm->disconn_slaves <= 0) |
| 1380 | { |
| 1381 | if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) |
| 1382 | { |
| 1383 | err = memif_syscall_error_handler (errno); |
| 1384 | DBG ("timerfd_settime: disarm"); |
| 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1389 | lm->free (c); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1390 | c = NULL; |
| 1391 | |
| 1392 | *conn = c; |
| 1393 | return err; |
| 1394 | } |
| 1395 | |
| 1396 | int |
| 1397 | memif_connect1 (memif_connection_t * c) |
| 1398 | { |
| 1399 | libmemif_main_t *lm = &libmemif_main; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1400 | memif_region_t *mr; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1401 | memif_queue_t *mq; |
| 1402 | int i; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1403 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1404 | for (i = 0; i < c->regions_num; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1405 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1406 | mr = &c->regions[i]; |
| 1407 | if (mr != NULL) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1408 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1409 | if (!mr->addr) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1410 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1411 | if (mr->is_external) |
| 1412 | { |
| 1413 | if (lm->get_external_region_addr == NULL) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1414 | return MEMIF_ERR_INVAL_ARG; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1415 | mr->addr = |
| 1416 | lm->get_external_region_addr (mr->region_size, mr->fd, |
| 1417 | c->private_ctx); |
| 1418 | } |
| 1419 | else |
| 1420 | { |
| 1421 | if (mr->fd < 0) |
| 1422 | return MEMIF_ERR_NO_SHMFD; |
| 1423 | |
| 1424 | if ((mr->addr = |
| 1425 | mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE, |
| 1426 | MAP_SHARED, mr->fd, 0)) == MAP_FAILED) |
| 1427 | { |
| 1428 | return memif_syscall_error_handler (errno); |
| 1429 | } |
| 1430 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1435 | for (i = 0; i < c->rx_queues_num; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1436 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1437 | mq = &c->rx_queues[i]; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1438 | if (mq != NULL) |
| 1439 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1440 | mq->ring = c->regions[mq->region].addr + mq->offset; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1441 | if (mq->ring->cookie != MEMIF_COOKIE) |
| 1442 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1443 | DBG ("wrong cookie on rx ring %u", i); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1444 | return MEMIF_ERR_COOKIE; |
| 1445 | } |
| 1446 | mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs = |
| 1447 | 0; |
| 1448 | } |
| 1449 | } |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1450 | |
| 1451 | for (i = 0; i < c->tx_queues_num; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1452 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1453 | mq = &c->tx_queues[i]; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1454 | if (mq != NULL) |
| 1455 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1456 | mq->ring = c->regions[mq->region].addr + mq->offset; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1457 | if (mq->ring->cookie != MEMIF_COOKIE) |
| 1458 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1459 | DBG ("wrong cookie on tx ring %u", i); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1460 | return MEMIF_ERR_COOKIE; |
| 1461 | } |
| 1462 | mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs = |
| 1463 | 0; |
| 1464 | } |
| 1465 | } |
| 1466 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1467 | lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD, |
| 1468 | c->private_ctx); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1469 | |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1473 | static inline int |
| 1474 | memif_add_region (libmemif_main_t * lm, memif_connection_t * conn, |
| 1475 | uint8_t has_buffers) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1476 | { |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1477 | memif_region_t *r; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1478 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1479 | r = |
| 1480 | lm->realloc (conn->regions, |
| 1481 | sizeof (memif_region_t) * ++conn->regions_num); |
| 1482 | if (r == NULL) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1483 | return MEMIF_ERR_NOMEM; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1484 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1485 | conn->regions = r; |
| 1486 | r = &conn->regions[conn->regions_num - 1]; |
| 1487 | memset (r, 0, sizeof (memif_region_t)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1488 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1489 | if (has_buffers != 0) |
| 1490 | { |
| 1491 | r->buffer_offset = 0; |
| 1492 | } |
| 1493 | else |
| 1494 | { |
| 1495 | r->buffer_offset = |
| 1496 | (conn->run_args.num_s2m_rings + |
| 1497 | conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) + |
| 1498 | sizeof (memif_desc_t) * |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1499 | (1 << conn-> |
| 1500 | run_args.log2_ring_size)); |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | r->region_size = (has_buffers == 0) ? r->buffer_offset : r->buffer_offset + |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1504 | conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) * |
| 1505 | (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings); |
| 1506 | |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1507 | if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1508 | return memif_syscall_error_handler (errno); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1509 | |
| 1510 | if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) |
| 1511 | return memif_syscall_error_handler (errno); |
| 1512 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1513 | if ((ftruncate (r->fd, r->region_size)) == -1) |
| 1514 | return memif_syscall_error_handler (errno); |
| 1515 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1516 | if ((r->addr = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE, |
| 1517 | MAP_SHARED, r->fd, 0)) == MAP_FAILED) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1518 | return memif_syscall_error_handler (errno); |
| 1519 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1520 | return MEMIF_ERR_SUCCESS; |
| 1521 | } |
| 1522 | |
| 1523 | static inline int |
| 1524 | memif_init_queues (libmemif_main_t * lm, memif_connection_t * conn) |
| 1525 | { |
| 1526 | int i, j; |
| 1527 | memif_ring_t *ring; |
| 1528 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1529 | for (i = 0; i < conn->run_args.num_s2m_rings; i++) |
| 1530 | { |
| 1531 | ring = memif_get_ring (conn, MEMIF_RING_S2M, i); |
| 1532 | DBG ("RING: %p I: %d", ring, i); |
| 1533 | ring->head = ring->tail = 0; |
| 1534 | ring->cookie = MEMIF_COOKIE; |
| 1535 | ring->flags = 0; |
| 1536 | for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++) |
| 1537 | { |
| 1538 | uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1539 | ring->desc[j].region = 1; |
| 1540 | ring->desc[j].offset = |
| 1541 | conn->regions[1].buffer_offset + |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1542 | (uint32_t) (slot * conn->run_args.buffer_size); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1543 | ring->desc[j].length = conn->run_args.buffer_size; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | for (i = 0; i < conn->run_args.num_m2s_rings; i++) |
| 1547 | { |
| 1548 | ring = memif_get_ring (conn, MEMIF_RING_M2S, i); |
| 1549 | DBG ("RING: %p I: %d", ring, i); |
| 1550 | ring->head = ring->tail = 0; |
| 1551 | ring->cookie = MEMIF_COOKIE; |
| 1552 | ring->flags = 0; |
| 1553 | for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++) |
| 1554 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1555 | uint16_t slot = (i + conn->run_args.num_s2m_rings) * |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1556 | (1 << conn->run_args.log2_ring_size) + j; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1557 | ring->desc[j].region = 1; |
| 1558 | ring->desc[j].offset = |
| 1559 | conn->regions[1].buffer_offset + |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1560 | (uint32_t) (slot * conn->run_args.buffer_size); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1561 | ring->desc[j].length = conn->run_args.buffer_size; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | memif_queue_t *mq; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1565 | DBG ("alloc: %p", lm->alloc); |
| 1566 | DBG ("size: %lu", sizeof (memif_queue_t) * conn->run_args.num_s2m_rings); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1567 | mq = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1568 | (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) * |
| 1569 | conn->run_args.num_s2m_rings); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1570 | if (mq == NULL) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1571 | return MEMIF_ERR_NOMEM; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1572 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1573 | int x; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1574 | memif_list_elt_t e; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1575 | for (x = 0; x < conn->run_args.num_s2m_rings; x++) |
| 1576 | { |
| 1577 | if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) |
| 1578 | return memif_syscall_error_handler (errno); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1579 | e.key = mq[x].int_fd; |
| 1580 | e.data_struct = conn; |
| 1581 | add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len); |
| 1582 | |
| 1583 | mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x); |
| 1584 | DBG ("RING: %p I: %d", mq[x].ring, x); |
| 1585 | mq[x].log2_ring_size = conn->run_args.log2_ring_size; |
| 1586 | mq[x].region = 0; |
| 1587 | mq[x].offset = |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1588 | (void *) mq[x].ring - (void *) conn->regions[mq->region].addr; |
| 1589 | mq[x].last_head = mq[x].last_tail = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1590 | mq[x].alloc_bufs = 0; |
| 1591 | } |
| 1592 | conn->tx_queues = mq; |
| 1593 | |
| 1594 | mq = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1595 | (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) * |
| 1596 | conn->run_args.num_m2s_rings); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1597 | if (mq == NULL) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1598 | return MEMIF_ERR_NOMEM; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1599 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1600 | for (x = 0; x < conn->run_args.num_m2s_rings; x++) |
| 1601 | { |
| 1602 | if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) |
| 1603 | return memif_syscall_error_handler (errno); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1604 | e.key = mq[x].int_fd; |
| 1605 | e.data_struct = conn; |
| 1606 | add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len); |
| 1607 | |
| 1608 | mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x); |
| 1609 | DBG ("RING: %p I: %d", mq[x].ring, x); |
| 1610 | mq[x].log2_ring_size = conn->run_args.log2_ring_size; |
| 1611 | mq[x].region = 0; |
| 1612 | mq[x].offset = |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1613 | (void *) mq[x].ring - (void *) conn->regions[mq->region].addr; |
| 1614 | mq[x].last_head = mq[x].last_tail = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1615 | mq[x].alloc_bufs = 0; |
| 1616 | } |
| 1617 | conn->rx_queues = mq; |
| 1618 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1619 | return MEMIF_ERR_SUCCESS; |
| 1620 | } |
| 1621 | |
| 1622 | int |
| 1623 | memif_init_regions_and_queues (memif_connection_t * conn) |
| 1624 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1625 | memif_region_t *r; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1626 | libmemif_main_t *lm = &libmemif_main; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1627 | |
| 1628 | /* region 0. rings */ |
| 1629 | memif_add_region (lm, conn, /* has_buffers */ 0); |
| 1630 | |
| 1631 | /* region 1. buffers */ |
| 1632 | if (lm->add_external_region) |
| 1633 | { |
| 1634 | r = |
| 1635 | (memif_region_t *) lm->realloc (conn->regions, |
| 1636 | sizeof (memif_region_t) * |
| 1637 | ++conn->regions_num); |
| 1638 | if (r == NULL) |
| 1639 | return MEMIF_ERR_NOMEM; |
| 1640 | conn->regions = r; |
| 1641 | |
| 1642 | conn->regions[1].region_size = |
| 1643 | conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) * |
| 1644 | (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings); |
| 1645 | conn->regions[1].buffer_offset = 0; |
| 1646 | lm->add_external_region (&conn->regions[1].addr, |
| 1647 | conn->regions[1].region_size, |
| 1648 | &conn->regions[1].fd, conn->private_ctx); |
| 1649 | conn->regions[1].is_external = 1; |
| 1650 | } |
| 1651 | else |
| 1652 | { |
| 1653 | memif_add_region (lm, conn, 1); |
| 1654 | } |
| 1655 | |
| 1656 | memif_init_queues (lm, conn); |
| 1657 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1658 | return 0; |
| 1659 | } |
| 1660 | |
| 1661 | int |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1662 | memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid, |
| 1663 | memif_buffer_t * bufs, uint16_t count, |
| 1664 | uint16_t * count_out) |
| 1665 | { |
| 1666 | memif_connection_t *c = (memif_connection_t *) conn; |
| 1667 | if (EXPECT_FALSE (c == NULL)) |
| 1668 | return MEMIF_ERR_NOCONN; |
| 1669 | if (EXPECT_FALSE (c->fd < 0)) |
| 1670 | return MEMIF_ERR_DISCONNECTED; |
| 1671 | uint8_t num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1672 | (c->args.is_master) ? c->run_args.num_m2s_rings : c-> |
| 1673 | run_args.num_s2m_rings; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1674 | if (EXPECT_FALSE (qid >= num)) |
| 1675 | return MEMIF_ERR_QID; |
| 1676 | if (EXPECT_FALSE (!count_out)) |
| 1677 | return MEMIF_ERR_INVAL_ARG; |
| 1678 | if (EXPECT_FALSE (c->args.is_master)) |
| 1679 | return MEMIF_ERR_INVAL_ARG; |
| 1680 | |
| 1681 | memif_queue_t *mq = &c->tx_queues[qid]; |
| 1682 | memif_ring_t *ring = mq->ring; |
| 1683 | memif_buffer_t *b0; |
| 1684 | uint16_t mask = (1 << mq->log2_ring_size) - 1; |
| 1685 | uint16_t ring_size; |
| 1686 | uint16_t slot, ns; |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 1687 | int err = MEMIF_ERR_SUCCESS; /* 0 */ |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1688 | *count_out = 0; |
| 1689 | |
| 1690 | ring_size = (1 << mq->log2_ring_size); |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1691 | slot = (c->args.is_master) ? ring->tail : ring->head; |
| 1692 | slot += mq->alloc_bufs; |
| 1693 | |
| 1694 | /* can only be called by slave */ |
Milan Lenco | f8b43e5 | 2018-06-26 15:16:15 +0200 | [diff] [blame] | 1695 | ns = ring_size - (ring->head + mq->alloc_bufs) + ring->tail; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1696 | |
| 1697 | b0 = bufs; |
| 1698 | |
| 1699 | while (count && ns) |
| 1700 | { |
| 1701 | if (EXPECT_FALSE ((b0->flags & MEMIF_BUFFER_FLAG_RX) == 0)) |
| 1702 | { |
| 1703 | /* not a valid buffer */ |
| 1704 | count--; |
| 1705 | continue; |
| 1706 | } |
| 1707 | b0->flags &= ~MEMIF_BUFFER_FLAG_RX; |
| 1708 | |
| 1709 | ((memif_ring_t *) b0->ring)->desc[b0->desc_index & mask].offset = ring->desc[slot & mask].offset; /* put free buffer on rx ring */ |
| 1710 | |
| 1711 | ring->desc[slot & mask].offset = |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1712 | (uint32_t) (b0->data - |
| 1713 | c->regions[ring->desc[slot & mask].region].addr); |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1714 | ring->desc[slot & mask].flags |= |
| 1715 | (b0->flags & MEMIF_BUFFER_FLAG_NEXT) ? MEMIF_DESC_FLAG_NEXT : 0; |
| 1716 | |
| 1717 | b0->desc_index = slot; |
| 1718 | |
| 1719 | mq->alloc_bufs++; |
| 1720 | slot++; |
| 1721 | |
| 1722 | count--; |
| 1723 | ns--; |
| 1724 | b0++; |
| 1725 | *count_out += 1; |
| 1726 | } |
| 1727 | |
| 1728 | DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count, |
| 1729 | mq->alloc_bufs); |
| 1730 | |
| 1731 | if (count) |
| 1732 | { |
| 1733 | DBG ("ring buffer full! qid: %u", qid); |
| 1734 | err = MEMIF_ERR_NOBUF_RING; |
| 1735 | } |
| 1736 | |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1737 | return err; |
| 1738 | } |
| 1739 | |
| 1740 | int |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1741 | memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, |
| 1742 | memif_buffer_t * bufs, uint16_t count, |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 1743 | uint16_t * count_out, uint16_t size) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1744 | { |
| 1745 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1746 | if (EXPECT_FALSE (c == NULL)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1747 | return MEMIF_ERR_NOCONN; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1748 | if (EXPECT_FALSE (c->fd < 0)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1749 | return MEMIF_ERR_DISCONNECTED; |
| 1750 | uint8_t num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1751 | (c->args.is_master) ? c->run_args.num_m2s_rings : c-> |
| 1752 | run_args.num_s2m_rings; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1753 | if (EXPECT_FALSE (qid >= num)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1754 | return MEMIF_ERR_QID; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1755 | if (EXPECT_FALSE (!count_out)) |
| 1756 | return MEMIF_ERR_INVAL_ARG; |
| 1757 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1758 | libmemif_main_t *lm = &libmemif_main; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1759 | memif_queue_t *mq = &c->tx_queues[qid]; |
| 1760 | memif_ring_t *ring = mq->ring; |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 1761 | memif_buffer_t *b0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1762 | uint16_t mask = (1 << mq->log2_ring_size) - 1; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1763 | uint32_t offset_mask = c->run_args.buffer_size - 1; |
Chun Li | 5dca546 | 2018-02-07 09:51:35 +0800 | [diff] [blame] | 1764 | uint16_t ring_size; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1765 | uint16_t slot, ns; |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 1766 | int err = MEMIF_ERR_SUCCESS; /* 0 */ |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1767 | uint16_t dst_left, src_left; |
| 1768 | uint16_t saved_count; |
| 1769 | memif_buffer_t *saved_b; |
| 1770 | *count_out = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1771 | |
Chun Li | 5dca546 | 2018-02-07 09:51:35 +0800 | [diff] [blame] | 1772 | ring_size = (1 << mq->log2_ring_size); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1773 | slot = (c->args.is_master) ? ring->tail : ring->head; |
| 1774 | slot += mq->alloc_bufs; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1775 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1776 | if (c->args.is_master) |
Milan Lenco | f8b43e5 | 2018-06-26 15:16:15 +0200 | [diff] [blame] | 1777 | ns = ring->head - (ring->tail + mq->alloc_bufs); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1778 | else |
Milan Lenco | f8b43e5 | 2018-06-26 15:16:15 +0200 | [diff] [blame] | 1779 | ns = ring_size - (ring->head + mq->alloc_bufs) + ring->tail; |
Jakub Grajciar | 04b68bd | 2017-10-30 10:34:54 +0100 | [diff] [blame] | 1780 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1781 | while (count && ns) |
| 1782 | { |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1783 | b0 = (bufs + *count_out); |
| 1784 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1785 | saved_b = b0; |
| 1786 | saved_count = count; |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 1787 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1788 | b0->desc_index = slot; |
| 1789 | ring->desc[slot & mask].flags = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1790 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1791 | /* slave can produce buffer with original length */ |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1792 | dst_left = (c->args.is_master) ? ring->desc[slot & mask].length : |
| 1793 | c->run_args.buffer_size; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1794 | src_left = size; |
| 1795 | |
| 1796 | while (src_left) |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 1797 | { |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1798 | if (EXPECT_FALSE (dst_left == 0)) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1799 | { |
| 1800 | if (count && ns) |
| 1801 | { |
| 1802 | slot++; |
| 1803 | *count_out += 1; |
| 1804 | mq->alloc_bufs++; |
| 1805 | ns--; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1806 | |
| 1807 | ring->desc[b0->desc_index & mask].flags |= |
| 1808 | MEMIF_DESC_FLAG_NEXT; |
| 1809 | b0->flags |= MEMIF_BUFFER_FLAG_NEXT; |
| 1810 | |
| 1811 | b0 = (bufs + *count_out); |
| 1812 | b0->desc_index = slot; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1813 | dst_left = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1814 | (c->args.is_master) ? ring->desc[slot & mask]. |
| 1815 | length : c->run_args.buffer_size; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1816 | ring->desc[slot & mask].flags = 0; |
| 1817 | } |
| 1818 | else |
| 1819 | { |
| 1820 | /* rollback allocated chain buffers */ |
| 1821 | memset (saved_b, 0, sizeof (memif_buffer_t) |
| 1822 | * (saved_count - count + 1)); |
| 1823 | *count_out -= saved_count - count; |
| 1824 | mq->alloc_bufs = saved_count - count; |
| 1825 | goto no_ns; |
| 1826 | } |
| 1827 | } |
| 1828 | b0->len = memif_min (dst_left, src_left); |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1829 | |
| 1830 | /* slave resets buffer offset */ |
| 1831 | if (c->args.is_master == 0) |
| 1832 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1833 | memif_desc_t *d = &ring->desc[slot & mask]; |
| 1834 | if (lm->get_external_buffer_offset) |
| 1835 | d->offset = lm->get_external_buffer_offset (c->private_ctx); |
| 1836 | else |
| 1837 | d->offset = d->offset - (d->offset & offset_mask); |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1838 | } |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1839 | b0->data = memif_get_buffer (c, ring, slot & mask); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1840 | |
| 1841 | src_left -= b0->len; |
| 1842 | dst_left -= b0->len; |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 1843 | } |
| 1844 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1845 | slot++; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1846 | *count_out += 1; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1847 | mq->alloc_bufs++; |
| 1848 | ns--; |
| 1849 | count--; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1850 | } |
| 1851 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1852 | no_ns: |
| 1853 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1854 | DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count, |
| 1855 | mq->alloc_bufs); |
| 1856 | |
| 1857 | if (count) |
| 1858 | { |
| 1859 | DBG ("ring buffer full! qid: %u", qid); |
| 1860 | err = MEMIF_ERR_NOBUF_RING; |
| 1861 | } |
| 1862 | |
| 1863 | return err; |
| 1864 | } |
| 1865 | |
| 1866 | int |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1867 | memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count, |
| 1868 | uint16_t headroom) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1869 | { |
| 1870 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1871 | if (EXPECT_FALSE (c == NULL)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1872 | return MEMIF_ERR_NOCONN; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1873 | if (EXPECT_FALSE (c->fd < 0)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1874 | return MEMIF_ERR_DISCONNECTED; |
| 1875 | uint8_t num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1876 | (c->args.is_master) ? c->run_args.num_s2m_rings : c-> |
| 1877 | run_args.num_m2s_rings; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1878 | if (EXPECT_FALSE (qid >= num)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1879 | return MEMIF_ERR_QID; |
| 1880 | libmemif_main_t *lm = &libmemif_main; |
| 1881 | memif_queue_t *mq = &c->rx_queues[qid]; |
| 1882 | memif_ring_t *ring = mq->ring; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1883 | uint16_t mask = (1 << mq->log2_ring_size) - 1; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1884 | uint32_t offset_mask = c->run_args.buffer_size - 1; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1885 | uint16_t slot; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1886 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1887 | if (c->args.is_master) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1888 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1889 | MEMIF_MEMORY_BARRIER (); |
| 1890 | ring->tail = |
| 1891 | (ring->tail + count <= |
| 1892 | mq->last_head) ? ring->tail + count : mq->last_head; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1893 | return MEMIF_ERR_SUCCESS; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1894 | } |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1895 | |
| 1896 | uint16_t head = ring->head; |
| 1897 | uint16_t ns = (1 << mq->log2_ring_size) - head + mq->last_tail; |
| 1898 | head += (count < ns) ? count : ns; |
| 1899 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1900 | slot = ring->head; |
| 1901 | memif_desc_t *d; |
| 1902 | while (slot < head) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1903 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1904 | d = &ring->desc[slot & mask]; |
| 1905 | d->region = 1; |
| 1906 | d->length = c->run_args.buffer_size - headroom; |
| 1907 | if (lm->get_external_buffer_offset) |
| 1908 | d->offset = lm->get_external_buffer_offset (c->private_ctx); |
| 1909 | else |
| 1910 | d->offset = d->offset - (d->offset & offset_mask) + headroom; |
| 1911 | slot++; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1912 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1913 | |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1914 | MEMIF_MEMORY_BARRIER (); |
| 1915 | ring->head = head; |
| 1916 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1917 | return MEMIF_ERR_SUCCESS; /* 0 */ |
| 1918 | } |
| 1919 | |
| 1920 | int |
| 1921 | memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, |
| 1922 | memif_buffer_t * bufs, uint16_t count, uint16_t * tx) |
| 1923 | { |
| 1924 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1925 | if (EXPECT_FALSE (c == NULL)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1926 | return MEMIF_ERR_NOCONN; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1927 | if (EXPECT_FALSE (c->fd < 0)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1928 | return MEMIF_ERR_DISCONNECTED; |
| 1929 | uint8_t num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1930 | (c->args.is_master) ? c->run_args.num_m2s_rings : c-> |
| 1931 | run_args.num_s2m_rings; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1932 | if (EXPECT_FALSE (qid >= num)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1933 | return MEMIF_ERR_QID; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1934 | if (EXPECT_FALSE (!tx)) |
| 1935 | return MEMIF_ERR_INVAL_ARG; |
| 1936 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1937 | memif_queue_t *mq = &c->tx_queues[qid]; |
| 1938 | memif_ring_t *ring = mq->ring; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1939 | uint16_t mask = (1 << mq->log2_ring_size) - 1; |
| 1940 | memif_buffer_t *b0; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1941 | *tx = 0; |
| 1942 | |
| 1943 | if (count > mq->alloc_bufs) |
| 1944 | count = mq->alloc_bufs; |
| 1945 | |
| 1946 | if (EXPECT_FALSE (count == 0)) |
| 1947 | return MEMIF_ERR_SUCCESS; |
| 1948 | |
| 1949 | while (count) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1950 | { |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1951 | b0 = (bufs + *tx); |
| 1952 | ring->desc[b0->desc_index & mask].length = b0->len; |
| 1953 | |
| 1954 | #ifdef MEMIF_DBG_SHM |
| 1955 | printf ("offset: %-6d\n", ring->desc[b0->desc_index & mask].offset); |
| 1956 | printf ("data: %p\n", |
| 1957 | memif_get_buffer (c, ring, b0->desc_index & mask)); |
| 1958 | printf ("index: %u\n", b0->desc_index); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1959 | print_bytes (memif_get_buffer (c, ring, b0->desc_index & mask), |
| 1960 | ring->desc[b0->desc_index & mask].length, DBG_TX_BUF); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1961 | #endif /* MEMIF_DBG_SHM */ |
| 1962 | |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1963 | *tx += 1; |
| 1964 | count--; |
| 1965 | } |
| 1966 | |
| 1967 | |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1968 | MEMIF_MEMORY_BARRIER (); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1969 | if (c->args.is_master) |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1970 | ring->tail = b0->desc_index + 1; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1971 | else |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1972 | ring->head = b0->desc_index + 1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1973 | |
| 1974 | mq->alloc_bufs -= *tx; |
| 1975 | |
| 1976 | if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) |
| 1977 | { |
| 1978 | uint64_t a = 1; |
| 1979 | int r = write (mq->int_fd, &a, sizeof (a)); |
| 1980 | if (r < 0) |
| 1981 | return MEMIF_ERR_INT_WRITE; |
| 1982 | } |
| 1983 | |
| 1984 | return MEMIF_ERR_SUCCESS; /* 0 */ |
| 1985 | } |
| 1986 | |
| 1987 | int |
| 1988 | memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, |
| 1989 | memif_buffer_t * bufs, uint16_t count, uint16_t * rx) |
| 1990 | { |
| 1991 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1992 | if (EXPECT_FALSE (c == NULL)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1993 | return MEMIF_ERR_NOCONN; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1994 | if (EXPECT_FALSE (c->fd < 0)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1995 | return MEMIF_ERR_DISCONNECTED; |
| 1996 | uint8_t num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1997 | (c->args.is_master) ? c->run_args.num_s2m_rings : c-> |
| 1998 | run_args.num_m2s_rings; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1999 | if (EXPECT_FALSE (qid >= num)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2000 | return MEMIF_ERR_QID; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2001 | if (EXPECT_FALSE (!rx)) |
| 2002 | return MEMIF_ERR_INVAL_ARG; |
| 2003 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2004 | memif_queue_t *mq = &c->rx_queues[qid]; |
| 2005 | memif_ring_t *ring = mq->ring; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2006 | uint16_t cur_slot, last_slot; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2007 | uint16_t ns; |
| 2008 | uint16_t mask = (1 << mq->log2_ring_size) - 1; |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 2009 | memif_buffer_t *b0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2010 | *rx = 0; |
| 2011 | |
| 2012 | uint64_t b; |
| 2013 | ssize_t r = read (mq->int_fd, &b, sizeof (b)); |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2014 | if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN))) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2015 | return memif_syscall_error_handler (errno); |
| 2016 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2017 | cur_slot = (c->args.is_master) ? mq->last_head : mq->last_tail; |
| 2018 | last_slot = (c->args.is_master) ? ring->head : ring->tail; |
| 2019 | if (cur_slot == last_slot) |
| 2020 | return MEMIF_ERR_SUCCESS; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2021 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2022 | ns = last_slot - cur_slot; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2023 | |
| 2024 | while (ns && count) |
| 2025 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2026 | b0 = (bufs + *rx); |
| 2027 | |
| 2028 | b0->desc_index = cur_slot; |
| 2029 | b0->data = memif_get_buffer (c, ring, cur_slot & mask); |
| 2030 | b0->len = ring->desc[cur_slot & mask].length; |
| 2031 | /* slave resets buffer length */ |
| 2032 | if (c->args.is_master == 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2033 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2034 | ring->desc[cur_slot & mask].length = c->run_args.buffer_size; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2035 | } |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2036 | |
| 2037 | b0->flags = MEMIF_BUFFER_FLAG_RX; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2038 | if (ring->desc[cur_slot & mask].flags & MEMIF_DESC_FLAG_NEXT) |
| 2039 | { |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2040 | b0->flags |= MEMIF_BUFFER_FLAG_NEXT; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2041 | ring->desc[cur_slot & mask].flags &= ~MEMIF_DESC_FLAG_NEXT; |
| 2042 | } |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2043 | /* b0->offset = ring->desc[cur_slot & mask].offset;*/ |
| 2044 | b0->ring = ring; |
Jakub Grajciar | ba3c4e8 | 2017-09-18 11:21:40 +0200 | [diff] [blame] | 2045 | #ifdef MEMIF_DBG_SHM |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2046 | printf ("data: %p\n", b0->data); |
| 2047 | printf ("index: %u\n", b0->desc_index); |
| 2048 | printf ("ring: %p\n", b0->ring); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2049 | print_bytes (b0->data, b0->len, DBG_RX_BUF); |
Jakub Grajciar | ba3c4e8 | 2017-09-18 11:21:40 +0200 | [diff] [blame] | 2050 | #endif /* MEMIF_DBG_SHM */ |
| 2051 | ns--; |
| 2052 | *rx += 1; |
| 2053 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2054 | count--; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2055 | cur_slot++; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2056 | } |
| 2057 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2058 | if (c->args.is_master) |
| 2059 | mq->last_head = cur_slot; |
| 2060 | else |
| 2061 | mq->last_tail = cur_slot; |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 2062 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2063 | if (ns) |
| 2064 | { |
| 2065 | DBG ("not enough buffers!"); |
| 2066 | return MEMIF_ERR_NOBUF; |
| 2067 | } |
| 2068 | |
| 2069 | return MEMIF_ERR_SUCCESS; /* 0 */ |
| 2070 | } |
| 2071 | |
| 2072 | int |
| 2073 | memif_get_details (memif_conn_handle_t conn, memif_details_t * md, |
| 2074 | char *buf, ssize_t buflen) |
| 2075 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2076 | libmemif_main_t *lm = &libmemif_main; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2077 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2078 | memif_socket_t *ms; |
| 2079 | int err = MEMIF_ERR_SUCCESS, i; |
| 2080 | ssize_t l0 = 0, l1; |
| 2081 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2082 | if (c == NULL) |
| 2083 | return MEMIF_ERR_NOCONN; |
| 2084 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2085 | ms = (memif_socket_t *) c->args.socket; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2086 | |
| 2087 | l1 = strlen ((char *) c->args.interface_name); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2088 | if (l0 + l1 < buflen) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2089 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2090 | md->if_name = |
| 2091 | (uint8_t *) strcpy (buf + l0, (char *) c->args.interface_name); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2092 | l0 += l1 + 1; |
| 2093 | } |
| 2094 | else |
| 2095 | err = MEMIF_ERR_NOBUF_DET; |
| 2096 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2097 | l1 = strlen ((char *) lm->app_name); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2098 | if (l0 + l1 < buflen) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2099 | { |
Jakub Grajciar | 5e1f69a | 2018-04-12 13:56:29 +0200 | [diff] [blame] | 2100 | md->inst_name = (uint8_t *) strcpy (buf + l0, (char *) lm->app_name); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2101 | l0 += l1 + 1; |
| 2102 | } |
| 2103 | else |
| 2104 | err = MEMIF_ERR_NOBUF_DET; |
| 2105 | |
| 2106 | l1 = strlen ((char *) c->remote_if_name); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2107 | if (l0 + l1 < buflen) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2108 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2109 | md->remote_if_name = |
| 2110 | (uint8_t *) strcpy (buf + l0, (char *) c->remote_if_name); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2111 | l0 += l1 + 1; |
| 2112 | } |
| 2113 | else |
| 2114 | err = MEMIF_ERR_NOBUF_DET; |
| 2115 | |
| 2116 | l1 = strlen ((char *) c->remote_name); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2117 | if (l0 + l1 < buflen) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2118 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2119 | md->remote_inst_name = |
| 2120 | (uint8_t *) strcpy (buf + l0, (char *) c->remote_name); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2121 | l0 += l1 + 1; |
| 2122 | } |
| 2123 | else |
| 2124 | err = MEMIF_ERR_NOBUF_DET; |
| 2125 | |
| 2126 | md->id = c->args.interface_id; |
| 2127 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2128 | if (strlen ((char *) c->args.secret) > 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2129 | { |
| 2130 | l1 = strlen ((char *) c->args.secret); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2131 | if (l0 + l1 < buflen) |
| 2132 | { |
Jakub Grajciar | 5e1f69a | 2018-04-12 13:56:29 +0200 | [diff] [blame] | 2133 | md->secret = (uint8_t *) strcpy (buf + l0, (char *) c->args.secret); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2134 | l0 += l1 + 1; |
| 2135 | } |
| 2136 | else |
| 2137 | err = MEMIF_ERR_NOBUF_DET; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2138 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2139 | |
| 2140 | md->role = (c->args.is_master) ? 0 : 1; |
| 2141 | md->mode = c->args.mode; |
| 2142 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2143 | l1 = strlen ((char *) ms->filename); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2144 | if (l0 + l1 < buflen) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2145 | { |
| 2146 | md->socket_filename = |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2147 | (uint8_t *) strcpy (buf + l0, (char *) ms->filename); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2148 | l0 += l1 + 1; |
| 2149 | } |
| 2150 | else |
| 2151 | err = MEMIF_ERR_NOBUF_DET; |
| 2152 | |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2153 | l1 = strlen ((char *) c->remote_disconnect_string); |
| 2154 | if (l0 + l1 < buflen) |
| 2155 | { |
| 2156 | md->error = |
| 2157 | (uint8_t *) strcpy (buf + l0, (char *) c->remote_disconnect_string); |
| 2158 | l0 += l1 + 1; |
| 2159 | } |
| 2160 | else |
| 2161 | err = MEMIF_ERR_NOBUF_DET; |
| 2162 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2163 | md->regions_num = c->regions_num; |
| 2164 | l1 = sizeof (memif_region_details_t) * md->regions_num; |
| 2165 | if (l0 + l1 <= buflen) |
| 2166 | { |
Koichiro Den | 33331ed | 2019-01-03 00:22:24 +0900 | [diff] [blame] | 2167 | md->regions = (memif_region_details_t *) (buf + l0); |
Koichiro Den | be420e5 | 2018-12-15 00:31:34 +0900 | [diff] [blame] | 2168 | for (i = 0; i < md->regions_num; i++) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2169 | { |
| 2170 | md->regions[i].index = i; |
| 2171 | md->regions[i].addr = c->regions[i].addr; |
| 2172 | md->regions[i].size = c->regions[i].region_size; |
| 2173 | md->regions[i].fd = c->regions[i].fd; |
| 2174 | md->regions[i].is_external = c->regions[i].is_external; |
| 2175 | } |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2176 | l0 += l1; |
| 2177 | } |
| 2178 | else |
| 2179 | err = MEMIF_ERR_NOBUF_DET; |
| 2180 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2181 | md->rx_queues_num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2182 | (c->args.is_master) ? c->run_args.num_s2m_rings : c-> |
| 2183 | run_args.num_m2s_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2184 | |
| 2185 | l1 = sizeof (memif_queue_details_t) * md->rx_queues_num; |
| 2186 | if (l0 + l1 <= buflen) |
| 2187 | { |
Koichiro Den | 33331ed | 2019-01-03 00:22:24 +0900 | [diff] [blame] | 2188 | md->rx_queues = (memif_queue_details_t *) (buf + l0); |
Koichiro Den | be420e5 | 2018-12-15 00:31:34 +0900 | [diff] [blame] | 2189 | for (i = 0; i < md->rx_queues_num; i++) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2190 | { |
| 2191 | md->rx_queues[i].region = c->rx_queues[i].region; |
| 2192 | md->rx_queues[i].qid = i; |
| 2193 | md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size); |
| 2194 | md->rx_queues[i].flags = c->rx_queues[i].ring->flags; |
| 2195 | md->rx_queues[i].head = c->rx_queues[i].ring->head; |
| 2196 | md->rx_queues[i].tail = c->rx_queues[i].ring->tail; |
| 2197 | md->rx_queues[i].buffer_size = c->run_args.buffer_size; |
| 2198 | } |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2199 | l0 += l1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2200 | } |
| 2201 | else |
| 2202 | err = MEMIF_ERR_NOBUF_DET; |
| 2203 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2204 | md->tx_queues_num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2205 | (c->args.is_master) ? c->run_args.num_m2s_rings : c-> |
| 2206 | run_args.num_s2m_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2207 | |
| 2208 | l1 = sizeof (memif_queue_details_t) * md->tx_queues_num; |
| 2209 | if (l0 + l1 <= buflen) |
| 2210 | { |
Koichiro Den | 33331ed | 2019-01-03 00:22:24 +0900 | [diff] [blame] | 2211 | md->tx_queues = (memif_queue_details_t *) (buf + l0); |
Koichiro Den | be420e5 | 2018-12-15 00:31:34 +0900 | [diff] [blame] | 2212 | for (i = 0; i < md->tx_queues_num; i++) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2213 | { |
| 2214 | md->tx_queues[i].region = c->tx_queues[i].region; |
| 2215 | md->tx_queues[i].qid = i; |
| 2216 | md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size); |
| 2217 | md->tx_queues[i].flags = c->tx_queues[i].ring->flags; |
| 2218 | md->tx_queues[i].head = c->tx_queues[i].ring->head; |
| 2219 | md->tx_queues[i].tail = c->tx_queues[i].ring->tail; |
| 2220 | md->tx_queues[i].buffer_size = c->run_args.buffer_size; |
| 2221 | } |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2222 | l0 += l1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2223 | } |
| 2224 | else |
| 2225 | err = MEMIF_ERR_NOBUF_DET; |
| 2226 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2227 | md->link_up_down = (c->fd > 0) ? 1 : 0; |
| 2228 | |
| 2229 | return err; /* 0 */ |
| 2230 | } |
| 2231 | |
| 2232 | int |
| 2233 | memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) |
| 2234 | { |
| 2235 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2236 | uint8_t num; |
| 2237 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2238 | *efd = -1; |
| 2239 | if (c == NULL) |
| 2240 | return MEMIF_ERR_NOCONN; |
| 2241 | if (c->fd < 0) |
| 2242 | return MEMIF_ERR_DISCONNECTED; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2243 | |
| 2244 | num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2245 | (c->args.is_master) ? c->run_args.num_s2m_rings : c-> |
| 2246 | run_args.num_m2s_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2247 | if (qid >= num) |
| 2248 | return MEMIF_ERR_QID; |
| 2249 | |
| 2250 | *efd = c->rx_queues[qid].int_fd; |
| 2251 | |
| 2252 | return MEMIF_ERR_SUCCESS; |
| 2253 | } |
| 2254 | |
| 2255 | int |
| 2256 | memif_cleanup () |
| 2257 | { |
| 2258 | libmemif_main_t *lm = &libmemif_main; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2259 | |
| 2260 | memif_delete_socket ((memif_socket_handle_t *) &lm->default_socket); |
| 2261 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2262 | if (lm->control_list) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2263 | lm->free (lm->control_list); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2264 | lm->control_list = NULL; |
| 2265 | if (lm->interrupt_list) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2266 | lm->free (lm->interrupt_list); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2267 | lm->interrupt_list = NULL; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2268 | if (lm->socket_list) |
| 2269 | lm->free (lm->socket_list); |
| 2270 | lm->socket_list = NULL; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2271 | if (lm->pending_list) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2272 | lm->free (lm->pending_list); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2273 | lm->pending_list = NULL; |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2274 | if (poll_cancel_fd != -1) |
| 2275 | close (poll_cancel_fd); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2276 | |
| 2277 | return MEMIF_ERR_SUCCESS; /* 0 */ |
| 2278 | } |