blob: cb07bf6eb2fd1ae466979438525019af2b89cabc [file] [log] [blame]
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001/*
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 Marion2cdc2bd2018-04-26 18:39:56 +020018#define _GNU_SOURCE
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020019#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 Grajciar435f5002018-04-20 09:20:50 +020048#include <linux/memfd.h>
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020049
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 Grajciar568cc462018-09-05 12:11:35 +020059#define ERRLIST_LEN 40
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020060#define MAX_ERRBUF_LEN 256
61
62#if __x86_x64__
63#define MEMIF_MEMORY_BARRIER() __builtin_ia32_sfence ()
64#else
Milan Lenco0a47c992017-10-12 14:19:31 +020065#define MEMIF_MEMORY_BARRIER() __sync_synchronize ()
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020066#endif /* __x86_x64__ */
67
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020068static char memif_buf[MAX_ERRBUF_LEN];
69
70const 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 Grajciar568cc462018-09-05 12:11:35 +020074 /* MEMIF_ERR_CONNREFUSED */
75 "Connection refused",
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020076 /* MEMIF_ERR_ACCES */
Paul Vinciguerra62237662020-05-29 23:03:06 -040077 "Permission to resource denied.",
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020078 /* 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 Vinciguerra62237662020-05-29 23:03:06 -040095 "Memif connection handle does not point to existing connection",
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020096 /* 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 Vinciguerraf4fbfd62020-05-15 23:13:36 -0400111 "Not enough space for memif details in supplied buffer. String data might be malformed.",
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200112 /* 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 Vinciguerraf4fbfd62020-05-15 23:13:36 -0400123 "Slave cannot accept connection request.",
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200124 /* 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 Lenco0a47c992017-10-12 14:19:31 +0200143 "Unknown message type received on control channel. (internal error)",
144 /* MEMIF_ERR_POLL_CANCEL */
Damjan Marion6d56fa42017-11-03 12:24:37 +0100145 "Memif event polling was canceled.",
146 /* MEMIF_ERR_MAX_RING */
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200147 "Maximum log2 ring size is 15",
Jakub Grajciarab7c2b02018-03-28 10:21:05 +0200148 /* MEMIF_ERR_PRIVHDR */
149 "Private headers not supported."
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200150};
151
152#define MEMIF_ERR_UNDEFINED "undefined error"
153
154char *
155memif_strerror (int err_code)
156{
157 if (err_code >= ERRLIST_LEN)
158 {
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000159 strlcpy (memif_buf, MEMIF_ERR_UNDEFINED, sizeof (memif_buf));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200160 }
161 else
162 {
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000163 strlcpy (memif_buf, memif_errlist[err_code], sizeof (memif_buf));
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100164 memif_buf[strlen (memif_errlist[err_code])] = '\0';
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200165 }
166 return memif_buf;
167}
168
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200169uint16_t
170memif_get_version ()
171{
172 return MEMIF_VERSION;
173}
174
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100175const char *
176memif_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 Grajciar7c5c40d2017-08-30 10:13:25 +0200185#define DBG_TX_BUF (0)
186#define DBG_RX_BUF (1)
187
188#ifdef MEMIF_DBG_SHM
189static void
190print_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 Grajciarba3c4e82017-09-18 11:21:40 +0200205#endif /* MEMIF_DBG_SHM */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200206
207int
208memif_syscall_error_handler (int err_code)
209{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200210 DBG ("%s", strerror (err_code));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200211
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 Marion6d56fa42017-11-03 12:24:37 +0100222/* connection refused if master does not exist
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200223 this error would spam the user until master was created */
Jakub Grajciar568cc462018-09-05 12:11:35 +0200224/*
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200225 if (err_code == ECONNREFUSED)
226 return MEMIF_ERR_SUCCESS;
Jakub Grajciar568cc462018-09-05 12:11:35 +0200227*/
228 if (err_code == ECONNREFUSED)
229 return MEMIF_ERR_CONNREFUSED;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200230 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
243static int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100244memif_add_epoll_fd (memif_socket_t *ms, memif_fd_event_t fde, uint32_t events)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200245{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100246 if (fde.fd < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200247 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100248 DBG ("invalid fd %d", fde.fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200249 return -1;
250 }
251 struct epoll_event evt;
252 memset (&evt, 0, sizeof (evt));
253 evt.events = events;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100254 evt.data.ptr = fde.private_ctx;
255 if (epoll_ctl (ms->epfd, EPOLL_CTL_ADD, fde.fd, &evt) < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200256 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100257 DBG ("epoll_ctl: %s fd %d", strerror (errno), fde.fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200258 return -1;
259 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100260 DBG ("fd %d added to epoll", fde.fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200261 return 0;
262}
263
264static int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100265memif_mod_epoll_fd (memif_socket_t *ms, memif_fd_event_t fde, uint32_t events)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200266{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100267 if (fde.fd < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200268 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100269 DBG ("invalid fd %d", fde.fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200270 return -1;
271 }
272 struct epoll_event evt;
273 memset (&evt, 0, sizeof (evt));
274 evt.events = events;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100275 evt.data.ptr = fde.private_ctx;
276 if (epoll_ctl (ms->epfd, EPOLL_CTL_MOD, fde.fd, &evt) < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200277 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100278 DBG ("epoll_ctl: %s fd %d", strerror (errno), fde.fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200279 return -1;
280 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100281 DBG ("fd %d modified on epoll", fde.fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200282 return 0;
283}
284
285static int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100286memif_del_epoll_fd (memif_socket_t *ms, memif_fd_event_t fde)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200287{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100288 if (fde.fd < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200289 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100290 DBG ("invalid fd %d", fde.fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200291 return -1;
292 }
293 struct epoll_event evt;
294 memset (&evt, 0, sizeof (evt));
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100295 if (epoll_ctl (ms->epfd, EPOLL_CTL_DEL, fde.fd, &evt) < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200296 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100297 DBG ("epoll_ctl: %s fd %d", strerror (errno), fde.fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200298 return -1;
299 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100300 DBG ("fd %d removed from epoll", fde.fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200301 return 0;
302}
303
304int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100305memif_control_fd_update (memif_fd_event_t fde, void *private_ctx)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200306{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100307 memif_socket_t *ms = (memif_socket_t *) private_ctx;
308 int fd;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200309
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100310 if (ms == NULL)
311 return MEMIF_ERR_INVAL_ARG;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200312
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100313 if (fde.type & MEMIF_FD_EVENT_DEL)
314 return memif_del_epoll_fd (ms, fde);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200315
316 uint32_t evt = 0;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100317 if (fde.type & MEMIF_FD_EVENT_READ)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200318 evt |= EPOLLIN;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100319 if (fde.type & MEMIF_FD_EVENT_WRITE)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200320 evt |= EPOLLOUT;
321
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100322 if (fde.type & MEMIF_FD_EVENT_MOD)
323 return memif_mod_epoll_fd (ms, fde, evt);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200324
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100325 return memif_add_epoll_fd (ms, fde, evt);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200326}
327
328static void
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100329memif_control_fd_update_register (memif_socket_t *ms,
330 memif_control_fd_update_t *cb)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200331{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100332 ms->args.on_control_fd_update = cb;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200333}
334
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200335void
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100336memif_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 Grajciar93a5dd12018-08-20 14:26:32 +0200341{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100342 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 Grajciar93a5dd12018-08-20 14:26:32 +0200347}
348
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200349static void
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100350memif_alloc_register (memif_socket_t *ms, memif_alloc_t *ma)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200351{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100352 ms->args.alloc = ma;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200353}
354
355static void
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100356memif_realloc_register (memif_socket_t *ms, memif_realloc_t *mr)
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200357{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100358 ms->args.realloc = mr;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200359}
360
361static void
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100362memif_free_register (memif_socket_t *ms, memif_free_t *mf)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200363{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100364 ms->args.free = mf;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200365}
366
367static inline memif_ring_t *
368memif_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 Grajciar93a5dd12018-08-20 14:26:32 +0200373 void *p = conn->regions[0].addr;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200374 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
382int
383memif_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 Grajciar568cc462018-09-05 12:11:35 +0200390 (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->
391 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200392 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 Grajciar134f1e02021-01-04 11:10:42 +0100400int
401memif_poll_cancel_handler (memif_fd_event_type_t type, void *private_ctx)
Jakub Grajciar12df4972019-07-01 14:24:48 +0200402{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100403 return MEMIF_ERR_POLL_CANCEL;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200404}
405
406int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100407memif_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šc69655e2022-04-22 08:06:17 +0000412 if (ms->timer_fd >= 0)
413 {
414 uint64_t u64;
Daniel Béreš98533422023-01-19 10:19:27 +0100415 ssize_t __attribute__ ((unused)) r;
Daniel Bérešc69655e2022-04-22 08:06:17 +0000416 /*
417 Have to read the timer fd else it stays read-ready
418 and makes epoll_pwait() return without sleeping
419 */
Daniel Béreš98533422023-01-19 10:19:27 +0100420 r = read (ms->timer_fd, &u64, sizeof (u64));
Daniel Bérešc69655e2022-04-22 08:06:17 +0000421 }
422
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100423 /* loop ms->slave_interfaces and request connection for disconnected ones */
424 TAILQ_FOREACH (c, &ms->slave_interfaces, next)
425 {
426 /* connected or connecting */
427 if (c->control_channel != NULL)
428 continue;
429
430 /* ignore errors */
431 memif_request_connection (c);
432 }
433
434 return MEMIF_ERR_SUCCESS;
435}
436
437int
438memif_set_connection_request_timer (memif_socket_handle_t sock,
439 struct itimerspec timer)
440{
441 memif_socket_t *ms = (memif_socket_t *) sock;
442 memif_fd_event_t fde;
443 memif_fd_event_data_t *fdata;
444 void *ctx;
445
446 if (ms == NULL)
447 return MEMIF_ERR_INVAL_ARG;
448
449 if (ms->timer_fd < 0)
450 {
451 /* only create new timer if there is a valid interval */
452 if (timer.it_interval.tv_sec == 0 && timer.it_interval.tv_nsec == 0)
453 return MEMIF_ERR_SUCCESS;
454
455 /* create timerfd */
456 ms->timer_fd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK);
457 if (ms->timer_fd < 0)
458 return memif_syscall_error_handler (errno);
459
460 /* start listening for events */
461 fdata = ms->args.alloc (sizeof (*fdata));
462 fdata->event_handler = memif_connect_handler;
463 fdata->private_ctx = ms;
464
465 fde.fd = ms->timer_fd;
466 fde.type = MEMIF_FD_EVENT_READ;
467 fde.private_ctx = fdata;
468
469 ctx = ms->epfd != -1 ? ms : ms->private_ctx;
470 ms->args.on_control_fd_update (fde, ctx);
471 }
472
473 ms->args.connection_request_timer = timer;
474
475 /* arm the timer */
476 if (timerfd_settime (ms->timer_fd, 0, &ms->args.connection_request_timer,
477 NULL) < 0)
478 return memif_syscall_error_handler (errno);
479
480 return MEMIF_ERR_SUCCESS;
481}
482
483int
484memif_create_socket (memif_socket_handle_t *sock, memif_socket_args_t *args,
Jakub Grajciar12df4972019-07-01 14:24:48 +0200485 void *private_ctx)
486{
Jakub Grajciar12df4972019-07-01 14:24:48 +0200487 memif_socket_t *ms = (memif_socket_t *) * sock;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100488 memif_fd_event_t fde;
489 memif_fd_event_data_t *fdata;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200490 int i, err = MEMIF_ERR_SUCCESS;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100491 void *ctx;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200492
493 /* allocate memif_socket_t */
494 ms = NULL;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100495 if (args->alloc != NULL)
496 ms = args->alloc (sizeof (memif_socket_t));
497 else
498 ms = malloc (sizeof (memif_socket_t));
Jakub Grajciar12df4972019-07-01 14:24:48 +0200499 if (ms == NULL)
500 {
501 err = MEMIF_ERR_NOMEM;
502 goto error;
503 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100504
505 /* default values */
Jakub Grajciar12df4972019-07-01 14:24:48 +0200506 memset (ms, 0, sizeof (memif_socket_t));
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100507 ms->epfd = -1;
508 ms->listener_fd = -1;
509 ms->poll_cancel_fd = -1;
510 ms->timer_fd = -1;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200511
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100512 /* copy arguments to internal struct */
513 memcpy (&ms->args, args, sizeof (*args));
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100514 ms->private_ctx = private_ctx;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200515
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100516 if (ms->args.alloc == NULL)
517 memif_alloc_register (ms, malloc);
518 if (ms->args.realloc == NULL)
519 memif_realloc_register (ms, realloc);
520 if (ms->args.free == NULL)
521 memif_free_register (ms, free);
522
523 TAILQ_INIT (&ms->master_interfaces);
524 TAILQ_INIT (&ms->slave_interfaces);
525
526 /* FIXME: implement connection request timer */
527
528 /* initialize internal epoll */
529 if (ms->args.on_control_fd_update == NULL)
Jakub Grajciar12df4972019-07-01 14:24:48 +0200530 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100531 ms->epfd = epoll_create (1);
532 /* register default fd update callback */
533 memif_control_fd_update_register (ms, memif_control_fd_update);
534 ms->poll_cancel_fd = eventfd (0, EFD_NONBLOCK);
535 if (ms->poll_cancel_fd < 0)
536 {
537 err = errno;
538 DBG ("eventfd: %s", strerror (err));
539 return memif_syscall_error_handler (err);
540 }
541 /* add interrupt fd to epfd */
542 fdata = ms->args.alloc (sizeof (*fdata));
543 fdata->event_handler = memif_poll_cancel_handler;
544 fdata->private_ctx = ms;
545
546 fde.fd = ms->poll_cancel_fd;
547 fde.type = MEMIF_FD_EVENT_READ;
548 fde.private_ctx = fdata;
549
550 ctx = ms->epfd != -1 ? ms : ms->private_ctx;
551 ms->args.on_control_fd_update (fde, ctx);
Jakub Grajciar12df4972019-07-01 14:24:48 +0200552 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100553
554 err =
555 memif_set_connection_request_timer (ms, ms->args.connection_request_timer);
556 if (err != MEMIF_ERR_SUCCESS)
557 goto error;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200558
559 *sock = ms;
560
561 return err;
562
563error:
564 if (ms != NULL)
565 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100566 ms->args.free (ms);
567 if (ms->epfd != -1)
568 close (ms->epfd);
569 if (ms->poll_cancel_fd != -1)
570 close (ms->poll_cancel_fd);
Jakub Grajciar12df4972019-07-01 14:24:48 +0200571 }
572 return err;
573}
574
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100575memif_socket_handle_t
576memif_get_socket_handle (memif_conn_handle_t conn)
577{
578 memif_connection_t *c = (memif_connection_t *) conn;
579
580 if (c == NULL)
581 return NULL;
582
583 return c->args.socket;
584}
585
586const char *
587memif_get_socket_path (memif_socket_handle_t sock)
588{
589 memif_socket_t *ms = (memif_socket_t *) sock;
590
591 if (ms == NULL)
592 return NULL;
593
594 return ms->args.path;
595}
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200596
597int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100598memif_get_listener_fd (memif_socket_handle_t sock)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200599{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100600 memif_socket_t *ms = (memif_socket_t *) sock;
601
602 if (ms == NULL)
603 return -1;
604
605 return ms->listener_fd;
606}
607
608int
609memif_set_listener_fd (memif_socket_handle_t sock, int fd)
610{
611 memif_socket_t *ms = (memif_socket_t *) sock;
612 memif_fd_event_t fde;
613 memif_fd_event_data_t *fdata;
614 void *ctx;
615
616 if ((ms == NULL) || (fd < 0))
617 return MEMIF_ERR_INVAL_ARG;
618
619 fdata = ms->args.alloc (sizeof (*fdata));
620 if (fdata == NULL)
621 return MEMIF_ERR_NOMEM;
622
623 ms->listener_fd = fd;
624
625 fdata->event_handler = memif_listener_handler;
626 fdata->private_ctx = ms;
627 ctx = ms->epfd != -1 ? ms : ms->private_ctx;
628 /* send fd to epoll */
629 fde.fd = ms->listener_fd;
630 fde.type = MEMIF_FD_EVENT_READ;
631 fde.private_ctx = fdata;
632 ms->args.on_control_fd_update (fde, ctx);
633
634 return MEMIF_ERR_SUCCESS;
635}
636
637int
638memif_create (memif_conn_handle_t *c, memif_conn_args_t *args,
639 memif_connection_update_t *on_connect,
640 memif_connection_update_t *on_disconnect,
641 memif_on_interrupt_t *on_interrupt, void *private_ctx)
642{
Jakub Grajciar12df4972019-07-01 14:24:48 +0200643 int err, index = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200644 memif_connection_t *conn = (memif_connection_t *) * c;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100645 memif_socket_t *ms = (memif_socket_t *) args->socket;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200646
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200647 if (conn != NULL)
648 {
649 DBG ("This handle already points to existing memif.");
650 return MEMIF_ERR_CONN;
651 }
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200652
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100653 if (ms == NULL)
654 {
655 DBG ("Missing memif socket");
656 return MEMIF_ERR_INVAL_ARG;
657 }
658
659 conn = (memif_connection_t *) ms->args.alloc (sizeof (*conn));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200660 if (conn == NULL)
661 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200662 err = MEMIF_ERR_NOMEM;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200663 goto error;
664 }
665 memset (conn, 0, sizeof (memif_connection_t));
666
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200667 conn->args.interface_id = args->interface_id;
668
669 if (args->log2_ring_size == 0)
670 args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE;
Damjan Marion6d56fa42017-11-03 12:24:37 +0100671 else if (args->log2_ring_size > MEMIF_MAX_LOG2_RING_SIZE)
672 {
673 err = MEMIF_ERR_MAX_RING;
674 goto error;
675 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200676 if (args->buffer_size == 0)
677 args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
678 if (args->num_s2m_rings == 0)
679 args->num_s2m_rings = MEMIF_DEFAULT_TX_QUEUES;
680 if (args->num_m2s_rings == 0)
681 args->num_m2s_rings = MEMIF_DEFAULT_RX_QUEUES;
682
683 conn->args.num_s2m_rings = args->num_s2m_rings;
684 conn->args.num_m2s_rings = args->num_m2s_rings;
685 conn->args.buffer_size = args->buffer_size;
686 conn->args.log2_ring_size = args->log2_ring_size;
687 conn->args.is_master = args->is_master;
688 conn->args.mode = args->mode;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100689 conn->args.socket = args->socket;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200690 conn->regions = NULL;
691 conn->tx_queues = NULL;
692 conn->rx_queues = NULL;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100693 conn->control_channel = NULL;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200694 conn->on_connect = on_connect;
695 conn->on_disconnect = on_disconnect;
696 conn->on_interrupt = on_interrupt;
697 conn->private_ctx = private_ctx;
698 memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t));
699
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100700 uint8_t l = sizeof (conn->args.interface_name);
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000701 strlcpy ((char *) conn->args.interface_name, (char *) args->interface_name,
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100702 l);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200703
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100704 if ((l = strlen ((char *) args->secret)) > 0)
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000705 strlcpy ((char *) conn->args.secret, (char *) args->secret,
706 sizeof (conn->args.secret));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200707
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100708 if (args->is_master)
709 TAILQ_INSERT_TAIL (&ms->master_interfaces, conn, next);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200710 else
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100711 TAILQ_INSERT_TAIL (&ms->slave_interfaces, conn, next);
712
713 err = memif_request_connection (conn);
714 if (err != MEMIF_ERR_SUCCESS && err != MEMIF_ERR_CONNREFUSED)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200715 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100716 if (args->is_master)
717 TAILQ_REMOVE (&ms->master_interfaces, conn, next);
718 else
719 TAILQ_REMOVE (&ms->slave_interfaces, conn, next);
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200720 goto error;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200721 }
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200722
Jakub Grajciar12df4972019-07-01 14:24:48 +0200723 *c = conn;
724
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200725 return 0;
726
727error:
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200728 if (conn != NULL)
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100729 ms->args.free (conn);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200730 *c = conn = NULL;
731 return err;
732}
733
Nathan Skrzypczak814eee52021-05-07 19:39:07 +0200734static inline int
735memif_path_is_abstract (const char *filename)
736{
737 return (filename[0] == '@');
738}
739
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200740int
Jakub Grajciar12df4972019-07-01 14:24:48 +0200741memif_request_connection (memif_conn_handle_t c)
Jakub Grajciar84b83772019-03-04 12:42:19 +0100742{
Jakub Grajciar84b83772019-03-04 12:42:19 +0100743 memif_connection_t *conn = (memif_connection_t *) c;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200744 memif_socket_t *ms;
Jakub Grajciar84b83772019-03-04 12:42:19 +0100745 int err = MEMIF_ERR_SUCCESS;
746 int sockfd = -1;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100747 struct sockaddr_un un = { 0 };
748 struct stat file_stat;
749 int on = 1;
750 memif_control_channel_t *cc = NULL;
751 memif_fd_event_t fde;
752 memif_fd_event_data_t *fdata = NULL;
Nathan Skrzypczak814eee52021-05-07 19:39:07 +0200753 int sunlen = sizeof (un);
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100754 void *ctx;
Jakub Grajciar84b83772019-03-04 12:42:19 +0100755
Jakub Grajciar12df4972019-07-01 14:24:48 +0200756 if (conn == NULL)
757 return MEMIF_ERR_NOCONN;
758
759 ms = (memif_socket_t *) conn->args.socket;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +0200760
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100761 /* if control channel is assigned, the interface is either connected or
762 * connecting */
763 if (conn->control_channel != NULL)
Jakub Grajciar84b83772019-03-04 12:42:19 +0100764 return MEMIF_ERR_ALRCONN;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100765 /* if interface is master and the socket is already listener we are done */
766 if (conn->args.is_master && (ms->listener_fd != -1))
767 return MEMIF_ERR_SUCCESS;
Jakub Grajciar84b83772019-03-04 12:42:19 +0100768
769 sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
770 if (sockfd < 0)
771 {
772 err = memif_syscall_error_handler (errno);
773 goto error;
774 }
775
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100776 un.sun_family = AF_UNIX;
Jakub Grajciar84b83772019-03-04 12:42:19 +0100777
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100778 /* use memcpy to support abstract socket
779 * ms->args.path is already a valid socket path
780 */
781 memcpy (un.sun_path, ms->args.path, sizeof (un.sun_path) - 1);
Jakub Grajciar84b83772019-03-04 12:42:19 +0100782
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100783 /* allocate fd event data */
784 fdata = ms->args.alloc (sizeof (*fdata));
785 if (fdata == NULL)
Jakub Grajciar84b83772019-03-04 12:42:19 +0100786 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100787 err = MEMIF_ERR_NOMEM;
Jakub Grajciar84b83772019-03-04 12:42:19 +0100788 goto error;
789 }
790
Nathan Skrzypczak814eee52021-05-07 19:39:07 +0200791 if (memif_path_is_abstract (ms->args.path))
792 {
793 /* Ensure the string is NULL terminated */
794 un.sun_path[sizeof (un.sun_path) - 1] = '\0';
795 /* sunlen is strlen(un.sun_path) + sizeof(un.sun_family) */
796 sunlen = strlen (un.sun_path) + (sizeof (un) - sizeof (un.sun_path));
797 /* Handle abstract socket by converting '@' -> '\0' */
798 un.sun_path[0] = '\0';
799 }
800
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100801 if (conn->args.is_master != 0)
802 {
803 /* Configure socket optins */
804 if (setsockopt (sockfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0)
805 {
806 err = memif_syscall_error_handler (errno);
807 goto error;
808 }
Nathan Skrzypczak814eee52021-05-07 19:39:07 +0200809 if (bind (sockfd, (struct sockaddr *) &un, sunlen) < 0)
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100810 {
811 err = memif_syscall_error_handler (errno);
812 goto error;
813 }
814 if (listen (sockfd, 1) < 0)
815 {
816 err = memif_syscall_error_handler (errno);
817 goto error;
818 }
Nathan Skrzypczak814eee52021-05-07 19:39:07 +0200819 if (!memif_path_is_abstract (ms->args.path))
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100820 {
821 /* Verify that the socket was created */
822 if (stat ((char *) ms->args.path, &file_stat) < 0)
823 {
824 err = memif_syscall_error_handler (errno);
825 goto error;
826 }
827 }
828
829 /* assign listener fd */
830 ms->listener_fd = sockfd;
831
832 fdata->event_handler = memif_listener_handler;
833 fdata->private_ctx = ms;
834 }
835 else
836 {
837 cc = ms->args.alloc (sizeof (*cc));
838 if (cc == NULL)
839 {
840 err = MEMIF_ERR_NOMEM;
841 goto error;
842 }
Nathan Skrzypczak814eee52021-05-07 19:39:07 +0200843 if (connect (sockfd, (struct sockaddr *) &un, sunlen) != 0)
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100844 {
845 err = MEMIF_ERR_CONNREFUSED;
846 goto error;
847 }
848
849 /* Create control channel */
850 cc->fd = sockfd;
851 cc->sock = ms;
852 cc->conn = conn;
853 TAILQ_INIT (&cc->msg_queue);
854
855 /* assign control channel to endpoint */
856 conn->control_channel = cc;
857
858 fdata->event_handler = memif_control_channel_handler;
859 fdata->private_ctx = cc;
860 }
861
862 /* if event polling is done internally, send memif socket as context */
863 ctx = ms->epfd != -1 ? ms : ms->private_ctx;
864 /* send fd to epoll */
865 fde.fd = sockfd;
866 fde.type = MEMIF_FD_EVENT_READ;
867 fde.private_ctx = fdata;
868 ms->args.on_control_fd_update (fde, ctx);
Jakub Grajciar12df4972019-07-01 14:24:48 +0200869
870 return err;
Jakub Grajciar84b83772019-03-04 12:42:19 +0100871
872error:
873 if (sockfd > 0)
874 close (sockfd);
875 sockfd = -1;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100876 if (fdata != NULL)
877 ms->args.free (fdata);
878 fdata = NULL;
879 if (cc != NULL)
880 ms->args.free (cc);
881 conn->control_channel = cc = NULL;
Jakub Grajciar84b83772019-03-04 12:42:19 +0100882 return err;
883}
884
885int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100886memif_control_fd_handler (void *ptr, memif_fd_event_type_t events)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200887{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100888 memif_fd_event_data_t *fdata = (memif_fd_event_data_t *) ptr;
msardara8f554b72018-12-11 18:36:55 +0100889
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100890 if (fdata == NULL)
891 return MEMIF_ERR_INVAL_ARG;
msardara8f554b72018-12-11 18:36:55 +0100892
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100893 return fdata->event_handler (events, fdata->private_ctx);
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200894}
895
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100896int
897memif_interrupt_handler (memif_fd_event_type_t type, void *private_ctx)
898{
899 memif_interrupt_t *idata = (memif_interrupt_t *) private_ctx;
900
901 if (idata == NULL)
902 return MEMIF_ERR_INVAL_ARG;
903
904 return idata->c->on_interrupt (idata->c, idata->c->private_ctx, idata->qid);
905}
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200906
907int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100908memif_poll_event (memif_socket_handle_t sock, int timeout)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200909{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100910 memif_socket_t *ms = (memif_socket_t *) sock;
msardara8f554b72018-12-11 18:36:55 +0100911 struct epoll_event evt;
912 int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100913 memif_fd_event_type_t events = 0;
Milan Lenco0a47c992017-10-12 14:19:31 +0200914 uint64_t counter = 0;
915 ssize_t r = 0;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100916 sigset_t sigset;
917
918 if (ms == NULL)
919 return MEMIF_ERR_INVAL_ARG;
920
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200921 memset (&evt, 0, sizeof (evt));
922 evt.events = EPOLLIN | EPOLLOUT;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200923 sigemptyset (&sigset);
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100924 en = epoll_pwait (ms->epfd, &evt, 1, timeout, &sigset);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200925 if (en < 0)
926 {
Milan Lenco0a47c992017-10-12 14:19:31 +0200927 err = errno;
928 DBG ("epoll_pwait: %s", strerror (err));
929 return memif_syscall_error_handler (err);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200930 }
931 if (en > 0)
932 {
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200933 if (evt.events & EPOLLIN)
934 events |= MEMIF_FD_EVENT_READ;
935 if (evt.events & EPOLLOUT)
936 events |= MEMIF_FD_EVENT_WRITE;
937 if (evt.events & EPOLLERR)
938 events |= MEMIF_FD_EVENT_ERROR;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100939 return memif_control_fd_handler (evt.data.ptr, events);
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200940 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100941 return MEMIF_ERR_SUCCESS;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200942}
943
944int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100945memif_cancel_poll_event (memif_socket_handle_t sock)
Milan Lenco0a47c992017-10-12 14:19:31 +0200946{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100947 memif_socket_t *ms = (memif_socket_t *) sock;
Milan Lenco0a47c992017-10-12 14:19:31 +0200948 uint64_t counter = 1;
949 ssize_t w = 0;
950
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100951 if (ms->poll_cancel_fd == -1)
952 return MEMIF_ERR_INVAL_ARG;
953 w = write (ms->poll_cancel_fd, &counter, sizeof (counter));
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200954 if (w < sizeof (counter))
955 return MEMIF_ERR_INT_WRITE;
956
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100957 return MEMIF_ERR_SUCCESS;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200958}
959
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100960void
961memif_close_queues (memif_socket_t *ms, memif_queue_t *queues, int nqueues)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200962{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100963 memif_fd_event_t fde;
964 memif_queue_t *mq;
965 void *ctx;
966
967 int i;
968 for (i = 0; i < nqueues; i++)
969 {
970 mq = &queues[i];
971 if (mq != NULL)
972 {
973 if (mq->int_fd > 0)
974 {
975 /* Stop listening for events */
976 fde.fd = mq->int_fd;
977 fde.type = MEMIF_FD_EVENT_DEL;
978 ctx = ms->epfd != -1 ? ms : ms->private_ctx;
979 ms->args.on_control_fd_update (fde, ctx);
980 close (mq->int_fd);
981 }
982 mq->int_fd = -1;
983 }
984 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200985}
986
987/* send disconnect msg and close interface */
988int
989memif_disconnect_internal (memif_connection_t * c)
990{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200991 int err = MEMIF_ERR_SUCCESS, i; /* 0 */
992 memif_queue_t *mq;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100993 memif_socket_t *ms = (memif_socket_t *) c->args.socket;
994 memif_fd_event_t fde;
995 void *ctx;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +0200996
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200997 c->on_disconnect ((void *) c, c->private_ctx);
998
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100999 /* Delete control channel */
1000 if (c->control_channel != NULL)
1001 memif_delete_control_channel (c->control_channel);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001002
1003 if (c->tx_queues != NULL)
1004 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001005 memif_close_queues (ms, c->tx_queues, c->tx_queues_num);
1006 ms->args.free (c->tx_queues);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001007 c->tx_queues = NULL;
1008 }
Jakub Grajciar718a19b2019-11-05 10:58:00 +01001009 c->tx_queues_num = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001010
1011 if (c->rx_queues != NULL)
1012 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001013 memif_close_queues (ms, c->rx_queues, c->rx_queues_num);
1014 ms->args.free (c->rx_queues);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001015 c->rx_queues = NULL;
1016 }
Jakub Grajciar718a19b2019-11-05 10:58:00 +01001017 c->rx_queues_num = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001018
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001019 /* TODO: Slave reuse regions */
1020
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001021 for (i = 0; i < c->regions_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001022 {
Jakub Grajciar412e7b62018-09-27 10:26:35 +02001023 if (&c->regions[i] == NULL)
1024 continue;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001025 if (c->regions[i].is_external != 0)
1026 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001027 ms->del_external_region (c->regions[i].addr,
1028 c->regions[i].region_size, c->regions[i].fd,
1029 c->private_ctx);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001030 }
1031 else
1032 {
1033 if (munmap (c->regions[i].addr, c->regions[i].region_size) < 0)
1034 return memif_syscall_error_handler (errno);
1035 if (c->regions[i].fd > 0)
1036 close (c->regions[i].fd);
1037 c->regions[i].fd = -1;
1038 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001039 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001040 ms->args.free (c->regions);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001041 c->regions = NULL;
1042 c->regions_num = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001043
1044 memset (&c->run_args, 0, sizeof (memif_conn_run_args_t));
1045
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001046 return err;
1047}
1048
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001049const char *
1050memif_get_socket_filename (memif_socket_handle_t sock)
1051{
1052 memif_socket_t *ms = (memif_socket_t *) sock;
1053
1054 if (ms == NULL)
1055 return NULL;
1056
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001057 return (char *) ms->args.path;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +02001058}
1059
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001060int
Jakub Grajciar12df4972019-07-01 14:24:48 +02001061memif_delete_socket (memif_socket_handle_t * sock)
1062{
1063 memif_socket_t *ms = (memif_socket_t *) * sock;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001064 memif_fd_event_t fde;
1065 void *ctx;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001066
1067 /* check if socket is in use */
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001068 if (ms == NULL || !TAILQ_EMPTY (&ms->master_interfaces) ||
1069 !TAILQ_EMPTY (&ms->slave_interfaces))
Jakub Grajciar12df4972019-07-01 14:24:48 +02001070 return MEMIF_ERR_INVAL_ARG;
1071
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001072 if (ms->listener_fd > 0)
1073 {
1074 fde.fd = ms->listener_fd;
1075 fde.type = MEMIF_FD_EVENT_DEL;
1076 ctx = ms->epfd != -1 ? ms : ms->private_ctx;
1077 ms->args.on_control_fd_update (fde, ctx);
1078 }
1079 ms->listener_fd = -1;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001080
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001081 if (ms->poll_cancel_fd > 0)
1082 {
1083 fde.fd = ms->poll_cancel_fd;
1084 fde.type = MEMIF_FD_EVENT_DEL;
1085 ctx = ms->epfd != -1 ? ms : ms->private_ctx;
1086 ms->args.on_control_fd_update (fde, ctx);
1087 }
1088 ms->poll_cancel_fd = -1;
1089
1090 if (ms->epfd > 0)
1091 close (ms->epfd);
1092 ms->epfd = -1;
1093
1094 ms->args.free (ms);
Jakub Grajciar12df4972019-07-01 14:24:48 +02001095 *sock = ms = NULL;
1096
1097 return MEMIF_ERR_SUCCESS;
1098}
1099
1100int
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001101memif_delete (memif_conn_handle_t * conn)
1102{
1103 memif_connection_t *c = (memif_connection_t *) * conn;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001104 memif_socket_t *ms;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001105 int err = MEMIF_ERR_SUCCESS;
1106
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001107 if (c == NULL)
1108 {
1109 DBG ("no connection");
1110 return MEMIF_ERR_NOCONN;
1111 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001112
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001113 err = memif_disconnect_internal (c);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001114
Jakub Grajciar12df4972019-07-01 14:24:48 +02001115 ms = (memif_socket_t *) c->args.socket;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001116
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001117 if (c->args.is_master)
1118 TAILQ_REMOVE (&ms->master_interfaces, c, next);
1119 else
1120 TAILQ_REMOVE (&ms->slave_interfaces, c, next);
1121 /* TODO: don't listen with empty interface queue */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001122
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001123 ms->args.free (c);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001124 c = NULL;
1125
1126 *conn = c;
1127 return err;
1128}
1129
1130int
1131memif_connect1 (memif_connection_t * c)
1132{
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001133 memif_socket_t *ms;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001134 memif_region_t *mr;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001135 memif_queue_t *mq;
1136 int i;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001137
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001138 if (c == NULL)
1139 return MEMIF_ERR_INVAL_ARG;
1140
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001141 ms = (memif_socket_t *) c->args.socket;
Jakub Grajciarbdf3ebe2019-09-09 10:39:08 +02001142
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001143 for (i = 0; i < c->regions_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001144 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001145 mr = &c->regions[i];
1146 if (mr != NULL)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001147 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001148 if (!mr->addr)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001149 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001150 if (mr->is_external)
1151 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001152 if (ms->get_external_region_addr == NULL)
Jakub Grajciar12df4972019-07-01 14:24:48 +02001153 return MEMIF_ERR_INVAL_ARG;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001154 mr->addr = ms->get_external_region_addr (
1155 mr->region_size, mr->fd, c->private_ctx);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001156 }
1157 else
1158 {
1159 if (mr->fd < 0)
1160 return MEMIF_ERR_NO_SHMFD;
1161
1162 if ((mr->addr =
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001163 mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE,
1164 MAP_SHARED, mr->fd, 0)) == MAP_FAILED)
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001165 {
1166 return memif_syscall_error_handler (errno);
1167 }
1168 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001169 }
1170 }
1171 }
1172
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001173 for (i = 0; i < c->rx_queues_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001174 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001175 mq = &c->rx_queues[i];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001176 if (mq != NULL)
1177 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001178 mq->ring = c->regions[mq->region].addr + mq->offset;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001179 if (mq->ring->cookie != MEMIF_COOKIE)
1180 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001181 DBG ("wrong cookie on rx ring %u", i);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001182 return MEMIF_ERR_COOKIE;
1183 }
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001184 mq->ring->head = mq->ring->tail = mq->last_head = mq->next_buf = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001185 }
1186 }
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001187
1188 for (i = 0; i < c->tx_queues_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001189 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001190 mq = &c->tx_queues[i];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001191 if (mq != NULL)
1192 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001193 mq->ring = c->regions[mq->region].addr + mq->offset;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001194 if (mq->ring->cookie != MEMIF_COOKIE)
1195 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001196 DBG ("wrong cookie on tx ring %u", i);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001197 return MEMIF_ERR_COOKIE;
1198 }
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001199 mq->ring->head = mq->ring->tail = mq->last_head = mq->next_buf = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001200 }
1201 }
1202
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001203 return 0;
1204}
1205
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001206static inline int
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001207memif_add_region (memif_connection_t *conn, uint8_t has_buffers)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001208{
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001209 memif_region_t *r;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001210 memif_socket_t *ms = (memif_socket_t *) conn->args.socket;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001211
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001212 r = ms->args.realloc (conn->regions,
1213 sizeof (memif_region_t) * ++conn->regions_num);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001214 if (r == NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001215 return MEMIF_ERR_NOMEM;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001216
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001217 conn->regions = r;
1218 r = &conn->regions[conn->regions_num - 1];
1219 memset (r, 0, sizeof (memif_region_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001220
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001221 if (has_buffers != 0)
1222 {
1223 r->buffer_offset = 0;
1224 }
1225 else
1226 {
1227 r->buffer_offset =
1228 (conn->run_args.num_s2m_rings +
1229 conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) +
1230 sizeof (memif_desc_t) *
Jakub Grajciar568cc462018-09-05 12:11:35 +02001231 (1 << conn->
1232 run_args.log2_ring_size));
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001233 }
1234
1235 r->region_size = (has_buffers == 0) ? r->buffer_offset : r->buffer_offset +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001236 conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
1237 (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
1238
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001239 if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001240 return memif_syscall_error_handler (errno);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001241
1242 if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
1243 return memif_syscall_error_handler (errno);
1244
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001245 if ((ftruncate (r->fd, r->region_size)) == -1)
1246 return memif_syscall_error_handler (errno);
1247
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001248 if ((r->addr = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE,
1249 MAP_SHARED, r->fd, 0)) == MAP_FAILED)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001250 return memif_syscall_error_handler (errno);
1251
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001252 return MEMIF_ERR_SUCCESS;
1253}
1254
1255static inline int
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001256memif_init_queues (memif_connection_t *conn)
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001257{
1258 int i, j;
1259 memif_ring_t *ring;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001260 memif_socket_t *ms = (memif_socket_t *) conn->args.socket;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001261
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001262 for (i = 0; i < conn->run_args.num_s2m_rings; i++)
1263 {
1264 ring = memif_get_ring (conn, MEMIF_RING_S2M, i);
1265 DBG ("RING: %p I: %d", ring, i);
1266 ring->head = ring->tail = 0;
1267 ring->cookie = MEMIF_COOKIE;
1268 ring->flags = 0;
1269 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1270 {
1271 uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001272 ring->desc[j].region = 1;
1273 ring->desc[j].offset =
1274 conn->regions[1].buffer_offset +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001275 (uint32_t) (slot * conn->run_args.buffer_size);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001276 ring->desc[j].length = conn->run_args.buffer_size;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001277 }
1278 }
1279 for (i = 0; i < conn->run_args.num_m2s_rings; i++)
1280 {
1281 ring = memif_get_ring (conn, MEMIF_RING_M2S, i);
1282 DBG ("RING: %p I: %d", ring, i);
1283 ring->head = ring->tail = 0;
1284 ring->cookie = MEMIF_COOKIE;
1285 ring->flags = 0;
1286 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1287 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001288 uint16_t slot = (i + conn->run_args.num_s2m_rings) *
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001289 (1 << conn->run_args.log2_ring_size) + j;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001290 ring->desc[j].region = 1;
1291 ring->desc[j].offset =
1292 conn->regions[1].buffer_offset +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001293 (uint32_t) (slot * conn->run_args.buffer_size);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001294 ring->desc[j].length = conn->run_args.buffer_size;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001295 }
1296 }
1297 memif_queue_t *mq;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001298 mq = (memif_queue_t *) ms->args.alloc (sizeof (memif_queue_t) *
1299 conn->run_args.num_s2m_rings);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001300 if (mq == NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001301 return MEMIF_ERR_NOMEM;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001302
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001303 int x;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001304
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001305 for (x = 0; x < conn->run_args.num_s2m_rings; x++)
1306 {
1307 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1308 return memif_syscall_error_handler (errno);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001309
1310 mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x);
1311 DBG ("RING: %p I: %d", mq[x].ring, x);
1312 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1313 mq[x].region = 0;
1314 mq[x].offset =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001315 (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
1316 mq[x].last_head = mq[x].last_tail = 0;
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001317 mq[x].next_buf = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001318 }
1319 conn->tx_queues = mq;
Jakub Grajciar718a19b2019-11-05 10:58:00 +01001320 conn->tx_queues_num = conn->run_args.num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001321
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001322 mq = (memif_queue_t *) ms->args.alloc (sizeof (memif_queue_t) *
1323 conn->run_args.num_m2s_rings);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001324 if (mq == NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001325 return MEMIF_ERR_NOMEM;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001326
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001327 for (x = 0; x < conn->run_args.num_m2s_rings; x++)
1328 {
1329 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1330 return memif_syscall_error_handler (errno);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001331
1332 mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x);
1333 DBG ("RING: %p I: %d", mq[x].ring, x);
1334 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1335 mq[x].region = 0;
1336 mq[x].offset =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001337 (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
1338 mq[x].last_head = mq[x].last_tail = 0;
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001339 mq[x].next_buf = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001340 }
1341 conn->rx_queues = mq;
Jakub Grajciar718a19b2019-11-05 10:58:00 +01001342 conn->rx_queues_num = conn->run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001343
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001344 return MEMIF_ERR_SUCCESS;
1345}
1346
1347int
1348memif_init_regions_and_queues (memif_connection_t * conn)
1349{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001350 memif_region_t *r;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001351 memif_socket_t *ms = (memif_socket_t *) conn->args.socket;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001352
1353 /* region 0. rings */
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001354 memif_add_region (conn, /* has_buffers */ 0);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001355
1356 /* region 1. buffers */
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001357 if (ms->add_external_region)
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001358 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001359 r = (memif_region_t *) ms->args.realloc (
1360 conn->regions, sizeof (memif_region_t) * ++conn->regions_num);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001361 if (r == NULL)
1362 return MEMIF_ERR_NOMEM;
1363 conn->regions = r;
1364
1365 conn->regions[1].region_size =
1366 conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
1367 (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
1368 conn->regions[1].buffer_offset = 0;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001369 ms->add_external_region (&conn->regions[1].addr,
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001370 conn->regions[1].region_size,
1371 &conn->regions[1].fd, conn->private_ctx);
1372 conn->regions[1].is_external = 1;
1373 }
1374 else
1375 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001376 memif_add_region (conn, 1);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001377 }
1378
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001379 memif_init_queues (conn);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001380
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001381 return 0;
1382}
1383
Jakub Grajciar47e68de2021-01-08 15:32:43 +01001384int
1385memif_set_next_free_buffer (memif_conn_handle_t conn, uint16_t qid,
1386 memif_buffer_t *buf)
1387{
1388 memif_connection_t *c = (memif_connection_t *) conn;
1389 if (EXPECT_FALSE (c == NULL))
1390 return MEMIF_ERR_NOCONN;
1391 if (EXPECT_FALSE (qid >= c->tx_queues_num))
1392 return MEMIF_ERR_QID;
1393 if (EXPECT_FALSE (buf == NULL))
1394 return MEMIF_ERR_INVAL_ARG;
1395
1396 uint16_t ring_size, ns;
1397 memif_queue_t *mq = &c->tx_queues[qid];
1398 memif_ring_t *ring = mq->ring;
1399
1400 ring_size = (1 << mq->log2_ring_size);
1401 if (c->args.is_master)
1402 ns = ring->head - mq->next_buf;
1403 else
1404 ns = ring_size - mq->next_buf + ring->tail;
1405
1406 if ((mq->next_buf - buf->desc_index) > ns)
1407 return MEMIF_ERR_INVAL_ARG;
1408
1409 mq->next_buf = buf->desc_index;
1410
1411 return MEMIF_ERR_SUCCESS;
1412}
1413
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001414static void
1415memif_buffer_enq_at_idx_internal (memif_queue_t *from_q, memif_queue_t *to_q,
1416 memif_buffer_t *buf, uint16_t slot)
1417{
1418 uint16_t from_mask = (1 << from_q->log2_ring_size) - 1;
1419 uint16_t to_mask = (1 << to_q->log2_ring_size) - 1;
1420 memif_desc_t *from_d, *to_d, tmp_d;
1421
1422 /* Get the descriptors */
1423 from_d = &from_q->ring->desc[buf->desc_index & from_mask];
1424 to_d = &to_q->ring->desc[slot & to_mask];
1425
1426 /* Swap descriptors */
1427 tmp_d = *from_d;
1428 *from_d = *to_d;
1429 *to_d = tmp_d;
1430
1431 /* Update descriptor index and queue for clients buffer */
1432 buf->desc_index = slot;
1433 buf->queue = to_q;
1434}
1435
1436int
1437memif_buffer_requeue (memif_conn_handle_t conn, memif_buffer_t *buf_a,
1438 memif_buffer_t *buf_b)
1439{
1440 memif_connection_t *c = (memif_connection_t *) conn;
1441 if (EXPECT_FALSE (c == NULL))
1442 return MEMIF_ERR_NOCONN;
1443 if (EXPECT_FALSE (c->args.is_master))
1444 return MEMIF_ERR_INVAL_ARG;
1445 if ((buf_a == NULL) || (buf_b == NULL))
1446 return MEMIF_ERR_INVAL_ARG;
1447
1448 int err;
1449 /* store buf_a information */
1450 uint16_t index_a = buf_a->desc_index;
1451 memif_queue_t *mq_a = buf_a->queue;
1452
1453 /* swap buffers, buf_a was updated with new desc_index and queue */
1454 memif_buffer_enq_at_idx_internal ((memif_queue_t *) buf_a->queue,
1455 (memif_queue_t *) buf_b->queue, buf_a,
1456 buf_b->desc_index);
1457
1458 /* update buf_b desc_index and queue */
1459 buf_b->desc_index = index_a;
1460 buf_b->queue = mq_a;
1461
1462 return MEMIF_ERR_SUCCESS;
1463}
1464
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001465int
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001466memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid,
1467 memif_buffer_t * bufs, uint16_t count,
1468 uint16_t * count_out)
1469{
1470 memif_connection_t *c = (memif_connection_t *) conn;
1471 if (EXPECT_FALSE (c == NULL))
1472 return MEMIF_ERR_NOCONN;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001473 if (EXPECT_FALSE (c->control_channel == NULL))
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001474 return MEMIF_ERR_DISCONNECTED;
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001475 if (EXPECT_FALSE (qid >= c->tx_queues_num))
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001476 return MEMIF_ERR_QID;
1477 if (EXPECT_FALSE (!count_out))
1478 return MEMIF_ERR_INVAL_ARG;
1479 if (EXPECT_FALSE (c->args.is_master))
1480 return MEMIF_ERR_INVAL_ARG;
1481
1482 memif_queue_t *mq = &c->tx_queues[qid];
1483 memif_ring_t *ring = mq->ring;
1484 memif_buffer_t *b0;
1485 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1486 uint16_t ring_size;
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001487 uint16_t ns;
1488 memif_queue_t *bmq;
msardara8f554b72018-12-11 18:36:55 +01001489 int err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001490 *count_out = 0;
1491
1492 ring_size = (1 << mq->log2_ring_size);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001493
1494 /* can only be called by slave */
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001495 ns = ring_size - mq->next_buf + ring->tail;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001496
1497 b0 = bufs;
1498
1499 while (count && ns)
1500 {
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001501 /* Swaps the descriptors, updates next_buf pointer and updates client
1502 * memif buffer */
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001503
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001504 memif_buffer_enq_at_idx_internal ((memif_queue_t *) b0->queue, mq, b0,
1505 mq->next_buf);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001506
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001507 mq->next_buf++; /* mark the buffer as allocated */
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001508 count--;
1509 ns--;
1510 b0++;
1511 *count_out += 1;
1512 }
1513
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001514 DBG ("allocated: %u/%u bufs. Next buffer pointer %d", *count_out, count,
1515 mq->next_buf);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001516
1517 if (count)
1518 {
1519 DBG ("ring buffer full! qid: %u", qid);
1520 err = MEMIF_ERR_NOBUF_RING;
1521 }
1522
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001523 return err;
1524}
1525
1526int
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001527memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
1528 memif_buffer_t * bufs, uint16_t count,
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001529 uint16_t * count_out, uint16_t size)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001530{
1531 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001532 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001533 return MEMIF_ERR_NOCONN;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001534 if (EXPECT_FALSE (c->control_channel == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001535 return MEMIF_ERR_DISCONNECTED;
1536 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001537 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
1538 run_args.num_s2m_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001539 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001540 return MEMIF_ERR_QID;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001541 if (EXPECT_FALSE (!count_out))
1542 return MEMIF_ERR_INVAL_ARG;
1543
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001544 memif_socket_t *ms = (memif_socket_t *) c->args.socket;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001545 memif_queue_t *mq = &c->tx_queues[qid];
1546 memif_ring_t *ring = mq->ring;
msardara8f554b72018-12-11 18:36:55 +01001547 memif_buffer_t *b0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001548 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Chun Li5dca5462018-02-07 09:51:35 +08001549 uint16_t ring_size;
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001550 uint16_t ns;
msardara8f554b72018-12-11 18:36:55 +01001551 int err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001552 uint16_t dst_left, src_left;
mblya9087792023-12-05 09:36:49 -08001553 uint16_t saved_count_out, delta_count;
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001554 uint16_t saved_next_buf;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001555 memif_buffer_t *saved_b;
1556 *count_out = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001557
Chun Li5dca5462018-02-07 09:51:35 +08001558 ring_size = (1 << mq->log2_ring_size);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001559
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001560 if (c->args.is_master)
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001561 ns = ring->head - mq->next_buf;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001562 else
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001563 ns = ring_size - mq->next_buf + ring->tail;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001564
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001565 while (count && ns)
1566 {
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001567 b0 = (bufs + *count_out);
1568
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001569 saved_b = b0;
mblya9087792023-12-05 09:36:49 -08001570 saved_count_out = *count_out;
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001571 saved_next_buf = mq->next_buf;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001572
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001573 b0->desc_index = mq->next_buf;
1574 ring->desc[mq->next_buf & mask].flags = 0;
Jakub Grajciar8caf9ec2021-12-01 09:07:09 +01001575 b0->flags = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001576
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001577 /* slave can produce buffer with original length */
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001578 dst_left = (c->args.is_master) ? ring->desc[mq->next_buf & mask].length :
1579 c->run_args.buffer_size;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001580 src_left = size;
1581
1582 while (src_left)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001583 {
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001584 if (EXPECT_FALSE (dst_left == 0))
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001585 {
mblya9087792023-12-05 09:36:49 -08001586 if (ns)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001587 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001588 ring->desc[b0->desc_index & mask].flags |=
1589 MEMIF_DESC_FLAG_NEXT;
1590 b0->flags |= MEMIF_BUFFER_FLAG_NEXT;
1591
1592 b0 = (bufs + *count_out);
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001593 b0->desc_index = mq->next_buf;
1594 dst_left = (c->args.is_master) ?
1595 ring->desc[mq->next_buf & mask].length :
1596 c->run_args.buffer_size;
1597 ring->desc[mq->next_buf & mask].flags = 0;
mblya9087792023-12-05 09:36:49 -08001598 b0->flags = 0;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001599 }
1600 else
1601 {
1602 /* rollback allocated chain buffers */
mblya9087792023-12-05 09:36:49 -08001603 delta_count = *count_out - saved_count_out;
1604 memset (saved_b, 0, sizeof (memif_buffer_t) * delta_count);
1605 *count_out -= delta_count;
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001606 mq->next_buf = saved_next_buf;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001607 goto no_ns;
1608 }
1609 }
1610 b0->len = memif_min (dst_left, src_left);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001611
1612 /* slave resets buffer offset */
1613 if (c->args.is_master == 0)
1614 {
mblya9087792023-12-05 09:36:49 -08001615 memif_desc_t *d = &ring->desc[mq->next_buf & mask];
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001616 if (ms->get_external_buffer_offset)
1617 d->offset = ms->get_external_buffer_offset (c->private_ctx);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001618 else
Mohsin Kazmi8a344842022-07-27 13:43:07 +00001619 d->offset = d->offset - (d->offset % c->run_args.buffer_size);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001620 }
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001621 b0->data = memif_get_buffer (c, ring, mq->next_buf & mask);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001622
1623 src_left -= b0->len;
1624 dst_left -= b0->len;
mblya9087792023-12-05 09:36:49 -08001625 *count_out += 1;
1626 mq->next_buf++;
1627 ns--;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001628 }
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001629 count--;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001630 }
1631
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001632no_ns:
1633
mblya9087792023-12-05 09:36:49 -08001634 DBG ("allocated: %u/%u bufs, size: %u. Next buffer pointer %d", *count_out,
1635 count, size, mq->next_buf);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001636
1637 if (count)
1638 {
1639 DBG ("ring buffer full! qid: %u", qid);
1640 err = MEMIF_ERR_NOBUF_RING;
1641 }
1642
1643 return err;
1644}
1645
1646int
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001647memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count,
1648 uint16_t headroom)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001649{
1650 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001651 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001652 return MEMIF_ERR_NOCONN;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001653 if (EXPECT_FALSE (c->control_channel == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001654 return MEMIF_ERR_DISCONNECTED;
1655 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001656 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
1657 run_args.num_m2s_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001658 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001659 return MEMIF_ERR_QID;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001660 memif_socket_t *ms = (memif_socket_t *) c->args.socket;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001661 memif_queue_t *mq = &c->rx_queues[qid];
1662 memif_ring_t *ring = mq->ring;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001663 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Jakub Grajciar0dea94b2021-01-26 07:38:30 +01001664 uint16_t slot, counter = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001665
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001666 if (c->args.is_master)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001667 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001668 MEMIF_MEMORY_BARRIER ();
1669 ring->tail =
1670 (ring->tail + count <=
1671 mq->last_head) ? ring->tail + count : mq->last_head;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001672 return MEMIF_ERR_SUCCESS;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001673 }
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001674
1675 uint16_t head = ring->head;
Jakub Grajciar0dea94b2021-01-26 07:38:30 +01001676 slot = head;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001677 uint16_t ns = (1 << mq->log2_ring_size) - head + mq->last_tail;
Jakub Grajciar0dea94b2021-01-26 07:38:30 +01001678 count = (count < ns) ? count : ns;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001679
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001680 memif_desc_t *d;
Jakub Grajciar0dea94b2021-01-26 07:38:30 +01001681 while (counter < count)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001682 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001683 d = &ring->desc[slot & mask];
1684 d->region = 1;
1685 d->length = c->run_args.buffer_size - headroom;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001686 if (ms->get_external_buffer_offset)
1687 d->offset = ms->get_external_buffer_offset (c->private_ctx);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001688 else
Mohsin Kazmi8a344842022-07-27 13:43:07 +00001689 d->offset =
1690 d->offset - (d->offset % c->run_args.buffer_size) + headroom;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001691 slot++;
Jakub Grajciar0dea94b2021-01-26 07:38:30 +01001692 counter++;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001693 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001694
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001695 MEMIF_MEMORY_BARRIER ();
Jakub Grajciar0dea94b2021-01-26 07:38:30 +01001696 ring->head = slot;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001697
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001698 return MEMIF_ERR_SUCCESS; /* 0 */
1699}
1700
1701int
1702memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
1703 memif_buffer_t * bufs, uint16_t count, uint16_t * tx)
1704{
1705 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001706 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001707 return MEMIF_ERR_NOCONN;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001708 if (EXPECT_FALSE (c->control_channel == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001709 return MEMIF_ERR_DISCONNECTED;
1710 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001711 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
1712 run_args.num_s2m_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001713 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001714 return MEMIF_ERR_QID;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001715 if (EXPECT_FALSE (!tx))
1716 return MEMIF_ERR_INVAL_ARG;
1717
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001718 memif_queue_t *mq = &c->tx_queues[qid];
1719 memif_ring_t *ring = mq->ring;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001720 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1721 memif_buffer_t *b0;
Jakub Grajciar14217482021-01-14 13:23:48 +01001722 memif_desc_t *d;
1723 int64_t data_offset;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001724 *tx = 0;
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001725 int err = MEMIF_ERR_SUCCESS;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001726
1727 if (EXPECT_FALSE (count == 0))
1728 return MEMIF_ERR_SUCCESS;
1729
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001730 uint16_t index;
1731 if (c->args.is_master)
1732 index = ring->tail;
1733 else
1734 index = ring->head;
1735
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001736 while (count)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001737 {
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001738 b0 = (bufs + *tx);
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001739 /* set error to MEMIF_ERR_INVAL_ARG and finish the sending process
1740 */
1741 if ((b0->desc_index & mask) != (index & mask))
1742 {
1743 err = MEMIF_ERR_INVAL_ARG;
1744 goto done;
1745 }
Jakub Grajciar14217482021-01-14 13:23:48 +01001746 d = &ring->desc[b0->desc_index & mask];
1747 d->length = b0->len;
Mohsin Kazmi8636a322022-07-18 11:21:05 +00001748 d->flags =
1749 ((b0->flags & MEMIF_BUFFER_FLAG_NEXT) == 1) ? MEMIF_DESC_FLAG_NEXT : 0;
Jakub Grajciar14217482021-01-14 13:23:48 +01001750 if (!c->args.is_master)
1751 {
1752 // reset headroom
Mohsin Kazmi8a344842022-07-27 13:43:07 +00001753 d->offset = d->offset - (d->offset % c->run_args.buffer_size);
Jakub Grajciar14217482021-01-14 13:23:48 +01001754 // calculate offset from user data
1755 data_offset = b0->data - (d->offset + c->regions[d->region].addr);
1756 if (data_offset != 0)
1757 {
Jakub Grajciarcef0cc12021-03-01 08:54:35 +01001758 /* verify data offset and buffer length */
Jakub Grajciar14217482021-01-14 13:23:48 +01001759 if ((data_offset < 0) ||
Jakub Grajciarcef0cc12021-03-01 08:54:35 +01001760 ((data_offset + b0->len) > c->run_args.buffer_size))
Jakub Grajciar14217482021-01-14 13:23:48 +01001761 {
Nathan Skrzypczak814eee52021-05-07 19:39:07 +02001762 DBG ("slot: %d, data_offset: %ld, length: %d",
Jakub Grajciarcef0cc12021-03-01 08:54:35 +01001763 b0->desc_index & mask, data_offset, b0->len);
Jakub Grajciar14217482021-01-14 13:23:48 +01001764 err = MEMIF_ERR_INVAL_ARG;
1765 goto done;
1766 }
1767 d->offset += data_offset;
1768 }
1769 }
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001770
1771#ifdef MEMIF_DBG_SHM
1772 printf ("offset: %-6d\n", ring->desc[b0->desc_index & mask].offset);
1773 printf ("data: %p\n",
1774 memif_get_buffer (c, ring, b0->desc_index & mask));
1775 printf ("index: %u\n", b0->desc_index);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001776 print_bytes (memif_get_buffer (c, ring, b0->desc_index & mask),
1777 ring->desc[b0->desc_index & mask].length, DBG_TX_BUF);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001778#endif /* MEMIF_DBG_SHM */
1779
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001780 *tx += 1;
1781 count--;
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001782 index++;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001783 }
1784
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001785done:
Milan Lenco0a47c992017-10-12 14:19:31 +02001786 MEMIF_MEMORY_BARRIER ();
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001787 if (c->args.is_master)
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001788 ring->tail = b0->desc_index + 1;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001789 else
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001790 ring->head = b0->desc_index + 1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001791
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001792 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
1793 {
1794 uint64_t a = 1;
1795 int r = write (mq->int_fd, &a, sizeof (a));
1796 if (r < 0)
1797 return MEMIF_ERR_INT_WRITE;
1798 }
1799
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001800 return err;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001801}
1802
1803int
1804memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
1805 memif_buffer_t * bufs, uint16_t count, uint16_t * rx)
1806{
1807 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001808 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001809 return MEMIF_ERR_NOCONN;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001810 if (EXPECT_FALSE (c->control_channel == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001811 return MEMIF_ERR_DISCONNECTED;
1812 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001813 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
1814 run_args.num_m2s_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001815 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001816 return MEMIF_ERR_QID;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001817 if (EXPECT_FALSE (!rx))
1818 return MEMIF_ERR_INVAL_ARG;
1819
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001820 memif_queue_t *mq = &c->rx_queues[qid];
1821 memif_ring_t *ring = mq->ring;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001822 uint16_t cur_slot, last_slot;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001823 uint16_t ns;
1824 uint16_t mask = (1 << mq->log2_ring_size) - 1;
msardara8f554b72018-12-11 18:36:55 +01001825 memif_buffer_t *b0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001826 *rx = 0;
1827
1828 uint64_t b;
Jan Cavojsky77a95cd2020-03-03 16:25:58 +01001829 ssize_t r;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001830
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001831 cur_slot = (c->args.is_master) ? mq->last_head : mq->last_tail;
1832 last_slot = (c->args.is_master) ? ring->head : ring->tail;
1833 if (cur_slot == last_slot)
Jan Cavojsky77a95cd2020-03-03 16:25:58 +01001834 {
1835 r = read (mq->int_fd, &b, sizeof (b));
1836 if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN)))
1837 return memif_syscall_error_handler (errno);
1838
1839 return MEMIF_ERR_SUCCESS;
1840 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001841
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001842 ns = last_slot - cur_slot;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001843
1844 while (ns && count)
1845 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001846 b0 = (bufs + *rx);
1847
1848 b0->desc_index = cur_slot;
1849 b0->data = memif_get_buffer (c, ring, cur_slot & mask);
1850 b0->len = ring->desc[cur_slot & mask].length;
Jakub Grajciar8caf9ec2021-12-01 09:07:09 +01001851 b0->flags = 0;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001852 /* slave resets buffer length */
1853 if (c->args.is_master == 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001854 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001855 ring->desc[cur_slot & mask].length = c->run_args.buffer_size;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001856 }
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001857
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001858 if (ring->desc[cur_slot & mask].flags & MEMIF_DESC_FLAG_NEXT)
1859 {
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001860 b0->flags |= MEMIF_BUFFER_FLAG_NEXT;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001861 ring->desc[cur_slot & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
1862 }
Jakub Grajciar14217482021-01-14 13:23:48 +01001863
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001864 b0->queue = mq;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001865#ifdef MEMIF_DBG_SHM
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001866 printf ("data: %p\n", b0->data);
1867 printf ("index: %u\n", b0->desc_index);
Jakub Grajciarf35fef22021-01-08 15:01:13 +01001868 printf ("queue: %p\n", b0->queue);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001869 print_bytes (b0->data, b0->len, DBG_RX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001870#endif /* MEMIF_DBG_SHM */
1871 ns--;
1872 *rx += 1;
1873
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001874 count--;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001875 cur_slot++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001876 }
1877
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001878 if (c->args.is_master)
1879 mq->last_head = cur_slot;
1880 else
1881 mq->last_tail = cur_slot;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001882
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001883 if (ns)
1884 {
1885 DBG ("not enough buffers!");
1886 return MEMIF_ERR_NOBUF;
1887 }
1888
Jan Cavojsky77a95cd2020-03-03 16:25:58 +01001889 r = read (mq->int_fd, &b, sizeof (b));
1890 if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN)))
1891 return memif_syscall_error_handler (errno);
1892
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001893 return MEMIF_ERR_SUCCESS; /* 0 */
1894}
1895
1896int
1897memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
1898 char *buf, ssize_t buflen)
1899{
1900 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001901 memif_socket_t *ms;
1902 int err = MEMIF_ERR_SUCCESS, i;
1903 ssize_t l0 = 0, l1;
1904
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001905 if (c == NULL)
1906 return MEMIF_ERR_NOCONN;
1907
Jakub Grajciar12df4972019-07-01 14:24:48 +02001908 ms = (memif_socket_t *) c->args.socket;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001909
1910 l1 = strlen ((char *) c->args.interface_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001911 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001912 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001913 md->if_name =
1914 (uint8_t *) strcpy (buf + l0, (char *) c->args.interface_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001915 l0 += l1 + 1;
1916 }
1917 else
1918 err = MEMIF_ERR_NOBUF_DET;
1919
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001920 l1 = strlen ((char *) ms->args.app_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001921 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001922 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001923 md->inst_name =
1924 (uint8_t *) strcpy (buf + l0, (char *) ms->args.app_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001925 l0 += l1 + 1;
1926 }
1927 else
1928 err = MEMIF_ERR_NOBUF_DET;
1929
1930 l1 = strlen ((char *) c->remote_if_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001931 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001932 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001933 md->remote_if_name =
1934 (uint8_t *) strcpy (buf + l0, (char *) c->remote_if_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001935 l0 += l1 + 1;
1936 }
1937 else
1938 err = MEMIF_ERR_NOBUF_DET;
1939
1940 l1 = strlen ((char *) c->remote_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001941 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001942 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001943 md->remote_inst_name =
1944 (uint8_t *) strcpy (buf + l0, (char *) c->remote_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001945 l0 += l1 + 1;
1946 }
1947 else
1948 err = MEMIF_ERR_NOBUF_DET;
1949
1950 md->id = c->args.interface_id;
1951
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001952 if (strlen ((char *) c->args.secret) > 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001953 {
1954 l1 = strlen ((char *) c->args.secret);
Milan Lenco0a47c992017-10-12 14:19:31 +02001955 if (l0 + l1 < buflen)
1956 {
Jakub Grajciar5e1f69a2018-04-12 13:56:29 +02001957 md->secret = (uint8_t *) strcpy (buf + l0, (char *) c->args.secret);
Milan Lenco0a47c992017-10-12 14:19:31 +02001958 l0 += l1 + 1;
1959 }
1960 else
1961 err = MEMIF_ERR_NOBUF_DET;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001962 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001963
1964 md->role = (c->args.is_master) ? 0 : 1;
1965 md->mode = c->args.mode;
1966
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001967 l1 = 108;
Milan Lenco0a47c992017-10-12 14:19:31 +02001968 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001969 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001970 md->socket_path = (uint8_t *) memcpy (buf + l0, ms->args.path, 108);
Jakub Grajciar134f1e02021-01-04 11:10:42 +01001971 l0 += l1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001972 }
1973 else
1974 err = MEMIF_ERR_NOBUF_DET;
1975
Jakub Grajciar568cc462018-09-05 12:11:35 +02001976 l1 = strlen ((char *) c->remote_disconnect_string);
1977 if (l0 + l1 < buflen)
1978 {
1979 md->error =
1980 (uint8_t *) strcpy (buf + l0, (char *) c->remote_disconnect_string);
1981 l0 += l1 + 1;
1982 }
1983 else
1984 err = MEMIF_ERR_NOBUF_DET;
1985
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001986 md->regions_num = c->regions_num;
1987 l1 = sizeof (memif_region_details_t) * md->regions_num;
1988 if (l0 + l1 <= buflen)
1989 {
Koichiro Den33331ed2019-01-03 00:22:24 +09001990 md->regions = (memif_region_details_t *) (buf + l0);
Koichiro Denbe420e52018-12-15 00:31:34 +09001991 for (i = 0; i < md->regions_num; i++)
Jakub Grajciar12df4972019-07-01 14:24:48 +02001992 {
1993 md->regions[i].index = i;
1994 md->regions[i].addr = c->regions[i].addr;
1995 md->regions[i].size = c->regions[i].region_size;
1996 md->regions[i].fd = c->regions[i].fd;
1997 md->regions[i].is_external = c->regions[i].is_external;
1998 }
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001999 l0 += l1;
2000 }
2001 else
2002 err = MEMIF_ERR_NOBUF_DET;
2003
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002004 md->rx_queues_num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002005 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2006 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002007
2008 l1 = sizeof (memif_queue_details_t) * md->rx_queues_num;
2009 if (l0 + l1 <= buflen)
2010 {
Koichiro Den33331ed2019-01-03 00:22:24 +09002011 md->rx_queues = (memif_queue_details_t *) (buf + l0);
Koichiro Denbe420e52018-12-15 00:31:34 +09002012 for (i = 0; i < md->rx_queues_num; i++)
Jakub Grajciar12df4972019-07-01 14:24:48 +02002013 {
2014 md->rx_queues[i].region = c->rx_queues[i].region;
2015 md->rx_queues[i].qid = i;
2016 md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size);
2017 md->rx_queues[i].flags = c->rx_queues[i].ring->flags;
2018 md->rx_queues[i].head = c->rx_queues[i].ring->head;
2019 md->rx_queues[i].tail = c->rx_queues[i].ring->tail;
2020 md->rx_queues[i].buffer_size = c->run_args.buffer_size;
2021 }
Milan Lenco0a47c992017-10-12 14:19:31 +02002022 l0 += l1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002023 }
2024 else
2025 err = MEMIF_ERR_NOBUF_DET;
2026
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002027 md->tx_queues_num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002028 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
2029 run_args.num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002030
2031 l1 = sizeof (memif_queue_details_t) * md->tx_queues_num;
2032 if (l0 + l1 <= buflen)
2033 {
Koichiro Den33331ed2019-01-03 00:22:24 +09002034 md->tx_queues = (memif_queue_details_t *) (buf + l0);
Koichiro Denbe420e52018-12-15 00:31:34 +09002035 for (i = 0; i < md->tx_queues_num; i++)
Jakub Grajciar12df4972019-07-01 14:24:48 +02002036 {
2037 md->tx_queues[i].region = c->tx_queues[i].region;
2038 md->tx_queues[i].qid = i;
2039 md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size);
2040 md->tx_queues[i].flags = c->tx_queues[i].ring->flags;
2041 md->tx_queues[i].head = c->tx_queues[i].ring->head;
2042 md->tx_queues[i].tail = c->tx_queues[i].ring->tail;
2043 md->tx_queues[i].buffer_size = c->run_args.buffer_size;
2044 }
Milan Lenco0a47c992017-10-12 14:19:31 +02002045 l0 += l1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002046 }
2047 else
2048 err = MEMIF_ERR_NOBUF_DET;
2049
Jakub Grajciar134f1e02021-01-04 11:10:42 +01002050 /* This is not completely true, clients should relay on
2051 * on_connect/on_disconnect callbacks */
2052 md->link_up_down = (c->control_channel != NULL) ? 1 : 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002053
2054 return err; /* 0 */
2055}
2056
2057int
2058memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd)
2059{
2060 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002061 uint8_t num;
2062
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002063 *efd = -1;
2064 if (c == NULL)
2065 return MEMIF_ERR_NOCONN;
Jakub Grajciar134f1e02021-01-04 11:10:42 +01002066 if (c->control_channel == NULL)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002067 return MEMIF_ERR_DISCONNECTED;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002068
2069 num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002070 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2071 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002072 if (qid >= num)
2073 return MEMIF_ERR_QID;
2074
2075 *efd = c->rx_queues[qid].int_fd;
2076
2077 return MEMIF_ERR_SUCCESS;
2078}