aboutsummaryrefslogtreecommitdiffstats
path: root/extras/libmemif/src
diff options
context:
space:
mode:
Diffstat (limited to 'extras/libmemif/src')
-rw-r--r--extras/libmemif/src/libmemif.h443
-rw-r--r--extras/libmemif/src/main.c2095
-rw-r--r--extras/libmemif/src/memif.h185
-rw-r--r--extras/libmemif/src/memif_private.h266
-rw-r--r--extras/libmemif/src/socket.c881
-rw-r--r--extras/libmemif/src/socket.h89
6 files changed, 3959 insertions, 0 deletions
diff --git a/extras/libmemif/src/libmemif.h b/extras/libmemif/src/libmemif.h
new file mode 100644
index 00000000..a2d1a5e2
--- /dev/null
+++ b/extras/libmemif/src/libmemif.h
@@ -0,0 +1,443 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+/** @file */
+
+#ifndef _LIBMEMIF_H_
+#define _LIBMEMIF_H_
+
+/** Libmemif version. */
+#define LIBMEMIF_VERSION "1.0"
+/** Default name of application using libmemif. */
+#define MEMIF_DEFAULT_APP_NAME "libmemif-app"
+
+#include <inttypes.h>
+
+#include <memif.h>
+
+/*! Error codes */
+typedef enum
+{
+ MEMIF_ERR_SUCCESS = 0, /*!< success */
+/* SYSCALL ERRORS */
+ MEMIF_ERR_SYSCALL, /*!< other syscall error */
+ MEMIF_ERR_ACCES, /*!< permission denied */
+ MEMIF_ERR_NO_FILE, /*!< file does not exist */
+ MEMIF_ERR_FILE_LIMIT, /*!< system open file limit */
+ MEMIF_ERR_PROC_FILE_LIMIT, /*!< process open file limit */
+ MEMIF_ERR_ALREADY, /*!< connection already requested */
+ MEMIF_ERR_AGAIN, /*!< fd is not socket, or operation would block */
+ MEMIF_ERR_BAD_FD, /*!< invalid fd */
+ MEMIF_ERR_NOMEM, /*!< out of memory */
+/* LIBMEMIF ERRORS */
+ MEMIF_ERR_INVAL_ARG, /*!< invalid argument */
+ MEMIF_ERR_NOCONN, /*!< handle points to no connection */
+ MEMIF_ERR_CONN, /*!< handle points to existing connection */
+ MEMIF_ERR_CB_FDUPDATE, /*!< user defined callback memif_control_fd_update_t error */
+ MEMIF_ERR_FILE_NOT_SOCK, /*!< file specified by socket filename
+ exists, but it's not socket */
+ MEMIF_ERR_NO_SHMFD, /*!< missing shm fd */
+ MEMIF_ERR_COOKIE, /*!< wrong cookie on ring */
+ MEMIF_ERR_NOBUF_RING, /*!< ring buffer full */
+ MEMIF_ERR_NOBUF, /*!< not enough memif buffers */
+ MEMIF_ERR_NOBUF_DET, /*!< memif details needs larger buffer */
+ MEMIF_ERR_INT_WRITE, /*!< send interrupt error */
+ MEMIF_ERR_MFMSG, /*!< malformed msg received */
+ MEMIF_ERR_QID, /*!< invalid queue id */
+/* MEMIF PROTO ERRORS */
+ MEMIF_ERR_PROTO, /*!< incompatible protocol version */
+ MEMIF_ERR_ID, /*!< unmatched interface id */
+ MEMIF_ERR_ACCSLAVE, /*!< slave cannot accept connection requests */
+ MEMIF_ERR_ALRCONN, /*!< memif is already connected */
+ MEMIF_ERR_MODE, /*!< mode mismatch */
+ MEMIF_ERR_SECRET, /*!< secret mismatch */
+ MEMIF_ERR_NOSECRET, /*!< secret required */
+ MEMIF_ERR_MAXREG, /*!< max region limit reached */
+ MEMIF_ERR_MAXRING, /*!< max ring limit reached */
+ MEMIF_ERR_NO_INTFD, /*!< missing interrupt fd */
+ MEMIF_ERR_DISCONNECT, /*!< disconenct received */
+ MEMIF_ERR_DISCONNECTED, /*!< peer interface disconnected */
+ MEMIF_ERR_UNKNOWN_MSG, /*!< unknown message type */
+} memif_err_t;
+
+/**
+ * @defgroup MEMIF_FD_EVENT Types of events that need to be watched for specific fd.
+ *
+ * @{
+ */
+
+/** user needs to set events that occured on fd and pass them to memif_control_fd_handler */
+#define MEMIF_FD_EVENT_READ (1 << 0)
+#define MEMIF_FD_EVENT_WRITE (1 << 1)
+/** inform libmemif that error occured on fd */
+#define MEMIF_FD_EVENT_ERROR (1 << 2)
+/** if set, informs that fd is going to be closed (user may want to stop watching for events on this fd) */
+#define MEMIF_FD_EVENT_DEL (1 << 3)
+/** update events */
+#define MEMIF_FD_EVENT_MOD (1 << 4)
+/** @} */
+
+/** *brief Memif connection handle
+ pointer of type void, pointing to internal structure
+*/
+typedef void *memif_conn_handle_t;
+/**
+ * @defgroup CALLBACKS Callback functions definitions
+ *
+ * @{
+ */
+
+/** \brief Memif control file descriptor update (callback function)
+ @param fd - new file descriptor to watch
+ @param events - event type(s) to watch for
+
+ This callback is called when there is new fd to watch for events on
+ or if fd is about to be closed (user mey want to stop watching for events on this fd).
+*/
+typedef int (memif_control_fd_update_t) (int fd, uint8_t events);
+
+/** \brief Memif connection status update (callback function)
+ @param conn - memif connection handle
+ @param private_ctx - private context
+
+ Informs user about connection status connected/disconnected.
+ On connected -> start watching for events on interrupt fd (optional).
+*/
+typedef int (memif_connection_update_t) (memif_conn_handle_t conn,
+ void *private_ctx);
+
+/** \brief Memif interrupt occured (callback function)
+ @param conn - memif connection handle
+ @param private_ctx - private context
+ @param qid - queue id on which interrupt occured
+
+ Called when event is received on interrupt fd.
+*/
+typedef int (memif_interrupt_t) (memif_conn_handle_t conn, void *private_ctx,
+ uint16_t qid);
+/** @} */
+
+/**
+ * @defgroup ARGS_N_BUFS Connection arguments and buffers
+ *
+ * @{
+ */
+
+/** \brief Memif connection arguments
+ @param socket_filename - socket filename
+ @param secret - otional parameter used as interface autenthication
+ @param num_s2m_rings - number of slave to master rings
+ @param num_m2s_rings - number of master to slave rings
+ @param buffer_size - size of buffer in shared memory
+ @param log2_ring_size - logarithm base 2 of ring size
+ @param is_master - 0 == master, 1 == slave
+ @param interface_id - id used to identify peer connection
+ @param interface_name - interface name
+ @param instance_name - application name
+ @param mode - 0 == ethernet, 1 == ip , 2 == punt/inject
+*/
+typedef struct
+{
+ uint8_t *socket_filename; /*!< default = /run/vpp/memif.sock */
+ uint8_t secret[24]; /*!< optional (interface authentication) */
+
+ uint8_t num_s2m_rings; /*!< default = 1 */
+ uint8_t num_m2s_rings; /*!< default = 1 */
+ uint16_t buffer_size; /*!< default = 2048 */
+ memif_log2_ring_size_t log2_ring_size; /*!< default = 10 (1024) */
+ uint8_t is_master;
+
+ memif_interface_id_t interface_id;
+ uint8_t interface_name[32];
+ uint8_t instance_name[32];
+ memif_interface_mode_t mode:8;
+} memif_conn_args_t;
+
+/*! memif receive mode */
+typedef enum
+{
+ MEMIF_RX_MODE_INTERRUPT = 0, /*!< interrupt mode */
+ MEMIF_RX_MODE_POLLING /*!< polling mode */
+} memif_rx_mode_t;
+
+/** \brief Memif buffer
+ @param desc_index - ring descriptor index
+ @param buffer_len - shared meory buffer length
+ @param data_len - data length
+ @param data - pointer to shared memory data
+*/
+typedef struct
+{
+ uint16_t desc_index;
+ uint32_t buffer_len;
+ uint32_t data_len;
+ void *data;
+} memif_buffer_t;
+/** @} */
+
+/**
+ * @defgroup MEMIF_DETAILS Memif details structs
+ *
+ * @{
+ */
+
+/** \brief Memif queue details
+ @param qid - queue id
+ @param ring_size - size of ring buffer in sharem memory
+ @param buffer_size - buffer size on sharem memory
+*/
+typedef struct
+{
+ uint8_t qid;
+ uint32_t ring_size;
+ uint16_t buffer_size;
+ /* add ring information */
+} memif_queue_details_t;
+
+/** \brief Memif details
+ @param if_name - interface name
+ @param inst_name - application name
+ @param remote_if_name - peer interface name
+ @param remote_inst_name - peer application name
+ @param id - connection id
+ @param secret - secret
+ @param role - 0 = master, 1 = slave
+ @param mode - 0 = ethernet, 1 = ip , 2 = punt/inject
+ @param socket_filename = socket filename
+ @param rx_queues_num - number of receive queues
+ @param tx_queues_num - number of transmit queues
+ @param rx_queues - struct containing receive queue details
+ @param tx_queues - struct containing transmit queue details
+ @param link_up_down - 1 = up (connected), 2 = down (disconnected)
+*/
+typedef struct
+{
+ uint8_t *if_name;
+ uint8_t *inst_name;
+ uint8_t *remote_if_name;
+ uint8_t *remote_inst_name;
+
+ uint32_t id;
+ uint8_t *secret; /* optional */
+ uint8_t role; /* 0 = master, 1 = slave */
+ uint8_t mode; /* 0 = ethernet, 1 = ip, 2 = punt/inject */
+ uint8_t *socket_filename;
+ uint8_t rx_queues_num;
+ uint8_t tx_queues_num;
+ memif_queue_details_t *rx_queues;
+ memif_queue_details_t *tx_queues;
+
+ uint8_t link_up_down; /* 1 = up, 0 = down */
+} memif_details_t;
+/** @} */
+
+/**
+ * @defgroup API_CALLS Api calls
+ *
+ * @{
+ */
+
+/** \biref Memif get queue event file descriptor
+ @param conn - memif connection handle
+ @param qid - queue id
+ @param[out] fd - returns event file descriptor
+
+ \return memif_err_t
+*/
+
+int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *fd);
+
+/** \brief Memif set rx mode
+ @param conn - memif connection handle
+ @param rx_mode - receive mode
+ @param qid - queue id
+
+ \return memif_err_t
+*/
+int memif_set_rx_mode (memif_conn_handle_t conn, memif_rx_mode_t rx_mode,
+ uint16_t qid);
+
+/** \brief Memif strerror
+ @param err_code - error code
+
+ Converts error code to error message.
+
+ \return Error string
+*/
+char *memif_strerror (int err_code);
+
+/** \brief Memif get details
+ @param conn - memif conenction handle
+ @param md - pointer to memif details struct
+ @param buf - buffer containing details strings
+ @param buflen - length of buffer
+
+ \return memif_err_t
+*/
+int memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
+ char *buf, ssize_t buflen);
+
+/** \brief Memif initialization
+ @param on_control_fd_update - if control fd updates inform user to watch new fd
+ @param app_name - application name
+
+ if param on_control_fd_update is set to NULL,
+ libmemif will handle file descriptor event polling
+ if a valid callback is set, file descriptor event polling needs to be done by
+ user application, all file descriptors and event types will be passed in
+ this callback to user application
+
+ Initialize internal libmemif structures. Create timerfd (used to periodically request connection by
+ disconnected memifs in slave mode, with no additional API call). This fd is passed to user with memif_control_fd_update_t
+ timer is inactive at this state. It activates with if there is at least one memif in slave mode.
+
+ \return memif_err_t
+*/
+int memif_init (memif_control_fd_update_t * on_control_fd_update,
+ char *app_name);
+
+/** \brief Memif cleanup
+
+ Free libmemif internal allocations.
+
+ \return 0
+*/
+int memif_cleanup ();
+
+/** \brief Memory interface create function
+ @param conn - connection handle for user app
+ @param args - memory interface connection arguments
+ @param on_connect - inform user about connected status
+ @param on_disconnect - inform user about disconnected status
+ @param on_interrupt - informs user about interrupt, if set to null user will not be notified about interrupt, user can use memif_get_queue_efd call to get interrupt fd to poll for events
+ @param private_ctx - private contex passed back to user with callback
+
+ Creates memory interface.
+
+ SLAVE-MODE -
+ Start timer that will send events to timerfd. If this fd is passed to memif_control_fd_handler
+ every disconnected memif in slave mode will send connection request.
+ On success new fd is passed to user with memif_control_fd_update_t.
+
+ MASTER-MODE -
+ Create listener socket and pass fd to user with memif_cntrol_fd_update_t.
+ If this fd is passed to memif_control_fd_handler accept will be called and
+ new fd will be passed to user with memif_control_fd_update_t.
+
+
+ \return memif_err_t
+*/
+int memif_create (memif_conn_handle_t * conn, memif_conn_args_t * args,
+ memif_connection_update_t * on_connect,
+ memif_connection_update_t * on_disconnect,
+ memif_interrupt_t * on_interrupt, void *private_ctx);
+
+/** \brief Memif control file descriptor handler
+ @param fd - file descriptor on which the event occured
+ @param events - event type(s) that occured
+
+ If event occures on any control fd, call memif_control_fd_handler.
+ Internal - lib will "identify" fd (timerfd, lsitener, control) and handle event accordingly.
+
+ FD-TYPE -
+ TIMERFD -
+ Every disconnected memif in slave mode will request connection.
+ LISTENER or CONTROL -
+ Handle socket messaging (internal connection establishment).
+ INTERRUPT -
+ Call on_interrupt callback (if set).
+
+ \return memif_err_t
+
+*/
+int memif_control_fd_handler (int fd, uint8_t events);
+
+/** \brief Memif delete
+ @param conn - pointer to memif connection handle
+
+
+ disconnect session (free queues and regions, close file descriptors, unmap shared memory)
+ set connection handle to NULL, to avoid possible double free
+
+ \return memif_err_t
+*/
+int memif_delete (memif_conn_handle_t * conn);
+
+/** \brief Memif buffer alloc
+ @param conn - memif conenction handle
+ @param qid - number indentifying queue
+ @param bufs - memif buffers
+ @param count - number of memif buffers to allocate
+ @param count_out - returns number of allocated buffers
+ @param size - minimal buffer size, 0 = standard buffer size
+
+ \return memif_err_t
+*/
+int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
+ memif_buffer_t * bufs, uint16_t count,
+ uint16_t * count_out, uint16_t size);
+
+/** \brief Memif buffer free
+ @param conn - memif conenction handle
+ @param qid - number indentifying queue
+ @param bufs - memif buffers
+ @param count - number of memif buffers to free
+ @param count_out - returns number of freed buffers
+
+ \return memif_err_t
+*/
+int memif_buffer_free (memif_conn_handle_t conn, uint16_t qid,
+ memif_buffer_t * bufs, uint16_t count,
+ uint16_t * count_out);
+
+/** \brief Memif transmit buffer burst
+ @param conn - memif conenction handle
+ @param qid - number indentifying queue
+ @param bufs - memif buffers
+ @param count - number of memif buffers to transmit
+ @param tx - returns number of transmitted buffers
+
+ \return memif_err_t
+*/
+int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
+ memif_buffer_t * bufs, uint16_t count, uint16_t * tx);
+
+/** \brief Memif receive buffer burst
+ @param conn - memif conenction handle
+ @param qid - number indentifying queue
+ @param bufs - memif buffers
+ @param count - number of memif buffers to receive
+ @param rx - returns number of received buffers
+
+ \return memif_err_t
+*/
+int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
+ memif_buffer_t * bufs, uint16_t count, uint16_t * rx);
+
+/** \brief Memif poll event
+ @param timeout - timeout in seconds
+
+ Passive event polling -
+ timeout = 0 - dont wait for event, check event queue if there is an event and return.
+ timeout = -1 - wait until event
+
+ \return memif_err_t
+*/
+int memif_poll_event (int timeout);
+/** @} */
+
+#endif /* _LIBMEMIF_H_ */
diff --git a/extras/libmemif/src/main.c b/extras/libmemif/src/main.c
new file mode 100644
index 00000000..49427670
--- /dev/null
+++ b/extras/libmemif/src/main.c
@@ -0,0 +1,2095 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#include <stdint.h>
+#include <net/if.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/uio.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdio.h>
+#include <netdb.h>
+#include <linux/ip.h>
+#include <linux/icmp.h>
+#include <arpa/inet.h>
+#include <stdlib.h>
+#include <netinet/if_ether.h>
+#include <net/if_arp.h>
+#include <asm/byteorder.h>
+#include <byteswap.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/eventfd.h>
+#include <sys/timerfd.h>
+#include <sys/epoll.h>
+#include <signal.h>
+
+/* memif protocol msg, ring and descriptor definitions */
+#include <memif.h>
+/* memif api */
+#include <libmemif.h>
+/* socket messaging functions */
+#include <socket.h>
+/* private structs and functions */
+#include <memif_private.h>
+
+#define ERRLIST_LEN 36
+#define MAX_ERRBUF_LEN 256
+
+#if __x86_x64__
+#define MEMIF_MEMORY_BARRIER() __builtin_ia32_sfence ()
+#else
+#define MEMIF_MEORY_BARRIER() __sync_synchronize ()
+#endif /* __x86_x64__ */
+
+libmemif_main_t libmemif_main;
+int memif_epfd;
+
+static char memif_buf[MAX_ERRBUF_LEN];
+
+const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */
+ "Success.",
+ /* MEMIF_ERR_SYSCALL */
+ "Unspecified syscall error (build with -DMEMIF_DBG or make debug).",
+ /* MEMIF_ERR_ACCES */
+ "Permission to resoure denied.",
+ /* MEMIF_ERR_NO_FILE */
+ "Socket file does not exist",
+ /* MEMIF_ERR_FILE_LIMIT */
+ "System limit on total numer of open files reached.",
+ /* MEMIF_ERR_PROC_FILE_LIMIT */
+ "Per-process limit on total number of open files reached.",
+ /* MEMIF_ERR_ALREADY */
+ "Connection already requested.",
+ /* MEMIF_ERR_AGAIN */
+ "File descriptor refers to file other than socket, or operation would block.",
+ /* MEMIF_ERR_BAD_FD */
+ "Bad file descriptor.",
+ /* MEMIF_ERR_NOMEM */
+ "Out of memory.",
+ /* MEMIF_ERR_INVAL_ARG */
+ "Invalid argument.",
+ /* MEMIF_ERR_NOCONN */
+ "Memif connection handle does not point to existing conenction",
+ /* MEMIF_ERR_CONN */
+ "Memif connection handle points to existing connection",
+ /* MEMIF_ERR_CB_FDUPDATE */
+ "Callback memif_control_fd_update_t returned error",
+ /* MEMIF_ERR_FILE_NOT_SOCK */
+ "File specified by socket filename exists and is not socket.",
+ /* MEMIF_ERR_NO_SHMFD */
+ "Missing shared memory file descriptor. (internal error)",
+ /* MEMIF_ERR_COOKIE */
+ "Invalid cookie on ring. (internal error)",
+ /* MEMIF_ERR_NOBUF_RING */
+ "Ring buffer full.",
+ /* MEMIF_ERR_NOBUF */
+ "Not enough memif buffers. There are unreceived data in shared memory.",
+ /* MEMIF_ERR_NOBUF_DET */
+ "Not enough space for memif details in suplied buffer. String data might be malformed.",
+ /* MEMIF_ERR_INT_WRITE */
+ "Send interrupt error.",
+ /* MEMIF_ERR_MFMSG */
+ "Malformed message received on control channel.",
+ /* MEMIF_ERR_QID */
+ "Invalid queue id",
+ /* MEMIF_ERR_PROTO */
+ "Incompatible memory interface protocol version.",
+ /* MEMIF_ERR_ID */
+ "Unmatched interface id.",
+ /* MEMIF_ERR_ACCSLAVE */
+ "Slave cannot accept connection reqest.",
+ /* MEMIF_ERR_ALRCONN */
+ "Interface is already connected.",
+ /* MEMIF_ERR_MODE */
+ "Mode mismatch.",
+ /* MEMIF_ERR_SECRET */
+ "Secret mismatch.",
+ /* MEMIF_ERR_NOSECRET */
+ "Secret required.",
+ /* MEMIF_ERR_MAXREG */
+ "Limit on total number of regions reached.",
+ /* MEMIF_ERR_MAXRING */
+ "Limit on total number of ring reached.",
+ /* MEMIF_ERR_NO_INTFD */
+ "Missing interrupt file descriptor. (internal error)",
+ /* MEMIF_ERR_DISCONNECT */
+ "Interface received disconnect request.",
+ /* MEMIF_ERR_DISCONNECTED */
+ "Interface is disconnected.",
+ /* MEMIF_ERR_UNKNOWN_MSG */
+ "Unknown message type received on control channel. (internal error)"
+};
+
+#define MEMIF_ERR_UNDEFINED "undefined error"
+
+char *
+memif_strerror (int err_code)
+{
+ if (err_code >= ERRLIST_LEN)
+ {
+ strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED));
+ memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0';
+ }
+ else
+ {
+ strncpy (memif_buf, memif_errlist[err_code],
+ strlen (memif_errlist[err_code]));
+ memif_buf[strlen (memif_errlist[err_code])] = '\0';
+ }
+ return memif_buf;
+}
+
+#define DBG_TX_BUF (0)
+#define DBG_RX_BUF (1)
+
+#ifdef MEMIF_DBG_SHM
+static void
+print_bytes (void *data, uint16_t len, uint8_t q)
+{
+ if (q == DBG_TX_BUF)
+ printf ("\nTX:\n\t");
+ else
+ printf ("\nRX:\n\t");
+ int i;
+ for (i = 0; i < len; i++)
+ {
+ if (i % 8 == 0)
+ printf ("\n%d:\t", i);
+ printf ("%02X ", ((uint8_t *) (data))[i]);
+ }
+ printf ("\n\n");
+}
+#endif /* MEMIF_DBG_SHM */
+
+int
+memif_syscall_error_handler (int err_code)
+{
+ DBG_UNIX ("%s", strerror (err_code));
+
+ if (err_code == 0)
+ return MEMIF_ERR_SUCCESS;
+ if (err_code == EACCES)
+ return MEMIF_ERR_ACCES;
+ if (err_code == ENFILE)
+ return MEMIF_ERR_FILE_LIMIT;
+ if (err_code == EMFILE)
+ return MEMIF_ERR_PROC_FILE_LIMIT;
+ if (err_code == ENOMEM)
+ return MEMIF_ERR_NOMEM;
+/* connection refused if master dows not exist
+ this error would spam the user until master was created */
+ if (err_code == ECONNREFUSED)
+ return MEMIF_ERR_SUCCESS;
+ if (err_code == EALREADY)
+ return MEMIF_ERR_ALREADY;
+ if (err_code == EAGAIN)
+ return MEMIF_ERR_AGAIN;
+ if (err_code == EBADF)
+ return MEMIF_ERR_BAD_FD;
+ if (err_code == ENOENT)
+ return MEMIF_ERR_NO_FILE;
+
+ /* other syscall errors */
+ return MEMIF_ERR_SYSCALL;
+}
+
+static int
+memif_add_epoll_fd (int fd, uint32_t events)
+{
+ if (fd < 0)
+ {
+ DBG ("invalid fd %d", fd);
+ return -1;
+ }
+ struct epoll_event evt;
+ memset (&evt, 0, sizeof (evt));
+ evt.events = events;
+ evt.data.fd = fd;
+ if (epoll_ctl (memif_epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
+ {
+ DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
+ return -1;
+ }
+ DBG ("fd %d added to epoll", fd);
+ return 0;
+}
+
+static int
+memif_mod_epoll_fd (int fd, uint32_t events)
+{
+ if (fd < 0)
+ {
+ DBG ("invalid fd %d", fd);
+ return -1;
+ }
+ struct epoll_event evt;
+ memset (&evt, 0, sizeof (evt));
+ evt.events = events;
+ evt.data.fd = fd;
+ if (epoll_ctl (memif_epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
+ {
+ DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
+ return -1;
+ }
+ DBG ("fd %d moddified on epoll", fd);
+ return 0;
+}
+
+static int
+memif_del_epoll_fd (int fd)
+{
+ if (fd < 0)
+ {
+ DBG ("invalid fd %d", fd);
+ return -1;
+ }
+ struct epoll_event evt;
+ memset (&evt, 0, sizeof (evt));
+ if (epoll_ctl (memif_epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
+ {
+ DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
+ return -1;
+ }
+ DBG ("fd %d removed from epoll", fd);
+ return 0;
+}
+
+int
+memif_control_fd_update (int fd, uint8_t events)
+{
+ if (events & MEMIF_FD_EVENT_DEL)
+ return memif_del_epoll_fd (fd);
+
+ uint32_t evt = 0;
+ if (events & MEMIF_FD_EVENT_READ)
+ evt |= EPOLLIN;
+ if (events & MEMIF_FD_EVENT_WRITE)
+ evt |= EPOLLOUT;
+
+ if (events & MEMIF_FD_EVENT_MOD)
+ return memif_mod_epoll_fd (fd, evt);
+
+ return memif_add_epoll_fd (fd, evt);
+}
+
+int
+add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list, uint16_t * len)
+{
+ libmemif_main_t *lm = &libmemif_main;
+
+ int i;
+ for (i = 0; i < *len; i++)
+ {
+ if ((*list)[i].data_struct == NULL)
+ {
+ (*list)[i].key = e->key;
+ (*list)[i].data_struct = e->data_struct;
+ return i;
+ }
+ }
+ memif_list_elt_t *tmp;
+ tmp = realloc (*list, sizeof (memif_list_elt_t) * *len * 2);
+ if (tmp == NULL)
+ return -1;
+
+ for (i = *len; i < *len * 2; i++)
+ {
+ tmp[i].key = -1;
+ tmp[i].data_struct = NULL;
+ }
+
+ tmp[*len].key = e->key;
+ tmp[*len].data_struct = e->data_struct;
+ i = *len;
+ *len = *len * 2;
+ *list = tmp;
+
+ return i;
+}
+
+int
+get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list, uint16_t len,
+ int key)
+{
+ if (key == -1)
+ {
+ *e = NULL;
+ return -1;
+ }
+ int i;
+ for (i = 0; i < len; i++)
+ {
+ if (list[i].key == key)
+ {
+ *e = &list[i];
+ return 0;
+ }
+ }
+ *e = NULL;
+ return -1;
+}
+
+/* does not free memory, only marks element as free */
+int
+free_list_elt (memif_list_elt_t * list, uint16_t len, int key)
+{
+ int i;
+ for (i = 0; i < len; i++)
+ {
+ if (list[i].key == key)
+ {
+ list[i].key = -1;
+ list[i].data_struct = NULL;
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+int
+free_list_elt_ctx (memif_list_elt_t * list, uint16_t len,
+ memif_connection_t * ctx)
+{
+ int i;
+ for (i = 0; i < len; i++)
+ {
+ if (list[i].key == -1)
+ {
+ if (list[i].data_struct == ctx)
+ {
+ list[i].data_struct = NULL;
+ return 0;
+ }
+ }
+ }
+
+ return -1;
+}
+
+static void
+memif_control_fd_update_register (memif_control_fd_update_t * cb)
+{
+ libmemif_main_t *lm = &libmemif_main;
+ lm->control_fd_update = cb;
+}
+
+int
+memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name)
+{
+ int err = MEMIF_ERR_SUCCESS; /* 0 */
+ libmemif_main_t *lm = &libmemif_main;
+
+ if (app_name)
+ {
+ lm->app_name = malloc (strlen (app_name) + sizeof (char));
+ memset (lm->app_name, 0, strlen (app_name) + sizeof (char));
+ strncpy ((char *) lm->app_name, app_name, strlen (app_name));
+ }
+ else
+ {
+ lm->app_name = malloc (strlen (MEMIF_DEFAULT_APP_NAME) + sizeof (char));
+ memset (lm->app_name, 0, strlen (app_name) + sizeof (char));
+ strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME,
+ strlen (MEMIF_DEFAULT_APP_NAME));
+ }
+
+ /* register control fd update callback */
+ if (on_control_fd_update != NULL)
+ memif_control_fd_update_register (on_control_fd_update);
+ else
+ {
+ memif_epfd = epoll_create (1);
+ memif_control_fd_update_register (memif_control_fd_update);
+ DBG ("libmemif event polling initialized");
+ }
+
+ memset (&lm->ms, 0, sizeof (memif_socket_t));
+
+ lm->control_list_len = 2;
+ lm->interrupt_list_len = 2;
+ lm->listener_list_len = 1;
+ lm->pending_list_len = 1;
+
+ lm->control_list =
+ malloc (sizeof (memif_list_elt_t) * lm->control_list_len);
+ lm->interrupt_list =
+ malloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len);
+ lm->listener_list =
+ malloc (sizeof (memif_list_elt_t) * lm->listener_list_len);
+ lm->pending_list =
+ malloc (sizeof (memif_list_elt_t) * lm->pending_list_len);
+
+ int i;
+ for (i = 0; i < lm->control_list_len; i++)
+ {
+ lm->control_list[i].key = -1;
+ lm->control_list[i].data_struct = NULL;
+ }
+ for (i = 0; i < lm->interrupt_list_len; i++)
+ {
+ lm->interrupt_list[i].key = -1;
+ lm->interrupt_list[i].data_struct = NULL;
+ }
+ for (i = 0; i < lm->listener_list_len; i++)
+ {
+ lm->listener_list[i].key = -1;
+ lm->listener_list[i].data_struct = NULL;
+ }
+ for (i = 0; i < lm->pending_list_len; i++)
+ {
+ lm->pending_list[i].key = -1;
+ lm->pending_list[i].data_struct = NULL;
+ }
+
+ lm->disconn_slaves = 0;
+
+ lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK);
+ if (lm->timerfd < 0)
+ {
+ err = errno;
+ DBG ("timerfd: %s", strerror (err));
+ return memif_syscall_error_handler (err);
+ }
+
+ lm->arm.it_value.tv_sec = 2;
+ lm->arm.it_value.tv_nsec = 0;
+ lm->arm.it_interval.tv_sec = 2;
+ lm->arm.it_interval.tv_nsec = 0;
+ memset (&lm->disarm, 0, sizeof (lm->disarm));
+
+ if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ) < 0)
+ {
+ DBG ("callback type memif_control_fd_update_t error!");
+ return MEMIF_ERR_CB_FDUPDATE;
+ }
+
+ return 0;
+}
+
+static inline memif_ring_t *
+memif_get_ring (memif_connection_t * conn, memif_ring_type_t type,
+ uint16_t ring_num)
+{
+ if (&conn->regions[0] == NULL)
+ return NULL;
+ void *p = conn->regions[0].shm;
+ int ring_size =
+ sizeof (memif_ring_t) +
+ sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size);
+ p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size;
+
+ return (memif_ring_t *) p;
+}
+
+int
+memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode,
+ uint16_t qid)
+{
+ memif_connection_t *conn = (memif_connection_t *) c;
+ if (conn == NULL)
+ return MEMIF_ERR_NOCONN;
+ uint8_t num =
+ (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->run_args.
+ num_m2s_rings;
+ if (qid >= num)
+ return MEMIF_ERR_QID;
+
+ conn->rx_queues[qid].ring->flags = rx_mode;
+ DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags);
+ return MEMIF_ERR_SUCCESS;
+}
+
+int
+memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
+ memif_connection_update_t * on_connect,
+ memif_connection_update_t * on_disconnect,
+ memif_interrupt_t * on_interrupt, void *private_ctx)
+{
+ int err, i, index, sockfd = -1;
+ memif_list_elt_t list_elt;
+ memif_connection_t *conn = (memif_connection_t *) * c;
+ if (conn != NULL)
+ {
+ DBG ("This handle already points to existing memif.");
+ return MEMIF_ERR_CONN;
+ }
+ conn = (memif_connection_t *) malloc (sizeof (memif_connection_t));
+ if (conn == NULL)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+ memset (conn, 0, sizeof (memif_connection_t));
+
+ libmemif_main_t *lm = &libmemif_main;
+
+ conn->args.interface_id = args->interface_id;
+
+ if (args->log2_ring_size == 0)
+ args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE;
+ if (args->buffer_size == 0)
+ args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
+ if (args->num_s2m_rings == 0)
+ args->num_s2m_rings = MEMIF_DEFAULT_TX_QUEUES;
+ if (args->num_m2s_rings == 0)
+ args->num_m2s_rings = MEMIF_DEFAULT_RX_QUEUES;
+
+ conn->args.num_s2m_rings = args->num_s2m_rings;
+ conn->args.num_m2s_rings = args->num_m2s_rings;
+ conn->args.buffer_size = args->buffer_size;
+ conn->args.log2_ring_size = args->log2_ring_size;
+ conn->args.is_master = args->is_master;
+ conn->args.mode = args->mode;
+ conn->msg_queue = NULL;
+ conn->regions = NULL;
+ conn->tx_queues = NULL;
+ conn->rx_queues = NULL;
+ conn->fd = -1;
+ conn->on_connect = on_connect;
+ conn->on_disconnect = on_disconnect;
+ conn->on_interrupt = on_interrupt;
+ conn->private_ctx = private_ctx;
+ memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t));
+
+ uint8_t l = strlen ((char *) args->interface_name);
+ strncpy ((char *) conn->args.interface_name, (char *) args->interface_name,
+ l);
+
+ l = strlen ((char *) args->instance_name);
+ strncpy ((char *) conn->args.instance_name, (char *) args->instance_name,
+ l);
+
+ /* allocate and initialize socket_filename so it can be copyed to sun_path
+ without memory leaks */
+ conn->args.socket_filename = malloc (sizeof (char *) * 108);
+ memset (conn->args.socket_filename, 0, 108 * sizeof (char *));
+
+ if (args->socket_filename)
+ {
+ if (conn->args.socket_filename == NULL)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+ strncpy ((char *) conn->args.socket_filename,
+ (char *) args->socket_filename,
+ strlen ((char *) args->socket_filename));
+ }
+ else
+ {
+ uint16_t sdl = strlen (MEMIF_DEFAULT_SOCKET_DIR);
+ uint16_t sfl = strlen (MEMIF_DEFAULT_SOCKET_FILENAME);
+ if (conn->args.socket_filename == NULL)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+ strncpy ((char *) conn->args.socket_filename,
+ MEMIF_DEFAULT_SOCKET_DIR, sdl);
+ conn->args.socket_filename[sdl] = '/';
+ strncpy ((char *) (conn->args.socket_filename + 1 + sdl),
+ MEMIF_DEFAULT_SOCKET_FILENAME, sfl);
+ }
+
+ if (args->secret)
+ {
+ l = strlen ((char *) args->secret);
+ strncpy ((char *) conn->args.secret, (char *) args->secret, l);
+ }
+
+ if (conn->args.is_master)
+ {
+ conn->run_args.buffer_size = conn->args.buffer_size;
+ memif_socket_t *ms;
+ memif_list_elt_t elt;
+ for (i = 0; i < lm->listener_list_len; i++)
+ {
+ if ((ms =
+ (memif_socket_t *) lm->listener_list[i].data_struct) != NULL)
+ {
+ if (strncmp
+ ((char *) ms->filename, (char *) conn->args.socket_filename,
+ strlen ((char *) ms->filename)) == 0)
+ {
+ /* add interface to listener socket */
+ elt.key = conn->args.interface_id;
+ *c = elt.data_struct = conn;
+ add_list_elt (&elt, &ms->interface_list,
+ &ms->interface_list_len);
+ ms->use_count++;
+ conn->listener_fd = ms->fd;
+ break;
+ }
+ }
+ else
+ {
+ struct stat file_stat;
+ if (stat ((char *) conn->args.socket_filename, &file_stat) == 0)
+ {
+ if (S_ISSOCK (file_stat.st_mode))
+ unlink ((char *) conn->args.socket_filename);
+ else
+ return memif_syscall_error_handler (errno);
+ }
+ DBG ("creating socket file");
+ ms = malloc (sizeof (memif_socket_t));
+ ms->filename =
+ malloc (strlen ((char *) conn->args.socket_filename) +
+ sizeof (char));
+ memset (ms->filename, 0,
+ strlen ((char *) conn->args.socket_filename) +
+ sizeof (char));
+ strncpy ((char *) ms->filename,
+ (char *) conn->args.socket_filename,
+ strlen ((char *) conn->args.socket_filename));
+ ms->interface_list_len = 1;
+ ms->interface_list =
+ malloc (sizeof (memif_list_elt_t) * ms->interface_list_len);
+ ms->interface_list[0].key = -1;
+ ms->interface_list[0].data_struct = NULL;
+ struct sockaddr_un un = { 0 };
+ int on = 1;
+
+ ms->fd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
+ if (ms->fd < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+ DBG ("socket %d created", ms->fd);
+ un.sun_family = AF_UNIX;
+ strncpy ((char *) un.sun_path, (char *) ms->filename,
+ sizeof (un.sun_path) - 1);
+ DBG ("sockopt");
+ if (setsockopt
+ (ms->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+ DBG ("bind");
+ if (bind (ms->fd, (struct sockaddr *) &un, sizeof (un)) < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+ DBG ("listen");
+ if (listen (ms->fd, 1) < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+ DBG ("stat");
+ if (stat ((char *) ms->filename, &file_stat) < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+
+ /* add interface to listener socket */
+ elt.key = conn->args.interface_id;
+ *c = elt.data_struct = conn;
+ add_list_elt (&elt, &ms->interface_list,
+ &ms->interface_list_len);
+ ms->use_count = 1;
+ conn->listener_fd = ms->fd;
+
+ /* add listener socket to libmemif main */
+ elt.key = ms->fd;
+ elt.data_struct = ms;
+ add_list_elt (&elt, &lm->listener_list, &lm->listener_list_len);
+ lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ);
+ break;
+ }
+ }
+ }
+ else
+ {
+ if (lm->disconn_slaves == 0)
+ {
+ if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+ }
+
+ lm->disconn_slaves++;
+
+ list_elt.key = -1;
+ *c = list_elt.data_struct = conn;
+ if ((index =
+ add_list_elt (&list_elt, &lm->control_list,
+ &lm->control_list_len)) < 0)
+ {
+ err = MEMIF_ERR_NOMEM;
+ goto error;
+ }
+ }
+
+ conn->index = index;
+
+ return 0;
+
+error:
+ if (sockfd > 0)
+ close (sockfd);
+ sockfd = -1;
+ if (conn->args.socket_filename)
+ free (conn->args.socket_filename);
+ if (conn != NULL)
+ free (conn);
+ *c = conn = NULL;
+ return err;
+}
+
+int
+memif_control_fd_handler (int fd, uint8_t events)
+{
+ int i, rv, sockfd = -1, err = MEMIF_ERR_SUCCESS; /* 0 */
+ uint16_t num;
+ memif_list_elt_t *e = NULL;
+ memif_connection_t *conn;
+ libmemif_main_t *lm = &libmemif_main;
+ if (fd == lm->timerfd)
+ {
+ uint64_t b;
+ ssize_t size;
+ size = read (fd, &b, sizeof (b));
+ for (i = 0; i < lm->control_list_len; i++)
+ {
+ if ((lm->control_list[i].key < 0)
+ && (lm->control_list[i].data_struct != NULL))
+ {
+ conn = lm->control_list[i].data_struct;
+ if (conn->args.is_master)
+ continue;
+
+ struct sockaddr_un sun;
+ sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0);
+ if (sockfd < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+
+ sun.sun_family = AF_UNIX;
+
+ strncpy (sun.sun_path, conn->args.socket_filename,
+ sizeof (sun.sun_path) - 1);
+
+ if (connect (sockfd, (struct sockaddr *) &sun,
+ sizeof (struct sockaddr_un)) == 0)
+ {
+ conn->fd = sockfd;
+ conn->read_fn = memif_conn_fd_read_ready;
+ conn->write_fn = memif_conn_fd_write_ready;
+ conn->error_fn = memif_conn_fd_error;
+
+ lm->control_list[conn->index].key = conn->fd;
+
+ lm->control_fd_update (sockfd,
+ MEMIF_FD_EVENT_READ |
+ MEMIF_FD_EVENT_WRITE);
+
+ lm->disconn_slaves--;
+ if (lm->disconn_slaves == 0)
+ {
+ if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL)
+ < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+ }
+ }
+ else
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+ }
+ }
+ }
+ else
+ {
+ get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd);
+ if (e != NULL)
+ {
+ if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL)
+ {
+ num =
+ (((memif_connection_t *) e->data_struct)->args.
+ is_master) ? ((memif_connection_t *) e->data_struct)->
+ run_args.num_s2m_rings : ((memif_connection_t *) e->
+ data_struct)->run_args.
+ num_m2s_rings;
+ for (i = 0; i < num; i++)
+ {
+ if (((memif_connection_t *) e->data_struct)->rx_queues[i].
+ int_fd == fd)
+ {
+ ((memif_connection_t *) e->
+ data_struct)->on_interrupt ((void *) e->data_struct,
+ ((memif_connection_t *)
+ e->data_struct)->
+ private_ctx, i);
+ return MEMIF_ERR_SUCCESS;
+ }
+ }
+ }
+ return MEMIF_ERR_SUCCESS;
+ }
+ get_list_elt (&e, lm->listener_list, lm->listener_list_len, fd);
+ if (e != NULL)
+ {
+ memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct);
+ return MEMIF_ERR_SUCCESS;
+ }
+
+ get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd);
+ if (e != NULL)
+ {
+ memif_read_ready (fd);
+ return MEMIF_ERR_SUCCESS;
+ }
+
+ get_list_elt (&e, lm->control_list, lm->control_list_len, fd);
+ if (e != NULL)
+ {
+ if (events & MEMIF_FD_EVENT_READ)
+ {
+ err =
+ ((memif_connection_t *) e->data_struct)->read_fn (e->
+ data_struct);
+ if (err != MEMIF_ERR_SUCCESS)
+ return err;
+ }
+ if (events & MEMIF_FD_EVENT_WRITE)
+ {
+ err =
+ ((memif_connection_t *) e->data_struct)->write_fn (e->
+ data_struct);
+ if (err != MEMIF_ERR_SUCCESS)
+ return err;
+ }
+ if (events & MEMIF_FD_EVENT_ERROR)
+ {
+ err =
+ ((memif_connection_t *) e->data_struct)->error_fn (e->
+ data_struct);
+ if (err != MEMIF_ERR_SUCCESS)
+ return err;
+ }
+ }
+ }
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+
+error:
+ if (sockfd > 0)
+ close (sockfd);
+ sockfd = -1;
+ return err;
+}
+
+int
+memif_poll_event (int timeout)
+{
+ libmemif_main_t *lm = &libmemif_main;
+ memif_list_elt_t *elt;
+ struct epoll_event evt, *e;
+ int en = 0, err = MEMIF_ERR_SUCCESS, i = 0; /* 0 */
+ uint16_t num;
+ uint32_t events = 0;
+ memset (&evt, 0, sizeof (evt));
+ evt.events = EPOLLIN | EPOLLOUT;
+ sigset_t sigset;
+ sigemptyset (&sigset);
+ en = epoll_pwait (memif_epfd, &evt, 1, timeout, &sigset);
+ if (en < 0)
+ {
+ DBG ("epoll_pwait: %s", strerror (errno));
+ return -1;
+ }
+ if (en > 0)
+ {
+ if (evt.events & EPOLLIN)
+ events |= MEMIF_FD_EVENT_READ;
+ if (evt.events & EPOLLOUT)
+ events |= MEMIF_FD_EVENT_WRITE;
+ if (evt.events & EPOLLERR)
+ events |= MEMIF_FD_EVENT_ERROR;
+ err = memif_control_fd_handler (evt.data.fd, events);
+ return err;
+ }
+ return 0;
+}
+
+static void
+memif_msg_queue_free (memif_msg_queue_elt_t ** e)
+{
+ if (*e == NULL)
+ return;
+ memif_msg_queue_free (&(*e)->next);
+ free (*e);
+ *e = NULL;
+ return;
+}
+
+/* send disconnect msg and close interface */
+int
+memif_disconnect_internal (memif_connection_t * c)
+{
+ if (c == NULL)
+ {
+ DBG ("no connection");
+ return MEMIF_ERR_NOCONN;
+ }
+ uint16_t num;
+ int err = MEMIF_ERR_SUCCESS, i; /* 0 */
+ memif_queue_t *mq;
+ libmemif_main_t *lm = &libmemif_main;
+ memif_list_elt_t *e;
+
+ c->on_disconnect ((void *) c, c->private_ctx);
+
+ if (c->fd > 0)
+ {
+ memif_msg_send_disconnect (c->fd, "interface deleted", 0);
+ lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL);
+ close (c->fd);
+ }
+ get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd);
+ if (e != NULL)
+ {
+ if (c->args.is_master)
+ free_list_elt (lm->control_list, lm->control_list_len, c->fd);
+ e->key = c->fd = -1;
+ }
+
+ if (c->tx_queues != NULL)
+ {
+ num =
+ (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
+ num_s2m_rings;
+ for (i = 0; i < num; i++)
+ {
+ mq = &c->tx_queues[i];
+ if (mq != NULL)
+ {
+ if (mq->int_fd > 0)
+ close (mq->int_fd);
+ free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
+ mq->int_fd);
+ mq->int_fd = -1;
+ }
+ }
+ free (c->tx_queues);
+ c->tx_queues = NULL;
+ }
+
+ if (c->rx_queues != NULL)
+ {
+ num =
+ (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
+ num_m2s_rings;
+ for (i = 0; i < num; i++)
+ {
+ mq = &c->rx_queues[i];
+ if (mq != NULL)
+ {
+ if (mq->int_fd > 0)
+ {
+ if (c->on_interrupt != NULL)
+ lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL);
+ close (mq->int_fd);
+ }
+ free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
+ mq->int_fd);
+ mq->int_fd = -1;
+ }
+ }
+ free (c->rx_queues);
+ c->rx_queues = NULL;
+ }
+
+ if (c->regions != NULL)
+ {
+ if (munmap (c->regions[0].shm, c->regions[0].region_size) < 0)
+ return memif_syscall_error_handler (errno);
+ if (c->regions[0].fd > 0)
+ close (c->regions[0].fd);
+ c->regions[0].fd = -1;
+ free (c->regions);
+ c->regions = NULL;
+ }
+
+ memset (&c->run_args, 0, sizeof (memif_conn_run_args_t));
+
+ memif_msg_queue_free (&c->msg_queue);
+
+ if (!(c->args.is_master))
+ {
+ if (lm->disconn_slaves == 0)
+ {
+ if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ DBG_UNIX ("timerfd_settime: arm");
+ }
+ }
+ lm->disconn_slaves++;
+ }
+
+ return err;
+}
+
+int
+memif_delete (memif_conn_handle_t * conn)
+{
+ memif_connection_t *c = (memif_connection_t *) * conn;
+ if (c == NULL)
+ {
+ DBG ("no connection");
+ return MEMIF_ERR_NOCONN;
+ }
+ libmemif_main_t *lm = &libmemif_main;
+ memif_list_elt_t *e = NULL;
+ memif_socket_t *ms = NULL;
+
+ int err = MEMIF_ERR_SUCCESS;
+
+ if (c->fd > 0)
+ {
+ DBG ("DISCONNECTING");
+ err = memif_disconnect_internal (c);
+ if (err == MEMIF_ERR_NOCONN)
+ return err;
+ }
+
+ free_list_elt_ctx (lm->control_list, lm->control_list_len, c);
+
+ if (c->args.is_master)
+ {
+ get_list_elt (&e, lm->listener_list, lm->listener_list_len,
+ c->listener_fd);
+ if (e != NULL)
+ {
+ ms = (memif_socket_t *) e->data_struct;
+ ms->use_count--;
+ free_list_elt (ms->interface_list, ms->interface_list_len,
+ c->args.interface_id);
+ if (ms->use_count <= 0)
+ {
+ lm->control_fd_update (c->listener_fd, MEMIF_FD_EVENT_DEL);
+ free_list_elt (lm->listener_list, lm->listener_list_len,
+ c->listener_fd);
+ close (c->listener_fd);
+ c->listener_fd = ms->fd = -1;
+ free (ms->interface_list);
+ ms->interface_list = NULL;
+ free (ms->filename);
+ ms->filename = NULL;
+ free (ms);
+ ms = NULL;
+ }
+ }
+ }
+ else
+ {
+ lm->disconn_slaves--;
+ if (lm->disconn_slaves <= 0)
+ {
+ if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ DBG ("timerfd_settime: disarm");
+ }
+ }
+ }
+
+ if (c->args.socket_filename)
+ free (c->args.socket_filename);
+ c->args.socket_filename = NULL;
+
+ free (c);
+ c = NULL;
+
+ *conn = c;
+ return err;
+}
+
+int
+memif_connect1 (memif_connection_t * c)
+{
+ libmemif_main_t *lm = &libmemif_main;
+ memif_region_t *mr = c->regions;
+ memif_queue_t *mq;
+ int i;
+ uint16_t num;
+
+ if (mr != NULL)
+ {
+ if (!mr->shm)
+ {
+ if (mr->fd < 0)
+ return MEMIF_ERR_NO_SHMFD;
+
+ if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, mr->fd, 0)) == MAP_FAILED)
+ {
+ return memif_syscall_error_handler (errno);
+ }
+ }
+ }
+
+ num =
+ (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
+ num_s2m_rings;
+ for (i = 0; i < num; i++)
+ {
+ mq = &c->tx_queues[i];
+ if (mq != NULL)
+ {
+ mq->ring = c->regions[mq->region].shm + mq->offset;
+ if (mq->ring->cookie != MEMIF_COOKIE)
+ {
+ DBG ("wrong cookie on tx ring %u", i);
+ return MEMIF_ERR_COOKIE;
+ }
+ mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
+ 0;
+ }
+ }
+ num =
+ (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
+ num_m2s_rings;
+ for (i = 0; i < num; i++)
+ {
+ mq = &c->rx_queues[i];
+ if (mq != NULL)
+ {
+ mq->ring = c->regions[mq->region].shm + mq->offset;
+ if (mq->ring->cookie != MEMIF_COOKIE)
+ {
+ DBG ("wrong cookie on rx ring %u", i);
+ return MEMIF_ERR_COOKIE;
+ }
+ mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
+ 0;
+ }
+ }
+
+ lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD);
+
+ return 0;
+}
+
+int
+memif_init_regions_and_queues (memif_connection_t * conn)
+{
+ memif_ring_t *ring = NULL;
+ uint64_t buffer_offset;
+ memif_region_t *r;
+ int i, j;
+ libmemif_main_t *lm = &libmemif_main;
+ memif_list_elt_t e;
+
+ conn->regions = (memif_region_t *) malloc (sizeof (memif_region_t));
+ if (conn->regions == NULL)
+ return memif_syscall_error_handler (errno);
+ r = conn->regions;
+
+ buffer_offset =
+ (conn->run_args.num_s2m_rings +
+ conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) +
+ sizeof (memif_desc_t) *
+ (1 << conn->run_args.log2_ring_size));
+
+ r->region_size = buffer_offset +
+ conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
+ (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
+
+ if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1)
+ return memif_syscall_error_handler (errno);
+/*
+ if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
+ return memif_syscall_error_handler (errno);
+*/
+ if ((ftruncate (r->fd, r->region_size)) == -1)
+ return memif_syscall_error_handler (errno);
+
+ if ((r->shm = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, r->fd, 0)) == MAP_FAILED)
+ return memif_syscall_error_handler (errno);
+
+ for (i = 0; i < conn->run_args.num_s2m_rings; i++)
+ {
+ ring = memif_get_ring (conn, MEMIF_RING_S2M, i);
+ DBG ("RING: %p I: %d", ring, i);
+ ring->head = ring->tail = 0;
+ ring->cookie = MEMIF_COOKIE;
+ ring->flags = 0;
+ for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
+ {
+ uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j;
+ ring->desc[j].region = 0;
+ ring->desc[j].offset = buffer_offset +
+ (uint32_t) (slot * conn->run_args.buffer_size);
+ ring->desc[j].buffer_length = conn->run_args.buffer_size;
+ }
+ }
+ for (i = 0; i < conn->run_args.num_m2s_rings; i++)
+ {
+ ring = memif_get_ring (conn, MEMIF_RING_M2S, i);
+ DBG ("RING: %p I: %d", ring, i);
+ ring->head = ring->tail = 0;
+ ring->cookie = MEMIF_COOKIE;
+ ring->flags = 0;
+ for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++)
+ {
+ uint16_t slot =
+ (i +
+ conn->run_args.num_s2m_rings) *
+ (1 << conn->run_args.log2_ring_size) + j;
+ ring->desc[j].region = 0;
+ ring->desc[j].offset = buffer_offset +
+ (uint32_t) (slot * conn->run_args.buffer_size);
+ ring->desc[j].buffer_length = conn->run_args.buffer_size;
+ }
+ }
+ memif_queue_t *mq;
+ mq =
+ (memif_queue_t *) malloc (sizeof (memif_queue_t) *
+ conn->run_args.num_s2m_rings);
+ if (mq == NULL)
+ return memif_syscall_error_handler (errno);
+ int x;
+ for (x = 0; x < conn->run_args.num_s2m_rings; x++)
+ {
+ if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
+ return memif_syscall_error_handler (errno);
+ /* add int fd to interrupt fd list */
+ e.key = mq[x].int_fd;
+ e.data_struct = conn;
+ add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
+
+ mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x);
+ DBG ("RING: %p I: %d", mq[x].ring, x);
+ mq[x].log2_ring_size = conn->run_args.log2_ring_size;
+ mq[x].region = 0;
+ mq[x].offset =
+ (void *) mq[x].ring - (void *) conn->regions[mq->region].shm;
+ mq[x].last_head = 0;
+ mq[x].alloc_bufs = 0;
+ }
+ conn->tx_queues = mq;
+
+ mq =
+ (memif_queue_t *) malloc (sizeof (memif_queue_t) *
+ conn->run_args.num_m2s_rings);
+ if (mq == NULL)
+ return memif_syscall_error_handler (errno);
+ for (x = 0; x < conn->run_args.num_m2s_rings; x++)
+ {
+ if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
+ return memif_syscall_error_handler (errno);
+ /* add int fd to interrupt fd list */
+ e.key = mq[x].int_fd;
+ e.data_struct = conn;
+ add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
+
+ mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x);
+ DBG ("RING: %p I: %d", mq[x].ring, x);
+ mq[x].log2_ring_size = conn->run_args.log2_ring_size;
+ mq[x].region = 0;
+ mq[x].offset =
+ (void *) mq[x].ring - (void *) conn->regions[mq->region].shm;
+ mq[x].last_head = 0;
+ mq[x].alloc_bufs = 0;
+ }
+ conn->rx_queues = mq;
+
+ return 0;
+}
+
+int
+memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
+ memif_buffer_t * bufs, uint16_t count,
+ uint16_t * count_out, uint16_t size)
+{
+ memif_connection_t *c = (memif_connection_t *) conn;
+ if (c == NULL)
+ return MEMIF_ERR_NOCONN;
+ if (c->fd < 0)
+ return MEMIF_ERR_DISCONNECTED;
+ uint8_t num =
+ (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
+ num_s2m_rings;
+ if (qid >= num)
+ return MEMIF_ERR_QID;
+ memif_queue_t *mq = &c->tx_queues[qid];
+ memif_ring_t *ring = mq->ring;
+ memif_buffer_t *b0, *b1;
+ uint8_t chain_buf0, chain_buf1;
+ uint16_t mask = (1 << mq->log2_ring_size) - 1;
+ uint16_t s0, s1, ns;
+ *count_out = 0;
+ int i, err = MEMIF_ERR_SUCCESS; /* 0 */
+
+ if (ring->tail != ring->head)
+ {
+ if (ring->head > ring->tail)
+ ns = (1 << mq->log2_ring_size) - ring->head + ring->tail;
+ else
+ ns = ring->tail - ring->head;
+ }
+ else
+ ns = (1 << mq->log2_ring_size);
+
+ /* (head == tail) ? receive function will asume that no packets are available */
+ ns -= 1;
+
+ while (count && ns)
+ {
+ while ((count > 2) && (ns > 2))
+ {
+ s0 = (ring->head + mq->alloc_bufs) & mask;
+ chain_buf0 = size / ring->desc[s0].buffer_length;
+ if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0))
+ chain_buf0++;
+
+ if (chain_buf0 > ns)
+ break;
+
+ s1 = (ring->head + mq->alloc_bufs + chain_buf0) & mask;
+ chain_buf1 = size / ring->desc[s1].buffer_length;
+ if (((size % ring->desc[s1].buffer_length) != 0) || (size == 0))
+ chain_buf1++;
+
+ if ((chain_buf0 + chain_buf1) > ns)
+ break;
+
+ b0 = (bufs + *count_out);
+ b1 = (bufs + *count_out + 1);
+
+ b0->desc_index = s0;
+ b1->desc_index = s1;
+ ring->desc[s0].flags = 0;
+ ring->desc[s1].flags = 0;
+ b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0;
+ b1->buffer_len = ring->desc[s1].buffer_length * chain_buf1;
+ /* TODO: support multiple regions -> ring descriptor contains region index */
+ b0->data = c->regions->shm + ring->desc[s0].offset;
+ b1->data = c->regions->shm + ring->desc[s1].offset;
+
+ for (i = 0; i < (memif_min (chain_buf0, chain_buf1) - 1); i++)
+ {
+ ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
+ ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
+ DBG ("allocating chained buffers");
+ }
+
+ if (chain_buf0 > chain_buf1)
+ {
+ for (; i < (chain_buf0 - 1); i++)
+ ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
+ }
+ else
+ {
+ for (; i < (chain_buf1 - 1); i++)
+ ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
+ }
+
+ mq->alloc_bufs += chain_buf0 + chain_buf1;
+
+ DBG ("allocated ring slots %u, %u", s0, s1);
+ count -= 2;
+ ns -= chain_buf0 + chain_buf1;
+ *count_out += 2;
+ }
+ s0 = (ring->head + mq->alloc_bufs) & mask;
+
+ b0 = (bufs + *count_out);
+
+ chain_buf0 = size / ring->desc[s0].buffer_length;
+ if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0))
+ chain_buf0++;
+
+ if (chain_buf0 > ns)
+ break;
+
+ b0->desc_index = s0;
+ ring->desc[s0].flags = 0;
+ b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0;
+ b0->data = c->regions->shm + ring->desc[s0].offset;
+
+ for (i = 0; i < (chain_buf0 - 1); i++)
+ {
+ ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
+ DBG ("allocating chained buffers");
+ }
+
+ mq->alloc_bufs += chain_buf0;
+
+ DBG ("allocated ring slot %u", s0);
+ count--;
+ ns -= chain_buf0;
+ *count_out += 1;
+ }
+
+ DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
+ mq->alloc_bufs);
+
+ if (count)
+ {
+ DBG ("ring buffer full! qid: %u", qid);
+ err = MEMIF_ERR_NOBUF_RING;
+ }
+
+ return err;
+}
+
+int
+memif_buffer_free (memif_conn_handle_t conn, uint16_t qid,
+ memif_buffer_t * bufs, uint16_t count,
+ uint16_t * count_out)
+{
+ memif_connection_t *c = (memif_connection_t *) conn;
+ if (c == NULL)
+ return MEMIF_ERR_NOCONN;
+ if (c->fd < 0)
+ return MEMIF_ERR_DISCONNECTED;
+ uint8_t num =
+ (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
+ num_m2s_rings;
+ if (qid >= num)
+ return MEMIF_ERR_QID;
+ libmemif_main_t *lm = &libmemif_main;
+ memif_queue_t *mq = &c->rx_queues[qid];
+ memif_ring_t *ring = mq->ring;
+ uint16_t tail = ring->tail;
+ uint16_t mask = (1 << mq->log2_ring_size) - 1;
+ uint8_t chain_buf0, chain_buf1;
+ memif_buffer_t *b0, *b1;
+ *count_out = 0;
+
+ if (mq->alloc_bufs < count)
+ count = mq->alloc_bufs;
+
+ while (count)
+ {
+ while (count > 2)
+ {
+ b0 = (bufs + *count_out);
+ b1 = (bufs + *count_out + 1);
+ chain_buf0 =
+ b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
+ if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) !=
+ 0)
+ chain_buf0++;
+ chain_buf1 =
+ b1->buffer_len / ring->desc[b1->desc_index].buffer_length;
+ if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) !=
+ 0)
+ chain_buf1++;
+ tail = (b1->desc_index + chain_buf1) & mask;
+ b0->data = NULL;
+ b1->data = NULL;
+
+ count -= 2;
+ *count_out += 2;
+ mq->alloc_bufs -= chain_buf0 + chain_buf1;
+ }
+ b0 = (bufs + *count_out);
+ chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
+ if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0)
+ chain_buf0++;
+ tail = (b0->desc_index + chain_buf0) & mask;
+ b0->data = NULL;
+
+ count--;
+ *count_out += 1;
+ mq->alloc_bufs -= chain_buf0;
+ }
+ MEMIF_MEORY_BARRIER ();
+ ring->tail = tail;
+ DBG ("tail: %u", ring->tail);
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+int
+memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
+ memif_buffer_t * bufs, uint16_t count, uint16_t * tx)
+{
+ memif_connection_t *c = (memif_connection_t *) conn;
+ if (c == NULL)
+ return MEMIF_ERR_NOCONN;
+ if (c->fd < 0)
+ return MEMIF_ERR_DISCONNECTED;
+ uint8_t num =
+ (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
+ num_s2m_rings;
+ if (qid >= num)
+ return MEMIF_ERR_QID;
+ memif_queue_t *mq = &c->tx_queues[qid];
+ memif_ring_t *ring = mq->ring;
+ uint16_t head = ring->head;
+ uint16_t mask = (1 << mq->log2_ring_size) - 1;
+ uint8_t chain_buf0, chain_buf1;
+ *tx = 0;
+ uint16_t curr_buf = 0;
+ memif_buffer_t *b0, *b1;
+ int i;
+
+ while (count)
+ {
+ while (count > 2)
+ {
+ b0 = (bufs + curr_buf);
+ b1 = (bufs + curr_buf + 1);
+ chain_buf0 =
+ b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
+ if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) !=
+ 0)
+ chain_buf0++;
+
+ chain_buf1 =
+ b1->buffer_len / ring->desc[b1->desc_index].buffer_length;
+ if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) !=
+ 0)
+ chain_buf1++;
+
+ for (i = 0; i < memif_min (chain_buf0, chain_buf1); i++)
+ {
+ /* b0 */
+ if (b0->data_len >
+ ring->desc[(b0->desc_index + i) & mask].buffer_length)
+ {
+ b0->data_len -=
+ ring->desc[(b0->desc_index + i) & mask].length =
+ ring->desc[(b0->desc_index + i) & mask].buffer_length;
+ }
+ else
+ {
+ ring->desc[(b0->desc_index + i) & mask].length =
+ b0->data_len;
+ b0->data_len = 0;
+ }
+ /* b1 */
+ if (b1->data_len >
+ ring->desc[(b1->desc_index + i) & mask].buffer_length)
+ {
+ b1->data_len -=
+ ring->desc[(b1->desc_index + i) & mask].length =
+ ring->desc[(b1->desc_index + i) & mask].buffer_length;
+ }
+ else
+ {
+ ring->desc[(b1->desc_index + i) & mask].length =
+ b1->data_len;
+ b1->data_len = 0;
+ }
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b0->data +
+ ring->desc[(b0->desc_index +
+ i) & mask].buffer_length *
+ (chain_buf0 - 1),
+ ring->desc[(b0->desc_index +
+ i) & mask].buffer_length, DBG_TX_BUF);
+ print_bytes (b1->data +
+ ring->desc[(b1->desc_index +
+ i) & mask].buffer_length *
+ (chain_buf1 - 1),
+ ring->desc[(b1->desc_index +
+ i) & mask].buffer_length, DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ }
+
+ if (chain_buf0 > chain_buf1)
+ {
+ for (; i < chain_buf0; i++)
+ {
+ if (b0->data_len >
+ ring->desc[(b0->desc_index + i) & mask].buffer_length)
+ {
+ b0->data_len -=
+ ring->desc[(b0->desc_index + i) & mask].length =
+ ring->desc[(b0->desc_index + i) & mask].buffer_length;
+ }
+ else
+ {
+ ring->desc[(b0->desc_index + i) & mask].length =
+ b0->data_len;
+ b0->data_len = 0;
+ }
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b0->data +
+ ring->desc[(b0->desc_index +
+ i) & mask].buffer_length *
+ (chain_buf0 - 1),
+ ring->desc[(b0->desc_index +
+ i) & mask].buffer_length,
+ DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ }
+ }
+ else
+ {
+ for (; i < chain_buf1; i++)
+ {
+ if (b1->data_len >
+ ring->desc[(b1->desc_index + i) & mask].buffer_length)
+ {
+ b1->data_len -=
+ ring->desc[(b1->desc_index + i) & mask].length =
+ ring->desc[(b1->desc_index + i) & mask].buffer_length;
+ }
+ else
+ {
+ ring->desc[(b1->desc_index + i) & mask].length =
+ b1->data_len;
+ b1->data_len = 0;
+ }
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b1->data +
+ ring->desc[(b1->desc_index +
+ i) & mask].buffer_length *
+ (chain_buf1 - 1),
+ ring->desc[(b1->desc_index +
+ i) & mask].buffer_length,
+ DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ }
+ }
+
+ head = (b1->desc_index + chain_buf1) & mask;
+
+ b0->data = NULL;
+#ifdef MEMIF_DBG
+ if (b0->data_len != 0)
+ DBG ("invalid b0 data length!");
+#endif /* MEMIF_DBG */
+ b1->data = NULL;
+#ifdef MEMIF_DBG
+ if (b1->data_len != 0)
+ DBG ("invalid b1 data length!");
+#endif /* MEMIF_DBG */
+
+ count -= 2;
+ *tx += chain_buf0 + chain_buf1;
+ curr_buf += 2;
+ }
+
+ b0 = (bufs + curr_buf);
+ chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
+ if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0)
+ chain_buf0++;
+
+ for (i = 0; i < chain_buf0; i++)
+ {
+ if (b0->data_len >
+ ring->desc[(b0->desc_index + i) & mask].buffer_length)
+ {
+ b0->data_len -= ring->desc[(b0->desc_index + i) & mask].length =
+ ring->desc[(b0->desc_index + i) & mask].buffer_length;
+ }
+ else
+ {
+ ring->desc[(b0->desc_index + i) & mask].length = b0->data_len;
+ b0->data_len = 0;
+ }
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b0->data +
+ ring->desc[(b0->desc_index + i) & mask].buffer_length *
+ (chain_buf0 - 1),
+ ring->desc[(b0->desc_index + i) & mask].buffer_length,
+ DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ }
+
+ head = (b0->desc_index + chain_buf0) & mask;
+
+ b0->data = NULL;
+#ifdef MEMIF_DBG
+ if (b0->data_len != 0)
+ DBG ("invalid b0 data length!");
+#endif /* MEMIF_DBG */
+
+ count--;
+ *tx += chain_buf0;
+ curr_buf++;
+ }
+ MEMIF_MEORY_BARRIER ();
+ ring->head = head;
+
+ mq->alloc_bufs -= *tx;
+
+ /* TODO: return num of buffers and packets */
+ *tx = curr_buf;
+
+ if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
+ {
+ uint64_t a = 1;
+ int r = write (mq->int_fd, &a, sizeof (a));
+ if (r < 0)
+ return MEMIF_ERR_INT_WRITE;
+ }
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+int
+memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
+ memif_buffer_t * bufs, uint16_t count, uint16_t * rx)
+{
+ memif_connection_t *c = (memif_connection_t *) conn;
+ if (c == NULL)
+ return MEMIF_ERR_NOCONN;
+ if (c->fd < 0)
+ return MEMIF_ERR_DISCONNECTED;
+ uint8_t num =
+ (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
+ num_m2s_rings;
+ if (qid >= num)
+ return MEMIF_ERR_QID;
+ memif_queue_t *mq = &c->rx_queues[qid];
+ memif_ring_t *ring = mq->ring;
+ uint16_t head = ring->head;
+ uint16_t ns;
+ uint16_t mask = (1 << mq->log2_ring_size) - 1;
+ memif_buffer_t *b0, *b1;
+ uint16_t curr_buf = 0;
+ *rx = 0;
+ int i;
+
+ uint64_t b;
+ ssize_t r = read (mq->int_fd, &b, sizeof (b));
+ if ((r == -1) && (errno != EAGAIN))
+ return memif_syscall_error_handler (errno);
+
+ if (head == mq->last_head)
+ return 0;
+
+ if (head > mq->last_head)
+ ns = head - mq->last_head;
+ else
+ ns = (1 << mq->log2_ring_size) - mq->last_head + head;
+
+ while (ns && count)
+ {
+ DBG ("ns: %u, count: %u", ns, count);
+ while ((ns > 2) && (count > 2))
+ {
+ b0 = (bufs + curr_buf);
+ b1 = (bufs + curr_buf + 1);
+
+ b0->desc_index = mq->last_head;
+ i = 0;
+ b0->data_len = 0;
+ b0->buffer_len = 0;
+
+ b0->data = memif_get_buffer (conn, ring, mq->last_head);
+ b0->data_len += ring->desc[mq->last_head].length;
+ b0->buffer_len += ring->desc[mq->last_head].buffer_length;
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b0->data +
+ ring->desc[b0->desc_index].buffer_length * i++,
+ ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
+ while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT)
+ {
+ ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT;
+ mq->last_head = (mq->last_head + 1) & mask;
+ b0->data_len += ring->desc[mq->last_head].length;
+ b0->buffer_len += ring->desc[mq->last_head].buffer_length;
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b0->data +
+ ring->desc[b0->desc_index].buffer_length * i++,
+ ring->desc[b0->desc_index].buffer_length,
+ DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
+ }
+ mq->last_head = (mq->last_head + 1) & mask;
+
+ b1->desc_index = mq->last_head;
+ i = 0;
+ b0->data_len = 0;
+ b0->buffer_len = 0;
+
+ b1->data = memif_get_buffer (conn, ring, mq->last_head);
+ b1->data_len += ring->desc[mq->last_head].length;
+ b1->buffer_len += ring->desc[mq->last_head].buffer_length;
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b1->data +
+ ring->desc[b1->desc_index].buffer_length * i++,
+ ring->desc[b1->desc_index].buffer_length, DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
+ while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT)
+ {
+ ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT;
+ mq->last_head = (mq->last_head + 1) & mask;
+ b1->data_len += ring->desc[mq->last_head].length;
+ b1->buffer_len += ring->desc[mq->last_head].buffer_length;
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b1->data +
+ ring->desc[b1->desc_index].buffer_length * i++,
+ ring->desc[b1->desc_index].buffer_length,
+ DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
+ }
+ mq->last_head = (mq->last_head + 1) & mask;
+
+ count -= 2;
+ curr_buf += 2;
+ }
+ b0 = (bufs + curr_buf);
+
+ b0->desc_index = mq->last_head;
+ i = 0;
+ b0->data_len = 0;
+ b0->buffer_len = 0;
+
+ b0->data = memif_get_buffer (conn, ring, mq->last_head);
+ b0->data_len += ring->desc[mq->last_head].length;
+ b0->buffer_len += ring->desc[mq->last_head].buffer_length;
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b0->data +
+ ring->desc[b0->desc_index].buffer_length * i++,
+ ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
+
+ while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT)
+ {
+ ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT;
+ mq->last_head = (mq->last_head + 1) & mask;
+ b0->data_len += ring->desc[mq->last_head].length;
+ b0->buffer_len += ring->desc[mq->last_head].buffer_length;
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b0->data +
+ ring->desc[b0->desc_index].buffer_length * i++,
+ ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
+ }
+ mq->last_head = (mq->last_head + 1) & mask;
+
+ count--;
+ curr_buf++;
+ }
+
+ mq->alloc_bufs += *rx;
+
+ /* TODO: return num of buffers and packets */
+ *rx = curr_buf;
+
+ if (ns)
+ {
+ DBG ("not enough buffers!");
+ return MEMIF_ERR_NOBUF;
+ }
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+int
+memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
+ char *buf, ssize_t buflen)
+{
+ memif_connection_t *c = (memif_connection_t *) conn;
+ if (c == NULL)
+ return MEMIF_ERR_NOCONN;
+
+ int err = MEMIF_ERR_SUCCESS, i;
+ ssize_t l0, l1, total_l;
+ l0 = 0;
+
+ l1 = strlen ((char *) c->args.interface_name);
+ if (l0 + l1 <= buflen)
+ {
+ md->if_name = strncpy (buf + l0, (char *) c->args.interface_name, l1);
+ md->if_name[l0 + l1] = '\0';
+ l0 += l1 + 1;
+ }
+ else
+ err = MEMIF_ERR_NOBUF_DET;
+
+ l1 = strlen ((char *) c->args.instance_name);
+ if (l0 + l1 <= buflen)
+ {
+ md->inst_name = strncpy (buf + l0, (char *) c->args.instance_name, l1);
+ md->inst_name[l0 + l1] = '\0';
+ l0 += l1 + 1;
+ }
+ else
+ err = MEMIF_ERR_NOBUF_DET;
+
+ l1 = strlen ((char *) c->remote_if_name);
+ if (l0 + l1 <= buflen)
+ {
+ md->remote_if_name = strncpy (buf + l0, (char *) c->remote_if_name, l1);
+ md->remote_if_name[l0 + l1] = '\0';
+ l0 += l1 + 1;
+ }
+ else
+ err = MEMIF_ERR_NOBUF_DET;
+
+ l1 = strlen ((char *) c->remote_name);
+ if (l0 + l1 <= buflen)
+ {
+ md->remote_inst_name = strncpy (buf + l0, (char *) c->remote_name, l1);
+ md->remote_inst_name[l0 + l1] = '\0';
+ l0 += l1 + 1;
+ }
+ else
+ err = MEMIF_ERR_NOBUF_DET;
+
+ md->id = c->args.interface_id;
+
+ if (c->args.secret)
+ {
+ l1 = strlen ((char *) c->args.secret);
+ md->secret = strncpy (buf + l0, (char *) c->args.secret, l1);
+ md->secret[l0 + l1] = '\0';
+ l0 += l1 + 1;
+ }
+ else
+ err = MEMIF_ERR_NOBUF_DET;
+
+ md->role = (c->args.is_master) ? 0 : 1;
+ md->mode = c->args.mode;
+
+ l1 = strlen ((char *) c->args.socket_filename);
+ if (l0 + l1 <= buflen)
+ {
+ md->socket_filename =
+ strncpy (buf + l0, (char *) c->args.socket_filename, l1);
+ md->socket_filename[l0 + l1] = '\0';
+ l0 += l1 + 1;
+ }
+ else
+ err = MEMIF_ERR_NOBUF_DET;
+
+ md->rx_queues_num =
+ (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
+ num_m2s_rings;
+
+ l1 = sizeof (memif_queue_details_t) * md->rx_queues_num;
+ if (l0 + l1 <= buflen)
+ {
+ md->rx_queues = (memif_queue_details_t *) buf + l0;
+ l0 = l1 + 1;
+ }
+ else
+ err = MEMIF_ERR_NOBUF_DET;
+
+ for (i = 0; i < md->rx_queues_num; i++)
+ {
+ md->rx_queues[i].qid = i;
+ md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size);
+ md->rx_queues[i].buffer_size = c->run_args.buffer_size;
+ }
+
+ md->tx_queues_num =
+ (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
+ num_s2m_rings;
+
+ l1 = sizeof (memif_queue_details_t) * md->tx_queues_num;
+ if (l0 + l1 <= buflen)
+ {
+ md->tx_queues = (memif_queue_details_t *) buf + l0;
+ l0 = l1 + 1;
+ }
+ else
+ err = MEMIF_ERR_NOBUF_DET;
+
+ for (i = 0; i < md->tx_queues_num; i++)
+ {
+ md->tx_queues[i].qid = i;
+ md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size);
+ md->tx_queues[i].buffer_size = c->run_args.buffer_size;
+ }
+
+ md->link_up_down = (c->fd > 0) ? 1 : 0;
+
+ return err; /* 0 */
+}
+
+int
+memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd)
+{
+ memif_connection_t *c = (memif_connection_t *) conn;
+ *efd = -1;
+ if (c == NULL)
+ return MEMIF_ERR_NOCONN;
+ if (c->fd < 0)
+ return MEMIF_ERR_DISCONNECTED;
+ uint8_t num =
+ (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
+ num_m2s_rings;
+ if (qid >= num)
+ return MEMIF_ERR_QID;
+
+ *efd = c->rx_queues[qid].int_fd;
+
+ return MEMIF_ERR_SUCCESS;
+}
+
+int
+memif_cleanup ()
+{
+ libmemif_main_t *lm = &libmemif_main;
+ if (lm->app_name)
+ free (lm->app_name);
+ lm->app_name = NULL;
+ if (lm->control_list)
+ free (lm->control_list);
+ lm->control_list = NULL;
+ if (lm->interrupt_list)
+ free (lm->interrupt_list);
+ lm->interrupt_list = NULL;
+ if (lm->listener_list)
+ free (lm->listener_list);
+ lm->listener_list = NULL;
+ if (lm->pending_list)
+ free (lm->pending_list);
+ lm->pending_list = NULL;
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
diff --git a/extras/libmemif/src/memif.h b/extras/libmemif/src/memif.h
new file mode 100644
index 00000000..11918eab
--- /dev/null
+++ b/extras/libmemif/src/memif.h
@@ -0,0 +1,185 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#ifndef _MEMIF_H_
+#define _MEMIF_H_
+
+#ifndef MEMIF_CACHELINE_SIZE
+#define MEMIF_CACHELINE_SIZE 64
+#endif
+
+#define MEMIF_COOKIE 0x3E31F10
+#define MEMIF_VERSION_MAJOR 1
+#define MEMIF_VERSION_MINOR 0
+#define MEMIF_VERSION ((MEMIF_VERSION_MAJOR << 8) | MEMIF_VERSION_MINOR)
+
+/*
+ * Type definitions
+ */
+
+typedef enum memif_msg_type
+{
+ MEMIF_MSG_TYPE_NONE = 0,
+ MEMIF_MSG_TYPE_ACK = 1,
+ MEMIF_MSG_TYPE_HELLO = 2,
+ MEMIF_MSG_TYPE_INIT = 3,
+ MEMIF_MSG_TYPE_ADD_REGION = 4,
+ MEMIF_MSG_TYPE_ADD_RING = 5,
+ MEMIF_MSG_TYPE_CONNECT = 6,
+ MEMIF_MSG_TYPE_CONNECTED = 7,
+ MEMIF_MSG_TYPE_DISCONNECT = 8,
+} memif_msg_type_t;
+
+typedef enum
+{
+ MEMIF_RING_S2M = 0,
+ MEMIF_RING_M2S = 1
+} memif_ring_type_t;
+
+typedef enum
+{
+ MEMIF_INTERFACE_MODE_ETHERNET = 0,
+ MEMIF_INTERFACE_MODE_IP = 1,
+ MEMIF_INTERFACE_MODE_PUNT_INJECT = 2,
+} memif_interface_mode_t;
+
+typedef uint16_t memif_region_index_t;
+typedef uint64_t memif_region_offset_t;
+typedef uint64_t memif_region_size_t;
+typedef uint16_t memif_ring_index_t;
+typedef uint32_t memif_interface_id_t;
+typedef uint16_t memif_version_t;
+typedef uint8_t memif_log2_ring_size_t;
+
+/*
+ * Socket messages
+ */
+
+typedef struct __attribute__ ((packed))
+{
+ uint8_t name[32];
+ memif_version_t min_version;
+ memif_version_t max_version;
+ memif_region_index_t max_region;
+ memif_ring_index_t max_m2s_ring;
+ memif_ring_index_t max_s2m_ring;
+ memif_log2_ring_size_t max_log2_ring_size;
+} memif_msg_hello_t;
+
+typedef struct __attribute__ ((packed))
+{
+ memif_version_t version;
+ memif_interface_id_t id;
+ memif_interface_mode_t mode:8;
+ uint8_t secret[24];
+ uint8_t name[32];
+} memif_msg_init_t;
+
+typedef struct __attribute__ ((packed))
+{
+ memif_region_index_t index;
+ memif_region_size_t size;
+} memif_msg_add_region_t;
+
+typedef struct __attribute__ ((packed))
+{
+ uint16_t flags;
+#define MEMIF_MSG_ADD_RING_FLAG_S2M (1 << 0)
+ memif_ring_index_t index;
+ memif_region_index_t region;
+ memif_region_offset_t offset;
+ memif_log2_ring_size_t log2_ring_size;
+} memif_msg_add_ring_t;
+
+typedef struct __attribute__ ((packed))
+{
+ uint8_t if_name[32];
+} memif_msg_connect_t;
+
+typedef struct __attribute__ ((packed))
+{
+ uint8_t if_name[32];
+} memif_msg_connected_t;
+
+typedef struct __attribute__ ((packed))
+{
+ uint32_t code;
+ uint8_t string[96];
+} memif_msg_disconnect_t;
+
+typedef struct __attribute__ ((packed, aligned (128)))
+{
+ memif_msg_type_t type:16;
+ union
+ {
+ memif_msg_hello_t hello;
+ memif_msg_init_t init;
+ memif_msg_add_region_t add_region;
+ memif_msg_add_ring_t add_ring;
+ memif_msg_connect_t connect;
+ memif_msg_connected_t connected;
+ memif_msg_disconnect_t disconnect;
+ };
+} memif_msg_t;
+
+_Static_assert (sizeof (memif_msg_t) == 128,
+ "Size of memif_msg_t must be 128");
+
+/*
+ * Ring and Descriptor Layout
+ */
+
+typedef struct __attribute__ ((packed))
+{
+ uint16_t flags;
+#define MEMIF_DESC_FLAG_NEXT (1 << 0)
+ memif_region_index_t region;
+ uint32_t buffer_length;
+ uint32_t length;
+ uint8_t reserved[4];
+ memif_region_offset_t offset;
+ uint64_t metadata;
+} memif_desc_t;
+
+_Static_assert (sizeof (memif_desc_t) == 32,
+ "Size of memif_dsct_t must be 32");
+
+#define MEMIF_CACHELINE_ALIGN_MARK(mark) \
+ uint8_t mark[0] __attribute__((aligned(MEMIF_CACHELINE_SIZE)))
+
+typedef struct
+{
+ MEMIF_CACHELINE_ALIGN_MARK (cacheline0);
+ uint32_t cookie;
+ uint16_t flags;
+#define MEMIF_RING_FLAG_MASK_INT 1
+ volatile uint16_t head;
+ MEMIF_CACHELINE_ALIGN_MARK (cacheline1);
+ volatile uint16_t tail;
+ MEMIF_CACHELINE_ALIGN_MARK (cacheline2);
+ memif_desc_t desc[0];
+} memif_ring_t;
+
+#endif /* _MEMIF_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/extras/libmemif/src/memif_private.h b/extras/libmemif/src/memif_private.h
new file mode 100644
index 00000000..83962bcf
--- /dev/null
+++ b/extras/libmemif/src/memif_private.h
@@ -0,0 +1,266 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+
+#ifndef _MEMIF_PRIVATE_H_
+#define _MEMIF_PRIVATE_H_
+
+#define _GNU_SOURCE
+#include <unistd.h>
+#include <sys/syscall.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <sys/timerfd.h>
+
+#include <libmemif.h>
+
+#define MEMIF_DEFAULT_SOCKET_DIR "/run/vpp"
+#define MEMIF_DEFAULT_SOCKET_FILENAME "memif.sock"
+#define MEMIF_DEFAULT_RING_SIZE 1024
+#define MEMIF_DEFAULT_LOG2_RING_SIZE 10
+#define MEMIF_DEFAULT_RX_QUEUES 1
+#define MEMIF_DEFAULT_TX_QUEUES 1
+#define MEMIF_DEFAULT_BUFFER_SIZE 2048
+
+#define MEMIF_MAX_M2S_RING 255
+#define MEMIF_MAX_S2M_RING 255
+#define MEMIF_MAX_REGION 255
+#define MEMIF_MAX_LOG2_RING_SIZE 14
+
+#define MEMIF_MAX_FDS 512
+
+#define memif_min(a,b) (((a) < (b)) ? (a) : (b))
+
+#ifdef MEMIF_DBG
+#define DBG(...) do { \
+ printf("MEMIF_DEBUG:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
+ printf(__VA_ARGS__); \
+ printf("\n"); \
+ } while (0)
+
+#define DBG_UNIX(...) do { \
+ printf("MEMIF_DEBUG_UNIX:%s:%s:%d: ", __FILE__, __func__, __LINE__); \
+ printf(__VA_ARGS__); \
+ printf("\n"); \
+ } while (0)
+
+#define error_return_unix(...) do { \
+ DBG_UNIX(__VA_ARGS__); \
+ return -1; \
+ } while (0)
+#define error_return(...) do { \
+ DBG(__VA_ARGS__); \
+ return -1; \
+ } while (0)
+#else
+#define DBG(...)
+#define DBG_UNIX(...)
+#define error_return_unix(...) do { \
+ return -1; \
+ } while (0)
+#define error_return(...) do { \
+ return -1; \
+ } while (0)
+
+#endif /* MEMIF_DBG */
+
+typedef struct
+{
+ void *shm;
+ uint32_t region_size;
+ int fd;
+} memif_region_t;
+
+typedef struct
+{
+ memif_ring_t *ring;
+ uint8_t log2_ring_size;
+ uint8_t region;
+ uint32_t offset;
+
+ uint16_t last_head;
+ uint16_t last_tail;
+
+ int int_fd;
+
+ uint64_t int_count;
+ uint32_t alloc_bufs;
+} memif_queue_t;
+
+typedef struct memif_msg_queue_elt
+{
+ memif_msg_t msg;
+ int fd;
+ struct memif_msg_queue_elt *next;
+} memif_msg_queue_elt_t;
+
+struct memif_connection;
+
+typedef struct memif_connection memif_connection_t;
+
+/* functions called by memif_control_fd_handler */
+typedef int (memif_fn) (memif_connection_t * conn);
+
+typedef struct
+{
+ uint8_t num_s2m_rings;
+ uint8_t num_m2s_rings;
+ uint16_t buffer_size;
+ memif_log2_ring_size_t log2_ring_size;
+} memif_conn_run_args_t;
+
+typedef struct memif_connection
+{
+ uint16_t index;
+ memif_conn_args_t args;
+ memif_conn_run_args_t run_args;
+
+ int fd;
+ int listener_fd;
+
+ memif_fn *write_fn, *read_fn, *error_fn;
+
+ memif_connection_update_t *on_connect, *on_disconnect;
+ memif_interrupt_t *on_interrupt;
+ void *private_ctx;
+
+ /* connection message queue */
+ memif_msg_queue_elt_t *msg_queue;
+
+ uint8_t remote_if_name[32];
+ uint8_t remote_name[32];
+ uint8_t remote_disconnect_string[96];
+
+ memif_region_t *regions;
+
+ memif_queue_t *rx_queues;
+ memif_queue_t *tx_queues;
+
+ uint16_t flags;
+#define MEMIF_CONNECTION_FLAG_WRITE (1 << 0)
+} memif_connection_t;
+
+/*
+ * WIP
+ */
+typedef struct
+{
+ int key; /* fd or id */
+ void *data_struct;
+} memif_list_elt_t;
+
+/*
+ * WIP
+ */
+typedef struct
+{
+ int fd;
+ uint16_t use_count;
+ uint8_t *filename;
+ uint16_t interface_list_len;
+ memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
+} memif_socket_t;
+
+/*
+ * WIP
+ */
+/* probably function like memif_cleanup () will need to be called to close timerfd */
+typedef struct
+{
+ memif_control_fd_update_t *control_fd_update;
+ int timerfd;
+ struct itimerspec arm, disarm;
+ uint16_t disconn_slaves;
+ uint8_t *app_name;
+
+ /* master implementation... */
+ memif_socket_t ms;
+
+ uint16_t control_list_len;
+ uint16_t interrupt_list_len;
+ uint16_t listener_list_len;
+ uint16_t pending_list_len;
+ memif_list_elt_t *control_list;
+ memif_list_elt_t *interrupt_list;
+ memif_list_elt_t *listener_list;
+ memif_list_elt_t *pending_list;
+} libmemif_main_t;
+
+extern libmemif_main_t libmemif_main;
+extern int memif_epfd;
+
+/* main.c */
+
+/* if region doesn't contain shared memory, mmap region, check ring cookie */
+int memif_connect1 (memif_connection_t * c);
+
+/* memory map region, initalize rings and queues */
+int memif_init_regions_and_queues (memif_connection_t * c);
+
+int memif_disconnect_internal (memif_connection_t * c);
+
+/* map errno to memif error code */
+int memif_syscall_error_handler (int err_code);
+
+int add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list,
+ uint16_t * len);
+
+int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list,
+ uint16_t len, int key);
+
+int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
+
+#ifndef __NR_memfd_create
+#if defined __x86_64__
+#define __NR_memfd_create 319
+#elif defined __arm__
+#define __NR_memfd_create 385
+#elif defined __aarch64__
+#define __NR_memfd_create 279
+#else
+#error "__NR_memfd_create unknown for this architecture"
+#endif
+#endif
+
+static inline int
+memfd_create (const char *name, unsigned int flags)
+{
+ return syscall (__NR_memfd_create, name, flags);
+}
+
+static inline void *
+memif_get_buffer (memif_connection_t * conn, memif_ring_t * ring,
+ uint16_t index)
+{
+ return (conn->regions[ring->desc[index].region].shm +
+ ring->desc[index].offset);
+}
+
+#ifndef F_LINUX_SPECIFIC_BASE
+#define F_LINUX_SPECIFIC_BASE 1024
+#endif
+#define MFD_ALLOW_SEALING 0x0002U
+#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
+#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
+
+#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
+#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
+#define F_SEAL_GROW 0x0004 /* prevent file from growing */
+#define F_SEAL_WRITE 0x0008 /* prevent writes */
+
+#endif /* _MEMIF_PRIVATE_H_ */
diff --git a/extras/libmemif/src/socket.c b/extras/libmemif/src/socket.c
new file mode 100644
index 00000000..ca24d929
--- /dev/null
+++ b/extras/libmemif/src/socket.c
@@ -0,0 +1,881 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#define _GNU_SOURCE
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <sys/uio.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include <socket.h>
+#include <memif.h>
+
+/* sends msg to socket */
+static_fn int
+memif_msg_send (int fd, memif_msg_t * msg, int afd)
+{
+ struct msghdr mh = { 0 };
+ struct iovec iov[1];
+ char ctl[CMSG_SPACE (sizeof (int))];
+ int rv, err = MEMIF_ERR_SUCCESS; /* 0 */
+
+ iov[0].iov_base = (void *) msg;
+ iov[0].iov_len = sizeof (memif_msg_t);
+ mh.msg_iov = iov;
+ mh.msg_iovlen = 1;
+
+ if (afd > 0)
+ {
+ struct cmsghdr *cmsg;
+ memset (&ctl, 0, sizeof (ctl));
+ mh.msg_control = ctl;
+ mh.msg_controllen = sizeof (ctl);
+ cmsg = CMSG_FIRSTHDR (&mh);
+ cmsg->cmsg_len = CMSG_LEN (sizeof (int));
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ memcpy (CMSG_DATA (cmsg), &afd, sizeof (int));
+ }
+ rv = sendmsg (fd, &mh, 0);
+ if (rv < 0)
+ err = memif_syscall_error_handler (errno);
+ DBG ("Message type %u sent", msg->type);
+ return err;
+}
+
+/* response from memif master - master is ready to handle next message */
+static_fn int
+memif_msg_enq_ack (memif_connection_t * c)
+{
+ memif_msg_queue_elt_t *e =
+ (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
+ if (e == NULL)
+ return memif_syscall_error_handler (errno);
+
+ memset (&e->msg, 0, sizeof (e->msg));
+ e->msg.type = MEMIF_MSG_TYPE_ACK;
+ e->fd = -1;
+
+ e->next = NULL;
+ if (c->msg_queue == NULL)
+ {
+ c->msg_queue = e;
+ return MEMIF_ERR_SUCCESS; /* 0 */
+ }
+
+ memif_msg_queue_elt_t *cur = c->msg_queue;
+ while (cur->next != NULL)
+ {
+ cur = cur->next;
+ }
+ cur->next = e;
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+static_fn int
+memif_msg_send_hello (int fd)
+{
+ libmemif_main_t *lm = &libmemif_main;
+ memif_msg_t msg = { 0 };
+ memif_msg_hello_t *h = &msg.hello;
+ msg.type = MEMIF_MSG_TYPE_HELLO;
+ h->min_version = MEMIF_VERSION;
+ h->max_version = MEMIF_VERSION;
+ h->max_s2m_ring = MEMIF_MAX_M2S_RING;
+ h->max_m2s_ring = MEMIF_MAX_M2S_RING;
+ h->max_region = MEMIF_MAX_REGION;
+ h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE;
+
+ strncpy ((char *) h->name, lm->app_name, strlen (lm->app_name));
+
+ /* msg hello is not enqueued but sent directly,
+ because it is the first msg to be sent */
+ return memif_msg_send (fd, &msg, -1);
+}
+
+/* send id and secret (optional) for interface identification */
+static_fn int
+memif_msg_enq_init (memif_connection_t * c)
+{
+ memif_msg_queue_elt_t *e =
+ (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
+ if (e == NULL)
+ return memif_syscall_error_handler (errno);
+ memset (e, 0, sizeof (memif_msg_queue_elt_t));
+
+ memset (&e->msg, 0, sizeof (e->msg));
+ memif_msg_init_t *i = &e->msg.init;
+
+ e->msg.type = MEMIF_MSG_TYPE_INIT;
+ e->fd = -1;
+ i->version = MEMIF_VERSION;
+ i->id = c->args.interface_id;
+ i->mode = c->args.mode;
+
+ strncpy ((char *) i->name, (char *) c->args.instance_name,
+ strlen ((char *) c->args.instance_name));
+ if (c->args.secret)
+ strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret));
+
+ e->next = NULL;
+ if (c->msg_queue == NULL)
+ {
+ c->msg_queue = e;
+ return MEMIF_ERR_SUCCESS; /* 0 */
+ }
+
+ memif_msg_queue_elt_t *cur = c->msg_queue;
+ while (cur->next != NULL)
+ {
+ cur = cur->next;
+ }
+ cur->next = e;
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+/* send information about region specified by region_index */
+static_fn int
+memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
+{
+ /* maybe check if region is valid? */
+ memif_region_t *mr = &c->regions[region_index];
+
+ memif_msg_queue_elt_t *e =
+ (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
+ if (e == NULL)
+ return memif_syscall_error_handler (errno);
+
+ memset (&e->msg, 0, sizeof (e->msg));
+ memif_msg_add_region_t *ar = &e->msg.add_region;
+
+ e->msg.type = MEMIF_MSG_TYPE_ADD_REGION;
+ e->fd = mr->fd;
+ ar->index = region_index;
+ ar->size = mr->region_size;
+
+ e->next = NULL;
+ if (c->msg_queue == NULL)
+ {
+ c->msg_queue = e;
+ return MEMIF_ERR_SUCCESS; /* 0 */
+ }
+
+ memif_msg_queue_elt_t *cur = c->msg_queue;
+ while (cur->next != NULL)
+ {
+ cur = cur->next;
+ }
+ cur->next = e;
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+/* send information about ring specified by direction (S2M | M2S) and index */
+static_fn int
+memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
+{
+ memif_msg_queue_elt_t *e =
+ (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
+ if (e == NULL)
+ return memif_syscall_error_handler (errno);
+
+ memset (&e->msg, 0, sizeof (e->msg));
+ memif_msg_add_ring_t *ar = &e->msg.add_ring;
+
+ e->msg.type = MEMIF_MSG_TYPE_ADD_RING;
+
+ /* TODO: support multiple rings */
+ memif_queue_t *mq;
+ if (dir == MEMIF_RING_M2S)
+ mq = &c->rx_queues[index];
+ else
+ mq = &c->tx_queues[index];
+
+ e->fd = mq->int_fd;
+ ar->index = index;
+ ar->offset = mq->offset;
+ ar->region = mq->region;
+ ar->log2_ring_size = mq->log2_ring_size;
+ ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0;
+
+ e->next = NULL;
+ if (c->msg_queue == NULL)
+ {
+ c->msg_queue = e;
+ return MEMIF_ERR_SUCCESS; /* 0 */
+ }
+
+ memif_msg_queue_elt_t *cur = c->msg_queue;
+ while (cur->next != NULL)
+ {
+ cur = cur->next;
+ }
+ cur->next = e;
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+/* used as connection request from slave */
+static_fn int
+memif_msg_enq_connect (memif_connection_t * c)
+{
+ memif_msg_queue_elt_t *e =
+ (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
+ if (e == NULL)
+ return memif_syscall_error_handler (errno);
+
+ memset (&e->msg, 0, sizeof (e->msg));
+ memif_msg_connect_t *cm = &e->msg.connect;
+
+ e->msg.type = MEMIF_MSG_TYPE_CONNECT;
+ e->fd = -1;
+ strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
+ strlen ((char *) c->args.interface_name));
+
+ e->next = NULL;
+ if (c->msg_queue == NULL)
+ {
+ c->msg_queue = e;
+ return MEMIF_ERR_SUCCESS; /* 0 */
+ }
+
+ memif_msg_queue_elt_t *cur = c->msg_queue;
+ while (cur->next != NULL)
+ {
+ cur = cur->next;
+ }
+ cur->next = e;
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+/* used as confirmation of connection by master */
+static_fn int
+memif_msg_enq_connected (memif_connection_t * c)
+{
+ memif_msg_queue_elt_t *e =
+ (memif_msg_queue_elt_t *) malloc (sizeof (memif_msg_queue_elt_t));
+ if (e == NULL)
+ return memif_syscall_error_handler (errno);
+
+ memset (&e->msg, 0, sizeof (e->msg));
+ memif_msg_connected_t *cm = &e->msg.connected;
+
+ e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
+ e->fd = -1;
+ strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
+ strlen ((char *) c->args.interface_name));
+
+ e->next = NULL;
+ if (c->msg_queue == NULL)
+ {
+ c->msg_queue = e;
+ return MEMIF_ERR_SUCCESS; /* 0 */
+ }
+
+ memif_msg_queue_elt_t *cur = c->msg_queue;
+ while (cur->next != NULL)
+ {
+ cur = cur->next;
+ }
+ cur->next = e;
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+/* immediately send disconnect msg */
+ /* specifie protocol for disconnect msg err_code
+ so that it will be compatible with VPP? (header/doc) */
+int
+memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
+{
+ memif_msg_t msg = { 0 };
+ memif_msg_disconnect_t *d = &msg.disconnect;
+
+ msg.type = MEMIF_MSG_TYPE_DISCONNECT;
+ d->code = err_code;
+ uint16_t l = strlen ((char *) err_string);
+ if (l > 96)
+ {
+ DBG ("Disconnect string too long. Sending first 96 characters.");
+ l = 96;
+ }
+ strncpy ((char *) d->string, (char *) err_string, l);
+
+ return memif_msg_send (fd, &msg, -1);
+}
+
+static_fn int
+memif_msg_receive_hello (memif_connection_t * c, memif_msg_t * msg)
+{
+ memif_msg_hello_t *h = &msg->hello;
+
+ if (msg->hello.min_version > MEMIF_VERSION ||
+ msg->hello.max_version < MEMIF_VERSION)
+ {
+ DBG ("incompatible protocol version");
+ return MEMIF_ERR_PROTO;
+ }
+
+ c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1,
+ c->args.num_s2m_rings);
+ c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1,
+ c->args.num_m2s_rings);
+ c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size,
+ c->args.log2_ring_size);
+ c->run_args.buffer_size = c->args.buffer_size;
+ strncpy ((char *) c->remote_name, (char *) h->name,
+ strlen ((char *) h->name));
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+/* handle interface identification (id, secret (optional)) */
+static_fn int
+memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
+{
+ memif_msg_init_t *i = &msg->init;
+ memif_list_elt_t *elt = NULL;
+ memif_list_elt_t elt2;
+ memif_connection_t *c = NULL;
+ libmemif_main_t *lm = &libmemif_main;
+ uint8_t err_string[96];
+ memset (err_string, 0, sizeof (char) * 96);
+ int err = MEMIF_ERR_SUCCESS; /* 0 */
+ int err_disc;
+ if (i->version != MEMIF_VERSION)
+ {
+ DBG ("MEMIF_VER_ERR");
+ strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR));
+ err = MEMIF_ERR_PROTO;
+ goto error;
+ }
+
+ get_list_elt (&elt, ms->interface_list, ms->interface_list_len, i->id);
+ if (elt == NULL)
+ {
+ DBG ("MEMIF_ID_ERR");
+ strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR));
+ err = MEMIF_ERR_ID;
+ goto error;
+ }
+
+ c = (memif_connection_t *) elt->data_struct;
+
+ if (!(c->args.is_master))
+ {
+ DBG ("MEMIF_SLAVE_ERR");
+ strncpy ((char *) err_string, MEMIF_SLAVE_ERR,
+ strlen (MEMIF_SLAVE_ERR));
+ err = MEMIF_ERR_ACCSLAVE;
+ goto error;
+ }
+ if (c->fd != -1)
+ {
+ DBG ("MEMIF_CONN_ERR");
+ strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR));
+ err = MEMIF_ERR_ALRCONN;
+ goto error;
+ }
+
+ c->fd = fd;
+
+ if (i->mode != c->args.mode)
+ {
+ DBG ("MEMIF_MODE_ERR");
+ strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR));
+ err = MEMIF_ERR_MODE;
+ goto error;
+ }
+
+ strncpy ((char *) c->remote_name, (char *) i->name,
+ strlen ((char *) i->name));
+
+ if (c->args.secret)
+ {
+ int r;
+ if (i->secret)
+ {
+ if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret))
+ {
+ DBG ("MEMIF_SECRET_ERR");
+ strncpy ((char *) err_string,
+ MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
+ err = MEMIF_ERR_SECRET;
+ goto error;
+ }
+ r = strncmp ((char *) i->secret, (char *) c->args.secret,
+ strlen ((char *) c->args.secret));
+ if (r != 0)
+ {
+ DBG ("MEMIF_SECRET_ERR");
+ strncpy ((char *) err_string,
+ MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
+ err = MEMIF_ERR_SECRET;
+ goto error;
+ }
+ }
+ else
+ {
+ DBG ("MEMIF_NOSECRET_ERR");
+ strncpy ((char *) err_string,
+ MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR));
+ err = MEMIF_ERR_NOSECRET;
+ goto error;
+ }
+ }
+
+ c->read_fn = memif_conn_fd_read_ready;
+ c->write_fn = memif_conn_fd_write_ready;
+ c->error_fn = memif_conn_fd_error;
+
+ elt2.key = c->fd;
+ elt2.data_struct = c;
+
+ add_list_elt (&elt2, &lm->control_list, &lm->control_list_len);
+ free_list_elt (lm->pending_list, lm->pending_list_len, fd);
+
+ return err;
+
+error:
+ memif_msg_send_disconnect (fd, err_string, 0);
+ lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL);
+ free_list_elt (lm->pending_list, lm->pending_list_len, fd);
+ close (fd);
+ fd = -1;
+ return err;
+}
+
+/* receive region information and add new region to connection (if possible) */
+static_fn int
+memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
+ int fd)
+{
+ memif_msg_add_region_t *ar = &msg->add_region;
+ memif_region_t *mr;
+ if (fd < 0)
+ return MEMIF_ERR_NO_SHMFD;
+
+ if (ar->index > MEMIF_MAX_REGION)
+ return MEMIF_ERR_MAXREG;
+
+ mr =
+ (memif_region_t *) realloc (c->regions,
+ sizeof (memif_region_t) * (ar->index + 1));
+ if (mr == NULL)
+ return memif_syscall_error_handler (errno);
+ c->regions = mr;
+ c->regions[ar->index].fd = fd;
+ c->regions[ar->index].region_size = ar->size;
+ c->regions[ar->index].shm = NULL;
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+/* receive ring information and add new ring to connection queue
+ (based on direction S2M | M2S) */
+static_fn int
+memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
+{
+ memif_msg_add_ring_t *ar = &msg->add_ring;
+
+ memif_queue_t *mq;
+
+ if (fd < 0)
+ return MEMIF_ERR_NO_INTFD;
+
+ if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M)
+ {
+ if (ar->index > MEMIF_MAX_S2M_RING)
+ return MEMIF_ERR_MAXRING;
+ if (ar->index >= c->args.num_s2m_rings)
+ return MEMIF_ERR_MAXRING;
+
+ mq =
+ (memif_queue_t *) realloc (c->rx_queues,
+ sizeof (memif_queue_t) * (ar->index + 1));
+ if (mq == NULL)
+ return memif_syscall_error_handler (errno);
+ c->rx_queues = mq;
+ c->rx_queues[ar->index].int_fd = fd;
+ c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
+ c->rx_queues[ar->index].region = ar->region;
+ c->rx_queues[ar->index].offset = ar->offset;
+ c->run_args.num_s2m_rings++;
+ }
+ else
+ {
+ if (ar->index > MEMIF_MAX_M2S_RING)
+ return MEMIF_ERR_MAXRING;
+ if (ar->index >= c->args.num_m2s_rings)
+ return MEMIF_ERR_MAXRING;
+
+ mq =
+ (memif_queue_t *) realloc (c->tx_queues,
+ sizeof (memif_queue_t) * (ar->index + 1));
+ if (mq == NULL)
+ return memif_syscall_error_handler (errno);
+ c->tx_queues = mq;
+ c->tx_queues[ar->index].int_fd = fd;
+ c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
+ c->tx_queues[ar->index].region = ar->region;
+ c->tx_queues[ar->index].offset = ar->offset;
+ c->run_args.num_m2s_rings++;
+ }
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+/* slave -> master */
+static_fn int
+memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
+{
+ memif_msg_connect_t *cm = &msg->connect;
+ libmemif_main_t *lm = &libmemif_main;
+ memif_list_elt_t elt;
+
+ int err;
+ err = memif_connect1 (c);
+ if (err != MEMIF_ERR_SUCCESS)
+ return err;
+
+ strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
+ strlen ((char *) cm->if_name));
+
+ int i;
+ if (c->on_interrupt != NULL)
+ {
+ for (i = 0; i < c->run_args.num_m2s_rings; i++)
+ {
+ elt.key = c->rx_queues[i].int_fd;
+ elt.data_struct = c;
+ add_list_elt (&elt, &lm->interrupt_list, &lm->interrupt_list_len);
+
+ lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
+ }
+
+ }
+
+ c->on_connect ((void *) c, c->private_ctx);
+
+ return err;
+}
+
+/* master -> slave */
+static_fn int
+memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
+{
+ memif_msg_connect_t *cm = &msg->connect;
+ libmemif_main_t *lm = &libmemif_main;
+
+ int err;
+ err = memif_connect1 (c);
+ if (err != MEMIF_ERR_SUCCESS)
+ return err;
+
+ strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
+ strlen ((char *) cm->if_name));
+
+ int i;
+ if (c->on_interrupt != NULL)
+ {
+ for (i = 0; i < c->run_args.num_s2m_rings; i++)
+ lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
+ }
+
+ c->on_connect ((void *) c, c->private_ctx);
+
+ return err;
+}
+
+static_fn int
+memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
+{
+ memif_msg_disconnect_t *d = &msg->disconnect;
+
+ memset (c->remote_disconnect_string, 0,
+ sizeof (c->remote_disconnect_string));
+ strncpy ((char *) c->remote_disconnect_string, (char *) d->string,
+ strlen ((char *) d->string));
+
+ /* on returning error, handle function will call memif_disconnect () */
+ DBG ("disconnect received: %s, mode: %d",
+ c->remote_disconnect_string, c->args.mode);
+ return MEMIF_ERR_DISCONNECT;
+}
+
+static_fn int
+memif_msg_receive (int ifd)
+{
+ char ctl[CMSG_SPACE (sizeof (int)) +
+ CMSG_SPACE (sizeof (struct ucred))] = { 0 };
+ struct msghdr mh = { 0 };
+ struct iovec iov[1];
+ memif_msg_t msg = { 0 };
+ ssize_t size;
+ int err = MEMIF_ERR_SUCCESS; /* 0 */
+ int fd = -1;
+ int i;
+ libmemif_main_t *lm = &libmemif_main;
+ memif_connection_t *c = NULL;
+ memif_socket_t *ms = NULL;
+ memif_list_elt_t *elt = NULL;
+
+ iov[0].iov_base = (void *) &msg;
+ iov[0].iov_len = sizeof (memif_msg_t);
+ mh.msg_iov = iov;
+ mh.msg_iovlen = 1;
+ mh.msg_control = ctl;
+ mh.msg_controllen = sizeof (ctl);
+
+ DBG ("recvmsg fd %d", ifd);
+ size = recvmsg (ifd, &mh, 0);
+ DBG ("done");
+ if (size != sizeof (memif_msg_t))
+ {
+ if (size == 0)
+ return MEMIF_ERR_DISCONNECTED;
+ else
+ return MEMIF_ERR_MFMSG;
+ }
+
+ struct ucred *cr = 0;
+ struct cmsghdr *cmsg;
+
+ cmsg = CMSG_FIRSTHDR (&mh);
+ while (cmsg)
+ {
+ if (cmsg->cmsg_level == SOL_SOCKET)
+ {
+ if (cmsg->cmsg_type == SCM_CREDENTIALS)
+ {
+ cr = (struct ucred *) CMSG_DATA (cmsg);
+ }
+ else if (cmsg->cmsg_type == SCM_RIGHTS)
+ {
+ int *fdp = (int *) CMSG_DATA (cmsg);
+ fd = *fdp;
+ }
+ }
+ cmsg = CMSG_NXTHDR (&mh, cmsg);
+ }
+
+ DBG ("Message type %u received", msg.type);
+
+ get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
+ if (elt != NULL)
+ c = (memif_connection_t *) elt->data_struct;
+
+ switch (msg.type)
+ {
+ case MEMIF_MSG_TYPE_ACK:
+ break;
+
+ case MEMIF_MSG_TYPE_HELLO:
+ if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS)
+ return err;
+ if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
+ return err;
+ if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS)
+ return err;
+ if ((err = memif_msg_enq_add_region (c, 0)) != MEMIF_ERR_SUCCESS)
+ return err;
+ for (i = 0; i < c->run_args.num_s2m_rings; i++)
+ {
+ if ((err =
+ memif_msg_enq_add_ring (c, i,
+ MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS)
+ return err;
+ }
+ for (i = 0; i < c->run_args.num_m2s_rings; i++)
+ {
+ if ((err =
+ memif_msg_enq_add_ring (c, i,
+ MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS)
+ return err;
+ }
+ if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS)
+ return err;
+ break;
+
+ case MEMIF_MSG_TYPE_INIT:
+ get_list_elt (&elt, lm->pending_list, lm->pending_list_len, ifd);
+ if (elt == NULL)
+ return -1;
+ ms = (memif_socket_t *) elt->data_struct;
+ if ((err = memif_msg_receive_init (ms, ifd, &msg)) != MEMIF_ERR_SUCCESS)
+ return err;
+ /* c->remote_pid = cr->pid */
+ /* c->remote_uid = cr->uid */
+ /* c->remote_gid = cr->gid */
+ get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
+ if (elt == NULL)
+ return -1;
+ c = (memif_connection_t *) elt->data_struct;
+ if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
+ return err;
+ break;
+
+ case MEMIF_MSG_TYPE_ADD_REGION:
+ if ((err =
+ memif_msg_receive_add_region (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
+ return err;
+ if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
+ return err;
+ break;
+
+ case MEMIF_MSG_TYPE_ADD_RING:
+ if ((err =
+ memif_msg_receive_add_ring (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
+ return err;
+ if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
+ return err;
+ break;
+
+ case MEMIF_MSG_TYPE_CONNECT:
+ if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS)
+ return err;
+ if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS)
+ return err;
+ break;
+
+ case MEMIF_MSG_TYPE_CONNECTED:
+ if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS)
+ return err;
+ break;
+
+ case MEMIF_MSG_TYPE_DISCONNECT:
+ if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS)
+ return err;
+ break;
+
+ default:
+ return MEMIF_ERR_UNKNOWN_MSG;;
+ break;
+ }
+
+ if (c != NULL)
+ c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
+/* libmemif_main_t *lm = &libmemif_main;
+ lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+int
+memif_conn_fd_error (memif_connection_t * c)
+{
+ DBG ("connection fd error");
+ strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19);
+ int err = memif_disconnect_internal (c);
+ return err;
+}
+
+/* calls memif_msg_receive to handle pending messages on socket */
+int
+memif_conn_fd_read_ready (memif_connection_t * c)
+{
+ int err;
+ err = memif_msg_receive (c->fd);
+ if (err != 0)
+ {
+ err = memif_disconnect_internal (c);
+ }
+ return err;
+}
+
+/* get msg from msg queue buffer and send it to socket */
+int
+memif_conn_fd_write_ready (memif_connection_t * c)
+{
+ int err = MEMIF_ERR_SUCCESS; /* 0 */
+
+
+ if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0)
+ goto done;
+
+ memif_msg_queue_elt_t *e = c->msg_queue;
+ if (e == NULL)
+ goto done;
+
+ c->msg_queue = c->msg_queue->next;
+
+ c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
+/*
+ libmemif_main_t *lm = &libmemif_main;
+
+ lm->control_fd_update (c->fd,
+ MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD);
+*/
+ err = memif_msg_send (c->fd, &e->msg, e->fd);
+ free (e);
+ goto done;
+
+done:
+ return err;
+}
+
+int
+memif_conn_fd_accept_ready (memif_socket_t * ms)
+{
+ int addr_len;
+ struct sockaddr_un client;
+ int conn_fd;
+ libmemif_main_t *lm = &libmemif_main;
+
+ DBG ("accept called");
+
+ addr_len = sizeof (client);
+ conn_fd =
+ accept (ms->fd, (struct sockaddr *) &client, (socklen_t *) & addr_len);
+
+ if (conn_fd < 0)
+ {
+ return memif_syscall_error_handler (errno);
+ }
+ DBG ("accept fd %d", ms->fd);
+ DBG ("conn fd %d", conn_fd);
+
+ memif_list_elt_t elt;
+ elt.key = conn_fd;
+ elt.data_struct = ms;
+
+ add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len);
+ lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE);
+
+ return memif_msg_send_hello (conn_fd);
+}
+
+int
+memif_read_ready (int fd)
+{
+ int err;
+ DBG ("call recv");
+ err = memif_msg_receive (fd);
+ DBG ("recv finished");
+ return err;
+}
diff --git a/extras/libmemif/src/socket.h b/extras/libmemif/src/socket.h
new file mode 100644
index 00000000..a8e9fbce
--- /dev/null
+++ b/extras/libmemif/src/socket.h
@@ -0,0 +1,89 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#ifndef _SOCKET_H_
+#define _SOCKET_H
+
+#include <memif_private.h>
+
+/* interface identification errors (disconnect messages)*/
+#define MEMIF_VER_ERR "incompatible version"
+#define MEMIF_ID_ERR "unmatched interface id"
+#define MEMIF_SLAVE_ERR "cannot connect to salve"
+#define MEMIF_CONN_ERR "already connected"
+#define MEMIF_MODE_ERR "mode mismatch"
+#define MEMIF_SECRET_ERR "incorrect secret"
+#define MEMIF_NOSECRET_ERR "secret required"
+
+/* socket.c */
+
+int memif_conn_fd_read_ready (memif_connection_t * c);
+
+int memif_conn_fd_write_ready (memif_connection_t * c);
+
+int memif_conn_fd_error (memif_connection_t * c);
+
+int memif_conn_fd_accept_ready (memif_socket_t * ms);
+
+int memif_read_ready (int fd);
+
+int memif_msg_send_disconnect (int fd, uint8_t * err_string,
+ uint32_t err_code);
+
+/* when compiling unit tests, compile functions without static keyword
+ and declare functions in header file */
+#ifdef MEMIF_UNIT_TEST
+#define static_fn
+
+int memif_msg_send (int fd, memif_msg_t * msg, int afd);
+
+int memif_msg_enq_ack (memif_connection_t * c);
+
+int memif_msg_send_hello (int fd);
+
+int memif_msg_enq_init (memif_connection_t * c);
+
+int memif_msg_enq_add_region (memif_connection_t * c, uint8_t region);
+
+int memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index,
+ uint8_t dir);
+
+int memif_msg_receive_hello (memif_connection_t * c, memif_msg_t * msg);
+
+int memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg);
+
+int memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
+ int fd);
+
+int memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg,
+ int fd);
+
+int memif_msg_enq_connect (memif_connection_t * c);
+
+int memif_msg_enq_connected (memif_connection_t * c);
+
+int memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg);
+
+int memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg);
+
+int memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg);
+
+#else
+#define static_fn static
+#endif /* MEMIF_UNIT_TEST */
+
+#endif /* _SOCKET_H_ */