summaryrefslogtreecommitdiffstats
path: root/extras
diff options
context:
space:
mode:
authorJakub Grajciar <jgrajcia@cisco.com>2018-08-20 14:26:32 +0200
committerDamjan Marion <dmarion@me.com>2018-08-30 12:35:46 +0000
commit93a5dd17232e42bea2fc9dcf59c652a9da31b2b3 (patch)
tree9f3c1b69bb96cc65353b468f8264204ca493913f /extras
parent43b06063015abfa42bc9c5ab925cd6b7ea3cbf42 (diff)
libmemif: external region support
region 0: descriptors region 1: buffers (external) Change-Id: Ia728967817b4c78bc00f8eed44606d0c5bc386b0 Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com>
Diffstat (limited to 'extras')
-rw-r--r--extras/libmemif/Makefile.am10
-rw-r--r--extras/libmemif/examples/icmp_responder-eb/main.c1073
-rw-r--r--extras/libmemif/examples/icmp_responder-epoll/main.c22
-rw-r--r--extras/libmemif/examples/icmp_responder-mt/main.c2
-rw-r--r--extras/libmemif/examples/icmp_responder/main.c2
-rw-r--r--extras/libmemif/src/libmemif.h98
-rw-r--r--extras/libmemif/src/main.c320
-rw-r--r--extras/libmemif/src/memif_private.h14
-rw-r--r--extras/libmemif/src/socket.c42
9 files changed, 1460 insertions, 123 deletions
diff --git a/extras/libmemif/Makefile.am b/extras/libmemif/Makefile.am
index 8640cf8cdb6..f66ef9695fa 100644
--- a/extras/libmemif/Makefile.am
+++ b/extras/libmemif/Makefile.am
@@ -78,7 +78,15 @@ icmpr_mt_SOURCES = examples/icmp_responder-mt/main.c \
icmpr_mt_LDADD = libmemif.la -lpthread
icmpr_mt_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src -I$(top_srcdir)/examples/icmp_responder
-noinst_PROGRAMS = icmpr icmpr-epoll icmpr-mt
+#
+# ICMP responder external buffer example
+#
+icmpr_eb_SOURCES = examples/icmp_responder-eb/main.c\
+ examples/icmp_responder/icmp_proto.c
+icmpr_eb_LDADD = libmemif.la -lpthread
+icmpr_eb_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src -I$(top_srcdir)/examples/icmp_responder
+
+noinst_PROGRAMS = icmpr icmpr-epoll icmpr-mt icmpr-eb
include_HEADERS = src/libmemif.h
diff --git a/extras/libmemif/examples/icmp_responder-eb/main.c b/extras/libmemif/examples/icmp_responder-eb/main.c
new file mode 100644
index 00000000000..9e71d283c2c
--- /dev/null
+++ b/extras/libmemif/examples/icmp_responder-eb/main.c
@@ -0,0 +1,1073 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2018 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 <stdlib.h>
+#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 <sys/epoll.h>
+#include <errno.h>
+#include <unistd.h>
+#include <signal.h>
+#include <pthread.h>
+#include <time.h>
+
+#ifndef TIME_UTC
+#define TIME_UTC 1
+#endif /* TIME_UTC */
+
+#include <libmemif.h>
+#include <icmp_proto.h>
+
+#define APP_NAME "ICMP_Responder"
+#define IF_NAME "memif_connection"
+
+#ifdef ICMP_DBG
+#define DBG(...) do { \
+ printf(APP_NAME":%s:%d",__func__,__LINE__); \
+ printf(__VA_ARGS__); \
+ printf("\n"); \
+ } while (0)
+#else
+#define DBG(...)
+#endif /* ICMP_DBG */
+
+#define INFO(...) do { \
+ printf("INFO: "__VA_ARGS__); \
+ printf("\n"); \
+ } while (0)
+
+#define MAX_MEMIF_BUFS 256
+#define MAX_CONNS 50
+
+
+#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
+
+#ifndef HAVE_MEMFD_CREATE
+static inline int
+memfd_create (const char *name, unsigned int flags)
+{
+ return syscall (__NR_memfd_create, name, flags);
+}
+#endif
+
+#ifndef F_LINUX_SPECIFIC_BASE
+#define F_LINUX_SPECIFIC_BASE 1024
+#endif
+
+#ifndef MFD_ALLOW_SEALING
+#define MFD_ALLOW_SEALING 0x0002U
+#endif
+
+#ifndef F_ADD_SEALS
+#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
+
+typedef struct
+{
+ uint16_t index;
+
+ memif_conn_handle_t conn;
+
+ uint16_t tx_buf_num;
+ uint16_t rx_buf_num;
+ memif_buffer_t *tx_bufs;
+ memif_buffer_t *rx_bufs;
+
+ uint8_t ip_addr[4];
+ uint64_t tx_counter, rx_counter, tx_err_counter;
+ uint64_t t_sec, t_nsec;
+} memif_connection_t;
+
+typedef struct
+{
+ uint16_t index;
+ uint64_t packet_num;
+ uint8_t ip_daddr[4];
+ uint8_t hw_daddr[6];
+} icmpr_thread_data_t;
+
+icmpr_thread_data_t icmpr_thread_data[MAX_CONNS];
+pthread_t thread[MAX_CONNS];
+int epfd;
+long ctx[MAX_CONNS];
+memif_connection_t memif_connection[MAX_CONNS];
+
+static void
+print_help ()
+{
+ printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
+#ifdef ICMP_DBG
+ printf (" (debug)");
+#endif
+ printf ("\n");
+ printf ("==============================\n");
+ printf ("libmemif version: %s", LIBMEMIF_VERSION);
+#ifdef MEMIF_DBG
+ printf (" (debug)");
+#endif
+ printf ("\n");
+ printf ("memif version: %d\n", memif_get_version ());
+ printf ("commands:\n");
+ printf ("\thelp - prints this help\n");
+ printf ("\texit - exit app\n");
+ printf
+ ("\tconn <index> <mode> <interrupt> - create memif. index used as interface id. mode: 0 = slave | 1 = master. interrupt: none = default | 1 = handle ARP only\n");
+ printf ("\tdel <index> - delete memif\n");
+ printf ("\tshow - show connection details\n");
+ printf ("\tsend <index> <tx> <ip> <mac> - send icmp\n");
+}
+
+static void
+print_memif_details ()
+{
+ memif_details_t md;
+ ssize_t buflen;
+ char *buf;
+ int err, i, e;
+ buflen = 2048;
+ buf = malloc (buflen);
+ printf ("MEMIF DETAILS\n");
+ printf ("==============================\n");
+ for (i = 0; i < MAX_CONNS; i++)
+ {
+ memif_connection_t *c = &memif_connection[i];
+
+ memset (&md, 0, sizeof (md));
+ memset (buf, 0, buflen);
+
+ err = memif_get_details (c->conn, &md, buf, buflen);
+ if (err != MEMIF_ERR_SUCCESS)
+ {
+ if (err != MEMIF_ERR_NOCONN)
+ INFO ("%s", memif_strerror (err));
+ continue;
+ }
+
+ printf ("interface index: %d\n", i);
+
+ printf ("\tinterface ip: %u.%u.%u.%u\n",
+ c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
+ printf ("\tinterface name: %s\n", (char *) md.if_name);
+ printf ("\tapp name: %s\n", (char *) md.inst_name);
+ printf ("\tremote interface name: %s\n", (char *) md.remote_if_name);
+ printf ("\tremote app name: %s\n", (char *) md.remote_inst_name);
+ printf ("\tid: %u\n", md.id);
+ printf ("\tsecret: %s\n", (char *) md.secret);
+ printf ("\trole: ");
+ if (md.role)
+ printf ("slave\n");
+ else
+ printf ("master\n");
+ printf ("\tmode: ");
+ switch (md.mode)
+ {
+ case 0:
+ printf ("ethernet\n");
+ break;
+ case 1:
+ printf ("ip\n");
+ break;
+ case 2:
+ printf ("punt/inject\n");
+ break;
+ default:
+ printf ("unknown\n");
+ break;
+ }
+ printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
+ printf ("\tregions:\n");
+ for (e = 0; e < md.regions_num; e++)
+ {
+ printf ("\t\tindex: %u\n", md.regions[e].index);
+ printf ("\t\taddress: %p\n", md.regions[e].addr);
+ printf ("\t\tsize: %u\n", md.regions[e].size);
+ printf ("\t\tfd: %d\n", md.regions[e].fd);
+ if (md.regions[e].is_external)
+ printf ("\t\texternal\n");
+ }
+ printf ("\trx queues:\n");
+ for (e = 0; e < md.rx_queues_num; e++)
+ {
+ printf ("\t\tqueue id: %u\n", md.rx_queues[e].qid);
+ printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
+ printf ("\t\tring rx mode: %s\n",
+ md.rx_queues[e].flags ? "polling" : "interrupt");
+ printf ("\t\tring head: %u\n", md.rx_queues[e].head);
+ printf ("\t\tring tail: %u\n", md.rx_queues[e].tail);
+ printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
+ }
+ printf ("\ttx queues:\n");
+ for (e = 0; e < md.tx_queues_num; e++)
+ {
+ printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid);
+ printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
+ printf ("\t\tring rx mode: %s\n",
+ md.tx_queues[e].flags ? "polling" : "interrupt");
+ printf ("\t\tring head: %u\n", md.tx_queues[e].head);
+ printf ("\t\tring tail: %u\n", md.tx_queues[e].tail);
+ printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
+ }
+ printf ("\tlink: ");
+ if (md.link_up_down)
+ printf ("up\n");
+ else
+ printf ("down\n");
+ }
+ free (buf);
+}
+
+void
+icmpr_print_counters ()
+{
+ int i;
+ for (i = 0; i < MAX_CONNS; i++)
+ {
+ memif_connection_t *c = &memif_connection[i];
+ if (c->conn == NULL)
+ continue;
+ printf ("===============================\n");
+ printf ("interface index: %d\n", c->index);
+ printf ("\trx: %lu\n", c->rx_counter);
+ printf ("\ttx: %lu\n", c->tx_counter);
+ printf ("\ttx_err: %lu\n", c->tx_err_counter);
+ }
+}
+
+void
+icmpr_reset_counters ()
+{
+ int i;
+ for (i = 0; i < MAX_CONNS; i++)
+ {
+ memif_connection_t *c = &memif_connection[i];
+ if (c->conn == NULL)
+ continue;
+ c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
+ }
+}
+
+int
+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 (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;
+}
+
+int
+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 (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;
+}
+
+int
+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 (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
+on_connect (memif_conn_handle_t conn, void *private_ctx)
+{
+ INFO ("memif connected!");
+ memif_refill_queue (conn, 0, -1, 0);
+ return 0;
+}
+
+int
+on_disconnect (memif_conn_handle_t conn, void *private_ctx)
+{
+ INFO ("memif disconnected!");
+ return 0;
+}
+
+int
+control_fd_update (int fd, uint8_t events)
+{
+ /* convert memif event definitions to epoll events */
+ if (events & MEMIF_FD_EVENT_DEL)
+ return 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 mod_epoll_fd (fd, evt);
+
+ return add_epoll_fd (fd, evt);
+}
+
+int
+icmpr_memif_delete (long index)
+{
+ if (index >= MAX_CONNS)
+ {
+ INFO ("connection array overflow");
+ return 0;
+ }
+ if (index < 0)
+ {
+ INFO ("don't even try...");
+ return 0;
+ }
+ memif_connection_t *c = &memif_connection[index];
+
+ if (c->rx_bufs)
+ free (c->rx_bufs);
+ c->rx_bufs = NULL;
+ c->rx_buf_num = 0;
+ if (c->tx_bufs)
+ free (c->tx_bufs);
+ c->tx_bufs = NULL;
+ c->tx_buf_num = 0;
+
+ int err;
+ /* disconenct then delete memif connection */
+ err = memif_delete (&c->conn);
+ if (err != MEMIF_ERR_SUCCESS)
+ INFO ("memif_delete: %s", memif_strerror (err));
+ if (c->conn != NULL)
+ INFO ("memif delete fail");
+ return 0;
+}
+
+int
+icmpr_free ()
+{
+ /* application cleanup */
+ int err;
+ long i;
+ for (i = 0; i < MAX_CONNS; i++)
+ {
+ memif_connection_t *c = &memif_connection[i];
+ if (c->conn)
+ icmpr_memif_delete (i);
+ }
+
+ err = memif_cleanup ();
+ if (err != MEMIF_ERR_SUCCESS)
+ INFO ("memif_delete: %s", memif_strerror (err));
+
+ return 0;
+}
+
+int
+icmpr_add_external_region (void * *addr, uint32_t size, int *fd,
+ void *private_ctx)
+{
+
+ int rfd;
+ void *raddr;
+ int err;
+
+ rfd = memfd_create ("memif region 1", MFD_ALLOW_SEALING);
+
+ fcntl (rfd, F_ADD_SEALS, F_SEAL_SHRINK);
+
+ err = ftruncate (rfd, size);
+
+ raddr = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, rfd, 0);
+
+ *addr = raddr;
+ *fd = rfd;
+
+ return 0;
+}
+
+void *
+icmpr_get_external_region_addr (uint32_t size, int fd, void *private_ctx)
+{
+ return mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+}
+
+int
+icmpr_del_external_region (void *addr, uint32_t size, int fd,
+ void *private_ctx)
+{
+ munmap (addr, size);
+ if (fd > 0)
+ close (fd);
+ fd = -1;
+ return 0;
+}
+
+int
+on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
+{
+ long index = *((long *) private_ctx);
+ memif_connection_t *c = &memif_connection[index];
+ if (c->index != index)
+ {
+ INFO ("invalid context: %ld/%u", index, c->index);
+ return 0;
+ }
+
+ int err = MEMIF_ERR_SUCCESS, ret_val;
+ uint16_t rx = 0, tx = 0;
+ uint16_t fb = 0;
+ int i = 0;
+ int j = 0;
+
+ do
+ {
+ err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
+ ret_val = err;
+ c->rx_counter += rx;
+ if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
+ {
+ INFO ("memif_rx_burst: %s", memif_strerror (err));
+ goto error;
+ }
+ i = 0;
+ memset (c->tx_bufs, 0, sizeof (memif_buffer_t) * rx);
+ err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 128);
+ if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
+ {
+ INFO ("memif_buffer_alloc: %s", memif_strerror (err));
+ goto error;
+ }
+ j = 0;
+ c->tx_err_counter += rx - tx;
+
+ while (tx)
+ {
+ resolve_packet ((void *) (c->rx_bufs + i)->data,
+ (c->rx_bufs + i)->len,
+ (void *) (c->tx_bufs + j)->data,
+ &(c->tx_bufs + j)->len, c->ip_addr);
+ i++;
+ j++;
+ tx--;
+ }
+
+ err = memif_refill_queue (c->conn, qid, rx, 0);
+ if (err != MEMIF_ERR_SUCCESS)
+ INFO ("memif_buffer_free: %s", memif_strerror (err));
+ rx -= rx;
+
+ DBG ("%u/%u alloc/free buffers", rx, MAX_MEMIF_BUFS - rx);
+
+ err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
+ if (err != MEMIF_ERR_SUCCESS)
+ {
+ INFO ("memif_tx_burst: %s", memif_strerror (err));
+ goto error;
+ }
+ c->tx_counter += tx;
+ }
+ while (ret_val == MEMIF_ERR_NOBUF);
+
+ return 0;
+
+error:
+ err = memif_refill_queue (c->conn, qid, rx, 0);
+ if (err != MEMIF_ERR_SUCCESS)
+ INFO ("memif_buffer_free: %s", memif_strerror (err));
+ c->rx_buf_num -= rx;
+ DBG ("freed %d buffers. %u/%u alloc/free buffers", fb, c->rx_buf_num,
+ MAX_MEMIF_BUFS - c->rx_buf_num);
+ return 0;
+}
+
+int
+on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
+{
+ long index = *((long *) private_ctx);
+ memif_connection_t *c = &memif_connection[index];
+ if (c->index != index)
+ {
+ INFO ("invalid context: %ld/%u", index, c->index);
+ return 0;
+ }
+
+ int err = MEMIF_ERR_SUCCESS, ret_val;
+ int i;
+ uint16_t rx, tx;
+ uint16_t fb;
+ uint16_t pck_seq;
+
+ do
+ {
+ /* receive data from shared memory buffers */
+ err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
+ ret_val = err;
+ if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
+ {
+ INFO ("memif_rx_burst: %s", memif_strerror (err));
+ goto error;
+ }
+ c->rx_buf_num += rx;
+ c->rx_counter += rx;
+
+ for (i = 0; i < rx; i++)
+ {
+ if (((struct ether_header *) (c->rx_bufs + i)->data)->ether_type ==
+ 0x0608)
+ {
+ err =
+ memif_buffer_alloc (c->conn, qid, c->tx_bufs, 1, &tx, 128);
+ if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
+ {
+ INFO ("memif_buffer_alloc: %s", memif_strerror (err));
+ goto error;
+ }
+ resolve_packet ((void *) (c->rx_bufs + i)->data,
+ (c->rx_bufs + i)->len,
+ (void *) (c->tx_bufs + i)->data,
+ &(c->tx_bufs + i)->len, c->ip_addr);
+ err =
+ memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &tx);
+ if (err != MEMIF_ERR_SUCCESS)
+ INFO ("memif_tx_burst: %s", memif_strerror (err));
+ c->tx_buf_num -= tx;
+ c->tx_counter += tx;
+ }
+ }
+
+ err = memif_refill_queue (c->conn, qid, rx, 0);
+ if (err != MEMIF_ERR_SUCCESS)
+ INFO ("memif_buffer_free: %s", memif_strerror (err));
+ c->rx_buf_num -= rx;
+
+
+ }
+ while (ret_val == MEMIF_ERR_NOBUF);
+
+ return 0;
+
+error:
+ err = memif_refill_queue (c->conn, qid, rx, 0);
+ if (err != MEMIF_ERR_SUCCESS)
+ INFO ("memif_buffer_free: %s", memif_strerror (err));
+ c->rx_buf_num -= rx;
+ DBG ("freed %d buffers. %u/%u alloc/free buffers",
+ fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
+ return 0;
+}
+
+int
+icmpr_memif_create (long index, long mode, char *s)
+{
+ if (index >= MAX_CONNS)
+ {
+ INFO ("connection array overflow");
+ return 0;
+ }
+ if (index < 0)
+ {
+ INFO ("...");
+ return 0;
+ }
+ memif_connection_t *c = &memif_connection[index];
+
+ memif_conn_args_t args;
+ int fd = -1;
+ memset (&args, 0, sizeof (args));
+ args.is_master = mode;
+ args.log2_ring_size = 11;
+ args.buffer_size = 2048;
+ args.num_s2m_rings = 1;
+ args.num_m2s_rings = 1;
+ strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
+ args.mode = 0;
+ args.interface_id = index;
+
+ int err;
+ if (s == NULL)
+ {
+ err = memif_create (&c->conn,
+ &args, on_connect, on_disconnect, on_interrupt,
+ &ctx[index]);
+ if (err != MEMIF_ERR_SUCCESS)
+ {
+ INFO ("memif_create: %s", memif_strerror (err));
+ return 0;
+ }
+ }
+ else
+ {
+ if (strncmp (s, "1", 1) == 0)
+ {
+ err = memif_create (&c->conn,
+ &args, on_connect, on_disconnect, on_interrupt1,
+ &ctx[index]);
+ if (err != MEMIF_ERR_SUCCESS)
+ {
+ INFO ("memif_create: %s", memif_strerror (err));
+ return 0;
+ }
+ }
+ else
+ {
+ INFO ("Unknown interrupt descriptor");
+ goto done;
+ }
+ }
+
+ c->index = index;
+ c->rx_buf_num = 0;
+ c->rx_bufs =
+ (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
+ c->tx_buf_num = 0;
+ c->tx_bufs =
+ (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
+
+ c->ip_addr[0] = 192;
+ c->ip_addr[1] = 168;
+ c->ip_addr[2] = c->index + 1;
+ c->ip_addr[3] = 2;
+
+done:
+ return 0;
+}
+
+void *
+icmpr_send_proc (void *data)
+{
+ icmpr_thread_data_t *d = (icmpr_thread_data_t *) data;
+ int index = d->index;
+ uint64_t count = d->packet_num;
+ memif_connection_t *c = &memif_connection[index];
+ if (c->conn == NULL)
+ {
+ INFO ("No connection at index %d.", index);
+ goto error;
+ }
+ uint16_t tx, i;
+ int err = MEMIF_ERR_SUCCESS;
+ uint32_t seq = 0;
+ struct timespec start, end;
+ memset (&start, 0, sizeof (start));
+ memset (&end, 0, sizeof (end));
+
+ timespec_get (&start, TIME_UTC);
+ while (count)
+ {
+ i = 0;
+ err =
+ memif_buffer_alloc (c->conn, 0, c->tx_bufs,
+ MAX_MEMIF_BUFS > count ? count : MAX_MEMIF_BUFS,
+ &tx, 128);
+
+ if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
+ {
+ INFO ("memif_buffer_alloc: %s", memif_strerror (err));
+ goto error;
+ }
+ c->tx_buf_num += tx;
+
+ while (tx)
+ {
+ while (tx > 4)
+ {
+ generate_packet ((void *) c->tx_bufs[i].data,
+ &c->tx_bufs[i].len, c->ip_addr, d->ip_daddr,
+ d->hw_daddr, seq++);
+ generate_packet ((void *) c->tx_bufs[i + 1].data,
+ &c->tx_bufs[i + 1].len, c->ip_addr,
+ d->ip_daddr, d->hw_daddr, seq++);
+ generate_packet ((void *) c->tx_bufs[i + 2].data,
+ &c->tx_bufs[i + 2].len, c->ip_addr,
+ d->ip_daddr, d->hw_daddr, seq++);
+ generate_packet ((void *) c->tx_bufs[i + 3].data,
+ &c->tx_bufs[i + 3].len, c->ip_addr,
+ d->ip_daddr, d->hw_daddr, seq++);
+ i += 4;
+ tx -= 4;
+ }
+ generate_packet ((void *) c->tx_bufs[i].data, &c->tx_bufs[i].len,
+ c->ip_addr, d->ip_daddr, d->hw_daddr, seq++);
+ i++;
+ tx--;
+ }
+
+ err = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num, &tx);
+ if (err != MEMIF_ERR_SUCCESS)
+ {
+ INFO ("memif_tx_burst: %s", memif_strerror (err));
+ goto error;
+ }
+ c->tx_buf_num -= tx;
+ c->tx_counter += tx;
+ count -= tx;
+ }
+
+ timespec_get (&end, TIME_UTC);
+ INFO ("\n\nPacket sequence finished!\nSeq len: %u", seq);
+ uint64_t t1 = end.tv_sec - start.tv_sec;
+ uint64_t t2;
+ if (end.tv_nsec > start.tv_nsec)
+ {
+ t2 = end.tv_nsec - start.tv_nsec;
+ }
+ else
+ {
+ t2 = start.tv_nsec - end.tv_nsec;
+ t1--;
+ }
+ c->t_sec = t1;
+ c->t_nsec = t2;
+ double tmp = t1;
+ tmp += t2 / 1e+9;
+ tmp = seq / tmp;
+ INFO ("Seq time: %lus %luns\nAverage pps: %f", t1, t2, tmp);
+
+error:
+ INFO ("Thread exiting...");
+ pthread_exit (NULL);
+}
+
+
+int
+icmpr_send (long index, long packet_num, char *hw, char *ip)
+{
+ if (index >= MAX_CONNS)
+ {
+ INFO ("connection array overflow");
+ return 0;
+ }
+ if (index < 0)
+ {
+ INFO ("...");
+ return 0;
+ }
+ memif_connection_t *c = &memif_connection[index];
+ if (c->conn == NULL)
+ return -1;
+ int err, i;
+ uint16_t tx = 0, rx = 0;
+ char *end, *ui;
+ uint8_t tmp[6];
+ icmpr_thread_data_t *data = &icmpr_thread_data[index];
+ data->index = index;
+ data->packet_num = packet_num;
+
+ ui = strtok (ip, ".");
+ if (ui == NULL)
+ goto error;
+ tmp[0] = strtol (ui, &end, 10);
+ ui = strtok (NULL, ".");
+ if (ui == NULL)
+ goto error;
+ tmp[1] = strtol (ui, &end, 10);
+ ui = strtok (NULL, ".");
+ if (ui == NULL)
+ goto error;
+ tmp[2] = strtol (ui, &end, 10);
+ ui = strtok (NULL, ".");
+ if (ui == NULL)
+ goto error;
+ tmp[3] = strtol (ui, &end, 10);
+
+ data->ip_daddr[0] = tmp[0];
+ data->ip_daddr[1] = tmp[1];
+ data->ip_daddr[2] = tmp[2];
+ data->ip_daddr[3] = tmp[3];
+
+ ui = strtok (hw, ":");
+ if (ui == NULL)
+ goto error;
+ tmp[0] = strtol (ui, &end, 16);
+ ui = strtok (NULL, ":");
+ if (ui == NULL)
+ goto error;
+ tmp[1] = strtol (ui, &end, 16);
+ ui = strtok (NULL, ":");
+ if (ui == NULL)
+ goto error;
+ tmp[2] = strtol (ui, &end, 16);
+ ui = strtok (NULL, ":");
+ if (ui == NULL)
+ goto error;
+ tmp[3] = strtol (ui, &end, 16);
+ ui = strtok (NULL, ":");
+ if (ui == NULL)
+ goto error;
+ tmp[4] = strtol (ui, &end, 16);
+ ui = strtok (NULL, ":");
+ if (ui == NULL)
+ goto error;
+ tmp[5] = strtol (ui, &end, 16);
+
+ pthread_create (&thread[index], NULL, icmpr_send_proc, (void *) data);
+ return 0;
+
+error:
+ INFO ("Invalid input\n");
+ return 0;
+}
+
+int
+user_input_handler ()
+{
+ char *in = (char *) malloc (256);
+ char *ui = fgets (in, 256, stdin);
+ char *end;
+ long a;
+
+ if (in[0] == '\n')
+ goto done;
+
+ ui = strtok (in, " ");
+
+ if (strncmp (ui, "exit", 4) == 0)
+ {
+ free (in);
+ icmpr_free ();
+ exit (EXIT_SUCCESS);
+ }
+ else if (strncmp (ui, "help", 4) == 0)
+ {
+ print_help ();
+ goto done;
+ }
+ else if (strncmp (ui, "conn", 4) == 0)
+ {
+ ui = strtok (NULL, " ");
+ if (ui != NULL)
+ a = strtol (ui, &end, 10);
+ else
+ {
+ INFO ("expected id");
+ goto done;
+ }
+ ui = strtok (NULL, " ");
+ if (ui != NULL)
+ icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
+ else
+ INFO ("expected mode <0|1>");
+ goto done;
+ }
+ else if (strncmp (ui, "del", 3) == 0)
+ {
+ ui = strtok (NULL, " ");
+ if (ui != NULL)
+ icmpr_memif_delete (strtol (ui, &end, 10));
+ else
+ {
+ INFO ("expected id");
+ goto done;
+ }
+ }
+ else if (strncmp (ui, "send", 4) == 0)
+ {
+ ui = strtok (NULL, " ");
+ if (ui != NULL)
+ a = strtol (ui, &end, 10);
+ else
+ {
+ INFO ("expected id");
+ goto done;
+ }
+ ui = strtok (NULL, " ");
+ if (ui != NULL)
+ icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "),
+ strtok (NULL, " "));
+ else
+ INFO ("expected count");
+ goto done;
+ }
+ else if (strncmp (ui, "show", 4) == 0)
+ {
+ print_memif_details ();
+ goto done;
+ }
+
+done:
+ free (in);
+ return 0;
+}
+
+int
+poll_event (int timeout)
+{
+ struct epoll_event evt, *e;
+ int app_err = 0, memif_err = 0, en = 0;
+ int tmp, nfd;
+ uint32_t events = 0;
+ memset (&evt, 0, sizeof (evt));
+ evt.events = EPOLLIN | EPOLLOUT;
+ sigset_t sigset;
+ sigemptyset (&sigset);
+ en = epoll_pwait (epfd, &evt, 1, timeout, &sigset);
+ /* id event polled */
+ if (en < 0)
+ {
+ DBG ("epoll_pwait: %s", strerror (errno));
+ return -1;
+ }
+ if (en > 0)
+ {
+ /* this app does not use any other file descriptors than stds and memif control fds */
+ if (evt.data.fd > 2)
+ {
+ /* event of memif control fd */
+ /* convert epolle events to memif events */
+ 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;
+ memif_err = memif_control_fd_handler (evt.data.fd, events);
+ if (memif_err != MEMIF_ERR_SUCCESS)
+ INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
+ }
+ else if (evt.data.fd == 0)
+ {
+ app_err = user_input_handler ();
+ }
+ else
+ {
+ DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
+ }
+ }
+
+ if ((app_err < 0) || (memif_err < 0))
+ {
+ if (app_err < 0)
+ DBG ("user input handler error");
+ if (memif_err < 0)
+ DBG ("memif control fd handler error");
+ return -1;
+ }
+
+ return 0;
+}
+
+int
+main ()
+{
+ epfd = epoll_create (1);
+ add_epoll_fd (0, EPOLLIN);
+
+ /* initialize memory interface */
+ int err, i;
+ /* if valid callback is passed as argument, fd event polling will be done by user
+ all file descriptors and events will be passed to user in this callback */
+ /* if callback is set to NULL libmemif will handle fd event polling */
+ err = memif_init (control_fd_update, APP_NAME, NULL, NULL, NULL);
+ if (err != MEMIF_ERR_SUCCESS)
+ {
+ INFO ("memif_init: %s", memif_strerror (err));
+ icmpr_free ();
+ exit (-1);
+ }
+
+ for (i = 0; i < MAX_CONNS; i++)
+ {
+ memset (&memif_connection[i], 0, sizeof (memif_connection_t));
+ ctx[i] = i;
+ }
+
+ memif_register_external_region (icmpr_add_external_region,
+ icmpr_get_external_region_addr,
+ icmpr_del_external_region, NULL);
+
+ print_help ();
+
+ /* main loop */
+ while (1)
+ {
+ if (poll_event (-1) < 0)
+ {
+ DBG ("poll_event error!");
+ }
+ }
+}
diff --git a/extras/libmemif/examples/icmp_responder-epoll/main.c b/extras/libmemif/examples/icmp_responder-epoll/main.c
index fd354a38aaf..70095ddfed5 100644
--- a/extras/libmemif/examples/icmp_responder-epoll/main.c
+++ b/extras/libmemif/examples/icmp_responder-epoll/main.c
@@ -86,6 +86,8 @@
#define MAX_MEMIF_BUFS 256
#define MAX_CONNS 50
+#define ICMPR_HEADROOM 64
+
int epfd;
int out_fd;
uint8_t enable_log;
@@ -282,7 +284,7 @@ int
on_connect (memif_conn_handle_t conn, void *private_ctx)
{
INFO ("memif connected!");
- memif_refill_queue (conn, 0, -1, 0);
+ memif_refill_queue (conn, 0, -1, ICMPR_HEADROOM);
enable_log = 1;
return 0;
}
@@ -406,13 +408,12 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
tx--;
}
- err = memif_refill_queue (c->conn, qid, rx, 0);
+ err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
if (err != MEMIF_ERR_SUCCESS)
INFO ("memif_buffer_free: %s", memif_strerror (err));
rx -= rx;
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- rx, rx, MAX_MEMIF_BUFS - rx);
+ DBG ("%u/%u alloc/free buffers", rx, MAX_MEMIF_BUFS - rx);
err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
if (err != MEMIF_ERR_SUCCESS)
@@ -428,7 +429,7 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
return 0;
error:
- err = memif_refill_queue (c->conn, qid, rx, 0);
+ err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
if (err != MEMIF_ERR_SUCCESS)
INFO ("memif_buffer_free: %s", memif_strerror (err));
c->rx_buf_num -= rx;
@@ -514,7 +515,7 @@ on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
}
/* mark memif buffers and shared memory buffers as free */
/* free processed buffers */
- err = memif_refill_queue (c->conn, qid, j, 0);
+ err = memif_refill_queue (c->conn, qid, j, ICMPR_HEADROOM);
if (err != MEMIF_ERR_SUCCESS)
INFO ("memif_buffer_free: %s", memif_strerror (err));
rx -= j;
@@ -540,7 +541,7 @@ on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
return 0;
error:
- err = memif_refill_queue (c->conn, qid, rx, 0);
+ err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
if (err != MEMIF_ERR_SUCCESS)
INFO ("memif_buffer_free: %s", memif_strerror (err));
c->rx_buf_num -= rx;
@@ -597,7 +598,7 @@ on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
}
}
- err = memif_refill_queue (c->conn, qid, rx, 0);
+ err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
if (err != MEMIF_ERR_SUCCESS)
INFO ("memif_buffer_free: %s", memif_strerror (err));
c->rx_buf_num -= rx;
@@ -609,7 +610,7 @@ on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
return 0;
error:
- err = memif_refill_queue (c->conn, qid, rx, 0);
+ err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
if (err != MEMIF_ERR_SUCCESS)
INFO ("memif_buffer_free: %s", memif_strerror (err));
c->rx_buf_num -= rx;
@@ -992,6 +993,7 @@ icmpr_send_proc (void *data)
count -= tx;
}
timespec_get (&end, TIME_UTC);
+ printf ("\n\n");
INFO ("Pakcet sequence finished!");
INFO ("Seq len: %u", seq);
uint64_t t1 = end.tv_sec - start.tv_sec;
@@ -1297,7 +1299,7 @@ main ()
/* if valid callback is passed as argument, fd event polling will be done by user
all file descriptors and events will be passed to user in this callback */
/* if callback is set to NULL libmemif will handle fd event polling */
- err = memif_init (control_fd_update, APP_NAME, NULL, NULL);
+ err = memif_init (control_fd_update, APP_NAME, NULL, NULL, NULL);
if (err != MEMIF_ERR_SUCCESS)
{
INFO ("memif_init: %s", memif_strerror (err));
diff --git a/extras/libmemif/examples/icmp_responder-mt/main.c b/extras/libmemif/examples/icmp_responder-mt/main.c
index c4ac907cb53..8fa64cf93af 100644
--- a/extras/libmemif/examples/icmp_responder-mt/main.c
+++ b/extras/libmemif/examples/icmp_responder-mt/main.c
@@ -894,7 +894,7 @@ main ()
/* if valid callback is passed as argument, fd event polling will be done by user
all file descriptors and events will be passed to user in this callback */
/* if callback is set to NULL libmemif will handle fd event polling */
- err = memif_init (control_fd_update, APP_NAME, NULL, NULL);
+ err = memif_init (control_fd_update, APP_NAME, NULL, NULL, NULL);
if (err != MEMIF_ERR_SUCCESS)
INFO ("memif_init: %s", memif_strerror (err));
diff --git a/extras/libmemif/examples/icmp_responder/main.c b/extras/libmemif/examples/icmp_responder/main.c
index 5e78acd3537..fa7bcb9a636 100644
--- a/extras/libmemif/examples/icmp_responder/main.c
+++ b/extras/libmemif/examples/icmp_responder/main.c
@@ -395,7 +395,7 @@ main (int argc, char *argv[])
/* if valid callback is passed as argument, fd event polling will be done by user
all file descriptors and events will be passed to user in this callback */
/* if callback is set to NULL libmemif will handle fd event polling */
- err = memif_init (NULL, APP_NAME, NULL, NULL);
+ err = memif_init (NULL, APP_NAME, NULL, NULL, NULL);
if (err != MEMIF_ERR_SUCCESS)
INFO ("memif_init: %s", memif_strerror (err));
diff --git a/extras/libmemif/src/libmemif.h b/extras/libmemif/src/libmemif.h
index 88b6a10f7b4..32fda2ab386 100644
--- a/extras/libmemif/src/libmemif.h
+++ b/extras/libmemif/src/libmemif.h
@@ -106,6 +106,15 @@ typedef void *memif_conn_handle_t;
*/
typedef void *(memif_alloc_t) (size_t size);
+
+/** \brief Memif realloc
+ @param ptr - pointer to memory block
+ @param size - requested allocation size
+
+ custom memory reallocation
+*/
+typedef void *(memif_realloc_t) (void *ptr, size_t size);
+
/** \brief Memif allocator free
@param size - requested allocation size
@@ -148,6 +157,68 @@ typedef int (memif_connection_update_t) (memif_conn_handle_t conn,
*/
typedef int (memif_interrupt_t) (memif_conn_handle_t conn, void *private_ctx,
uint16_t qid);
+
+/** @} */
+
+/**
+ * @defgroup EXTERNAL_REGION External region APIs
+ * @ingroup libmemif
+ *
+ * @{
+ */
+
+/** \brief Get external buffer offset (optional)
+ @param private_ctx - private context
+
+ Find unallocated external buffer and return its offset.
+*/
+typedef uint32_t (memif_get_external_buffer_offset_t) (void *private_ctx);
+
+/** \brief Add external region
+ @param[out] addr - region address
+ @param size - requested region size
+ @param fd[out] - file descriptor
+ @param private_ctx - private context
+
+ Called by slave. Add external region created by client.
+*/
+typedef int (memif_add_external_region_t) (void * *addr, uint32_t size,
+ int *fd, void *private_ctx);
+
+/** \brief Get external region address
+ @param size - requested region size
+ @param fd - file descriptor
+ @param private_ctx - private context
+
+ Called by master. Get region address from client.
+
+ \return region address
+*/
+typedef void *(memif_get_external_region_addr_t) (uint32_t size, int fd,
+ void *private_ctx);
+
+/** \brief Delete external region
+ @param addr - region address
+ @param size - region size
+ @param fd - file descriptor
+ @param private_ctx - private context
+
+ Delete external region.
+*/
+typedef int (memif_del_external_region_t) (void *addr, uint32_t size, int fd,
+ void *private_ctx);
+
+/** \brief Register external region
+ @param ar - add external region callback
+ @param gr - get external region addr callback
+ @param dr - delete external region callback
+ @param go - get external buffer offset callback (optional)
+*/
+void memif_register_external_region (memif_add_external_region_t * ar,
+ memif_get_external_region_addr_t * gr,
+ memif_del_external_region_t * dr,
+ memif_get_external_buffer_offset_t * go);
+
/** @} */
/**
@@ -230,6 +301,7 @@ typedef struct
*/
/** \brief Memif queue details
+ @param region - region index
@param qid - queue id
@param ring_size - size of ring buffer in sharem memory
@param flags - ring flags
@@ -239,6 +311,7 @@ typedef struct
*/
typedef struct
{
+ uint8_t region;
uint8_t qid;
uint32_t ring_size;
/** if set queue is in polling mode, else in interrupt mode */
@@ -249,6 +322,22 @@ typedef struct
uint16_t buffer_size;
} memif_queue_details_t;
+/** \brief Memif region details
+ @param index - region index
+ @param addr - region address
+ @param size - region size
+ @param fd - file descriptor
+ @param is_external - if not zero then region is defined by client
+*/
+typedef struct
+{
+ uint8_t index;
+ void *addr;
+ uint32_t size;
+ int fd;
+ uint8_t is_external;
+} memif_region_details_t;
+
/** \brief Memif details
@param if_name - interface name
@param inst_name - application name
@@ -258,7 +347,9 @@ typedef struct
@param secret - secret
@param role - 0 = master, 1 = slave
@param mode - 0 = ethernet, 1 = ip , 2 = punt/inject
- @param socket_filename = socket filename
+ @param socket_filename - socket filename
+ @param regions_num - number of regions
+ @param regions - struct containing region details
@param rx_queues_num - number of receive queues
@param tx_queues_num - number of transmit queues
@param rx_queues - struct containing receive queue details
@@ -277,6 +368,8 @@ typedef struct
uint8_t role; /* 0 = master, 1 = slave */
uint8_t mode; /* 0 = ethernet, 1 = ip, 2 = punt/inject */
uint8_t *socket_filename;
+ uint8_t regions_num;
+ memif_region_details_t *regions;
uint8_t rx_queues_num;
uint8_t tx_queues_num;
memif_queue_details_t *rx_queues;
@@ -343,6 +436,7 @@ int memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
@param on_control_fd_update - if control fd updates inform user to watch new fd
@param app_name - application name (will be truncated to 32 chars)
@param memif_alloc - cutom memory allocator, NULL = default
+ @param memif_realloc - custom memory reallocation, NULL = default
@param memif_free - custom memory free, NULL = default
if param on_control_fd_update is set to NULL,
@@ -359,7 +453,7 @@ int memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
*/
int memif_init (memif_control_fd_update_t * on_control_fd_update,
char *app_name, memif_alloc_t * memif_alloc,
- memif_free_t * memif_free);
+ memif_realloc_t * memif_realloc, memif_free_t * memif_free);
/** \brief Memif cleanup
diff --git a/extras/libmemif/src/main.c b/extras/libmemif/src/main.c
index 1f4672ca4eb..5d8f03a9417 100644
--- a/extras/libmemif/src/main.c
+++ b/extras/libmemif/src/main.c
@@ -325,7 +325,7 @@ add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list, uint16_t * len)
}
}
memif_list_elt_t *tmp;
- tmp = realloc (*list, sizeof (memif_list_elt_t) * *len * 2);
+ tmp = lm->realloc (*list, sizeof (memif_list_elt_t) * *len * 2);
if (tmp == NULL)
return -1;
@@ -411,6 +411,19 @@ memif_control_fd_update_register (memif_control_fd_update_t * cb)
lm->control_fd_update = cb;
}
+void
+memif_register_external_region (memif_add_external_region_t * ar,
+ memif_get_external_region_addr_t * gr,
+ memif_del_external_region_t * dr,
+ memif_get_external_buffer_offset_t * go)
+{
+ libmemif_main_t *lm = &libmemif_main;
+ lm->add_external_region = ar;
+ lm->get_external_region_addr = gr;
+ lm->del_external_region = dr;
+ lm->get_external_buffer_offset = go;
+}
+
static void
memif_alloc_register (memif_alloc_t * ma)
{
@@ -419,6 +432,13 @@ memif_alloc_register (memif_alloc_t * ma)
}
static void
+memif_realloc_register (memif_realloc_t * mr)
+{
+ libmemif_main_t *lm = &libmemif_main;
+ lm->realloc = mr;
+}
+
+static void
memif_free_register (memif_free_t * mf)
{
libmemif_main_t *lm = &libmemif_main;
@@ -427,7 +447,8 @@ memif_free_register (memif_free_t * mf)
int
memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
- memif_alloc_t * memif_alloc, memif_free_t * memif_free)
+ memif_alloc_t * memif_alloc, memif_realloc_t * memif_realloc,
+ memif_free_t * memif_free)
{
int err = MEMIF_ERR_SUCCESS; /* 0 */
libmemif_main_t *lm = &libmemif_main;
@@ -440,6 +461,13 @@ memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
else
memif_alloc_register (malloc);
+ if (memif_realloc != NULL)
+ {
+ memif_realloc_register (memif_realloc);
+ }
+ else
+ memif_realloc_register (realloc);
+
if (memif_free != NULL)
memif_free_register (memif_free);
else
@@ -564,7 +592,7 @@ memif_get_ring (memif_connection_t * conn, memif_ring_type_t type,
{
if (&conn->regions[0] == NULL)
return NULL;
- void *p = conn->regions[0].shm;
+ void *p = conn->regions[0].addr;
int ring_size =
sizeof (memif_ring_t) +
sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size);
@@ -887,7 +915,7 @@ memif_control_fd_handler (int fd, uint8_t events)
sun.sun_family = AF_UNIX;
- strncpy (sun.sun_path, (char*) conn->args.socket_filename,
+ strncpy (sun.sun_path, (char *) conn->args.socket_filename,
sizeof (sun.sun_path) - 1);
if (connect (sockfd, (struct sockaddr *) &sun,
@@ -1149,16 +1177,26 @@ memif_disconnect_internal (memif_connection_t * c)
c->rx_queues = NULL;
}
- if (c->regions != NULL)
+ for (i = 0; i < c->regions_num; i++)
{
- 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;
- lm->free (c->regions);
- c->regions = NULL;
+ if (c->regions[i].is_external != 0)
+ {
+ lm->del_external_region (c->regions[i].addr,
+ c->regions[i].region_size,
+ c->regions[i].fd, c->private_ctx);
+ }
+ else
+ {
+ if (munmap (c->regions[i].addr, c->regions[i].region_size) < 0)
+ return memif_syscall_error_handler (errno);
+ if (c->regions[i].fd > 0)
+ close (c->regions[i].fd);
+ c->regions[i].fd = -1;
+ }
}
+ lm->free (c->regions);
+ c->regions = NULL;
+ c->regions_num = 0;
memset (&c->run_args, 0, sizeof (memif_conn_run_args_t));
@@ -1259,56 +1297,67 @@ int
memif_connect1 (memif_connection_t * c)
{
libmemif_main_t *lm = &libmemif_main;
- memif_region_t *mr = c->regions;
+ memif_region_t *mr;
memif_queue_t *mq;
int i;
uint16_t num;
- if (mr != NULL)
+ for (i = 0; i < c->regions_num; i++)
{
- if (!mr->shm)
+ mr = &c->regions[i];
+ if (mr != NULL)
{
- 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)
+ if (!mr->addr)
{
- return memif_syscall_error_handler (errno);
+ if (mr->is_external)
+ {
+ if (lm->get_external_region_addr == NULL)
+ return 99; /* FIXME: propper error report */
+ mr->addr =
+ lm->get_external_region_addr (mr->region_size, mr->fd,
+ c->private_ctx);
+ }
+ else
+ {
+ if (mr->fd < 0)
+ return MEMIF_ERR_NO_SHMFD;
+
+ if ((mr->addr =
+ 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++)
+ for (i = 0; i < c->rx_queues_num; i++)
{
- mq = &c->tx_queues[i];
+ mq = &c->rx_queues[i];
if (mq != NULL)
{
- mq->ring = c->regions[mq->region].shm + mq->offset;
+ mq->ring = c->regions[mq->region].addr + mq->offset;
if (mq->ring->cookie != MEMIF_COOKIE)
{
- DBG ("wrong cookie on tx ring %u", i);
+ 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;
}
}
- num =
- (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
- num_m2s_rings;
- for (i = 0; i < num; i++)
+
+ for (i = 0; i < c->tx_queues_num; i++)
{
- mq = &c->rx_queues[i];
+ mq = &c->tx_queues[i];
if (mq != NULL)
{
- mq->ring = c->regions[mq->region].shm + mq->offset;
+ mq->ring = c->regions[mq->region].addr + mq->offset;
if (mq->ring->cookie != MEMIF_COOKIE)
{
- DBG ("wrong cookie on rx ring %u", i);
+ DBG ("wrong cookie on tx ring %u", i);
return MEMIF_ERR_COOKIE;
}
mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
@@ -1321,32 +1370,40 @@ memif_connect1 (memif_connection_t * c)
return 0;
}
-int
-memif_init_regions_and_queues (memif_connection_t * conn)
+static inline int
+memif_add_region (libmemif_main_t * lm, memif_connection_t * conn,
+ uint8_t has_buffers)
{
- memif_ring_t *ring = NULL;
memif_region_t *r;
- int i, j;
- libmemif_main_t *lm = &libmemif_main;
- memif_list_elt_t e;
- conn->regions = (memif_region_t *) lm->alloc (sizeof (memif_region_t));
- if (conn->regions == NULL)
+ r =
+ lm->realloc (conn->regions,
+ sizeof (memif_region_t) * ++conn->regions_num);
+ if (r == NULL)
return MEMIF_ERR_NOMEM;
- r = conn->regions;
- r->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));
+ conn->regions = r;
+ r = &conn->regions[conn->regions_num - 1];
+ memset (r, 0, sizeof (memif_region_t));
- r->region_size = r->buffer_offset +
+ if (has_buffers != 0)
+ {
+ r->buffer_offset = 0;
+ }
+ else
+ {
+ r->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 = (has_buffers == 0) ? r->buffer_offset : r->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)
if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1)
return memif_syscall_error_handler (errno);
@@ -1356,10 +1413,19 @@ memif_init_regions_and_queues (memif_connection_t * conn)
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)
+ if ((r->addr = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, r->fd, 0)) == MAP_FAILED)
return memif_syscall_error_handler (errno);
+ return MEMIF_ERR_SUCCESS;
+}
+
+static inline int
+memif_init_queues (libmemif_main_t * lm, memif_connection_t * conn)
+{
+ int i, j;
+ memif_ring_t *ring;
+
for (i = 0; i < conn->run_args.num_s2m_rings; i++)
{
ring = memif_get_ring (conn, MEMIF_RING_S2M, i);
@@ -1370,8 +1436,9 @@ memif_init_regions_and_queues (memif_connection_t * conn)
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 = r->buffer_offset +
+ ring->desc[j].region = 1;
+ ring->desc[j].offset =
+ conn->regions[1].buffer_offset +
(uint32_t) (slot * conn->run_args.buffer_size);
ring->desc[j].length = conn->run_args.buffer_size;
}
@@ -1385,28 +1452,30 @@ memif_init_regions_and_queues (memif_connection_t * conn)
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) *
+ 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 = r->buffer_offset +
+ ring->desc[j].region = 1;
+ ring->desc[j].offset =
+ conn->regions[1].buffer_offset +
(uint32_t) (slot * conn->run_args.buffer_size);
ring->desc[j].length = conn->run_args.buffer_size;
}
}
memif_queue_t *mq;
+ DBG ("alloc: %p", lm->alloc);
+ DBG ("size: %lu", sizeof (memif_queue_t) * conn->run_args.num_s2m_rings);
mq =
(memif_queue_t *) lm->alloc (sizeof (memif_queue_t) *
conn->run_args.num_s2m_rings);
if (mq == NULL)
return MEMIF_ERR_NOMEM;
+
int x;
+ memif_list_elt_t e;
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);
@@ -1416,8 +1485,8 @@ memif_init_regions_and_queues (memif_connection_t * conn)
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;
+ (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
+ mq[x].last_head = mq[x].last_tail = 0;
mq[x].alloc_bufs = 0;
}
conn->tx_queues = mq;
@@ -1427,11 +1496,11 @@ memif_init_regions_and_queues (memif_connection_t * conn)
conn->run_args.num_m2s_rings);
if (mq == NULL)
return MEMIF_ERR_NOMEM;
+
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);
@@ -1441,12 +1510,54 @@ memif_init_regions_and_queues (memif_connection_t * conn)
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;
+ (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
+ mq[x].last_head = mq[x].last_tail = 0;
mq[x].alloc_bufs = 0;
}
conn->rx_queues = mq;
+ return MEMIF_ERR_SUCCESS;
+}
+
+int
+memif_init_regions_and_queues (memif_connection_t * conn)
+{
+ memif_ring_t *ring = NULL;
+ memif_region_t *r;
+ int i, j;
+ libmemif_main_t *lm = &libmemif_main;
+ memif_list_elt_t e;
+
+ /* region 0. rings */
+ memif_add_region (lm, conn, /* has_buffers */ 0);
+
+ /* region 1. buffers */
+ if (lm->add_external_region)
+ {
+ r =
+ (memif_region_t *) lm->realloc (conn->regions,
+ sizeof (memif_region_t) *
+ ++conn->regions_num);
+ if (r == NULL)
+ return MEMIF_ERR_NOMEM;
+ conn->regions = r;
+
+ conn->regions[1].region_size =
+ conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
+ (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
+ conn->regions[1].buffer_offset = 0;
+ lm->add_external_region (&conn->regions[1].addr,
+ conn->regions[1].region_size,
+ &conn->regions[1].fd, conn->private_ctx);
+ conn->regions[1].is_external = 1;
+ }
+ else
+ {
+ memif_add_region (lm, conn, 1);
+ }
+
+ memif_init_queues (lm, conn);
+
return 0;
}
@@ -1501,7 +1612,8 @@ memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid,
((memif_ring_t *) b0->ring)->desc[b0->desc_index & mask].offset = ring->desc[slot & mask].offset; /* put free buffer on rx ring */
ring->desc[slot & mask].offset =
- (uint32_t) (b0->data - c->regions->shm);
+ (uint32_t) (b0->data -
+ c->regions[ring->desc[slot & mask].region].addr);
ring->desc[slot & mask].flags |=
(b0->flags & MEMIF_BUFFER_FLAG_NEXT) ? MEMIF_DESC_FLAG_NEXT : 0;
@@ -1547,10 +1659,12 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
if (EXPECT_FALSE (!count_out))
return MEMIF_ERR_INVAL_ARG;
+ libmemif_main_t *lm = &libmemif_main;
memif_queue_t *mq = &c->tx_queues[qid];
memif_ring_t *ring = mq->ring;
memif_buffer_t *b0, *b1;
uint16_t mask = (1 << mq->log2_ring_size) - 1;
+ uint32_t offset_mask = c->run_args.buffer_size - 1;
uint16_t ring_size;
uint16_t slot, ns;
int i, err = MEMIF_ERR_SUCCESS; /* 0 */
@@ -1620,14 +1734,13 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
/* slave resets buffer offset */
if (c->args.is_master == 0)
{
- uint16_t x =
- (ring->desc[slot & mask].offset -
- c->regions->buffer_offset) / c->run_args.buffer_size;
- ring->desc[slot & mask].offset =
- c->regions->buffer_offset + (x * c->run_args.buffer_size);
+ memif_desc_t *d = &ring->desc[slot & mask];
+ if (lm->get_external_buffer_offset)
+ d->offset = lm->get_external_buffer_offset (c->private_ctx);
+ else
+ d->offset = d->offset - (d->offset & offset_mask);
}
-
- b0->data = c->regions->shm + ring->desc[slot & mask].offset;
+ b0->data = memif_get_buffer (c, ring, slot & mask);
src_left -= b0->len;
dst_left -= b0->len;
@@ -1673,6 +1786,7 @@ memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count,
memif_queue_t *mq = &c->rx_queues[qid];
memif_ring_t *ring = mq->ring;
uint16_t mask = (1 << mq->log2_ring_size) - 1;
+ uint32_t offset_mask = c->run_args.buffer_size - 1;
uint16_t slot;
if (c->args.is_master)
@@ -1688,20 +1802,18 @@ memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count,
uint16_t ns = (1 << mq->log2_ring_size) - head + mq->last_tail;
head += (count < ns) ? count : ns;
- if (headroom)
+ slot = ring->head;
+ memif_desc_t *d;
+ while (slot < head)
{
- slot = ring->head;
- while (slot < head)
- {
- uint16_t x =
- (ring->desc[slot & mask].offset -
- c->regions->buffer_offset) / c->run_args.buffer_size;
- ring->desc[slot & mask].offset =
- c->regions->buffer_offset + (x * c->run_args.buffer_size) +
- headroom;
-
- slot++;
- }
+ d = &ring->desc[slot & mask];
+ d->region = 1;
+ d->length = c->run_args.buffer_size - headroom;
+ if (lm->get_external_buffer_offset)
+ d->offset = lm->get_external_buffer_offset (c->private_ctx);
+ else
+ d->offset = d->offset - (d->offset & offset_mask) + headroom;
+ slot++;
}
MEMIF_MEMORY_BARRIER ();
@@ -1878,7 +1990,8 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
l1 = strlen ((char *) c->args.interface_name);
if (l0 + l1 < buflen)
{
- md->if_name = (uint8_t *) strcpy (buf + l0, (char *) c->args.interface_name);
+ md->if_name =
+ (uint8_t *) strcpy (buf + l0, (char *) c->args.interface_name);
l0 += l1 + 1;
}
else
@@ -1896,7 +2009,8 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
l1 = strlen ((char *) c->remote_if_name);
if (l0 + l1 < buflen)
{
- md->remote_if_name = (uint8_t *) strcpy (buf + l0, (char *) c->remote_if_name);
+ md->remote_if_name =
+ (uint8_t *) strcpy (buf + l0, (char *) c->remote_if_name);
l0 += l1 + 1;
}
else
@@ -1905,7 +2019,8 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
l1 = strlen ((char *) c->remote_name);
if (l0 + l1 < buflen)
{
- md->remote_inst_name = (uint8_t *) strcpy (buf + l0, (char *) c->remote_name);
+ md->remote_inst_name =
+ (uint8_t *) strcpy (buf + l0, (char *) c->remote_name);
l0 += l1 + 1;
}
else
@@ -1913,7 +2028,7 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
md->id = c->args.interface_id;
- if (strlen((char *) c->args.secret) > 0)
+ if (strlen ((char *) c->args.secret) > 0)
{
l1 = strlen ((char *) c->args.secret);
if (l0 + l1 < buflen)
@@ -1938,6 +2053,25 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
else
err = MEMIF_ERR_NOBUF_DET;
+ md->regions_num = c->regions_num;
+ l1 = sizeof (memif_region_details_t) * md->regions_num;
+ if (l0 + l1 <= buflen)
+ {
+ md->regions = (memif_region_details_t *) buf + l0;
+ l0 += l1;
+ }
+ else
+ err = MEMIF_ERR_NOBUF_DET;
+
+ for (i = 0; i < md->regions_num; i++)
+ {
+ md->regions[i].index = i;
+ md->regions[i].addr = c->regions[i].addr;
+ md->regions[i].size = c->regions[i].region_size;
+ md->regions[i].fd = c->regions[i].fd;
+ md->regions[i].is_external = c->regions[i].is_external;
+ }
+
md->rx_queues_num =
(c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
num_m2s_rings;
@@ -1953,6 +2087,7 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
for (i = 0; i < md->rx_queues_num; i++)
{
+ md->rx_queues[i].region = c->rx_queues[i].region;
md->rx_queues[i].qid = i;
md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size);
md->rx_queues[i].flags = c->rx_queues[i].ring->flags;
@@ -1976,6 +2111,7 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
for (i = 0; i < md->tx_queues_num; i++)
{
+ md->tx_queues[i].region = c->tx_queues[i].region;
md->tx_queues[i].qid = i;
md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size);
md->tx_queues[i].flags = c->tx_queues[i].ring->flags;
diff --git a/extras/libmemif/src/memif_private.h b/extras/libmemif/src/memif_private.h
index 5ea5494decb..deca80c3350 100644
--- a/extras/libmemif/src/memif_private.h
+++ b/extras/libmemif/src/memif_private.h
@@ -67,10 +67,11 @@ _Static_assert (strlen (MEMIF_DEFAULT_APP_NAME) <= MEMIF_NAME_LEN,
typedef struct
{
- void *shm;
+ void *addr;
uint32_t region_size;
uint32_t buffer_offset;
int fd;
+ uint8_t is_external;
} memif_region_t;
typedef struct
@@ -133,8 +134,11 @@ typedef struct memif_connection
uint8_t remote_name[MEMIF_NAME_LEN];
uint8_t remote_disconnect_string[96];
+ uint8_t regions_num;
memif_region_t *regions;
+ uint8_t rx_queues_num;
+ uint8_t tx_queues_num;
memif_queue_t *rx_queues;
memif_queue_t *tx_queues;
@@ -165,7 +169,13 @@ typedef struct
uint16_t disconn_slaves;
uint8_t app_name[MEMIF_NAME_LEN];
+ memif_add_external_region_t *add_external_region;
+ memif_get_external_region_addr_t *get_external_region_addr;
+ memif_del_external_region_t *del_external_region;
+ memif_get_external_buffer_offset_t *get_external_buffer_offset;
+
memif_alloc_t *alloc;
+ memif_realloc_t *realloc;
memif_free_t *free;
uint16_t control_list_len;
@@ -226,7 +236,7 @@ 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 +
+ return (conn->regions[ring->desc[index].region].addr +
ring->desc[index].offset);
}
diff --git a/extras/libmemif/src/socket.c b/extras/libmemif/src/socket.c
index 3982c96ba90..7c4bbd437d2 100644
--- a/extras/libmemif/src/socket.c
+++ b/extras/libmemif/src/socket.c
@@ -112,7 +112,8 @@ memif_msg_send_hello (int fd)
h->max_region = MEMIF_MAX_REGION;
h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE;
- strncpy ((char *) h->name, (char *) lm->app_name, strlen ((char *) lm->app_name));
+ strncpy ((char *) h->name, (char *) lm->app_name,
+ strlen ((char *) lm->app_name));
/* msg hello is not enqueued but sent directly,
because it is the first msg to be sent */
@@ -141,7 +142,7 @@ memif_msg_enq_init (memif_connection_t * c)
strncpy ((char *) i->name, (char *) lm->app_name,
strlen ((char *) lm->app_name));
- if (strlen((char *) c->args.secret) > 0)
+ if (strlen ((char *) c->args.secret) > 0)
strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret));
e->next = NULL;
@@ -166,7 +167,6 @@ static_fn int
memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
{
libmemif_main_t *lm = &libmemif_main;
- /* maybe check if region is valid? */
memif_region_t *mr = &c->regions[region_index];
memif_msg_queue_elt_t *e =
@@ -424,10 +424,10 @@ memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
strncpy ((char *) c->remote_name, (char *) i->name,
strlen ((char *) i->name));
- if (strlen((char *) c->args.secret) > 0)
+ if (strlen ((char *) c->args.secret) > 0)
{
int r;
- if (strlen((char *) i->secret) > 0)
+ if (strlen ((char *) i->secret) > 0)
{
if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret))
{
@@ -484,6 +484,8 @@ static_fn int
memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
int fd)
{
+ libmemif_main_t *lm = &libmemif_main;
+
memif_msg_add_region_t *ar = &msg->add_region;
memif_region_t *mr;
if (fd < 0)
@@ -493,14 +495,19 @@ memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
return MEMIF_ERR_MAXREG;
mr =
- (memif_region_t *) realloc (c->regions,
- sizeof (memif_region_t) * (ar->index + 1));
+ (memif_region_t *) lm->realloc (c->regions,
+ sizeof (memif_region_t) *
+ (++c->regions_num));
if (mr == NULL)
return memif_syscall_error_handler (errno);
+ memset (mr + ar->index, 0, sizeof (memif_region_t));
c->regions = mr;
c->regions[ar->index].fd = fd;
c->regions[ar->index].region_size = ar->size;
- c->regions[ar->index].shm = NULL;
+ c->regions[ar->index].addr = NULL;
+
+ if (lm->get_external_region_addr)
+ c->regions[ar->index].is_external = 1;
return MEMIF_ERR_SUCCESS; /* 0 */
}
@@ -510,6 +517,8 @@ memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
static_fn int
memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
{
+ libmemif_main_t *lm = &libmemif_main;
+
memif_msg_add_ring_t *ar = &msg->add_ring;
memif_queue_t *mq;
@@ -528,8 +537,9 @@ memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
return MEMIF_ERR_MAXRING;
mq =
- (memif_queue_t *) realloc (c->rx_queues,
- sizeof (memif_queue_t) * (ar->index + 1));
+ (memif_queue_t *) lm->realloc (c->rx_queues,
+ sizeof (memif_queue_t) *
+ (++c->rx_queues_num));
memset (mq + ar->index, 0, sizeof (memif_queue_t));
if (mq == NULL)
return memif_syscall_error_handler (errno);
@@ -548,8 +558,9 @@ memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
return MEMIF_ERR_MAXRING;
mq =
- (memif_queue_t *) realloc (c->tx_queues,
- sizeof (memif_queue_t) * (ar->index + 1));
+ (memif_queue_t *) lm->realloc (c->tx_queues,
+ sizeof (memif_queue_t) *
+ (++c->tx_queues_num));
memset (mq + ar->index, 0, sizeof (memif_queue_t));
if (mq == NULL)
return memif_syscall_error_handler (errno);
@@ -718,8 +729,11 @@ memif_msg_receive (int ifd)
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->regions_num; i++)
+ {
+ if ((err = memif_msg_enq_add_region (c, i)) != MEMIF_ERR_SUCCESS)
+ return err;
+ }
for (i = 0; i < c->run_args.num_s2m_rings; i++)
{
if ((err =