blob: f3d8f9a55efbeb9954c5343235221107b32c36a8 [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
68libmemif_main_t libmemif_main;
69int memif_epfd;
Milan Lenco0a47c992017-10-12 14:19:31 +020070int poll_cancel_fd = -1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020071
72static char memif_buf[MAX_ERRBUF_LEN];
73
74const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */
75 "Success.",
76 /* MEMIF_ERR_SYSCALL */
77 "Unspecified syscall error (build with -DMEMIF_DBG or make debug).",
Jakub Grajciar568cc462018-09-05 12:11:35 +020078 /* MEMIF_ERR_CONNREFUSED */
79 "Connection refused",
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020080 /* MEMIF_ERR_ACCES */
81 "Permission to resoure denied.",
82 /* MEMIF_ERR_NO_FILE */
83 "Socket file does not exist",
84 /* MEMIF_ERR_FILE_LIMIT */
85 "System limit on total numer of open files reached.",
86 /* MEMIF_ERR_PROC_FILE_LIMIT */
87 "Per-process limit on total number of open files reached.",
88 /* MEMIF_ERR_ALREADY */
89 "Connection already requested.",
90 /* MEMIF_ERR_AGAIN */
91 "File descriptor refers to file other than socket, or operation would block.",
92 /* MEMIF_ERR_BAD_FD */
93 "Bad file descriptor.",
94 /* MEMIF_ERR_NOMEM */
95 "Out of memory.",
96 /* MEMIF_ERR_INVAL_ARG */
97 "Invalid argument.",
98 /* MEMIF_ERR_NOCONN */
99 "Memif connection handle does not point to existing conenction",
100 /* MEMIF_ERR_CONN */
101 "Memif connection handle points to existing connection",
102 /* MEMIF_ERR_CB_FDUPDATE */
103 "Callback memif_control_fd_update_t returned error",
104 /* MEMIF_ERR_FILE_NOT_SOCK */
105 "File specified by socket filename exists and is not socket.",
106 /* MEMIF_ERR_NO_SHMFD */
107 "Missing shared memory file descriptor. (internal error)",
108 /* MEMIF_ERR_COOKIE */
109 "Invalid cookie on ring. (internal error)",
110 /* MEMIF_ERR_NOBUF_RING */
111 "Ring buffer full.",
112 /* MEMIF_ERR_NOBUF */
113 "Not enough memif buffers. There are unreceived data in shared memory.",
114 /* MEMIF_ERR_NOBUF_DET */
115 "Not enough space for memif details in suplied buffer. String data might be malformed.",
116 /* MEMIF_ERR_INT_WRITE */
117 "Send interrupt error.",
118 /* MEMIF_ERR_MFMSG */
119 "Malformed message received on control channel.",
120 /* MEMIF_ERR_QID */
121 "Invalid queue id",
122 /* MEMIF_ERR_PROTO */
123 "Incompatible memory interface protocol version.",
124 /* MEMIF_ERR_ID */
125 "Unmatched interface id.",
126 /* MEMIF_ERR_ACCSLAVE */
127 "Slave cannot accept connection reqest.",
128 /* MEMIF_ERR_ALRCONN */
129 "Interface is already connected.",
130 /* MEMIF_ERR_MODE */
131 "Mode mismatch.",
132 /* MEMIF_ERR_SECRET */
133 "Secret mismatch.",
134 /* MEMIF_ERR_NOSECRET */
135 "Secret required.",
136 /* MEMIF_ERR_MAXREG */
137 "Limit on total number of regions reached.",
138 /* MEMIF_ERR_MAXRING */
139 "Limit on total number of ring reached.",
140 /* MEMIF_ERR_NO_INTFD */
141 "Missing interrupt file descriptor. (internal error)",
142 /* MEMIF_ERR_DISCONNECT */
143 "Interface received disconnect request.",
144 /* MEMIF_ERR_DISCONNECTED */
145 "Interface is disconnected.",
146 /* MEMIF_ERR_UNKNOWN_MSG */
Milan Lenco0a47c992017-10-12 14:19:31 +0200147 "Unknown message type received on control channel. (internal error)",
148 /* MEMIF_ERR_POLL_CANCEL */
Damjan Marion6d56fa42017-11-03 12:24:37 +0100149 "Memif event polling was canceled.",
150 /* MEMIF_ERR_MAX_RING */
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200151 "Maximum log2 ring size is 15",
Jakub Grajciarab7c2b02018-03-28 10:21:05 +0200152 /* MEMIF_ERR_PRIVHDR */
153 "Private headers not supported."
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200154};
155
156#define MEMIF_ERR_UNDEFINED "undefined error"
157
158char *
159memif_strerror (int err_code)
160{
161 if (err_code >= ERRLIST_LEN)
162 {
163 strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED));
164 memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0';
165 }
166 else
167 {
168 strncpy (memif_buf, memif_errlist[err_code],
169 strlen (memif_errlist[err_code]));
170 memif_buf[strlen (memif_errlist[err_code])] = '\0';
171 }
172 return memif_buf;
173}
174
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200175uint16_t
176memif_get_version ()
177{
178 return MEMIF_VERSION;
179}
180
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200181#define DBG_TX_BUF (0)
182#define DBG_RX_BUF (1)
183
184#ifdef MEMIF_DBG_SHM
185static void
186print_bytes (void *data, uint16_t len, uint8_t q)
187{
188 if (q == DBG_TX_BUF)
189 printf ("\nTX:\n\t");
190 else
191 printf ("\nRX:\n\t");
192 int i;
193 for (i = 0; i < len; i++)
194 {
195 if (i % 8 == 0)
196 printf ("\n%d:\t", i);
197 printf ("%02X ", ((uint8_t *) (data))[i]);
198 }
199 printf ("\n\n");
200}
Jakub Grajciarba3c4e82017-09-18 11:21:40 +0200201#endif /* MEMIF_DBG_SHM */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200202
203int
204memif_syscall_error_handler (int err_code)
205{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200206 DBG ("%s", strerror (err_code));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200207
208 if (err_code == 0)
209 return MEMIF_ERR_SUCCESS;
210 if (err_code == EACCES)
211 return MEMIF_ERR_ACCES;
212 if (err_code == ENFILE)
213 return MEMIF_ERR_FILE_LIMIT;
214 if (err_code == EMFILE)
215 return MEMIF_ERR_PROC_FILE_LIMIT;
216 if (err_code == ENOMEM)
217 return MEMIF_ERR_NOMEM;
Damjan Marion6d56fa42017-11-03 12:24:37 +0100218/* connection refused if master does not exist
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200219 this error would spam the user until master was created */
Jakub Grajciar568cc462018-09-05 12:11:35 +0200220/*
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200221 if (err_code == ECONNREFUSED)
222 return MEMIF_ERR_SUCCESS;
Jakub Grajciar568cc462018-09-05 12:11:35 +0200223*/
224 if (err_code == ECONNREFUSED)
225 return MEMIF_ERR_CONNREFUSED;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200226 if (err_code == EALREADY)
227 return MEMIF_ERR_ALREADY;
228 if (err_code == EAGAIN)
229 return MEMIF_ERR_AGAIN;
230 if (err_code == EBADF)
231 return MEMIF_ERR_BAD_FD;
232 if (err_code == ENOENT)
233 return MEMIF_ERR_NO_FILE;
234
235 /* other syscall errors */
236 return MEMIF_ERR_SYSCALL;
237}
238
239static int
240memif_add_epoll_fd (int fd, uint32_t events)
241{
242 if (fd < 0)
243 {
244 DBG ("invalid fd %d", fd);
245 return -1;
246 }
247 struct epoll_event evt;
248 memset (&evt, 0, sizeof (evt));
249 evt.events = events;
250 evt.data.fd = fd;
251 if (epoll_ctl (memif_epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
252 {
253 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
254 return -1;
255 }
256 DBG ("fd %d added to epoll", fd);
257 return 0;
258}
259
260static int
261memif_mod_epoll_fd (int fd, uint32_t events)
262{
263 if (fd < 0)
264 {
265 DBG ("invalid fd %d", fd);
266 return -1;
267 }
268 struct epoll_event evt;
269 memset (&evt, 0, sizeof (evt));
270 evt.events = events;
271 evt.data.fd = fd;
272 if (epoll_ctl (memif_epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
273 {
274 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
275 return -1;
276 }
277 DBG ("fd %d moddified on epoll", fd);
278 return 0;
279}
280
281static int
282memif_del_epoll_fd (int fd)
283{
284 if (fd < 0)
285 {
286 DBG ("invalid fd %d", fd);
287 return -1;
288 }
289 struct epoll_event evt;
290 memset (&evt, 0, sizeof (evt));
291 if (epoll_ctl (memif_epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
292 {
293 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
294 return -1;
295 }
296 DBG ("fd %d removed from epoll", fd);
297 return 0;
298}
299
300int
Jakub Grajciar12df4972019-07-01 14:24:48 +0200301memif_control_fd_update (int fd, uint8_t events, void *private_ctx)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200302{
303 if (events & MEMIF_FD_EVENT_DEL)
304 return memif_del_epoll_fd (fd);
305
306 uint32_t evt = 0;
307 if (events & MEMIF_FD_EVENT_READ)
308 evt |= EPOLLIN;
309 if (events & MEMIF_FD_EVENT_WRITE)
310 evt |= EPOLLOUT;
311
312 if (events & MEMIF_FD_EVENT_MOD)
313 return memif_mod_epoll_fd (fd, evt);
314
315 return memif_add_epoll_fd (fd, evt);
316}
317
318int
319add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list, uint16_t * len)
320{
321 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200322 memif_list_elt_t *tmp;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200323 int i;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200324
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200325 for (i = 0; i < *len; i++)
326 {
327 if ((*list)[i].data_struct == NULL)
328 {
329 (*list)[i].key = e->key;
330 (*list)[i].data_struct = e->data_struct;
331 return i;
332 }
333 }
Jakub Grajciar12df4972019-07-01 14:24:48 +0200334
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200335 tmp = lm->realloc (*list, sizeof (memif_list_elt_t) * *len * 2);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200336 if (tmp == NULL)
337 return -1;
338
339 for (i = *len; i < *len * 2; i++)
340 {
341 tmp[i].key = -1;
342 tmp[i].data_struct = NULL;
343 }
344
345 tmp[*len].key = e->key;
346 tmp[*len].data_struct = e->data_struct;
347 i = *len;
348 *len = *len * 2;
349 *list = tmp;
350
351 return i;
352}
353
354int
355get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list, uint16_t len,
356 int key)
357{
Jakub Grajciar12df4972019-07-01 14:24:48 +0200358 int i;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200359 if (key == -1)
360 {
361 *e = NULL;
362 return -1;
363 }
Jakub Grajciar12df4972019-07-01 14:24:48 +0200364
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200365 for (i = 0; i < len; i++)
366 {
367 if (list[i].key == key)
368 {
369 *e = &list[i];
370 return 0;
371 }
372 }
373 *e = NULL;
374 return -1;
375}
376
377/* does not free memory, only marks element as free */
378int
379free_list_elt (memif_list_elt_t * list, uint16_t len, int key)
380{
381 int i;
382 for (i = 0; i < len; i++)
383 {
384 if (list[i].key == key)
385 {
386 list[i].key = -1;
387 list[i].data_struct = NULL;
388 return 0;
389 }
390 }
391
392 return -1;
393}
394
395int
396free_list_elt_ctx (memif_list_elt_t * list, uint16_t len,
397 memif_connection_t * ctx)
398{
399 int i;
400 for (i = 0; i < len; i++)
401 {
402 if (list[i].key == -1)
403 {
404 if (list[i].data_struct == ctx)
405 {
406 list[i].data_struct = NULL;
407 return 0;
408 }
409 }
410 }
411
412 return -1;
413}
414
415static void
416memif_control_fd_update_register (memif_control_fd_update_t * cb)
417{
418 libmemif_main_t *lm = &libmemif_main;
419 lm->control_fd_update = cb;
420}
421
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200422void
423memif_register_external_region (memif_add_external_region_t * ar,
424 memif_get_external_region_addr_t * gr,
425 memif_del_external_region_t * dr,
426 memif_get_external_buffer_offset_t * go)
427{
428 libmemif_main_t *lm = &libmemif_main;
429 lm->add_external_region = ar;
430 lm->get_external_region_addr = gr;
431 lm->del_external_region = dr;
432 lm->get_external_buffer_offset = go;
433}
434
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200435static void
436memif_alloc_register (memif_alloc_t * ma)
437{
438 libmemif_main_t *lm = &libmemif_main;
439 lm->alloc = ma;
440}
441
442static void
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200443memif_realloc_register (memif_realloc_t * mr)
444{
445 libmemif_main_t *lm = &libmemif_main;
446 lm->realloc = mr;
447}
448
449static void
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200450memif_free_register (memif_free_t * mf)
451{
452 libmemif_main_t *lm = &libmemif_main;
453 lm->free = mf;
454}
455
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200456int
Jakub Grajciar12df4972019-07-01 14:24:48 +0200457memif_set_connection_request_timer (struct itimerspec timer)
Jakub Grajciar84b83772019-03-04 12:42:19 +0100458{
459 libmemif_main_t *lm = &libmemif_main;
460 int err = MEMIF_ERR_SUCCESS;
461
462 lm->arm = timer;
463
464 /* overwrite timer, if already armed */
465 if (lm->disconn_slaves != 0)
466 {
467 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
468 {
469 err = memif_syscall_error_handler (errno);
470 }
471 }
472 return err;
473}
474
475int
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200476memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200477 memif_alloc_t * memif_alloc, memif_realloc_t * memif_realloc,
478 memif_free_t * memif_free)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200479{
480 int err = MEMIF_ERR_SUCCESS; /* 0 */
481 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar19418712018-03-13 13:57:50 +0100482 memset (lm, 0, sizeof (libmemif_main_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200483
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200484 if (memif_alloc != NULL)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200485 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200486 memif_alloc_register (memif_alloc);
487 }
488 else
489 memif_alloc_register (malloc);
490
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200491 if (memif_realloc != NULL)
492 {
493 memif_realloc_register (memif_realloc);
494 }
495 else
496 memif_realloc_register (realloc);
497
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200498 if (memif_free != NULL)
499 memif_free_register (memif_free);
500 else
501 memif_free_register (free);
502
503 if (app_name != NULL)
504 {
505 uint8_t len = (strlen (app_name) > MEMIF_NAME_LEN)
506 ? strlen (app_name) : MEMIF_NAME_LEN;
507 strncpy ((char *) lm->app_name, app_name, len);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200508 }
509 else
510 {
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200511 strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME,
512 strlen (MEMIF_DEFAULT_APP_NAME));
513 }
514
515 /* register control fd update callback */
516 if (on_control_fd_update != NULL)
517 memif_control_fd_update_register (on_control_fd_update);
518 else
519 {
520 memif_epfd = epoll_create (1);
521 memif_control_fd_update_register (memif_control_fd_update);
Milan Lenco0a47c992017-10-12 14:19:31 +0200522 if ((poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0)
523 {
524 err = errno;
525 DBG ("eventfd: %s", strerror (err));
526 return memif_syscall_error_handler (err);
527 }
Jakub Grajciar12df4972019-07-01 14:24:48 +0200528 lm->control_fd_update (poll_cancel_fd, MEMIF_FD_EVENT_READ, NULL);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200529 DBG ("libmemif event polling initialized");
530 }
531
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200532 lm->control_list_len = 2;
533 lm->interrupt_list_len = 2;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200534 lm->socket_list_len = 1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200535 lm->pending_list_len = 1;
536
537 lm->control_list =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200538 lm->alloc (sizeof (memif_list_elt_t) * lm->control_list_len);
539 if (lm->control_list == NULL)
540 {
541 err = MEMIF_ERR_NOMEM;
542 goto error;
543 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200544 lm->interrupt_list =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200545 lm->alloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len);
546 if (lm->interrupt_list == NULL)
547 {
548 err = MEMIF_ERR_NOMEM;
549 goto error;
550 }
Jakub Grajciar12df4972019-07-01 14:24:48 +0200551 lm->socket_list =
552 lm->alloc (sizeof (memif_list_elt_t) * lm->socket_list_len);
553 if (lm->socket_list == NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200554 {
555 err = MEMIF_ERR_NOMEM;
556 goto error;
557 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200558 lm->pending_list =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200559 lm->alloc (sizeof (memif_list_elt_t) * lm->pending_list_len);
560 if (lm->pending_list == NULL)
561 {
562 err = MEMIF_ERR_NOMEM;
563 goto error;
564 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200565
566 int i;
567 for (i = 0; i < lm->control_list_len; i++)
568 {
569 lm->control_list[i].key = -1;
570 lm->control_list[i].data_struct = NULL;
571 }
572 for (i = 0; i < lm->interrupt_list_len; i++)
573 {
574 lm->interrupt_list[i].key = -1;
575 lm->interrupt_list[i].data_struct = NULL;
576 }
Jakub Grajciar12df4972019-07-01 14:24:48 +0200577 for (i = 0; i < lm->socket_list_len; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200578 {
Jakub Grajciar12df4972019-07-01 14:24:48 +0200579 lm->socket_list[i].key = -1;
580 lm->socket_list[i].data_struct = NULL;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200581 }
582 for (i = 0; i < lm->pending_list_len; i++)
583 {
584 lm->pending_list[i].key = -1;
585 lm->pending_list[i].data_struct = NULL;
586 }
587
588 lm->disconn_slaves = 0;
589
590 lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK);
591 if (lm->timerfd < 0)
592 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200593 err = memif_syscall_error_handler (errno);
594 goto error;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200595 }
596
Jakub Grajciar84b83772019-03-04 12:42:19 +0100597 lm->arm.it_value.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC;
598 lm->arm.it_value.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC;
599 lm->arm.it_interval.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC;
600 lm->arm.it_interval.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200601
Jakub Grajciar12df4972019-07-01 14:24:48 +0200602 if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ, NULL) < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200603 {
604 DBG ("callback type memif_control_fd_update_t error!");
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200605 err = MEMIF_ERR_CB_FDUPDATE;
606 goto error;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200607 }
608
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200609 return err;
610
611error:
612 memif_cleanup ();
613 return err;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200614}
615
616static inline memif_ring_t *
617memif_get_ring (memif_connection_t * conn, memif_ring_type_t type,
618 uint16_t ring_num)
619{
620 if (&conn->regions[0] == NULL)
621 return NULL;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200622 void *p = conn->regions[0].addr;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200623 int ring_size =
624 sizeof (memif_ring_t) +
625 sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size);
626 p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size;
627
628 return (memif_ring_t *) p;
629}
630
631int
632memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode,
633 uint16_t qid)
634{
635 memif_connection_t *conn = (memif_connection_t *) c;
636 if (conn == NULL)
637 return MEMIF_ERR_NOCONN;
638 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +0200639 (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->
640 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200641 if (qid >= num)
642 return MEMIF_ERR_QID;
643
644 conn->rx_queues[qid].ring->flags = rx_mode;
645 DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags);
646 return MEMIF_ERR_SUCCESS;
647}
648
Jakub Grajciar12df4972019-07-01 14:24:48 +0200649static int
650memif_socket_start_listening (memif_socket_t * ms)
651{
652 libmemif_main_t *lm = &libmemif_main;
653 memif_list_elt_t elt;
654 struct stat file_stat;
655 struct sockaddr_un un = { 0 };
656 int on = 1;
657 int err = MEMIF_ERR_SUCCESS;
658
659 if (ms->type == MEMIF_SOCKET_TYPE_CLIENT)
660 return MEMIF_ERR_INVAL_ARG;
661
662 /* check if file exists */
663 if (stat ((char *) ms->filename, &file_stat) == 0)
664 {
665 if (S_ISSOCK (file_stat.st_mode))
666 unlink ((char *) ms->filename);
667 else
668 return memif_syscall_error_handler (errno);
669 }
670
671 ms->fd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
672 if (ms->fd < 0)
673 {
674 err = memif_syscall_error_handler (errno);
675 goto error;
676 }
677
678 DBG ("socket %d created", ms->fd);
679 un.sun_family = AF_UNIX;
680 strncpy ((char *) un.sun_path, (char *) ms->filename,
681 sizeof (un.sun_path) - 1);
682 if (setsockopt (ms->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0)
683 {
684 err = memif_syscall_error_handler (errno);
685 goto error;
686 }
687 if (bind (ms->fd, (struct sockaddr *) &un, sizeof (un)) < 0)
688 {
689 err = memif_syscall_error_handler (errno);
690 goto error;
691 }
692 if (listen (ms->fd, 1) < 0)
693 {
694 err = memif_syscall_error_handler (errno);
695 goto error;
696 }
697 if (stat ((char *) ms->filename, &file_stat) < 0)
698 {
699 err = memif_syscall_error_handler (errno);
700 goto error;
701 }
702
703 /* add socket to libmemif main */
704 elt.key = ms->fd;
705 elt.data_struct = ms;
706 add_list_elt (&elt, &lm->socket_list, &lm->socket_list_len);
707 lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ, ms->private_ctx);
708
709 ms->type = MEMIF_SOCKET_TYPE_LISTENER;
710
711 return err;
712
713error:
714 if (ms->fd > 0)
715 {
716 close (ms->fd);
717 ms->fd = -1;
718 }
719 return err;
720}
721
722int
723memif_create_socket (memif_socket_handle_t * sock, const char *filename,
724 void *private_ctx)
725{
726 libmemif_main_t *lm = &libmemif_main;
727 memif_socket_t *ms = (memif_socket_t *) * sock;
728 int i, err = MEMIF_ERR_SUCCESS;
729
730 for (i = 0; i < lm->socket_list_len; i++)
731 {
732 if ((ms = (memif_socket_t *) lm->socket_list[i].data_struct) != NULL)
733 {
734 if (strncmp ((char *) ms->filename, filename,
735 strlen ((char *) ms->filename)) == 0)
736 return MEMIF_ERR_INVAL_ARG;
737 }
738 }
739
740 /* allocate memif_socket_t */
741 ms = NULL;
742 ms = lm->alloc (sizeof (memif_socket_t));
743 if (ms == NULL)
744 {
745 err = MEMIF_ERR_NOMEM;
746 goto error;
747 }
748 memset (ms, 0, sizeof (memif_socket_t));
749 /* set filename */
750 ms->filename = lm->alloc (strlen (filename) + sizeof (char));
751 if (ms->filename == NULL)
752 {
753 err = MEMIF_ERR_NOMEM;
754 goto error;
755 }
756 memset (ms->filename, 0, strlen (filename) + sizeof (char));
757 strncpy ((char *) ms->filename, filename, strlen (filename));
758
759 ms->type = MEMIF_SOCKET_TYPE_NONE;
760
761 ms->interface_list_len = 1;
762 ms->interface_list =
763 lm->alloc (sizeof (memif_list_elt_t) * ms->interface_list_len);
764 if (ms->interface_list == NULL)
765 {
766 err = MEMIF_ERR_NOMEM;
767 goto error;
768 }
769 ms->interface_list[0].key = -1;
770 ms->interface_list[0].data_struct = NULL;
771
772 *sock = ms;
773
774 return err;
775
776error:
777 if (ms != NULL)
778 {
779 if (ms->filename != NULL)
780 {
781 lm->free (ms->filename);
782 ms->filename = NULL;
783 }
784 if (ms->fd > 0)
785 {
786 close (ms->fd);
787 ms->fd = -1;
788 }
789 if (ms->interface_list != NULL)
790 {
791 lm->free (ms->interface_list);
792 ms->interface_list = NULL;
793 ms->interface_list_len = 0;
794 }
795 lm->free (ms);
796 *sock = ms = NULL;
797 }
798 return err;
799}
800
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200801int
802memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
803 memif_connection_update_t * on_connect,
804 memif_connection_update_t * on_disconnect,
805 memif_interrupt_t * on_interrupt, void *private_ctx)
806{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200807 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200808 int err, index = 0;
809 memif_list_elt_t elt;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200810 memif_connection_t *conn = (memif_connection_t *) * c;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200811 memif_socket_t *ms;
812
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200813 if (conn != NULL)
814 {
815 DBG ("This handle already points to existing memif.");
816 return MEMIF_ERR_CONN;
817 }
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200818 conn = (memif_connection_t *) lm->alloc (sizeof (memif_connection_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200819 if (conn == NULL)
820 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200821 err = MEMIF_ERR_NOMEM;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200822 goto error;
823 }
824 memset (conn, 0, sizeof (memif_connection_t));
825
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200826 conn->args.interface_id = args->interface_id;
827
828 if (args->log2_ring_size == 0)
829 args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE;
Damjan Marion6d56fa42017-11-03 12:24:37 +0100830 else if (args->log2_ring_size > MEMIF_MAX_LOG2_RING_SIZE)
831 {
832 err = MEMIF_ERR_MAX_RING;
833 goto error;
834 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200835 if (args->buffer_size == 0)
836 args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
837 if (args->num_s2m_rings == 0)
838 args->num_s2m_rings = MEMIF_DEFAULT_TX_QUEUES;
839 if (args->num_m2s_rings == 0)
840 args->num_m2s_rings = MEMIF_DEFAULT_RX_QUEUES;
841
842 conn->args.num_s2m_rings = args->num_s2m_rings;
843 conn->args.num_m2s_rings = args->num_m2s_rings;
844 conn->args.buffer_size = args->buffer_size;
845 conn->args.log2_ring_size = args->log2_ring_size;
846 conn->args.is_master = args->is_master;
847 conn->args.mode = args->mode;
848 conn->msg_queue = NULL;
849 conn->regions = NULL;
850 conn->tx_queues = NULL;
851 conn->rx_queues = NULL;
852 conn->fd = -1;
853 conn->on_connect = on_connect;
854 conn->on_disconnect = on_disconnect;
855 conn->on_interrupt = on_interrupt;
856 conn->private_ctx = private_ctx;
857 memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t));
858
859 uint8_t l = strlen ((char *) args->interface_name);
860 strncpy ((char *) conn->args.interface_name, (char *) args->interface_name,
861 l);
862
Jakub Grajciar12df4972019-07-01 14:24:48 +0200863 if ((l = strlen ((char *) args->secret)) > 0)
864 strncpy ((char *) conn->args.secret, (char *) args->secret, l);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200865
Jakub Grajciar12df4972019-07-01 14:24:48 +0200866 if (args->socket != NULL)
867 conn->args.socket = args->socket;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200868 else
869 {
Jakub Grajciar12df4972019-07-01 14:24:48 +0200870 if (lm->default_socket == NULL)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200871 {
Jakub Grajciar12df4972019-07-01 14:24:48 +0200872 err =
873 memif_create_socket ((memif_socket_handle_t *) &
874 lm->default_socket,
875 MEMIF_DEFAULT_SOCKET_PATH, NULL);
876 if (err != MEMIF_ERR_SUCCESS)
877 goto error;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200878 }
Jakub Grajciar12df4972019-07-01 14:24:48 +0200879 conn->args.socket = lm->default_socket;
880 }
881 ms = (memif_socket_t *) conn->args.socket;
882
883 if ((conn->args.is_master && ms->type == MEMIF_SOCKET_TYPE_CLIENT) ||
884 (!conn->args.is_master && ms->type == MEMIF_SOCKET_TYPE_LISTENER))
885 {
886 err = MEMIF_ERR_INVAL_ARG;
887 goto error;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200888 }
889
Jakub Grajciar12df4972019-07-01 14:24:48 +0200890 elt.key = conn->args.interface_id;
891 elt.data_struct = conn;
892 add_list_elt (&elt, &ms->interface_list, &ms->interface_list_len);
893 ms->use_count++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200894
895 if (conn->args.is_master)
896 {
Jakub Grajciar12df4972019-07-01 14:24:48 +0200897 if (ms->type == MEMIF_SOCKET_TYPE_NONE)
898 err = memif_socket_start_listening (ms);
899 if (err != MEMIF_ERR_SUCCESS)
900 goto error;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200901 }
902 else
903 {
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200904 lm->disconn_slaves++;
905
Jakub Grajciar12df4972019-07-01 14:24:48 +0200906 elt.key = -1;
907 elt.data_struct = conn;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200908 if ((index =
Jakub Grajciar12df4972019-07-01 14:24:48 +0200909 add_list_elt (&elt, &lm->control_list, &lm->control_list_len)) < 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200910 {
911 err = MEMIF_ERR_NOMEM;
912 goto error;
913 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200914
Jakub Grajciar84b83772019-03-04 12:42:19 +0100915 conn->index = index;
916
917 /* try connectiong to master */
Jakub Grajciar12df4972019-07-01 14:24:48 +0200918 err = memif_request_connection (conn);
Jakub Grajciar84b83772019-03-04 12:42:19 +0100919 if ((err < 0) && (lm->disconn_slaves == 1))
920 {
921 /* connection failed, arm reconnect timer (if not armed) */
922 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
923 {
924 err = memif_syscall_error_handler (errno);
925 goto error;
926 }
927 }
928 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200929
Jakub Grajciar12df4972019-07-01 14:24:48 +0200930 *c = conn;
931
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200932 return 0;
933
934error:
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200935 if (conn != NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200936 lm->free (conn);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200937 *c = conn = NULL;
938 return err;
939}
940
941int
Jakub Grajciar12df4972019-07-01 14:24:48 +0200942memif_request_connection (memif_conn_handle_t c)
Jakub Grajciar84b83772019-03-04 12:42:19 +0100943{
944 libmemif_main_t *lm = &libmemif_main;
945 memif_connection_t *conn = (memif_connection_t *) c;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200946 memif_socket_t *ms;
Jakub Grajciar84b83772019-03-04 12:42:19 +0100947 int err = MEMIF_ERR_SUCCESS;
948 int sockfd = -1;
949 struct sockaddr_un sun;
950
Jakub Grajciar12df4972019-07-01 14:24:48 +0200951 if (conn == NULL)
952 return MEMIF_ERR_NOCONN;
953
954 ms = (memif_socket_t *) conn->args.socket;
955
956 if (conn->args.is_master || ms->type == MEMIF_SOCKET_TYPE_LISTENER)
Jakub Grajciar84b83772019-03-04 12:42:19 +0100957 return MEMIF_ERR_INVAL_ARG;
958 if (conn->fd > 0)
959 return MEMIF_ERR_ALRCONN;
960
961 sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
962 if (sockfd < 0)
963 {
964 err = memif_syscall_error_handler (errno);
965 goto error;
966 }
967
968 sun.sun_family = AF_UNIX;
969
Jakub Grajciar12df4972019-07-01 14:24:48 +0200970 strncpy (sun.sun_path, (char *) ms->filename, sizeof (sun.sun_path) - 1);
Jakub Grajciar84b83772019-03-04 12:42:19 +0100971
972 if (connect (sockfd, (struct sockaddr *) &sun,
Jakub Grajciar12df4972019-07-01 14:24:48 +0200973 sizeof (struct sockaddr_un)) == 0)
Jakub Grajciar84b83772019-03-04 12:42:19 +0100974 {
975 conn->fd = sockfd;
976 conn->read_fn = memif_conn_fd_read_ready;
977 conn->write_fn = memif_conn_fd_write_ready;
978 conn->error_fn = memif_conn_fd_error;
979
980 lm->control_list[conn->index].key = conn->fd;
981
982 lm->control_fd_update (sockfd,
Jakub Grajciar12df4972019-07-01 14:24:48 +0200983 MEMIF_FD_EVENT_READ |
984 MEMIF_FD_EVENT_WRITE, conn->private_ctx);
Jakub Grajciar84b83772019-03-04 12:42:19 +0100985
986 lm->disconn_slaves--;
987 if (lm->disconn_slaves == 0)
988 {
989 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0)
990 {
991 err = memif_syscall_error_handler (errno);
992 return err;
993 }
994 }
995 }
996 else
997 {
998 strcpy ((char *) conn->remote_disconnect_string,
Jakub Grajciar12df4972019-07-01 14:24:48 +0200999 memif_strerror (memif_syscall_error_handler (errno)));
Jakub Grajciar84b83772019-03-04 12:42:19 +01001000 goto error;
1001 }
1002
Jakub Grajciar12df4972019-07-01 14:24:48 +02001003 ms->type = MEMIF_SOCKET_TYPE_CLIENT;
1004
1005 return err;
Jakub Grajciar84b83772019-03-04 12:42:19 +01001006
1007error:
1008 if (sockfd > 0)
1009 close (sockfd);
1010 sockfd = -1;
1011 return err;
1012}
1013
1014int
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001015memif_control_fd_handler (int fd, uint8_t events)
1016{
Jakub Grajciar84b83772019-03-04 12:42:19 +01001017 int i, err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001018 uint16_t num;
1019 memif_list_elt_t *e = NULL;
1020 memif_connection_t *conn;
1021 libmemif_main_t *lm = &libmemif_main;
1022 if (fd == lm->timerfd)
1023 {
1024 uint64_t b;
1025 ssize_t size;
1026 size = read (fd, &b, sizeof (b));
msardara8f554b72018-12-11 18:36:55 +01001027
1028 if (size == -1)
Jakub Grajciar12df4972019-07-01 14:24:48 +02001029 goto error;
msardara8f554b72018-12-11 18:36:55 +01001030
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001031 for (i = 0; i < lm->control_list_len; i++)
1032 {
1033 if ((lm->control_list[i].key < 0)
1034 && (lm->control_list[i].data_struct != NULL))
1035 {
1036 conn = lm->control_list[i].data_struct;
1037 if (conn->args.is_master)
1038 continue;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001039 err = memif_request_connection (conn);
1040 if (err != MEMIF_ERR_SUCCESS)
1041 DBG ("memif_request_connection: %s", memif_strerror (err));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001042 }
1043 }
1044 }
1045 else
1046 {
1047 get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd);
1048 if (e != NULL)
1049 {
1050 if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL)
1051 {
1052 num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001053 (((memif_connection_t *) e->data_struct)->
1054 args.is_master) ? ((memif_connection_t *) e->
1055 data_struct)->run_args.
1056 num_s2m_rings : ((memif_connection_t *) e->data_struct)->
1057 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001058 for (i = 0; i < num; i++)
1059 {
Jakub Grajciar568cc462018-09-05 12:11:35 +02001060 if (((memif_connection_t *) e->data_struct)->
1061 rx_queues[i].int_fd == fd)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001062 {
Jakub Grajciar568cc462018-09-05 12:11:35 +02001063 ((memif_connection_t *) e->data_struct)->
1064 on_interrupt ((void *) e->data_struct,
1065 ((memif_connection_t *) e->
1066 data_struct)->private_ctx, i);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001067 return MEMIF_ERR_SUCCESS;
1068 }
1069 }
1070 }
1071 return MEMIF_ERR_SUCCESS;
1072 }
Jakub Grajciar12df4972019-07-01 14:24:48 +02001073 get_list_elt (&e, lm->socket_list, lm->socket_list_len, fd);
1074 if (e != NULL
1075 && ((memif_socket_t *) e->data_struct)->type ==
1076 MEMIF_SOCKET_TYPE_LISTENER)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001077 {
Jakub Grajciar12df4972019-07-01 14:24:48 +02001078 err =
1079 memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct);
Jakub Grajciar84b83772019-03-04 12:42:19 +01001080 return err;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001081 }
1082
1083 get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd);
1084 if (e != NULL)
1085 {
Jakub Grajciar84b83772019-03-04 12:42:19 +01001086 err = memif_read_ready (fd);
1087 return err;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001088 }
1089
1090 get_list_elt (&e, lm->control_list, lm->control_list_len, fd);
1091 if (e != NULL)
1092 {
1093 if (events & MEMIF_FD_EVENT_READ)
1094 {
1095 err =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001096 ((memif_connection_t *) e->data_struct)->
1097 read_fn (e->data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001098 if (err != MEMIF_ERR_SUCCESS)
1099 return err;
1100 }
1101 if (events & MEMIF_FD_EVENT_WRITE)
1102 {
1103 err =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001104 ((memif_connection_t *) e->data_struct)->
1105 write_fn (e->data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001106 if (err != MEMIF_ERR_SUCCESS)
1107 return err;
1108 }
1109 if (events & MEMIF_FD_EVENT_ERROR)
1110 {
1111 err =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001112 ((memif_connection_t *) e->data_struct)->
1113 error_fn (e->data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001114 if (err != MEMIF_ERR_SUCCESS)
1115 return err;
1116 }
1117 }
1118 }
1119
1120 return MEMIF_ERR_SUCCESS; /* 0 */
1121
1122error:
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001123 return err;
1124}
1125
1126int
1127memif_poll_event (int timeout)
1128{
msardara8f554b72018-12-11 18:36:55 +01001129 struct epoll_event evt;
1130 int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001131 uint32_t events = 0;
Milan Lenco0a47c992017-10-12 14:19:31 +02001132 uint64_t counter = 0;
1133 ssize_t r = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001134 memset (&evt, 0, sizeof (evt));
1135 evt.events = EPOLLIN | EPOLLOUT;
1136 sigset_t sigset;
1137 sigemptyset (&sigset);
1138 en = epoll_pwait (memif_epfd, &evt, 1, timeout, &sigset);
1139 if (en < 0)
1140 {
Milan Lenco0a47c992017-10-12 14:19:31 +02001141 err = errno;
1142 DBG ("epoll_pwait: %s", strerror (err));
1143 return memif_syscall_error_handler (err);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001144 }
1145 if (en > 0)
1146 {
Milan Lenco0a47c992017-10-12 14:19:31 +02001147 if (evt.data.fd == poll_cancel_fd)
1148 {
1149 r = read (evt.data.fd, &counter, sizeof (counter));
msardara8f554b72018-12-11 18:36:55 +01001150 if (r == -1)
1151 return MEMIF_ERR_DISCONNECTED;
1152
Milan Lenco0a47c992017-10-12 14:19:31 +02001153 return MEMIF_ERR_POLL_CANCEL;
1154 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001155 if (evt.events & EPOLLIN)
1156 events |= MEMIF_FD_EVENT_READ;
1157 if (evt.events & EPOLLOUT)
1158 events |= MEMIF_FD_EVENT_WRITE;
1159 if (evt.events & EPOLLERR)
1160 events |= MEMIF_FD_EVENT_ERROR;
1161 err = memif_control_fd_handler (evt.data.fd, events);
1162 return err;
1163 }
1164 return 0;
1165}
1166
Milan Lenco0a47c992017-10-12 14:19:31 +02001167int
1168memif_cancel_poll_event ()
1169{
1170 uint64_t counter = 1;
1171 ssize_t w = 0;
1172
1173 if (poll_cancel_fd == -1)
1174 return 0;
1175 w = write (poll_cancel_fd, &counter, sizeof (counter));
1176 if (w < sizeof (counter))
1177 return MEMIF_ERR_INT_WRITE;
1178
1179 return 0;
1180}
1181
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001182static void
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001183memif_msg_queue_free (libmemif_main_t * lm, memif_msg_queue_elt_t ** e)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001184{
1185 if (*e == NULL)
1186 return;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001187 memif_msg_queue_free (lm, &(*e)->next);
1188 lm->free (*e);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001189 *e = NULL;
1190 return;
1191}
1192
1193/* send disconnect msg and close interface */
1194int
1195memif_disconnect_internal (memif_connection_t * c)
1196{
1197 if (c == NULL)
1198 {
1199 DBG ("no connection");
1200 return MEMIF_ERR_NOCONN;
1201 }
1202 uint16_t num;
1203 int err = MEMIF_ERR_SUCCESS, i; /* 0 */
1204 memif_queue_t *mq;
1205 libmemif_main_t *lm = &libmemif_main;
1206 memif_list_elt_t *e;
1207
1208 c->on_disconnect ((void *) c, c->private_ctx);
1209
1210 if (c->fd > 0)
1211 {
Jakub Grajciar5e1f69a2018-04-12 13:56:29 +02001212 memif_msg_send_disconnect (c->fd, (uint8_t *) "interface deleted", 0);
Jakub Grajciar12df4972019-07-01 14:24:48 +02001213 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL, c->private_ctx);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001214 close (c->fd);
1215 }
1216 get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd);
1217 if (e != NULL)
1218 {
1219 if (c->args.is_master)
1220 free_list_elt (lm->control_list, lm->control_list_len, c->fd);
1221 e->key = c->fd = -1;
1222 }
1223
1224 if (c->tx_queues != NULL)
1225 {
1226 num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001227 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
1228 run_args.num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001229 for (i = 0; i < num; i++)
1230 {
1231 mq = &c->tx_queues[i];
1232 if (mq != NULL)
1233 {
1234 if (mq->int_fd > 0)
1235 close (mq->int_fd);
1236 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1237 mq->int_fd);
1238 mq->int_fd = -1;
1239 }
1240 }
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001241 lm->free (c->tx_queues);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001242 c->tx_queues = NULL;
1243 }
1244
1245 if (c->rx_queues != NULL)
1246 {
1247 num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001248 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
1249 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001250 for (i = 0; i < num; i++)
1251 {
1252 mq = &c->rx_queues[i];
1253 if (mq != NULL)
1254 {
1255 if (mq->int_fd > 0)
1256 {
1257 if (c->on_interrupt != NULL)
Jakub Grajciar12df4972019-07-01 14:24:48 +02001258 lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL,
1259 c->private_ctx);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001260 close (mq->int_fd);
1261 }
1262 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1263 mq->int_fd);
1264 mq->int_fd = -1;
1265 }
1266 }
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001267 lm->free (c->rx_queues);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001268 c->rx_queues = NULL;
1269 }
1270
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001271 for (i = 0; i < c->regions_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001272 {
Jakub Grajciar412e7b62018-09-27 10:26:35 +02001273 if (&c->regions[i] == NULL)
1274 continue;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001275 if (c->regions[i].is_external != 0)
1276 {
1277 lm->del_external_region (c->regions[i].addr,
1278 c->regions[i].region_size,
1279 c->regions[i].fd, c->private_ctx);
1280 }
1281 else
1282 {
1283 if (munmap (c->regions[i].addr, c->regions[i].region_size) < 0)
1284 return memif_syscall_error_handler (errno);
1285 if (c->regions[i].fd > 0)
1286 close (c->regions[i].fd);
1287 c->regions[i].fd = -1;
1288 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001289 }
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001290 lm->free (c->regions);
1291 c->regions = NULL;
1292 c->regions_num = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001293
1294 memset (&c->run_args, 0, sizeof (memif_conn_run_args_t));
1295
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001296 memif_msg_queue_free (lm, &c->msg_queue);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001297
1298 if (!(c->args.is_master))
1299 {
1300 if (lm->disconn_slaves == 0)
1301 {
1302 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
1303 {
1304 err = memif_syscall_error_handler (errno);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001305 DBG ("timerfd_settime: arm");
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001306 }
1307 }
1308 lm->disconn_slaves++;
1309 }
1310
1311 return err;
1312}
1313
1314int
Jakub Grajciar12df4972019-07-01 14:24:48 +02001315memif_delete_socket (memif_socket_handle_t * sock)
1316{
1317 memif_socket_t *ms = (memif_socket_t *) * sock;
1318 libmemif_main_t *lm = &libmemif_main;
1319
1320 /* check if socket is in use */
1321 if (ms == NULL || ms->type != MEMIF_SOCKET_TYPE_NONE)
1322 return MEMIF_ERR_INVAL_ARG;
1323
1324 lm->free (ms->interface_list);
1325 ms->interface_list = NULL;
1326 lm->free (ms->filename);
1327 ms->filename = NULL;
1328 lm->free (ms);
1329 *sock = ms = NULL;
1330
1331 return MEMIF_ERR_SUCCESS;
1332}
1333
1334int
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001335memif_delete (memif_conn_handle_t * conn)
1336{
1337 memif_connection_t *c = (memif_connection_t *) * conn;
Jakub Grajciar12df4972019-07-01 14:24:48 +02001338 libmemif_main_t *lm = &libmemif_main;
1339 memif_socket_t *ms = NULL;
1340 int err = MEMIF_ERR_SUCCESS;
1341
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001342 if (c == NULL)
1343 {
1344 DBG ("no connection");
1345 return MEMIF_ERR_NOCONN;
1346 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001347
1348 if (c->fd > 0)
1349 {
1350 DBG ("DISCONNECTING");
1351 err = memif_disconnect_internal (c);
1352 if (err == MEMIF_ERR_NOCONN)
1353 return err;
1354 }
1355
1356 free_list_elt_ctx (lm->control_list, lm->control_list_len, c);
1357
Jakub Grajciar12df4972019-07-01 14:24:48 +02001358 ms = (memif_socket_t *) c->args.socket;
1359 ms->use_count--;
1360 free_list_elt (ms->interface_list, ms->interface_list_len,
1361 c->args.interface_id);
1362 if (ms->use_count <= 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001363 {
Jakub Grajciar12df4972019-07-01 14:24:48 +02001364 /* stop listening on this socket */
1365 if (ms->type == MEMIF_SOCKET_TYPE_LISTENER)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001366 {
Jakub Grajciar12df4972019-07-01 14:24:48 +02001367 lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_DEL, ms->private_ctx);
1368 free_list_elt (lm->socket_list, lm->socket_list_len, ms->fd);
1369 close (ms->fd);
1370 ms->fd = -1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001371 }
Jakub Grajciar12df4972019-07-01 14:24:48 +02001372 /* socket not in use */
1373 ms->type = MEMIF_SOCKET_TYPE_NONE;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001374 }
Jakub Grajciar12df4972019-07-01 14:24:48 +02001375
1376 if (!c->args.is_master)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001377 {
1378 lm->disconn_slaves--;
1379 if (lm->disconn_slaves <= 0)
1380 {
1381 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0)
1382 {
1383 err = memif_syscall_error_handler (errno);
1384 DBG ("timerfd_settime: disarm");
1385 }
1386 }
1387 }
1388
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001389 lm->free (c);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001390 c = NULL;
1391
1392 *conn = c;
1393 return err;
1394}
1395
1396int
1397memif_connect1 (memif_connection_t * c)
1398{
1399 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001400 memif_region_t *mr;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001401 memif_queue_t *mq;
1402 int i;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001403
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001404 for (i = 0; i < c->regions_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001405 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001406 mr = &c->regions[i];
1407 if (mr != NULL)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001408 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001409 if (!mr->addr)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001410 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001411 if (mr->is_external)
1412 {
1413 if (lm->get_external_region_addr == NULL)
Jakub Grajciar12df4972019-07-01 14:24:48 +02001414 return MEMIF_ERR_INVAL_ARG;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001415 mr->addr =
1416 lm->get_external_region_addr (mr->region_size, mr->fd,
1417 c->private_ctx);
1418 }
1419 else
1420 {
1421 if (mr->fd < 0)
1422 return MEMIF_ERR_NO_SHMFD;
1423
1424 if ((mr->addr =
1425 mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE,
1426 MAP_SHARED, mr->fd, 0)) == MAP_FAILED)
1427 {
1428 return memif_syscall_error_handler (errno);
1429 }
1430 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001431 }
1432 }
1433 }
1434
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001435 for (i = 0; i < c->rx_queues_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001436 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001437 mq = &c->rx_queues[i];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001438 if (mq != NULL)
1439 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001440 mq->ring = c->regions[mq->region].addr + mq->offset;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001441 if (mq->ring->cookie != MEMIF_COOKIE)
1442 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001443 DBG ("wrong cookie on rx ring %u", i);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001444 return MEMIF_ERR_COOKIE;
1445 }
1446 mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
1447 0;
1448 }
1449 }
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001450
1451 for (i = 0; i < c->tx_queues_num; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001452 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001453 mq = &c->tx_queues[i];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001454 if (mq != NULL)
1455 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001456 mq->ring = c->regions[mq->region].addr + mq->offset;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001457 if (mq->ring->cookie != MEMIF_COOKIE)
1458 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001459 DBG ("wrong cookie on tx ring %u", i);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001460 return MEMIF_ERR_COOKIE;
1461 }
1462 mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
1463 0;
1464 }
1465 }
1466
Jakub Grajciar12df4972019-07-01 14:24:48 +02001467 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD,
1468 c->private_ctx);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001469
1470 return 0;
1471}
1472
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001473static inline int
1474memif_add_region (libmemif_main_t * lm, memif_connection_t * conn,
1475 uint8_t has_buffers)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001476{
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001477 memif_region_t *r;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001478
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001479 r =
1480 lm->realloc (conn->regions,
1481 sizeof (memif_region_t) * ++conn->regions_num);
1482 if (r == NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001483 return MEMIF_ERR_NOMEM;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001484
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001485 conn->regions = r;
1486 r = &conn->regions[conn->regions_num - 1];
1487 memset (r, 0, sizeof (memif_region_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001488
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001489 if (has_buffers != 0)
1490 {
1491 r->buffer_offset = 0;
1492 }
1493 else
1494 {
1495 r->buffer_offset =
1496 (conn->run_args.num_s2m_rings +
1497 conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) +
1498 sizeof (memif_desc_t) *
Jakub Grajciar568cc462018-09-05 12:11:35 +02001499 (1 << conn->
1500 run_args.log2_ring_size));
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001501 }
1502
1503 r->region_size = (has_buffers == 0) ? r->buffer_offset : r->buffer_offset +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001504 conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
1505 (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
1506
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001507 if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001508 return memif_syscall_error_handler (errno);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001509
1510 if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
1511 return memif_syscall_error_handler (errno);
1512
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001513 if ((ftruncate (r->fd, r->region_size)) == -1)
1514 return memif_syscall_error_handler (errno);
1515
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001516 if ((r->addr = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE,
1517 MAP_SHARED, r->fd, 0)) == MAP_FAILED)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001518 return memif_syscall_error_handler (errno);
1519
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001520 return MEMIF_ERR_SUCCESS;
1521}
1522
1523static inline int
1524memif_init_queues (libmemif_main_t * lm, memif_connection_t * conn)
1525{
1526 int i, j;
1527 memif_ring_t *ring;
1528
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001529 for (i = 0; i < conn->run_args.num_s2m_rings; i++)
1530 {
1531 ring = memif_get_ring (conn, MEMIF_RING_S2M, i);
1532 DBG ("RING: %p I: %d", ring, i);
1533 ring->head = ring->tail = 0;
1534 ring->cookie = MEMIF_COOKIE;
1535 ring->flags = 0;
1536 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1537 {
1538 uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001539 ring->desc[j].region = 1;
1540 ring->desc[j].offset =
1541 conn->regions[1].buffer_offset +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001542 (uint32_t) (slot * conn->run_args.buffer_size);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001543 ring->desc[j].length = conn->run_args.buffer_size;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001544 }
1545 }
1546 for (i = 0; i < conn->run_args.num_m2s_rings; i++)
1547 {
1548 ring = memif_get_ring (conn, MEMIF_RING_M2S, i);
1549 DBG ("RING: %p I: %d", ring, i);
1550 ring->head = ring->tail = 0;
1551 ring->cookie = MEMIF_COOKIE;
1552 ring->flags = 0;
1553 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1554 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001555 uint16_t slot = (i + conn->run_args.num_s2m_rings) *
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001556 (1 << conn->run_args.log2_ring_size) + j;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001557 ring->desc[j].region = 1;
1558 ring->desc[j].offset =
1559 conn->regions[1].buffer_offset +
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001560 (uint32_t) (slot * conn->run_args.buffer_size);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001561 ring->desc[j].length = conn->run_args.buffer_size;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001562 }
1563 }
1564 memif_queue_t *mq;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001565 DBG ("alloc: %p", lm->alloc);
1566 DBG ("size: %lu", sizeof (memif_queue_t) * conn->run_args.num_s2m_rings);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001567 mq =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001568 (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) *
1569 conn->run_args.num_s2m_rings);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001570 if (mq == NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001571 return MEMIF_ERR_NOMEM;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001572
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001573 int x;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001574 memif_list_elt_t e;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001575 for (x = 0; x < conn->run_args.num_s2m_rings; x++)
1576 {
1577 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1578 return memif_syscall_error_handler (errno);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001579 e.key = mq[x].int_fd;
1580 e.data_struct = conn;
1581 add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
1582
1583 mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x);
1584 DBG ("RING: %p I: %d", mq[x].ring, x);
1585 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1586 mq[x].region = 0;
1587 mq[x].offset =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001588 (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
1589 mq[x].last_head = mq[x].last_tail = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001590 mq[x].alloc_bufs = 0;
1591 }
1592 conn->tx_queues = mq;
1593
1594 mq =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001595 (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) *
1596 conn->run_args.num_m2s_rings);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001597 if (mq == NULL)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001598 return MEMIF_ERR_NOMEM;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001599
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001600 for (x = 0; x < conn->run_args.num_m2s_rings; x++)
1601 {
1602 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1603 return memif_syscall_error_handler (errno);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001604 e.key = mq[x].int_fd;
1605 e.data_struct = conn;
1606 add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
1607
1608 mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x);
1609 DBG ("RING: %p I: %d", mq[x].ring, x);
1610 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1611 mq[x].region = 0;
1612 mq[x].offset =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001613 (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
1614 mq[x].last_head = mq[x].last_tail = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001615 mq[x].alloc_bufs = 0;
1616 }
1617 conn->rx_queues = mq;
1618
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001619 return MEMIF_ERR_SUCCESS;
1620}
1621
1622int
1623memif_init_regions_and_queues (memif_connection_t * conn)
1624{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001625 memif_region_t *r;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001626 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001627
1628 /* region 0. rings */
1629 memif_add_region (lm, conn, /* has_buffers */ 0);
1630
1631 /* region 1. buffers */
1632 if (lm->add_external_region)
1633 {
1634 r =
1635 (memif_region_t *) lm->realloc (conn->regions,
1636 sizeof (memif_region_t) *
1637 ++conn->regions_num);
1638 if (r == NULL)
1639 return MEMIF_ERR_NOMEM;
1640 conn->regions = r;
1641
1642 conn->regions[1].region_size =
1643 conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
1644 (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
1645 conn->regions[1].buffer_offset = 0;
1646 lm->add_external_region (&conn->regions[1].addr,
1647 conn->regions[1].region_size,
1648 &conn->regions[1].fd, conn->private_ctx);
1649 conn->regions[1].is_external = 1;
1650 }
1651 else
1652 {
1653 memif_add_region (lm, conn, 1);
1654 }
1655
1656 memif_init_queues (lm, conn);
1657
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001658 return 0;
1659}
1660
1661int
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001662memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid,
1663 memif_buffer_t * bufs, uint16_t count,
1664 uint16_t * count_out)
1665{
1666 memif_connection_t *c = (memif_connection_t *) conn;
1667 if (EXPECT_FALSE (c == NULL))
1668 return MEMIF_ERR_NOCONN;
1669 if (EXPECT_FALSE (c->fd < 0))
1670 return MEMIF_ERR_DISCONNECTED;
1671 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001672 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
1673 run_args.num_s2m_rings;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001674 if (EXPECT_FALSE (qid >= num))
1675 return MEMIF_ERR_QID;
1676 if (EXPECT_FALSE (!count_out))
1677 return MEMIF_ERR_INVAL_ARG;
1678 if (EXPECT_FALSE (c->args.is_master))
1679 return MEMIF_ERR_INVAL_ARG;
1680
1681 memif_queue_t *mq = &c->tx_queues[qid];
1682 memif_ring_t *ring = mq->ring;
1683 memif_buffer_t *b0;
1684 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1685 uint16_t ring_size;
1686 uint16_t slot, ns;
msardara8f554b72018-12-11 18:36:55 +01001687 int err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001688 *count_out = 0;
1689
1690 ring_size = (1 << mq->log2_ring_size);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001691 slot = (c->args.is_master) ? ring->tail : ring->head;
1692 slot += mq->alloc_bufs;
1693
1694 /* can only be called by slave */
Milan Lencof8b43e52018-06-26 15:16:15 +02001695 ns = ring_size - (ring->head + mq->alloc_bufs) + ring->tail;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001696
1697 b0 = bufs;
1698
1699 while (count && ns)
1700 {
1701 if (EXPECT_FALSE ((b0->flags & MEMIF_BUFFER_FLAG_RX) == 0))
1702 {
1703 /* not a valid buffer */
1704 count--;
1705 continue;
1706 }
1707 b0->flags &= ~MEMIF_BUFFER_FLAG_RX;
1708
1709 ((memif_ring_t *) b0->ring)->desc[b0->desc_index & mask].offset = ring->desc[slot & mask].offset; /* put free buffer on rx ring */
1710
1711 ring->desc[slot & mask].offset =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001712 (uint32_t) (b0->data -
1713 c->regions[ring->desc[slot & mask].region].addr);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001714 ring->desc[slot & mask].flags |=
1715 (b0->flags & MEMIF_BUFFER_FLAG_NEXT) ? MEMIF_DESC_FLAG_NEXT : 0;
1716
1717 b0->desc_index = slot;
1718
1719 mq->alloc_bufs++;
1720 slot++;
1721
1722 count--;
1723 ns--;
1724 b0++;
1725 *count_out += 1;
1726 }
1727
1728 DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
1729 mq->alloc_bufs);
1730
1731 if (count)
1732 {
1733 DBG ("ring buffer full! qid: %u", qid);
1734 err = MEMIF_ERR_NOBUF_RING;
1735 }
1736
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001737 return err;
1738}
1739
1740int
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001741memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
1742 memif_buffer_t * bufs, uint16_t count,
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001743 uint16_t * count_out, uint16_t size)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001744{
1745 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001746 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001747 return MEMIF_ERR_NOCONN;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001748 if (EXPECT_FALSE (c->fd < 0))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001749 return MEMIF_ERR_DISCONNECTED;
1750 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001751 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
1752 run_args.num_s2m_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001753 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001754 return MEMIF_ERR_QID;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001755 if (EXPECT_FALSE (!count_out))
1756 return MEMIF_ERR_INVAL_ARG;
1757
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001758 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001759 memif_queue_t *mq = &c->tx_queues[qid];
1760 memif_ring_t *ring = mq->ring;
msardara8f554b72018-12-11 18:36:55 +01001761 memif_buffer_t *b0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001762 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001763 uint32_t offset_mask = c->run_args.buffer_size - 1;
Chun Li5dca5462018-02-07 09:51:35 +08001764 uint16_t ring_size;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001765 uint16_t slot, ns;
msardara8f554b72018-12-11 18:36:55 +01001766 int err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001767 uint16_t dst_left, src_left;
1768 uint16_t saved_count;
1769 memif_buffer_t *saved_b;
1770 *count_out = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001771
Chun Li5dca5462018-02-07 09:51:35 +08001772 ring_size = (1 << mq->log2_ring_size);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001773 slot = (c->args.is_master) ? ring->tail : ring->head;
1774 slot += mq->alloc_bufs;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001775
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001776 if (c->args.is_master)
Milan Lencof8b43e52018-06-26 15:16:15 +02001777 ns = ring->head - (ring->tail + mq->alloc_bufs);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001778 else
Milan Lencof8b43e52018-06-26 15:16:15 +02001779 ns = ring_size - (ring->head + mq->alloc_bufs) + ring->tail;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001780
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001781 while (count && ns)
1782 {
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001783 b0 = (bufs + *count_out);
1784
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001785 saved_b = b0;
1786 saved_count = count;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001787
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001788 b0->desc_index = slot;
1789 ring->desc[slot & mask].flags = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001790
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001791 /* slave can produce buffer with original length */
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001792 dst_left = (c->args.is_master) ? ring->desc[slot & mask].length :
1793 c->run_args.buffer_size;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001794 src_left = size;
1795
1796 while (src_left)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001797 {
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001798 if (EXPECT_FALSE (dst_left == 0))
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001799 {
1800 if (count && ns)
1801 {
1802 slot++;
1803 *count_out += 1;
1804 mq->alloc_bufs++;
1805 ns--;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001806
1807 ring->desc[b0->desc_index & mask].flags |=
1808 MEMIF_DESC_FLAG_NEXT;
1809 b0->flags |= MEMIF_BUFFER_FLAG_NEXT;
1810
1811 b0 = (bufs + *count_out);
1812 b0->desc_index = slot;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001813 dst_left =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001814 (c->args.is_master) ? ring->desc[slot & mask].
1815 length : c->run_args.buffer_size;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001816 ring->desc[slot & mask].flags = 0;
1817 }
1818 else
1819 {
1820 /* rollback allocated chain buffers */
1821 memset (saved_b, 0, sizeof (memif_buffer_t)
1822 * (saved_count - count + 1));
1823 *count_out -= saved_count - count;
1824 mq->alloc_bufs = saved_count - count;
1825 goto no_ns;
1826 }
1827 }
1828 b0->len = memif_min (dst_left, src_left);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001829
1830 /* slave resets buffer offset */
1831 if (c->args.is_master == 0)
1832 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001833 memif_desc_t *d = &ring->desc[slot & mask];
1834 if (lm->get_external_buffer_offset)
1835 d->offset = lm->get_external_buffer_offset (c->private_ctx);
1836 else
1837 d->offset = d->offset - (d->offset & offset_mask);
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001838 }
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001839 b0->data = memif_get_buffer (c, ring, slot & mask);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001840
1841 src_left -= b0->len;
1842 dst_left -= b0->len;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001843 }
1844
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001845 slot++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001846 *count_out += 1;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001847 mq->alloc_bufs++;
1848 ns--;
1849 count--;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001850 }
1851
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001852no_ns:
1853
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001854 DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
1855 mq->alloc_bufs);
1856
1857 if (count)
1858 {
1859 DBG ("ring buffer full! qid: %u", qid);
1860 err = MEMIF_ERR_NOBUF_RING;
1861 }
1862
1863 return err;
1864}
1865
1866int
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001867memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count,
1868 uint16_t headroom)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001869{
1870 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001871 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001872 return MEMIF_ERR_NOCONN;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001873 if (EXPECT_FALSE (c->fd < 0))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001874 return MEMIF_ERR_DISCONNECTED;
1875 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001876 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
1877 run_args.num_m2s_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001878 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001879 return MEMIF_ERR_QID;
1880 libmemif_main_t *lm = &libmemif_main;
1881 memif_queue_t *mq = &c->rx_queues[qid];
1882 memif_ring_t *ring = mq->ring;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001883 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001884 uint32_t offset_mask = c->run_args.buffer_size - 1;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001885 uint16_t slot;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001886
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001887 if (c->args.is_master)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001888 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001889 MEMIF_MEMORY_BARRIER ();
1890 ring->tail =
1891 (ring->tail + count <=
1892 mq->last_head) ? ring->tail + count : mq->last_head;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001893 return MEMIF_ERR_SUCCESS;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001894 }
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001895
1896 uint16_t head = ring->head;
1897 uint16_t ns = (1 << mq->log2_ring_size) - head + mq->last_tail;
1898 head += (count < ns) ? count : ns;
1899
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001900 slot = ring->head;
1901 memif_desc_t *d;
1902 while (slot < head)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001903 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02001904 d = &ring->desc[slot & mask];
1905 d->region = 1;
1906 d->length = c->run_args.buffer_size - headroom;
1907 if (lm->get_external_buffer_offset)
1908 d->offset = lm->get_external_buffer_offset (c->private_ctx);
1909 else
1910 d->offset = d->offset - (d->offset & offset_mask) + headroom;
1911 slot++;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001912 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001913
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001914 MEMIF_MEMORY_BARRIER ();
1915 ring->head = head;
1916
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001917 return MEMIF_ERR_SUCCESS; /* 0 */
1918}
1919
1920int
1921memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
1922 memif_buffer_t * bufs, uint16_t count, uint16_t * tx)
1923{
1924 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001925 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001926 return MEMIF_ERR_NOCONN;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001927 if (EXPECT_FALSE (c->fd < 0))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001928 return MEMIF_ERR_DISCONNECTED;
1929 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001930 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
1931 run_args.num_s2m_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001932 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001933 return MEMIF_ERR_QID;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001934 if (EXPECT_FALSE (!tx))
1935 return MEMIF_ERR_INVAL_ARG;
1936
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001937 memif_queue_t *mq = &c->tx_queues[qid];
1938 memif_ring_t *ring = mq->ring;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001939 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1940 memif_buffer_t *b0;
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001941 *tx = 0;
1942
1943 if (count > mq->alloc_bufs)
1944 count = mq->alloc_bufs;
1945
1946 if (EXPECT_FALSE (count == 0))
1947 return MEMIF_ERR_SUCCESS;
1948
1949 while (count)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001950 {
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001951 b0 = (bufs + *tx);
1952 ring->desc[b0->desc_index & mask].length = b0->len;
1953
1954#ifdef MEMIF_DBG_SHM
1955 printf ("offset: %-6d\n", ring->desc[b0->desc_index & mask].offset);
1956 printf ("data: %p\n",
1957 memif_get_buffer (c, ring, b0->desc_index & mask));
1958 printf ("index: %u\n", b0->desc_index);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001959 print_bytes (memif_get_buffer (c, ring, b0->desc_index & mask),
1960 ring->desc[b0->desc_index & mask].length, DBG_TX_BUF);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001961#endif /* MEMIF_DBG_SHM */
1962
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001963 *tx += 1;
1964 count--;
1965 }
1966
1967
Milan Lenco0a47c992017-10-12 14:19:31 +02001968 MEMIF_MEMORY_BARRIER ();
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001969 if (c->args.is_master)
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001970 ring->tail = b0->desc_index + 1;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001971 else
Jakub Grajciar3744fc72018-03-29 13:15:10 +02001972 ring->head = b0->desc_index + 1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001973
1974 mq->alloc_bufs -= *tx;
1975
1976 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
1977 {
1978 uint64_t a = 1;
1979 int r = write (mq->int_fd, &a, sizeof (a));
1980 if (r < 0)
1981 return MEMIF_ERR_INT_WRITE;
1982 }
1983
1984 return MEMIF_ERR_SUCCESS; /* 0 */
1985}
1986
1987int
1988memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
1989 memif_buffer_t * bufs, uint16_t count, uint16_t * rx)
1990{
1991 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001992 if (EXPECT_FALSE (c == NULL))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001993 return MEMIF_ERR_NOCONN;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001994 if (EXPECT_FALSE (c->fd < 0))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001995 return MEMIF_ERR_DISCONNECTED;
1996 uint8_t num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02001997 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
1998 run_args.num_m2s_rings;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02001999 if (EXPECT_FALSE (qid >= num))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002000 return MEMIF_ERR_QID;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002001 if (EXPECT_FALSE (!rx))
2002 return MEMIF_ERR_INVAL_ARG;
2003
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002004 memif_queue_t *mq = &c->rx_queues[qid];
2005 memif_ring_t *ring = mq->ring;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002006 uint16_t cur_slot, last_slot;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002007 uint16_t ns;
2008 uint16_t mask = (1 << mq->log2_ring_size) - 1;
msardara8f554b72018-12-11 18:36:55 +01002009 memif_buffer_t *b0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002010 *rx = 0;
2011
2012 uint64_t b;
2013 ssize_t r = read (mq->int_fd, &b, sizeof (b));
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002014 if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN)))
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002015 return memif_syscall_error_handler (errno);
2016
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002017 cur_slot = (c->args.is_master) ? mq->last_head : mq->last_tail;
2018 last_slot = (c->args.is_master) ? ring->head : ring->tail;
2019 if (cur_slot == last_slot)
2020 return MEMIF_ERR_SUCCESS;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002021
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002022 ns = last_slot - cur_slot;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002023
2024 while (ns && count)
2025 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002026 b0 = (bufs + *rx);
2027
2028 b0->desc_index = cur_slot;
2029 b0->data = memif_get_buffer (c, ring, cur_slot & mask);
2030 b0->len = ring->desc[cur_slot & mask].length;
2031 /* slave resets buffer length */
2032 if (c->args.is_master == 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002033 {
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002034 ring->desc[cur_slot & mask].length = c->run_args.buffer_size;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002035 }
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002036
2037 b0->flags = MEMIF_BUFFER_FLAG_RX;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002038 if (ring->desc[cur_slot & mask].flags & MEMIF_DESC_FLAG_NEXT)
2039 {
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002040 b0->flags |= MEMIF_BUFFER_FLAG_NEXT;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002041 ring->desc[cur_slot & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
2042 }
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002043/* b0->offset = ring->desc[cur_slot & mask].offset;*/
2044 b0->ring = ring;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02002045#ifdef MEMIF_DBG_SHM
Jakub Grajciar3744fc72018-03-29 13:15:10 +02002046 printf ("data: %p\n", b0->data);
2047 printf ("index: %u\n", b0->desc_index);
2048 printf ("ring: %p\n", b0->ring);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002049 print_bytes (b0->data, b0->len, DBG_RX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02002050#endif /* MEMIF_DBG_SHM */
2051 ns--;
2052 *rx += 1;
2053
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002054 count--;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002055 cur_slot++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002056 }
2057
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002058 if (c->args.is_master)
2059 mq->last_head = cur_slot;
2060 else
2061 mq->last_tail = cur_slot;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02002062
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002063 if (ns)
2064 {
2065 DBG ("not enough buffers!");
2066 return MEMIF_ERR_NOBUF;
2067 }
2068
2069 return MEMIF_ERR_SUCCESS; /* 0 */
2070}
2071
2072int
2073memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
2074 char *buf, ssize_t buflen)
2075{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002076 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002077 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002078 memif_socket_t *ms;
2079 int err = MEMIF_ERR_SUCCESS, i;
2080 ssize_t l0 = 0, l1;
2081
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002082 if (c == NULL)
2083 return MEMIF_ERR_NOCONN;
2084
Jakub Grajciar12df4972019-07-01 14:24:48 +02002085 ms = (memif_socket_t *) c->args.socket;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002086
2087 l1 = strlen ((char *) c->args.interface_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02002088 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002089 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002090 md->if_name =
2091 (uint8_t *) strcpy (buf + l0, (char *) c->args.interface_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002092 l0 += l1 + 1;
2093 }
2094 else
2095 err = MEMIF_ERR_NOBUF_DET;
2096
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002097 l1 = strlen ((char *) lm->app_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02002098 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002099 {
Jakub Grajciar5e1f69a2018-04-12 13:56:29 +02002100 md->inst_name = (uint8_t *) strcpy (buf + l0, (char *) lm->app_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002101 l0 += l1 + 1;
2102 }
2103 else
2104 err = MEMIF_ERR_NOBUF_DET;
2105
2106 l1 = strlen ((char *) c->remote_if_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02002107 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002108 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002109 md->remote_if_name =
2110 (uint8_t *) strcpy (buf + l0, (char *) c->remote_if_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002111 l0 += l1 + 1;
2112 }
2113 else
2114 err = MEMIF_ERR_NOBUF_DET;
2115
2116 l1 = strlen ((char *) c->remote_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02002117 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002118 {
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002119 md->remote_inst_name =
2120 (uint8_t *) strcpy (buf + l0, (char *) c->remote_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002121 l0 += l1 + 1;
2122 }
2123 else
2124 err = MEMIF_ERR_NOBUF_DET;
2125
2126 md->id = c->args.interface_id;
2127
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002128 if (strlen ((char *) c->args.secret) > 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002129 {
2130 l1 = strlen ((char *) c->args.secret);
Milan Lenco0a47c992017-10-12 14:19:31 +02002131 if (l0 + l1 < buflen)
2132 {
Jakub Grajciar5e1f69a2018-04-12 13:56:29 +02002133 md->secret = (uint8_t *) strcpy (buf + l0, (char *) c->args.secret);
Milan Lenco0a47c992017-10-12 14:19:31 +02002134 l0 += l1 + 1;
2135 }
2136 else
2137 err = MEMIF_ERR_NOBUF_DET;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002138 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002139
2140 md->role = (c->args.is_master) ? 0 : 1;
2141 md->mode = c->args.mode;
2142
Jakub Grajciar12df4972019-07-01 14:24:48 +02002143 l1 = strlen ((char *) ms->filename);
Milan Lenco0a47c992017-10-12 14:19:31 +02002144 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002145 {
2146 md->socket_filename =
Jakub Grajciar12df4972019-07-01 14:24:48 +02002147 (uint8_t *) strcpy (buf + l0, (char *) ms->filename);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002148 l0 += l1 + 1;
2149 }
2150 else
2151 err = MEMIF_ERR_NOBUF_DET;
2152
Jakub Grajciar568cc462018-09-05 12:11:35 +02002153 l1 = strlen ((char *) c->remote_disconnect_string);
2154 if (l0 + l1 < buflen)
2155 {
2156 md->error =
2157 (uint8_t *) strcpy (buf + l0, (char *) c->remote_disconnect_string);
2158 l0 += l1 + 1;
2159 }
2160 else
2161 err = MEMIF_ERR_NOBUF_DET;
2162
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002163 md->regions_num = c->regions_num;
2164 l1 = sizeof (memif_region_details_t) * md->regions_num;
2165 if (l0 + l1 <= buflen)
2166 {
Koichiro Den33331ed2019-01-03 00:22:24 +09002167 md->regions = (memif_region_details_t *) (buf + l0);
Koichiro Denbe420e52018-12-15 00:31:34 +09002168 for (i = 0; i < md->regions_num; i++)
Jakub Grajciar12df4972019-07-01 14:24:48 +02002169 {
2170 md->regions[i].index = i;
2171 md->regions[i].addr = c->regions[i].addr;
2172 md->regions[i].size = c->regions[i].region_size;
2173 md->regions[i].fd = c->regions[i].fd;
2174 md->regions[i].is_external = c->regions[i].is_external;
2175 }
Jakub Grajciar93a5dd12018-08-20 14:26:32 +02002176 l0 += l1;
2177 }
2178 else
2179 err = MEMIF_ERR_NOBUF_DET;
2180
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002181 md->rx_queues_num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002182 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2183 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002184
2185 l1 = sizeof (memif_queue_details_t) * md->rx_queues_num;
2186 if (l0 + l1 <= buflen)
2187 {
Koichiro Den33331ed2019-01-03 00:22:24 +09002188 md->rx_queues = (memif_queue_details_t *) (buf + l0);
Koichiro Denbe420e52018-12-15 00:31:34 +09002189 for (i = 0; i < md->rx_queues_num; i++)
Jakub Grajciar12df4972019-07-01 14:24:48 +02002190 {
2191 md->rx_queues[i].region = c->rx_queues[i].region;
2192 md->rx_queues[i].qid = i;
2193 md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size);
2194 md->rx_queues[i].flags = c->rx_queues[i].ring->flags;
2195 md->rx_queues[i].head = c->rx_queues[i].ring->head;
2196 md->rx_queues[i].tail = c->rx_queues[i].ring->tail;
2197 md->rx_queues[i].buffer_size = c->run_args.buffer_size;
2198 }
Milan Lenco0a47c992017-10-12 14:19:31 +02002199 l0 += l1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002200 }
2201 else
2202 err = MEMIF_ERR_NOBUF_DET;
2203
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002204 md->tx_queues_num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002205 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
2206 run_args.num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002207
2208 l1 = sizeof (memif_queue_details_t) * md->tx_queues_num;
2209 if (l0 + l1 <= buflen)
2210 {
Koichiro Den33331ed2019-01-03 00:22:24 +09002211 md->tx_queues = (memif_queue_details_t *) (buf + l0);
Koichiro Denbe420e52018-12-15 00:31:34 +09002212 for (i = 0; i < md->tx_queues_num; i++)
Jakub Grajciar12df4972019-07-01 14:24:48 +02002213 {
2214 md->tx_queues[i].region = c->tx_queues[i].region;
2215 md->tx_queues[i].qid = i;
2216 md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size);
2217 md->tx_queues[i].flags = c->tx_queues[i].ring->flags;
2218 md->tx_queues[i].head = c->tx_queues[i].ring->head;
2219 md->tx_queues[i].tail = c->tx_queues[i].ring->tail;
2220 md->tx_queues[i].buffer_size = c->run_args.buffer_size;
2221 }
Milan Lenco0a47c992017-10-12 14:19:31 +02002222 l0 += l1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002223 }
2224 else
2225 err = MEMIF_ERR_NOBUF_DET;
2226
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002227 md->link_up_down = (c->fd > 0) ? 1 : 0;
2228
2229 return err; /* 0 */
2230}
2231
2232int
2233memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd)
2234{
2235 memif_connection_t *c = (memif_connection_t *) conn;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002236 uint8_t num;
2237
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002238 *efd = -1;
2239 if (c == NULL)
2240 return MEMIF_ERR_NOCONN;
2241 if (c->fd < 0)
2242 return MEMIF_ERR_DISCONNECTED;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002243
2244 num =
Jakub Grajciar568cc462018-09-05 12:11:35 +02002245 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2246 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002247 if (qid >= num)
2248 return MEMIF_ERR_QID;
2249
2250 *efd = c->rx_queues[qid].int_fd;
2251
2252 return MEMIF_ERR_SUCCESS;
2253}
2254
2255int
2256memif_cleanup ()
2257{
2258 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002259
2260 memif_delete_socket ((memif_socket_handle_t *) &lm->default_socket);
2261
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002262 if (lm->control_list)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002263 lm->free (lm->control_list);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002264 lm->control_list = NULL;
2265 if (lm->interrupt_list)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002266 lm->free (lm->interrupt_list);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002267 lm->interrupt_list = NULL;
Jakub Grajciar12df4972019-07-01 14:24:48 +02002268 if (lm->socket_list)
2269 lm->free (lm->socket_list);
2270 lm->socket_list = NULL;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002271 if (lm->pending_list)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +02002272 lm->free (lm->pending_list);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002273 lm->pending_list = NULL;
Milan Lenco0a47c992017-10-12 14:19:31 +02002274 if (poll_cancel_fd != -1)
2275 close (poll_cancel_fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002276
2277 return MEMIF_ERR_SUCCESS; /* 0 */
2278}