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