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