blob: 2e2602e52c124381485fb9ce3b1e1060c6a38bfc [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;
409
410 if (app_name)
411 {
412 lm->app_name = malloc (strlen (app_name) + sizeof (char));
413 memset (lm->app_name, 0, strlen (app_name) + sizeof (char));
414 strncpy ((char *) lm->app_name, app_name, strlen (app_name));
415 }
416 else
417 {
418 lm->app_name = malloc (strlen (MEMIF_DEFAULT_APP_NAME) + sizeof (char));
419 memset (lm->app_name, 0, strlen (app_name) + sizeof (char));
420 strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME,
421 strlen (MEMIF_DEFAULT_APP_NAME));
422 }
423
424 /* register control fd update callback */
425 if (on_control_fd_update != NULL)
426 memif_control_fd_update_register (on_control_fd_update);
427 else
428 {
429 memif_epfd = epoll_create (1);
430 memif_control_fd_update_register (memif_control_fd_update);
Milan Lenco0a47c992017-10-12 14:19:31 +0200431 if ((poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0)
432 {
433 err = errno;
434 DBG ("eventfd: %s", strerror (err));
435 return memif_syscall_error_handler (err);
436 }
437 lm->control_fd_update (poll_cancel_fd, MEMIF_FD_EVENT_READ);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200438 DBG ("libmemif event polling initialized");
439 }
440
441 memset (&lm->ms, 0, sizeof (memif_socket_t));
442
443 lm->control_list_len = 2;
444 lm->interrupt_list_len = 2;
445 lm->listener_list_len = 1;
446 lm->pending_list_len = 1;
447
448 lm->control_list =
449 malloc (sizeof (memif_list_elt_t) * lm->control_list_len);
450 lm->interrupt_list =
451 malloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len);
452 lm->listener_list =
453 malloc (sizeof (memif_list_elt_t) * lm->listener_list_len);
454 lm->pending_list =
455 malloc (sizeof (memif_list_elt_t) * lm->pending_list_len);
456
457 int i;
458 for (i = 0; i < lm->control_list_len; i++)
459 {
460 lm->control_list[i].key = -1;
461 lm->control_list[i].data_struct = NULL;
462 }
463 for (i = 0; i < lm->interrupt_list_len; i++)
464 {
465 lm->interrupt_list[i].key = -1;
466 lm->interrupt_list[i].data_struct = NULL;
467 }
468 for (i = 0; i < lm->listener_list_len; i++)
469 {
470 lm->listener_list[i].key = -1;
471 lm->listener_list[i].data_struct = NULL;
472 }
473 for (i = 0; i < lm->pending_list_len; i++)
474 {
475 lm->pending_list[i].key = -1;
476 lm->pending_list[i].data_struct = NULL;
477 }
478
479 lm->disconn_slaves = 0;
480
481 lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK);
482 if (lm->timerfd < 0)
483 {
484 err = errno;
485 DBG ("timerfd: %s", strerror (err));
486 return memif_syscall_error_handler (err);
487 }
488
489 lm->arm.it_value.tv_sec = 2;
490 lm->arm.it_value.tv_nsec = 0;
491 lm->arm.it_interval.tv_sec = 2;
492 lm->arm.it_interval.tv_nsec = 0;
493 memset (&lm->disarm, 0, sizeof (lm->disarm));
494
495 if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ) < 0)
496 {
497 DBG ("callback type memif_control_fd_update_t error!");
498 return MEMIF_ERR_CB_FDUPDATE;
499 }
500
501 return 0;
502}
503
504static inline memif_ring_t *
505memif_get_ring (memif_connection_t * conn, memif_ring_type_t type,
506 uint16_t ring_num)
507{
508 if (&conn->regions[0] == NULL)
509 return NULL;
510 void *p = conn->regions[0].shm;
511 int ring_size =
512 sizeof (memif_ring_t) +
513 sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size);
514 p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size;
515
516 return (memif_ring_t *) p;
517}
518
519int
520memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode,
521 uint16_t qid)
522{
523 memif_connection_t *conn = (memif_connection_t *) c;
524 if (conn == NULL)
525 return MEMIF_ERR_NOCONN;
526 uint8_t num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +0200527 (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->run_args.
528 num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200529 if (qid >= num)
530 return MEMIF_ERR_QID;
531
532 conn->rx_queues[qid].ring->flags = rx_mode;
533 DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags);
534 return MEMIF_ERR_SUCCESS;
535}
536
537int
538memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
539 memif_connection_update_t * on_connect,
540 memif_connection_update_t * on_disconnect,
541 memif_interrupt_t * on_interrupt, void *private_ctx)
542{
543 int err, i, index, sockfd = -1;
544 memif_list_elt_t list_elt;
545 memif_connection_t *conn = (memif_connection_t *) * c;
546 if (conn != NULL)
547 {
548 DBG ("This handle already points to existing memif.");
549 return MEMIF_ERR_CONN;
550 }
551 conn = (memif_connection_t *) malloc (sizeof (memif_connection_t));
552 if (conn == NULL)
553 {
554 err = memif_syscall_error_handler (errno);
555 goto error;
556 }
557 memset (conn, 0, sizeof (memif_connection_t));
558
559 libmemif_main_t *lm = &libmemif_main;
560
561 conn->args.interface_id = args->interface_id;
562
563 if (args->log2_ring_size == 0)
564 args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE;
Damjan Marion6d56fa42017-11-03 12:24:37 +0100565 else if (args->log2_ring_size > MEMIF_MAX_LOG2_RING_SIZE)
566 {
567 err = MEMIF_ERR_MAX_RING;
568 goto error;
569 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200570 if (args->buffer_size == 0)
571 args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
572 if (args->num_s2m_rings == 0)
573 args->num_s2m_rings = MEMIF_DEFAULT_TX_QUEUES;
574 if (args->num_m2s_rings == 0)
575 args->num_m2s_rings = MEMIF_DEFAULT_RX_QUEUES;
576
577 conn->args.num_s2m_rings = args->num_s2m_rings;
578 conn->args.num_m2s_rings = args->num_m2s_rings;
579 conn->args.buffer_size = args->buffer_size;
580 conn->args.log2_ring_size = args->log2_ring_size;
581 conn->args.is_master = args->is_master;
582 conn->args.mode = args->mode;
583 conn->msg_queue = NULL;
584 conn->regions = NULL;
585 conn->tx_queues = NULL;
586 conn->rx_queues = NULL;
587 conn->fd = -1;
588 conn->on_connect = on_connect;
589 conn->on_disconnect = on_disconnect;
590 conn->on_interrupt = on_interrupt;
591 conn->private_ctx = private_ctx;
592 memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t));
593
594 uint8_t l = strlen ((char *) args->interface_name);
595 strncpy ((char *) conn->args.interface_name, (char *) args->interface_name,
596 l);
597
598 l = strlen ((char *) args->instance_name);
599 strncpy ((char *) conn->args.instance_name, (char *) args->instance_name,
600 l);
601
602 /* allocate and initialize socket_filename so it can be copyed to sun_path
603 without memory leaks */
604 conn->args.socket_filename = malloc (sizeof (char *) * 108);
605 memset (conn->args.socket_filename, 0, 108 * sizeof (char *));
606
607 if (args->socket_filename)
608 {
609 if (conn->args.socket_filename == NULL)
610 {
611 err = memif_syscall_error_handler (errno);
612 goto error;
613 }
614 strncpy ((char *) conn->args.socket_filename,
615 (char *) args->socket_filename,
616 strlen ((char *) args->socket_filename));
617 }
618 else
619 {
620 uint16_t sdl = strlen (MEMIF_DEFAULT_SOCKET_DIR);
621 uint16_t sfl = strlen (MEMIF_DEFAULT_SOCKET_FILENAME);
622 if (conn->args.socket_filename == NULL)
623 {
624 err = memif_syscall_error_handler (errno);
625 goto error;
626 }
627 strncpy ((char *) conn->args.socket_filename,
628 MEMIF_DEFAULT_SOCKET_DIR, sdl);
629 conn->args.socket_filename[sdl] = '/';
630 strncpy ((char *) (conn->args.socket_filename + 1 + sdl),
631 MEMIF_DEFAULT_SOCKET_FILENAME, sfl);
632 }
633
634 if (args->secret)
635 {
636 l = strlen ((char *) args->secret);
637 strncpy ((char *) conn->args.secret, (char *) args->secret, l);
638 }
639
640 if (conn->args.is_master)
641 {
642 conn->run_args.buffer_size = conn->args.buffer_size;
643 memif_socket_t *ms;
644 memif_list_elt_t elt;
645 for (i = 0; i < lm->listener_list_len; i++)
646 {
647 if ((ms =
648 (memif_socket_t *) lm->listener_list[i].data_struct) != NULL)
649 {
650 if (strncmp
651 ((char *) ms->filename, (char *) conn->args.socket_filename,
652 strlen ((char *) ms->filename)) == 0)
653 {
654 /* add interface to listener socket */
655 elt.key = conn->args.interface_id;
656 *c = elt.data_struct = conn;
657 add_list_elt (&elt, &ms->interface_list,
658 &ms->interface_list_len);
659 ms->use_count++;
660 conn->listener_fd = ms->fd;
661 break;
662 }
663 }
664 else
665 {
666 struct stat file_stat;
667 if (stat ((char *) conn->args.socket_filename, &file_stat) == 0)
668 {
669 if (S_ISSOCK (file_stat.st_mode))
670 unlink ((char *) conn->args.socket_filename);
671 else
672 return memif_syscall_error_handler (errno);
673 }
674 DBG ("creating socket file");
675 ms = malloc (sizeof (memif_socket_t));
Jakub Grajciarb467b2a2017-09-14 14:12:10 +0200676 ms->filename =
677 malloc (strlen ((char *) conn->args.socket_filename) +
678 sizeof (char));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200679 memset (ms->filename, 0,
680 strlen ((char *) conn->args.socket_filename) +
681 sizeof (char));
682 strncpy ((char *) ms->filename,
683 (char *) conn->args.socket_filename,
684 strlen ((char *) conn->args.socket_filename));
685 ms->interface_list_len = 1;
686 ms->interface_list =
687 malloc (sizeof (memif_list_elt_t) * ms->interface_list_len);
688 ms->interface_list[0].key = -1;
689 ms->interface_list[0].data_struct = NULL;
690 struct sockaddr_un un = { 0 };
691 int on = 1;
692
693 ms->fd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
694 if (ms->fd < 0)
695 {
696 err = memif_syscall_error_handler (errno);
697 goto error;
698 }
699 DBG ("socket %d created", ms->fd);
700 un.sun_family = AF_UNIX;
701 strncpy ((char *) un.sun_path, (char *) ms->filename,
702 sizeof (un.sun_path) - 1);
703 DBG ("sockopt");
704 if (setsockopt
705 (ms->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0)
706 {
707 err = memif_syscall_error_handler (errno);
708 goto error;
709 }
710 DBG ("bind");
711 if (bind (ms->fd, (struct sockaddr *) &un, sizeof (un)) < 0)
712 {
713 err = memif_syscall_error_handler (errno);
714 goto error;
715 }
716 DBG ("listen");
717 if (listen (ms->fd, 1) < 0)
718 {
719 err = memif_syscall_error_handler (errno);
720 goto error;
721 }
722 DBG ("stat");
723 if (stat ((char *) ms->filename, &file_stat) < 0)
724 {
725 err = memif_syscall_error_handler (errno);
726 goto error;
727 }
728
729 /* add interface to listener socket */
730 elt.key = conn->args.interface_id;
731 *c = elt.data_struct = conn;
732 add_list_elt (&elt, &ms->interface_list,
733 &ms->interface_list_len);
734 ms->use_count = 1;
735 conn->listener_fd = ms->fd;
736
737 /* add listener socket to libmemif main */
738 elt.key = ms->fd;
739 elt.data_struct = ms;
740 add_list_elt (&elt, &lm->listener_list, &lm->listener_list_len);
741 lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ);
742 break;
743 }
744 }
745 }
746 else
747 {
748 if (lm->disconn_slaves == 0)
749 {
750 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
751 {
752 err = memif_syscall_error_handler (errno);
753 goto error;
754 }
755 }
756
757 lm->disconn_slaves++;
758
759 list_elt.key = -1;
760 *c = list_elt.data_struct = conn;
761 if ((index =
762 add_list_elt (&list_elt, &lm->control_list,
763 &lm->control_list_len)) < 0)
764 {
765 err = MEMIF_ERR_NOMEM;
766 goto error;
767 }
768 }
769
770 conn->index = index;
771
772 return 0;
773
774error:
775 if (sockfd > 0)
776 close (sockfd);
777 sockfd = -1;
778 if (conn->args.socket_filename)
779 free (conn->args.socket_filename);
780 if (conn != NULL)
781 free (conn);
782 *c = conn = NULL;
783 return err;
784}
785
786int
787memif_control_fd_handler (int fd, uint8_t events)
788{
789 int i, rv, sockfd = -1, err = MEMIF_ERR_SUCCESS; /* 0 */
790 uint16_t num;
791 memif_list_elt_t *e = NULL;
792 memif_connection_t *conn;
793 libmemif_main_t *lm = &libmemif_main;
794 if (fd == lm->timerfd)
795 {
796 uint64_t b;
797 ssize_t size;
798 size = read (fd, &b, sizeof (b));
799 for (i = 0; i < lm->control_list_len; i++)
800 {
801 if ((lm->control_list[i].key < 0)
802 && (lm->control_list[i].data_struct != NULL))
803 {
804 conn = lm->control_list[i].data_struct;
805 if (conn->args.is_master)
806 continue;
807
808 struct sockaddr_un sun;
809 sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
810 if (sockfd < 0)
811 {
812 err = memif_syscall_error_handler (errno);
813 goto error;
814 }
815
816 sun.sun_family = AF_UNIX;
817
818 strncpy (sun.sun_path, conn->args.socket_filename,
819 sizeof (sun.sun_path) - 1);
820
821 if (connect (sockfd, (struct sockaddr *) &sun,
822 sizeof (struct sockaddr_un)) == 0)
823 {
824 conn->fd = sockfd;
825 conn->read_fn = memif_conn_fd_read_ready;
826 conn->write_fn = memif_conn_fd_write_ready;
827 conn->error_fn = memif_conn_fd_error;
828
829 lm->control_list[conn->index].key = conn->fd;
830
831 lm->control_fd_update (sockfd,
832 MEMIF_FD_EVENT_READ |
833 MEMIF_FD_EVENT_WRITE);
834
835 lm->disconn_slaves--;
836 if (lm->disconn_slaves == 0)
837 {
838 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL)
839 < 0)
840 {
841 err = memif_syscall_error_handler (errno);
842 goto error;
843 }
844 }
845 }
846 else
847 {
848 err = memif_syscall_error_handler (errno);
849 goto error;
850 }
851 }
852 }
853 }
854 else
855 {
856 get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd);
857 if (e != NULL)
858 {
859 if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL)
860 {
861 num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +0200862 (((memif_connection_t *) e->data_struct)->args.
863 is_master) ? ((memif_connection_t *) e->data_struct)->
864 run_args.num_s2m_rings : ((memif_connection_t *) e->
865 data_struct)->run_args.
866 num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200867 for (i = 0; i < num; i++)
868 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +0200869 if (((memif_connection_t *) e->data_struct)->rx_queues[i].
870 int_fd == fd)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200871 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +0200872 ((memif_connection_t *) e->
873 data_struct)->on_interrupt ((void *) e->data_struct,
874 ((memif_connection_t *)
875 e->data_struct)->
876 private_ctx, i);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200877 return MEMIF_ERR_SUCCESS;
878 }
879 }
880 }
881 return MEMIF_ERR_SUCCESS;
882 }
883 get_list_elt (&e, lm->listener_list, lm->listener_list_len, fd);
884 if (e != NULL)
885 {
886 memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct);
887 return MEMIF_ERR_SUCCESS;
888 }
889
890 get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd);
891 if (e != NULL)
892 {
893 memif_read_ready (fd);
894 return MEMIF_ERR_SUCCESS;
895 }
896
897 get_list_elt (&e, lm->control_list, lm->control_list_len, fd);
898 if (e != NULL)
899 {
900 if (events & MEMIF_FD_EVENT_READ)
901 {
902 err =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +0200903 ((memif_connection_t *) e->data_struct)->read_fn (e->
904 data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200905 if (err != MEMIF_ERR_SUCCESS)
906 return err;
907 }
908 if (events & MEMIF_FD_EVENT_WRITE)
909 {
910 err =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +0200911 ((memif_connection_t *) e->data_struct)->write_fn (e->
912 data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200913 if (err != MEMIF_ERR_SUCCESS)
914 return err;
915 }
916 if (events & MEMIF_FD_EVENT_ERROR)
917 {
918 err =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +0200919 ((memif_connection_t *) e->data_struct)->error_fn (e->
920 data_struct);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200921 if (err != MEMIF_ERR_SUCCESS)
922 return err;
923 }
924 }
925 }
926
927 return MEMIF_ERR_SUCCESS; /* 0 */
928
929error:
930 if (sockfd > 0)
931 close (sockfd);
932 sockfd = -1;
933 return err;
934}
935
936int
937memif_poll_event (int timeout)
938{
939 libmemif_main_t *lm = &libmemif_main;
940 memif_list_elt_t *elt;
941 struct epoll_event evt, *e;
942 int en = 0, err = MEMIF_ERR_SUCCESS, i = 0; /* 0 */
943 uint16_t num;
944 uint32_t events = 0;
Milan Lenco0a47c992017-10-12 14:19:31 +0200945 uint64_t counter = 0;
946 ssize_t r = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200947 memset (&evt, 0, sizeof (evt));
948 evt.events = EPOLLIN | EPOLLOUT;
949 sigset_t sigset;
950 sigemptyset (&sigset);
951 en = epoll_pwait (memif_epfd, &evt, 1, timeout, &sigset);
952 if (en < 0)
953 {
Milan Lenco0a47c992017-10-12 14:19:31 +0200954 err = errno;
955 DBG ("epoll_pwait: %s", strerror (err));
956 return memif_syscall_error_handler (err);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200957 }
958 if (en > 0)
959 {
Milan Lenco0a47c992017-10-12 14:19:31 +0200960 if (evt.data.fd == poll_cancel_fd)
961 {
962 r = read (evt.data.fd, &counter, sizeof (counter));
963 return MEMIF_ERR_POLL_CANCEL;
964 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200965 if (evt.events & EPOLLIN)
966 events |= MEMIF_FD_EVENT_READ;
967 if (evt.events & EPOLLOUT)
968 events |= MEMIF_FD_EVENT_WRITE;
969 if (evt.events & EPOLLERR)
970 events |= MEMIF_FD_EVENT_ERROR;
971 err = memif_control_fd_handler (evt.data.fd, events);
972 return err;
973 }
974 return 0;
975}
976
Milan Lenco0a47c992017-10-12 14:19:31 +0200977int
978memif_cancel_poll_event ()
979{
980 uint64_t counter = 1;
981 ssize_t w = 0;
982
983 if (poll_cancel_fd == -1)
984 return 0;
985 w = write (poll_cancel_fd, &counter, sizeof (counter));
986 if (w < sizeof (counter))
987 return MEMIF_ERR_INT_WRITE;
988
989 return 0;
990}
991
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200992static void
993memif_msg_queue_free (memif_msg_queue_elt_t ** e)
994{
995 if (*e == NULL)
996 return;
997 memif_msg_queue_free (&(*e)->next);
998 free (*e);
999 *e = NULL;
1000 return;
1001}
1002
1003/* send disconnect msg and close interface */
1004int
1005memif_disconnect_internal (memif_connection_t * c)
1006{
1007 if (c == NULL)
1008 {
1009 DBG ("no connection");
1010 return MEMIF_ERR_NOCONN;
1011 }
1012 uint16_t num;
1013 int err = MEMIF_ERR_SUCCESS, i; /* 0 */
1014 memif_queue_t *mq;
1015 libmemif_main_t *lm = &libmemif_main;
1016 memif_list_elt_t *e;
1017
1018 c->on_disconnect ((void *) c, c->private_ctx);
1019
1020 if (c->fd > 0)
1021 {
1022 memif_msg_send_disconnect (c->fd, "interface deleted", 0);
1023 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL);
1024 close (c->fd);
1025 }
1026 get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd);
1027 if (e != NULL)
1028 {
1029 if (c->args.is_master)
1030 free_list_elt (lm->control_list, lm->control_list_len, c->fd);
1031 e->key = c->fd = -1;
1032 }
1033
1034 if (c->tx_queues != NULL)
1035 {
1036 num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001037 (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
1038 num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001039 for (i = 0; i < num; i++)
1040 {
1041 mq = &c->tx_queues[i];
1042 if (mq != NULL)
1043 {
1044 if (mq->int_fd > 0)
1045 close (mq->int_fd);
1046 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1047 mq->int_fd);
1048 mq->int_fd = -1;
1049 }
1050 }
1051 free (c->tx_queues);
1052 c->tx_queues = NULL;
1053 }
1054
1055 if (c->rx_queues != NULL)
1056 {
1057 num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001058 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
1059 num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001060 for (i = 0; i < num; i++)
1061 {
1062 mq = &c->rx_queues[i];
1063 if (mq != NULL)
1064 {
1065 if (mq->int_fd > 0)
1066 {
1067 if (c->on_interrupt != NULL)
1068 lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL);
1069 close (mq->int_fd);
1070 }
1071 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1072 mq->int_fd);
1073 mq->int_fd = -1;
1074 }
1075 }
1076 free (c->rx_queues);
1077 c->rx_queues = NULL;
1078 }
1079
1080 if (c->regions != NULL)
1081 {
1082 if (munmap (c->regions[0].shm, c->regions[0].region_size) < 0)
1083 return memif_syscall_error_handler (errno);
1084 if (c->regions[0].fd > 0)
1085 close (c->regions[0].fd);
1086 c->regions[0].fd = -1;
1087 free (c->regions);
1088 c->regions = NULL;
1089 }
1090
1091 memset (&c->run_args, 0, sizeof (memif_conn_run_args_t));
1092
1093 memif_msg_queue_free (&c->msg_queue);
1094
1095 if (!(c->args.is_master))
1096 {
1097 if (lm->disconn_slaves == 0)
1098 {
1099 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
1100 {
1101 err = memif_syscall_error_handler (errno);
1102 DBG_UNIX ("timerfd_settime: arm");
1103 }
1104 }
1105 lm->disconn_slaves++;
1106 }
1107
1108 return err;
1109}
1110
1111int
1112memif_delete (memif_conn_handle_t * conn)
1113{
1114 memif_connection_t *c = (memif_connection_t *) * conn;
1115 if (c == NULL)
1116 {
1117 DBG ("no connection");
1118 return MEMIF_ERR_NOCONN;
1119 }
1120 libmemif_main_t *lm = &libmemif_main;
1121 memif_list_elt_t *e = NULL;
1122 memif_socket_t *ms = NULL;
1123
1124 int err = MEMIF_ERR_SUCCESS;
1125
1126 if (c->fd > 0)
1127 {
1128 DBG ("DISCONNECTING");
1129 err = memif_disconnect_internal (c);
1130 if (err == MEMIF_ERR_NOCONN)
1131 return err;
1132 }
1133
1134 free_list_elt_ctx (lm->control_list, lm->control_list_len, c);
1135
1136 if (c->args.is_master)
1137 {
1138 get_list_elt (&e, lm->listener_list, lm->listener_list_len,
1139 c->listener_fd);
1140 if (e != NULL)
1141 {
1142 ms = (memif_socket_t *) e->data_struct;
1143 ms->use_count--;
1144 free_list_elt (ms->interface_list, ms->interface_list_len,
1145 c->args.interface_id);
1146 if (ms->use_count <= 0)
1147 {
1148 lm->control_fd_update (c->listener_fd, MEMIF_FD_EVENT_DEL);
1149 free_list_elt (lm->listener_list, lm->listener_list_len,
1150 c->listener_fd);
1151 close (c->listener_fd);
1152 c->listener_fd = ms->fd = -1;
1153 free (ms->interface_list);
1154 ms->interface_list = NULL;
1155 free (ms->filename);
1156 ms->filename = NULL;
1157 free (ms);
1158 ms = NULL;
1159 }
1160 }
1161 }
1162 else
1163 {
1164 lm->disconn_slaves--;
1165 if (lm->disconn_slaves <= 0)
1166 {
1167 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0)
1168 {
1169 err = memif_syscall_error_handler (errno);
1170 DBG ("timerfd_settime: disarm");
1171 }
1172 }
1173 }
1174
1175 if (c->args.socket_filename)
1176 free (c->args.socket_filename);
1177 c->args.socket_filename = NULL;
1178
1179 free (c);
1180 c = NULL;
1181
1182 *conn = c;
1183 return err;
1184}
1185
1186int
1187memif_connect1 (memif_connection_t * c)
1188{
1189 libmemif_main_t *lm = &libmemif_main;
1190 memif_region_t *mr = c->regions;
1191 memif_queue_t *mq;
1192 int i;
1193 uint16_t num;
1194
1195 if (mr != NULL)
1196 {
1197 if (!mr->shm)
1198 {
1199 if (mr->fd < 0)
1200 return MEMIF_ERR_NO_SHMFD;
1201
1202 if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE,
1203 MAP_SHARED, mr->fd, 0)) == MAP_FAILED)
1204 {
1205 return memif_syscall_error_handler (errno);
1206 }
1207 }
1208 }
1209
1210 num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001211 (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
1212 num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001213 for (i = 0; i < num; i++)
1214 {
1215 mq = &c->tx_queues[i];
1216 if (mq != NULL)
1217 {
1218 mq->ring = c->regions[mq->region].shm + mq->offset;
1219 if (mq->ring->cookie != MEMIF_COOKIE)
1220 {
1221 DBG ("wrong cookie on tx ring %u", i);
1222 return MEMIF_ERR_COOKIE;
1223 }
1224 mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
1225 0;
1226 }
1227 }
1228 num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001229 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
1230 num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001231 for (i = 0; i < num; i++)
1232 {
1233 mq = &c->rx_queues[i];
1234 if (mq != NULL)
1235 {
1236 mq->ring = c->regions[mq->region].shm + mq->offset;
1237 if (mq->ring->cookie != MEMIF_COOKIE)
1238 {
1239 DBG ("wrong cookie on rx ring %u", i);
1240 return MEMIF_ERR_COOKIE;
1241 }
1242 mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
1243 0;
1244 }
1245 }
1246
1247 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD);
1248
1249 return 0;
1250}
1251
1252int
1253memif_init_regions_and_queues (memif_connection_t * conn)
1254{
1255 memif_ring_t *ring = NULL;
1256 uint64_t buffer_offset;
1257 memif_region_t *r;
1258 int i, j;
1259 libmemif_main_t *lm = &libmemif_main;
1260 memif_list_elt_t e;
1261
1262 conn->regions = (memif_region_t *) malloc (sizeof (memif_region_t));
1263 if (conn->regions == NULL)
1264 return memif_syscall_error_handler (errno);
1265 r = conn->regions;
1266
1267 buffer_offset =
1268 (conn->run_args.num_s2m_rings +
1269 conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) +
1270 sizeof (memif_desc_t) *
1271 (1 << conn->run_args.log2_ring_size));
1272
1273 r->region_size = buffer_offset +
1274 conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
1275 (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
1276
1277 if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1)
1278 return memif_syscall_error_handler (errno);
1279/*
1280 if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
1281 return memif_syscall_error_handler (errno);
1282*/
1283 if ((ftruncate (r->fd, r->region_size)) == -1)
1284 return memif_syscall_error_handler (errno);
1285
1286 if ((r->shm = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE,
1287 MAP_SHARED, r->fd, 0)) == MAP_FAILED)
1288 return memif_syscall_error_handler (errno);
1289
1290 for (i = 0; i < conn->run_args.num_s2m_rings; i++)
1291 {
1292 ring = memif_get_ring (conn, MEMIF_RING_S2M, i);
1293 DBG ("RING: %p I: %d", ring, i);
1294 ring->head = ring->tail = 0;
1295 ring->cookie = MEMIF_COOKIE;
1296 ring->flags = 0;
1297 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1298 {
1299 uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j;
1300 ring->desc[j].region = 0;
1301 ring->desc[j].offset = buffer_offset +
1302 (uint32_t) (slot * conn->run_args.buffer_size);
1303 ring->desc[j].buffer_length = conn->run_args.buffer_size;
1304 }
1305 }
1306 for (i = 0; i < conn->run_args.num_m2s_rings; i++)
1307 {
1308 ring = memif_get_ring (conn, MEMIF_RING_M2S, i);
1309 DBG ("RING: %p I: %d", ring, i);
1310 ring->head = ring->tail = 0;
1311 ring->cookie = MEMIF_COOKIE;
1312 ring->flags = 0;
1313 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1314 {
1315 uint16_t slot =
1316 (i +
1317 conn->run_args.num_s2m_rings) *
1318 (1 << conn->run_args.log2_ring_size) + j;
1319 ring->desc[j].region = 0;
1320 ring->desc[j].offset = buffer_offset +
1321 (uint32_t) (slot * conn->run_args.buffer_size);
1322 ring->desc[j].buffer_length = conn->run_args.buffer_size;
1323 }
1324 }
1325 memif_queue_t *mq;
1326 mq =
1327 (memif_queue_t *) malloc (sizeof (memif_queue_t) *
1328 conn->run_args.num_s2m_rings);
1329 if (mq == NULL)
1330 return memif_syscall_error_handler (errno);
1331 int x;
1332 for (x = 0; x < conn->run_args.num_s2m_rings; x++)
1333 {
1334 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1335 return memif_syscall_error_handler (errno);
1336 /* add int fd to interrupt fd list */
1337 e.key = mq[x].int_fd;
1338 e.data_struct = conn;
1339 add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
1340
1341 mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x);
1342 DBG ("RING: %p I: %d", mq[x].ring, x);
1343 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1344 mq[x].region = 0;
1345 mq[x].offset =
1346 (void *) mq[x].ring - (void *) conn->regions[mq->region].shm;
1347 mq[x].last_head = 0;
1348 mq[x].alloc_bufs = 0;
1349 }
1350 conn->tx_queues = mq;
1351
1352 mq =
1353 (memif_queue_t *) malloc (sizeof (memif_queue_t) *
1354 conn->run_args.num_m2s_rings);
1355 if (mq == NULL)
1356 return memif_syscall_error_handler (errno);
1357 for (x = 0; x < conn->run_args.num_m2s_rings; x++)
1358 {
1359 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1360 return memif_syscall_error_handler (errno);
1361 /* add int fd to interrupt fd list */
1362 e.key = mq[x].int_fd;
1363 e.data_struct = conn;
1364 add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
1365
1366 mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x);
1367 DBG ("RING: %p I: %d", mq[x].ring, x);
1368 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1369 mq[x].region = 0;
1370 mq[x].offset =
1371 (void *) mq[x].ring - (void *) conn->regions[mq->region].shm;
1372 mq[x].last_head = 0;
1373 mq[x].alloc_bufs = 0;
1374 }
1375 conn->rx_queues = mq;
1376
1377 return 0;
1378}
1379
1380int
1381memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
1382 memif_buffer_t * bufs, uint16_t count,
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001383 uint16_t * count_out, uint16_t size)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001384{
1385 memif_connection_t *c = (memif_connection_t *) conn;
1386 if (c == NULL)
1387 return MEMIF_ERR_NOCONN;
1388 if (c->fd < 0)
1389 return MEMIF_ERR_DISCONNECTED;
1390 uint8_t num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001391 (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
1392 num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001393 if (qid >= num)
1394 return MEMIF_ERR_QID;
1395 memif_queue_t *mq = &c->tx_queues[qid];
1396 memif_ring_t *ring = mq->ring;
1397 memif_buffer_t *b0, *b1;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001398 uint8_t chain_buf = 1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001399 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001400 uint16_t head = ring->head;
1401 uint16_t tail = ring->tail;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001402 uint16_t s0, s1, ns;
1403 *count_out = 0;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001404 int i, err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001405
Damjan Marion6d56fa42017-11-03 12:24:37 +01001406 ns = (1 << mq->log2_ring_size) - head + tail;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001407
Damjan Marion6d56fa42017-11-03 12:24:37 +01001408 /* calculate number of chain buffers */
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001409 if (size > ring->desc[0].buffer_length)
1410 {
1411 chain_buf = size / ring->desc[s0].buffer_length;
1412 if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0))
1413 chain_buf++;
1414 }
1415
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001416 while (count && ns)
1417 {
1418 while ((count > 2) && (ns > 2))
1419 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001420 s0 = (ring->head + mq->alloc_bufs) & mask;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001421 s1 = (ring->head + mq->alloc_bufs + chain_buf) & mask;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001422
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001423 if ((2 * chain_buf) > ns)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001424 break;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001425
1426 b0 = (bufs + *count_out);
1427 b1 = (bufs + *count_out + 1);
1428
Damjan Marion6d56fa42017-11-03 12:24:37 +01001429 b0->desc_index = head + mq->alloc_bufs;
1430 b1->desc_index = head + mq->alloc_bufs + chain_buf;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001431 ring->desc[s0].flags = 0;
1432 ring->desc[s1].flags = 0;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001433 b0->buffer_len = ring->desc[s0].buffer_length * chain_buf;
1434 b1->buffer_len = ring->desc[s1].buffer_length * chain_buf;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001435 /* TODO: support multiple regions -> ring descriptor contains region index */
1436 b0->data = c->regions->shm + ring->desc[s0].offset;
1437 b1->data = c->regions->shm + ring->desc[s1].offset;
1438
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001439 for (i = 0; i < (chain_buf - 1); i++)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001440 {
1441 ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
1442 ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
1443 DBG ("allocating chained buffers");
1444 }
1445
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001446 mq->alloc_bufs += 2 * chain_buf;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001447
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001448 DBG ("allocated ring slots %u, %u", s0, s1);
1449 count -= 2;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001450 ns -= (2 * chain_buf);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001451 *count_out += 2;
1452 }
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001453 s0 = (ring->head + mq->alloc_bufs) & mask;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001454
1455 b0 = (bufs + *count_out);
1456
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001457 if (chain_buf > ns)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001458 break;
1459
Damjan Marion6d56fa42017-11-03 12:24:37 +01001460 b0->desc_index = head + mq->alloc_bufs;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001461 ring->desc[s0].flags = 0;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001462 b0->buffer_len = ring->desc[s0].buffer_length * chain_buf;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001463 b0->data = c->regions->shm + ring->desc[s0].offset;
1464
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001465 for (i = 0; i < (chain_buf - 1); i++)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001466 {
1467 ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
1468 DBG ("allocating chained buffers");
1469 }
1470
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001471 mq->alloc_bufs += chain_buf;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001472
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001473 DBG ("allocated ring slot %u", s0);
1474 count--;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001475 ns -= chain_buf;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001476 *count_out += 1;
1477 }
1478
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001479 DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
1480 mq->alloc_bufs);
1481
1482 if (count)
1483 {
1484 DBG ("ring buffer full! qid: %u", qid);
1485 err = MEMIF_ERR_NOBUF_RING;
1486 }
1487
1488 return err;
1489}
1490
1491int
1492memif_buffer_free (memif_conn_handle_t conn, uint16_t qid,
1493 memif_buffer_t * bufs, uint16_t count,
1494 uint16_t * count_out)
1495{
1496 memif_connection_t *c = (memif_connection_t *) conn;
1497 if (c == NULL)
1498 return MEMIF_ERR_NOCONN;
1499 if (c->fd < 0)
1500 return MEMIF_ERR_DISCONNECTED;
1501 uint8_t num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001502 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
1503 num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001504 if (qid >= num)
1505 return MEMIF_ERR_QID;
1506 libmemif_main_t *lm = &libmemif_main;
1507 memif_queue_t *mq = &c->rx_queues[qid];
1508 memif_ring_t *ring = mq->ring;
1509 uint16_t tail = ring->tail;
1510 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001511 uint8_t chain_buf0, chain_buf1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001512 memif_buffer_t *b0, *b1;
1513 *count_out = 0;
1514
1515 if (mq->alloc_bufs < count)
1516 count = mq->alloc_bufs;
1517
1518 while (count)
1519 {
1520 while (count > 2)
1521 {
1522 b0 = (bufs + *count_out);
1523 b1 = (bufs + *count_out + 1);
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001524 chain_buf0 =
1525 b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
1526 if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) !=
1527 0)
1528 chain_buf0++;
1529 chain_buf1 =
1530 b1->buffer_len / ring->desc[b1->desc_index].buffer_length;
1531 if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) !=
1532 0)
1533 chain_buf1++;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001534 tail = b1->desc_index + chain_buf1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001535 b0->data = NULL;
1536 b1->data = NULL;
1537
1538 count -= 2;
1539 *count_out += 2;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001540 mq->alloc_bufs -= chain_buf0 + chain_buf1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001541 }
1542 b0 = (bufs + *count_out);
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001543 chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
1544 if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0)
1545 chain_buf0++;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001546 tail = b0->desc_index + chain_buf0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001547 b0->data = NULL;
1548
1549 count--;
1550 *count_out += 1;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001551 mq->alloc_bufs -= chain_buf0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001552 }
Milan Lenco0a47c992017-10-12 14:19:31 +02001553 MEMIF_MEMORY_BARRIER ();
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001554 ring->tail = tail;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001555 DBG ("tail: %u", ring->tail);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001556
1557 return MEMIF_ERR_SUCCESS; /* 0 */
1558}
1559
1560int
1561memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
1562 memif_buffer_t * bufs, uint16_t count, uint16_t * tx)
1563{
1564 memif_connection_t *c = (memif_connection_t *) conn;
1565 if (c == NULL)
1566 return MEMIF_ERR_NOCONN;
1567 if (c->fd < 0)
1568 return MEMIF_ERR_DISCONNECTED;
1569 uint8_t num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001570 (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
1571 num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001572 if (qid >= num)
1573 return MEMIF_ERR_QID;
1574 memif_queue_t *mq = &c->tx_queues[qid];
1575 memif_ring_t *ring = mq->ring;
1576 uint16_t head = ring->head;
1577 uint16_t mask = (1 << mq->log2_ring_size) - 1;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001578 uint8_t chain_buf0, chain_buf1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001579 *tx = 0;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001580 uint16_t curr_buf = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001581 memif_buffer_t *b0, *b1;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001582 int i;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001583
1584 while (count)
1585 {
1586 while (count > 2)
1587 {
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001588 b0 = (bufs + curr_buf);
1589 b1 = (bufs + curr_buf + 1);
1590 chain_buf0 =
1591 b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
1592 if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) !=
1593 0)
1594 chain_buf0++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001595
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001596 chain_buf1 =
1597 b1->buffer_len / ring->desc[b1->desc_index].buffer_length;
1598 if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) !=
1599 0)
1600 chain_buf1++;
1601
1602 for (i = 0; i < memif_min (chain_buf0, chain_buf1); i++)
1603 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001604 /* b0 */
1605 if (b0->data_len >
1606 ring->desc[(b0->desc_index + i) & mask].buffer_length)
1607 {
1608 b0->data_len -=
1609 ring->desc[(b0->desc_index + i) & mask].length =
1610 ring->desc[(b0->desc_index + i) & mask].buffer_length;
1611 }
1612 else
1613 {
1614 ring->desc[(b0->desc_index + i) & mask].length =
1615 b0->data_len;
1616 b0->data_len = 0;
1617 }
1618 /* b1 */
1619 if (b1->data_len >
1620 ring->desc[(b1->desc_index + i) & mask].buffer_length)
1621 {
1622 b1->data_len -=
1623 ring->desc[(b1->desc_index + i) & mask].length =
1624 ring->desc[(b1->desc_index + i) & mask].buffer_length;
1625 }
1626 else
1627 {
1628 ring->desc[(b1->desc_index + i) & mask].length =
1629 b1->data_len;
1630 b1->data_len = 0;
1631 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001632#ifdef MEMIF_DBG_SHM
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001633 print_bytes (b0->data +
1634 ring->desc[(b0->desc_index +
1635 i) & mask].buffer_length *
1636 (chain_buf0 - 1),
1637 ring->desc[(b0->desc_index +
1638 i) & mask].buffer_length, DBG_TX_BUF);
1639 print_bytes (b1->data +
1640 ring->desc[(b1->desc_index +
1641 i) & mask].buffer_length *
1642 (chain_buf1 - 1),
1643 ring->desc[(b1->desc_index +
1644 i) & mask].buffer_length, DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001645#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001646 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001647
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001648 if (chain_buf0 > chain_buf1)
1649 {
1650 for (; i < chain_buf0; i++)
1651 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001652 if (b0->data_len >
1653 ring->desc[(b0->desc_index + i) & mask].buffer_length)
1654 {
1655 b0->data_len -=
1656 ring->desc[(b0->desc_index + i) & mask].length =
1657 ring->desc[(b0->desc_index + i) & mask].buffer_length;
1658 }
1659 else
1660 {
1661 ring->desc[(b0->desc_index + i) & mask].length =
1662 b0->data_len;
1663 b0->data_len = 0;
1664 }
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001665#ifdef MEMIF_DBG_SHM
1666 print_bytes (b0->data +
1667 ring->desc[(b0->desc_index +
1668 i) & mask].buffer_length *
1669 (chain_buf0 - 1),
1670 ring->desc[(b0->desc_index +
1671 i) & mask].buffer_length,
1672 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001673#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001674 }
1675 }
1676 else
1677 {
1678 for (; i < chain_buf1; i++)
1679 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001680 if (b1->data_len >
1681 ring->desc[(b1->desc_index + i) & mask].buffer_length)
1682 {
1683 b1->data_len -=
1684 ring->desc[(b1->desc_index + i) & mask].length =
1685 ring->desc[(b1->desc_index + i) & mask].buffer_length;
1686 }
1687 else
1688 {
1689 ring->desc[(b1->desc_index + i) & mask].length =
1690 b1->data_len;
1691 b1->data_len = 0;
1692 }
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001693#ifdef MEMIF_DBG_SHM
1694 print_bytes (b1->data +
1695 ring->desc[(b1->desc_index +
1696 i) & mask].buffer_length *
1697 (chain_buf1 - 1),
1698 ring->desc[(b1->desc_index +
1699 i) & mask].buffer_length,
1700 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001701#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001702 }
1703 }
1704
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001705 head = (b1->desc_index + chain_buf1) & mask;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001706
1707 b0->data = NULL;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001708#ifdef MEMIF_DBG
1709 if (b0->data_len != 0)
1710 DBG ("invalid b0 data length!");
1711#endif /* MEMIF_DBG */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001712 b1->data = NULL;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001713#ifdef MEMIF_DBG
1714 if (b1->data_len != 0)
1715 DBG ("invalid b1 data length!");
1716#endif /* MEMIF_DBG */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001717
1718 count -= 2;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001719 *tx += chain_buf0 + chain_buf1;
1720 curr_buf += 2;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001721 }
1722
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001723 b0 = (bufs + curr_buf);
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001724 chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
1725 if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0)
1726 chain_buf0++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001727
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001728 for (i = 0; i < chain_buf0; i++)
1729 {
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001730 if (b0->data_len >
1731 ring->desc[(b0->desc_index + i) & mask].buffer_length)
1732 {
1733 b0->data_len -= ring->desc[(b0->desc_index + i) & mask].length =
1734 ring->desc[(b0->desc_index + i) & mask].buffer_length;
1735 }
1736 else
1737 {
1738 ring->desc[(b0->desc_index + i) & mask].length = b0->data_len;
1739 b0->data_len = 0;
1740 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001741#ifdef MEMIF_DBG_SHM
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001742 print_bytes (b0->data +
1743 ring->desc[(b0->desc_index + i) & mask].buffer_length *
1744 (chain_buf0 - 1),
1745 ring->desc[(b0->desc_index + i) & mask].buffer_length,
1746 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001747#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001748 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001749
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001750 head = (b0->desc_index + chain_buf0) & mask;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001751
1752 b0->data = NULL;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001753#ifdef MEMIF_DBG
1754 if (b0->data_len != 0)
1755 DBG ("invalid b0 data length!");
1756#endif /* MEMIF_DBG */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001757
1758 count--;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001759 *tx += chain_buf0;
1760 curr_buf++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001761 }
Milan Lenco0a47c992017-10-12 14:19:31 +02001762 MEMIF_MEMORY_BARRIER ();
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001763 ring->head = head;
1764
1765 mq->alloc_bufs -= *tx;
1766
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001767 /* TODO: return num of buffers and packets */
1768 *tx = curr_buf;
1769
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001770 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
1771 {
1772 uint64_t a = 1;
1773 int r = write (mq->int_fd, &a, sizeof (a));
1774 if (r < 0)
1775 return MEMIF_ERR_INT_WRITE;
1776 }
1777
1778 return MEMIF_ERR_SUCCESS; /* 0 */
1779}
1780
1781int
1782memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
1783 memif_buffer_t * bufs, uint16_t count, uint16_t * rx)
1784{
1785 memif_connection_t *c = (memif_connection_t *) conn;
1786 if (c == NULL)
1787 return MEMIF_ERR_NOCONN;
1788 if (c->fd < 0)
1789 return MEMIF_ERR_DISCONNECTED;
1790 uint8_t num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001791 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
1792 num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001793 if (qid >= num)
1794 return MEMIF_ERR_QID;
1795 memif_queue_t *mq = &c->rx_queues[qid];
1796 memif_ring_t *ring = mq->ring;
1797 uint16_t head = ring->head;
1798 uint16_t ns;
1799 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1800 memif_buffer_t *b0, *b1;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001801 uint16_t curr_buf = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001802 *rx = 0;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001803#ifdef MEMIF_DBG_SHM
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001804 int i;
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001805#endif /* MEMIF_DBG_SHM */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001806
1807 uint64_t b;
1808 ssize_t r = read (mq->int_fd, &b, sizeof (b));
1809 if ((r == -1) && (errno != EAGAIN))
1810 return memif_syscall_error_handler (errno);
1811
1812 if (head == mq->last_head)
1813 return 0;
1814
Damjan Marion6d56fa42017-11-03 12:24:37 +01001815 ns = head - mq->last_head;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001816
1817 while (ns && count)
1818 {
1819 while ((ns > 2) && (count > 2))
1820 {
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001821 b0 = (bufs + curr_buf);
1822 b1 = (bufs + curr_buf + 1);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001823
1824 b0->desc_index = mq->last_head;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001825 b0->data = memif_get_buffer (conn, ring, mq->last_head & mask);
1826 b0->data_len = ring->desc[mq->last_head & mask].length;
1827 b0->buffer_len = ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001828#ifdef MEMIF_DBG_SHM
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001829 i = 0;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001830 print_bytes (b0->data +
Damjan Marion6d56fa42017-11-03 12:24:37 +01001831 ring->desc[b0->desc_index & mask].buffer_length * i++,
1832 ring->desc[b0->desc_index & mask].buffer_length, DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001833#endif /* MEMIF_DBG_SHM */
1834 ns--;
1835 *rx += 1;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001836 while (ring->desc[mq->last_head & mask].flags & MEMIF_DESC_FLAG_NEXT)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001837 {
Damjan Marion6d56fa42017-11-03 12:24:37 +01001838 ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
1839 mq->last_head++;
1840 b0->data_len += ring->desc[mq->last_head & mask].length;
1841 b0->buffer_len += ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001842#ifdef MEMIF_DBG_SHM
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001843 print_bytes (b0->data +
Damjan Marion6d56fa42017-11-03 12:24:37 +01001844 ring->desc[b0->desc_index & mask].buffer_length * i++,
1845 ring->desc[b0->desc_index & mask].buffer_length,
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001846 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001847#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001848 ns--;
1849 *rx += 1;
1850 }
Damjan Marion6d56fa42017-11-03 12:24:37 +01001851 mq->last_head++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001852
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001853 b1->desc_index = mq->last_head;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001854 b1->data = memif_get_buffer (conn, ring, mq->last_head & mask);
1855 b1->data_len = ring->desc[mq->last_head & mask].length;
1856 b1->buffer_len = ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001857#ifdef MEMIF_DBG_SHM
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001858 i = 0;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001859 print_bytes (b1->data +
Damjan Marion6d56fa42017-11-03 12:24:37 +01001860 ring->desc[b1->desc_index & mask].buffer_length * i++,
1861 ring->desc[b1->desc_index & mask].buffer_length, DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001862#endif /* MEMIF_DBG_SHM */
1863 ns--;
1864 *rx += 1;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001865 while (ring->desc[mq->last_head & mask].flags & MEMIF_DESC_FLAG_NEXT)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001866 {
Damjan Marion6d56fa42017-11-03 12:24:37 +01001867 ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
1868 mq->last_head++;
1869 b1->data_len += ring->desc[mq->last_head & mask].length;
1870 b1->buffer_len += ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001871#ifdef MEMIF_DBG_SHM
1872 print_bytes (b1->data +
Damjan Marion6d56fa42017-11-03 12:24:37 +01001873 ring->desc[b1->desc_index & mask].buffer_length * i++,
1874 ring->desc[b1->desc_index & mask].buffer_length,
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001875 DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001876#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001877 ns--;
1878 *rx += 1;
1879 }
Damjan Marion6d56fa42017-11-03 12:24:37 +01001880 mq->last_head++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001881
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001882 count -= 2;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001883 curr_buf += 2;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001884 }
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001885 b0 = (bufs + curr_buf);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001886
1887 b0->desc_index = mq->last_head;
Damjan Marion6d56fa42017-11-03 12:24:37 +01001888 b0->data = memif_get_buffer (conn, ring, mq->last_head & mask);
1889 b0->data_len = ring->desc[mq->last_head & mask].length;
1890 b0->buffer_len = ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001891#ifdef MEMIF_DBG_SHM
Jakub Grajciar04b68bd2017-10-30 10:34:54 +01001892 i = 0;
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001893 print_bytes (b0->data +
Damjan Marion6d56fa42017-11-03 12:24:37 +01001894 ring->desc[b0->desc_index & mask].buffer_length * i++,
1895 ring->desc[b0->desc_index & mask].buffer_length, DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001896#endif /* MEMIF_DBG_SHM */
1897 ns--;
1898 *rx += 1;
1899
Damjan Marion6d56fa42017-11-03 12:24:37 +01001900 while (ring->desc[mq->last_head & mask].flags & MEMIF_DESC_FLAG_NEXT)
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001901 {
Damjan Marion6d56fa42017-11-03 12:24:37 +01001902 ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
1903 mq->last_head++;
1904 b0->data_len += ring->desc[mq->last_head & mask].length;
1905 b0->buffer_len += ring->desc[mq->last_head & mask].buffer_length;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001906#ifdef MEMIF_DBG_SHM
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001907 print_bytes (b0->data +
Damjan Marion6d56fa42017-11-03 12:24:37 +01001908 ring->desc[b0->desc_index & mask].buffer_length * i++,
1909 ring->desc[b0->desc_index & mask].buffer_length, DBG_TX_BUF);
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02001910#endif /* MEMIF_DBG_SHM */
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001911 ns--;
1912 *rx += 1;
1913 }
Damjan Marion6d56fa42017-11-03 12:24:37 +01001914 mq->last_head++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001915
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001916 count--;
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001917 curr_buf++;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001918 }
1919
1920 mq->alloc_bufs += *rx;
1921
Jakub Grajciarb467b2a2017-09-14 14:12:10 +02001922 /* TODO: return num of buffers and packets */
1923 *rx = curr_buf;
1924
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001925 if (ns)
1926 {
1927 DBG ("not enough buffers!");
1928 return MEMIF_ERR_NOBUF;
1929 }
1930
1931 return MEMIF_ERR_SUCCESS; /* 0 */
1932}
1933
1934int
1935memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
1936 char *buf, ssize_t buflen)
1937{
1938 memif_connection_t *c = (memif_connection_t *) conn;
1939 if (c == NULL)
1940 return MEMIF_ERR_NOCONN;
1941
1942 int err = MEMIF_ERR_SUCCESS, i;
1943 ssize_t l0, l1, total_l;
1944 l0 = 0;
1945
1946 l1 = strlen ((char *) c->args.interface_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001947 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001948 {
Milan Lenco0a47c992017-10-12 14:19:31 +02001949 md->if_name = strcpy (buf + l0, (char *) c->args.interface_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001950 l0 += l1 + 1;
1951 }
1952 else
1953 err = MEMIF_ERR_NOBUF_DET;
1954
1955 l1 = strlen ((char *) c->args.instance_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001956 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001957 {
Milan Lenco0a47c992017-10-12 14:19:31 +02001958 md->inst_name = strcpy (buf + l0, (char *) c->args.instance_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001959 l0 += l1 + 1;
1960 }
1961 else
1962 err = MEMIF_ERR_NOBUF_DET;
1963
1964 l1 = strlen ((char *) c->remote_if_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001965 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001966 {
Milan Lenco0a47c992017-10-12 14:19:31 +02001967 md->remote_if_name = strcpy (buf + l0, (char *) c->remote_if_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001968 l0 += l1 + 1;
1969 }
1970 else
1971 err = MEMIF_ERR_NOBUF_DET;
1972
1973 l1 = strlen ((char *) c->remote_name);
Milan Lenco0a47c992017-10-12 14:19:31 +02001974 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001975 {
Milan Lenco0a47c992017-10-12 14:19:31 +02001976 md->remote_inst_name = strcpy (buf + l0, (char *) c->remote_name);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001977 l0 += l1 + 1;
1978 }
1979 else
1980 err = MEMIF_ERR_NOBUF_DET;
1981
1982 md->id = c->args.interface_id;
1983
1984 if (c->args.secret)
1985 {
1986 l1 = strlen ((char *) c->args.secret);
Milan Lenco0a47c992017-10-12 14:19:31 +02001987 if (l0 + l1 < buflen)
1988 {
1989 md->secret = strcpy (buf + l0, (char *) c->args.secret);
1990 l0 += l1 + 1;
1991 }
1992 else
1993 err = MEMIF_ERR_NOBUF_DET;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001994 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001995
1996 md->role = (c->args.is_master) ? 0 : 1;
1997 md->mode = c->args.mode;
1998
1999 l1 = strlen ((char *) c->args.socket_filename);
Milan Lenco0a47c992017-10-12 14:19:31 +02002000 if (l0 + l1 < buflen)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002001 {
2002 md->socket_filename =
Milan Lenco0a47c992017-10-12 14:19:31 +02002003 strcpy (buf + l0, (char *) c->args.socket_filename);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002004 l0 += l1 + 1;
2005 }
2006 else
2007 err = MEMIF_ERR_NOBUF_DET;
2008
2009 md->rx_queues_num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02002010 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
2011 num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002012
2013 l1 = sizeof (memif_queue_details_t) * md->rx_queues_num;
2014 if (l0 + l1 <= buflen)
2015 {
2016 md->rx_queues = (memif_queue_details_t *) buf + l0;
Milan Lenco0a47c992017-10-12 14:19:31 +02002017 l0 += l1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002018 }
2019 else
2020 err = MEMIF_ERR_NOBUF_DET;
2021
2022 for (i = 0; i < md->rx_queues_num; i++)
2023 {
2024 md->rx_queues[i].qid = i;
2025 md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size);
2026 md->rx_queues[i].buffer_size = c->run_args.buffer_size;
2027 }
2028
2029 md->tx_queues_num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02002030 (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
2031 num_s2m_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002032
2033 l1 = sizeof (memif_queue_details_t) * md->tx_queues_num;
2034 if (l0 + l1 <= buflen)
2035 {
2036 md->tx_queues = (memif_queue_details_t *) buf + l0;
Milan Lenco0a47c992017-10-12 14:19:31 +02002037 l0 += l1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002038 }
2039 else
2040 err = MEMIF_ERR_NOBUF_DET;
2041
2042 for (i = 0; i < md->tx_queues_num; i++)
2043 {
2044 md->tx_queues[i].qid = i;
2045 md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size);
2046 md->tx_queues[i].buffer_size = c->run_args.buffer_size;
2047 }
2048
2049 md->link_up_down = (c->fd > 0) ? 1 : 0;
2050
2051 return err; /* 0 */
2052}
2053
2054int
2055memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd)
2056{
2057 memif_connection_t *c = (memif_connection_t *) conn;
2058 *efd = -1;
2059 if (c == NULL)
2060 return MEMIF_ERR_NOCONN;
2061 if (c->fd < 0)
2062 return MEMIF_ERR_DISCONNECTED;
2063 uint8_t num =
Jakub Grajciarba3c4e82017-09-18 11:21:40 +02002064 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
2065 num_m2s_rings;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002066 if (qid >= num)
2067 return MEMIF_ERR_QID;
2068
2069 *efd = c->rx_queues[qid].int_fd;
2070
2071 return MEMIF_ERR_SUCCESS;
2072}
2073
2074int
2075memif_cleanup ()
2076{
2077 libmemif_main_t *lm = &libmemif_main;
2078 if (lm->app_name)
2079 free (lm->app_name);
2080 lm->app_name = NULL;
2081 if (lm->control_list)
2082 free (lm->control_list);
2083 lm->control_list = NULL;
2084 if (lm->interrupt_list)
2085 free (lm->interrupt_list);
2086 lm->interrupt_list = NULL;
2087 if (lm->listener_list)
2088 free (lm->listener_list);
2089 lm->listener_list = NULL;
2090 if (lm->pending_list)
2091 free (lm->pending_list);
2092 lm->pending_list = NULL;
Milan Lenco0a47c992017-10-12 14:19:31 +02002093 if (poll_cancel_fd != -1)
2094 close (poll_cancel_fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02002095
2096 return MEMIF_ERR_SUCCESS; /* 0 */
2097}