blob: cb24083c8e3b8cadaf3015e1de9607f8784e8e28 [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
18#include <stdint.h>
19#include <net/if.h>
20#include <sys/types.h>
21#include <fcntl.h>
22#include <sys/ioctl.h>
23#include <sys/socket.h>
24#include <sys/un.h>
25#include <sys/uio.h>
26#include <sys/mman.h>
27#include <sys/prctl.h>
28#include <inttypes.h>
29#include <string.h>
30#include <stdio.h>
31#include <netdb.h>
32#include <linux/ip.h>
33#include <linux/icmp.h>
34#include <arpa/inet.h>
35#include <stdlib.h>
36#include <netinet/if_ether.h>
37#include <net/if_arp.h>
38#include <asm/byteorder.h>
39#include <byteswap.h>
40#include <string.h>
41#include <errno.h>
42#include <sys/stat.h>
43#include <sys/eventfd.h>
44#include <sys/timerfd.h>
45#include <sys/epoll.h>
46#include <signal.h>
47
48/* memif protocol msg, ring and descriptor definitions */
49#include <memif.h>
50/* memif api */
51#include <libmemif.h>
52/* socket messaging functions */
53#include <socket.h>
54/* private structs and functions */
55#include <memif_private.h>
56
Damjan Marion6d56fa42017-11-03 12:24:37 +010057#define ERRLIST_LEN 38
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020058#define MAX_ERRBUF_LEN 256
59
60#if __x86_x64__
61#define MEMIF_MEMORY_BARRIER() __builtin_ia32_sfence ()
62#else
Milan Lenco0a47c992017-10-12 14:19:31 +020063#define MEMIF_MEMORY_BARRIER() __sync_synchronize ()
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020064#endif /* __x86_x64__ */
65
66libmemif_main_t libmemif_main;
67int memif_epfd;
Milan Lenco0a47c992017-10-12 14:19:31 +020068int poll_cancel_fd = -1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020069
70static char memif_buf[MAX_ERRBUF_LEN];
71
72const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */
73 "Success.",
74 /* MEMIF_ERR_SYSCALL */
75 "Unspecified syscall error (build with -DMEMIF_DBG or make debug).",
76 /* MEMIF_ERR_ACCES */
77 "Permission to resoure denied.",
78 /* MEMIF_ERR_NO_FILE */
79 "Socket file does not exist",
80 /* MEMIF_ERR_FILE_LIMIT */
81 "System limit on total numer of open files reached.",
82 /* MEMIF_ERR_PROC_FILE_LIMIT */
83 "Per-process limit on total number of open files reached.",
84 /* MEMIF_ERR_ALREADY */
85 "Connection already requested.",
86 /* MEMIF_ERR_AGAIN */
87 "File descriptor refers to file other than socket, or operation would block.",
88 /* MEMIF_ERR_BAD_FD */
89 "Bad file descriptor.",
90 /* MEMIF_ERR_NOMEM */
91 "Out of memory.",
92 /* MEMIF_ERR_INVAL_ARG */
93 "Invalid argument.",
94 /* MEMIF_ERR_NOCONN */
95 "Memif connection handle does not point to existing conenction",
96 /* MEMIF_ERR_CONN */
97 "Memif connection handle points to existing connection",
98 /* MEMIF_ERR_CB_FDUPDATE */
99 "Callback memif_control_fd_update_t returned error",
100 /* MEMIF_ERR_FILE_NOT_SOCK */
101 "File specified by socket filename exists and is not socket.",
102 /* MEMIF_ERR_NO_SHMFD */
103 "Missing shared memory file descriptor. (internal error)",
104 /* MEMIF_ERR_COOKIE */
105 "Invalid cookie on ring. (internal error)",
106 /* MEMIF_ERR_NOBUF_RING */
107 "Ring buffer full.",
108 /* MEMIF_ERR_NOBUF */
109 "Not enough memif buffers. There are unreceived data in shared memory.",
110 /* MEMIF_ERR_NOBUF_DET */
111 "Not enough space for memif details in suplied buffer. String data might be malformed.",
112 /* MEMIF_ERR_INT_WRITE */
113 "Send interrupt error.",
114 /* MEMIF_ERR_MFMSG */
115 "Malformed message received on control channel.",
116 /* MEMIF_ERR_QID */
117 "Invalid queue id",
118 /* MEMIF_ERR_PROTO */
119 "Incompatible memory interface protocol version.",
120 /* MEMIF_ERR_ID */
121 "Unmatched interface id.",
122 /* MEMIF_ERR_ACCSLAVE */
123 "Slave cannot accept connection reqest.",
124 /* MEMIF_ERR_ALRCONN */
125 "Interface is already connected.",
126 /* MEMIF_ERR_MODE */
127 "Mode mismatch.",
128 /* MEMIF_ERR_SECRET */
129 "Secret mismatch.",
130 /* MEMIF_ERR_NOSECRET */
131 "Secret required.",
132 /* MEMIF_ERR_MAXREG */
133 "Limit on total number of regions reached.",
134 /* MEMIF_ERR_MAXRING */
135 "Limit on total number of ring reached.",
136 /* MEMIF_ERR_NO_INTFD */
137 "Missing interrupt file descriptor. (internal error)",
138 /* MEMIF_ERR_DISCONNECT */
139 "Interface received disconnect request.",
140 /* MEMIF_ERR_DISCONNECTED */
141 "Interface is disconnected.",
142 /* MEMIF_ERR_UNKNOWN_MSG */
Milan 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 */
147 "Maximum log2 ring size is 15"
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200148};
149
150#define MEMIF_ERR_UNDEFINED "undefined error"
151
152char *
153memif_strerror (int err_code)
154{
155 if (err_code >= ERRLIST_LEN)
156 {
157 strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED));
158 memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0';
159 }
160 else
161 {
162 strncpy (memif_buf, memif_errlist[err_code],
163 strlen (memif_errlist[err_code]));
164 memif_buf[strlen (memif_errlist[err_code])] = '\0';
165 }
166 return memif_buf;
167}
168
169#define DBG_TX_BUF (0)
170#define DBG_RX_BUF (1)
171
172#ifdef MEMIF_DBG_SHM
173static void
174print_bytes (void *data, uint16_t len, uint8_t q)
175{
176 if (q == DBG_TX_BUF)
177 printf ("\nTX:\n\t");
178 else
179 printf ("\nRX:\n\t");
180 int i;
181 for (i = 0; i < len; i++)
182 {
183 if (i % 8 == 0)
184 printf ("\n%d:\t", i);
185 printf ("%02X ", ((uint8_t *) (data))[i]);
186 }
187 printf ("\n\n");
188}
Jakub Grajciarba3c4e82017-09-18 11:21:40 +0200189#endif /* MEMIF_DBG_SHM */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200190
191int
192memif_syscall_error_handler (int err_code)
193{
194 DBG_UNIX ("%s", strerror (err_code));
195
196 if (err_code == 0)
197 return MEMIF_ERR_SUCCESS;
198 if (err_code == EACCES)
199 return MEMIF_ERR_ACCES;
200 if (err_code == ENFILE)
201 return MEMIF_ERR_FILE_LIMIT;
202 if (err_code == EMFILE)
203 return MEMIF_ERR_PROC_FILE_LIMIT;
204 if (err_code == ENOMEM)
205 return MEMIF_ERR_NOMEM;
Damjan Marion6d56fa42017-11-03 12:24:37 +0100206/* connection refused if master does not exist
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200207 this error would spam the user until master was created */
208 if (err_code == ECONNREFUSED)
209 return MEMIF_ERR_SUCCESS;
210 if (err_code == EALREADY)
211 return MEMIF_ERR_ALREADY;
212 if (err_code == EAGAIN)
213 return MEMIF_ERR_AGAIN;
214 if (err_code == EBADF)
215 return MEMIF_ERR_BAD_FD;
216 if (err_code == ENOENT)
217 return MEMIF_ERR_NO_FILE;
218
219 /* other syscall errors */
220 return MEMIF_ERR_SYSCALL;
221}
222
223static int
224memif_add_epoll_fd (int fd, uint32_t events)
225{
226 if (fd < 0)
227 {
228 DBG ("invalid fd %d", fd);
229 return -1;
230 }
231 struct epoll_event evt;
232 memset (&evt, 0, sizeof (evt));
233 evt.events = events;
234 evt.data.fd = fd;
235 if (epoll_ctl (memif_epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
236 {
237 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
238 return -1;
239 }
240 DBG ("fd %d added to epoll", fd);
241 return 0;
242}
243
244static int
245memif_mod_epoll_fd (int fd, uint32_t events)
246{
247 if (fd < 0)
248 {
249 DBG ("invalid fd %d", fd);
250 return -1;
251 }
252 struct epoll_event evt;
253 memset (&evt, 0, sizeof (evt));
254 evt.events = events;
255 evt.data.fd = fd;
256 if (epoll_ctl (memif_epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
257 {
258 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
259 return -1;
260 }
261 DBG ("fd %d moddified on epoll", fd);
262 return 0;
263}
264
265static int
266memif_del_epoll_fd (int fd)
267{
268 if (fd < 0)
269 {
270 DBG ("invalid fd %d", fd);
271 return -1;
272 }
273 struct epoll_event evt;
274 memset (&evt, 0, sizeof (evt));
275 if (epoll_ctl (memif_epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
276 {
277 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
278 return -1;
279 }
280 DBG ("fd %d removed from epoll", fd);
281 return 0;
282}
283
284int
285memif_control_fd_update (int fd, uint8_t events)
286{
287 if (events & MEMIF_FD_EVENT_DEL)
288 return memif_del_epoll_fd (fd);
289
290 uint32_t evt = 0;
291 if (events & MEMIF_FD_EVENT_READ)
292 evt |= EPOLLIN;
293 if (events & MEMIF_FD_EVENT_WRITE)
294 evt |= EPOLLOUT;
295
296 if (events & MEMIF_FD_EVENT_MOD)
297 return memif_mod_epoll_fd (fd, evt);
298
299 return memif_add_epoll_fd (fd, evt);
300}
301
302int
303add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list, uint16_t * len)
304{
305 libmemif_main_t *lm = &libmemif_main;
306
307 int i;
308 for (i = 0; i < *len; i++)
309 {
310 if ((*list)[i].data_struct == NULL)
311 {
312 (*list)[i].key = e->key;
313 (*list)[i].data_struct = e->data_struct;
314 return i;
315 }
316 }
317 memif_list_elt_t *tmp;
318 tmp = realloc (*list, sizeof (memif_list_elt_t) * *len * 2);
319 if (tmp == NULL)
320 return -1;
321
322 for (i = *len; i < *len * 2; i++)
323 {
324 tmp[i].key = -1;
325 tmp[i].data_struct = NULL;
326 }
327
328 tmp[*len].key = e->key;
329 tmp[*len].data_struct = e->data_struct;
330 i = *len;
331 *len = *len * 2;
332 *list = tmp;
333
334 return i;
335}
336
337int
338get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list, uint16_t len,
339 int key)
340{
341 if (key == -1)
342 {
343 *e = NULL;
344 return -1;
345 }
346 int i;
347 for (i = 0; i < len; i++)
348 {
349 if (list[i].key == key)
350 {
351 *e = &list[i];
352 return 0;
353 }
354 }
355 *e = NULL;
356 return -1;
357}
358
359/* does not free memory, only marks element as free */
360int
361free_list_elt (memif_list_elt_t * list, uint16_t len, int key)
362{
363 int i;
364 for (i = 0; i < len; i++)
365 {
366 if (list[i].key == key)
367 {
368 list[i].key = -1;
369 list[i].data_struct = NULL;
370 return 0;
371 }
372 }
373
374 return -1;
375}
376
377int
378free_list_elt_ctx (memif_list_elt_t * list, uint16_t len,
379 memif_connection_t * ctx)
380{
381 int i;
382 for (i = 0; i < len; i++)
383 {
384 if (list[i].key == -1)
385 {
386 if (list[i].data_struct == ctx)
387 {
388 list[i].data_struct = NULL;
389 return 0;
390 }
391 }
392 }
393
394 return -1;
395}
396
397static void
398memif_control_fd_update_register (memif_control_fd_update_t * cb)
399{
400 libmemif_main_t *lm = &libmemif_main;
401 lm->control_fd_update = cb;
402}
403
404int
405memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name)
406{
407 int err = MEMIF_ERR_SUCCESS; /* 0 */
408 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar19418712018-03-13 13:57:50 +0100409 memset (lm, 0, sizeof (libmemif_main_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200410
411 if (app_name)
412 {
Jakub Grajciar19418712018-03-13 13:57:50 +0100413 uint8_t len = (strlen (app_name) < MEMIF_NAME_LEN)
414 ? MEMIF_NAME_LEN : strlen (app_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200415 strncpy ((char *) lm->app_name, app_name, strlen (app_name));
416 }
417 else
418 {
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200419 strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME,
420 strlen (MEMIF_DEFAULT_APP_NAME));
421 }
422
423 /* register control fd update callback */
424 if (on_control_fd_update != NULL)
425 memif_control_fd_update_register (on_control_fd_update);
426 else
427 {
428 memif_epfd = epoll_create (1);
429 memif_control_fd_update_register (memif_control_fd_update);
Milan Lenco0a47c992017-10-12 14:19:31 +0200430 if ((poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0)
431 {
432 err = errno;
433 DBG ("eventfd: %s", strerror (err));
434 return memif_syscall_error_handler (err);
435 }
436 lm->control_fd_update (poll_cancel_fd, MEMIF_FD_EVENT_READ);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200437 DBG ("libmemif event polling initialized");
438 }
439
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200440 lm->control_list_len = 2;
441 lm->interrupt_list_len = 2;
442 lm->listener_list_len = 1;
443 lm->pending_list_len = 1;
444
445 lm->control_list =
446 malloc (sizeof (memif_list_elt_t) * lm->control_list_len);
447 lm->interrupt_list =
448 malloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len);
449 lm->listener_list =
450 malloc (sizeof (memif_list_elt_t) * lm->listener_list_len);
451 lm->pending_list =
452 malloc (sizeof (memif_list_elt_t) * lm->pending_list_len);
453
454 int i;
455 for (i = 0; i < lm->control_list_len; i++)
456 {
457 lm->control_list[i].key = -1;
458 lm->control_list[i].data_struct = NULL;
459 }
460 for (i = 0; i < lm->interrupt_list_len; i++)
461 {
462 lm->interrupt_list[i].key = -1;
463 lm->interrupt_list[i].data_struct = NULL;
464 }
465 for (i = 0; i < lm->listener_list_len; i++)
466 {
467 lm->listener_list[i].key = -1;
468 lm->listener_list[i].data_struct = NULL;
469 }
470 for (i = 0; i < lm->pending_list_len; i++)
471 {
472 lm->pending_list[i].key = -1;
473 lm->pending_list[i].data_struct = NULL;
474 }
475
476 lm->disconn_slaves = 0;
477
478 lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK);
479 if (lm->timerfd < 0)
480 {
481 err = errno;
482 DBG ("timerfd: %s", strerror (err));
483 return memif_syscall_error_handler (err);
484 }
485
486 lm->arm.it_value.tv_sec = 2;
487 lm->arm.it_value.tv_nsec = 0;
488 lm->arm.it_interval.tv_sec = 2;
489 lm->arm.it_interval.tv_nsec = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200490
491 if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ) < 0)
492 {
493 DBG ("callback type memif_control_fd_update_t error!");
494 return MEMIF_ERR_CB_FDUPDATE;
495 }
496
497 return 0;
498}
499
500static inline memif_ring_t *
501memif_get_ring (memif_connection_t * conn, memif_ring_type_t type,
502 uint16_t ring_num)
503{
504 if (&conn->regions[0] == NULL)
505 return NULL;
506 void *p = conn->regions[0].shm;
507 int ring_size =
508 sizeof (memif_ring_t) +
509 sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size);
510 p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size;
511
512 return (memif_ring_t *) p;
513}
514
515int
516memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode,
517 uint16_t qid)
518{
519 memif_connection_t *conn = (memif_connection_t *) c;
520 if (conn == NULL)
521 return MEMIF_ERR_NOCONN;
522 uint8_t num =
Jakub Grajciar84197552017-11-16 14:02:49 +0100523 (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->
524 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200525 if (qid >= num)
526 return MEMIF_ERR_QID;
527
528 conn->rx_queues[qid].ring->flags = rx_mode;
529 DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags);
530 return MEMIF_ERR_SUCCESS;
531}
532
533int
534memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
535 memif_connection_update_t * on_connect,
536 memif_connection_update_t * on_disconnect,
537 memif_interrupt_t * on_interrupt, void *private_ctx)
538{
539 int err, i, index, sockfd = -1;
540 memif_list_elt_t list_elt;
541 memif_connection_t *conn = (memif_connection_t *) * c;
542 if (conn != NULL)
543 {
544 DBG ("This handle already points to existing memif.");
545 return MEMIF_ERR_CONN;
546 }
547 conn = (memif_connection_t *) malloc (sizeof (memif_connection_t));
548 if (conn == NULL)
549 {
550 err = memif_syscall_error_handler (errno);
551 goto error;
552 }
553 memset (conn, 0, sizeof (memif_connection_t));
554
555 libmemif_main_t *lm = &libmemif_main;
556
557 conn->args.interface_id = args->interface_id;
558
559 if (args->log2_ring_size == 0)
560 args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE;
Damjan Marion6d56fa42017-11-03 12:24:37 +0100561 else if (args->log2_ring_size > MEMIF_MAX_LOG2_RING_SIZE)
562 {
563 err = MEMIF_ERR_MAX_RING;
564 goto error;
565 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200566 if (args->buffer_size == 0)
567 args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
568 if (args->num_s2m_rings == 0)
569 args->num_s2m_rings = MEMIF_DEFAULT_TX_QUEUES;
570 if (args->num_m2s_rings == 0)
571 args->num_m2s_rings = MEMIF_DEFAULT_RX_QUEUES;
572
573 conn->args.num_s2m_rings = args->num_s2m_rings;
574 conn->args.num_m2s_rings = args->num_m2s_rings;
575 conn->args.buffer_size = args->buffer_size;
576 conn->args.log2_ring_size = args->log2_ring_size;
577 conn->args.is_master = args->is_master;
578 conn->args.mode = args->mode;
579 conn->msg_queue = NULL;
580 conn->regions = NULL;
581 conn->tx_queues = NULL;
582 conn->rx_queues = NULL;
583 conn->fd = -1;
584 conn->on_connect = on_connect;
585 conn->on_disconnect = on_disconnect;
586 conn->on_interrupt = on_interrupt;
587 conn->private_ctx = private_ctx;
588 memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t));
589
590 uint8_t l = strlen ((char *) args->interface_name);
591 strncpy ((char *) conn->args.interface_name, (char *) args->interface_name,
592 l);
593
594 l = strlen ((char *) args->instance_name);
595 strncpy ((char *) conn->args.instance_name, (char *) args->instance_name,
596 l);
597
598 /* allocate and initialize socket_filename so it can be copyed to sun_path
599 without memory leaks */
600 conn->args.socket_filename = malloc (sizeof (char *) * 108);
601 memset (conn->args.socket_filename, 0, 108 * sizeof (char *));
602
603 if (args->socket_filename)
604 {
605 if (conn->args.socket_filename == NULL)
606 {
607 err = memif_syscall_error_handler (errno);
608 goto error;
609 }
610 strncpy ((char *) conn->args.socket_filename,
611 (char *) args->socket_filename,
612 strlen ((char *) args->socket_filename));
613 }
614 else
615 {
616 uint16_t sdl = strlen (MEMIF_DEFAULT_SOCKET_DIR);
617 uint16_t sfl = strlen (MEMIF_DEFAULT_SOCKET_FILENAME);
618 if (conn->args.socket_filename == NULL)
619 {
620 err = memif_syscall_error_handler (errno);
621 goto error;
622 }
623 strncpy ((char *) conn->args.socket_filename,
624 MEMIF_DEFAULT_SOCKET_DIR, sdl);
625 conn->args.socket_filename[sdl] = '/';
626 strncpy ((char *) (conn->args.socket_filename + 1 + sdl),
627 MEMIF_DEFAULT_SOCKET_FILENAME, sfl);
628 }
629
630 if (args->secret)
631 {
632 l = strlen ((char *) args->secret);
633 strncpy ((char *) conn->args.secret, (char *) args->secret, l);
634 }
635
636 if (conn->args.is_master)
637 {
638 conn->run_args.buffer_size = conn->args.buffer_size;
639 memif_socket_t *ms;
640 memif_list_elt_t elt;
641 for (i = 0; i < lm->listener_list_len; i++)
642 {
643 if ((ms =
644 (memif_socket_t *) lm->listener_list[i].data_struct) != NULL)
645 {
646 if (strncmp
647 ((char *) ms->filename, (char *) conn->args.socket_filename,
648 strlen ((char *) ms->filename)) == 0)
649 {
650 /* add interface to listener socket */
651 elt.key = conn->args.interface_id;
652 *c = elt.data_struct = conn;
653 add_list_elt (&elt, &ms->interface_list,
654 &ms->interface_list_len);
655 ms->use_count++;
656 conn->listener_fd = ms->fd;
657 break;
658 }
659 }
660 else
661 {
662 struct stat file_stat;
663 if (stat ((char *) conn->args.socket_filename, &file_stat) == 0)
664 {
665 if (S_ISSOCK (file_stat.st_mode))
666 unlink ((char *) conn->args.socket_filename);
667 else
668 return memif_syscall_error_handler (errno);
669 }
670 DBG ("creating socket file");
671 ms = malloc (sizeof (memif_socket_t));
Jakub Grajciarb467b2a2017-09-14 14:12:10 +0200672 ms->filename =
673 malloc (strlen ((char *) conn->args.socket_filename) +
674 sizeof (char));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200675 memset (ms->filename, 0,
676 strlen ((char *) conn->args.socket_filename) +
677 sizeof (char));
678 strncpy ((char *) ms->filename,
679 (char *) conn->args.socket_filename,
680 strlen ((char *) conn->args.socket_filename));
681 ms->interface_list_len = 1;
682 ms->interface_list =
683 malloc (sizeof (memif_list_elt_t) * ms->interface_list_len);
684 ms->interface_list[0].key = -1;
685 ms->interface_list[0].data_struct = NULL;
686 struct sockaddr_un un = { 0 };
687 int on = 1;
688
689 ms->fd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
690 if (ms->fd < 0)
691 {
692 err = memif_syscall_error_handler (errno);
693 goto error;
694 }
695 DBG ("socket %d created", ms->fd);
696 un.sun_family = AF_UNIX;
697 strncpy ((char *) un.sun_path, (char *) ms->filename,
698 sizeof (un.sun_path) - 1);
699 DBG ("sockopt");
700 if (setsockopt
701 (ms->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0)
702 {
703 err = memif_syscall_error_handler (errno);
704 goto error;
705 }
706 DBG ("bind");
707 if (bind (ms->fd, (struct sockaddr *) &un, sizeof (un)) < 0)
708 {
709 err = memif_syscall_error_handler (errno);
710 goto error;
711 }
712 DBG ("listen");
713 if (listen (ms->fd, 1) < 0)
714 {
715 err = memif_syscall_error_handler (errno);
716 goto error;
717 }
718 DBG ("stat");
719 if (stat ((char *) ms->filename, &file_stat) < 0)
720 {
721 err = memif_syscall_error_handler (errno);
722 goto error;
723 }
724
725 /* add interface to listener socket */
726 elt.key = conn->args.interface_id;
727 *c = elt.data_struct = conn;
728 add_list_elt (&elt, &ms->interface_list,
729 &ms->interface_list_len);
730 ms->use_count = 1;
731 conn->listener_fd = ms->fd;
732
733 /* add listener socket to libmemif main */
734 elt.key = ms->fd;
735 elt.data_struct = ms;
736 add_list_elt (&elt, &lm->listener_list, &lm->listener_list_len);
737 lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ);
738 break;
739 }
740 }
741 }
742 else
743 {
744 if (lm->disconn_slaves == 0)
745 {
746 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
747 {
748 err = memif_syscall_error_handler (errno);
749 goto error;
750 }
751 }
752
753 lm->disconn_slaves++;
754
755 list_elt.key = -1;
756 *c = list_elt.data_struct = conn;
757 if ((index =
758 add_list_elt (&list_elt, &lm->control_list,
759 &lm->control_list_len)) < 0)
760 {
761 err = MEMIF_ERR_NOMEM;
762 goto error;
763 }
764 }
765
766 conn->index = index;
767
768 return 0;
769
770error:
771 if (sockfd > 0)
772 close (sockfd);
773 sockfd = -1;
774 if (conn->args.socket_filename)
775 free (conn->args.socket_filename);
776 if (conn != NULL)
777 free (conn);
778 *c = conn = NULL;
779 return err;
780}
781
782int
783memif_control_fd_handler (int fd, uint8_t events)
784{
785 int i, rv, sockfd = -1, err = MEMIF_ERR_SUCCESS; /* 0 */
786 uint16_t num;
787 memif_list_elt_t *e = NULL;
788 memif_connection_t *conn;
789 libmemif_main_t *lm = &libmemif_main;
790 if (fd == lm->timerfd)
791 {
792 uint64_t b;
793 ssize_t size;
794 size = read (fd, &b, sizeof (b));
795 for (i = 0; i < lm->control_list_len; i++)
796 {
797 if ((lm->control_list[i].key < 0)
798 && (lm->control_list[i].data_struct != NULL))
799 {
800 conn = lm->control_list[i].data_struct;
801 if (conn->args.is_master)
802 continue;
803
804 struct sockaddr_un sun;
805 sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
806 if (sockfd < 0)
807 {
808 err = memif_syscall_error_handler (errno);
809 goto error;
810 }
811
812 sun.sun_family = AF_UNIX;
813
814 strncpy (sun.sun_path, conn->args.socket_filename,
815 sizeof (sun.sun_path) - 1);
816
817 if (connect (sockfd, (struct sockaddr *) &sun,
818 sizeof (struct sockaddr_un)) == 0)
819 {
820 conn->fd = sockfd;
821 conn->read_fn = memif_conn_fd_read_ready;
822 conn->write_fn = memif_conn_fd_write_ready;
823 conn->error_fn = memif_conn_fd_error;
824
825 lm->control_list[conn->index].key = conn->fd;
826
827 lm->control_fd_update (sockfd,
828 MEMIF_FD_EVENT_READ |
829 MEMIF_FD_EVENT_WRITE);
830
831 lm->disconn_slaves--;
832 if (lm->disconn_slaves == 0)
833 {
834 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL)
835 < 0)
836 {
837 err = memif_syscall_error_handler (errno);
838 goto error;
839 }
840 }
841 }
842 else
843 {
844 err = memif_syscall_error_handler (errno);
845 goto error;
846 }
847 }
848 }
849 }
850 else
851 {
852 get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd);
853 if (e != NULL)
854 {
855 if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL)
856 {
857 num =
Jakub Grajciar84197552017-11-16 14:02:49 +0100858 (((memif_connection_t *) e->data_struct)->
859 args.is_master) ? ((memif_connection_t *) e->
860 data_struct)->run_args.
861 num_s2m_rings : ((memif_connection_t *) e->data_struct)->
862 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200863 for (i = 0; i < num; i++)
864 {
Jakub Grajciar84197552017-11-16 14:02:49 +0100865 if (((memif_connection_t *) e->data_struct)->
866 rx_queues[i].int_fd == fd)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200867 {
Jakub Grajciar84197552017-11-16 14:02:49 +0100868 ((memif_connection_t *) e->data_struct)->
869 on_interrupt ((void *) e->data_struct,
870 ((memif_connection_t *) e->
871 data_struct)->private_ctx, i);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200872 return MEMIF_ERR_SUCCESS;
873 }
874 }
875 }
876 return MEMIF_ERR_SUCCESS;
877 }
878 get_list_elt (&e, lm->listener_list, lm->listener_list_len, fd);
879 if (e != NULL)
880 {
881 memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct);
882 return MEMIF_ERR_SUCCESS;
883 }
884
885 get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd);
886 if (e != NULL)
887 {
888 memif_read_ready (fd);
889 return MEMIF_ERR_SUCCESS;
890 }
891
892 get_list_elt (&e, lm->control_list, lm->control_list_len, fd);
893 if (e != NULL)
894 {
895 if (events & MEMIF_FD_EVENT_READ)
896 {
897 err =
Jakub Grajciar84197552017-11-16 14:02:49 +0100898 ((memif_connection_t *) e->data_struct)->
899 read_fn (e->data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200900 if (err != MEMIF_ERR_SUCCESS)
901 return err;
902 }
903 if (events & MEMIF_FD_EVENT_WRITE)
904 {
905 err =
Jakub Grajciar84197552017-11-16 14:02:49 +0100906 ((memif_connection_t *) e->data_struct)->
907 write_fn (e->data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200908 if (err != MEMIF_ERR_SUCCESS)
909 return err;
910 }
911 if (events & MEMIF_FD_EVENT_ERROR)
912 {
913 err =
Jakub Grajciar84197552017-11-16 14:02:49 +0100914 ((memif_connection_t *) e->data_struct)->
915 error_fn (e->data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200916 if (err != MEMIF_ERR_SUCCESS)
917 return err;
918 }
919 }
920 }
921
922 return MEMIF_ERR_SUCCESS; /* 0 */
923
924error:
925 if (sockfd > 0)
926 close (sockfd);
927 sockfd = -1;
928 return err;
929}
930
931int
932memif_poll_event (int timeout)
933{
934 libmemif_main_t *lm = &libmemif_main;
935 memif_list_elt_t *elt;
936 struct epoll_event evt, *e;
937 int en = 0, err = MEMIF_ERR_SUCCESS, i = 0; /* 0 */
938 uint16_t num;
939 uint32_t events = 0;
Milan Lenco0a47c992017-10-12 14:19:31 +0200940 uint64_t counter = 0;
941 ssize_t r = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200942 memset (&evt, 0, sizeof (evt));
943 evt.events = EPOLLIN | EPOLLOUT;
944 sigset_t sigset;
945 sigemptyset (&sigset);
946 en = epoll_pwait (memif_epfd, &evt, 1, timeout, &sigset);
947 if (en < 0)
948 {
Milan Lenco0a47c992017-10-12 14:19:31 +0200949 err = errno;
950 DBG ("epoll_pwait: %s", strerror (err));
951 return memif_syscall_error_handler (err);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200952 }
953 if (en > 0)
954 {
Milan Lenco0a47c992017-10-12 14:19:31 +0200955 if (evt.data.fd == poll_cancel_fd)
956 {
957 r = read (evt.data.fd, &counter, sizeof (counter));
958 return MEMIF_ERR_POLL_CANCEL;
959 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200960 if (evt.events & EPOLLIN)
961 events |= MEMIF_FD_EVENT_READ;
962 if (evt.events & EPOLLOUT)
963 events |= MEMIF_FD_EVENT_WRITE;
964 if (evt.events & EPOLLERR)
965 events |= MEMIF_FD_EVENT_ERROR;
966 err = memif_control_fd_handler (evt.data.fd, events);
967 return err;
968 }
969 return 0;
970}
971
Milan Lenco0a47c992017-10-12 14:19:31 +0200972int
973memif_cancel_poll_event ()
974{
975 uint64_t counter = 1;
976 ssize_t w = 0;
977
978 if (poll_cancel_fd == -1)
979 return 0;
980 w = write (poll_cancel_fd, &counter, sizeof (counter));
981 if (w < sizeof (counter))
982 return MEMIF_ERR_INT_WRITE;
983
984 return 0;
985}
986
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200987static void
988memif_msg_queue_free (memif_msg_queue_elt_t ** e)
989{
990 if (*e == NULL)
991 return;
992 memif_msg_queue_free (&(*e)->next);
993 free (*e);
994 *e = NULL;
995 return;
996}
997
998/* send disconnect msg and close interface */
999int
1000memif_disconnect_internal (memif_connection_t * c)
1001{
1002 if (c == NULL)
1003 {
1004 DBG ("no connection");
1005 return MEMIF_ERR_NOCONN;
1006 }
1007 uint16_t num;
1008 int err = MEMIF_ERR_SUCCESS, i; /* 0 */
1009 memif_queue_t *mq;
1010 libmemif_main_t *lm = &libmemif_main;
1011 memif_list_elt_t *e;
1012
1013 c->on_disconnect ((void *) c, c->private_ctx);
1014
1015 if (c->fd > 0)
1016 {
1017 memif_msg_send_disconnect (c->fd, "interface deleted", 0);
1018 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL);
1019 close (c->fd);
1020 }
1021 get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd);
1022 if (e != NULL)
1023 {
1024 if (c->args.is_master)
1025 free_list_elt (lm->control_list, lm->control_list_len, c->fd);
1026 e->key = c->fd = -1;
1027 }
1028
1029 if (c->tx_queues != NULL)
1030 {
1031 num =
Jakub Grajciar84197552017-11-16 14:02:49 +01001032 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
1033 run_args.num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001034 for (i = 0; i < num; i++)
1035 {
1036 mq = &c->tx_queues[i];
1037 if (mq != NULL)
1038 {
1039 if (mq->int_fd > 0)
1040 close (mq->int_fd);
1041 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1042 mq->int_fd);
1043 mq->int_fd = -1;
1044 }
1045 }
1046 free (c->tx_queues);
1047 c->tx_queues = NULL;
1048 }
1049
1050 if (c->rx_queues != NULL)
1051 {
1052 num =
Jakub Grajciar84197552017-11-16 14:02:49 +01001053 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
1054 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001055 for (i = 0; i < num; i++)
1056 {
1057 mq = &c->rx_queues[i];
1058 if (mq != NULL)
1059 {
1060 if (mq->int_fd > 0)
1061 {
1062 if (c->on_interrupt != NULL)
1063 lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL);
1064 close (mq->int_fd);
1065 }
1066 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1067 mq->int_fd);
1068 mq->int_fd = -1;
1069 }
1070 }
1071 free (c->rx_queues);
1072 c->rx_queues = NULL;
1073 }
1074
1075 if (c->regions != NULL)
1076 {
1077 if (munmap (c->regions[0].shm, c->regions[0].region_size) < 0)
1078 return memif_syscall_error_handler (errno);
1079 if (c->regions[0].fd > 0)
1080 close (c->regions[0].fd);
1081 c->regions[0].fd = -1;
1082 free (c->regions);
1083 c->regions = NULL;
1084 }
1085
1086 memset (&c->run_args, 0, sizeof (memif_conn_run_args_t));
1087
1088 memif_msg_queue_free (&c->msg_queue);
1089
1090 if (!(c->args.is_master))
1091 {
1092 if (lm->disconn_slaves == 0)
1093 {
1094 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
1095 {
1096 err = memif_syscall_error_handler (errno);
1097 DBG_UNIX ("timerfd_settime: arm");
1098 }
1099 }
1100 lm->disconn_slaves++;
1101 }
1102
1103 return err;
1104}
1105
1106int
1107memif_delete (memif_conn_handle_t * conn)
1108{
1109 memif_connection_t *c = (memif_connection_t *) * conn;
1110 if (c == NULL)
1111 {
1112 DBG ("no connection");
1113 return MEMIF_ERR_NOCONN;
1114 }
1115 libmemif_main_t *lm = &libmemif_main;
1116 memif_list_elt_t *e = NULL;
1117 memif_socket_t *ms = NULL;
1118
1119 int err = MEMIF_ERR_SUCCESS;
1120
1121 if (c->fd > 0)
1122 {
1123 DBG ("DISCONNECTING");
1124 err = memif_disconnect_internal (c);
1125 if (err == MEMIF_ERR_NOCONN)
1126 return err;
1127 }
1128
1129 free_list_elt_ctx (lm->control_list, lm->control_list_len, c);
1130
1131 if (c->args.is_master)
1132 {
1133 get_list_elt (&e, lm->listener_list, lm->listener_list_len,
1134 c->listener_fd);
1135 if (e != NULL)
1136 {
1137 ms = (memif_socket_t *) e->data_struct;
1138 ms->use_count--;
1139 free_list_elt (ms->interface_list, ms->interface_list_len,
1140 c->args.interface_id);
1141 if (ms->use_count <= 0)
1142 {
1143 lm->control_fd_update (c->listener_fd, MEMIF_FD_EVENT_DEL);
1144 free_list_elt (lm->listener_list, lm->listener_list_len,
1145 c->listener_fd);
1146 close (c->listener_fd);
1147 c->listener_fd = ms->fd = -1;
1148 free (ms->interface_list);
1149 ms->interface_list = NULL;
1150 free (ms->filename);
1151 ms->filename = NULL;
1152 free (ms);
1153 ms = NULL;
1154 }
1155 }
1156 }
1157 else
1158 {
1159 lm->disconn_slaves--;
1160 if (lm->disconn_slaves <= 0)
1161 {
1162 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0)
1163 {
1164 err = memif_syscall_error_handler (errno);
1165 DBG ("timerfd_settime: disarm");
1166 }
1167 }
1168 }
1169
1170 if (c->args.socket_filename)
1171 free (c->args.socket_filename);
1172 c->args.socket_filename = NULL;
1173
1174 free (c);
1175 c = NULL;
1176
1177 *conn = c;
1178 return err;
1179}
1180
1181int
1182memif_connect1 (memif_connection_t * c)
1183{
1184 libmemif_main_t *lm = &libmemif_main;
1185 memif_region_t *mr = c->regions;
1186 memif_queue_t *mq;
1187 int i;
1188 uint16_t num;
1189
1190 if (mr != NULL)
1191 {
1192 if (!mr->shm)
1193 {
1194 if (mr->fd < 0)
1195 return MEMIF_ERR_NO_SHMFD;
1196
1197 if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE,
1198 MAP_SHARED, mr->fd, 0)) == MAP_FAILED)
1199 {
1200 return memif_syscall_error_handler (errno);
1201 }
1202 }
1203 }
1204
1205 num =
Jakub Grajciar84197552017-11-16 14:02:49 +01001206 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
1207 run_args.num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001208 for (i = 0; i < num; i++)
1209 {
1210 mq = &c->tx_queues[i];
1211 if (mq != NULL)
1212 {
1213 mq->ring = c->regions[mq->region].shm + mq->offset;
1214 if (mq->ring->cookie != MEMIF_COOKIE)
1215 {
1216 DBG ("wrong cookie on tx ring %u", i);
1217 return MEMIF_ERR_COOKIE;
1218 }
1219 mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
1220 0;
1221 }
1222 }
1223 num =
Jakub Grajciar84197552017-11-16 14:02:49 +01001224 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
1225 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001226 for (i = 0; i < num; i++)
1227 {
1228 mq = &c->rx_queues[i];
1229 if (mq != NULL)
1230 {
1231 mq->ring = c->regions[mq->region].shm + mq->offset;
1232 if (mq->ring->cookie != MEMIF_COOKIE)
1233 {
1234 DBG ("wrong cookie on rx ring %u", i);
1235 return MEMIF_ERR_COOKIE;
1236 }
1237 mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
1238 0;
1239 }
1240 }
1241
1242 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD);
1243
1244 return 0;
1245}
1246
1247int
1248memif_init_regions_and_queues (memif_connection_t * conn)
1249{
1250 memif_ring_t *ring = NULL;
1251 uint64_t buffer_offset;
1252 memif_region_t *r;
1253 int i, j;
1254 libmemif_main_t *lm = &libmemif_main;
1255 memif_list_elt_t e;
1256
1257 conn->regions = (memif_region_t *) malloc (sizeof (memif_region_t));
1258 if (conn->regions == NULL)
1259 return memif_syscall_error_handler (errno);
1260 r = conn->regions;
1261
1262 buffer_offset =
1263 (conn->run_args.num_s2m_rings +
1264 conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) +
1265 sizeof (memif_desc_t) *
1266 (1 << conn->run_args.log2_ring_size));
1267
1268 r->region_size = buffer_offset +
1269 conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
1270 (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
1271
1272 if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1)
1273 return memif_syscall_error_handler (errno);
1274/*
1275 if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
1276 return memif_syscall_error_handler (errno);
1277*/
1278 if ((ftruncate (r->fd, r->region_size)) == -1)
1279 return memif_syscall_error_handler (errno);
1280
1281 if ((r->shm = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE,
1282 MAP_SHARED, r->fd, 0)) == MAP_FAILED)
1283 return memif_syscall_error_handler (errno);
1284
1285 for (i = 0; i < conn->run_args.num_s2m_rings; i++)
1286 {
1287 ring = memif_get_ring (conn, MEMIF_RING_S2M, i);
1288 DBG ("RING: %p I: %d", ring, i);
1289 ring->head = ring->tail = 0;
1290 ring->cookie = MEMIF_COOKIE;
1291 ring->flags = 0;
1292 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1293 {
1294 uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j;
1295 ring->desc[j].region = 0;
1296 ring->desc[j].offset = buffer_offset +
1297 (uint32_t) (slot * conn->run_args.buffer_size);
1298 ring->desc[j].buffer_length = conn->run_args.buffer_size;
1299 }
1300 }
1301 for (i = 0; i < conn->run_args.num_m2s_rings; i++)
1302 {
1303 ring = memif_get_ring (conn, MEMIF_RING_M2S, i);
1304 DBG ("RING: %p I: %d", ring, i);
1305 ring->head = ring->tail = 0;
1306 ring->cookie = MEMIF_COOKIE;
1307 ring->flags = 0;
1308 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1309 {
1310 uint16_t slot =
1311 (i +
1312 conn->run_args.num_s2m_rings) *
1313 (1 << conn->run_args.log2_ring_size) + j;
1314 ring->desc[j].region = 0;
1315 ring->desc[j].offset = buffer_offset +
1316 (uint32_t) (slot * conn->run_args.buffer_size);
1317 ring->desc[j].buffer_length = conn->run_args.buffer_size;
1318 }
1319 }
1320 memif_queue_t *mq;
1321 mq =
1322 (memif_queue_t *) malloc (sizeof (memif_queue_t) *
1323 conn->run_args.num_s2m_rings);
1324 if (mq == NULL)
1325 return memif_syscall_error_handler (errno);
1326 int x;
1327 for (x = 0; x < conn->run_args.num_s2m_rings; x++)
1328 {
1329 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1330 return memif_syscall_error_handler (errno);
1331 /* add int fd to interrupt fd list */
1332 e.key = mq[x].int_fd;
1333 e.data_struct = conn;
1334 add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
1335
1336 mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x);
1337 DBG ("RING: %p I: %d", mq[x].ring, x);
1338 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1339 mq[x].region = 0;
1340 mq[x].offset =
1341 (void *) mq[x].ring - (void *) conn->regions[mq->region].shm;
1342 mq[x].last_head = 0;
1343 mq[x].alloc_bufs = 0;
1344 }
1345 conn->tx_queues = mq;
1346
1347 mq =
1348 (memif_queue_t *) malloc (sizeof (memif_queue_t) *
1349 conn->run_args.num_m2s_rings);
1350 if (mq == NULL)
1351 return memif_syscall_error_handler (errno);
1352 for (x = 0; x < conn->run_args.num_m2s_rings; x++)
1353 {
1354 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1355 return memif_syscall_error_handler (errno);
1356 /* add int fd to interrupt fd list */
1357 e.key = mq[x].int_fd;
1358 e.data_struct = conn;
1359 add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
1360
1361 mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x);
1362 DBG ("RING: %p I: %d", mq[x].ring, x);
1363 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1364 mq[x].region = 0;
1365 mq[x].offset =
1366 (void *) mq[x].ring - (void *) conn->regions[mq->region].shm;
1367 mq[x].last_head = 0;
1368 mq[x].alloc_bufs = 0;
1369 }
1370 conn->rx_queues = mq;
1371
1372 return 0;
1373}
1374
1375int
1376memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
1377 memif_buffer_t * bufs, uint16_t count,
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001378 uint16_t * count_out, uint16_t size)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001379{
1380 memif_connection_t *c = (memif_connection_t *) conn;
1381 if (c == NULL)
1382 return MEMIF_ERR_NOCONN;
1383 if (c->fd < 0)
1384 return MEMIF_ERR_DISCONNECTED;
1385 uint8_t num =
Jakub Grajciar84197552017-11-16 14:02:49 +01001386 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
1387 run_args.num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001388 if (qid >= num)
1389 return MEMIF_ERR_QID;
1390 memif_queue_t *mq = &c->tx_queues[qid];
1391 memif_ring_t *ring = mq->ring;
1392 memif_buffer_t *b0, *b1;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001393 uint8_t chain_buf = 1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001394 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001395 uint16_t head = ring->head;
1396 uint16_t tail = ring->tail;
Chun Li5dca5462018-02-07 09:51:35 +08001397 uint16_t ring_size;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001398 uint16_t s0, s1, ns;
1399 *count_out = 0;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001400 int i, err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001401
Chun Li5dca5462018-02-07 09:51:35 +08001402 ring_size = (1 << mq->log2_ring_size);
1403 ns = ring_size - head + tail;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001404
Damjan Marion6d56fa42017-11-03 12:24:37 +01001405 /* calculate number of chain buffers */
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001406 if (size > ring->desc[0].buffer_length)
1407 {
Jakub Grajciar84197552017-11-16 14:02:49 +01001408 chain_buf = size / ring->desc[0].buffer_length;
1409 if (((size % ring->desc[0].buffer_length) != 0) || (size == 0))
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001410 chain_buf++;
1411 }
1412
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001413 while (count && ns)
1414 {
1415 while ((count > 2) && (ns > 2))
1416 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001417 s0 = (ring->head + mq->alloc_bufs) & mask;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001418 s1 = (ring->head + mq->alloc_bufs + chain_buf) & mask;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001419
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001420 if ((2 * chain_buf) > ns)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001421 break;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001422
1423 b0 = (bufs + *count_out);
1424 b1 = (bufs + *count_out + 1);
1425
Damjan Marion6d56fa42017-11-03 12:24:37 +01001426 b0->desc_index = head + mq->alloc_bufs;
1427 b1->desc_index = head + mq->alloc_bufs + chain_buf;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001428 ring->desc[s0].flags = 0;
1429 ring->desc[s1].flags = 0;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001430 b0->buffer_len = ring->desc[s0].buffer_length * chain_buf;
1431 b1->buffer_len = ring->desc[s1].buffer_length * chain_buf;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001432 /* TODO: support multiple regions -> ring descriptor contains region index */
1433 b0->data = c->regions->shm + ring->desc[s0].offset;
1434 b1->data = c->regions->shm + ring->desc[s1].offset;
1435
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001436 for (i = 0; i < (chain_buf - 1); i++)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001437 {
1438 ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
1439 ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
1440 DBG ("allocating chained buffers");
1441 }
1442
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001443 mq->alloc_bufs += 2 * chain_buf;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001444
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001445 DBG ("allocated ring slots %u, %u", s0, s1);
1446 count -= 2;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001447 ns -= (2 * chain_buf);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001448 *count_out += 2;
1449 }
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001450 s0 = (ring->head + mq->alloc_bufs) & mask;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001451
1452 b0 = (bufs + *count_out);
1453
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001454 if (chain_buf > ns)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001455 break;
1456
Damjan Marion6d56fa42017-11-03 12:24:37 +01001457 b0->desc_index = head + mq->alloc_bufs;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001458 ring->desc[s0].flags = 0;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001459 b0->buffer_len = ring->desc[s0].buffer_length * chain_buf;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001460 b0->data = c->regions->shm + ring->desc[s0].offset;
1461
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001462 for (i = 0; i < (chain_buf - 1); i++)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001463 {
1464 ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
1465 DBG ("allocating chained buffers");
1466 }
1467
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001468 mq->alloc_bufs += chain_buf;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001469
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001470 DBG ("allocated ring slot %u", s0);
1471 count--;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001472 ns -= chain_buf;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001473 *count_out += 1;
1474 }
1475
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001476 DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
1477 mq->alloc_bufs);
1478
1479 if (count)
1480 {
1481 DBG ("ring buffer full! qid: %u", qid);
1482 err = MEMIF_ERR_NOBUF_RING;
1483 }
1484
1485 return err;
1486}
1487
1488int
1489memif_buffer_free (memif_conn_handle_t conn, uint16_t qid,
1490 memif_buffer_t * bufs, uint16_t count,
1491 uint16_t * count_out)
1492{
1493 memif_connection_t *c = (memif_connection_t *) conn;
1494 if (c == NULL)
1495 return MEMIF_ERR_NOCONN;
1496 if (c->fd < 0)
1497 return MEMIF_ERR_DISCONNECTED;
1498 uint8_t num =
Jakub Grajciar84197552017-11-16 14:02:49 +01001499 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
1500 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001501 if (qid >= num)
1502 return MEMIF_ERR_QID;
1503 libmemif_main_t *lm = &libmemif_main;
1504 memif_queue_t *mq = &c->rx_queues[qid];
1505 memif_ring_t *ring = mq->ring;
1506 uint16_t tail = ring->tail;
1507 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001508 uint8_t chain_buf0, chain_buf1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001509 memif_buffer_t *b0, *b1;
1510 *count_out = 0;
1511
1512 if (mq->alloc_bufs < count)
1513 count = mq->alloc_bufs;
1514
1515 while (count)
1516 {
1517 while (count > 2)
1518 {
1519 b0 = (bufs + *count_out);
1520 b1 = (bufs + *count_out + 1);
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001521 chain_buf0 =
Jakub Grajciar84197552017-11-16 14:02:49 +01001522 b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
1523 if ((b0->buffer_len %
1524 ring->desc[b0->desc_index & mask].buffer_length) != 0)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001525 chain_buf0++;
1526 chain_buf1 =
Jakub Grajciar84197552017-11-16 14:02:49 +01001527 b1->buffer_len / ring->desc[b1->desc_index & mask].buffer_length;
1528 if ((b1->buffer_len %
1529 ring->desc[b1->desc_index & mask].buffer_length) != 0)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001530 chain_buf1++;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001531 tail = b1->desc_index + chain_buf1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001532 b0->data = NULL;
1533 b1->data = NULL;
1534
1535 count -= 2;
1536 *count_out += 2;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001537 mq->alloc_bufs -= chain_buf0 + chain_buf1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001538 }
1539 b0 = (bufs + *count_out);
Jakub Grajciar84197552017-11-16 14:02:49 +01001540 chain_buf0 =
1541 b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
1542 if ((b0->buffer_len %
1543 ring->desc[b0->desc_index & mask].buffer_length) != 0)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001544 chain_buf0++;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001545 tail = b0->desc_index + chain_buf0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001546 b0->data = NULL;
1547
1548 count--;
1549 *count_out += 1;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001550 mq->alloc_bufs -= chain_buf0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001551 }
Milan Lenco0a47c992017-10-12 14:19:31 +02001552 MEMIF_MEMORY_BARRIER ();
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001553 ring->tail = tail;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001554 DBG ("tail: %u", ring->tail);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001555
1556 return MEMIF_ERR_SUCCESS; /* 0 */
1557}
1558
1559int
1560memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
1561 memif_buffer_t * bufs, uint16_t count, uint16_t * tx)
1562{
1563 memif_connection_t *c = (memif_connection_t *) conn;
1564 if (c == NULL)
1565 return MEMIF_ERR_NOCONN;
1566 if (c->fd < 0)
1567 return MEMIF_ERR_DISCONNECTED;
1568 uint8_t num =
Jakub Grajciar84197552017-11-16 14:02:49 +01001569 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
1570 run_args.num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001571 if (qid >= num)
1572 return MEMIF_ERR_QID;
1573 memif_queue_t *mq = &c->tx_queues[qid];
1574 memif_ring_t *ring = mq->ring;
1575 uint16_t head = ring->head;
1576 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001577 uint8_t chain_buf0, chain_buf1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001578 *tx = 0;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001579 uint16_t curr_buf = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001580 memif_buffer_t *b0, *b1;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001581 int i;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001582
1583 while (count)
1584 {
1585 while (count > 2)
1586 {
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001587 b0 = (bufs + curr_buf);
1588 b1 = (bufs + curr_buf + 1);
1589 chain_buf0 =
Jakub Grajciar84197552017-11-16 14:02:49 +01001590 b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
1591 if ((b0->buffer_len %
1592 ring->desc[b0->desc_index & mask].buffer_length) != 0)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001593 chain_buf0++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001594
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001595 chain_buf1 =
Jakub Grajciar84197552017-11-16 14:02:49 +01001596 b1->buffer_len / ring->desc[b1->desc_index & mask].buffer_length;
1597 if ((b1->buffer_len %
1598 ring->desc[b1->desc_index & mask].buffer_length) != 0)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001599 chain_buf1++;
1600
1601 for (i = 0; i < memif_min (chain_buf0, chain_buf1); i++)
1602 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001603 /* b0 */
1604 if (b0->data_len >
1605 ring->desc[(b0->desc_index + i) & mask].buffer_length)
1606 {
1607 b0->data_len -=
1608 ring->desc[(b0->desc_index + i) & mask].length =
1609 ring->desc[(b0->desc_index + i) & mask].buffer_length;
1610 }
1611 else
1612 {
1613 ring->desc[(b0->desc_index + i) & mask].length =
1614 b0->data_len;
1615 b0->data_len = 0;
1616 }
1617 /* b1 */
1618 if (b1->data_len >
1619 ring->desc[(b1->desc_index + i) & mask].buffer_length)
1620 {
1621 b1->data_len -=
1622 ring->desc[(b1->desc_index + i) & mask].length =
1623 ring->desc[(b1->desc_index + i) & mask].buffer_length;
1624 }
1625 else
1626 {
1627 ring->desc[(b1->desc_index + i) & mask].length =
1628 b1->data_len;
1629 b1->data_len = 0;
1630 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001631#ifdef MEMIF_DBG_SHM
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001632 print_bytes (b0->data +
1633 ring->desc[(b0->desc_index +
1634 i) & mask].buffer_length *
1635 (chain_buf0 - 1),
1636 ring->desc[(b0->desc_index +
1637 i) & mask].buffer_length, DBG_TX_BUF);
1638 print_bytes (b1->data +
1639 ring->desc[(b1->desc_index +
1640 i) & mask].buffer_length *
1641 (chain_buf1 - 1),
1642 ring->desc[(b1->desc_index +
1643 i) & mask].buffer_length, DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001644#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001645 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001646
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001647 if (chain_buf0 > chain_buf1)
1648 {
1649 for (; i < chain_buf0; i++)
1650 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001651 if (b0->data_len >
1652 ring->desc[(b0->desc_index + i) & mask].buffer_length)
1653 {
1654 b0->data_len -=
1655 ring->desc[(b0->desc_index + i) & mask].length =
1656 ring->desc[(b0->desc_index + i) & mask].buffer_length;
1657 }
1658 else
1659 {
1660 ring->desc[(b0->desc_index + i) & mask].length =
1661 b0->data_len;
1662 b0->data_len = 0;
1663 }
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001664#ifdef MEMIF_DBG_SHM
1665 print_bytes (b0->data +
1666 ring->desc[(b0->desc_index +
1667 i) & mask].buffer_length *
1668 (chain_buf0 - 1),
1669 ring->desc[(b0->desc_index +
1670 i) & mask].buffer_length,
1671 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001672#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001673 }
1674 }
1675 else
1676 {
1677 for (; i < chain_buf1; i++)
1678 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001679 if (b1->data_len >
1680 ring->desc[(b1->desc_index + i) & mask].buffer_length)
1681 {
1682 b1->data_len -=
1683 ring->desc[(b1->desc_index + i) & mask].length =
1684 ring->desc[(b1->desc_index + i) & mask].buffer_length;
1685 }
1686 else
1687 {
1688 ring->desc[(b1->desc_index + i) & mask].length =
1689 b1->data_len;
1690 b1->data_len = 0;
1691 }
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001692#ifdef MEMIF_DBG_SHM
1693 print_bytes (b1->data +
1694 ring->desc[(b1->desc_index +
1695 i) & mask].buffer_length *
1696 (chain_buf1 - 1),
1697 ring->desc[(b1->desc_index +
1698 i) & mask].buffer_length,
1699 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001700#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001701 }
1702 }
1703
Jakub Grajciar84197552017-11-16 14:02:49 +01001704 head = b1->desc_index + chain_buf1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001705
1706 b0->data = NULL;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001707#ifdef MEMIF_DBG
1708 if (b0->data_len != 0)
1709 DBG ("invalid b0 data length!");
1710#endif /* MEMIF_DBG */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001711 b1->data = NULL;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001712#ifdef MEMIF_DBG
1713 if (b1->data_len != 0)
1714 DBG ("invalid b1 data length!");
1715#endif /* MEMIF_DBG */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001716
1717 count -= 2;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001718 *tx += chain_buf0 + chain_buf1;
1719 curr_buf += 2;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001720 }
1721
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001722 b0 = (bufs + curr_buf);
Jakub Grajciar84197552017-11-16 14:02:49 +01001723 chain_buf0 =
1724 b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
1725 if ((b0->buffer_len %
1726 ring->desc[b0->desc_index & mask].buffer_length) != 0)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001727 chain_buf0++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001728
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001729 for (i = 0; i < chain_buf0; i++)
1730 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001731 if (b0->data_len >
1732 ring->desc[(b0->desc_index + i) & mask].buffer_length)
1733 {
1734 b0->data_len -= ring->desc[(b0->desc_index + i) & mask].length =
1735 ring->desc[(b0->desc_index + i) & mask].buffer_length;
1736 }
1737 else
1738 {
1739 ring->desc[(b0->desc_index + i) & mask].length = b0->data_len;
1740 b0->data_len = 0;
1741 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001742#ifdef MEMIF_DBG_SHM
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001743 print_bytes (b0->data +
1744 ring->desc[(b0->desc_index + i) & mask].buffer_length *
1745 (chain_buf0 - 1),
1746 ring->desc[(b0->desc_index + i) & mask].buffer_length,
1747 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001748#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001749 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001750
Jakub Grajciar84197552017-11-16 14:02:49 +01001751 head = b0->desc_index + chain_buf0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001752
1753 b0->data = NULL;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001754#ifdef MEMIF_DBG
1755 if (b0->data_len != 0)
1756 DBG ("invalid b0 data length!");
1757#endif /* MEMIF_DBG */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001758
1759 count--;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001760 *tx += chain_buf0;
1761 curr_buf++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001762 }
Milan Lenco0a47c992017-10-12 14:19:31 +02001763 MEMIF_MEMORY_BARRIER ();
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001764 ring->head = head;
1765
1766 mq->alloc_bufs -= *tx;
1767
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001768 /* TODO: return num of buffers and packets */
1769 *tx = curr_buf;
1770
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001771 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
1772 {
1773 uint64_t a = 1;
1774 int r = write (mq->int_fd, &a, sizeof (a));
1775 if (r < 0)
1776 return MEMIF_ERR_INT_WRITE;
1777 }
1778
1779 return MEMIF_ERR_SUCCESS; /* 0 */
1780}
1781
1782int
1783memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
1784 memif_buffer_t * bufs, uint16_t count, uint16_t * rx)
1785{
1786 memif_connection_t *c = (memif_connection_t *) conn;
1787 if (c == NULL)
1788 return MEMIF_ERR_NOCONN;
1789 if (c->fd < 0)
1790 return MEMIF_ERR_DISCONNECTED;
1791 uint8_t num =
Jakub Grajciar84197552017-11-16 14:02:49 +01001792 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
1793 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001794 if (qid >= num)
1795 return MEMIF_ERR_QID;
1796 memif_queue_t *mq = &c->rx_queues[qid];
1797 memif_ring_t *ring = mq->ring;
1798 uint16_t head = ring->head;
1799 uint16_t ns;
1800 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1801 memif_buffer_t *b0, *b1;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001802 uint16_t curr_buf = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001803 *rx = 0;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001804#ifdef MEMIF_DBG_SHM
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001805 int i;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001806#endif /* MEMIF_DBG_SHM */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001807
1808 uint64_t b;
1809 ssize_t r = read (mq->int_fd, &b, sizeof (b));
1810 if ((r == -1) && (errno != EAGAIN))
1811 return memif_syscall_error_handler (errno);
1812
1813 if (head == mq->last_head)
1814 return 0;
1815
Damjan Marion6d56fa42017-11-03 12:24:37 +01001816 ns = head - mq->last_head;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001817
1818 while (ns && count)
1819 {
1820 while ((ns > 2) && (count > 2))
1821 {
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001822 b0 = (bufs + curr_buf);
1823 b1 = (bufs + curr_buf + 1);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001824
1825 b0->desc_index = mq->last_head;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001826 b0->data = memif_get_buffer (conn, ring, mq->last_head & mask);
1827 b0->data_len = ring->desc[mq->last_head & mask].length;
1828 b0->buffer_len = ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001829#ifdef MEMIF_DBG_SHM
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001830 i = 0;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001831 print_bytes (b0->data +
Damjan Marion6d56fa42017-11-03 12:24:37 +01001832 ring->desc[b0->desc_index & mask].buffer_length * i++,
Jakub Grajciar84197552017-11-16 14:02:49 +01001833 ring->desc[b0->desc_index & mask].buffer_length,
1834 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001835#endif /* MEMIF_DBG_SHM */
1836 ns--;
1837 *rx += 1;
Jakub Grajciar84197552017-11-16 14:02:49 +01001838 while (ring->desc[mq->last_head & mask].
1839 flags & MEMIF_DESC_FLAG_NEXT)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001840 {
Damjan Marion6d56fa42017-11-03 12:24:37 +01001841 ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
1842 mq->last_head++;
1843 b0->data_len += ring->desc[mq->last_head & mask].length;
Jakub Grajciar84197552017-11-16 14:02:49 +01001844 b0->buffer_len +=
1845 ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001846#ifdef MEMIF_DBG_SHM
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001847 print_bytes (b0->data +
Jakub Grajciar84197552017-11-16 14:02:49 +01001848 ring->desc[b0->desc_index & mask].buffer_length *
1849 i++,
Damjan Marion6d56fa42017-11-03 12:24:37 +01001850 ring->desc[b0->desc_index & mask].buffer_length,
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001851 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001852#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001853 ns--;
1854 *rx += 1;
1855 }
Damjan Marion6d56fa42017-11-03 12:24:37 +01001856 mq->last_head++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001857
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001858 b1->desc_index = mq->last_head;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001859 b1->data = memif_get_buffer (conn, ring, mq->last_head & mask);
1860 b1->data_len = ring->desc[mq->last_head & mask].length;
1861 b1->buffer_len = ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001862#ifdef MEMIF_DBG_SHM
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001863 i = 0;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001864 print_bytes (b1->data +
Damjan Marion6d56fa42017-11-03 12:24:37 +01001865 ring->desc[b1->desc_index & mask].buffer_length * i++,
Jakub Grajciar84197552017-11-16 14:02:49 +01001866 ring->desc[b1->desc_index & mask].buffer_length,
1867 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001868#endif /* MEMIF_DBG_SHM */
1869 ns--;
1870 *rx += 1;
Jakub Grajciar84197552017-11-16 14:02:49 +01001871 while (ring->desc[mq->last_head & mask].
1872 flags & MEMIF_DESC_FLAG_NEXT)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001873 {
Damjan Marion6d56fa42017-11-03 12:24:37 +01001874 ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
1875 mq->last_head++;
1876 b1->data_len += ring->desc[mq->last_head & mask].length;
Jakub Grajciar84197552017-11-16 14:02:49 +01001877 b1->buffer_len +=
1878 ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001879#ifdef MEMIF_DBG_SHM
1880 print_bytes (b1->data +
Jakub Grajciar84197552017-11-16 14:02:49 +01001881 ring->desc[b1->desc_index & mask].buffer_length *
1882 i++,
Damjan Marion6d56fa42017-11-03 12:24:37 +01001883 ring->desc[b1->desc_index & mask].buffer_length,
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001884 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001885#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001886 ns--;
1887 *rx += 1;
1888 }
Damjan Marion6d56fa42017-11-03 12:24:37 +01001889 mq->last_head++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001890
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001891 count -= 2;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001892 curr_buf += 2;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001893 }
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001894 b0 = (bufs + curr_buf);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001895
1896 b0->desc_index = mq->last_head;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001897 b0->data = memif_get_buffer (conn, ring, mq->last_head & mask);
1898 b0->data_len = ring->desc[mq->last_head & mask].length;
1899 b0->buffer_len = ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001900#ifdef MEMIF_DBG_SHM
Jakub Grajciar84197552017-11-16 14:02:49 +01001901 i = 0;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001902 print_bytes (b0->data +
Damjan Marion6d56fa42017-11-03 12:24:37 +01001903 ring->desc[b0->desc_index & mask].buffer_length * i++,
Jakub Grajciar84197552017-11-16 14:02:49 +01001904 ring->desc[b0->desc_index & mask].buffer_length,
1905 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001906#endif /* MEMIF_DBG_SHM */
1907 ns--;
1908 *rx += 1;
1909
Damjan Marion6d56fa42017-11-03 12:24:37 +01001910 while (ring->desc[mq->last_head & mask].flags & MEMIF_DESC_FLAG_NEXT)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001911 {
Damjan Marion6d56fa42017-11-03 12:24:37 +01001912 ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
1913 mq->last_head++;
1914 b0->data_len += ring->desc[mq->last_head & mask].length;
1915 b0->buffer_len += ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001916#ifdef MEMIF_DBG_SHM
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001917 print_bytes (b0->data +
Damjan Marion6d56fa42017-11-03 12:24:37 +01001918 ring->desc[b0->desc_index & mask].buffer_length * i++,
Jakub Grajciar84197552017-11-16 14:02:49 +01001919 ring->desc[b0->desc_index & mask].buffer_length,
1920 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001921#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001922 ns--;
1923 *rx += 1;
1924 }
Damjan Marion6d56fa42017-11-03 12:24:37 +01001925 mq->last_head++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001926
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001927 count--;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001928 curr_buf++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001929 }
1930
1931 mq->alloc_bufs += *rx;
1932
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001933 /* TODO: return num of buffers and packets */
1934 *rx = curr_buf;
1935
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001936 if (ns)
1937 {
1938 DBG ("not enough buffers!");
1939 return MEMIF_ERR_NOBUF;
1940 }
1941
1942 return MEMIF_ERR_SUCCESS; /* 0 */
1943}
1944
1945int
1946memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
1947 char *buf, ssize_t buflen)
1948{
1949 memif_connection_t *c = (memif_connection_t *) conn;
1950 if (c == NULL)
1951 return MEMIF_ERR_NOCONN;
1952
1953 int err = MEMIF_ERR_SUCCESS, i;
1954 ssize_t l0, l1, total_l;
1955 l0 = 0;
1956
1957 l1 = strlen ((char *) c->args.interface_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001958 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001959 {
Milan Lenco0a47c992017-10-12 14:19:31 +02001960 md->if_name = strcpy (buf + l0, (char *) c->args.interface_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001961 l0 += l1 + 1;
1962 }
1963 else
1964 err = MEMIF_ERR_NOBUF_DET;
1965
1966 l1 = strlen ((char *) c->args.instance_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001967 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001968 {
Milan Lenco0a47c992017-10-12 14:19:31 +02001969 md->inst_name = strcpy (buf + l0, (char *) c->args.instance_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001970 l0 += l1 + 1;
1971 }
1972 else
1973 err = MEMIF_ERR_NOBUF_DET;
1974
1975 l1 = strlen ((char *) c->remote_if_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001976 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001977 {
Milan Lenco0a47c992017-10-12 14:19:31 +02001978 md->remote_if_name = strcpy (buf + l0, (char *) c->remote_if_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001979 l0 += l1 + 1;
1980 }
1981 else
1982 err = MEMIF_ERR_NOBUF_DET;
1983
1984 l1 = strlen ((char *) c->remote_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001985 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001986 {
Milan Lenco0a47c992017-10-12 14:19:31 +02001987 md->remote_inst_name = strcpy (buf + l0, (char *) c->remote_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001988 l0 += l1 + 1;
1989 }
1990 else
1991 err = MEMIF_ERR_NOBUF_DET;
1992
1993 md->id = c->args.interface_id;
1994
1995 if (c->args.secret)
1996 {
1997 l1 = strlen ((char *) c->args.secret);
Milan Lenco0a47c992017-10-12 14:19:31 +02001998 if (l0 + l1 < buflen)
1999 {
2000 md->secret = strcpy (buf + l0, (char *) c->args.secret);
2001 l0 += l1 + 1;
2002 }
2003 else
2004 err = MEMIF_ERR_NOBUF_DET;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002005 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002006
2007 md->role = (c->args.is_master) ? 0 : 1;
2008 md->mode = c->args.mode;
2009
2010 l1 = strlen ((char *) c->args.socket_filename);
Milan Lenco0a47c992017-10-12 14:19:31 +02002011 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002012 {
2013 md->socket_filename =
Milan Lenco0a47c992017-10-12 14:19:31 +02002014 strcpy (buf + l0, (char *) c->args.socket_filename);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002015 l0 += l1 + 1;
2016 }
2017 else
2018 err = MEMIF_ERR_NOBUF_DET;
2019
2020 md->rx_queues_num =
Jakub Grajciar84197552017-11-16 14:02:49 +01002021 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2022 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002023
2024 l1 = sizeof (memif_queue_details_t) * md->rx_queues_num;
2025 if (l0 + l1 <= buflen)
2026 {
2027 md->rx_queues = (memif_queue_details_t *) buf + l0;
Milan Lenco0a47c992017-10-12 14:19:31 +02002028 l0 += l1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002029 }
2030 else
2031 err = MEMIF_ERR_NOBUF_DET;
2032
2033 for (i = 0; i < md->rx_queues_num; i++)
2034 {
2035 md->rx_queues[i].qid = i;
2036 md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size);
Jakub Grajciar84197552017-11-16 14:02:49 +01002037 md->rx_queues[i].flags = c->rx_queues[i].ring->flags;
2038 md->rx_queues[i].head = c->rx_queues[i].ring->head;
2039 md->rx_queues[i].tail = c->rx_queues[i].ring->tail;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002040 md->rx_queues[i].buffer_size = c->run_args.buffer_size;
2041 }
2042
2043 md->tx_queues_num =
Jakub Grajciar84197552017-11-16 14:02:49 +01002044 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
2045 run_args.num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002046
2047 l1 = sizeof (memif_queue_details_t) * md->tx_queues_num;
2048 if (l0 + l1 <= buflen)
2049 {
2050 md->tx_queues = (memif_queue_details_t *) buf + l0;
Milan Lenco0a47c992017-10-12 14:19:31 +02002051 l0 += l1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002052 }
2053 else
2054 err = MEMIF_ERR_NOBUF_DET;
2055
2056 for (i = 0; i < md->tx_queues_num; i++)
2057 {
2058 md->tx_queues[i].qid = i;
2059 md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size);
Jakub Grajciar84197552017-11-16 14:02:49 +01002060 md->tx_queues[i].flags = c->tx_queues[i].ring->flags;
2061 md->tx_queues[i].head = c->tx_queues[i].ring->head;
2062 md->tx_queues[i].tail = c->tx_queues[i].ring->tail;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002063 md->tx_queues[i].buffer_size = c->run_args.buffer_size;
2064 }
2065
2066 md->link_up_down = (c->fd > 0) ? 1 : 0;
2067
2068 return err; /* 0 */
2069}
2070
2071int
2072memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd)
2073{
2074 memif_connection_t *c = (memif_connection_t *) conn;
2075 *efd = -1;
2076 if (c == NULL)
2077 return MEMIF_ERR_NOCONN;
2078 if (c->fd < 0)
2079 return MEMIF_ERR_DISCONNECTED;
2080 uint8_t num =
Jakub Grajciar84197552017-11-16 14:02:49 +01002081 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2082 run_args.num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002083 if (qid >= num)
2084 return MEMIF_ERR_QID;
2085
2086 *efd = c->rx_queues[qid].int_fd;
2087
2088 return MEMIF_ERR_SUCCESS;
2089}
2090
2091int
2092memif_cleanup ()
2093{
2094 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002095 if (lm->control_list)
2096 free (lm->control_list);
2097 lm->control_list = NULL;
2098 if (lm->interrupt_list)
2099 free (lm->interrupt_list);
2100 lm->interrupt_list = NULL;
2101 if (lm->listener_list)
2102 free (lm->listener_list);
2103 lm->listener_list = NULL;
2104 if (lm->pending_list)
2105 free (lm->pending_list);
2106 lm->pending_list = NULL;
Milan Lenco0a47c992017-10-12 14:19:31 +02002107 if (poll_cancel_fd != -1)
2108 close (poll_cancel_fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002109
2110 return MEMIF_ERR_SUCCESS; /* 0 */
2111}