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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <sys/types.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
27 #include <sys/prctl.h>
33 #include <linux/icmp.h>
34 #include <arpa/inet.h>
36 #include <netinet/if_ether.h>
37 #include <net/if_arp.h>
38 #include <asm/byteorder.h>
43 #include <sys/eventfd.h>
44 #include <sys/timerfd.h>
45 #include <sys/epoll.h>
48 /* memif protocol msg, ring and descriptor definitions */
52 /* socket messaging functions */
54 /* private structs and functions */
55 #include <memif_private.h>
57 #define ERRLIST_LEN 36
58 #define MAX_ERRBUF_LEN 256
61 #define MEMIF_MEMORY_BARRIER() __builtin_ia32_sfence ()
63 #define MEMIF_MEORY_BARRIER() __sync_synchronize ()
64 #endif /* __x86_x64__ */
66 libmemif_main_t libmemif_main;
69 static char memif_buf[MAX_ERRBUF_LEN];
71 const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */
73 /* MEMIF_ERR_SYSCALL */
74 "Unspecified syscall error (build with -DMEMIF_DBG or make debug).",
76 "Permission to resoure denied.",
77 /* MEMIF_ERR_NO_FILE */
78 "Socket file does not exist",
79 /* MEMIF_ERR_FILE_LIMIT */
80 "System limit on total numer of open files reached.",
81 /* MEMIF_ERR_PROC_FILE_LIMIT */
82 "Per-process limit on total number of open files reached.",
83 /* MEMIF_ERR_ALREADY */
84 "Connection already requested.",
86 "File descriptor refers to file other than socket, or operation would block.",
87 /* MEMIF_ERR_BAD_FD */
88 "Bad file descriptor.",
91 /* MEMIF_ERR_INVAL_ARG */
93 /* MEMIF_ERR_NOCONN */
94 "Memif connection handle does not point to existing conenction",
96 "Memif connection handle points to existing connection",
97 /* MEMIF_ERR_CB_FDUPDATE */
98 "Callback memif_control_fd_update_t returned error",
99 /* MEMIF_ERR_FILE_NOT_SOCK */
100 "File specified by socket filename exists and is not socket.",
101 /* MEMIF_ERR_NO_SHMFD */
102 "Missing shared memory file descriptor. (internal error)",
103 /* MEMIF_ERR_COOKIE */
104 "Invalid cookie on ring. (internal error)",
105 /* MEMIF_ERR_NOBUF_RING */
107 /* MEMIF_ERR_NOBUF */
108 "Not enough memif buffers. There are unreceived data in shared memory.",
109 /* MEMIF_ERR_NOBUF_DET */
110 "Not enough space for memif details in suplied buffer. String data might be malformed.",
111 /* MEMIF_ERR_INT_WRITE */
112 "Send interrupt error.",
113 /* MEMIF_ERR_MFMSG */
114 "Malformed message received on control channel.",
117 /* MEMIF_ERR_PROTO */
118 "Incompatible memory interface protocol version.",
120 "Unmatched interface id.",
121 /* MEMIF_ERR_ACCSLAVE */
122 "Slave cannot accept connection reqest.",
123 /* MEMIF_ERR_ALRCONN */
124 "Interface is already connected.",
127 /* MEMIF_ERR_SECRET */
129 /* MEMIF_ERR_NOSECRET */
131 /* MEMIF_ERR_MAXREG */
132 "Limit on total number of regions reached.",
133 /* MEMIF_ERR_MAXRING */
134 "Limit on total number of ring reached.",
135 /* MEMIF_ERR_NO_INTFD */
136 "Missing interrupt file descriptor. (internal error)",
137 /* MEMIF_ERR_DISCONNECT */
138 "Interface received disconnect request.",
139 /* MEMIF_ERR_DISCONNECTED */
140 "Interface is disconnected.",
141 /* MEMIF_ERR_UNKNOWN_MSG */
142 "Unknown message type received on control channel. (internal error)"
145 #define MEMIF_ERR_UNDEFINED "undefined error"
148 memif_strerror (int err_code)
150 if (err_code >= ERRLIST_LEN)
152 strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED));
153 memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0';
157 strncpy (memif_buf, memif_errlist[err_code],
158 strlen (memif_errlist[err_code]));
159 memif_buf[strlen (memif_errlist[err_code])] = '\0';
164 #define DBG_TX_BUF (0)
165 #define DBG_RX_BUF (1)
169 print_bytes (void *data, uint16_t len, uint8_t q)
172 printf ("\nTX:\n\t");
174 printf ("\nRX:\n\t");
176 for (i = 0; i < len; i++)
179 printf ("\n%d:\t", i);
180 printf ("%02X ", ((uint8_t *) (data))[i]);
184 #endif /* MEMIF_DBG_SHM */
187 memif_syscall_error_handler (int err_code)
189 DBG_UNIX ("%s", strerror (err_code));
192 return MEMIF_ERR_SUCCESS;
193 if (err_code == EACCES)
194 return MEMIF_ERR_ACCES;
195 if (err_code == ENFILE)
196 return MEMIF_ERR_FILE_LIMIT;
197 if (err_code == EMFILE)
198 return MEMIF_ERR_PROC_FILE_LIMIT;
199 if (err_code == ENOMEM)
200 return MEMIF_ERR_NOMEM;
201 /* connection refused if master dows not exist
202 this error would spam the user until master was created */
203 if (err_code == ECONNREFUSED)
204 return MEMIF_ERR_SUCCESS;
205 if (err_code == EALREADY)
206 return MEMIF_ERR_ALREADY;
207 if (err_code == EAGAIN)
208 return MEMIF_ERR_AGAIN;
209 if (err_code == EBADF)
210 return MEMIF_ERR_BAD_FD;
211 if (err_code == ENOENT)
212 return MEMIF_ERR_NO_FILE;
214 /* other syscall errors */
215 return MEMIF_ERR_SYSCALL;
219 memif_add_epoll_fd (int fd, uint32_t events)
223 DBG ("invalid fd %d", fd);
226 struct epoll_event evt;
227 memset (&evt, 0, sizeof (evt));
230 if (epoll_ctl (memif_epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
232 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
235 DBG ("fd %d added to epoll", fd);
240 memif_mod_epoll_fd (int fd, uint32_t events)
244 DBG ("invalid fd %d", fd);
247 struct epoll_event evt;
248 memset (&evt, 0, sizeof (evt));
251 if (epoll_ctl (memif_epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
253 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
256 DBG ("fd %d moddified on epoll", fd);
261 memif_del_epoll_fd (int fd)
265 DBG ("invalid fd %d", fd);
268 struct epoll_event evt;
269 memset (&evt, 0, sizeof (evt));
270 if (epoll_ctl (memif_epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
272 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
275 DBG ("fd %d removed from epoll", fd);
280 memif_control_fd_update (int fd, uint8_t events)
282 if (events & MEMIF_FD_EVENT_DEL)
283 return memif_del_epoll_fd (fd);
286 if (events & MEMIF_FD_EVENT_READ)
288 if (events & MEMIF_FD_EVENT_WRITE)
291 if (events & MEMIF_FD_EVENT_MOD)
292 return memif_mod_epoll_fd (fd, evt);
294 return memif_add_epoll_fd (fd, evt);
298 add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list, uint16_t * len)
300 libmemif_main_t *lm = &libmemif_main;
303 for (i = 0; i < *len; i++)
305 if ((*list)[i].data_struct == NULL)
307 (*list)[i].key = e->key;
308 (*list)[i].data_struct = e->data_struct;
312 memif_list_elt_t *tmp;
313 tmp = realloc (*list, sizeof (memif_list_elt_t) * *len * 2);
317 for (i = *len; i < *len * 2; i++)
320 tmp[i].data_struct = NULL;
323 tmp[*len].key = e->key;
324 tmp[*len].data_struct = e->data_struct;
333 get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list, uint16_t len,
342 for (i = 0; i < len; i++)
344 if (list[i].key == key)
354 /* does not free memory, only marks element as free */
356 free_list_elt (memif_list_elt_t * list, uint16_t len, int key)
359 for (i = 0; i < len; i++)
361 if (list[i].key == key)
364 list[i].data_struct = NULL;
373 free_list_elt_ctx (memif_list_elt_t * list, uint16_t len,
374 memif_connection_t * ctx)
377 for (i = 0; i < len; i++)
379 if (list[i].key == -1)
381 if (list[i].data_struct == ctx)
383 list[i].data_struct = NULL;
393 memif_control_fd_update_register (memif_control_fd_update_t * cb)
395 libmemif_main_t *lm = &libmemif_main;
396 lm->control_fd_update = cb;
400 memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name)
402 int err = MEMIF_ERR_SUCCESS; /* 0 */
403 libmemif_main_t *lm = &libmemif_main;
407 lm->app_name = malloc (strlen (app_name) + sizeof (char));
408 memset (lm->app_name, 0, strlen (app_name) + sizeof (char));
409 strncpy ((char *) lm->app_name, app_name, strlen (app_name));
413 lm->app_name = malloc (strlen (MEMIF_DEFAULT_APP_NAME) + sizeof (char));
414 memset (lm->app_name, 0, strlen (app_name) + sizeof (char));
415 strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME,
416 strlen (MEMIF_DEFAULT_APP_NAME));
419 /* register control fd update callback */
420 if (on_control_fd_update != NULL)
421 memif_control_fd_update_register (on_control_fd_update);
424 memif_epfd = epoll_create (1);
425 memif_control_fd_update_register (memif_control_fd_update);
426 DBG ("libmemif event polling initialized");
429 memset (&lm->ms, 0, sizeof (memif_socket_t));
431 lm->control_list_len = 2;
432 lm->interrupt_list_len = 2;
433 lm->listener_list_len = 1;
434 lm->pending_list_len = 1;
437 malloc (sizeof (memif_list_elt_t) * lm->control_list_len);
439 malloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len);
441 malloc (sizeof (memif_list_elt_t) * lm->listener_list_len);
443 malloc (sizeof (memif_list_elt_t) * lm->pending_list_len);
446 for (i = 0; i < lm->control_list_len; i++)
448 lm->control_list[i].key = -1;
449 lm->control_list[i].data_struct = NULL;
451 for (i = 0; i < lm->interrupt_list_len; i++)
453 lm->interrupt_list[i].key = -1;
454 lm->interrupt_list[i].data_struct = NULL;
456 for (i = 0; i < lm->listener_list_len; i++)
458 lm->listener_list[i].key = -1;
459 lm->listener_list[i].data_struct = NULL;
461 for (i = 0; i < lm->pending_list_len; i++)
463 lm->pending_list[i].key = -1;
464 lm->pending_list[i].data_struct = NULL;
467 lm->disconn_slaves = 0;
469 lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK);
473 DBG ("timerfd: %s", strerror (err));
474 return memif_syscall_error_handler (err);
477 lm->arm.it_value.tv_sec = 2;
478 lm->arm.it_value.tv_nsec = 0;
479 lm->arm.it_interval.tv_sec = 2;
480 lm->arm.it_interval.tv_nsec = 0;
481 memset (&lm->disarm, 0, sizeof (lm->disarm));
483 if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ) < 0)
485 DBG ("callback type memif_control_fd_update_t error!");
486 return MEMIF_ERR_CB_FDUPDATE;
492 static inline memif_ring_t *
493 memif_get_ring (memif_connection_t * conn, memif_ring_type_t type,
496 if (&conn->regions[0] == NULL)
498 void *p = conn->regions[0].shm;
500 sizeof (memif_ring_t) +
501 sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size);
502 p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size;
504 return (memif_ring_t *) p;
508 memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode,
511 memif_connection_t *conn = (memif_connection_t *) c;
513 return MEMIF_ERR_NOCONN;
515 (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->run_args.
518 return MEMIF_ERR_QID;
520 conn->rx_queues[qid].ring->flags = rx_mode;
521 DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags);
522 return MEMIF_ERR_SUCCESS;
526 memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
527 memif_connection_update_t * on_connect,
528 memif_connection_update_t * on_disconnect,
529 memif_interrupt_t * on_interrupt, void *private_ctx)
531 int err, i, index, sockfd = -1;
532 memif_list_elt_t list_elt;
533 memif_connection_t *conn = (memif_connection_t *) * c;
536 DBG ("This handle already points to existing memif.");
537 return MEMIF_ERR_CONN;
539 conn = (memif_connection_t *) malloc (sizeof (memif_connection_t));
542 err = memif_syscall_error_handler (errno);
545 memset (conn, 0, sizeof (memif_connection_t));
547 libmemif_main_t *lm = &libmemif_main;
549 conn->args.interface_id = args->interface_id;
551 if (args->log2_ring_size == 0)
552 args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE;
553 if (args->buffer_size == 0)
554 args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
555 if (args->num_s2m_rings == 0)
556 args->num_s2m_rings = MEMIF_DEFAULT_TX_QUEUES;
557 if (args->num_m2s_rings == 0)
558 args->num_m2s_rings = MEMIF_DEFAULT_RX_QUEUES;
560 conn->args.num_s2m_rings = args->num_s2m_rings;
561 conn->args.num_m2s_rings = args->num_m2s_rings;
562 conn->args.buffer_size = args->buffer_size;
563 conn->args.log2_ring_size = args->log2_ring_size;
564 conn->args.is_master = args->is_master;
565 conn->args.mode = args->mode;
566 conn->msg_queue = NULL;
567 conn->regions = NULL;
568 conn->tx_queues = NULL;
569 conn->rx_queues = NULL;
571 conn->on_connect = on_connect;
572 conn->on_disconnect = on_disconnect;
573 conn->on_interrupt = on_interrupt;
574 conn->private_ctx = private_ctx;
575 memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t));
577 uint8_t l = strlen ((char *) args->interface_name);
578 strncpy ((char *) conn->args.interface_name, (char *) args->interface_name,
581 l = strlen ((char *) args->instance_name);
582 strncpy ((char *) conn->args.instance_name, (char *) args->instance_name,
585 /* allocate and initialize socket_filename so it can be copyed to sun_path
586 without memory leaks */
587 conn->args.socket_filename = malloc (sizeof (char *) * 108);
588 memset (conn->args.socket_filename, 0, 108 * sizeof (char *));
590 if (args->socket_filename)
592 if (conn->args.socket_filename == NULL)
594 err = memif_syscall_error_handler (errno);
597 strncpy ((char *) conn->args.socket_filename,
598 (char *) args->socket_filename,
599 strlen ((char *) args->socket_filename));
603 uint16_t sdl = strlen (MEMIF_DEFAULT_SOCKET_DIR);
604 uint16_t sfl = strlen (MEMIF_DEFAULT_SOCKET_FILENAME);
605 if (conn->args.socket_filename == NULL)
607 err = memif_syscall_error_handler (errno);
610 strncpy ((char *) conn->args.socket_filename,
611 MEMIF_DEFAULT_SOCKET_DIR, sdl);
612 conn->args.socket_filename[sdl] = '/';
613 strncpy ((char *) (conn->args.socket_filename + 1 + sdl),
614 MEMIF_DEFAULT_SOCKET_FILENAME, sfl);
619 l = strlen ((char *) args->secret);
620 strncpy ((char *) conn->args.secret, (char *) args->secret, l);
623 if (conn->args.is_master)
625 conn->run_args.buffer_size = conn->args.buffer_size;
627 memif_list_elt_t elt;
628 for (i = 0; i < lm->listener_list_len; i++)
631 (memif_socket_t *) lm->listener_list[i].data_struct) != NULL)
634 ((char *) ms->filename, (char *) conn->args.socket_filename,
635 strlen ((char *) ms->filename)) == 0)
637 /* add interface to listener socket */
638 elt.key = conn->args.interface_id;
639 *c = elt.data_struct = conn;
640 add_list_elt (&elt, &ms->interface_list,
641 &ms->interface_list_len);
643 conn->listener_fd = ms->fd;
649 struct stat file_stat;
650 if (stat ((char *) conn->args.socket_filename, &file_stat) == 0)
652 if (S_ISSOCK (file_stat.st_mode))
653 unlink ((char *) conn->args.socket_filename);
655 return memif_syscall_error_handler (errno);
657 DBG ("creating socket file");
658 ms = malloc (sizeof (memif_socket_t));
660 malloc (strlen ((char *) conn->args.socket_filename) +
662 memset (ms->filename, 0,
663 strlen ((char *) conn->args.socket_filename) +
665 strncpy ((char *) ms->filename,
666 (char *) conn->args.socket_filename,
667 strlen ((char *) conn->args.socket_filename));
668 ms->interface_list_len = 1;
670 malloc (sizeof (memif_list_elt_t) * ms->interface_list_len);
671 ms->interface_list[0].key = -1;
672 ms->interface_list[0].data_struct = NULL;
673 struct sockaddr_un un = { 0 };
676 ms->fd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
679 err = memif_syscall_error_handler (errno);
682 DBG ("socket %d created", ms->fd);
683 un.sun_family = AF_UNIX;
684 strncpy ((char *) un.sun_path, (char *) ms->filename,
685 sizeof (un.sun_path) - 1);
688 (ms->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0)
690 err = memif_syscall_error_handler (errno);
694 if (bind (ms->fd, (struct sockaddr *) &un, sizeof (un)) < 0)
696 err = memif_syscall_error_handler (errno);
700 if (listen (ms->fd, 1) < 0)
702 err = memif_syscall_error_handler (errno);
706 if (stat ((char *) ms->filename, &file_stat) < 0)
708 err = memif_syscall_error_handler (errno);
712 /* add interface to listener socket */
713 elt.key = conn->args.interface_id;
714 *c = elt.data_struct = conn;
715 add_list_elt (&elt, &ms->interface_list,
716 &ms->interface_list_len);
718 conn->listener_fd = ms->fd;
720 /* add listener socket to libmemif main */
722 elt.data_struct = ms;
723 add_list_elt (&elt, &lm->listener_list, &lm->listener_list_len);
724 lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ);
731 if (lm->disconn_slaves == 0)
733 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
735 err = memif_syscall_error_handler (errno);
740 lm->disconn_slaves++;
743 *c = list_elt.data_struct = conn;
745 add_list_elt (&list_elt, &lm->control_list,
746 &lm->control_list_len)) < 0)
748 err = MEMIF_ERR_NOMEM;
761 if (conn->args.socket_filename)
762 free (conn->args.socket_filename);
770 memif_control_fd_handler (int fd, uint8_t events)
772 int i, rv, sockfd = -1, err = MEMIF_ERR_SUCCESS; /* 0 */
774 memif_list_elt_t *e = NULL;
775 memif_connection_t *conn;
776 libmemif_main_t *lm = &libmemif_main;
777 if (fd == lm->timerfd)
781 size = read (fd, &b, sizeof (b));
782 for (i = 0; i < lm->control_list_len; i++)
784 if ((lm->control_list[i].key < 0)
785 && (lm->control_list[i].data_struct != NULL))
787 conn = lm->control_list[i].data_struct;
788 if (conn->args.is_master)
791 struct sockaddr_un sun;
792 sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
795 err = memif_syscall_error_handler (errno);
799 sun.sun_family = AF_UNIX;
801 strncpy (sun.sun_path, conn->args.socket_filename,
802 sizeof (sun.sun_path) - 1);
804 if (connect (sockfd, (struct sockaddr *) &sun,
805 sizeof (struct sockaddr_un)) == 0)
808 conn->read_fn = memif_conn_fd_read_ready;
809 conn->write_fn = memif_conn_fd_write_ready;
810 conn->error_fn = memif_conn_fd_error;
812 lm->control_list[conn->index].key = conn->fd;
814 lm->control_fd_update (sockfd,
815 MEMIF_FD_EVENT_READ |
816 MEMIF_FD_EVENT_WRITE);
818 lm->disconn_slaves--;
819 if (lm->disconn_slaves == 0)
821 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL)
824 err = memif_syscall_error_handler (errno);
831 err = memif_syscall_error_handler (errno);
839 get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd);
842 if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL)
845 (((memif_connection_t *) e->data_struct)->args.
846 is_master) ? ((memif_connection_t *) e->data_struct)->
847 run_args.num_s2m_rings : ((memif_connection_t *) e->
848 data_struct)->run_args.
850 for (i = 0; i < num; i++)
852 if (((memif_connection_t *) e->data_struct)->rx_queues[i].
855 ((memif_connection_t *) e->
856 data_struct)->on_interrupt ((void *) e->data_struct,
857 ((memif_connection_t *)
860 return MEMIF_ERR_SUCCESS;
864 return MEMIF_ERR_SUCCESS;
866 get_list_elt (&e, lm->listener_list, lm->listener_list_len, fd);
869 memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct);
870 return MEMIF_ERR_SUCCESS;
873 get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd);
876 memif_read_ready (fd);
877 return MEMIF_ERR_SUCCESS;
880 get_list_elt (&e, lm->control_list, lm->control_list_len, fd);
883 if (events & MEMIF_FD_EVENT_READ)
886 ((memif_connection_t *) e->data_struct)->read_fn (e->
888 if (err != MEMIF_ERR_SUCCESS)
891 if (events & MEMIF_FD_EVENT_WRITE)
894 ((memif_connection_t *) e->data_struct)->write_fn (e->
896 if (err != MEMIF_ERR_SUCCESS)
899 if (events & MEMIF_FD_EVENT_ERROR)
902 ((memif_connection_t *) e->data_struct)->error_fn (e->
904 if (err != MEMIF_ERR_SUCCESS)
910 return MEMIF_ERR_SUCCESS; /* 0 */
920 memif_poll_event (int timeout)
922 libmemif_main_t *lm = &libmemif_main;
923 memif_list_elt_t *elt;
924 struct epoll_event evt, *e;
925 int en = 0, err = MEMIF_ERR_SUCCESS, i = 0; /* 0 */
928 memset (&evt, 0, sizeof (evt));
929 evt.events = EPOLLIN | EPOLLOUT;
931 sigemptyset (&sigset);
932 en = epoll_pwait (memif_epfd, &evt, 1, timeout, &sigset);
935 DBG ("epoll_pwait: %s", strerror (errno));
940 if (evt.events & EPOLLIN)
941 events |= MEMIF_FD_EVENT_READ;
942 if (evt.events & EPOLLOUT)
943 events |= MEMIF_FD_EVENT_WRITE;
944 if (evt.events & EPOLLERR)
945 events |= MEMIF_FD_EVENT_ERROR;
946 err = memif_control_fd_handler (evt.data.fd, events);
953 memif_msg_queue_free (memif_msg_queue_elt_t ** e)
957 memif_msg_queue_free (&(*e)->next);
963 /* send disconnect msg and close interface */
965 memif_disconnect_internal (memif_connection_t * c)
969 DBG ("no connection");
970 return MEMIF_ERR_NOCONN;
973 int err = MEMIF_ERR_SUCCESS, i; /* 0 */
975 libmemif_main_t *lm = &libmemif_main;
978 c->on_disconnect ((void *) c, c->private_ctx);
982 memif_msg_send_disconnect (c->fd, "interface deleted", 0);
983 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL);
986 get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd);
989 if (c->args.is_master)
990 free_list_elt (lm->control_list, lm->control_list_len, c->fd);
994 if (c->tx_queues != NULL)
997 (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
999 for (i = 0; i < num; i++)
1001 mq = &c->tx_queues[i];
1006 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1011 free (c->tx_queues);
1012 c->tx_queues = NULL;
1015 if (c->rx_queues != NULL)
1018 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
1020 for (i = 0; i < num; i++)
1022 mq = &c->rx_queues[i];
1027 if (c->on_interrupt != NULL)
1028 lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL);
1031 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1036 free (c->rx_queues);
1037 c->rx_queues = NULL;
1040 if (c->regions != NULL)
1042 if (munmap (c->regions[0].shm, c->regions[0].region_size) < 0)
1043 return memif_syscall_error_handler (errno);
1044 if (c->regions[0].fd > 0)
1045 close (c->regions[0].fd);
1046 c->regions[0].fd = -1;
1051 memset (&c->run_args, 0, sizeof (memif_conn_run_args_t));
1053 memif_msg_queue_free (&c->msg_queue);
1055 if (!(c->args.is_master))
1057 if (lm->disconn_slaves == 0)
1059 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
1061 err = memif_syscall_error_handler (errno);
1062 DBG_UNIX ("timerfd_settime: arm");
1065 lm->disconn_slaves++;
1072 memif_delete (memif_conn_handle_t * conn)
1074 memif_connection_t *c = (memif_connection_t *) * conn;
1077 DBG ("no connection");
1078 return MEMIF_ERR_NOCONN;
1080 libmemif_main_t *lm = &libmemif_main;
1081 memif_list_elt_t *e = NULL;
1082 memif_socket_t *ms = NULL;
1084 int err = MEMIF_ERR_SUCCESS;
1088 DBG ("DISCONNECTING");
1089 err = memif_disconnect_internal (c);
1090 if (err == MEMIF_ERR_NOCONN)
1094 free_list_elt_ctx (lm->control_list, lm->control_list_len, c);
1096 if (c->args.is_master)
1098 get_list_elt (&e, lm->listener_list, lm->listener_list_len,
1102 ms = (memif_socket_t *) e->data_struct;
1104 free_list_elt (ms->interface_list, ms->interface_list_len,
1105 c->args.interface_id);
1106 if (ms->use_count <= 0)
1108 lm->control_fd_update (c->listener_fd, MEMIF_FD_EVENT_DEL);
1109 free_list_elt (lm->listener_list, lm->listener_list_len,
1111 close (c->listener_fd);
1112 c->listener_fd = ms->fd = -1;
1113 free (ms->interface_list);
1114 ms->interface_list = NULL;
1115 free (ms->filename);
1116 ms->filename = NULL;
1124 lm->disconn_slaves--;
1125 if (lm->disconn_slaves <= 0)
1127 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0)
1129 err = memif_syscall_error_handler (errno);
1130 DBG ("timerfd_settime: disarm");
1135 if (c->args.socket_filename)
1136 free (c->args.socket_filename);
1137 c->args.socket_filename = NULL;
1147 memif_connect1 (memif_connection_t * c)
1149 libmemif_main_t *lm = &libmemif_main;
1150 memif_region_t *mr = c->regions;
1160 return MEMIF_ERR_NO_SHMFD;
1162 if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE,
1163 MAP_SHARED, mr->fd, 0)) == MAP_FAILED)
1165 return memif_syscall_error_handler (errno);
1171 (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
1173 for (i = 0; i < num; i++)
1175 mq = &c->tx_queues[i];
1178 mq->ring = c->regions[mq->region].shm + mq->offset;
1179 if (mq->ring->cookie != MEMIF_COOKIE)
1181 DBG ("wrong cookie on tx ring %u", i);
1182 return MEMIF_ERR_COOKIE;
1184 mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
1189 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
1191 for (i = 0; i < num; i++)
1193 mq = &c->rx_queues[i];
1196 mq->ring = c->regions[mq->region].shm + mq->offset;
1197 if (mq->ring->cookie != MEMIF_COOKIE)
1199 DBG ("wrong cookie on rx ring %u", i);
1200 return MEMIF_ERR_COOKIE;
1202 mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
1207 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD);
1213 memif_init_regions_and_queues (memif_connection_t * conn)
1215 memif_ring_t *ring = NULL;
1216 uint64_t buffer_offset;
1219 libmemif_main_t *lm = &libmemif_main;
1222 conn->regions = (memif_region_t *) malloc (sizeof (memif_region_t));
1223 if (conn->regions == NULL)
1224 return memif_syscall_error_handler (errno);
1228 (conn->run_args.num_s2m_rings +
1229 conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) +
1230 sizeof (memif_desc_t) *
1231 (1 << conn->run_args.log2_ring_size));
1233 r->region_size = buffer_offset +
1234 conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
1235 (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
1237 if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1)
1238 return memif_syscall_error_handler (errno);
1240 if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
1241 return memif_syscall_error_handler (errno);
1243 if ((ftruncate (r->fd, r->region_size)) == -1)
1244 return memif_syscall_error_handler (errno);
1246 if ((r->shm = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE,
1247 MAP_SHARED, r->fd, 0)) == MAP_FAILED)
1248 return memif_syscall_error_handler (errno);
1250 for (i = 0; i < conn->run_args.num_s2m_rings; i++)
1252 ring = memif_get_ring (conn, MEMIF_RING_S2M, i);
1253 DBG ("RING: %p I: %d", ring, i);
1254 ring->head = ring->tail = 0;
1255 ring->cookie = MEMIF_COOKIE;
1257 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1259 uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j;
1260 ring->desc[j].region = 0;
1261 ring->desc[j].offset = buffer_offset +
1262 (uint32_t) (slot * conn->run_args.buffer_size);
1263 ring->desc[j].buffer_length = conn->run_args.buffer_size;
1266 for (i = 0; i < conn->run_args.num_m2s_rings; i++)
1268 ring = memif_get_ring (conn, MEMIF_RING_M2S, i);
1269 DBG ("RING: %p I: %d", ring, i);
1270 ring->head = ring->tail = 0;
1271 ring->cookie = MEMIF_COOKIE;
1273 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1277 conn->run_args.num_s2m_rings) *
1278 (1 << conn->run_args.log2_ring_size) + j;
1279 ring->desc[j].region = 0;
1280 ring->desc[j].offset = buffer_offset +
1281 (uint32_t) (slot * conn->run_args.buffer_size);
1282 ring->desc[j].buffer_length = conn->run_args.buffer_size;
1287 (memif_queue_t *) malloc (sizeof (memif_queue_t) *
1288 conn->run_args.num_s2m_rings);
1290 return memif_syscall_error_handler (errno);
1292 for (x = 0; x < conn->run_args.num_s2m_rings; x++)
1294 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1295 return memif_syscall_error_handler (errno);
1296 /* add int fd to interrupt fd list */
1297 e.key = mq[x].int_fd;
1298 e.data_struct = conn;
1299 add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
1301 mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x);
1302 DBG ("RING: %p I: %d", mq[x].ring, x);
1303 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1306 (void *) mq[x].ring - (void *) conn->regions[mq->region].shm;
1307 mq[x].last_head = 0;
1308 mq[x].alloc_bufs = 0;
1310 conn->tx_queues = mq;
1313 (memif_queue_t *) malloc (sizeof (memif_queue_t) *
1314 conn->run_args.num_m2s_rings);
1316 return memif_syscall_error_handler (errno);
1317 for (x = 0; x < conn->run_args.num_m2s_rings; x++)
1319 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1320 return memif_syscall_error_handler (errno);
1321 /* add int fd to interrupt fd list */
1322 e.key = mq[x].int_fd;
1323 e.data_struct = conn;
1324 add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
1326 mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x);
1327 DBG ("RING: %p I: %d", mq[x].ring, x);
1328 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1331 (void *) mq[x].ring - (void *) conn->regions[mq->region].shm;
1332 mq[x].last_head = 0;
1333 mq[x].alloc_bufs = 0;
1335 conn->rx_queues = mq;
1341 memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
1342 memif_buffer_t * bufs, uint16_t count,
1343 uint16_t * count_out, uint16_t size)
1345 memif_connection_t *c = (memif_connection_t *) conn;
1347 return MEMIF_ERR_NOCONN;
1349 return MEMIF_ERR_DISCONNECTED;
1351 (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
1354 return MEMIF_ERR_QID;
1355 memif_queue_t *mq = &c->tx_queues[qid];
1356 memif_ring_t *ring = mq->ring;
1357 memif_buffer_t *b0, *b1;
1358 uint8_t chain_buf0, chain_buf1;
1359 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1360 uint16_t s0, s1, ns;
1362 int i, err = MEMIF_ERR_SUCCESS; /* 0 */
1364 if (ring->tail != ring->head)
1366 if (ring->head > ring->tail)
1367 ns = (1 << mq->log2_ring_size) - ring->head + ring->tail;
1369 ns = ring->tail - ring->head;
1372 ns = (1 << mq->log2_ring_size);
1374 /* (head == tail) ? receive function will asume that no packets are available */
1379 while ((count > 2) && (ns > 2))
1381 s0 = (ring->head + mq->alloc_bufs) & mask;
1382 chain_buf0 = size / ring->desc[s0].buffer_length;
1383 if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0))
1386 if (chain_buf0 > ns)
1389 s1 = (ring->head + mq->alloc_bufs + chain_buf0) & mask;
1390 chain_buf1 = size / ring->desc[s1].buffer_length;
1391 if (((size % ring->desc[s1].buffer_length) != 0) || (size == 0))
1394 if ((chain_buf0 + chain_buf1) > ns)
1397 b0 = (bufs + *count_out);
1398 b1 = (bufs + *count_out + 1);
1400 b0->desc_index = s0;
1401 b1->desc_index = s1;
1402 ring->desc[s0].flags = 0;
1403 ring->desc[s1].flags = 0;
1404 b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0;
1405 b1->buffer_len = ring->desc[s1].buffer_length * chain_buf1;
1406 /* TODO: support multiple regions -> ring descriptor contains region index */
1407 b0->data = c->regions->shm + ring->desc[s0].offset;
1408 b1->data = c->regions->shm + ring->desc[s1].offset;
1410 for (i = 0; i < (memif_min (chain_buf0, chain_buf1) - 1); i++)
1412 ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
1413 ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
1414 DBG ("allocating chained buffers");
1417 if (chain_buf0 > chain_buf1)
1419 for (; i < (chain_buf0 - 1); i++)
1420 ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
1424 for (; i < (chain_buf1 - 1); i++)
1425 ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
1428 mq->alloc_bufs += chain_buf0 + chain_buf1;
1430 DBG ("allocated ring slots %u, %u", s0, s1);
1432 ns -= chain_buf0 + chain_buf1;
1435 s0 = (ring->head + mq->alloc_bufs) & mask;
1437 b0 = (bufs + *count_out);
1439 chain_buf0 = size / ring->desc[s0].buffer_length;
1440 if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0))
1443 if (chain_buf0 > ns)
1446 b0->desc_index = s0;
1447 ring->desc[s0].flags = 0;
1448 b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0;
1449 b0->data = c->regions->shm + ring->desc[s0].offset;
1451 for (i = 0; i < (chain_buf0 - 1); i++)
1453 ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
1454 DBG ("allocating chained buffers");
1457 mq->alloc_bufs += chain_buf0;
1459 DBG ("allocated ring slot %u", s0);
1465 DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
1470 DBG ("ring buffer full! qid: %u", qid);
1471 err = MEMIF_ERR_NOBUF_RING;
1478 memif_buffer_free (memif_conn_handle_t conn, uint16_t qid,
1479 memif_buffer_t * bufs, uint16_t count,
1480 uint16_t * count_out)
1482 memif_connection_t *c = (memif_connection_t *) conn;
1484 return MEMIF_ERR_NOCONN;
1486 return MEMIF_ERR_DISCONNECTED;
1488 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
1491 return MEMIF_ERR_QID;
1492 libmemif_main_t *lm = &libmemif_main;
1493 memif_queue_t *mq = &c->rx_queues[qid];
1494 memif_ring_t *ring = mq->ring;
1495 uint16_t tail = ring->tail;
1496 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1497 uint8_t chain_buf0, chain_buf1;
1498 memif_buffer_t *b0, *b1;
1501 if (mq->alloc_bufs < count)
1502 count = mq->alloc_bufs;
1508 b0 = (bufs + *count_out);
1509 b1 = (bufs + *count_out + 1);
1511 b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
1512 if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) !=
1516 b1->buffer_len / ring->desc[b1->desc_index].buffer_length;
1517 if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) !=
1520 tail = (b1->desc_index + chain_buf1) & mask;
1526 mq->alloc_bufs -= chain_buf0 + chain_buf1;
1528 b0 = (bufs + *count_out);
1529 chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
1530 if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0)
1532 tail = (b0->desc_index + chain_buf0) & mask;
1537 mq->alloc_bufs -= chain_buf0;
1539 MEMIF_MEORY_BARRIER ();
1541 DBG ("tail: %u", ring->tail);
1543 return MEMIF_ERR_SUCCESS; /* 0 */
1547 memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
1548 memif_buffer_t * bufs, uint16_t count, uint16_t * tx)
1550 memif_connection_t *c = (memif_connection_t *) conn;
1552 return MEMIF_ERR_NOCONN;
1554 return MEMIF_ERR_DISCONNECTED;
1556 (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
1559 return MEMIF_ERR_QID;
1560 memif_queue_t *mq = &c->tx_queues[qid];
1561 memif_ring_t *ring = mq->ring;
1562 uint16_t head = ring->head;
1563 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1564 uint8_t chain_buf0, chain_buf1;
1566 uint16_t curr_buf = 0;
1567 memif_buffer_t *b0, *b1;
1574 b0 = (bufs + curr_buf);
1575 b1 = (bufs + curr_buf + 1);
1577 b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
1578 if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) !=
1583 b1->buffer_len / ring->desc[b1->desc_index].buffer_length;
1584 if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) !=
1588 for (i = 0; i < memif_min (chain_buf0, chain_buf1); i++)
1592 ring->desc[(b0->desc_index + i) & mask].buffer_length)
1595 ring->desc[(b0->desc_index + i) & mask].length =
1596 ring->desc[(b0->desc_index + i) & mask].buffer_length;
1600 ring->desc[(b0->desc_index + i) & mask].length =
1606 ring->desc[(b1->desc_index + i) & mask].buffer_length)
1609 ring->desc[(b1->desc_index + i) & mask].length =
1610 ring->desc[(b1->desc_index + i) & mask].buffer_length;
1614 ring->desc[(b1->desc_index + i) & mask].length =
1618 #ifdef MEMIF_DBG_SHM
1619 print_bytes (b0->data +
1620 ring->desc[(b0->desc_index +
1621 i) & mask].buffer_length *
1623 ring->desc[(b0->desc_index +
1624 i) & mask].buffer_length, DBG_TX_BUF);
1625 print_bytes (b1->data +
1626 ring->desc[(b1->desc_index +
1627 i) & mask].buffer_length *
1629 ring->desc[(b1->desc_index +
1630 i) & mask].buffer_length, DBG_TX_BUF);
1631 #endif /* MEMIF_DBG_SHM */
1634 if (chain_buf0 > chain_buf1)
1636 for (; i < chain_buf0; i++)
1639 ring->desc[(b0->desc_index + i) & mask].buffer_length)
1642 ring->desc[(b0->desc_index + i) & mask].length =
1643 ring->desc[(b0->desc_index + i) & mask].buffer_length;
1647 ring->desc[(b0->desc_index + i) & mask].length =
1651 #ifdef MEMIF_DBG_SHM
1652 print_bytes (b0->data +
1653 ring->desc[(b0->desc_index +
1654 i) & mask].buffer_length *
1656 ring->desc[(b0->desc_index +
1657 i) & mask].buffer_length,
1659 #endif /* MEMIF_DBG_SHM */
1664 for (; i < chain_buf1; i++)
1667 ring->desc[(b1->desc_index + i) & mask].buffer_length)
1670 ring->desc[(b1->desc_index + i) & mask].length =
1671 ring->desc[(b1->desc_index + i) & mask].buffer_length;
1675 ring->desc[(b1->desc_index + i) & mask].length =
1679 #ifdef MEMIF_DBG_SHM
1680 print_bytes (b1->data +
1681 ring->desc[(b1->desc_index +
1682 i) & mask].buffer_length *
1684 ring->desc[(b1->desc_index +
1685 i) & mask].buffer_length,
1687 #endif /* MEMIF_DBG_SHM */
1691 head = (b1->desc_index + chain_buf1) & mask;
1695 if (b0->data_len != 0)
1696 DBG ("invalid b0 data length!");
1697 #endif /* MEMIF_DBG */
1700 if (b1->data_len != 0)
1701 DBG ("invalid b1 data length!");
1702 #endif /* MEMIF_DBG */
1705 *tx += chain_buf0 + chain_buf1;
1709 b0 = (bufs + curr_buf);
1710 chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
1711 if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0)
1714 for (i = 0; i < chain_buf0; i++)
1717 ring->desc[(b0->desc_index + i) & mask].buffer_length)
1719 b0->data_len -= ring->desc[(b0->desc_index + i) & mask].length =
1720 ring->desc[(b0->desc_index + i) & mask].buffer_length;
1724 ring->desc[(b0->desc_index + i) & mask].length = b0->data_len;
1727 #ifdef MEMIF_DBG_SHM
1728 print_bytes (b0->data +
1729 ring->desc[(b0->desc_index + i) & mask].buffer_length *
1731 ring->desc[(b0->desc_index + i) & mask].buffer_length,
1733 #endif /* MEMIF_DBG_SHM */
1736 head = (b0->desc_index + chain_buf0) & mask;
1740 if (b0->data_len != 0)
1741 DBG ("invalid b0 data length!");
1742 #endif /* MEMIF_DBG */
1748 MEMIF_MEORY_BARRIER ();
1751 mq->alloc_bufs -= *tx;
1753 /* TODO: return num of buffers and packets */
1756 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
1759 int r = write (mq->int_fd, &a, sizeof (a));
1761 return MEMIF_ERR_INT_WRITE;
1764 return MEMIF_ERR_SUCCESS; /* 0 */
1768 memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
1769 memif_buffer_t * bufs, uint16_t count, uint16_t * rx)
1771 memif_connection_t *c = (memif_connection_t *) conn;
1773 return MEMIF_ERR_NOCONN;
1775 return MEMIF_ERR_DISCONNECTED;
1777 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
1780 return MEMIF_ERR_QID;
1781 memif_queue_t *mq = &c->rx_queues[qid];
1782 memif_ring_t *ring = mq->ring;
1783 uint16_t head = ring->head;
1785 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1786 memif_buffer_t *b0, *b1;
1787 uint16_t curr_buf = 0;
1792 ssize_t r = read (mq->int_fd, &b, sizeof (b));
1793 if ((r == -1) && (errno != EAGAIN))
1794 return memif_syscall_error_handler (errno);
1796 if (head == mq->last_head)
1799 if (head > mq->last_head)
1800 ns = head - mq->last_head;
1802 ns = (1 << mq->log2_ring_size) - mq->last_head + head;
1806 DBG ("ns: %u, count: %u", ns, count);
1807 while ((ns > 2) && (count > 2))
1809 b0 = (bufs + curr_buf);
1810 b1 = (bufs + curr_buf + 1);
1812 b0->desc_index = mq->last_head;
1817 b0->data = memif_get_buffer (conn, ring, mq->last_head);
1818 b0->data_len += ring->desc[mq->last_head].length;
1819 b0->buffer_len += ring->desc[mq->last_head].buffer_length;
1820 #ifdef MEMIF_DBG_SHM
1821 print_bytes (b0->data +
1822 ring->desc[b0->desc_index].buffer_length * i++,
1823 ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF);
1824 #endif /* MEMIF_DBG_SHM */
1827 while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT)
1829 ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT;
1830 mq->last_head = (mq->last_head + 1) & mask;
1831 b0->data_len += ring->desc[mq->last_head].length;
1832 b0->buffer_len += ring->desc[mq->last_head].buffer_length;
1833 #ifdef MEMIF_DBG_SHM
1834 print_bytes (b0->data +
1835 ring->desc[b0->desc_index].buffer_length * i++,
1836 ring->desc[b0->desc_index].buffer_length,
1838 #endif /* MEMIF_DBG_SHM */
1842 mq->last_head = (mq->last_head + 1) & mask;
1844 b1->desc_index = mq->last_head;
1849 b1->data = memif_get_buffer (conn, ring, mq->last_head);
1850 b1->data_len += ring->desc[mq->last_head].length;
1851 b1->buffer_len += ring->desc[mq->last_head].buffer_length;
1852 #ifdef MEMIF_DBG_SHM
1853 print_bytes (b1->data +
1854 ring->desc[b1->desc_index].buffer_length * i++,
1855 ring->desc[b1->desc_index].buffer_length, DBG_TX_BUF);
1856 #endif /* MEMIF_DBG_SHM */
1859 while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT)
1861 ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT;
1862 mq->last_head = (mq->last_head + 1) & mask;
1863 b1->data_len += ring->desc[mq->last_head].length;
1864 b1->buffer_len += ring->desc[mq->last_head].buffer_length;
1865 #ifdef MEMIF_DBG_SHM
1866 print_bytes (b1->data +
1867 ring->desc[b1->desc_index].buffer_length * i++,
1868 ring->desc[b1->desc_index].buffer_length,
1870 #endif /* MEMIF_DBG_SHM */
1874 mq->last_head = (mq->last_head + 1) & mask;
1879 b0 = (bufs + curr_buf);
1881 b0->desc_index = mq->last_head;
1886 b0->data = memif_get_buffer (conn, ring, mq->last_head);
1887 b0->data_len += ring->desc[mq->last_head].length;
1888 b0->buffer_len += ring->desc[mq->last_head].buffer_length;
1889 #ifdef MEMIF_DBG_SHM
1890 print_bytes (b0->data +
1891 ring->desc[b0->desc_index].buffer_length * i++,
1892 ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF);
1893 #endif /* MEMIF_DBG_SHM */
1897 while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT)
1899 ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT;
1900 mq->last_head = (mq->last_head + 1) & mask;
1901 b0->data_len += ring->desc[mq->last_head].length;
1902 b0->buffer_len += ring->desc[mq->last_head].buffer_length;
1903 #ifdef MEMIF_DBG_SHM
1904 print_bytes (b0->data +
1905 ring->desc[b0->desc_index].buffer_length * i++,
1906 ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF);
1907 #endif /* MEMIF_DBG_SHM */
1911 mq->last_head = (mq->last_head + 1) & mask;
1917 mq->alloc_bufs += *rx;
1919 /* TODO: return num of buffers and packets */
1924 DBG ("not enough buffers!");
1925 return MEMIF_ERR_NOBUF;
1928 return MEMIF_ERR_SUCCESS; /* 0 */
1932 memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
1933 char *buf, ssize_t buflen)
1935 memif_connection_t *c = (memif_connection_t *) conn;
1937 return MEMIF_ERR_NOCONN;
1939 int err = MEMIF_ERR_SUCCESS, i;
1940 ssize_t l0, l1, total_l;
1943 l1 = strlen ((char *) c->args.interface_name);
1944 if (l0 + l1 <= buflen)
1946 md->if_name = strncpy (buf + l0, (char *) c->args.interface_name, l1);
1947 md->if_name[l0 + l1] = '\0';
1951 err = MEMIF_ERR_NOBUF_DET;
1953 l1 = strlen ((char *) c->args.instance_name);
1954 if (l0 + l1 <= buflen)
1956 md->inst_name = strncpy (buf + l0, (char *) c->args.instance_name, l1);
1957 md->inst_name[l0 + l1] = '\0';
1961 err = MEMIF_ERR_NOBUF_DET;
1963 l1 = strlen ((char *) c->remote_if_name);
1964 if (l0 + l1 <= buflen)
1966 md->remote_if_name = strncpy (buf + l0, (char *) c->remote_if_name, l1);
1967 md->remote_if_name[l0 + l1] = '\0';
1971 err = MEMIF_ERR_NOBUF_DET;
1973 l1 = strlen ((char *) c->remote_name);
1974 if (l0 + l1 <= buflen)
1976 md->remote_inst_name = strncpy (buf + l0, (char *) c->remote_name, l1);
1977 md->remote_inst_name[l0 + l1] = '\0';
1981 err = MEMIF_ERR_NOBUF_DET;
1983 md->id = c->args.interface_id;
1987 l1 = strlen ((char *) c->args.secret);
1988 md->secret = strncpy (buf + l0, (char *) c->args.secret, l1);
1989 md->secret[l0 + l1] = '\0';
1993 err = MEMIF_ERR_NOBUF_DET;
1995 md->role = (c->args.is_master) ? 0 : 1;
1996 md->mode = c->args.mode;
1998 l1 = strlen ((char *) c->args.socket_filename);
1999 if (l0 + l1 <= buflen)
2001 md->socket_filename =
2002 strncpy (buf + l0, (char *) c->args.socket_filename, l1);
2003 md->socket_filename[l0 + l1] = '\0';
2007 err = MEMIF_ERR_NOBUF_DET;
2010 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
2013 l1 = sizeof (memif_queue_details_t) * md->rx_queues_num;
2014 if (l0 + l1 <= buflen)
2016 md->rx_queues = (memif_queue_details_t *) buf + l0;
2020 err = MEMIF_ERR_NOBUF_DET;
2022 for (i = 0; i < md->rx_queues_num; i++)
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;
2030 (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
2033 l1 = sizeof (memif_queue_details_t) * md->tx_queues_num;
2034 if (l0 + l1 <= buflen)
2036 md->tx_queues = (memif_queue_details_t *) buf + l0;
2040 err = MEMIF_ERR_NOBUF_DET;
2042 for (i = 0; i < md->tx_queues_num; i++)
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;
2049 md->link_up_down = (c->fd > 0) ? 1 : 0;
2055 memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd)
2057 memif_connection_t *c = (memif_connection_t *) conn;
2060 return MEMIF_ERR_NOCONN;
2062 return MEMIF_ERR_DISCONNECTED;
2064 (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
2067 return MEMIF_ERR_QID;
2069 *efd = c->rx_queues[qid].int_fd;
2071 return MEMIF_ERR_SUCCESS;
2077 libmemif_main_t *lm = &libmemif_main;
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;
2094 return MEMIF_ERR_SUCCESS; /* 0 */