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 *------------------------------------------------------------------
21 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
28 #include <sys/prctl.h>
34 #include <linux/icmp.h>
35 #include <arpa/inet.h>
37 #include <netinet/if_ether.h>
38 #include <net/if_arp.h>
39 #include <asm/byteorder.h>
44 #include <sys/eventfd.h>
45 #include <sys/timerfd.h>
46 #include <sys/epoll.h>
48 #include <linux/memfd.h>
50 /* memif protocol msg, ring and descriptor definitions */
54 /* socket messaging functions */
56 /* private structs and functions */
57 #include <memif_private.h>
59 #define ERRLIST_LEN 40
60 #define MAX_ERRBUF_LEN 256
63 #define MEMIF_MEMORY_BARRIER() __builtin_ia32_sfence ()
65 #define MEMIF_MEMORY_BARRIER() __sync_synchronize ()
66 #endif /* __x86_x64__ */
68 libmemif_main_t libmemif_main;
70 static char memif_buf[MAX_ERRBUF_LEN];
72 const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */
74 /* MEMIF_ERR_SYSCALL */
75 "Unspecified syscall error (build with -DMEMIF_DBG or make debug).",
76 /* MEMIF_ERR_CONNREFUSED */
79 "Permission to resource denied.",
80 /* MEMIF_ERR_NO_FILE */
81 "Socket file does not exist",
82 /* MEMIF_ERR_FILE_LIMIT */
83 "System limit on total numer of open files reached.",
84 /* MEMIF_ERR_PROC_FILE_LIMIT */
85 "Per-process limit on total number of open files reached.",
86 /* MEMIF_ERR_ALREADY */
87 "Connection already requested.",
89 "File descriptor refers to file other than socket, or operation would block.",
90 /* MEMIF_ERR_BAD_FD */
91 "Bad file descriptor.",
94 /* MEMIF_ERR_INVAL_ARG */
96 /* MEMIF_ERR_NOCONN */
97 "Memif connection handle does not point to existing connection",
99 "Memif connection handle points to existing connection",
100 /* MEMIF_ERR_CB_FDUPDATE */
101 "Callback memif_control_fd_update_t returned error",
102 /* MEMIF_ERR_FILE_NOT_SOCK */
103 "File specified by socket filename exists and is not socket.",
104 /* MEMIF_ERR_NO_SHMFD */
105 "Missing shared memory file descriptor. (internal error)",
106 /* MEMIF_ERR_COOKIE */
107 "Invalid cookie on ring. (internal error)",
108 /* MEMIF_ERR_NOBUF_RING */
110 /* MEMIF_ERR_NOBUF */
111 "Not enough memif buffers. There are unreceived data in shared memory.",
112 /* MEMIF_ERR_NOBUF_DET */
113 "Not enough space for memif details in supplied buffer. String data might be malformed.",
114 /* MEMIF_ERR_INT_WRITE */
115 "Send interrupt error.",
116 /* MEMIF_ERR_MFMSG */
117 "Malformed message received on control channel.",
120 /* MEMIF_ERR_PROTO */
121 "Incompatible memory interface protocol version.",
123 "Unmatched interface id.",
124 /* MEMIF_ERR_ACCSLAVE */
125 "Slave cannot accept connection request.",
126 /* MEMIF_ERR_ALRCONN */
127 "Interface is already connected.",
130 /* MEMIF_ERR_SECRET */
132 /* MEMIF_ERR_NOSECRET */
134 /* MEMIF_ERR_MAXREG */
135 "Limit on total number of regions reached.",
136 /* MEMIF_ERR_MAXRING */
137 "Limit on total number of ring reached.",
138 /* MEMIF_ERR_NO_INTFD */
139 "Missing interrupt file descriptor. (internal error)",
140 /* MEMIF_ERR_DISCONNECT */
141 "Interface received disconnect request.",
142 /* MEMIF_ERR_DISCONNECTED */
143 "Interface is disconnected.",
144 /* MEMIF_ERR_UNKNOWN_MSG */
145 "Unknown message type received on control channel. (internal error)",
146 /* MEMIF_ERR_POLL_CANCEL */
147 "Memif event polling was canceled.",
148 /* MEMIF_ERR_MAX_RING */
149 "Maximum log2 ring size is 15",
150 /* MEMIF_ERR_PRIVHDR */
151 "Private headers not supported."
154 #define MEMIF_ERR_UNDEFINED "undefined error"
157 memif_strerror (int err_code)
159 if (err_code >= ERRLIST_LEN)
161 strlcpy (memif_buf, MEMIF_ERR_UNDEFINED, sizeof (memif_buf));
165 strlcpy (memif_buf, memif_errlist[err_code], sizeof (memif_buf));
173 return MEMIF_VERSION;
176 #define DBG_TX_BUF (0)
177 #define DBG_RX_BUF (1)
181 print_bytes (void *data, uint16_t len, uint8_t q)
184 printf ("\nTX:\n\t");
186 printf ("\nRX:\n\t");
188 for (i = 0; i < len; i++)
191 printf ("\n%d:\t", i);
192 printf ("%02X ", ((uint8_t *) (data))[i]);
196 #endif /* MEMIF_DBG_SHM */
199 memif_syscall_error_handler (int err_code)
201 DBG ("%s", strerror (err_code));
204 return MEMIF_ERR_SUCCESS;
205 if (err_code == EACCES)
206 return MEMIF_ERR_ACCES;
207 if (err_code == ENFILE)
208 return MEMIF_ERR_FILE_LIMIT;
209 if (err_code == EMFILE)
210 return MEMIF_ERR_PROC_FILE_LIMIT;
211 if (err_code == ENOMEM)
212 return MEMIF_ERR_NOMEM;
213 /* connection refused if master does not exist
214 this error would spam the user until master was created */
216 if (err_code == ECONNREFUSED)
217 return MEMIF_ERR_SUCCESS;
219 if (err_code == ECONNREFUSED)
220 return MEMIF_ERR_CONNREFUSED;
221 if (err_code == EALREADY)
222 return MEMIF_ERR_ALREADY;
223 if (err_code == EAGAIN)
224 return MEMIF_ERR_AGAIN;
225 if (err_code == EBADF)
226 return MEMIF_ERR_BAD_FD;
227 if (err_code == ENOENT)
228 return MEMIF_ERR_NO_FILE;
230 /* other syscall errors */
231 return MEMIF_ERR_SYSCALL;
236 get_libmemif_main (memif_socket_t * ms)
238 if (ms != NULL && ms->lm != NULL)
240 return &libmemif_main;
244 memif_add_epoll_fd (libmemif_main_t * lm, int fd, uint32_t events)
248 DBG ("invalid fd %d", fd);
251 struct epoll_event evt;
252 memset (&evt, 0, sizeof (evt));
255 if (epoll_ctl (lm->epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
257 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
260 DBG ("fd %d added to epoll", fd);
265 memif_mod_epoll_fd (libmemif_main_t * lm, int fd, uint32_t events)
269 DBG ("invalid fd %d", fd);
272 struct epoll_event evt;
273 memset (&evt, 0, sizeof (evt));
276 if (epoll_ctl (lm->epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
278 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
281 DBG ("fd %d modified on epoll", fd);
286 memif_del_epoll_fd (libmemif_main_t * lm, int fd)
290 DBG ("invalid fd %d", fd);
293 struct epoll_event evt;
294 memset (&evt, 0, sizeof (evt));
295 if (epoll_ctl (lm->epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
297 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
300 DBG ("fd %d removed from epoll", fd);
305 memif_control_fd_update (int fd, uint8_t events, void *private_ctx)
309 lm = (private_ctx == NULL) ? &libmemif_main : (libmemif_main_t *) private_ctx;
311 if (events & MEMIF_FD_EVENT_DEL)
312 return memif_del_epoll_fd (lm, fd);
315 if (events & MEMIF_FD_EVENT_READ)
317 if (events & MEMIF_FD_EVENT_WRITE)
320 if (events & MEMIF_FD_EVENT_MOD)
321 return memif_mod_epoll_fd (lm, fd, evt);
323 return memif_add_epoll_fd (lm, fd, evt);
327 add_list_elt (libmemif_main_t * lm, memif_list_elt_t * e,
328 memif_list_elt_t ** list, uint16_t * len)
330 memif_list_elt_t *tmp;
333 for (i = 0; i < *len; i++)
335 if ((*list)[i].data_struct == NULL)
337 (*list)[i].key = e->key;
338 (*list)[i].data_struct = e->data_struct;
343 tmp = lm->realloc (*list, sizeof (memif_list_elt_t) * *len * 2);
347 for (i = *len; i < *len * 2; i++)
350 tmp[i].data_struct = NULL;
353 tmp[*len].key = e->key;
354 tmp[*len].data_struct = e->data_struct;
363 get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list, uint16_t len,
373 for (i = 0; i < len; i++)
375 if (list[i].key == key)
385 /* does not free memory, only marks element as free */
387 free_list_elt (memif_list_elt_t * list, uint16_t len, int key)
390 for (i = 0; i < len; i++)
392 if (list[i].key == key)
395 list[i].data_struct = NULL;
404 free_list_elt_ctx (memif_list_elt_t * list, uint16_t len,
405 memif_connection_t * ctx)
408 for (i = 0; i < len; i++)
410 if (list[i].key == -1)
412 if (list[i].data_struct == ctx)
414 list[i].data_struct = NULL;
424 memif_control_fd_update_register (libmemif_main_t * lm,
425 memif_control_fd_update_t * cb)
427 lm->control_fd_update = cb;
431 memif_register_external_region (memif_add_external_region_t * ar,
432 memif_get_external_region_addr_t * gr,
433 memif_del_external_region_t * dr,
434 memif_get_external_buffer_offset_t * go)
436 libmemif_main_t *lm = &libmemif_main;
437 lm->add_external_region = ar;
438 lm->get_external_region_addr = gr;
439 lm->del_external_region = dr;
440 lm->get_external_buffer_offset = go;
444 memif_alloc_register (libmemif_main_t * lm, memif_alloc_t * ma)
450 memif_realloc_register (libmemif_main_t * lm, memif_realloc_t * mr)
456 memif_free_register (libmemif_main_t * lm, memif_free_t * mf)
462 memif_set_connection_request_timer (struct itimerspec timer)
464 libmemif_main_t *lm = &libmemif_main;
465 int err = MEMIF_ERR_SUCCESS;
469 /* overwrite timer, if already armed */
470 if (lm->disconn_slaves != 0)
472 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
474 err = memif_syscall_error_handler (errno);
481 memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
482 memif_alloc_t * memif_alloc, memif_realloc_t * memif_realloc,
483 memif_free_t * memif_free)
485 int err = MEMIF_ERR_SUCCESS; /* 0 */
486 libmemif_main_t *lm = &libmemif_main;
487 memset (lm, 0, sizeof (libmemif_main_t));
489 /* register custom memory management */
490 if (memif_alloc != NULL)
492 memif_alloc_register (lm, memif_alloc);
495 memif_alloc_register (lm, malloc);
497 if (memif_realloc != NULL)
499 memif_realloc_register (lm, memif_realloc);
502 memif_realloc_register (lm, realloc);
504 if (memif_free != NULL)
505 memif_free_register (lm, memif_free);
507 memif_free_register (lm, free);
509 if (app_name != NULL)
511 strlcpy ((char *) lm->app_name, app_name, sizeof (lm->app_name));
515 strlcpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME,
516 sizeof (lm->app_name));
519 lm->poll_cancel_fd = -1;
520 /* register control fd update callback */
521 if (on_control_fd_update != NULL)
522 memif_control_fd_update_register (lm, on_control_fd_update);
525 lm->epfd = epoll_create (1);
526 memif_control_fd_update_register (lm, memif_control_fd_update);
527 if ((lm->poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0)
530 DBG ("eventfd: %s", strerror (err));
531 return memif_syscall_error_handler (err);
533 lm->control_fd_update (lm->poll_cancel_fd, MEMIF_FD_EVENT_READ, lm->private_ctx);
534 DBG ("libmemif event polling initialized");
537 lm->control_list_len = 2;
538 lm->interrupt_list_len = 2;
539 lm->socket_list_len = 1;
540 lm->pending_list_len = 1;
543 lm->alloc (sizeof (memif_list_elt_t) * lm->control_list_len);
544 if (lm->control_list == NULL)
546 err = MEMIF_ERR_NOMEM;
550 lm->alloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len);
551 if (lm->interrupt_list == NULL)
553 err = MEMIF_ERR_NOMEM;
557 lm->alloc (sizeof (memif_list_elt_t) * lm->socket_list_len);
558 if (lm->socket_list == NULL)
560 err = MEMIF_ERR_NOMEM;
564 lm->alloc (sizeof (memif_list_elt_t) * lm->pending_list_len);
565 if (lm->pending_list == NULL)
567 err = MEMIF_ERR_NOMEM;
572 for (i = 0; i < lm->control_list_len; i++)
574 lm->control_list[i].key = -1;
575 lm->control_list[i].data_struct = NULL;
577 for (i = 0; i < lm->interrupt_list_len; i++)
579 lm->interrupt_list[i].key = -1;
580 lm->interrupt_list[i].data_struct = NULL;
582 for (i = 0; i < lm->socket_list_len; i++)
584 lm->socket_list[i].key = -1;
585 lm->socket_list[i].data_struct = NULL;
587 for (i = 0; i < lm->pending_list_len; i++)
589 lm->pending_list[i].key = -1;
590 lm->pending_list[i].data_struct = NULL;
593 lm->disconn_slaves = 0;
595 lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK);
598 err = memif_syscall_error_handler (errno);
602 lm->arm.it_value.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC;
603 lm->arm.it_value.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC;
604 lm->arm.it_interval.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC;
605 lm->arm.it_interval.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC;
607 if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ, lm->private_ctx) < 0)
609 DBG ("callback type memif_control_fd_update_t error!");
610 err = MEMIF_ERR_CB_FDUPDATE;
614 /* Create default socket */
615 err = memif_create_socket ((memif_socket_handle_t *) &
617 MEMIF_DEFAULT_SOCKET_PATH, NULL);
618 if (err != MEMIF_ERR_SUCCESS)
628 static inline memif_ring_t *
629 memif_get_ring (memif_connection_t * conn, memif_ring_type_t type,
632 if (&conn->regions[0] == NULL)
634 void *p = conn->regions[0].addr;
636 sizeof (memif_ring_t) +
637 sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size);
638 p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size;
640 return (memif_ring_t *) p;
644 memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode,
647 memif_connection_t *conn = (memif_connection_t *) c;
649 return MEMIF_ERR_NOCONN;
651 (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->
652 run_args.num_m2s_rings;
654 return MEMIF_ERR_QID;
656 conn->rx_queues[qid].ring->flags = rx_mode;
657 DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags);
658 return MEMIF_ERR_SUCCESS;
662 memif_socket_start_listening (memif_socket_t * ms)
664 libmemif_main_t *lm = get_libmemif_main (ms);
665 memif_list_elt_t elt;
666 struct stat file_stat;
667 struct sockaddr_un un = { 0 };
669 int err = MEMIF_ERR_SUCCESS;
671 if (ms->type == MEMIF_SOCKET_TYPE_CLIENT)
672 return MEMIF_ERR_INVAL_ARG;
674 /* check if file exists */
675 if (stat ((char *) ms->filename, &file_stat) == 0)
677 if (S_ISSOCK (file_stat.st_mode))
678 unlink ((char *) ms->filename);
680 return memif_syscall_error_handler (errno);
683 ms->fd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
686 err = memif_syscall_error_handler (errno);
690 DBG ("socket %d created", ms->fd);
691 un.sun_family = AF_UNIX;
692 strlcpy ((char *) un.sun_path, (char *) ms->filename, sizeof (un.sun_path));
693 if (setsockopt (ms->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0)
695 err = memif_syscall_error_handler (errno);
698 if (bind (ms->fd, (struct sockaddr *) &un, sizeof (un)) < 0)
700 err = memif_syscall_error_handler (errno);
703 if (listen (ms->fd, 1) < 0)
705 err = memif_syscall_error_handler (errno);
708 if (stat ((char *) ms->filename, &file_stat) < 0)
710 err = memif_syscall_error_handler (errno);
714 /* add socket to libmemif main */
716 elt.data_struct = ms;
717 add_list_elt (lm, &elt, &lm->socket_list, &lm->socket_list_len);
718 /* if lm->private_ctx == lm event polling is done by libmemif */
719 lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ, lm->private_ctx);
721 ms->type = MEMIF_SOCKET_TYPE_LISTENER;
735 memif_create_socket (memif_socket_handle_t * sock, const char *filename,
738 libmemif_main_t *lm = &libmemif_main;
739 memif_socket_t *ms = (memif_socket_t *) * sock;
740 int i, err = MEMIF_ERR_SUCCESS;
742 for (i = 0; i < lm->socket_list_len; i++)
744 if ((ms = (memif_socket_t *) lm->socket_list[i].data_struct) != NULL)
746 if (strncmp ((char *) ms->filename, filename,
747 strlen ((char *) ms->filename)) == 0)
748 return MEMIF_ERR_INVAL_ARG;
752 /* allocate memif_socket_t */
754 ms = lm->alloc (sizeof (memif_socket_t));
757 err = MEMIF_ERR_NOMEM;
760 memset (ms, 0, sizeof (memif_socket_t));
762 memset (ms->filename, 0, sizeof (ms->filename));
763 strlcpy ((char *) ms->filename, filename, sizeof (ms->filename));
765 ms->type = MEMIF_SOCKET_TYPE_NONE;
767 ms->interface_list_len = 1;
769 lm->alloc (sizeof (memif_list_elt_t) * ms->interface_list_len);
770 if (ms->interface_list == NULL)
772 err = MEMIF_ERR_NOMEM;
775 ms->interface_list[0].key = -1;
776 ms->interface_list[0].data_struct = NULL;
790 if (ms->interface_list != NULL)
792 lm->free (ms->interface_list);
793 ms->interface_list = NULL;
794 ms->interface_list_len = 0;
804 memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
805 memif_connection_update_t * on_connect,
806 memif_connection_update_t * on_disconnect,
807 memif_interrupt_t * on_interrupt, void *private_ctx)
809 libmemif_main_t *lm = get_libmemif_main (args->socket);
811 memif_list_elt_t elt;
812 memif_connection_t *conn = (memif_connection_t *) * c;
817 DBG ("This handle already points to existing memif.");
818 return MEMIF_ERR_CONN;
821 conn = (memif_connection_t *) lm->alloc (sizeof (memif_connection_t));
824 err = MEMIF_ERR_NOMEM;
827 memset (conn, 0, sizeof (memif_connection_t));
829 conn->args.interface_id = args->interface_id;
831 if (args->log2_ring_size == 0)
832 args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE;
833 else if (args->log2_ring_size > MEMIF_MAX_LOG2_RING_SIZE)
835 err = MEMIF_ERR_MAX_RING;
838 if (args->buffer_size == 0)
839 args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
840 if (args->num_s2m_rings == 0)
841 args->num_s2m_rings = MEMIF_DEFAULT_TX_QUEUES;
842 if (args->num_m2s_rings == 0)
843 args->num_m2s_rings = MEMIF_DEFAULT_RX_QUEUES;
845 conn->args.num_s2m_rings = args->num_s2m_rings;
846 conn->args.num_m2s_rings = args->num_m2s_rings;
847 conn->args.buffer_size = args->buffer_size;
848 conn->args.log2_ring_size = args->log2_ring_size;
849 conn->args.is_master = args->is_master;
850 conn->args.mode = args->mode;
851 conn->msg_queue = NULL;
852 conn->regions = NULL;
853 conn->tx_queues = NULL;
854 conn->rx_queues = NULL;
856 conn->on_connect = on_connect;
857 conn->on_disconnect = on_disconnect;
858 conn->on_interrupt = on_interrupt;
859 conn->private_ctx = private_ctx;
860 memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t));
862 strlcpy ((char *) conn->args.interface_name, (char *) args->interface_name,
863 sizeof (conn->args.interface_name));
865 if ((strlen ((char *) args->secret)) > 0)
866 strlcpy ((char *) conn->args.secret, (char *) args->secret,
867 sizeof (conn->args.secret));
869 if (args->socket != NULL)
870 conn->args.socket = args->socket;
871 else if (lm->default_socket != NULL)
872 conn->args.socket = lm->default_socket;
875 err = MEMIF_ERR_INVAL_ARG;
879 ms = (memif_socket_t *) conn->args.socket;
881 if ((conn->args.is_master && ms->type == MEMIF_SOCKET_TYPE_CLIENT) ||
882 (!conn->args.is_master && ms->type == MEMIF_SOCKET_TYPE_LISTENER))
884 err = MEMIF_ERR_INVAL_ARG;
888 elt.key = conn->args.interface_id;
889 elt.data_struct = conn;
890 add_list_elt (lm, &elt, &ms->interface_list, &ms->interface_list_len);
893 if (conn->args.is_master)
895 if (ms->type == MEMIF_SOCKET_TYPE_NONE)
897 err = memif_socket_start_listening (ms);
898 if (err != MEMIF_ERR_SUCCESS)
905 elt.data_struct = conn;
907 add_list_elt (lm, &elt, &lm->control_list,
908 &lm->control_list_len)) < 0)
910 err = MEMIF_ERR_NOMEM;
916 /* try connecting to master */
917 err = memif_request_connection (conn);
918 if ((err != MEMIF_ERR_SUCCESS) && (lm->disconn_slaves == 0))
920 /* connection failed, arm reconnect timer (if not armed) */
921 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
923 err = memif_syscall_error_handler (errno);
927 lm->disconn_slaves++;
942 memif_request_connection (memif_conn_handle_t c)
944 memif_connection_t *conn = (memif_connection_t *) c;
947 int err = MEMIF_ERR_SUCCESS;
949 struct sockaddr_un sun;
952 return MEMIF_ERR_NOCONN;
954 ms = (memif_socket_t *) conn->args.socket;
955 lm = get_libmemif_main (ms);
958 if (conn->args.is_master || ms->type == MEMIF_SOCKET_TYPE_LISTENER)
959 return MEMIF_ERR_INVAL_ARG;
961 return MEMIF_ERR_ALRCONN;
963 sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
966 err = memif_syscall_error_handler (errno);
970 sun.sun_family = AF_UNIX;
972 strlcpy (sun.sun_path, (char *) ms->filename, sizeof (sun.sun_path));
974 if (connect (sockfd, (struct sockaddr *) &sun,
975 sizeof (struct sockaddr_un)) == 0)
978 conn->read_fn = memif_conn_fd_read_ready;
979 conn->write_fn = memif_conn_fd_write_ready;
980 conn->error_fn = memif_conn_fd_error;
982 lm->control_list[conn->index].key = conn->fd;
983 lm->control_fd_update (sockfd,
984 MEMIF_FD_EVENT_READ |
985 MEMIF_FD_EVENT_WRITE, lm->private_ctx);
987 lm->disconn_slaves--;
988 if (lm->disconn_slaves == 0)
990 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0)
992 err = memif_syscall_error_handler (errno);
999 err = memif_syscall_error_handler (errno);
1000 strcpy ((char *) conn->remote_disconnect_string, memif_strerror (err));
1004 ms->type = MEMIF_SOCKET_TYPE_CLIENT;
1016 memif_control_fd_handler (int fd, uint8_t events)
1018 int i, err = MEMIF_ERR_SUCCESS; /* 0 */
1020 memif_list_elt_t *e = NULL;
1021 memif_connection_t *conn;
1022 libmemif_main_t *lm = &libmemif_main;
1023 if (fd == lm->timerfd)
1027 size = read (fd, &b, sizeof (b));
1032 for (i = 0; i < lm->control_list_len; i++)
1034 if ((lm->control_list[i].key < 0)
1035 && (lm->control_list[i].data_struct != NULL))
1037 conn = lm->control_list[i].data_struct;
1038 if (conn->args.is_master)
1040 err = memif_request_connection (conn);
1041 if (err != MEMIF_ERR_SUCCESS)
1042 DBG ("memif_request_connection: %s", memif_strerror (err));
1048 get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd);
1051 if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL)
1054 (((memif_connection_t *) e->data_struct)->
1055 args.is_master) ? ((memif_connection_t *) e->
1056 data_struct)->run_args.
1057 num_s2m_rings : ((memif_connection_t *) e->data_struct)->
1058 run_args.num_m2s_rings;
1059 for (i = 0; i < num; i++)
1061 if (((memif_connection_t *) e->data_struct)->
1062 rx_queues[i].int_fd == fd)
1064 ((memif_connection_t *) e->data_struct)->
1065 on_interrupt ((void *) e->data_struct,
1066 ((memif_connection_t *) e->
1067 data_struct)->private_ctx, i);
1068 return MEMIF_ERR_SUCCESS;
1072 return MEMIF_ERR_SUCCESS;
1074 get_list_elt (&e, lm->socket_list, lm->socket_list_len, fd);
1076 && ((memif_socket_t *) e->data_struct)->type ==
1077 MEMIF_SOCKET_TYPE_LISTENER)
1080 memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct);
1084 get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd);
1087 err = memif_read_ready (lm, fd);
1091 get_list_elt (&e, lm->control_list, lm->control_list_len, fd);
1094 if (events & MEMIF_FD_EVENT_READ)
1097 ((memif_connection_t *) e->data_struct)->
1098 read_fn (e->data_struct);
1099 if (err != MEMIF_ERR_SUCCESS)
1102 if (events & MEMIF_FD_EVENT_WRITE)
1105 ((memif_connection_t *) e->data_struct)->
1106 write_fn (e->data_struct);
1107 if (err != MEMIF_ERR_SUCCESS)
1110 if (events & MEMIF_FD_EVENT_ERROR)
1113 ((memif_connection_t *) e->data_struct)->
1114 error_fn (e->data_struct);
1115 if (err != MEMIF_ERR_SUCCESS)
1121 return MEMIF_ERR_SUCCESS; /* 0 */
1129 memif_poll_event (int timeout)
1131 libmemif_main_t *lm = &libmemif_main;
1132 struct epoll_event evt;
1133 int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */
1134 uint32_t events = 0;
1135 uint64_t counter = 0;
1137 memset (&evt, 0, sizeof (evt));
1138 evt.events = EPOLLIN | EPOLLOUT;
1140 sigemptyset (&sigset);
1141 en = epoll_pwait (lm->epfd, &evt, 1, timeout, &sigset);
1145 DBG ("epoll_pwait: %s", strerror (err));
1146 return memif_syscall_error_handler (err);
1150 if (evt.data.fd == lm->poll_cancel_fd)
1152 r = read (evt.data.fd, &counter, sizeof (counter));
1154 return MEMIF_ERR_DISCONNECTED;
1156 return MEMIF_ERR_POLL_CANCEL;
1158 if (evt.events & EPOLLIN)
1159 events |= MEMIF_FD_EVENT_READ;
1160 if (evt.events & EPOLLOUT)
1161 events |= MEMIF_FD_EVENT_WRITE;
1162 if (evt.events & EPOLLERR)
1163 events |= MEMIF_FD_EVENT_ERROR;
1164 err = memif_control_fd_handler (evt.data.fd, events);
1171 memif_cancel_poll_event ()
1173 libmemif_main_t *lm = &libmemif_main;
1174 uint64_t counter = 1;
1177 if (lm->poll_cancel_fd == -1)
1179 w = write (lm->poll_cancel_fd, &counter, sizeof (counter));
1180 if (w < sizeof (counter))
1181 return MEMIF_ERR_INT_WRITE;
1187 memif_msg_queue_free (libmemif_main_t * lm, memif_msg_queue_elt_t ** e)
1191 memif_msg_queue_free (lm, &(*e)->next);
1197 /* send disconnect msg and close interface */
1199 memif_disconnect_internal (memif_connection_t * c)
1201 int err = MEMIF_ERR_SUCCESS, i; /* 0 */
1203 libmemif_main_t *lm;
1204 memif_list_elt_t *e;
1208 DBG ("no connection");
1209 return MEMIF_ERR_NOCONN;
1212 lm = get_libmemif_main (c->args.socket);
1214 c->on_disconnect ((void *) c, c->private_ctx);
1218 memif_msg_send_disconnect (c->fd, (uint8_t *) "interface deleted", 0);
1219 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL, lm->private_ctx);
1222 get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd);
1225 if (c->args.is_master)
1226 free_list_elt (lm->control_list, lm->control_list_len, c->fd);
1227 e->key = c->fd = -1;
1230 if (c->tx_queues != NULL)
1232 for (i = 0; i < c->tx_queues_num; i++)
1234 mq = &c->tx_queues[i];
1239 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1244 lm->free (c->tx_queues);
1245 c->tx_queues = NULL;
1247 c->tx_queues_num = 0;
1249 if (c->rx_queues != NULL)
1251 for (i = 0; i < c->rx_queues_num; i++)
1253 mq = &c->rx_queues[i];
1258 if (c->on_interrupt != NULL)
1259 lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL,
1263 free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
1268 lm->free (c->rx_queues);
1269 c->rx_queues = NULL;
1271 c->rx_queues_num = 0;
1273 for (i = 0; i < c->regions_num; i++)
1275 if (&c->regions[i] == NULL)
1277 if (c->regions[i].is_external != 0)
1279 lm->del_external_region (c->regions[i].addr,
1280 c->regions[i].region_size,
1281 c->regions[i].fd, c->private_ctx);
1285 if (munmap (c->regions[i].addr, c->regions[i].region_size) < 0)
1286 return memif_syscall_error_handler (errno);
1287 if (c->regions[i].fd > 0)
1288 close (c->regions[i].fd);
1289 c->regions[i].fd = -1;
1292 lm->free (c->regions);
1296 memset (&c->run_args, 0, sizeof (memif_conn_run_args_t));
1298 memif_msg_queue_free (lm, &c->msg_queue);
1300 if (!(c->args.is_master))
1302 if (lm->disconn_slaves == 0)
1304 if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
1306 err = memif_syscall_error_handler (errno);
1307 DBG ("timerfd_settime: arm");
1310 lm->disconn_slaves++;
1317 memif_get_socket_filename (memif_socket_handle_t sock)
1319 memif_socket_t *ms = (memif_socket_t *) sock;
1324 return (char *) ms->filename;
1328 memif_delete_socket (memif_socket_handle_t * sock)
1330 memif_socket_t *ms = (memif_socket_t *) * sock;
1331 libmemif_main_t *lm;
1333 /* check if socket is in use */
1334 if (ms == NULL || ms->use_count > 0)
1335 return MEMIF_ERR_INVAL_ARG;
1337 lm = get_libmemif_main (ms);
1339 lm->free (ms->interface_list);
1340 ms->interface_list = NULL;
1344 return MEMIF_ERR_SUCCESS;
1348 memif_delete (memif_conn_handle_t * conn)
1350 memif_connection_t *c = (memif_connection_t *) * conn;
1351 libmemif_main_t *lm;
1352 memif_socket_t *ms = NULL;
1353 int err = MEMIF_ERR_SUCCESS;
1357 DBG ("no connection");
1358 return MEMIF_ERR_NOCONN;
1363 DBG ("DISCONNECTING");
1364 err = memif_disconnect_internal (c);
1365 if (err == MEMIF_ERR_NOCONN)
1369 lm = get_libmemif_main (c->args.socket);
1371 free_list_elt_ctx (lm->control_list, lm->control_list_len, c);
1373 ms = (memif_socket_t *) c->args.socket;
1375 free_list_elt (ms->interface_list, ms->interface_list_len,
1376 c->args.interface_id);
1377 if (ms->use_count <= 0)
1379 /* stop listening on this socket */
1380 if (ms->type == MEMIF_SOCKET_TYPE_LISTENER)
1382 lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_DEL, lm->private_ctx);
1383 free_list_elt (lm->socket_list, lm->socket_list_len, ms->fd);
1387 /* socket not in use */
1388 ms->type = MEMIF_SOCKET_TYPE_NONE;
1391 if (!c->args.is_master)
1393 lm->disconn_slaves--;
1394 if (lm->disconn_slaves <= 0)
1396 if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0)
1398 err = memif_syscall_error_handler (errno);
1399 DBG ("timerfd_settime: disarm");
1412 memif_connect1 (memif_connection_t * c)
1414 libmemif_main_t *lm;
1420 return MEMIF_ERR_INVAL_ARG;
1422 lm = get_libmemif_main (c->args.socket);
1424 for (i = 0; i < c->regions_num; i++)
1426 mr = &c->regions[i];
1431 if (mr->is_external)
1433 if (lm->get_external_region_addr == NULL)
1434 return MEMIF_ERR_INVAL_ARG;
1436 lm->get_external_region_addr (mr->region_size, mr->fd,
1442 return MEMIF_ERR_NO_SHMFD;
1445 mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE,
1446 MAP_SHARED, mr->fd, 0)) == MAP_FAILED)
1448 return memif_syscall_error_handler (errno);
1455 for (i = 0; i < c->rx_queues_num; i++)
1457 mq = &c->rx_queues[i];
1460 mq->ring = c->regions[mq->region].addr + mq->offset;
1461 if (mq->ring->cookie != MEMIF_COOKIE)
1463 DBG ("wrong cookie on rx ring %u", i);
1464 return MEMIF_ERR_COOKIE;
1466 mq->ring->head = mq->ring->tail = mq->last_head = mq->next_buf = 0;
1470 for (i = 0; i < c->tx_queues_num; i++)
1472 mq = &c->tx_queues[i];
1475 mq->ring = c->regions[mq->region].addr + mq->offset;
1476 if (mq->ring->cookie != MEMIF_COOKIE)
1478 DBG ("wrong cookie on tx ring %u", i);
1479 return MEMIF_ERR_COOKIE;
1481 mq->ring->head = mq->ring->tail = mq->last_head = mq->next_buf = 0;
1485 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD,
1492 memif_add_region (libmemif_main_t * lm, memif_connection_t * conn,
1493 uint8_t has_buffers)
1498 lm->realloc (conn->regions,
1499 sizeof (memif_region_t) * ++conn->regions_num);
1501 return MEMIF_ERR_NOMEM;
1504 r = &conn->regions[conn->regions_num - 1];
1505 memset (r, 0, sizeof (memif_region_t));
1507 if (has_buffers != 0)
1509 r->buffer_offset = 0;
1514 (conn->run_args.num_s2m_rings +
1515 conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) +
1516 sizeof (memif_desc_t) *
1518 run_args.log2_ring_size));
1521 r->region_size = (has_buffers == 0) ? r->buffer_offset : r->buffer_offset +
1522 conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
1523 (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
1525 if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1)
1526 return memif_syscall_error_handler (errno);
1528 if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
1529 return memif_syscall_error_handler (errno);
1531 if ((ftruncate (r->fd, r->region_size)) == -1)
1532 return memif_syscall_error_handler (errno);
1534 if ((r->addr = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE,
1535 MAP_SHARED, r->fd, 0)) == MAP_FAILED)
1536 return memif_syscall_error_handler (errno);
1538 return MEMIF_ERR_SUCCESS;
1542 memif_init_queues (libmemif_main_t * lm, memif_connection_t * conn)
1547 for (i = 0; i < conn->run_args.num_s2m_rings; i++)
1549 ring = memif_get_ring (conn, MEMIF_RING_S2M, i);
1550 DBG ("RING: %p I: %d", ring, i);
1551 ring->head = ring->tail = 0;
1552 ring->cookie = MEMIF_COOKIE;
1554 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1556 uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j;
1557 ring->desc[j].region = 1;
1558 ring->desc[j].offset =
1559 conn->regions[1].buffer_offset +
1560 (uint32_t) (slot * conn->run_args.buffer_size);
1561 ring->desc[j].length = conn->run_args.buffer_size;
1564 for (i = 0; i < conn->run_args.num_m2s_rings; i++)
1566 ring = memif_get_ring (conn, MEMIF_RING_M2S, i);
1567 DBG ("RING: %p I: %d", ring, i);
1568 ring->head = ring->tail = 0;
1569 ring->cookie = MEMIF_COOKIE;
1571 for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
1573 uint16_t slot = (i + conn->run_args.num_s2m_rings) *
1574 (1 << conn->run_args.log2_ring_size) + j;
1575 ring->desc[j].region = 1;
1576 ring->desc[j].offset =
1577 conn->regions[1].buffer_offset +
1578 (uint32_t) (slot * conn->run_args.buffer_size);
1579 ring->desc[j].length = conn->run_args.buffer_size;
1583 DBG ("alloc: %p", lm->alloc);
1584 DBG ("size: %lu", sizeof (memif_queue_t) * conn->run_args.num_s2m_rings);
1586 (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) *
1587 conn->run_args.num_s2m_rings);
1589 return MEMIF_ERR_NOMEM;
1593 for (x = 0; x < conn->run_args.num_s2m_rings; x++)
1595 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1596 return memif_syscall_error_handler (errno);
1597 e.key = mq[x].int_fd;
1598 e.data_struct = conn;
1599 add_list_elt (lm, &e, &lm->interrupt_list, &lm->interrupt_list_len);
1601 mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x);
1602 DBG ("RING: %p I: %d", mq[x].ring, x);
1603 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1606 (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
1607 mq[x].last_head = mq[x].last_tail = 0;
1610 conn->tx_queues = mq;
1611 conn->tx_queues_num = conn->run_args.num_s2m_rings;
1614 (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) *
1615 conn->run_args.num_m2s_rings);
1617 return MEMIF_ERR_NOMEM;
1619 for (x = 0; x < conn->run_args.num_m2s_rings; x++)
1621 if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
1622 return memif_syscall_error_handler (errno);
1623 e.key = mq[x].int_fd;
1624 e.data_struct = conn;
1625 add_list_elt (lm, &e, &lm->interrupt_list, &lm->interrupt_list_len);
1627 mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x);
1628 DBG ("RING: %p I: %d", mq[x].ring, x);
1629 mq[x].log2_ring_size = conn->run_args.log2_ring_size;
1632 (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
1633 mq[x].last_head = mq[x].last_tail = 0;
1636 conn->rx_queues = mq;
1637 conn->rx_queues_num = conn->run_args.num_m2s_rings;
1639 return MEMIF_ERR_SUCCESS;
1643 memif_init_regions_and_queues (memif_connection_t * conn)
1646 libmemif_main_t *lm;
1649 return MEMIF_ERR_INVAL_ARG;
1651 lm = get_libmemif_main (conn->args.socket);
1653 /* region 0. rings */
1654 memif_add_region (lm, conn, /* has_buffers */ 0);
1656 /* region 1. buffers */
1657 if (lm->add_external_region)
1660 (memif_region_t *) lm->realloc (conn->regions,
1661 sizeof (memif_region_t) *
1662 ++conn->regions_num);
1664 return MEMIF_ERR_NOMEM;
1667 conn->regions[1].region_size =
1668 conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
1669 (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
1670 conn->regions[1].buffer_offset = 0;
1671 lm->add_external_region (&conn->regions[1].addr,
1672 conn->regions[1].region_size,
1673 &conn->regions[1].fd, conn->private_ctx);
1674 conn->regions[1].is_external = 1;
1678 memif_add_region (lm, conn, 1);
1681 memif_init_queues (lm, conn);
1687 memif_set_next_free_buffer (memif_conn_handle_t conn, uint16_t qid,
1688 memif_buffer_t *buf)
1690 memif_connection_t *c = (memif_connection_t *) conn;
1691 if (EXPECT_FALSE (c == NULL))
1692 return MEMIF_ERR_NOCONN;
1693 if (EXPECT_FALSE (qid >= c->tx_queues_num))
1694 return MEMIF_ERR_QID;
1695 if (EXPECT_FALSE (buf == NULL))
1696 return MEMIF_ERR_INVAL_ARG;
1698 uint16_t ring_size, ns;
1699 memif_queue_t *mq = &c->tx_queues[qid];
1700 memif_ring_t *ring = mq->ring;
1702 ring_size = (1 << mq->log2_ring_size);
1703 if (c->args.is_master)
1704 ns = ring->head - mq->next_buf;
1706 ns = ring_size - mq->next_buf + ring->tail;
1708 if ((mq->next_buf - buf->desc_index) > ns)
1709 return MEMIF_ERR_INVAL_ARG;
1711 mq->next_buf = buf->desc_index;
1713 return MEMIF_ERR_SUCCESS;
1717 memif_buffer_enq_at_idx_internal (memif_queue_t *from_q, memif_queue_t *to_q,
1718 memif_buffer_t *buf, uint16_t slot)
1720 uint16_t from_mask = (1 << from_q->log2_ring_size) - 1;
1721 uint16_t to_mask = (1 << to_q->log2_ring_size) - 1;
1722 memif_desc_t *from_d, *to_d, tmp_d;
1724 /* Get the descriptors */
1725 from_d = &from_q->ring->desc[buf->desc_index & from_mask];
1726 to_d = &to_q->ring->desc[slot & to_mask];
1728 /* Swap descriptors */
1733 /* Update descriptor index and queue for clients buffer */
1734 buf->desc_index = slot;
1739 memif_buffer_requeue (memif_conn_handle_t conn, memif_buffer_t *buf_a,
1740 memif_buffer_t *buf_b)
1742 memif_connection_t *c = (memif_connection_t *) conn;
1743 if (EXPECT_FALSE (c == NULL))
1744 return MEMIF_ERR_NOCONN;
1745 if (EXPECT_FALSE (c->args.is_master))
1746 return MEMIF_ERR_INVAL_ARG;
1747 if ((buf_a == NULL) || (buf_b == NULL))
1748 return MEMIF_ERR_INVAL_ARG;
1751 /* store buf_a information */
1752 uint16_t index_a = buf_a->desc_index;
1753 memif_queue_t *mq_a = buf_a->queue;
1755 /* swap buffers, buf_a was updated with new desc_index and queue */
1756 memif_buffer_enq_at_idx_internal ((memif_queue_t *) buf_a->queue,
1757 (memif_queue_t *) buf_b->queue, buf_a,
1760 /* update buf_b desc_index and queue */
1761 buf_b->desc_index = index_a;
1762 buf_b->queue = mq_a;
1764 return MEMIF_ERR_SUCCESS;
1768 memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid,
1769 memif_buffer_t * bufs, uint16_t count,
1770 uint16_t * count_out)
1772 memif_connection_t *c = (memif_connection_t *) conn;
1773 if (EXPECT_FALSE (c == NULL))
1774 return MEMIF_ERR_NOCONN;
1775 if (EXPECT_FALSE (c->fd < 0))
1776 return MEMIF_ERR_DISCONNECTED;
1777 if (EXPECT_FALSE (qid >= c->tx_queues_num))
1778 return MEMIF_ERR_QID;
1779 if (EXPECT_FALSE (!count_out))
1780 return MEMIF_ERR_INVAL_ARG;
1781 if (EXPECT_FALSE (c->args.is_master))
1782 return MEMIF_ERR_INVAL_ARG;
1784 memif_queue_t *mq = &c->tx_queues[qid];
1785 memif_ring_t *ring = mq->ring;
1787 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1791 int err = MEMIF_ERR_SUCCESS; /* 0 */
1794 ring_size = (1 << mq->log2_ring_size);
1796 /* can only be called by slave */
1797 ns = ring_size - mq->next_buf + ring->tail;
1803 /* Swaps the descriptors, updates next_buf pointer and updates client
1806 memif_buffer_enq_at_idx_internal ((memif_queue_t *) b0->queue, mq, b0,
1809 mq->next_buf++; /* mark the buffer as allocated */
1816 DBG ("allocated: %u/%u bufs. Next buffer pointer %d", *count_out, count,
1821 DBG ("ring buffer full! qid: %u", qid);
1822 err = MEMIF_ERR_NOBUF_RING;
1829 memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
1830 memif_buffer_t * bufs, uint16_t count,
1831 uint16_t * count_out, uint16_t size)
1833 memif_connection_t *c = (memif_connection_t *) conn;
1834 if (EXPECT_FALSE (c == NULL))
1835 return MEMIF_ERR_NOCONN;
1836 if (EXPECT_FALSE (c->fd < 0))
1837 return MEMIF_ERR_DISCONNECTED;
1839 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
1840 run_args.num_s2m_rings;
1841 if (EXPECT_FALSE (qid >= num))
1842 return MEMIF_ERR_QID;
1843 if (EXPECT_FALSE (!count_out))
1844 return MEMIF_ERR_INVAL_ARG;
1846 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
1847 memif_queue_t *mq = &c->tx_queues[qid];
1848 memif_ring_t *ring = mq->ring;
1850 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1851 uint32_t offset_mask = c->run_args.buffer_size - 1;
1854 int err = MEMIF_ERR_SUCCESS; /* 0 */
1855 uint16_t dst_left, src_left;
1856 uint16_t saved_count;
1857 uint16_t saved_next_buf;
1858 memif_buffer_t *saved_b;
1861 ring_size = (1 << mq->log2_ring_size);
1863 if (c->args.is_master)
1864 ns = ring->head - mq->next_buf;
1866 ns = ring_size - mq->next_buf + ring->tail;
1870 b0 = (bufs + *count_out);
1873 saved_count = count;
1874 saved_next_buf = mq->next_buf;
1876 b0->desc_index = mq->next_buf;
1877 ring->desc[mq->next_buf & mask].flags = 0;
1879 /* slave can produce buffer with original length */
1880 dst_left = (c->args.is_master) ? ring->desc[mq->next_buf & mask].length :
1881 c->run_args.buffer_size;
1886 if (EXPECT_FALSE (dst_left == 0))
1894 ring->desc[b0->desc_index & mask].flags |=
1895 MEMIF_DESC_FLAG_NEXT;
1896 b0->flags |= MEMIF_BUFFER_FLAG_NEXT;
1898 b0 = (bufs + *count_out);
1899 b0->desc_index = mq->next_buf;
1900 dst_left = (c->args.is_master) ?
1901 ring->desc[mq->next_buf & mask].length :
1902 c->run_args.buffer_size;
1903 ring->desc[mq->next_buf & mask].flags = 0;
1907 /* rollback allocated chain buffers */
1908 memset (saved_b, 0, sizeof (memif_buffer_t)
1909 * (saved_count - count + 1));
1910 *count_out -= saved_count - count;
1911 mq->next_buf = saved_next_buf;
1915 b0->len = memif_min (dst_left, src_left);
1917 /* slave resets buffer offset */
1918 if (c->args.is_master == 0)
1920 memif_desc_t *d = &ring->desc[mq->next_buf & mask];
1921 if (lm->get_external_buffer_offset)
1922 d->offset = lm->get_external_buffer_offset (c->private_ctx);
1924 d->offset = d->offset - (d->offset & offset_mask);
1926 b0->data = memif_get_buffer (c, ring, mq->next_buf & mask);
1928 src_left -= b0->len;
1929 dst_left -= b0->len;
1940 DBG ("allocated: %u/%u bufs. Next buffer pointer %d", *count_out, count,
1945 DBG ("ring buffer full! qid: %u", qid);
1946 err = MEMIF_ERR_NOBUF_RING;
1953 memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count,
1956 memif_connection_t *c = (memif_connection_t *) conn;
1957 if (EXPECT_FALSE (c == NULL))
1958 return MEMIF_ERR_NOCONN;
1959 if (EXPECT_FALSE (c->fd < 0))
1960 return MEMIF_ERR_DISCONNECTED;
1962 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
1963 run_args.num_m2s_rings;
1964 if (EXPECT_FALSE (qid >= num))
1965 return MEMIF_ERR_QID;
1966 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
1967 memif_queue_t *mq = &c->rx_queues[qid];
1968 memif_ring_t *ring = mq->ring;
1969 uint16_t mask = (1 << mq->log2_ring_size) - 1;
1970 uint32_t offset_mask = c->run_args.buffer_size - 1;
1971 uint16_t slot, counter = 0;
1973 if (c->args.is_master)
1975 MEMIF_MEMORY_BARRIER ();
1977 (ring->tail + count <=
1978 mq->last_head) ? ring->tail + count : mq->last_head;
1979 return MEMIF_ERR_SUCCESS;
1982 uint16_t head = ring->head;
1984 uint16_t ns = (1 << mq->log2_ring_size) - head + mq->last_tail;
1985 count = (count < ns) ? count : ns;
1988 while (counter < count)
1990 d = &ring->desc[slot & mask];
1992 d->length = c->run_args.buffer_size - headroom;
1993 if (lm->get_external_buffer_offset)
1994 d->offset = lm->get_external_buffer_offset (c->private_ctx);
1996 d->offset = d->offset - (d->offset & offset_mask) + headroom;
2001 MEMIF_MEMORY_BARRIER ();
2004 return MEMIF_ERR_SUCCESS; /* 0 */
2008 memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
2009 memif_buffer_t * bufs, uint16_t count, uint16_t * tx)
2011 memif_connection_t *c = (memif_connection_t *) conn;
2012 if (EXPECT_FALSE (c == NULL))
2013 return MEMIF_ERR_NOCONN;
2014 if (EXPECT_FALSE (c->fd < 0))
2015 return MEMIF_ERR_DISCONNECTED;
2017 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
2018 run_args.num_s2m_rings;
2019 if (EXPECT_FALSE (qid >= num))
2020 return MEMIF_ERR_QID;
2021 if (EXPECT_FALSE (!tx))
2022 return MEMIF_ERR_INVAL_ARG;
2024 memif_queue_t *mq = &c->tx_queues[qid];
2025 memif_ring_t *ring = mq->ring;
2026 uint16_t mask = (1 << mq->log2_ring_size) - 1;
2027 uint32_t offset_mask = c->run_args.buffer_size - 1;
2030 int64_t data_offset;
2032 int err = MEMIF_ERR_SUCCESS;
2034 if (EXPECT_FALSE (count == 0))
2035 return MEMIF_ERR_SUCCESS;
2038 if (c->args.is_master)
2046 /* set error to MEMIF_ERR_INVAL_ARG and finish the sending process
2048 if ((b0->desc_index & mask) != (index & mask))
2050 err = MEMIF_ERR_INVAL_ARG;
2053 d = &ring->desc[b0->desc_index & mask];
2054 d->length = b0->len;
2055 if (!c->args.is_master)
2058 d->offset = d->offset - (d->offset & offset_mask);
2059 // calculate offset from user data
2060 data_offset = b0->data - (d->offset + c->regions[d->region].addr);
2061 if (data_offset != 0)
2063 /* verify data offset and buffer length */
2064 if ((data_offset < 0) ||
2065 ((data_offset + b0->len) > c->run_args.buffer_size))
2067 DBG ("slot: %d, data_offset: %d, length: %d",
2068 b0->desc_index & mask, data_offset, b0->len);
2069 err = MEMIF_ERR_INVAL_ARG;
2072 d->offset += data_offset;
2076 #ifdef MEMIF_DBG_SHM
2077 printf ("offset: %-6d\n", ring->desc[b0->desc_index & mask].offset);
2078 printf ("data: %p\n",
2079 memif_get_buffer (c, ring, b0->desc_index & mask));
2080 printf ("index: %u\n", b0->desc_index);
2081 print_bytes (memif_get_buffer (c, ring, b0->desc_index & mask),
2082 ring->desc[b0->desc_index & mask].length, DBG_TX_BUF);
2083 #endif /* MEMIF_DBG_SHM */
2091 MEMIF_MEMORY_BARRIER ();
2092 if (c->args.is_master)
2093 ring->tail = b0->desc_index + 1;
2095 ring->head = b0->desc_index + 1;
2097 if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
2100 int r = write (mq->int_fd, &a, sizeof (a));
2102 return MEMIF_ERR_INT_WRITE;
2109 memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
2110 memif_buffer_t * bufs, uint16_t count, uint16_t * rx)
2112 memif_connection_t *c = (memif_connection_t *) conn;
2113 if (EXPECT_FALSE (c == NULL))
2114 return MEMIF_ERR_NOCONN;
2115 if (EXPECT_FALSE (c->fd < 0))
2116 return MEMIF_ERR_DISCONNECTED;
2118 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2119 run_args.num_m2s_rings;
2120 if (EXPECT_FALSE (qid >= num))
2121 return MEMIF_ERR_QID;
2122 if (EXPECT_FALSE (!rx))
2123 return MEMIF_ERR_INVAL_ARG;
2125 memif_queue_t *mq = &c->rx_queues[qid];
2126 memif_ring_t *ring = mq->ring;
2127 uint16_t cur_slot, last_slot;
2129 uint16_t mask = (1 << mq->log2_ring_size) - 1;
2136 cur_slot = (c->args.is_master) ? mq->last_head : mq->last_tail;
2137 last_slot = (c->args.is_master) ? ring->head : ring->tail;
2138 if (cur_slot == last_slot)
2140 r = read (mq->int_fd, &b, sizeof (b));
2141 if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN)))
2142 return memif_syscall_error_handler (errno);
2144 return MEMIF_ERR_SUCCESS;
2147 ns = last_slot - cur_slot;
2153 b0->desc_index = cur_slot;
2154 b0->data = memif_get_buffer (c, ring, cur_slot & mask);
2155 b0->len = ring->desc[cur_slot & mask].length;
2156 /* slave resets buffer length */
2157 if (c->args.is_master == 0)
2159 ring->desc[cur_slot & mask].length = c->run_args.buffer_size;
2162 if (ring->desc[cur_slot & mask].flags & MEMIF_DESC_FLAG_NEXT)
2164 b0->flags |= MEMIF_BUFFER_FLAG_NEXT;
2165 ring->desc[cur_slot & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
2169 #ifdef MEMIF_DBG_SHM
2170 printf ("data: %p\n", b0->data);
2171 printf ("index: %u\n", b0->desc_index);
2172 printf ("queue: %p\n", b0->queue);
2173 print_bytes (b0->data, b0->len, DBG_RX_BUF);
2174 #endif /* MEMIF_DBG_SHM */
2182 if (c->args.is_master)
2183 mq->last_head = cur_slot;
2185 mq->last_tail = cur_slot;
2189 DBG ("not enough buffers!");
2190 return MEMIF_ERR_NOBUF;
2193 r = read (mq->int_fd, &b, sizeof (b));
2194 if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN)))
2195 return memif_syscall_error_handler (errno);
2197 return MEMIF_ERR_SUCCESS; /* 0 */
2201 memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
2202 char *buf, ssize_t buflen)
2204 memif_connection_t *c = (memif_connection_t *) conn;
2205 libmemif_main_t *lm;
2207 int err = MEMIF_ERR_SUCCESS, i;
2211 return MEMIF_ERR_NOCONN;
2213 ms = (memif_socket_t *) c->args.socket;
2214 lm = get_libmemif_main (ms);
2216 l1 = strlen ((char *) c->args.interface_name);
2217 if (l0 + l1 < buflen)
2220 (uint8_t *) strcpy (buf + l0, (char *) c->args.interface_name);
2224 err = MEMIF_ERR_NOBUF_DET;
2226 l1 = strlen ((char *) lm->app_name);
2227 if (l0 + l1 < buflen)
2229 md->inst_name = (uint8_t *) strcpy (buf + l0, (char *) lm->app_name);
2233 err = MEMIF_ERR_NOBUF_DET;
2235 l1 = strlen ((char *) c->remote_if_name);
2236 if (l0 + l1 < buflen)
2238 md->remote_if_name =
2239 (uint8_t *) strcpy (buf + l0, (char *) c->remote_if_name);
2243 err = MEMIF_ERR_NOBUF_DET;
2245 l1 = strlen ((char *) c->remote_name);
2246 if (l0 + l1 < buflen)
2248 md->remote_inst_name =
2249 (uint8_t *) strcpy (buf + l0, (char *) c->remote_name);
2253 err = MEMIF_ERR_NOBUF_DET;
2255 md->id = c->args.interface_id;
2257 if (strlen ((char *) c->args.secret) > 0)
2259 l1 = strlen ((char *) c->args.secret);
2260 if (l0 + l1 < buflen)
2262 md->secret = (uint8_t *) strcpy (buf + l0, (char *) c->args.secret);
2266 err = MEMIF_ERR_NOBUF_DET;
2269 md->role = (c->args.is_master) ? 0 : 1;
2270 md->mode = c->args.mode;
2272 l1 = strlen ((char *) ms->filename);
2273 if (l0 + l1 < buflen)
2275 md->socket_filename =
2276 (uint8_t *) strcpy (buf + l0, (char *) ms->filename);
2280 err = MEMIF_ERR_NOBUF_DET;
2282 l1 = strlen ((char *) c->remote_disconnect_string);
2283 if (l0 + l1 < buflen)
2286 (uint8_t *) strcpy (buf + l0, (char *) c->remote_disconnect_string);
2290 err = MEMIF_ERR_NOBUF_DET;
2292 md->regions_num = c->regions_num;
2293 l1 = sizeof (memif_region_details_t) * md->regions_num;
2294 if (l0 + l1 <= buflen)
2296 md->regions = (memif_region_details_t *) (buf + l0);
2297 for (i = 0; i < md->regions_num; i++)
2299 md->regions[i].index = i;
2300 md->regions[i].addr = c->regions[i].addr;
2301 md->regions[i].size = c->regions[i].region_size;
2302 md->regions[i].fd = c->regions[i].fd;
2303 md->regions[i].is_external = c->regions[i].is_external;
2308 err = MEMIF_ERR_NOBUF_DET;
2311 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2312 run_args.num_m2s_rings;
2314 l1 = sizeof (memif_queue_details_t) * md->rx_queues_num;
2315 if (l0 + l1 <= buflen)
2317 md->rx_queues = (memif_queue_details_t *) (buf + l0);
2318 for (i = 0; i < md->rx_queues_num; i++)
2320 md->rx_queues[i].region = c->rx_queues[i].region;
2321 md->rx_queues[i].qid = i;
2322 md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size);
2323 md->rx_queues[i].flags = c->rx_queues[i].ring->flags;
2324 md->rx_queues[i].head = c->rx_queues[i].ring->head;
2325 md->rx_queues[i].tail = c->rx_queues[i].ring->tail;
2326 md->rx_queues[i].buffer_size = c->run_args.buffer_size;
2331 err = MEMIF_ERR_NOBUF_DET;
2334 (c->args.is_master) ? c->run_args.num_m2s_rings : c->
2335 run_args.num_s2m_rings;
2337 l1 = sizeof (memif_queue_details_t) * md->tx_queues_num;
2338 if (l0 + l1 <= buflen)
2340 md->tx_queues = (memif_queue_details_t *) (buf + l0);
2341 for (i = 0; i < md->tx_queues_num; i++)
2343 md->tx_queues[i].region = c->tx_queues[i].region;
2344 md->tx_queues[i].qid = i;
2345 md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size);
2346 md->tx_queues[i].flags = c->tx_queues[i].ring->flags;
2347 md->tx_queues[i].head = c->tx_queues[i].ring->head;
2348 md->tx_queues[i].tail = c->tx_queues[i].ring->tail;
2349 md->tx_queues[i].buffer_size = c->run_args.buffer_size;
2354 err = MEMIF_ERR_NOBUF_DET;
2356 md->link_up_down = (c->fd > 0) ? 1 : 0;
2362 memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd)
2364 memif_connection_t *c = (memif_connection_t *) conn;
2369 return MEMIF_ERR_NOCONN;
2371 return MEMIF_ERR_DISCONNECTED;
2374 (c->args.is_master) ? c->run_args.num_s2m_rings : c->
2375 run_args.num_m2s_rings;
2377 return MEMIF_ERR_QID;
2379 *efd = c->rx_queues[qid].int_fd;
2381 return MEMIF_ERR_SUCCESS;
2387 libmemif_main_t *lm = &libmemif_main;
2390 err = memif_delete_socket ((memif_socket_handle_t *) & lm->default_socket);
2391 if (err != MEMIF_ERR_SUCCESS)
2394 if (lm->control_list)
2395 lm->free (lm->control_list);
2396 lm->control_list = NULL;
2397 if (lm->interrupt_list)
2398 lm->free (lm->interrupt_list);
2399 lm->interrupt_list = NULL;
2400 if (lm->socket_list)
2401 lm->free (lm->socket_list);
2402 lm->socket_list = NULL;
2403 if (lm->pending_list)
2404 lm->free (lm->pending_list);
2405 lm->pending_list = NULL;
2406 if (lm->poll_cancel_fd != -1)
2407 close (lm->poll_cancel_fd);
2409 return MEMIF_ERR_SUCCESS; /* 0 */