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; |
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).", |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 76 | /* MEMIF_ERR_CONNREFUSED */ |
| 77 | "Connection refused", |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 78 | /* MEMIF_ERR_ACCES */ |
Paul Vinciguerra | 6223766 | 2020-05-29 23:03:06 -0400 | [diff] [blame] | 79 | "Permission to resource denied.", |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 80 | /* MEMIF_ERR_NO_FILE */ |
| 81 | "Socket file does not exist", |
| 82 | /* MEMIF_ERR_FILE_LIMIT */ |
| 83 | "System limit on total numer of open files reached.", |
| 84 | /* MEMIF_ERR_PROC_FILE_LIMIT */ |
| 85 | "Per-process limit on total number of open files reached.", |
| 86 | /* MEMIF_ERR_ALREADY */ |
| 87 | "Connection already requested.", |
| 88 | /* MEMIF_ERR_AGAIN */ |
| 89 | "File descriptor refers to file other than socket, or operation would block.", |
| 90 | /* MEMIF_ERR_BAD_FD */ |
| 91 | "Bad file descriptor.", |
| 92 | /* MEMIF_ERR_NOMEM */ |
| 93 | "Out of memory.", |
| 94 | /* MEMIF_ERR_INVAL_ARG */ |
| 95 | "Invalid argument.", |
| 96 | /* MEMIF_ERR_NOCONN */ |
Paul Vinciguerra | 6223766 | 2020-05-29 23:03:06 -0400 | [diff] [blame] | 97 | "Memif connection handle does not point to existing connection", |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 98 | /* MEMIF_ERR_CONN */ |
| 99 | "Memif connection handle points to existing connection", |
| 100 | /* MEMIF_ERR_CB_FDUPDATE */ |
| 101 | "Callback memif_control_fd_update_t returned error", |
| 102 | /* MEMIF_ERR_FILE_NOT_SOCK */ |
| 103 | "File specified by socket filename exists and is not socket.", |
| 104 | /* MEMIF_ERR_NO_SHMFD */ |
| 105 | "Missing shared memory file descriptor. (internal error)", |
| 106 | /* MEMIF_ERR_COOKIE */ |
| 107 | "Invalid cookie on ring. (internal error)", |
| 108 | /* MEMIF_ERR_NOBUF_RING */ |
| 109 | "Ring buffer full.", |
| 110 | /* MEMIF_ERR_NOBUF */ |
| 111 | "Not enough memif buffers. There are unreceived data in shared memory.", |
| 112 | /* MEMIF_ERR_NOBUF_DET */ |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 113 | "Not enough space for memif details in supplied buffer. String data might be malformed.", |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 114 | /* MEMIF_ERR_INT_WRITE */ |
| 115 | "Send interrupt error.", |
| 116 | /* MEMIF_ERR_MFMSG */ |
| 117 | "Malformed message received on control channel.", |
| 118 | /* MEMIF_ERR_QID */ |
| 119 | "Invalid queue id", |
| 120 | /* MEMIF_ERR_PROTO */ |
| 121 | "Incompatible memory interface protocol version.", |
| 122 | /* MEMIF_ERR_ID */ |
| 123 | "Unmatched interface id.", |
| 124 | /* MEMIF_ERR_ACCSLAVE */ |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 125 | "Slave cannot accept connection request.", |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 126 | /* MEMIF_ERR_ALRCONN */ |
| 127 | "Interface is already connected.", |
| 128 | /* MEMIF_ERR_MODE */ |
| 129 | "Mode mismatch.", |
| 130 | /* MEMIF_ERR_SECRET */ |
| 131 | "Secret mismatch.", |
| 132 | /* MEMIF_ERR_NOSECRET */ |
| 133 | "Secret required.", |
| 134 | /* MEMIF_ERR_MAXREG */ |
| 135 | "Limit on total number of regions reached.", |
| 136 | /* MEMIF_ERR_MAXRING */ |
| 137 | "Limit on total number of ring reached.", |
| 138 | /* MEMIF_ERR_NO_INTFD */ |
| 139 | "Missing interrupt file descriptor. (internal error)", |
| 140 | /* MEMIF_ERR_DISCONNECT */ |
| 141 | "Interface received disconnect request.", |
| 142 | /* MEMIF_ERR_DISCONNECTED */ |
| 143 | "Interface is disconnected.", |
| 144 | /* MEMIF_ERR_UNKNOWN_MSG */ |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 145 | "Unknown message type received on control channel. (internal error)", |
| 146 | /* MEMIF_ERR_POLL_CANCEL */ |
Damjan Marion | 6d56fa4 | 2017-11-03 12:24:37 +0100 | [diff] [blame] | 147 | "Memif event polling was canceled.", |
| 148 | /* MEMIF_ERR_MAX_RING */ |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 149 | "Maximum log2 ring size is 15", |
Jakub Grajciar | ab7c2b0 | 2018-03-28 10:21:05 +0200 | [diff] [blame] | 150 | /* MEMIF_ERR_PRIVHDR */ |
| 151 | "Private headers not supported." |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | #define MEMIF_ERR_UNDEFINED "undefined error" |
| 155 | |
| 156 | char * |
| 157 | memif_strerror (int err_code) |
| 158 | { |
| 159 | if (err_code >= ERRLIST_LEN) |
| 160 | { |
| 161 | strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED)); |
| 162 | memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0'; |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | strncpy (memif_buf, memif_errlist[err_code], |
| 167 | strlen (memif_errlist[err_code])); |
| 168 | memif_buf[strlen (memif_errlist[err_code])] = '\0'; |
| 169 | } |
| 170 | return memif_buf; |
| 171 | } |
| 172 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 173 | uint16_t |
| 174 | memif_get_version () |
| 175 | { |
| 176 | return MEMIF_VERSION; |
| 177 | } |
| 178 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 179 | #define DBG_TX_BUF (0) |
| 180 | #define DBG_RX_BUF (1) |
| 181 | |
| 182 | #ifdef MEMIF_DBG_SHM |
| 183 | static void |
| 184 | print_bytes (void *data, uint16_t len, uint8_t q) |
| 185 | { |
| 186 | if (q == DBG_TX_BUF) |
| 187 | printf ("\nTX:\n\t"); |
| 188 | else |
| 189 | printf ("\nRX:\n\t"); |
| 190 | int i; |
| 191 | for (i = 0; i < len; i++) |
| 192 | { |
| 193 | if (i % 8 == 0) |
| 194 | printf ("\n%d:\t", i); |
| 195 | printf ("%02X ", ((uint8_t *) (data))[i]); |
| 196 | } |
| 197 | printf ("\n\n"); |
| 198 | } |
Jakub Grajciar | ba3c4e8 | 2017-09-18 11:21:40 +0200 | [diff] [blame] | 199 | #endif /* MEMIF_DBG_SHM */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 200 | |
| 201 | int |
| 202 | memif_syscall_error_handler (int err_code) |
| 203 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 204 | DBG ("%s", strerror (err_code)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 205 | |
| 206 | if (err_code == 0) |
| 207 | return MEMIF_ERR_SUCCESS; |
| 208 | if (err_code == EACCES) |
| 209 | return MEMIF_ERR_ACCES; |
| 210 | if (err_code == ENFILE) |
| 211 | return MEMIF_ERR_FILE_LIMIT; |
| 212 | if (err_code == EMFILE) |
| 213 | return MEMIF_ERR_PROC_FILE_LIMIT; |
| 214 | if (err_code == ENOMEM) |
| 215 | return MEMIF_ERR_NOMEM; |
Damjan Marion | 6d56fa4 | 2017-11-03 12:24:37 +0100 | [diff] [blame] | 216 | /* connection refused if master does not exist |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 217 | this error would spam the user until master was created */ |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 218 | /* |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 219 | if (err_code == ECONNREFUSED) |
| 220 | return MEMIF_ERR_SUCCESS; |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 221 | */ |
| 222 | if (err_code == ECONNREFUSED) |
| 223 | return MEMIF_ERR_CONNREFUSED; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 224 | if (err_code == EALREADY) |
| 225 | return MEMIF_ERR_ALREADY; |
| 226 | if (err_code == EAGAIN) |
| 227 | return MEMIF_ERR_AGAIN; |
| 228 | if (err_code == EBADF) |
| 229 | return MEMIF_ERR_BAD_FD; |
| 230 | if (err_code == ENOENT) |
| 231 | return MEMIF_ERR_NO_FILE; |
| 232 | |
| 233 | /* other syscall errors */ |
| 234 | return MEMIF_ERR_SYSCALL; |
| 235 | } |
| 236 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 237 | /* Always valid */ |
| 238 | libmemif_main_t * |
| 239 | get_libmemif_main (memif_socket_t * ms) |
| 240 | { |
| 241 | if (ms != NULL && ms->lm != NULL) |
| 242 | return ms->lm; |
| 243 | return &libmemif_main; |
| 244 | } |
| 245 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 246 | static int |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 247 | memif_add_epoll_fd (libmemif_main_t * lm, int fd, uint32_t events) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 248 | { |
| 249 | if (fd < 0) |
| 250 | { |
| 251 | DBG ("invalid fd %d", fd); |
| 252 | return -1; |
| 253 | } |
| 254 | struct epoll_event evt; |
| 255 | memset (&evt, 0, sizeof (evt)); |
| 256 | evt.events = events; |
| 257 | evt.data.fd = fd; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 258 | if (epoll_ctl (lm->epfd, EPOLL_CTL_ADD, fd, &evt) < 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 259 | { |
| 260 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 261 | return -1; |
| 262 | } |
| 263 | DBG ("fd %d added to epoll", fd); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 268 | memif_mod_epoll_fd (libmemif_main_t * lm, int fd, uint32_t events) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 269 | { |
| 270 | if (fd < 0) |
| 271 | { |
| 272 | DBG ("invalid fd %d", fd); |
| 273 | return -1; |
| 274 | } |
| 275 | struct epoll_event evt; |
| 276 | memset (&evt, 0, sizeof (evt)); |
| 277 | evt.events = events; |
| 278 | evt.data.fd = fd; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 279 | if (epoll_ctl (lm->epfd, EPOLL_CTL_MOD, fd, &evt) < 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 280 | { |
| 281 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 282 | return -1; |
| 283 | } |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 284 | DBG ("fd %d modified on epoll", fd); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 289 | memif_del_epoll_fd (libmemif_main_t * lm, int fd) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 290 | { |
| 291 | if (fd < 0) |
| 292 | { |
| 293 | DBG ("invalid fd %d", fd); |
| 294 | return -1; |
| 295 | } |
| 296 | struct epoll_event evt; |
| 297 | memset (&evt, 0, sizeof (evt)); |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 298 | if (epoll_ctl (lm->epfd, EPOLL_CTL_DEL, fd, &evt) < 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 299 | { |
| 300 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 301 | return -1; |
| 302 | } |
| 303 | DBG ("fd %d removed from epoll", fd); |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | int |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 308 | memif_control_fd_update (int fd, uint8_t events, void *private_ctx) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 309 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 310 | libmemif_main_t *lm; |
| 311 | |
Jakub Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 312 | lm = (private_ctx == NULL) ? &libmemif_main : (libmemif_main_t *) private_ctx; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 313 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 314 | if (events & MEMIF_FD_EVENT_DEL) |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 315 | return memif_del_epoll_fd (lm, fd); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 316 | |
| 317 | uint32_t evt = 0; |
| 318 | if (events & MEMIF_FD_EVENT_READ) |
| 319 | evt |= EPOLLIN; |
| 320 | if (events & MEMIF_FD_EVENT_WRITE) |
| 321 | evt |= EPOLLOUT; |
| 322 | |
| 323 | if (events & MEMIF_FD_EVENT_MOD) |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 324 | return memif_mod_epoll_fd (lm, fd, evt); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 325 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 326 | return memif_add_epoll_fd (lm, fd, evt); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | int |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 330 | add_list_elt (libmemif_main_t * lm, memif_list_elt_t * e, |
| 331 | memif_list_elt_t ** list, uint16_t * len) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 332 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 333 | memif_list_elt_t *tmp; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 334 | int i; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 335 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 336 | for (i = 0; i < *len; i++) |
| 337 | { |
| 338 | if ((*list)[i].data_struct == NULL) |
| 339 | { |
| 340 | (*list)[i].key = e->key; |
| 341 | (*list)[i].data_struct = e->data_struct; |
| 342 | return i; |
| 343 | } |
| 344 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 345 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 346 | tmp = lm->realloc (*list, sizeof (memif_list_elt_t) * *len * 2); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 347 | if (tmp == NULL) |
| 348 | return -1; |
| 349 | |
| 350 | for (i = *len; i < *len * 2; i++) |
| 351 | { |
| 352 | tmp[i].key = -1; |
| 353 | tmp[i].data_struct = NULL; |
| 354 | } |
| 355 | |
| 356 | tmp[*len].key = e->key; |
| 357 | tmp[*len].data_struct = e->data_struct; |
| 358 | i = *len; |
| 359 | *len = *len * 2; |
| 360 | *list = tmp; |
| 361 | |
| 362 | return i; |
| 363 | } |
| 364 | |
| 365 | int |
| 366 | get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list, uint16_t len, |
| 367 | int key) |
| 368 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 369 | int i; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 370 | if (key == -1) |
| 371 | { |
| 372 | *e = NULL; |
| 373 | return -1; |
| 374 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 375 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 376 | for (i = 0; i < len; i++) |
| 377 | { |
| 378 | if (list[i].key == key) |
| 379 | { |
| 380 | *e = &list[i]; |
| 381 | return 0; |
| 382 | } |
| 383 | } |
| 384 | *e = NULL; |
| 385 | return -1; |
| 386 | } |
| 387 | |
| 388 | /* does not free memory, only marks element as free */ |
| 389 | int |
| 390 | free_list_elt (memif_list_elt_t * list, uint16_t len, int key) |
| 391 | { |
| 392 | int i; |
| 393 | for (i = 0; i < len; i++) |
| 394 | { |
| 395 | if (list[i].key == key) |
| 396 | { |
| 397 | list[i].key = -1; |
| 398 | list[i].data_struct = NULL; |
| 399 | return 0; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | int |
| 407 | free_list_elt_ctx (memif_list_elt_t * list, uint16_t len, |
| 408 | memif_connection_t * ctx) |
| 409 | { |
| 410 | int i; |
| 411 | for (i = 0; i < len; i++) |
| 412 | { |
| 413 | if (list[i].key == -1) |
| 414 | { |
| 415 | if (list[i].data_struct == ctx) |
| 416 | { |
| 417 | list[i].data_struct = NULL; |
| 418 | return 0; |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return -1; |
| 424 | } |
| 425 | |
| 426 | static void |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 427 | memif_control_fd_update_register (libmemif_main_t * lm, |
| 428 | memif_control_fd_update_t * cb) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 429 | { |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 430 | lm->control_fd_update = cb; |
| 431 | } |
| 432 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 433 | void |
| 434 | memif_register_external_region (memif_add_external_region_t * ar, |
| 435 | memif_get_external_region_addr_t * gr, |
| 436 | memif_del_external_region_t * dr, |
| 437 | memif_get_external_buffer_offset_t * go) |
| 438 | { |
| 439 | libmemif_main_t *lm = &libmemif_main; |
| 440 | lm->add_external_region = ar; |
| 441 | lm->get_external_region_addr = gr; |
| 442 | lm->del_external_region = dr; |
| 443 | lm->get_external_buffer_offset = go; |
| 444 | } |
| 445 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 446 | static void |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 447 | memif_alloc_register (libmemif_main_t * lm, memif_alloc_t * ma) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 448 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 449 | lm->alloc = ma; |
| 450 | } |
| 451 | |
| 452 | static void |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 453 | memif_realloc_register (libmemif_main_t * lm, memif_realloc_t * mr) |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 454 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 455 | lm->realloc = mr; |
| 456 | } |
| 457 | |
| 458 | static void |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 459 | memif_free_register (libmemif_main_t * lm, memif_free_t * mf) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 460 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 461 | lm->free = mf; |
| 462 | } |
| 463 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 464 | int |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 465 | memif_set_connection_request_timer (struct itimerspec timer) |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 466 | { |
| 467 | libmemif_main_t *lm = &libmemif_main; |
| 468 | int err = MEMIF_ERR_SUCCESS; |
| 469 | |
| 470 | lm->arm = timer; |
| 471 | |
| 472 | /* overwrite timer, if already armed */ |
| 473 | if (lm->disconn_slaves != 0) |
| 474 | { |
| 475 | if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) |
| 476 | { |
| 477 | err = memif_syscall_error_handler (errno); |
| 478 | } |
| 479 | } |
| 480 | return err; |
| 481 | } |
| 482 | |
| 483 | int |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 484 | memif_per_thread_set_connection_request_timer (memif_per_thread_main_handle_t |
| 485 | pt_main, |
| 486 | struct itimerspec timer) |
| 487 | { |
| 488 | libmemif_main_t *lm = (libmemif_main_t *) pt_main; |
| 489 | int err = MEMIF_ERR_SUCCESS; |
| 490 | |
| 491 | lm->arm = timer; |
| 492 | |
| 493 | /* overwrite timer, if already armed */ |
| 494 | if (lm->disconn_slaves != 0) |
| 495 | { |
| 496 | if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) |
| 497 | { |
| 498 | err = memif_syscall_error_handler (errno); |
| 499 | } |
| 500 | } |
| 501 | return err; |
| 502 | } |
| 503 | |
| 504 | int |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 505 | 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] | 506 | memif_alloc_t * memif_alloc, memif_realloc_t * memif_realloc, |
| 507 | memif_free_t * memif_free) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 508 | { |
| 509 | int err = MEMIF_ERR_SUCCESS; /* 0 */ |
| 510 | libmemif_main_t *lm = &libmemif_main; |
Jakub Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 511 | memset (lm, 0, sizeof (libmemif_main_t)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 512 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 513 | /* register custom memory management */ |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 514 | if (memif_alloc != NULL) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 515 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 516 | memif_alloc_register (lm, memif_alloc); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 517 | } |
| 518 | else |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 519 | memif_alloc_register (lm, malloc); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 520 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 521 | if (memif_realloc != NULL) |
| 522 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 523 | memif_realloc_register (lm, memif_realloc); |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 524 | } |
| 525 | else |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 526 | memif_realloc_register (lm, realloc); |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 527 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 528 | if (memif_free != NULL) |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 529 | memif_free_register (lm, memif_free); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 530 | else |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 531 | memif_free_register (lm, free); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 532 | |
| 533 | if (app_name != NULL) |
| 534 | { |
| 535 | uint8_t len = (strlen (app_name) > MEMIF_NAME_LEN) |
| 536 | ? strlen (app_name) : MEMIF_NAME_LEN; |
| 537 | strncpy ((char *) lm->app_name, app_name, len); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 538 | } |
| 539 | else |
| 540 | { |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 541 | strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME, |
| 542 | strlen (MEMIF_DEFAULT_APP_NAME)); |
| 543 | } |
| 544 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 545 | lm->poll_cancel_fd = -1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 546 | /* register control fd update callback */ |
| 547 | if (on_control_fd_update != NULL) |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 548 | memif_control_fd_update_register (lm, on_control_fd_update); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 549 | else |
| 550 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 551 | lm->epfd = epoll_create (1); |
| 552 | memif_control_fd_update_register (lm, memif_control_fd_update); |
| 553 | if ((lm->poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0) |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 554 | { |
| 555 | err = errno; |
| 556 | DBG ("eventfd: %s", strerror (err)); |
| 557 | return memif_syscall_error_handler (err); |
| 558 | } |
Jakub Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 559 | lm->control_fd_update (lm->poll_cancel_fd, MEMIF_FD_EVENT_READ, lm->private_ctx); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 560 | DBG ("libmemif event polling initialized"); |
| 561 | } |
| 562 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 563 | lm->control_list_len = 2; |
| 564 | lm->interrupt_list_len = 2; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 565 | lm->socket_list_len = 1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 566 | lm->pending_list_len = 1; |
| 567 | |
| 568 | lm->control_list = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 569 | lm->alloc (sizeof (memif_list_elt_t) * lm->control_list_len); |
| 570 | if (lm->control_list == NULL) |
| 571 | { |
| 572 | err = MEMIF_ERR_NOMEM; |
| 573 | goto error; |
| 574 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 575 | lm->interrupt_list = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 576 | lm->alloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len); |
| 577 | if (lm->interrupt_list == NULL) |
| 578 | { |
| 579 | err = MEMIF_ERR_NOMEM; |
| 580 | goto error; |
| 581 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 582 | lm->socket_list = |
| 583 | lm->alloc (sizeof (memif_list_elt_t) * lm->socket_list_len); |
| 584 | if (lm->socket_list == NULL) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 585 | { |
| 586 | err = MEMIF_ERR_NOMEM; |
| 587 | goto error; |
| 588 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 589 | lm->pending_list = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 590 | lm->alloc (sizeof (memif_list_elt_t) * lm->pending_list_len); |
| 591 | if (lm->pending_list == NULL) |
| 592 | { |
| 593 | err = MEMIF_ERR_NOMEM; |
| 594 | goto error; |
| 595 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 596 | |
| 597 | int i; |
| 598 | for (i = 0; i < lm->control_list_len; i++) |
| 599 | { |
| 600 | lm->control_list[i].key = -1; |
| 601 | lm->control_list[i].data_struct = NULL; |
| 602 | } |
| 603 | for (i = 0; i < lm->interrupt_list_len; i++) |
| 604 | { |
| 605 | lm->interrupt_list[i].key = -1; |
| 606 | lm->interrupt_list[i].data_struct = NULL; |
| 607 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 608 | for (i = 0; i < lm->socket_list_len; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 609 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 610 | lm->socket_list[i].key = -1; |
| 611 | lm->socket_list[i].data_struct = NULL; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 612 | } |
| 613 | for (i = 0; i < lm->pending_list_len; i++) |
| 614 | { |
| 615 | lm->pending_list[i].key = -1; |
| 616 | lm->pending_list[i].data_struct = NULL; |
| 617 | } |
| 618 | |
| 619 | lm->disconn_slaves = 0; |
| 620 | |
| 621 | lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK); |
| 622 | if (lm->timerfd < 0) |
| 623 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 624 | err = memif_syscall_error_handler (errno); |
| 625 | goto error; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 626 | } |
| 627 | |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 628 | lm->arm.it_value.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC; |
| 629 | lm->arm.it_value.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC; |
| 630 | lm->arm.it_interval.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC; |
| 631 | lm->arm.it_interval.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 632 | |
Jakub Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 633 | if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ, lm->private_ctx) < 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 634 | { |
| 635 | DBG ("callback type memif_control_fd_update_t error!"); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 636 | err = MEMIF_ERR_CB_FDUPDATE; |
| 637 | goto error; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 638 | } |
| 639 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 640 | /* Create default socket */ |
| 641 | err = memif_create_socket ((memif_socket_handle_t *) & |
| 642 | lm->default_socket, |
| 643 | MEMIF_DEFAULT_SOCKET_PATH, NULL); |
| 644 | if (err != MEMIF_ERR_SUCCESS) |
| 645 | goto error; |
| 646 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 647 | return err; |
| 648 | |
| 649 | error: |
| 650 | memif_cleanup (); |
| 651 | return err; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 652 | } |
| 653 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 654 | int |
| 655 | memif_per_thread_init (memif_per_thread_main_handle_t * pt_main, |
| 656 | void *private_ctx, |
| 657 | memif_control_fd_update_t * on_control_fd_update, |
| 658 | char *app_name, memif_alloc_t * memif_alloc, |
| 659 | memif_realloc_t * memif_realloc, |
| 660 | memif_free_t * memif_free) |
| 661 | { |
| 662 | memif_err_t err = MEMIF_ERR_SUCCESS; |
| 663 | int i; |
| 664 | libmemif_main_t *lm; |
| 665 | |
| 666 | /* Allocate unique libmemif main */ |
| 667 | if (memif_alloc != NULL) |
| 668 | lm = memif_alloc (sizeof (libmemif_main_t)); |
| 669 | else |
| 670 | lm = malloc (sizeof (libmemif_main_t)); |
| 671 | |
| 672 | if (lm == NULL) |
| 673 | return MEMIF_ERR_NOMEM; |
| 674 | |
| 675 | memset (lm, 0, sizeof (libmemif_main_t)); |
| 676 | |
| 677 | /* register custom memory management */ |
| 678 | if (memif_alloc != NULL) |
| 679 | { |
| 680 | memif_alloc_register (lm, memif_alloc); |
| 681 | } |
| 682 | else |
| 683 | memif_alloc_register (lm, malloc); |
| 684 | |
| 685 | if (memif_realloc != NULL) |
| 686 | { |
| 687 | memif_realloc_register (lm, memif_realloc); |
| 688 | } |
| 689 | else |
| 690 | memif_realloc_register (lm, realloc); |
| 691 | |
| 692 | if (memif_free != NULL) |
| 693 | memif_free_register (lm, memif_free); |
| 694 | else |
| 695 | memif_free_register (lm, free); |
| 696 | |
| 697 | lm->private_ctx = private_ctx; |
| 698 | |
| 699 | /* set app name */ |
| 700 | if (app_name != NULL) |
| 701 | { |
| 702 | uint8_t len = (strlen (app_name) > MEMIF_NAME_LEN) |
| 703 | ? strlen (app_name) : MEMIF_NAME_LEN; |
| 704 | strncpy ((char *) lm->app_name, app_name, len); |
| 705 | } |
| 706 | else |
| 707 | { |
| 708 | strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME, |
| 709 | strlen (MEMIF_DEFAULT_APP_NAME)); |
| 710 | } |
| 711 | |
| 712 | lm->poll_cancel_fd = -1; |
| 713 | /* register control fd update callback */ |
| 714 | if (on_control_fd_update != NULL) |
| 715 | memif_control_fd_update_register (lm, on_control_fd_update); |
| 716 | else |
| 717 | { |
Jakub Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 718 | /* private_ctx only used internally by memif_control_fd_update |
| 719 | * pointer to this libmemif main |
| 720 | */ |
| 721 | lm->private_ctx = lm; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 722 | lm->epfd = epoll_create (1); |
| 723 | memif_control_fd_update_register (lm, memif_control_fd_update); |
| 724 | if ((lm->poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0) |
| 725 | { |
| 726 | err = errno; |
| 727 | DBG ("eventfd: %s", strerror (err)); |
| 728 | return memif_syscall_error_handler (err); |
| 729 | } |
| 730 | lm->control_fd_update (lm->poll_cancel_fd, MEMIF_FD_EVENT_READ, |
| 731 | lm->private_ctx); |
| 732 | DBG ("libmemif event polling initialized"); |
| 733 | } |
| 734 | |
| 735 | /* Initialize lists */ |
| 736 | lm->control_list_len = 2; |
| 737 | lm->interrupt_list_len = 2; |
| 738 | lm->socket_list_len = 1; |
| 739 | lm->pending_list_len = 1; |
| 740 | |
| 741 | lm->control_list = |
| 742 | lm->alloc (sizeof (memif_list_elt_t) * lm->control_list_len); |
| 743 | if (lm->control_list == NULL) |
| 744 | { |
| 745 | err = MEMIF_ERR_NOMEM; |
| 746 | goto error; |
| 747 | } |
| 748 | lm->interrupt_list = |
| 749 | lm->alloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len); |
| 750 | if (lm->interrupt_list == NULL) |
| 751 | { |
| 752 | err = MEMIF_ERR_NOMEM; |
| 753 | goto error; |
| 754 | } |
| 755 | lm->socket_list = |
| 756 | lm->alloc (sizeof (memif_list_elt_t) * lm->socket_list_len); |
| 757 | if (lm->socket_list == NULL) |
| 758 | { |
| 759 | err = MEMIF_ERR_NOMEM; |
| 760 | goto error; |
| 761 | } |
| 762 | lm->pending_list = |
| 763 | lm->alloc (sizeof (memif_list_elt_t) * lm->pending_list_len); |
| 764 | if (lm->pending_list == NULL) |
| 765 | { |
| 766 | err = MEMIF_ERR_NOMEM; |
| 767 | goto error; |
| 768 | } |
| 769 | |
| 770 | for (i = 0; i < lm->control_list_len; i++) |
| 771 | { |
| 772 | lm->control_list[i].key = -1; |
| 773 | lm->control_list[i].data_struct = NULL; |
| 774 | } |
| 775 | for (i = 0; i < lm->interrupt_list_len; i++) |
| 776 | { |
| 777 | lm->interrupt_list[i].key = -1; |
| 778 | lm->interrupt_list[i].data_struct = NULL; |
| 779 | } |
| 780 | for (i = 0; i < lm->socket_list_len; i++) |
| 781 | { |
| 782 | lm->socket_list[i].key = -1; |
| 783 | lm->socket_list[i].data_struct = NULL; |
| 784 | } |
| 785 | for (i = 0; i < lm->pending_list_len; i++) |
| 786 | { |
| 787 | lm->pending_list[i].key = -1; |
| 788 | lm->pending_list[i].data_struct = NULL; |
| 789 | } |
| 790 | |
| 791 | /* Initialize autoconnect */ |
| 792 | lm->disconn_slaves = 0; |
| 793 | |
| 794 | lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK); |
| 795 | if (lm->timerfd < 0) |
| 796 | { |
| 797 | err = memif_syscall_error_handler (errno); |
| 798 | goto error; |
| 799 | } |
| 800 | |
| 801 | lm->arm.it_value.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC; |
| 802 | lm->arm.it_value.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC; |
| 803 | lm->arm.it_interval.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC; |
| 804 | lm->arm.it_interval.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC; |
| 805 | |
| 806 | if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ, |
| 807 | lm->private_ctx) < 0) |
| 808 | { |
| 809 | DBG ("callback type memif_control_fd_update_t error!"); |
| 810 | err = MEMIF_ERR_CB_FDUPDATE; |
| 811 | goto error; |
| 812 | } |
| 813 | |
| 814 | *pt_main = lm; |
| 815 | |
| 816 | return err; |
| 817 | |
| 818 | error: |
| 819 | *pt_main = lm; |
| 820 | memif_per_thread_cleanup (pt_main); |
| 821 | return err; |
| 822 | } |
| 823 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 824 | static inline memif_ring_t * |
| 825 | memif_get_ring (memif_connection_t * conn, memif_ring_type_t type, |
| 826 | uint16_t ring_num) |
| 827 | { |
| 828 | if (&conn->regions[0] == NULL) |
| 829 | return NULL; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 830 | void *p = conn->regions[0].addr; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 831 | int ring_size = |
| 832 | sizeof (memif_ring_t) + |
| 833 | sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size); |
| 834 | p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size; |
| 835 | |
| 836 | return (memif_ring_t *) p; |
| 837 | } |
| 838 | |
| 839 | int |
| 840 | memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode, |
| 841 | uint16_t qid) |
| 842 | { |
| 843 | memif_connection_t *conn = (memif_connection_t *) c; |
| 844 | if (conn == NULL) |
| 845 | return MEMIF_ERR_NOCONN; |
| 846 | uint8_t num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 847 | (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn-> |
| 848 | run_args.num_m2s_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 849 | if (qid >= num) |
| 850 | return MEMIF_ERR_QID; |
| 851 | |
| 852 | conn->rx_queues[qid].ring->flags = rx_mode; |
| 853 | DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags); |
| 854 | return MEMIF_ERR_SUCCESS; |
| 855 | } |
| 856 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 857 | static int |
| 858 | memif_socket_start_listening (memif_socket_t * ms) |
| 859 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 860 | libmemif_main_t *lm = get_libmemif_main (ms); |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 861 | memif_list_elt_t elt; |
| 862 | struct stat file_stat; |
| 863 | struct sockaddr_un un = { 0 }; |
| 864 | int on = 1; |
| 865 | int err = MEMIF_ERR_SUCCESS; |
| 866 | |
| 867 | if (ms->type == MEMIF_SOCKET_TYPE_CLIENT) |
| 868 | return MEMIF_ERR_INVAL_ARG; |
| 869 | |
| 870 | /* check if file exists */ |
| 871 | if (stat ((char *) ms->filename, &file_stat) == 0) |
| 872 | { |
| 873 | if (S_ISSOCK (file_stat.st_mode)) |
| 874 | unlink ((char *) ms->filename); |
| 875 | else |
| 876 | return memif_syscall_error_handler (errno); |
| 877 | } |
| 878 | |
| 879 | ms->fd = socket (AF_UNIX, SOCK_SEQPACKET, 0); |
| 880 | if (ms->fd < 0) |
| 881 | { |
| 882 | err = memif_syscall_error_handler (errno); |
| 883 | goto error; |
| 884 | } |
| 885 | |
| 886 | DBG ("socket %d created", ms->fd); |
| 887 | un.sun_family = AF_UNIX; |
| 888 | strncpy ((char *) un.sun_path, (char *) ms->filename, |
| 889 | sizeof (un.sun_path) - 1); |
| 890 | if (setsockopt (ms->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0) |
| 891 | { |
| 892 | err = memif_syscall_error_handler (errno); |
| 893 | goto error; |
| 894 | } |
| 895 | if (bind (ms->fd, (struct sockaddr *) &un, sizeof (un)) < 0) |
| 896 | { |
| 897 | err = memif_syscall_error_handler (errno); |
| 898 | goto error; |
| 899 | } |
| 900 | if (listen (ms->fd, 1) < 0) |
| 901 | { |
| 902 | err = memif_syscall_error_handler (errno); |
| 903 | goto error; |
| 904 | } |
| 905 | if (stat ((char *) ms->filename, &file_stat) < 0) |
| 906 | { |
| 907 | err = memif_syscall_error_handler (errno); |
| 908 | goto error; |
| 909 | } |
| 910 | |
| 911 | /* add socket to libmemif main */ |
| 912 | elt.key = ms->fd; |
| 913 | elt.data_struct = ms; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 914 | add_list_elt (lm, &elt, &lm->socket_list, &lm->socket_list_len); |
Jakub Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 915 | /* if lm->private_ctx == lm event polling is done by libmemif */ |
| 916 | lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ, lm->private_ctx); |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 917 | |
| 918 | ms->type = MEMIF_SOCKET_TYPE_LISTENER; |
| 919 | |
| 920 | return err; |
| 921 | |
| 922 | error: |
| 923 | if (ms->fd > 0) |
| 924 | { |
| 925 | close (ms->fd); |
| 926 | ms->fd = -1; |
| 927 | } |
| 928 | return err; |
| 929 | } |
| 930 | |
| 931 | int |
| 932 | memif_create_socket (memif_socket_handle_t * sock, const char *filename, |
| 933 | void *private_ctx) |
| 934 | { |
| 935 | libmemif_main_t *lm = &libmemif_main; |
| 936 | memif_socket_t *ms = (memif_socket_t *) * sock; |
| 937 | int i, err = MEMIF_ERR_SUCCESS; |
| 938 | |
| 939 | for (i = 0; i < lm->socket_list_len; i++) |
| 940 | { |
| 941 | if ((ms = (memif_socket_t *) lm->socket_list[i].data_struct) != NULL) |
| 942 | { |
| 943 | if (strncmp ((char *) ms->filename, filename, |
| 944 | strlen ((char *) ms->filename)) == 0) |
| 945 | return MEMIF_ERR_INVAL_ARG; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | /* allocate memif_socket_t */ |
| 950 | ms = NULL; |
| 951 | ms = lm->alloc (sizeof (memif_socket_t)); |
| 952 | if (ms == NULL) |
| 953 | { |
| 954 | err = MEMIF_ERR_NOMEM; |
| 955 | goto error; |
| 956 | } |
| 957 | memset (ms, 0, sizeof (memif_socket_t)); |
| 958 | /* set filename */ |
| 959 | ms->filename = lm->alloc (strlen (filename) + sizeof (char)); |
| 960 | if (ms->filename == NULL) |
| 961 | { |
| 962 | err = MEMIF_ERR_NOMEM; |
| 963 | goto error; |
| 964 | } |
| 965 | memset (ms->filename, 0, strlen (filename) + sizeof (char)); |
| 966 | strncpy ((char *) ms->filename, filename, strlen (filename)); |
| 967 | |
| 968 | ms->type = MEMIF_SOCKET_TYPE_NONE; |
| 969 | |
| 970 | ms->interface_list_len = 1; |
| 971 | ms->interface_list = |
| 972 | lm->alloc (sizeof (memif_list_elt_t) * ms->interface_list_len); |
| 973 | if (ms->interface_list == NULL) |
| 974 | { |
| 975 | err = MEMIF_ERR_NOMEM; |
| 976 | goto error; |
| 977 | } |
| 978 | ms->interface_list[0].key = -1; |
| 979 | ms->interface_list[0].data_struct = NULL; |
| 980 | |
| 981 | *sock = ms; |
| 982 | |
| 983 | return err; |
| 984 | |
| 985 | error: |
| 986 | if (ms != NULL) |
| 987 | { |
| 988 | if (ms->filename != NULL) |
| 989 | { |
| 990 | lm->free (ms->filename); |
| 991 | ms->filename = NULL; |
| 992 | } |
| 993 | if (ms->fd > 0) |
| 994 | { |
| 995 | close (ms->fd); |
| 996 | ms->fd = -1; |
| 997 | } |
| 998 | if (ms->interface_list != NULL) |
| 999 | { |
| 1000 | lm->free (ms->interface_list); |
| 1001 | ms->interface_list = NULL; |
| 1002 | ms->interface_list_len = 0; |
| 1003 | } |
| 1004 | lm->free (ms); |
| 1005 | *sock = ms = NULL; |
| 1006 | } |
| 1007 | return err; |
| 1008 | } |
| 1009 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1010 | int |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1011 | memif_per_thread_create_socket (memif_per_thread_main_handle_t pt_main, |
| 1012 | memif_socket_handle_t * sock, |
| 1013 | const char *filename, void *private_ctx) |
| 1014 | { |
| 1015 | libmemif_main_t *lm = (libmemif_main_t *) pt_main; |
| 1016 | memif_socket_t *ms = (memif_socket_t *) * sock; |
| 1017 | int i, err = MEMIF_ERR_SUCCESS; |
| 1018 | |
| 1019 | if (lm == NULL) |
| 1020 | return MEMIF_ERR_INVAL_ARG; |
| 1021 | |
| 1022 | for (i = 0; i < lm->socket_list_len; i++) |
| 1023 | { |
| 1024 | if ((ms = (memif_socket_t *) lm->socket_list[i].data_struct) != NULL) |
| 1025 | { |
| 1026 | if (strncmp ((char *) ms->filename, filename, |
| 1027 | strlen ((char *) ms->filename)) == 0) |
| 1028 | return MEMIF_ERR_INVAL_ARG; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | /* allocate memif_socket_t */ |
| 1033 | ms = NULL; |
| 1034 | ms = lm->alloc (sizeof (memif_socket_t)); |
| 1035 | if (ms == NULL) |
| 1036 | { |
| 1037 | err = MEMIF_ERR_NOMEM; |
| 1038 | goto error; |
| 1039 | } |
| 1040 | memset (ms, 0, sizeof (memif_socket_t)); |
| 1041 | ms->lm = lm; |
| 1042 | /* set filename */ |
| 1043 | ms->filename = lm->alloc (strlen (filename) + sizeof (char)); |
| 1044 | if (ms->filename == NULL) |
| 1045 | { |
| 1046 | err = MEMIF_ERR_NOMEM; |
| 1047 | goto error; |
| 1048 | } |
| 1049 | memset (ms->filename, 0, strlen (filename) + sizeof (char)); |
| 1050 | strncpy ((char *) ms->filename, filename, strlen (filename)); |
| 1051 | |
| 1052 | ms->type = MEMIF_SOCKET_TYPE_NONE; |
| 1053 | |
| 1054 | ms->interface_list_len = 1; |
| 1055 | ms->interface_list = |
| 1056 | lm->alloc (sizeof (memif_list_elt_t) * ms->interface_list_len); |
| 1057 | if (ms->interface_list == NULL) |
| 1058 | { |
| 1059 | err = MEMIF_ERR_NOMEM; |
| 1060 | goto error; |
| 1061 | } |
| 1062 | ms->interface_list[0].key = -1; |
| 1063 | ms->interface_list[0].data_struct = NULL; |
| 1064 | |
| 1065 | *sock = ms; |
| 1066 | |
| 1067 | return err; |
| 1068 | |
| 1069 | error: |
| 1070 | if (ms != NULL) |
| 1071 | { |
| 1072 | if (ms->filename != NULL) |
| 1073 | { |
| 1074 | lm->free (ms->filename); |
| 1075 | ms->filename = NULL; |
| 1076 | } |
| 1077 | if (ms->fd > 0) |
| 1078 | { |
| 1079 | close (ms->fd); |
| 1080 | ms->fd = -1; |
| 1081 | } |
| 1082 | if (ms->interface_list != NULL) |
| 1083 | { |
| 1084 | lm->free (ms->interface_list); |
| 1085 | ms->interface_list = NULL; |
| 1086 | ms->interface_list_len = 0; |
| 1087 | } |
| 1088 | lm->free (ms); |
| 1089 | *sock = ms = NULL; |
| 1090 | } |
| 1091 | return err; |
| 1092 | } |
| 1093 | |
| 1094 | int |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1095 | memif_create (memif_conn_handle_t * c, memif_conn_args_t * args, |
| 1096 | memif_connection_update_t * on_connect, |
| 1097 | memif_connection_update_t * on_disconnect, |
| 1098 | memif_interrupt_t * on_interrupt, void *private_ctx) |
| 1099 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1100 | libmemif_main_t *lm = get_libmemif_main (args->socket); |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1101 | int err, index = 0; |
| 1102 | memif_list_elt_t elt; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1103 | memif_connection_t *conn = (memif_connection_t *) * c; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1104 | memif_socket_t *ms; |
| 1105 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1106 | if (conn != NULL) |
| 1107 | { |
| 1108 | DBG ("This handle already points to existing memif."); |
| 1109 | return MEMIF_ERR_CONN; |
| 1110 | } |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1111 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1112 | conn = (memif_connection_t *) lm->alloc (sizeof (memif_connection_t)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1113 | if (conn == NULL) |
| 1114 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1115 | err = MEMIF_ERR_NOMEM; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1116 | goto error; |
| 1117 | } |
| 1118 | memset (conn, 0, sizeof (memif_connection_t)); |
| 1119 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1120 | conn->args.interface_id = args->interface_id; |
| 1121 | |
| 1122 | if (args->log2_ring_size == 0) |
| 1123 | args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE; |
Damjan Marion | 6d56fa4 | 2017-11-03 12:24:37 +0100 | [diff] [blame] | 1124 | else if (args->log2_ring_size > MEMIF_MAX_LOG2_RING_SIZE) |
| 1125 | { |
| 1126 | err = MEMIF_ERR_MAX_RING; |
| 1127 | goto error; |
| 1128 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1129 | if (args->buffer_size == 0) |
| 1130 | args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE; |
| 1131 | if (args->num_s2m_rings == 0) |
| 1132 | args->num_s2m_rings = MEMIF_DEFAULT_TX_QUEUES; |
| 1133 | if (args->num_m2s_rings == 0) |
| 1134 | args->num_m2s_rings = MEMIF_DEFAULT_RX_QUEUES; |
| 1135 | |
| 1136 | conn->args.num_s2m_rings = args->num_s2m_rings; |
| 1137 | conn->args.num_m2s_rings = args->num_m2s_rings; |
| 1138 | conn->args.buffer_size = args->buffer_size; |
| 1139 | conn->args.log2_ring_size = args->log2_ring_size; |
| 1140 | conn->args.is_master = args->is_master; |
| 1141 | conn->args.mode = args->mode; |
| 1142 | conn->msg_queue = NULL; |
| 1143 | conn->regions = NULL; |
| 1144 | conn->tx_queues = NULL; |
| 1145 | conn->rx_queues = NULL; |
| 1146 | conn->fd = -1; |
| 1147 | conn->on_connect = on_connect; |
| 1148 | conn->on_disconnect = on_disconnect; |
| 1149 | conn->on_interrupt = on_interrupt; |
| 1150 | conn->private_ctx = private_ctx; |
| 1151 | memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t)); |
| 1152 | |
| 1153 | uint8_t l = strlen ((char *) args->interface_name); |
| 1154 | strncpy ((char *) conn->args.interface_name, (char *) args->interface_name, |
| 1155 | l); |
| 1156 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1157 | if ((l = strlen ((char *) args->secret)) > 0) |
| 1158 | strncpy ((char *) conn->args.secret, (char *) args->secret, l); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1159 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1160 | if (args->socket != NULL) |
| 1161 | conn->args.socket = args->socket; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1162 | else if (lm->default_socket != NULL) |
| 1163 | conn->args.socket = lm->default_socket; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1164 | else |
| 1165 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1166 | err = MEMIF_ERR_INVAL_ARG; |
| 1167 | goto error; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1168 | } |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1169 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1170 | ms = (memif_socket_t *) conn->args.socket; |
| 1171 | |
| 1172 | if ((conn->args.is_master && ms->type == MEMIF_SOCKET_TYPE_CLIENT) || |
| 1173 | (!conn->args.is_master && ms->type == MEMIF_SOCKET_TYPE_LISTENER)) |
| 1174 | { |
| 1175 | err = MEMIF_ERR_INVAL_ARG; |
| 1176 | goto error; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1177 | } |
| 1178 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1179 | elt.key = conn->args.interface_id; |
| 1180 | elt.data_struct = conn; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1181 | add_list_elt (lm, &elt, &ms->interface_list, &ms->interface_list_len); |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1182 | ms->use_count++; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1183 | |
| 1184 | if (conn->args.is_master) |
| 1185 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1186 | if (ms->type == MEMIF_SOCKET_TYPE_NONE) |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1187 | { |
| 1188 | err = memif_socket_start_listening (ms); |
| 1189 | if (err != MEMIF_ERR_SUCCESS) |
| 1190 | goto error; |
| 1191 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1192 | } |
| 1193 | else |
| 1194 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1195 | elt.key = -1; |
| 1196 | elt.data_struct = conn; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1197 | if ((index = |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1198 | add_list_elt (lm, &elt, &lm->control_list, |
| 1199 | &lm->control_list_len)) < 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1200 | { |
| 1201 | err = MEMIF_ERR_NOMEM; |
| 1202 | goto error; |
| 1203 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1204 | |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1205 | conn->index = index; |
| 1206 | |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 1207 | /* try connecting to master */ |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1208 | err = memif_request_connection (conn); |
Jakub Grajciar | 272e2e4 | 2019-07-24 10:33:11 +0200 | [diff] [blame] | 1209 | if ((err != MEMIF_ERR_SUCCESS) && (lm->disconn_slaves == 0)) |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1210 | { |
| 1211 | /* connection failed, arm reconnect timer (if not armed) */ |
| 1212 | if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) |
| 1213 | { |
| 1214 | err = memif_syscall_error_handler (errno); |
| 1215 | goto error; |
| 1216 | } |
| 1217 | } |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1218 | lm->disconn_slaves++; |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1219 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1220 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1221 | *c = conn; |
| 1222 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1223 | return 0; |
| 1224 | |
| 1225 | error: |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1226 | if (conn != NULL) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1227 | lm->free (conn); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1228 | *c = conn = NULL; |
| 1229 | return err; |
| 1230 | } |
| 1231 | |
| 1232 | int |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1233 | memif_request_connection (memif_conn_handle_t c) |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1234 | { |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1235 | memif_connection_t *conn = (memif_connection_t *) c; |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 1236 | libmemif_main_t *lm; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1237 | memif_socket_t *ms; |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1238 | int err = MEMIF_ERR_SUCCESS; |
| 1239 | int sockfd = -1; |
| 1240 | struct sockaddr_un sun; |
| 1241 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1242 | if (conn == NULL) |
| 1243 | return MEMIF_ERR_NOCONN; |
| 1244 | |
| 1245 | ms = (memif_socket_t *) conn->args.socket; |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 1246 | lm = get_libmemif_main (ms); |
| 1247 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1248 | |
| 1249 | if (conn->args.is_master || ms->type == MEMIF_SOCKET_TYPE_LISTENER) |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1250 | return MEMIF_ERR_INVAL_ARG; |
| 1251 | if (conn->fd > 0) |
| 1252 | return MEMIF_ERR_ALRCONN; |
| 1253 | |
| 1254 | sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); |
| 1255 | if (sockfd < 0) |
| 1256 | { |
| 1257 | err = memif_syscall_error_handler (errno); |
| 1258 | goto error; |
| 1259 | } |
| 1260 | |
| 1261 | sun.sun_family = AF_UNIX; |
| 1262 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1263 | strncpy (sun.sun_path, (char *) ms->filename, sizeof (sun.sun_path) - 1); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1264 | |
| 1265 | if (connect (sockfd, (struct sockaddr *) &sun, |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1266 | sizeof (struct sockaddr_un)) == 0) |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1267 | { |
| 1268 | conn->fd = sockfd; |
| 1269 | conn->read_fn = memif_conn_fd_read_ready; |
| 1270 | conn->write_fn = memif_conn_fd_write_ready; |
| 1271 | conn->error_fn = memif_conn_fd_error; |
| 1272 | |
| 1273 | lm->control_list[conn->index].key = conn->fd; |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1274 | lm->control_fd_update (sockfd, |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1275 | MEMIF_FD_EVENT_READ | |
Jakub Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 1276 | MEMIF_FD_EVENT_WRITE, lm->private_ctx); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1277 | |
| 1278 | lm->disconn_slaves--; |
| 1279 | if (lm->disconn_slaves == 0) |
| 1280 | { |
| 1281 | if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) |
| 1282 | { |
| 1283 | err = memif_syscall_error_handler (errno); |
| 1284 | return err; |
| 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | else |
| 1289 | { |
Jakub Grajciar | 272e2e4 | 2019-07-24 10:33:11 +0200 | [diff] [blame] | 1290 | err = memif_syscall_error_handler (errno); |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1291 | strcpy ((char *) conn->remote_disconnect_string, memif_strerror (err)); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1292 | goto error; |
| 1293 | } |
| 1294 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1295 | ms->type = MEMIF_SOCKET_TYPE_CLIENT; |
| 1296 | |
| 1297 | return err; |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1298 | |
| 1299 | error: |
| 1300 | if (sockfd > 0) |
| 1301 | close (sockfd); |
| 1302 | sockfd = -1; |
| 1303 | return err; |
| 1304 | } |
| 1305 | |
| 1306 | int |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1307 | memif_control_fd_handler (int fd, uint8_t events) |
| 1308 | { |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1309 | int i, err = MEMIF_ERR_SUCCESS; /* 0 */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1310 | uint16_t num; |
| 1311 | memif_list_elt_t *e = NULL; |
| 1312 | memif_connection_t *conn; |
| 1313 | libmemif_main_t *lm = &libmemif_main; |
| 1314 | if (fd == lm->timerfd) |
| 1315 | { |
| 1316 | uint64_t b; |
| 1317 | ssize_t size; |
| 1318 | size = read (fd, &b, sizeof (b)); |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 1319 | |
| 1320 | if (size == -1) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1321 | goto error; |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 1322 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1323 | for (i = 0; i < lm->control_list_len; i++) |
| 1324 | { |
| 1325 | if ((lm->control_list[i].key < 0) |
| 1326 | && (lm->control_list[i].data_struct != NULL)) |
| 1327 | { |
| 1328 | conn = lm->control_list[i].data_struct; |
| 1329 | if (conn->args.is_master) |
| 1330 | continue; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1331 | err = memif_request_connection (conn); |
| 1332 | if (err != MEMIF_ERR_SUCCESS) |
| 1333 | DBG ("memif_request_connection: %s", memif_strerror (err)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | else |
| 1338 | { |
| 1339 | get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd); |
| 1340 | if (e != NULL) |
| 1341 | { |
| 1342 | if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL) |
| 1343 | { |
| 1344 | num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1345 | (((memif_connection_t *) e->data_struct)-> |
| 1346 | args.is_master) ? ((memif_connection_t *) e-> |
| 1347 | data_struct)->run_args. |
| 1348 | num_s2m_rings : ((memif_connection_t *) e->data_struct)-> |
| 1349 | run_args.num_m2s_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1350 | for (i = 0; i < num; i++) |
| 1351 | { |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1352 | if (((memif_connection_t *) e->data_struct)-> |
| 1353 | rx_queues[i].int_fd == fd) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1354 | { |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1355 | ((memif_connection_t *) e->data_struct)-> |
| 1356 | on_interrupt ((void *) e->data_struct, |
| 1357 | ((memif_connection_t *) e-> |
| 1358 | data_struct)->private_ctx, i); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1359 | return MEMIF_ERR_SUCCESS; |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | return MEMIF_ERR_SUCCESS; |
| 1364 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1365 | get_list_elt (&e, lm->socket_list, lm->socket_list_len, fd); |
| 1366 | if (e != NULL |
| 1367 | && ((memif_socket_t *) e->data_struct)->type == |
| 1368 | MEMIF_SOCKET_TYPE_LISTENER) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1369 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1370 | err = |
| 1371 | memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1372 | return err; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd); |
| 1376 | if (e != NULL) |
| 1377 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1378 | err = memif_read_ready (lm, fd); |
| 1379 | return err; |
| 1380 | } |
| 1381 | |
| 1382 | get_list_elt (&e, lm->control_list, lm->control_list_len, fd); |
| 1383 | if (e != NULL) |
| 1384 | { |
| 1385 | if (events & MEMIF_FD_EVENT_READ) |
| 1386 | { |
| 1387 | err = |
| 1388 | ((memif_connection_t *) e->data_struct)-> |
| 1389 | read_fn (e->data_struct); |
| 1390 | if (err != MEMIF_ERR_SUCCESS) |
| 1391 | return err; |
| 1392 | } |
| 1393 | if (events & MEMIF_FD_EVENT_WRITE) |
| 1394 | { |
| 1395 | err = |
| 1396 | ((memif_connection_t *) e->data_struct)-> |
| 1397 | write_fn (e->data_struct); |
| 1398 | if (err != MEMIF_ERR_SUCCESS) |
| 1399 | return err; |
| 1400 | } |
| 1401 | if (events & MEMIF_FD_EVENT_ERROR) |
| 1402 | { |
| 1403 | err = |
| 1404 | ((memif_connection_t *) e->data_struct)-> |
| 1405 | error_fn (e->data_struct); |
| 1406 | if (err != MEMIF_ERR_SUCCESS) |
| 1407 | return err; |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | return MEMIF_ERR_SUCCESS; /* 0 */ |
| 1413 | |
| 1414 | error: |
| 1415 | return err; |
| 1416 | } |
| 1417 | |
| 1418 | int |
| 1419 | memif_per_thread_control_fd_handler (memif_per_thread_main_handle_t pt_main, |
| 1420 | int fd, uint8_t events) |
| 1421 | { |
| 1422 | int i, err = MEMIF_ERR_SUCCESS; /* 0 */ |
| 1423 | uint16_t num; |
| 1424 | memif_list_elt_t *e = NULL; |
| 1425 | memif_connection_t *conn; |
| 1426 | libmemif_main_t *lm = (libmemif_main_t *) pt_main; |
| 1427 | |
| 1428 | if (fd == lm->timerfd) |
| 1429 | { |
| 1430 | uint64_t b; |
| 1431 | ssize_t size; |
| 1432 | size = read (fd, &b, sizeof (b)); |
| 1433 | |
| 1434 | if (size == -1) |
| 1435 | goto error; |
| 1436 | |
| 1437 | for (i = 0; i < lm->control_list_len; i++) |
| 1438 | { |
| 1439 | if ((lm->control_list[i].key < 0) |
| 1440 | && (lm->control_list[i].data_struct != NULL)) |
| 1441 | { |
| 1442 | conn = lm->control_list[i].data_struct; |
| 1443 | if (conn->args.is_master) |
| 1444 | continue; |
| 1445 | err = memif_request_connection (conn); |
| 1446 | if (err != MEMIF_ERR_SUCCESS) |
| 1447 | DBG ("memif_request_connection: %s", memif_strerror (err)); |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | else |
| 1452 | { |
| 1453 | get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd); |
| 1454 | if (e != NULL) |
| 1455 | { |
| 1456 | if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL) |
| 1457 | { |
| 1458 | num = |
| 1459 | (((memif_connection_t *) e->data_struct)-> |
| 1460 | args.is_master) ? ((memif_connection_t *) e-> |
| 1461 | data_struct)->run_args. |
| 1462 | num_s2m_rings : ((memif_connection_t *) e->data_struct)-> |
| 1463 | run_args.num_m2s_rings; |
| 1464 | for (i = 0; i < num; i++) |
| 1465 | { |
| 1466 | if (((memif_connection_t *) e->data_struct)-> |
| 1467 | rx_queues[i].int_fd == fd) |
| 1468 | { |
| 1469 | ((memif_connection_t *) e->data_struct)-> |
| 1470 | on_interrupt ((void *) e->data_struct, |
| 1471 | ((memif_connection_t *) e-> |
| 1472 | data_struct)->private_ctx, i); |
| 1473 | return MEMIF_ERR_SUCCESS; |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| 1477 | return MEMIF_ERR_SUCCESS; |
| 1478 | } |
| 1479 | get_list_elt (&e, lm->socket_list, lm->socket_list_len, fd); |
| 1480 | if (e != NULL |
| 1481 | && ((memif_socket_t *) e->data_struct)->type == |
| 1482 | MEMIF_SOCKET_TYPE_LISTENER) |
| 1483 | { |
| 1484 | err = |
| 1485 | memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct); |
| 1486 | return err; |
| 1487 | } |
| 1488 | |
| 1489 | get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd); |
| 1490 | if (e != NULL) |
| 1491 | { |
| 1492 | err = memif_read_ready (lm, fd); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 1493 | return err; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | get_list_elt (&e, lm->control_list, lm->control_list_len, fd); |
| 1497 | if (e != NULL) |
| 1498 | { |
| 1499 | if (events & MEMIF_FD_EVENT_READ) |
| 1500 | { |
| 1501 | err = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1502 | ((memif_connection_t *) e->data_struct)-> |
| 1503 | read_fn (e->data_struct); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1504 | if (err != MEMIF_ERR_SUCCESS) |
| 1505 | return err; |
| 1506 | } |
| 1507 | if (events & MEMIF_FD_EVENT_WRITE) |
| 1508 | { |
| 1509 | err = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1510 | ((memif_connection_t *) e->data_struct)-> |
| 1511 | write_fn (e->data_struct); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1512 | if (err != MEMIF_ERR_SUCCESS) |
| 1513 | return err; |
| 1514 | } |
| 1515 | if (events & MEMIF_FD_EVENT_ERROR) |
| 1516 | { |
| 1517 | err = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1518 | ((memif_connection_t *) e->data_struct)-> |
| 1519 | error_fn (e->data_struct); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1520 | if (err != MEMIF_ERR_SUCCESS) |
| 1521 | return err; |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | return MEMIF_ERR_SUCCESS; /* 0 */ |
| 1527 | |
| 1528 | error: |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1529 | return err; |
| 1530 | } |
| 1531 | |
| 1532 | int |
| 1533 | memif_poll_event (int timeout) |
| 1534 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1535 | libmemif_main_t *lm = &libmemif_main; |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 1536 | struct epoll_event evt; |
| 1537 | int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1538 | uint32_t events = 0; |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1539 | uint64_t counter = 0; |
| 1540 | ssize_t r = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1541 | memset (&evt, 0, sizeof (evt)); |
| 1542 | evt.events = EPOLLIN | EPOLLOUT; |
| 1543 | sigset_t sigset; |
| 1544 | sigemptyset (&sigset); |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1545 | en = epoll_pwait (lm->epfd, &evt, 1, timeout, &sigset); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1546 | if (en < 0) |
| 1547 | { |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1548 | err = errno; |
| 1549 | DBG ("epoll_pwait: %s", strerror (err)); |
| 1550 | return memif_syscall_error_handler (err); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1551 | } |
| 1552 | if (en > 0) |
| 1553 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1554 | if (evt.data.fd == lm->poll_cancel_fd) |
| 1555 | { |
| 1556 | r = read (evt.data.fd, &counter, sizeof (counter)); |
| 1557 | if (r == -1) |
| 1558 | return MEMIF_ERR_DISCONNECTED; |
| 1559 | |
| 1560 | return MEMIF_ERR_POLL_CANCEL; |
| 1561 | } |
| 1562 | if (evt.events & EPOLLIN) |
| 1563 | events |= MEMIF_FD_EVENT_READ; |
| 1564 | if (evt.events & EPOLLOUT) |
| 1565 | events |= MEMIF_FD_EVENT_WRITE; |
| 1566 | if (evt.events & EPOLLERR) |
| 1567 | events |= MEMIF_FD_EVENT_ERROR; |
| 1568 | err = memif_control_fd_handler (evt.data.fd, events); |
| 1569 | return err; |
| 1570 | } |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | int |
| 1575 | memif_per_thread_poll_event (memif_per_thread_main_handle_t pt_main, |
| 1576 | int timeout) |
| 1577 | { |
| 1578 | libmemif_main_t *lm = (libmemif_main_t *) pt_main; |
| 1579 | struct epoll_event evt; |
| 1580 | int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */ |
| 1581 | uint32_t events = 0; |
| 1582 | uint64_t counter = 0; |
| 1583 | ssize_t r = 0; |
| 1584 | memset (&evt, 0, sizeof (evt)); |
| 1585 | evt.events = EPOLLIN | EPOLLOUT; |
| 1586 | sigset_t sigset; |
| 1587 | sigemptyset (&sigset); |
| 1588 | en = epoll_pwait (lm->epfd, &evt, 1, timeout, &sigset); |
| 1589 | if (en < 0) |
| 1590 | { |
| 1591 | err = errno; |
| 1592 | DBG ("epoll_pwait: %s", strerror (err)); |
| 1593 | return memif_syscall_error_handler (err); |
| 1594 | } |
| 1595 | if (en > 0) |
| 1596 | { |
| 1597 | if (evt.data.fd == lm->poll_cancel_fd) |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1598 | { |
| 1599 | r = read (evt.data.fd, &counter, sizeof (counter)); |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 1600 | if (r == -1) |
| 1601 | return MEMIF_ERR_DISCONNECTED; |
| 1602 | |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1603 | return MEMIF_ERR_POLL_CANCEL; |
| 1604 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1605 | if (evt.events & EPOLLIN) |
| 1606 | events |= MEMIF_FD_EVENT_READ; |
| 1607 | if (evt.events & EPOLLOUT) |
| 1608 | events |= MEMIF_FD_EVENT_WRITE; |
| 1609 | if (evt.events & EPOLLERR) |
| 1610 | events |= MEMIF_FD_EVENT_ERROR; |
| 1611 | err = memif_control_fd_handler (evt.data.fd, events); |
| 1612 | return err; |
| 1613 | } |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1617 | int |
| 1618 | memif_cancel_poll_event () |
| 1619 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1620 | libmemif_main_t *lm = &libmemif_main; |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1621 | uint64_t counter = 1; |
| 1622 | ssize_t w = 0; |
| 1623 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1624 | if (lm->poll_cancel_fd == -1) |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1625 | return 0; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1626 | w = write (lm->poll_cancel_fd, &counter, sizeof (counter)); |
| 1627 | if (w < sizeof (counter)) |
| 1628 | return MEMIF_ERR_INT_WRITE; |
| 1629 | |
| 1630 | return 0; |
| 1631 | } |
| 1632 | |
| 1633 | int |
| 1634 | memif_per_thread_cancel_poll_event (memif_per_thread_main_handle_t pt_main) |
| 1635 | { |
| 1636 | libmemif_main_t *lm = (libmemif_main_t *) pt_main; |
| 1637 | uint64_t counter = 1; |
| 1638 | ssize_t w = 0; |
| 1639 | |
| 1640 | if (lm == NULL) |
| 1641 | return MEMIF_ERR_INVAL_ARG; |
| 1642 | |
| 1643 | if (lm->poll_cancel_fd == -1) |
| 1644 | return 0; |
| 1645 | w = write (lm->poll_cancel_fd, &counter, sizeof (counter)); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 1646 | if (w < sizeof (counter)) |
| 1647 | return MEMIF_ERR_INT_WRITE; |
| 1648 | |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1652 | static void |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1653 | 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] | 1654 | { |
| 1655 | if (*e == NULL) |
| 1656 | return; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1657 | memif_msg_queue_free (lm, &(*e)->next); |
| 1658 | lm->free (*e); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1659 | *e = NULL; |
| 1660 | return; |
| 1661 | } |
| 1662 | |
| 1663 | /* send disconnect msg and close interface */ |
| 1664 | int |
| 1665 | memif_disconnect_internal (memif_connection_t * c) |
| 1666 | { |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1667 | int err = MEMIF_ERR_SUCCESS, i; /* 0 */ |
| 1668 | memif_queue_t *mq; |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 1669 | libmemif_main_t *lm; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1670 | memif_list_elt_t *e; |
| 1671 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1672 | if (c == NULL) |
| 1673 | { |
| 1674 | DBG ("no connection"); |
| 1675 | return MEMIF_ERR_NOCONN; |
| 1676 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1677 | |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 1678 | lm = get_libmemif_main (c->args.socket); |
| 1679 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1680 | c->on_disconnect ((void *) c, c->private_ctx); |
| 1681 | |
| 1682 | if (c->fd > 0) |
| 1683 | { |
Jakub Grajciar | 5e1f69a | 2018-04-12 13:56:29 +0200 | [diff] [blame] | 1684 | memif_msg_send_disconnect (c->fd, (uint8_t *) "interface deleted", 0); |
Jakub Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 1685 | lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL, lm->private_ctx); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1686 | close (c->fd); |
| 1687 | } |
| 1688 | get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd); |
| 1689 | if (e != NULL) |
| 1690 | { |
| 1691 | if (c->args.is_master) |
| 1692 | free_list_elt (lm->control_list, lm->control_list_len, c->fd); |
| 1693 | e->key = c->fd = -1; |
| 1694 | } |
| 1695 | |
| 1696 | if (c->tx_queues != NULL) |
| 1697 | { |
Jakub Grajciar | 718a19b | 2019-11-05 10:58:00 +0100 | [diff] [blame] | 1698 | for (i = 0; i < c->tx_queues_num; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1699 | { |
| 1700 | mq = &c->tx_queues[i]; |
| 1701 | if (mq != NULL) |
| 1702 | { |
| 1703 | if (mq->int_fd > 0) |
| 1704 | close (mq->int_fd); |
| 1705 | free_list_elt (lm->interrupt_list, lm->interrupt_list_len, |
| 1706 | mq->int_fd); |
| 1707 | mq->int_fd = -1; |
| 1708 | } |
| 1709 | } |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1710 | lm->free (c->tx_queues); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1711 | c->tx_queues = NULL; |
| 1712 | } |
Jakub Grajciar | 718a19b | 2019-11-05 10:58:00 +0100 | [diff] [blame] | 1713 | c->tx_queues_num = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1714 | |
| 1715 | if (c->rx_queues != NULL) |
| 1716 | { |
Jakub Grajciar | 718a19b | 2019-11-05 10:58:00 +0100 | [diff] [blame] | 1717 | for (i = 0; i < c->rx_queues_num; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1718 | { |
| 1719 | mq = &c->rx_queues[i]; |
| 1720 | if (mq != NULL) |
| 1721 | { |
| 1722 | if (mq->int_fd > 0) |
| 1723 | { |
| 1724 | if (c->on_interrupt != NULL) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1725 | lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL, |
Jakub Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 1726 | lm->private_ctx); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1727 | close (mq->int_fd); |
| 1728 | } |
| 1729 | free_list_elt (lm->interrupt_list, lm->interrupt_list_len, |
| 1730 | mq->int_fd); |
| 1731 | mq->int_fd = -1; |
| 1732 | } |
| 1733 | } |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1734 | lm->free (c->rx_queues); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1735 | c->rx_queues = NULL; |
| 1736 | } |
Jakub Grajciar | 718a19b | 2019-11-05 10:58:00 +0100 | [diff] [blame] | 1737 | c->rx_queues_num = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1738 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1739 | for (i = 0; i < c->regions_num; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1740 | { |
Jakub Grajciar | 412e7b6 | 2018-09-27 10:26:35 +0200 | [diff] [blame] | 1741 | if (&c->regions[i] == NULL) |
| 1742 | continue; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1743 | if (c->regions[i].is_external != 0) |
| 1744 | { |
| 1745 | lm->del_external_region (c->regions[i].addr, |
| 1746 | c->regions[i].region_size, |
| 1747 | c->regions[i].fd, c->private_ctx); |
| 1748 | } |
| 1749 | else |
| 1750 | { |
| 1751 | if (munmap (c->regions[i].addr, c->regions[i].region_size) < 0) |
| 1752 | return memif_syscall_error_handler (errno); |
| 1753 | if (c->regions[i].fd > 0) |
| 1754 | close (c->regions[i].fd); |
| 1755 | c->regions[i].fd = -1; |
| 1756 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1757 | } |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1758 | lm->free (c->regions); |
| 1759 | c->regions = NULL; |
| 1760 | c->regions_num = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1761 | |
| 1762 | memset (&c->run_args, 0, sizeof (memif_conn_run_args_t)); |
| 1763 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1764 | memif_msg_queue_free (lm, &c->msg_queue); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1765 | |
| 1766 | if (!(c->args.is_master)) |
| 1767 | { |
| 1768 | if (lm->disconn_slaves == 0) |
| 1769 | { |
| 1770 | if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) |
| 1771 | { |
| 1772 | err = memif_syscall_error_handler (errno); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1773 | DBG ("timerfd_settime: arm"); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1774 | } |
| 1775 | } |
| 1776 | lm->disconn_slaves++; |
| 1777 | } |
| 1778 | |
| 1779 | return err; |
| 1780 | } |
| 1781 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 1782 | const char * |
| 1783 | memif_get_socket_filename (memif_socket_handle_t sock) |
| 1784 | { |
| 1785 | memif_socket_t *ms = (memif_socket_t *) sock; |
| 1786 | |
| 1787 | if (ms == NULL) |
| 1788 | return NULL; |
| 1789 | |
| 1790 | return (char *) ms->filename; |
| 1791 | } |
| 1792 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1793 | int |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1794 | memif_delete_socket (memif_socket_handle_t * sock) |
| 1795 | { |
| 1796 | memif_socket_t *ms = (memif_socket_t *) * sock; |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 1797 | libmemif_main_t *lm; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1798 | |
| 1799 | /* check if socket is in use */ |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 1800 | if (ms == NULL || ms->use_count > 0) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1801 | return MEMIF_ERR_INVAL_ARG; |
| 1802 | |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 1803 | lm = get_libmemif_main (ms); |
| 1804 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1805 | lm->free (ms->interface_list); |
| 1806 | ms->interface_list = NULL; |
| 1807 | lm->free (ms->filename); |
| 1808 | ms->filename = NULL; |
| 1809 | lm->free (ms); |
| 1810 | *sock = ms = NULL; |
| 1811 | |
| 1812 | return MEMIF_ERR_SUCCESS; |
| 1813 | } |
| 1814 | |
| 1815 | int |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1816 | memif_delete (memif_conn_handle_t * conn) |
| 1817 | { |
| 1818 | memif_connection_t *c = (memif_connection_t *) * conn; |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 1819 | libmemif_main_t *lm; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1820 | memif_socket_t *ms = NULL; |
| 1821 | int err = MEMIF_ERR_SUCCESS; |
| 1822 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1823 | if (c == NULL) |
| 1824 | { |
| 1825 | DBG ("no connection"); |
| 1826 | return MEMIF_ERR_NOCONN; |
| 1827 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1828 | |
| 1829 | if (c->fd > 0) |
| 1830 | { |
| 1831 | DBG ("DISCONNECTING"); |
| 1832 | err = memif_disconnect_internal (c); |
| 1833 | if (err == MEMIF_ERR_NOCONN) |
| 1834 | return err; |
| 1835 | } |
| 1836 | |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 1837 | lm = get_libmemif_main (c->args.socket); |
| 1838 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1839 | free_list_elt_ctx (lm->control_list, lm->control_list_len, c); |
| 1840 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1841 | ms = (memif_socket_t *) c->args.socket; |
| 1842 | ms->use_count--; |
| 1843 | free_list_elt (ms->interface_list, ms->interface_list_len, |
| 1844 | c->args.interface_id); |
| 1845 | if (ms->use_count <= 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1846 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1847 | /* stop listening on this socket */ |
| 1848 | if (ms->type == MEMIF_SOCKET_TYPE_LISTENER) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1849 | { |
Jakub Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 1850 | lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_DEL, lm->private_ctx); |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1851 | free_list_elt (lm->socket_list, lm->socket_list_len, ms->fd); |
| 1852 | close (ms->fd); |
| 1853 | ms->fd = -1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1854 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1855 | /* socket not in use */ |
| 1856 | ms->type = MEMIF_SOCKET_TYPE_NONE; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1857 | } |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1858 | |
| 1859 | if (!c->args.is_master) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1860 | { |
| 1861 | lm->disconn_slaves--; |
| 1862 | if (lm->disconn_slaves <= 0) |
| 1863 | { |
| 1864 | if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) |
| 1865 | { |
| 1866 | err = memif_syscall_error_handler (errno); |
| 1867 | DBG ("timerfd_settime: disarm"); |
| 1868 | } |
| 1869 | } |
| 1870 | } |
| 1871 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1872 | lm->free (c); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1873 | c = NULL; |
| 1874 | |
| 1875 | *conn = c; |
| 1876 | return err; |
| 1877 | } |
| 1878 | |
| 1879 | int |
| 1880 | memif_connect1 (memif_connection_t * c) |
| 1881 | { |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 1882 | libmemif_main_t *lm; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1883 | memif_region_t *mr; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1884 | memif_queue_t *mq; |
| 1885 | int i; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1886 | |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 1887 | if (c == NULL) |
| 1888 | return MEMIF_ERR_INVAL_ARG; |
| 1889 | |
| 1890 | lm = get_libmemif_main (c->args.socket); |
| 1891 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1892 | for (i = 0; i < c->regions_num; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1893 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1894 | mr = &c->regions[i]; |
| 1895 | if (mr != NULL) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1896 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1897 | if (!mr->addr) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1898 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1899 | if (mr->is_external) |
| 1900 | { |
| 1901 | if (lm->get_external_region_addr == NULL) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1902 | return MEMIF_ERR_INVAL_ARG; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1903 | mr->addr = |
| 1904 | lm->get_external_region_addr (mr->region_size, mr->fd, |
| 1905 | c->private_ctx); |
| 1906 | } |
| 1907 | else |
| 1908 | { |
| 1909 | if (mr->fd < 0) |
| 1910 | return MEMIF_ERR_NO_SHMFD; |
| 1911 | |
| 1912 | if ((mr->addr = |
| 1913 | mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE, |
| 1914 | MAP_SHARED, mr->fd, 0)) == MAP_FAILED) |
| 1915 | { |
| 1916 | return memif_syscall_error_handler (errno); |
| 1917 | } |
| 1918 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1919 | } |
| 1920 | } |
| 1921 | } |
| 1922 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1923 | for (i = 0; i < c->rx_queues_num; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1924 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1925 | mq = &c->rx_queues[i]; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1926 | if (mq != NULL) |
| 1927 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1928 | mq->ring = c->regions[mq->region].addr + mq->offset; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1929 | if (mq->ring->cookie != MEMIF_COOKIE) |
| 1930 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1931 | DBG ("wrong cookie on rx ring %u", i); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1932 | return MEMIF_ERR_COOKIE; |
| 1933 | } |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 1934 | mq->ring->head = mq->ring->tail = mq->last_head = mq->next_buf = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1935 | } |
| 1936 | } |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1937 | |
| 1938 | for (i = 0; i < c->tx_queues_num; i++) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1939 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1940 | mq = &c->tx_queues[i]; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1941 | if (mq != NULL) |
| 1942 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1943 | mq->ring = c->regions[mq->region].addr + mq->offset; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1944 | if (mq->ring->cookie != MEMIF_COOKIE) |
| 1945 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1946 | DBG ("wrong cookie on tx ring %u", i); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1947 | return MEMIF_ERR_COOKIE; |
| 1948 | } |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 1949 | mq->ring->head = mq->ring->tail = mq->last_head = mq->next_buf = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 1953 | lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD, |
Jakub Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 1954 | lm->private_ctx); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1955 | |
| 1956 | return 0; |
| 1957 | } |
| 1958 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1959 | static inline int |
| 1960 | memif_add_region (libmemif_main_t * lm, memif_connection_t * conn, |
| 1961 | uint8_t has_buffers) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1962 | { |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1963 | memif_region_t *r; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1964 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1965 | r = |
| 1966 | lm->realloc (conn->regions, |
| 1967 | sizeof (memif_region_t) * ++conn->regions_num); |
| 1968 | if (r == NULL) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1969 | return MEMIF_ERR_NOMEM; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1970 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1971 | conn->regions = r; |
| 1972 | r = &conn->regions[conn->regions_num - 1]; |
| 1973 | memset (r, 0, sizeof (memif_region_t)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1974 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1975 | if (has_buffers != 0) |
| 1976 | { |
| 1977 | r->buffer_offset = 0; |
| 1978 | } |
| 1979 | else |
| 1980 | { |
| 1981 | r->buffer_offset = |
| 1982 | (conn->run_args.num_s2m_rings + |
| 1983 | conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) + |
| 1984 | sizeof (memif_desc_t) * |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 1985 | (1 << conn-> |
| 1986 | run_args.log2_ring_size)); |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | r->region_size = (has_buffers == 0) ? r->buffer_offset : r->buffer_offset + |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1990 | conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) * |
| 1991 | (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings); |
| 1992 | |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 1993 | if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1994 | return memif_syscall_error_handler (errno); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 1995 | |
| 1996 | if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) |
| 1997 | return memif_syscall_error_handler (errno); |
| 1998 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1999 | if ((ftruncate (r->fd, r->region_size)) == -1) |
| 2000 | return memif_syscall_error_handler (errno); |
| 2001 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2002 | if ((r->addr = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE, |
| 2003 | MAP_SHARED, r->fd, 0)) == MAP_FAILED) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2004 | return memif_syscall_error_handler (errno); |
| 2005 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2006 | return MEMIF_ERR_SUCCESS; |
| 2007 | } |
| 2008 | |
| 2009 | static inline int |
| 2010 | memif_init_queues (libmemif_main_t * lm, memif_connection_t * conn) |
| 2011 | { |
| 2012 | int i, j; |
| 2013 | memif_ring_t *ring; |
| 2014 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2015 | for (i = 0; i < conn->run_args.num_s2m_rings; i++) |
| 2016 | { |
| 2017 | ring = memif_get_ring (conn, MEMIF_RING_S2M, i); |
| 2018 | DBG ("RING: %p I: %d", ring, i); |
| 2019 | ring->head = ring->tail = 0; |
| 2020 | ring->cookie = MEMIF_COOKIE; |
| 2021 | ring->flags = 0; |
| 2022 | for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++) |
| 2023 | { |
| 2024 | uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2025 | ring->desc[j].region = 1; |
| 2026 | ring->desc[j].offset = |
| 2027 | conn->regions[1].buffer_offset + |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2028 | (uint32_t) (slot * conn->run_args.buffer_size); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2029 | ring->desc[j].length = conn->run_args.buffer_size; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2030 | } |
| 2031 | } |
| 2032 | for (i = 0; i < conn->run_args.num_m2s_rings; i++) |
| 2033 | { |
| 2034 | ring = memif_get_ring (conn, MEMIF_RING_M2S, i); |
| 2035 | DBG ("RING: %p I: %d", ring, i); |
| 2036 | ring->head = ring->tail = 0; |
| 2037 | ring->cookie = MEMIF_COOKIE; |
| 2038 | ring->flags = 0; |
| 2039 | for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++) |
| 2040 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2041 | uint16_t slot = (i + conn->run_args.num_s2m_rings) * |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2042 | (1 << conn->run_args.log2_ring_size) + j; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2043 | ring->desc[j].region = 1; |
| 2044 | ring->desc[j].offset = |
| 2045 | conn->regions[1].buffer_offset + |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2046 | (uint32_t) (slot * conn->run_args.buffer_size); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2047 | ring->desc[j].length = conn->run_args.buffer_size; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2048 | } |
| 2049 | } |
| 2050 | memif_queue_t *mq; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2051 | DBG ("alloc: %p", lm->alloc); |
| 2052 | 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] | 2053 | mq = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2054 | (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) * |
| 2055 | conn->run_args.num_s2m_rings); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2056 | if (mq == NULL) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2057 | return MEMIF_ERR_NOMEM; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2058 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2059 | int x; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2060 | memif_list_elt_t e; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2061 | for (x = 0; x < conn->run_args.num_s2m_rings; x++) |
| 2062 | { |
| 2063 | if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) |
| 2064 | return memif_syscall_error_handler (errno); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2065 | e.key = mq[x].int_fd; |
| 2066 | e.data_struct = conn; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 2067 | add_list_elt (lm, &e, &lm->interrupt_list, &lm->interrupt_list_len); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2068 | |
| 2069 | mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x); |
| 2070 | DBG ("RING: %p I: %d", mq[x].ring, x); |
| 2071 | mq[x].log2_ring_size = conn->run_args.log2_ring_size; |
| 2072 | mq[x].region = 0; |
| 2073 | mq[x].offset = |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2074 | (void *) mq[x].ring - (void *) conn->regions[mq->region].addr; |
| 2075 | mq[x].last_head = mq[x].last_tail = 0; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2076 | mq[x].next_buf = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2077 | } |
| 2078 | conn->tx_queues = mq; |
Jakub Grajciar | 718a19b | 2019-11-05 10:58:00 +0100 | [diff] [blame] | 2079 | conn->tx_queues_num = conn->run_args.num_s2m_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2080 | |
| 2081 | mq = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2082 | (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) * |
| 2083 | conn->run_args.num_m2s_rings); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2084 | if (mq == NULL) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2085 | return MEMIF_ERR_NOMEM; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2086 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2087 | for (x = 0; x < conn->run_args.num_m2s_rings; x++) |
| 2088 | { |
| 2089 | if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) |
| 2090 | return memif_syscall_error_handler (errno); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2091 | e.key = mq[x].int_fd; |
| 2092 | e.data_struct = conn; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 2093 | add_list_elt (lm, &e, &lm->interrupt_list, &lm->interrupt_list_len); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2094 | |
| 2095 | mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x); |
| 2096 | DBG ("RING: %p I: %d", mq[x].ring, x); |
| 2097 | mq[x].log2_ring_size = conn->run_args.log2_ring_size; |
| 2098 | mq[x].region = 0; |
| 2099 | mq[x].offset = |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2100 | (void *) mq[x].ring - (void *) conn->regions[mq->region].addr; |
| 2101 | mq[x].last_head = mq[x].last_tail = 0; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2102 | mq[x].next_buf = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2103 | } |
| 2104 | conn->rx_queues = mq; |
Jakub Grajciar | 718a19b | 2019-11-05 10:58:00 +0100 | [diff] [blame] | 2105 | conn->rx_queues_num = conn->run_args.num_m2s_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2106 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2107 | return MEMIF_ERR_SUCCESS; |
| 2108 | } |
| 2109 | |
| 2110 | int |
| 2111 | memif_init_regions_and_queues (memif_connection_t * conn) |
| 2112 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2113 | memif_region_t *r; |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 2114 | libmemif_main_t *lm; |
| 2115 | |
| 2116 | if (conn == NULL) |
| 2117 | return MEMIF_ERR_INVAL_ARG; |
| 2118 | |
| 2119 | lm = get_libmemif_main (conn->args.socket); |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2120 | |
| 2121 | /* region 0. rings */ |
| 2122 | memif_add_region (lm, conn, /* has_buffers */ 0); |
| 2123 | |
| 2124 | /* region 1. buffers */ |
| 2125 | if (lm->add_external_region) |
| 2126 | { |
| 2127 | r = |
| 2128 | (memif_region_t *) lm->realloc (conn->regions, |
| 2129 | sizeof (memif_region_t) * |
| 2130 | ++conn->regions_num); |
| 2131 | if (r == NULL) |
| 2132 | return MEMIF_ERR_NOMEM; |
| 2133 | conn->regions = r; |
| 2134 | |
| 2135 | conn->regions[1].region_size = |
| 2136 | conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) * |
| 2137 | (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings); |
| 2138 | conn->regions[1].buffer_offset = 0; |
| 2139 | lm->add_external_region (&conn->regions[1].addr, |
| 2140 | conn->regions[1].region_size, |
| 2141 | &conn->regions[1].fd, conn->private_ctx); |
| 2142 | conn->regions[1].is_external = 1; |
| 2143 | } |
| 2144 | else |
| 2145 | { |
| 2146 | memif_add_region (lm, conn, 1); |
| 2147 | } |
| 2148 | |
| 2149 | memif_init_queues (lm, conn); |
| 2150 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2151 | return 0; |
| 2152 | } |
| 2153 | |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2154 | static void |
| 2155 | memif_buffer_enq_at_idx_internal (memif_queue_t *from_q, memif_queue_t *to_q, |
| 2156 | memif_buffer_t *buf, uint16_t slot) |
| 2157 | { |
| 2158 | uint16_t from_mask = (1 << from_q->log2_ring_size) - 1; |
| 2159 | uint16_t to_mask = (1 << to_q->log2_ring_size) - 1; |
| 2160 | memif_desc_t *from_d, *to_d, tmp_d; |
| 2161 | |
| 2162 | /* Get the descriptors */ |
| 2163 | from_d = &from_q->ring->desc[buf->desc_index & from_mask]; |
| 2164 | to_d = &to_q->ring->desc[slot & to_mask]; |
| 2165 | |
| 2166 | /* Swap descriptors */ |
| 2167 | tmp_d = *from_d; |
| 2168 | *from_d = *to_d; |
| 2169 | *to_d = tmp_d; |
| 2170 | |
| 2171 | /* Update descriptor index and queue for clients buffer */ |
| 2172 | buf->desc_index = slot; |
| 2173 | buf->queue = to_q; |
| 2174 | } |
| 2175 | |
| 2176 | int |
| 2177 | memif_buffer_requeue (memif_conn_handle_t conn, memif_buffer_t *buf_a, |
| 2178 | memif_buffer_t *buf_b) |
| 2179 | { |
| 2180 | memif_connection_t *c = (memif_connection_t *) conn; |
| 2181 | if (EXPECT_FALSE (c == NULL)) |
| 2182 | return MEMIF_ERR_NOCONN; |
| 2183 | if (EXPECT_FALSE (c->args.is_master)) |
| 2184 | return MEMIF_ERR_INVAL_ARG; |
| 2185 | if ((buf_a == NULL) || (buf_b == NULL)) |
| 2186 | return MEMIF_ERR_INVAL_ARG; |
| 2187 | |
| 2188 | int err; |
| 2189 | /* store buf_a information */ |
| 2190 | uint16_t index_a = buf_a->desc_index; |
| 2191 | memif_queue_t *mq_a = buf_a->queue; |
| 2192 | |
| 2193 | /* swap buffers, buf_a was updated with new desc_index and queue */ |
| 2194 | memif_buffer_enq_at_idx_internal ((memif_queue_t *) buf_a->queue, |
| 2195 | (memif_queue_t *) buf_b->queue, buf_a, |
| 2196 | buf_b->desc_index); |
| 2197 | |
| 2198 | /* update buf_b desc_index and queue */ |
| 2199 | buf_b->desc_index = index_a; |
| 2200 | buf_b->queue = mq_a; |
| 2201 | |
| 2202 | return MEMIF_ERR_SUCCESS; |
| 2203 | } |
| 2204 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2205 | int |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2206 | memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid, |
| 2207 | memif_buffer_t * bufs, uint16_t count, |
| 2208 | uint16_t * count_out) |
| 2209 | { |
| 2210 | memif_connection_t *c = (memif_connection_t *) conn; |
| 2211 | if (EXPECT_FALSE (c == NULL)) |
| 2212 | return MEMIF_ERR_NOCONN; |
| 2213 | if (EXPECT_FALSE (c->fd < 0)) |
| 2214 | return MEMIF_ERR_DISCONNECTED; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2215 | if (EXPECT_FALSE (qid >= c->tx_queues_num)) |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2216 | return MEMIF_ERR_QID; |
| 2217 | if (EXPECT_FALSE (!count_out)) |
| 2218 | return MEMIF_ERR_INVAL_ARG; |
| 2219 | if (EXPECT_FALSE (c->args.is_master)) |
| 2220 | return MEMIF_ERR_INVAL_ARG; |
| 2221 | |
| 2222 | memif_queue_t *mq = &c->tx_queues[qid]; |
| 2223 | memif_ring_t *ring = mq->ring; |
| 2224 | memif_buffer_t *b0; |
| 2225 | uint16_t mask = (1 << mq->log2_ring_size) - 1; |
| 2226 | uint16_t ring_size; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2227 | uint16_t ns; |
| 2228 | memif_queue_t *bmq; |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 2229 | int err = MEMIF_ERR_SUCCESS; /* 0 */ |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2230 | *count_out = 0; |
| 2231 | |
| 2232 | ring_size = (1 << mq->log2_ring_size); |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2233 | |
| 2234 | /* can only be called by slave */ |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2235 | ns = ring_size - mq->next_buf + ring->tail; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2236 | |
| 2237 | b0 = bufs; |
| 2238 | |
| 2239 | while (count && ns) |
| 2240 | { |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2241 | /* Swaps the descriptors, updates next_buf pointer and updates client |
| 2242 | * memif buffer */ |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2243 | |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2244 | memif_buffer_enq_at_idx_internal ((memif_queue_t *) b0->queue, mq, b0, |
| 2245 | mq->next_buf); |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2246 | |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2247 | mq->next_buf++; /* mark the buffer as allocated */ |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2248 | count--; |
| 2249 | ns--; |
| 2250 | b0++; |
| 2251 | *count_out += 1; |
| 2252 | } |
| 2253 | |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2254 | DBG ("allocated: %u/%u bufs. Next buffer pointer %d", *count_out, count, |
| 2255 | mq->next_buf); |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2256 | |
| 2257 | if (count) |
| 2258 | { |
| 2259 | DBG ("ring buffer full! qid: %u", qid); |
| 2260 | err = MEMIF_ERR_NOBUF_RING; |
| 2261 | } |
| 2262 | |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2263 | return err; |
| 2264 | } |
| 2265 | |
| 2266 | int |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2267 | memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, |
| 2268 | memif_buffer_t * bufs, uint16_t count, |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 2269 | uint16_t * count_out, uint16_t size) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2270 | { |
| 2271 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2272 | if (EXPECT_FALSE (c == NULL)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2273 | return MEMIF_ERR_NOCONN; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2274 | if (EXPECT_FALSE (c->fd < 0)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2275 | return MEMIF_ERR_DISCONNECTED; |
| 2276 | uint8_t num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2277 | (c->args.is_master) ? c->run_args.num_m2s_rings : c-> |
| 2278 | run_args.num_s2m_rings; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2279 | if (EXPECT_FALSE (qid >= num)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2280 | return MEMIF_ERR_QID; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2281 | if (EXPECT_FALSE (!count_out)) |
| 2282 | return MEMIF_ERR_INVAL_ARG; |
| 2283 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 2284 | libmemif_main_t *lm = get_libmemif_main (c->args.socket); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2285 | memif_queue_t *mq = &c->tx_queues[qid]; |
| 2286 | memif_ring_t *ring = mq->ring; |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 2287 | memif_buffer_t *b0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2288 | uint16_t mask = (1 << mq->log2_ring_size) - 1; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2289 | uint32_t offset_mask = c->run_args.buffer_size - 1; |
Chun Li | 5dca546 | 2018-02-07 09:51:35 +0800 | [diff] [blame] | 2290 | uint16_t ring_size; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2291 | uint16_t ns; |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 2292 | int err = MEMIF_ERR_SUCCESS; /* 0 */ |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2293 | uint16_t dst_left, src_left; |
| 2294 | uint16_t saved_count; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2295 | uint16_t saved_next_buf; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2296 | memif_buffer_t *saved_b; |
| 2297 | *count_out = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2298 | |
Chun Li | 5dca546 | 2018-02-07 09:51:35 +0800 | [diff] [blame] | 2299 | ring_size = (1 << mq->log2_ring_size); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2300 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2301 | if (c->args.is_master) |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2302 | ns = ring->head - mq->next_buf; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2303 | else |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2304 | ns = ring_size - mq->next_buf + ring->tail; |
Jakub Grajciar | 04b68bd | 2017-10-30 10:34:54 +0100 | [diff] [blame] | 2305 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2306 | while (count && ns) |
| 2307 | { |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2308 | b0 = (bufs + *count_out); |
| 2309 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2310 | saved_b = b0; |
| 2311 | saved_count = count; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2312 | saved_next_buf = mq->next_buf; |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 2313 | |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2314 | b0->desc_index = mq->next_buf; |
| 2315 | ring->desc[mq->next_buf & mask].flags = 0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2316 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2317 | /* slave can produce buffer with original length */ |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2318 | dst_left = (c->args.is_master) ? ring->desc[mq->next_buf & mask].length : |
| 2319 | c->run_args.buffer_size; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2320 | src_left = size; |
| 2321 | |
| 2322 | while (src_left) |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 2323 | { |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2324 | if (EXPECT_FALSE (dst_left == 0)) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2325 | { |
| 2326 | if (count && ns) |
| 2327 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2328 | *count_out += 1; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2329 | mq->next_buf++; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2330 | ns--; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2331 | |
| 2332 | ring->desc[b0->desc_index & mask].flags |= |
| 2333 | MEMIF_DESC_FLAG_NEXT; |
| 2334 | b0->flags |= MEMIF_BUFFER_FLAG_NEXT; |
| 2335 | |
| 2336 | b0 = (bufs + *count_out); |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2337 | b0->desc_index = mq->next_buf; |
| 2338 | dst_left = (c->args.is_master) ? |
| 2339 | ring->desc[mq->next_buf & mask].length : |
| 2340 | c->run_args.buffer_size; |
| 2341 | ring->desc[mq->next_buf & mask].flags = 0; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2342 | } |
| 2343 | else |
| 2344 | { |
| 2345 | /* rollback allocated chain buffers */ |
| 2346 | memset (saved_b, 0, sizeof (memif_buffer_t) |
| 2347 | * (saved_count - count + 1)); |
| 2348 | *count_out -= saved_count - count; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2349 | mq->next_buf = saved_next_buf; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2350 | goto no_ns; |
| 2351 | } |
| 2352 | } |
| 2353 | b0->len = memif_min (dst_left, src_left); |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2354 | |
| 2355 | /* slave resets buffer offset */ |
| 2356 | if (c->args.is_master == 0) |
| 2357 | { |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2358 | memif_desc_t *d = &ring->desc[mq->next_buf & mask]; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2359 | if (lm->get_external_buffer_offset) |
| 2360 | d->offset = lm->get_external_buffer_offset (c->private_ctx); |
| 2361 | else |
| 2362 | d->offset = d->offset - (d->offset & offset_mask); |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2363 | } |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2364 | b0->data = memif_get_buffer (c, ring, mq->next_buf & mask); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2365 | |
| 2366 | src_left -= b0->len; |
| 2367 | dst_left -= b0->len; |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 2368 | } |
| 2369 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2370 | *count_out += 1; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2371 | mq->next_buf++; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2372 | ns--; |
| 2373 | count--; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2374 | } |
| 2375 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2376 | no_ns: |
| 2377 | |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2378 | DBG ("allocated: %u/%u bufs. Next buffer pointer %d", *count_out, count, |
| 2379 | mq->next_buf); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2380 | |
| 2381 | if (count) |
| 2382 | { |
| 2383 | DBG ("ring buffer full! qid: %u", qid); |
| 2384 | err = MEMIF_ERR_NOBUF_RING; |
| 2385 | } |
| 2386 | |
| 2387 | return err; |
| 2388 | } |
| 2389 | |
| 2390 | int |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2391 | memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count, |
| 2392 | uint16_t headroom) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2393 | { |
| 2394 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2395 | if (EXPECT_FALSE (c == NULL)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2396 | return MEMIF_ERR_NOCONN; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2397 | if (EXPECT_FALSE (c->fd < 0)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2398 | return MEMIF_ERR_DISCONNECTED; |
| 2399 | uint8_t num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2400 | (c->args.is_master) ? c->run_args.num_s2m_rings : c-> |
| 2401 | run_args.num_m2s_rings; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2402 | if (EXPECT_FALSE (qid >= num)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2403 | return MEMIF_ERR_QID; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 2404 | libmemif_main_t *lm = get_libmemif_main (c->args.socket); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2405 | memif_queue_t *mq = &c->rx_queues[qid]; |
| 2406 | memif_ring_t *ring = mq->ring; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2407 | uint16_t mask = (1 << mq->log2_ring_size) - 1; |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2408 | uint32_t offset_mask = c->run_args.buffer_size - 1; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2409 | uint16_t slot; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2410 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2411 | if (c->args.is_master) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2412 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2413 | MEMIF_MEMORY_BARRIER (); |
| 2414 | ring->tail = |
| 2415 | (ring->tail + count <= |
| 2416 | mq->last_head) ? ring->tail + count : mq->last_head; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2417 | return MEMIF_ERR_SUCCESS; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2418 | } |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2419 | |
| 2420 | uint16_t head = ring->head; |
| 2421 | uint16_t ns = (1 << mq->log2_ring_size) - head + mq->last_tail; |
| 2422 | head += (count < ns) ? count : ns; |
| 2423 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2424 | slot = ring->head; |
| 2425 | memif_desc_t *d; |
| 2426 | while (slot < head) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2427 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2428 | d = &ring->desc[slot & mask]; |
| 2429 | d->region = 1; |
| 2430 | d->length = c->run_args.buffer_size - headroom; |
| 2431 | if (lm->get_external_buffer_offset) |
| 2432 | d->offset = lm->get_external_buffer_offset (c->private_ctx); |
| 2433 | else |
| 2434 | d->offset = d->offset - (d->offset & offset_mask) + headroom; |
| 2435 | slot++; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2436 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2437 | |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2438 | MEMIF_MEMORY_BARRIER (); |
| 2439 | ring->head = head; |
| 2440 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2441 | return MEMIF_ERR_SUCCESS; /* 0 */ |
| 2442 | } |
| 2443 | |
| 2444 | int |
| 2445 | memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, |
| 2446 | memif_buffer_t * bufs, uint16_t count, uint16_t * tx) |
| 2447 | { |
| 2448 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2449 | if (EXPECT_FALSE (c == NULL)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2450 | return MEMIF_ERR_NOCONN; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2451 | if (EXPECT_FALSE (c->fd < 0)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2452 | return MEMIF_ERR_DISCONNECTED; |
| 2453 | uint8_t num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2454 | (c->args.is_master) ? c->run_args.num_m2s_rings : c-> |
| 2455 | run_args.num_s2m_rings; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2456 | if (EXPECT_FALSE (qid >= num)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2457 | return MEMIF_ERR_QID; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2458 | if (EXPECT_FALSE (!tx)) |
| 2459 | return MEMIF_ERR_INVAL_ARG; |
| 2460 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2461 | memif_queue_t *mq = &c->tx_queues[qid]; |
| 2462 | memif_ring_t *ring = mq->ring; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2463 | uint16_t mask = (1 << mq->log2_ring_size) - 1; |
| 2464 | memif_buffer_t *b0; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2465 | *tx = 0; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2466 | int err = MEMIF_ERR_SUCCESS; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2467 | |
| 2468 | if (EXPECT_FALSE (count == 0)) |
| 2469 | return MEMIF_ERR_SUCCESS; |
| 2470 | |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2471 | uint16_t index; |
| 2472 | if (c->args.is_master) |
| 2473 | index = ring->tail; |
| 2474 | else |
| 2475 | index = ring->head; |
| 2476 | |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2477 | while (count) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2478 | { |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2479 | b0 = (bufs + *tx); |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2480 | /* set error to MEMIF_ERR_INVAL_ARG and finish the sending process |
| 2481 | */ |
| 2482 | if ((b0->desc_index & mask) != (index & mask)) |
| 2483 | { |
| 2484 | err = MEMIF_ERR_INVAL_ARG; |
| 2485 | goto done; |
| 2486 | } |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2487 | ring->desc[b0->desc_index & mask].length = b0->len; |
| 2488 | |
| 2489 | #ifdef MEMIF_DBG_SHM |
| 2490 | printf ("offset: %-6d\n", ring->desc[b0->desc_index & mask].offset); |
| 2491 | printf ("data: %p\n", |
| 2492 | memif_get_buffer (c, ring, b0->desc_index & mask)); |
| 2493 | printf ("index: %u\n", b0->desc_index); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2494 | print_bytes (memif_get_buffer (c, ring, b0->desc_index & mask), |
| 2495 | ring->desc[b0->desc_index & mask].length, DBG_TX_BUF); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2496 | #endif /* MEMIF_DBG_SHM */ |
| 2497 | |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2498 | *tx += 1; |
| 2499 | count--; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2500 | index++; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2501 | } |
| 2502 | |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2503 | done: |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2504 | MEMIF_MEMORY_BARRIER (); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2505 | if (c->args.is_master) |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2506 | ring->tail = b0->desc_index + 1; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2507 | else |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2508 | ring->head = b0->desc_index + 1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2509 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2510 | if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) |
| 2511 | { |
| 2512 | uint64_t a = 1; |
| 2513 | int r = write (mq->int_fd, &a, sizeof (a)); |
| 2514 | if (r < 0) |
| 2515 | return MEMIF_ERR_INT_WRITE; |
| 2516 | } |
| 2517 | |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2518 | return err; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | int |
| 2522 | memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, |
| 2523 | memif_buffer_t * bufs, uint16_t count, uint16_t * rx) |
| 2524 | { |
| 2525 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2526 | if (EXPECT_FALSE (c == NULL)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2527 | return MEMIF_ERR_NOCONN; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2528 | if (EXPECT_FALSE (c->fd < 0)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2529 | return MEMIF_ERR_DISCONNECTED; |
| 2530 | uint8_t num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2531 | (c->args.is_master) ? c->run_args.num_s2m_rings : c-> |
| 2532 | run_args.num_m2s_rings; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2533 | if (EXPECT_FALSE (qid >= num)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2534 | return MEMIF_ERR_QID; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2535 | if (EXPECT_FALSE (!rx)) |
| 2536 | return MEMIF_ERR_INVAL_ARG; |
| 2537 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2538 | memif_queue_t *mq = &c->rx_queues[qid]; |
| 2539 | memif_ring_t *ring = mq->ring; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2540 | uint16_t cur_slot, last_slot; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2541 | uint16_t ns; |
| 2542 | uint16_t mask = (1 << mq->log2_ring_size) - 1; |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 2543 | memif_buffer_t *b0; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2544 | *rx = 0; |
| 2545 | |
| 2546 | uint64_t b; |
Jan Cavojsky | 77a95cd | 2020-03-03 16:25:58 +0100 | [diff] [blame] | 2547 | ssize_t r; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2548 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2549 | cur_slot = (c->args.is_master) ? mq->last_head : mq->last_tail; |
| 2550 | last_slot = (c->args.is_master) ? ring->head : ring->tail; |
| 2551 | if (cur_slot == last_slot) |
Jan Cavojsky | 77a95cd | 2020-03-03 16:25:58 +0100 | [diff] [blame] | 2552 | { |
| 2553 | r = read (mq->int_fd, &b, sizeof (b)); |
| 2554 | if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN))) |
| 2555 | return memif_syscall_error_handler (errno); |
| 2556 | |
| 2557 | return MEMIF_ERR_SUCCESS; |
| 2558 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2559 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2560 | ns = last_slot - cur_slot; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2561 | |
| 2562 | while (ns && count) |
| 2563 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2564 | b0 = (bufs + *rx); |
| 2565 | |
| 2566 | b0->desc_index = cur_slot; |
| 2567 | b0->data = memif_get_buffer (c, ring, cur_slot & mask); |
| 2568 | b0->len = ring->desc[cur_slot & mask].length; |
| 2569 | /* slave resets buffer length */ |
| 2570 | if (c->args.is_master == 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2571 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2572 | ring->desc[cur_slot & mask].length = c->run_args.buffer_size; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2573 | } |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2574 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2575 | if (ring->desc[cur_slot & mask].flags & MEMIF_DESC_FLAG_NEXT) |
| 2576 | { |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2577 | b0->flags |= MEMIF_BUFFER_FLAG_NEXT; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2578 | ring->desc[cur_slot & mask].flags &= ~MEMIF_DESC_FLAG_NEXT; |
| 2579 | } |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2580 | /* b0->offset = ring->desc[cur_slot & mask].offset;*/ |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2581 | b0->queue = mq; |
Jakub Grajciar | ba3c4e8 | 2017-09-18 11:21:40 +0200 | [diff] [blame] | 2582 | #ifdef MEMIF_DBG_SHM |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 2583 | printf ("data: %p\n", b0->data); |
| 2584 | printf ("index: %u\n", b0->desc_index); |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 2585 | printf ("queue: %p\n", b0->queue); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2586 | print_bytes (b0->data, b0->len, DBG_RX_BUF); |
Jakub Grajciar | ba3c4e8 | 2017-09-18 11:21:40 +0200 | [diff] [blame] | 2587 | #endif /* MEMIF_DBG_SHM */ |
| 2588 | ns--; |
| 2589 | *rx += 1; |
| 2590 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2591 | count--; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2592 | cur_slot++; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2593 | } |
| 2594 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2595 | if (c->args.is_master) |
| 2596 | mq->last_head = cur_slot; |
| 2597 | else |
| 2598 | mq->last_tail = cur_slot; |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 2599 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2600 | if (ns) |
| 2601 | { |
| 2602 | DBG ("not enough buffers!"); |
| 2603 | return MEMIF_ERR_NOBUF; |
| 2604 | } |
| 2605 | |
Jan Cavojsky | 77a95cd | 2020-03-03 16:25:58 +0100 | [diff] [blame] | 2606 | r = read (mq->int_fd, &b, sizeof (b)); |
| 2607 | if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN))) |
| 2608 | return memif_syscall_error_handler (errno); |
| 2609 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2610 | return MEMIF_ERR_SUCCESS; /* 0 */ |
| 2611 | } |
| 2612 | |
| 2613 | int |
| 2614 | memif_get_details (memif_conn_handle_t conn, memif_details_t * md, |
| 2615 | char *buf, ssize_t buflen) |
| 2616 | { |
| 2617 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 2618 | libmemif_main_t *lm; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2619 | memif_socket_t *ms; |
| 2620 | int err = MEMIF_ERR_SUCCESS, i; |
| 2621 | ssize_t l0 = 0, l1; |
| 2622 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2623 | if (c == NULL) |
| 2624 | return MEMIF_ERR_NOCONN; |
| 2625 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2626 | ms = (memif_socket_t *) c->args.socket; |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 2627 | lm = get_libmemif_main (ms); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2628 | |
| 2629 | l1 = strlen ((char *) c->args.interface_name); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2630 | if (l0 + l1 < buflen) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2631 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2632 | md->if_name = |
| 2633 | (uint8_t *) strcpy (buf + l0, (char *) c->args.interface_name); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2634 | l0 += l1 + 1; |
| 2635 | } |
| 2636 | else |
| 2637 | err = MEMIF_ERR_NOBUF_DET; |
| 2638 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2639 | l1 = strlen ((char *) lm->app_name); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2640 | if (l0 + l1 < buflen) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2641 | { |
Jakub Grajciar | 5e1f69a | 2018-04-12 13:56:29 +0200 | [diff] [blame] | 2642 | md->inst_name = (uint8_t *) strcpy (buf + l0, (char *) lm->app_name); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2643 | l0 += l1 + 1; |
| 2644 | } |
| 2645 | else |
| 2646 | err = MEMIF_ERR_NOBUF_DET; |
| 2647 | |
| 2648 | l1 = strlen ((char *) c->remote_if_name); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2649 | if (l0 + l1 < buflen) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2650 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2651 | md->remote_if_name = |
| 2652 | (uint8_t *) strcpy (buf + l0, (char *) c->remote_if_name); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2653 | l0 += l1 + 1; |
| 2654 | } |
| 2655 | else |
| 2656 | err = MEMIF_ERR_NOBUF_DET; |
| 2657 | |
| 2658 | l1 = strlen ((char *) c->remote_name); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2659 | if (l0 + l1 < buflen) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2660 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2661 | md->remote_inst_name = |
| 2662 | (uint8_t *) strcpy (buf + l0, (char *) c->remote_name); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2663 | l0 += l1 + 1; |
| 2664 | } |
| 2665 | else |
| 2666 | err = MEMIF_ERR_NOBUF_DET; |
| 2667 | |
| 2668 | md->id = c->args.interface_id; |
| 2669 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2670 | if (strlen ((char *) c->args.secret) > 0) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2671 | { |
| 2672 | l1 = strlen ((char *) c->args.secret); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2673 | if (l0 + l1 < buflen) |
| 2674 | { |
Jakub Grajciar | 5e1f69a | 2018-04-12 13:56:29 +0200 | [diff] [blame] | 2675 | md->secret = (uint8_t *) strcpy (buf + l0, (char *) c->args.secret); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2676 | l0 += l1 + 1; |
| 2677 | } |
| 2678 | else |
| 2679 | err = MEMIF_ERR_NOBUF_DET; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2680 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2681 | |
| 2682 | md->role = (c->args.is_master) ? 0 : 1; |
| 2683 | md->mode = c->args.mode; |
| 2684 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2685 | l1 = strlen ((char *) ms->filename); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2686 | if (l0 + l1 < buflen) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2687 | { |
| 2688 | md->socket_filename = |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2689 | (uint8_t *) strcpy (buf + l0, (char *) ms->filename); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2690 | l0 += l1 + 1; |
| 2691 | } |
| 2692 | else |
| 2693 | err = MEMIF_ERR_NOBUF_DET; |
| 2694 | |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2695 | l1 = strlen ((char *) c->remote_disconnect_string); |
| 2696 | if (l0 + l1 < buflen) |
| 2697 | { |
| 2698 | md->error = |
| 2699 | (uint8_t *) strcpy (buf + l0, (char *) c->remote_disconnect_string); |
| 2700 | l0 += l1 + 1; |
| 2701 | } |
| 2702 | else |
| 2703 | err = MEMIF_ERR_NOBUF_DET; |
| 2704 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2705 | md->regions_num = c->regions_num; |
| 2706 | l1 = sizeof (memif_region_details_t) * md->regions_num; |
| 2707 | if (l0 + l1 <= buflen) |
| 2708 | { |
Koichiro Den | 33331ed | 2019-01-03 00:22:24 +0900 | [diff] [blame] | 2709 | md->regions = (memif_region_details_t *) (buf + l0); |
Koichiro Den | be420e5 | 2018-12-15 00:31:34 +0900 | [diff] [blame] | 2710 | for (i = 0; i < md->regions_num; i++) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2711 | { |
| 2712 | md->regions[i].index = i; |
| 2713 | md->regions[i].addr = c->regions[i].addr; |
| 2714 | md->regions[i].size = c->regions[i].region_size; |
| 2715 | md->regions[i].fd = c->regions[i].fd; |
| 2716 | md->regions[i].is_external = c->regions[i].is_external; |
| 2717 | } |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 2718 | l0 += l1; |
| 2719 | } |
| 2720 | else |
| 2721 | err = MEMIF_ERR_NOBUF_DET; |
| 2722 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2723 | md->rx_queues_num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2724 | (c->args.is_master) ? c->run_args.num_s2m_rings : c-> |
| 2725 | run_args.num_m2s_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2726 | |
| 2727 | l1 = sizeof (memif_queue_details_t) * md->rx_queues_num; |
| 2728 | if (l0 + l1 <= buflen) |
| 2729 | { |
Koichiro Den | 33331ed | 2019-01-03 00:22:24 +0900 | [diff] [blame] | 2730 | md->rx_queues = (memif_queue_details_t *) (buf + l0); |
Koichiro Den | be420e5 | 2018-12-15 00:31:34 +0900 | [diff] [blame] | 2731 | for (i = 0; i < md->rx_queues_num; i++) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2732 | { |
| 2733 | md->rx_queues[i].region = c->rx_queues[i].region; |
| 2734 | md->rx_queues[i].qid = i; |
| 2735 | md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size); |
| 2736 | md->rx_queues[i].flags = c->rx_queues[i].ring->flags; |
| 2737 | md->rx_queues[i].head = c->rx_queues[i].ring->head; |
| 2738 | md->rx_queues[i].tail = c->rx_queues[i].ring->tail; |
| 2739 | md->rx_queues[i].buffer_size = c->run_args.buffer_size; |
| 2740 | } |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2741 | l0 += l1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2742 | } |
| 2743 | else |
| 2744 | err = MEMIF_ERR_NOBUF_DET; |
| 2745 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2746 | md->tx_queues_num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2747 | (c->args.is_master) ? c->run_args.num_m2s_rings : c-> |
| 2748 | run_args.num_s2m_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2749 | |
| 2750 | l1 = sizeof (memif_queue_details_t) * md->tx_queues_num; |
| 2751 | if (l0 + l1 <= buflen) |
| 2752 | { |
Koichiro Den | 33331ed | 2019-01-03 00:22:24 +0900 | [diff] [blame] | 2753 | md->tx_queues = (memif_queue_details_t *) (buf + l0); |
Koichiro Den | be420e5 | 2018-12-15 00:31:34 +0900 | [diff] [blame] | 2754 | for (i = 0; i < md->tx_queues_num; i++) |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2755 | { |
| 2756 | md->tx_queues[i].region = c->tx_queues[i].region; |
| 2757 | md->tx_queues[i].qid = i; |
| 2758 | md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size); |
| 2759 | md->tx_queues[i].flags = c->tx_queues[i].ring->flags; |
| 2760 | md->tx_queues[i].head = c->tx_queues[i].ring->head; |
| 2761 | md->tx_queues[i].tail = c->tx_queues[i].ring->tail; |
| 2762 | md->tx_queues[i].buffer_size = c->run_args.buffer_size; |
| 2763 | } |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 2764 | l0 += l1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2765 | } |
| 2766 | else |
| 2767 | err = MEMIF_ERR_NOBUF_DET; |
| 2768 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2769 | md->link_up_down = (c->fd > 0) ? 1 : 0; |
| 2770 | |
| 2771 | return err; /* 0 */ |
| 2772 | } |
| 2773 | |
| 2774 | int |
| 2775 | memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) |
| 2776 | { |
| 2777 | memif_connection_t *c = (memif_connection_t *) conn; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2778 | uint8_t num; |
| 2779 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2780 | *efd = -1; |
| 2781 | if (c == NULL) |
| 2782 | return MEMIF_ERR_NOCONN; |
| 2783 | if (c->fd < 0) |
| 2784 | return MEMIF_ERR_DISCONNECTED; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2785 | |
| 2786 | num = |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 2787 | (c->args.is_master) ? c->run_args.num_s2m_rings : c-> |
| 2788 | run_args.num_m2s_rings; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2789 | if (qid >= num) |
| 2790 | return MEMIF_ERR_QID; |
| 2791 | |
| 2792 | *efd = c->rx_queues[qid].int_fd; |
| 2793 | |
| 2794 | return MEMIF_ERR_SUCCESS; |
| 2795 | } |
| 2796 | |
| 2797 | int |
| 2798 | memif_cleanup () |
| 2799 | { |
| 2800 | libmemif_main_t *lm = &libmemif_main; |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 2801 | int err; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2802 | |
Jakub Grajciar | bdf3ebe | 2019-09-09 10:39:08 +0200 | [diff] [blame] | 2803 | err = memif_delete_socket ((memif_socket_handle_t *) & lm->default_socket); |
| 2804 | if (err != MEMIF_ERR_SUCCESS) |
| 2805 | return err; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2806 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2807 | if (lm->control_list) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2808 | lm->free (lm->control_list); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2809 | lm->control_list = NULL; |
| 2810 | if (lm->interrupt_list) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2811 | lm->free (lm->interrupt_list); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2812 | lm->interrupt_list = NULL; |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 2813 | if (lm->socket_list) |
| 2814 | lm->free (lm->socket_list); |
| 2815 | lm->socket_list = NULL; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2816 | if (lm->pending_list) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 2817 | lm->free (lm->pending_list); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2818 | lm->pending_list = NULL; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 2819 | if (lm->poll_cancel_fd != -1) |
| 2820 | close (lm->poll_cancel_fd); |
| 2821 | |
| 2822 | return MEMIF_ERR_SUCCESS; /* 0 */ |
| 2823 | } |
| 2824 | |
| 2825 | int |
| 2826 | memif_per_thread_cleanup (memif_per_thread_main_handle_t * pt_main) |
| 2827 | { |
| 2828 | libmemif_main_t *lm = (libmemif_main_t *) * pt_main; |
| 2829 | |
| 2830 | if (lm == NULL) |
| 2831 | return MEMIF_ERR_INVAL_ARG; |
| 2832 | |
| 2833 | /* No default socket in case of per thread */ |
| 2834 | |
| 2835 | if (lm->control_list) |
| 2836 | lm->free (lm->control_list); |
| 2837 | lm->control_list = NULL; |
| 2838 | if (lm->interrupt_list) |
| 2839 | lm->free (lm->interrupt_list); |
| 2840 | lm->interrupt_list = NULL; |
| 2841 | if (lm->socket_list) |
| 2842 | lm->free (lm->socket_list); |
| 2843 | lm->socket_list = NULL; |
| 2844 | if (lm->pending_list) |
| 2845 | lm->free (lm->pending_list); |
| 2846 | lm->pending_list = NULL; |
| 2847 | if (lm->poll_cancel_fd != -1) |
| 2848 | close (lm->poll_cancel_fd); |
| 2849 | |
| 2850 | lm->free (lm); |
| 2851 | |
| 2852 | *pt_main = NULL; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 2853 | |
| 2854 | return MEMIF_ERR_SUCCESS; /* 0 */ |
| 2855 | } |