summaryrefslogtreecommitdiffstats
path: root/extras
diff options
context:
space:
mode:
authorJakub Grajciar <jgrajcia@cisco.com>2021-01-04 15:01:54 +0100
committerDamjan Marion <dmarion@me.com>2021-09-27 14:35:45 +0000
commit9f99e9a60e31f558f9eb5f8e3762d7a97bde67a8 (patch)
tree3c65462f6e1de3a47b46fbcfeb639d692dc154b0 /extras
parent24681606b9e1f78436846ef3de936829c03b5ca9 (diff)
libmemif: remove old examples
Type: refactor Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com> Change-Id: I2331f6fb43ca533afb32f7d448adda27b19689ac
Diffstat (limited to 'extras')
-rw-r--r--extras/libmemif/examples/CMakeLists.txt12
-rw-r--r--extras/libmemif/examples/icmp_responder-eb/main.c1069
-rw-r--r--extras/libmemif/examples/icmp_responder-epoll/main.c1320
-rw-r--r--extras/libmemif/examples/icmp_responder-mt/main.c914
-rw-r--r--extras/libmemif/examples/icmp_responder-mt_3-1/main.c549
-rw-r--r--extras/libmemif/examples/icmp_responder-zero-copy-slave/main.c1270
-rw-r--r--extras/libmemif/examples/icmp_responder/icmp_proto.c524
-rw-r--r--extras/libmemif/examples/icmp_responder/icmp_proto.h45
-rw-r--r--extras/libmemif/examples/icmp_responder/main.c417
9 files changed, 4 insertions, 6116 deletions
diff --git a/extras/libmemif/examples/CMakeLists.txt b/extras/libmemif/examples/CMakeLists.txt
index 4f120e59f75..6adacaa3392 100644
--- a/extras/libmemif/examples/CMakeLists.txt
+++ b/extras/libmemif/examples/CMakeLists.txt
@@ -13,18 +13,14 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
-set(HEADERS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/icmp_responder)
+set(HEADERS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(COMMON_SOURCE_FILES
- icmp_responder/icmp_proto.c)
+
+)
list(APPEND EXAMPLES_LIST
- icmp_responder/main.c
- icmp_responder-epoll/main.c
- icmp_responder-mt/main.c
- icmp_responder-mt_3-1/main.c
- icmp_responder-eb/main.c
- icmp_responder-zero-copy-slave/main.c
+
)
foreach (EXAMPLE_SRC ${EXAMPLES_LIST})
diff --git a/extras/libmemif/examples/icmp_responder-eb/main.c b/extras/libmemif/examples/icmp_responder-eb/main.c
deleted file mode 100644
index 86a49da7935..00000000000
--- a/extras/libmemif/examples/icmp_responder-eb/main.c
+++ /dev/null
@@ -1,1069 +0,0 @@
-/*
- *------------------------------------------------------------------
- * 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 modified 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, void *ctx)
-{
- /* 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;
- /* disconnect 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);
-
- if (err)
- return err;
-
- 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;
- 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", rx, 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;
-
- 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",
- rx, 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;
- 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;
- 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;
- int app_err = 0, memif_err = 0, en = 0;
- 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 epoll 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
deleted file mode 100644
index c2ea348d897..00000000000
--- a/extras/libmemif/examples/icmp_responder-epoll/main.c
+++ /dev/null
@@ -1,1320 +0,0 @@
-/*
- *------------------------------------------------------------------
- * 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 <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
-
-#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)
-#define LOG(...) do { \
- if (enable_log) { \
- dprintf (out_fd, __VA_ARGS__); \
- dprintf (out_fd, "\n"); \
- } \
- } while (0)
-#define LOG_FILE "/tmp/memif_time_test.txt"
-#else
-#define DBG(...)
-#define LOG(...)
-#endif
-
-#define INFO(...) do { \
- printf ("INFO: "__VA_ARGS__); \
- printf ("\n"); \
- } while (0)
-
-
-/* maximum tx/rx memif buffers */
-#define MAX_MEMIF_BUFS 256
-#define MAX_CONNS 50
-
-#define ICMPR_HEADROOM 64
-
-int epfd;
-int out_fd;
-uint8_t enable_log;
-
-typedef struct
-{
- uint16_t index;
- /* memif connection handle */
- memif_conn_handle_t conn;
- /* tx buffers */
- memif_buffer_t *tx_bufs;
- /* allocated tx buffers counter */
- /* number of tx buffers pointing to shared memory */
- uint16_t tx_buf_num;
- /* rx buffers */
- memif_buffer_t *rx_bufs;
- /* allocated rx buffers counter */
- /* number of rx buffers pointing to shared memory */
- uint16_t rx_buf_num;
- /* interface ip address */
- uint8_t ip_addr[4];
- uint16_t seq;
- 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;
-
-memif_connection_t memif_connection[MAX_CONNS];
-icmpr_thread_data_t icmpr_thread_data[MAX_CONNS];
-pthread_t thread[MAX_CONNS];
-long ctx[MAX_CONNS];
-
-/* print details for all memif connections */
-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 ("\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");
- printf ("\treason: %s\n", md.error);
- }
- }
- free (buf);
-}
-
-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 modified 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;
-}
-
-/* informs user about connected status. private_ctx is used by user to identify connection
- (multiple connections WIP) */
-int
-on_connect (memif_conn_handle_t conn, void *private_ctx)
-{
- INFO ("memif connected!");
- memif_refill_queue (conn, 0, -1, ICMPR_HEADROOM);
- enable_log = 1;
- return 0;
-}
-
-/* informs user about disconnected status. private_ctx is used by user to identify connection
- (multiple connections WIP) */
-int
-on_disconnect (memif_conn_handle_t conn, void *private_ctx)
-{
- INFO ("memif disconnected!");
- return 0;
-}
-
-/* user needs to watch new fd or stop watching fd that is about to be closed.
- control fd will be modified during connection establishment to minimize CPU usage */
-int
-control_fd_update (int fd, uint8_t events, void *ctx)
-{
- /* 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_buffer_alloc (long index, long n, uint16_t * r, uint16_t i,
- uint16_t qid)
-{
- memif_connection_t *c = &memif_connection[index];
- int err;
- /* set data pointer to shared memory and set buffer_len to shared memory buffer len */
- err = memif_buffer_alloc (c->conn, qid, c->tx_bufs + i, n, r, 128);
- if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
- {
- INFO ("memif_buffer_alloc: %s", memif_strerror (err));
- return -1;
- }
- c->tx_buf_num += *r;
- DBG ("allocated %d/%ld buffers, %u free buffers", *r, n,
- MAX_MEMIF_BUFS - c->tx_buf_num);
- return 0;
-}
-
-int
-icmpr_tx_burst (long index, uint16_t qid)
-{
- memif_connection_t *c = &memif_connection[index];
- int err;
- uint16_t r;
- /* inform peer memif interface about data in shared memory buffers */
- /* mark memif buffers as free */
- err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_tx_burst: %s", memif_strerror (err));
- c->tx_buf_num -= r;
- c->tx_counter += r;
- return 0;
-}
-
-/* called when event is polled on interrupt file descriptor.
- there are packets in shared memory ready to be received */
-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;
- int i = 0; /* rx buffer iterator */
- int j = 0; /* tx buffer iterator */
-
- /* loop while there are packets in shm */
- do
- {
- /* receive data from shared memory buffers */
- 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, ICMPR_HEADROOM);
- 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, ICMPR_HEADROOM);
- 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",
- rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
- return 0;
-}
-
-/* called when event is polled on interrupt file descriptor.
- there are packets in shared memory ready to be received */
-/* dev test modification: loop until TX == RX (don't drop buffers) */
-int
-on_interrupt0 (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;
- int i; /* rx buffer iterator */
- int j; /* tx bufferiterator */
-
- /* loop while there are packets in shm */
- do
- {
- /* receive data from shared memory buffers */
- 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;
-
- /* loop while there are RX buffers to be processed */
- while (rx)
- {
-
- /* try to alloc required number of buffers buffers */
- 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;
-
- /* process buffers */
- while (tx)
- {
- while (tx > 2)
- {
- 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);
- resolve_packet ((void *) (c->rx_bufs + i + 1)->data,
- (c->rx_bufs + i + 1)->len,
- (void *) (c->tx_bufs + j + 1)->data,
- &(c->tx_bufs + j + 1)->len, c->ip_addr);
-
- i += 2;
- j += 2;
- tx -= 2;
- }
- 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--;
- }
- /* mark memif buffers and shared memory buffers as free */
- /* free processed buffers */
- 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;
-
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- rx, rx, MAX_MEMIF_BUFS - rx);
-
- /* transmit allocated buffers */
- 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, ICMPR_HEADROOM);
- 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",
- rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
- return 0;
-}
-
-/* called when event is polled on interrupt file descriptor.
- there are packets in shared memory ready to be received */
-/* dev test modification: handle only ARP requests */
-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;
-
- 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)
- {
- if (icmpr_buffer_alloc (c->index, 1, &tx, i, qid) < 0)
- break;
- 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);
- icmpr_tx_burst (c->index, qid);
- }
- }
-
- 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;
-
-
- }
- while (ret_val == MEMIF_ERR_NOBUF);
-
- return 0;
-
-error:
- 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;
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- rx, 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 ("don't even try...");
- return 0;
- }
- memif_connection_t *c = &memif_connection[index];
-
- /* setting memif connection arguments */
- memif_conn_args_t args;
- 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;
- /* socket filename is not specified, because this app is supposed to
- connect to VPP over memif. so default socket filename will be used */
- /* default socketfile = /run/vpp/memif.sock */
-
- args.interface_id = index;
- /* last argument for memif_create (void * private_ctx) is used by user
- to identify connection. this context is returned with callbacks */
- int err;
- /* default interrupt */
- 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, "0", 1) == 0)
- {
- err = memif_create (&c->conn,
- &args, on_connect, on_disconnect, on_interrupt0,
- &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;
- /* alloc memif buffers */
- 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;
-
- c->seq = c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
-
-done:
- return 0;
-}
-
-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;
- /* disconnect 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;
-}
-
-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-desc>] - create memif. index is also used as interface id, mode 0 = slave 1 = master, interrupt-desc none = default 0 = if ring is full wait 1 = handle only ARP requests\n");
- printf ("\tdel <index> - delete memif\n");
- printf ("\tshow - show connection details\n");
- printf ("\tip-set <index> <ip-addr> - set interface ip address\n");
- printf
- ("\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
- printf ("\tsh-count - print counters\n");
- printf ("\tcl-count - clear counters\n");
- printf ("\tsend <index> <tx> <ip> <mac> - send icmp\n");
-}
-
-int
-icmpr_free ()
-{
- /* application cleanup */
- int err;
- long i;
- if (out_fd > 0)
- close (out_fd);
- out_fd = -1;
- 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_set_ip (long index, char *ip)
-{
- 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->conn == NULL)
- {
- INFO ("no connection at index %ld", index);
- return 0;
- }
-
- char *end;
- char *ui;
- uint8_t tmp[4];
- 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);
-
- c->ip_addr[0] = tmp[0];
- c->ip_addr[1] = tmp[1];
- c->ip_addr[2] = tmp[2];
- c->ip_addr[3] = tmp[3];
-
- INFO ("memif %ld ip address set to %u.%u.%u.%u",
- index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
-
- return 0;
-
-error:
- INFO ("invalid ip address");
- return 0;
-}
-
-int
-icmpr_set_rx_mode (long index, long qid, char *mode)
-{
- 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->conn == NULL)
- {
- INFO ("no connection at index %ld", index);
- return 0;
- }
-
- if (strncmp (mode, "interrupt", 9) == 0)
- {
- memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid);
- }
-
- else if (strncmp (mode, "polling", 7) == 0)
- {
- memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid);
- }
- else
- INFO ("expected rx mode <interrupt|polling>");
- return 0;
-}
-
-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);
- printf ("\tts: %lus %luns\n", c->t_sec, c->t_nsec);
- }
-}
-
-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->t_sec = c->t_nsec = c->tx_err_counter = c->tx_counter =
- c->rx_counter = 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. Thread exiting...\n", index);
- pthread_exit (NULL);
- }
- 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 Thread exiting...\n",
- memif_strerror (err));
- pthread_exit (NULL);
- }
- c->tx_buf_num += tx;
-
- while (tx)
- {
- while (tx > 2)
- {
- 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++);
- i += 2;
- tx -= 2;
- }
- 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 Thread exiting...\n",
- memif_strerror (err));
- pthread_exit (NULL);
- }
- c->tx_buf_num -= tx;
- c->tx_counter += tx;
- count -= tx;
- }
- timespec_get (&end, TIME_UTC);
- printf ("\n\n");
- INFO ("Packet sequence finished!");
- INFO ("Seq 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;
- INFO ("Seq time: %lus %luns", t1, t2);
- double tmp = t1;
- tmp += t2 / 1e+9;
- tmp = seq / tmp;
- INFO ("Average pps: %f", tmp);
- INFO ("Thread exiting...");
- pthread_exit (NULL);
-}
-
-int
-icmpr_send (long index, long packet_num, char *hw, char *ip)
-{
- memif_connection_t *c = &memif_connection[index];
- char *end;
- char *ui;
- uint8_t tmp[6];
- if (c->conn == NULL)
- return -1;
- c->seq = 0;
- icmpr_thread_data_t *data = &icmpr_thread_data[index];
- data->index = index;
- data->packet_num = packet_num;
- INFO ("PN: %lu", data->packet_num);
- printf ("%s\n", ip);
- printf ("%s\n", hw);
-
- 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);
-
- data->hw_daddr[0] = tmp[0];
- data->hw_daddr[1] = tmp[1];
- data->hw_daddr[2] = tmp[2];
- data->hw_daddr[3] = tmp[3];
- data->hw_daddr[4] = tmp[4];
- data->hw_daddr[5] = tmp[5];
-
- 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, "show", 4) == 0)
- {
- print_memif_details ();
- goto done;
- }
- else if (strncmp (ui, "ip-set", 6) == 0)
- {
- ui = strtok (NULL, " ");
- if (ui != NULL)
- icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " "));
- else
- INFO ("expected id");
- goto done;
- }
- else if (strncmp (ui, "rx-mode", 7) == 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_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " "));
- else
- INFO ("expected qid");
- goto done;
- }
- else if (strncmp (ui, "sh-count", 8) == 0)
- {
- icmpr_print_counters ();
- }
- else if (strncmp (ui, "cl-count", 8) == 0)
- {
- icmpr_reset_counters ();
- }
- 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
- {
- INFO ("unknown command: %s", ui);
- goto done;
- }
-
- return 0;
-done:
- free (in);
- return 0;
-}
-
-int
-poll_event (int timeout)
-{
- struct epoll_event evt;
- int app_err = 0, memif_err = 0, en = 0;
- uint32_t events = 0;
- struct timespec start, end;
- 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 */
- timespec_get (&start, TIME_UTC);
- 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 epoll 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);
- }
- }
-
- timespec_get (&end, TIME_UTC);
- LOG ("interrupt: %ld", end.tv_nsec - start.tv_nsec);
-
- 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);
-
-#ifdef LOG_FILE
- remove (LOG_FILE);
- enable_log = 0;
-
- out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
- if (out_fd < 0)
- INFO ("Error opening log file: %s", strerror (errno));
-#endif /* LOG_FILE */
-
- /* 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;
- }
-
- print_help ();
-
- /* main loop */
- while (1)
- {
- if (poll_event (-1) < 0)
- {
- DBG ("poll_event error!");
- }
- }
-}
diff --git a/extras/libmemif/examples/icmp_responder-mt/main.c b/extras/libmemif/examples/icmp_responder-mt/main.c
deleted file mode 100644
index f37c6a026fb..00000000000
--- a/extras/libmemif/examples/icmp_responder-mt/main.c
+++ /dev/null
@@ -1,914 +0,0 @@
-/*
- *------------------------------------------------------------------
- * 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 <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 <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
-
-#define INFO(...) do { \
- printf ("INFO: "__VA_ARGS__); \
- printf ("\n"); \
- } while (0)
-
-/* maximum tx/rx memif buffers */
-#define MAX_MEMIF_BUFS 256
-#define MAX_CONNS 50
-#define MAX_QUEUES 2
-#define MAX_THREADS ((MAX_CONNS) * (MAX_QUEUES))
-
-int main_epfd;
-
-typedef struct
-{
- /* thread id */
- uint8_t id;
- /* memif connection index */
- uint16_t index;
- /* id of queue to be handled by thread */
- uint8_t qid;
- uint8_t isRunning;
-
- uint16_t rx_buf_num;
- uint16_t tx_buf_num;
- memif_buffer_t *rx_bufs;
- memif_buffer_t *tx_bufs;
-} memif_thread_data_t;
-
-typedef struct
-{
- uint16_t index;
- /* memif connection handle */
- memif_conn_handle_t conn;
- /* interface ip address */
- uint8_t ip_addr[4];
- /* inform pthread about connection termination */
- uint8_t pending_del;
-} memif_connection_t;
-
-memif_connection_t memif_connection[MAX_CONNS];
-long ctx[MAX_CONNS];
-
-/* thread data specific for each thread */
-memif_thread_data_t thread_data[MAX_THREADS];
-pthread_t thread[MAX_THREADS];
-
-void
-user_signal_handler (int sig)
-{
-}
-
-static void
-print_memif_details ()
-{
- memif_details_t md;
- ssize_t buflen;
- char *buf;
- int err, i, e, ti;
- 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 ("\trx queues:\n");
- for (e = 0; e < md.rx_queues_num; e++)
- {
- ti = (i * MAX_QUEUES) + e;
- printf ("\tqueue id: %u\n", md.rx_queues[e].qid);
- printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
- printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
- printf ("\t\tthread id: %u\n", thread_data[ti].id);
- printf ("\t\tthread connection index: %u\n", thread_data[ti].index);
- printf ("\t\tthread running: ");
- if (thread_data[ti].isRunning)
- printf ("yes\n");
- else
- printf ("no");
- }
- printf ("\ttx queues:\n");
- for (e = 0; e < md.tx_queues_num; e++)
- {
- printf ("\tqueue id: %u\n", md.tx_queues[e].qid);
- printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
- 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);
-}
-
-int
-add_epoll_fd (int epfd, 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 epfd, 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 modified on epoll", fd);
- return 0;
-}
-
-int
-del_epoll_fd (int epfd, 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;
-}
-
-void *
-memif_rx_poll (void *ptr)
-{
- memif_thread_data_t *data = (memif_thread_data_t *) ptr;
- memif_connection_t *c = &memif_connection[data->index];
- int err;
- uint16_t rx = 0, tx = 0, fb = 0;
-
- data->rx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
- data->tx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
- data->rx_buf_num = 0;
- data->tx_buf_num = 0;
-
- data->isRunning = 1;
- INFO ("pthread id %u starts in polling mode", data->id);
-
- while (1)
- {
- if (c->pending_del)
- goto close;
-
- /* receive data from shared memory buffers */
- err =
- memif_rx_burst (c->conn, data->qid, data->rx_bufs, MAX_MEMIF_BUFS,
- &rx);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("memif_rx_burst: %s", memif_strerror (err));
- data->rx_buf_num += rx;
- goto error;
- }
- data->rx_buf_num += rx;
- if (rx == 0)
- {
- continue;
- }
-
- DBG ("thread id: %u", data->id);
-
- DBG ("received %d buffers. %u/%u alloc/free buffers",
- rx, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
-
- err =
- memif_buffer_alloc (c->conn, data->qid, data->tx_bufs,
- data->rx_buf_num, &tx, 0);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("memif_buffer_alloc: %s", memif_strerror (err));
- data->tx_buf_num += tx;
- goto error;
- }
- data->tx_buf_num += tx;
- DBG ("allocated %d/%d buffers, %u free buffers",
- tx, data->rx_buf_num, MAX_MEMIF_BUFS - data->tx_buf_num);
-
- int i;
- for (i = 0; i < rx; i++)
- {
- resolve_packet ((void *) (data->rx_bufs + i)->data,
- (data->rx_bufs + i)->len,
- (void *) (data->tx_bufs + i)->data,
- &(data->tx_bufs + i)->len, c->ip_addr);
- }
-
- /* mark memif buffers and shared memory buffers as free */
- err = memif_refill_queue (c->conn, data->qid, rx, 0);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_buffer_free: %s", memif_strerror (err));
- data->rx_buf_num -= fb;
-
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
-
- err =
- memif_tx_burst (c->conn, data->qid, data->tx_bufs, data->tx_buf_num,
- &tx);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("memif_tx_burst: %s", memif_strerror (err));
- goto error;
- }
- DBG ("tx: %d/%u", tx, data->tx_buf_num);
- data->tx_buf_num -= tx;
- }
-
-error:
- INFO ("thread %u error!", data->id);
- goto close;
-
-close:
- err = memif_refill_queue (c->conn, data->qid, rx, 0);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_buffer_free: %s", memif_strerror (err));
- data->rx_buf_num -= fb;
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
- free (data->rx_bufs);
- free (data->tx_bufs);
- data->isRunning = 0;
- INFO ("pthread id %u exit", data->id);
- pthread_exit (NULL);
-}
-
-void *
-memif_rx_interrupt (void *ptr)
-{
- memif_thread_data_t *data = (memif_thread_data_t *) ptr;
- memif_connection_t *c = &memif_connection[data->index];
- int err;
- uint16_t rx = 0, tx = 0, fb = 0;
- struct epoll_event evt;
- int en = 0;
- sigset_t sigset;
-
- signal (SIGUSR1, user_signal_handler);
-
- data->rx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
- data->tx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
- data->rx_buf_num = 0;
- data->tx_buf_num = 0;
-
- data->isRunning = 1;
- INFO ("pthread id %u starts in interrupt mode", data->id);
- int thread_epfd = epoll_create (1);
-
- /* get interrupt queue id */
- int fd = -1;
- err = memif_get_queue_efd (c->conn, data->qid, &fd);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("memif_get_queue_efd: %s", memif_strerror (err));
- goto error;
- }
- add_epoll_fd (thread_epfd, fd, EPOLLIN);
-
- while (1)
- {
- memset (&evt, 0, sizeof (evt));
- evt.events = EPOLLIN | EPOLLOUT;
- sigemptyset (&sigset);
- en = epoll_pwait (thread_epfd, &evt, 1, -1, &sigset);
- if (en < 0)
- {
- if (errno == EINTR)
- goto close;
- DBG ("epoll_pwait: %s", strerror (errno));
- goto error;
- }
- else if (en > 0)
- {
- /* receive data from shared memory buffers */
- err =
- memif_rx_burst (c->conn, data->qid, data->rx_bufs, MAX_MEMIF_BUFS,
- &rx);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("memif_rx_burst: %s", memif_strerror (err));
- data->rx_buf_num += rx;
- goto error;
- }
- data->rx_buf_num += rx;
- if (rx == 0)
- {
- continue;
- }
-
- DBG ("thread id: %u", data->id);
-
- DBG ("received %d buffers. %u/%u alloc/free buffers",
- rx, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
-
- err =
- memif_buffer_alloc (c->conn, data->qid, data->tx_bufs,
- data->rx_buf_num, &tx, 0);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("memif_buffer_alloc: %s", memif_strerror (err));
- data->tx_buf_num += tx;
- goto error;
- }
- data->tx_buf_num += tx;
- DBG ("allocated %d/%d buffers, %u free buffers",
- tx, data->rx_buf_num, MAX_MEMIF_BUFS - data->tx_buf_num);
-
- int i;
- for (i = 0; i < rx; i++)
- {
- resolve_packet ((void *) (data->rx_bufs + i)->data,
- (data->rx_bufs + i)->len,
- (void *) (data->tx_bufs + i)->data,
- &(data->tx_bufs + i)->len, c->ip_addr);
- }
-
- /* mark memif buffers and shared memory buffers as free */
- err = memif_refill_queue (c->conn, data->qid, rx, 0);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_buffer_free: %s", memif_strerror (err));
- data->rx_buf_num -= fb;
-
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
-
- err =
- memif_tx_burst (c->conn, data->qid, data->tx_bufs,
- data->tx_buf_num, &tx);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("memif_tx_burst: %s", memif_strerror (err));
- goto error;
- }
- DBG ("tx: %d/%u", tx, data->tx_buf_num);
- data->tx_buf_num -= tx;
- }
- }
-
-error:
- INFO ("thread %u error!", data->id);
- goto close;
-
-close:
- err = memif_refill_queue (c->conn, data->qid, rx, 0);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_buffer_free: %s", memif_strerror (err));
- data->rx_buf_num -= fb;
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
- free (data->rx_bufs);
- free (data->tx_bufs);
- data->isRunning = 0;
- INFO ("pthread id %u exit", data->id);
- pthread_exit (NULL);
-
-}
-
-/* informs user about connected status. private_ctx is used by user to identify connection
- (multiple connections WIP) */
-int
-on_connect (memif_conn_handle_t conn, void *private_ctx)
-{
- long index = (*(long *) private_ctx);
- int err, i, ti;
- INFO ("memif connected! index %ld", index);
- memif_connection_t *c = &memif_connection[index];
- c->pending_del = 0;
-
- for (i = 0; i < MAX_QUEUES; i++)
- {
- err = memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, i);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_set_rx_mode: %s qid: %u", memif_strerror (err), i);
- else
- {
- ti = (index * MAX_QUEUES) + i;
- if (thread_data[ti].isRunning)
- {
- INFO ("thread id: %d already running!", ti);
- continue;
- }
- thread_data[ti].index = index;
- thread_data[ti].qid = i;
- thread_data[ti].id = ti;
- if ((i % 2) == 0)
- pthread_create (&thread[ti],
- NULL, memif_rx_poll, (void *) &thread_data[ti]);
- else
- pthread_create (&thread[ti],
- NULL, memif_rx_interrupt,
- (void *) &thread_data[ti]);
- }
-
- }
- return 0;
-}
-
-/* informs user about disconnected status. private_ctx is used by user to identify connection
- (multiple connections WIP) */
-int
-on_disconnect (memif_conn_handle_t conn, void *private_ctx)
-{
- void *ptr;
- long index = (*(long *) private_ctx);
- memif_connection_t *c = &memif_connection[index];
- int i, ti;
- INFO ("memif disconnected!");
- /* inform thread in polling mode about memif disconnection */
- c->pending_del = 1;
- for (i = 0; i < MAX_QUEUES; i++)
- {
- ti = (index * MAX_QUEUES) + i;
- if (!thread_data[ti].isRunning)
- continue;
- if ((i % 2) != 0)
- pthread_kill (thread[ti], SIGUSR1); /* interrupt thread in interrupt mode */
- pthread_join (thread[ti], &ptr);
- }
- return 0;
-}
-
-/* user needs to watch new fd or stop watching fd that is about to be closed.
- control fd will be modified during connection establishment to minimize CPU usage */
-int
-control_fd_update (int fd, uint8_t events, void *ctx)
-{
- /* convert memif event definitions to epoll events */
- if (events & MEMIF_FD_EVENT_DEL)
- return del_epoll_fd (main_epfd, 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 (main_epfd, fd, evt);
-
- return add_epoll_fd (main_epfd, fd, evt);
-}
-
-int
-icmpr_memif_create (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];
-
- /* setting memif connection arguments */
- memif_conn_args_t args;
- memset (&args, 0, sizeof (args));
- args.is_master = 0;
- args.log2_ring_size = 10;
- args.buffer_size = 2048;
- args.num_s2m_rings = 2;
- args.num_m2s_rings = 2;
- strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
- args.mode = 0;
- /* socket filename is not specified, because this app is supposed to
- connect to VPP over memif. so default socket filename will be used */
- /* default socketfile = /run/vpp/memif.sock */
-
- args.interface_id = index;
- /* last argument for memif_create (void * private_ctx) is used by user
- to identify connection. this context is returned with callbacks */
- int err = memif_create (&c->conn,
- &args, on_connect, on_disconnect, NULL,
- &ctx[index]);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("memif_create: %s", memif_strerror (err));
- return 0;
- }
-
- c->index = index;
-
- c->ip_addr[0] = 192;
- c->ip_addr[1] = 168;
- c->ip_addr[2] = c->index + 1;
- c->ip_addr[3] = 2;
- return 0;
-}
-
-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];
-
- int err;
- /* disconnect then delete memif connection */
- err = memif_delete (&c->conn);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_delete: %s", memif_strerror (err));
- return 0;
-}
-
-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> - create memif (slave-mode)\n");
- printf ("\tdel <index> - delete memif\n");
- printf ("\tshow - show connection details\n");
- printf ("\tip-set <index> <ip-addr> - set interface ip address\n");
-}
-
-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_set_ip (long index, char *ip)
-{
- 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->conn == NULL)
- {
- INFO ("no connection at index %ld", index);
- return 0;
- }
-
- char *end;
- char *ui;
- uint8_t tmp[4];
- 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);
-
- c->ip_addr[0] = tmp[0];
- c->ip_addr[1] = tmp[1];
- c->ip_addr[2] = tmp[2];
- c->ip_addr[3] = tmp[3];
-
- INFO ("memif %ld ip address set to %u.%u.%u.%u",
- index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
-
- return 0;
-
-error:
- INFO ("invalid ip address");
- return 0;
-}
-
-
-int
-user_input_handler ()
-{
- char *in = (char *) malloc (256);
- char *ui = fgets (in, 256, stdin);
- char *end;
- 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)
- icmpr_memif_create (strtol (ui, &end, 10));
- else
- INFO ("expected id");
- 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, "show", 4) == 0)
- {
- print_memif_details ();
- goto done;
- }
- else if (strncmp (ui, "ip-set", 6) == 0)
- {
- ui = strtok (NULL, " ");
- if (ui != NULL)
- icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " "));
- else
- INFO ("expected id");
- goto done;
- }
- else
- {
- DBG ("unknown command: %s", ui);
- goto done;
- }
-
- return 0;
-done:
- free (in);
- return 0;
-}
-
-int
-poll_event (int timeout)
-{
- struct epoll_event evt;
- int app_err = 0, memif_err = 0, en = 0;
- uint32_t events = 0;
- memset (&evt, 0, sizeof (evt));
- evt.events = EPOLLIN | EPOLLOUT;
- sigset_t sigset;
- sigemptyset (&sigset);
- en = epoll_pwait (main_epfd, &evt, 1, timeout, &sigset);
- 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 epoll 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 ()
-{
- main_epfd = epoll_create (1);
- add_epoll_fd (main_epfd, 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));
-
- for (i = 0; i < MAX_CONNS; i++)
- {
- memif_connection[i].conn = NULL;
- ctx[i] = i;
- }
-
- memset (&thread_data, 0, sizeof (memif_thread_data_t) * MAX_THREADS);
-
- print_help ();
-
- /* main loop */
- while (1)
- {
- if (poll_event (-1) < 0)
- {
- DBG ("poll_event error!");
- }
- }
-}
diff --git a/extras/libmemif/examples/icmp_responder-mt_3-1/main.c b/extras/libmemif/examples/icmp_responder-mt_3-1/main.c
deleted file mode 100644
index 86c57d192e3..00000000000
--- a/extras/libmemif/examples/icmp_responder-mt_3-1/main.c
+++ /dev/null
@@ -1,549 +0,0 @@
-/*
- *------------------------------------------------------------------
- * Copyright (c) 2019 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 <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <getopt.h>
-#include <errno.h>
-#include <inttypes.h>
-#include <pthread.h>
-#include <stdbool.h>
-#include <unistd.h>
-
-#include <sys/epoll.h>
-#include <sys/eventfd.h>
-
-#include <libmemif.h>
-#include <icmp_proto.h>
-
-
-#define APP_NAME "ICMP_Responder_mt_v3.1"
-#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
-
-#define ICMPR_BUFFER_LENGTH 32
-#define ICMPR_SOCKET_FILENAME_LEN 256
-#define ICMPR_MEMIF_BUFFER_NUM 256
-
-static struct option options[] = {
- {"threads", required_argument, 0, 't'},
- {"if_num", required_argument, 0, 'i'}
-};
-
-struct memif_connection
-{
- uint16_t id; /* unique interface id */
- bool connected; /* is connected */
- struct per_thread_data *ptd; /* per thread data */
- memif_conn_handle_t handle; /* memif connection handle */
- uint8_t ip_addr[4]; /* ip4 address */
-};
-
-struct per_thread_data
-{
- bool running; /* is thread main loop running */
- uint8_t index; /* thread index */
- int epfd; /* epoll file descriptor */
- int pcfd; /* poll cancel file descriptor */
- uint16_t if_num; /* number of interfaces on this thread */
- struct memif_connection *conns; /* memif connections pool */
- memif_per_thread_main_handle_t pt_main; /* memif per thread main handle */
- memif_socket_handle_t socket_handle; /* memif socket handle */
-};
-
-struct icmpr_main
-{
- uint8_t threads; /* number of threads */
- uint16_t per_thread_if_num; /* number of interfaces per thread */
- struct per_thread_data *ptd; /* per thread data pool */
- pthread_t *pthread; /* thread pool */
-};
-
-struct icmpr_main icmpr_main;
-
-int
-add_epoll_fd (int epfd, 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 epfd, 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 modified on epoll", fd);
- return 0;
-}
-
-int
-del_epoll_fd (int epfd, 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;
-}
-
-/* Called when libmemif requests an update on any of its file descriptors */
-static int
-control_fd_update (int fd, uint8_t events, void *private_ctx)
-{
- struct per_thread_data *ptd = (struct per_thread_data *) private_ctx;
- uint32_t evt = 0;
-
- if (ptd == NULL)
- return -1;
-
- /* convert memif event definitions to epoll events */
- if (events & MEMIF_FD_EVENT_DEL)
- return del_epoll_fd (ptd->epfd, fd);
-
- 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 (ptd->epfd, fd, evt);
-
- return add_epoll_fd (ptd->epfd, fd, evt);
-}
-
-static int
-on_connect (memif_conn_handle_t conn, void *private_ctx)
-{
- struct per_thread_data *ptd = (struct per_thread_data *) private_ctx;
- struct memif_connection *c;
- int i = 0;
-
- while (i < ptd->if_num && ptd->conns[i].handle != conn)
- i++;
- c = &ptd->conns[i];
-
- c->connected = true;
- DBG ("Connected: %u", c->id);
-
- memif_refill_queue (conn, 0, -1, 0);
-
- return 0;
-}
-
-static int
-on_disconnect (memif_conn_handle_t conn, void *private_ctx)
-{
- struct per_thread_data *ptd = (struct per_thread_data *) private_ctx;
- struct memif_connection *c;
- int i = 0;
-
- while (i < ptd->if_num && ptd->conns[i].handle != conn)
- i++;
- c = &ptd->conns[i];
-
- c->connected = false;
- DBG ("Disconnected: %u", c->id);
-
- return 0;
-}
-
-static int
-on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
-{
- struct per_thread_data *ptd = (struct per_thread_data *) private_ctx;
- struct memif_connection *c;
- memif_buffer_t mbufs[ICMPR_MEMIF_BUFFER_NUM];
- uint16_t rx = 0;
- uint16_t tx = 0;
- uint16_t ret;
- memif_err_t err;
- int i = 0;
-
- memset (mbufs, 0, sizeof (memif_buffer_t) * ICMPR_MEMIF_BUFFER_NUM);
-
- while (i < ptd->if_num && ptd->conns[i].handle != conn)
- i++;
- c = &ptd->conns[i];
-
- /* receive data from shared memory buffers */
- err = memif_rx_burst (conn, qid, mbufs, ICMPR_MEMIF_BUFFER_NUM, &rx);
- if (err != MEMIF_ERR_SUCCESS)
- {
- printf ("memif_rx_burst: %s\n", memif_strerror (err));
- goto error;
- }
-
- /* resolve packet in place (zer-copy slave) */
- for (i = 0; i < rx; i++)
- resolve_packet2 (mbufs[i].data, &mbufs[i].len, c->ip_addr);
-
- /* enqueue received buffers */
- err = memif_buffer_enq_tx (conn, qid, mbufs, i, &tx);
- if (err != MEMIF_ERR_SUCCESS)
- {
- printf ("memif_rx_burst: %s\n", memif_strerror (err));
- goto error;
- }
-
- /* mark shared memory buffers as free */
- err = memif_refill_queue (conn, qid, rx, 0);
- if (err != MEMIF_ERR_SUCCESS)
- {
- printf ("memif_rx_burst: %s\n", memif_strerror (err));
- goto error;
- }
-
- err = memif_tx_burst (conn, qid, mbufs, tx, &ret);
- if (err != MEMIF_ERR_SUCCESS)
- {
- printf ("memif_rx_burst: %s\n", memif_strerror (err));
- goto error;
- }
-
- return 0;
-
-error:
- memif_refill_queue (conn, qid, -1, 0);
- return -1;
-}
-
-int
-poll_event (memif_per_thread_main_handle_t pt_main, int pcfd, int epfd,
- int timeout)
-{
- struct epoll_event evt;
- int en = 0;
- uint8_t events = 0;
- memset (&evt, 0, sizeof (evt));
- evt.events = EPOLLIN | EPOLLOUT;
-
- en = epoll_pwait (epfd, &evt, 1, timeout, NULL);
- if (en < 0)
- {
- printf ("epoll_pwait: %s\n", strerror (errno));
- return -1;
- }
-
- if (en > 0)
- {
- /* Cancel event polling */
- if (evt.data.fd == pcfd)
- return 1;
-
- 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;
-
- /* No need to use locks, as the database is separated */
- memif_per_thread_control_fd_handler (pt_main, evt.data.fd, events);
- }
-
- return 0;
-}
-
-static void *
-icmpr_thread_fn (void *data)
-{
- struct per_thread_data *ptd = (struct per_thread_data *) data;
- int rv;
- uint16_t i;
- char socket_filename[ICMPR_SOCKET_FILENAME_LEN] = "/run/vpp/memif";
- memif_conn_args_t args;
-
- ptd->epfd = epoll_create (1);
-
- ptd->conns = malloc (sizeof (struct memif_connection) * ptd->if_num);
- if (ptd->conns == NULL)
- {
- printf ("%s\n", strerror (errno));
- return NULL;
- }
-
- memset (ptd->conns, 0, sizeof (struct memif_connection) * ptd->if_num);
-
- /* Initialize memif database (per thread). */
- rv =
- memif_per_thread_init (&ptd->pt_main, ptd, control_fd_update, APP_NAME,
- NULL, NULL, NULL);
- if (rv != MEMIF_ERR_SUCCESS)
- {
- printf ("memif_per_thread_init: %s\n", memif_strerror (rv));
- return NULL;
- }
-
- /* Create unique socket. Each thread requires a unique socket. Interfaces created
- * on the same thread can share one socket.
- */
- socket_filename[strlen (socket_filename)] = '0' + ptd->index;
- strncpy (socket_filename + strlen (socket_filename), ".sock", 5);
- DBG ("socket_filename: %s", socket_filename);
-
- rv = memif_per_thread_create_socket (ptd->pt_main, &ptd->socket_handle,
- socket_filename, ptd);
- if (rv != MEMIF_ERR_SUCCESS)
- {
- printf ("memif_per_thread_create_socket: %s\n", memif_strerror (rv));
- return NULL;
- }
-
- /* Create interfaces on this thread */
- for (i = 0; i < ptd->if_num; i++)
- {
- ptd->conns[i].ip_addr[0] = 192;
- ptd->conns[i].ip_addr[1] = 168;
- ptd->conns[i].ip_addr[2] = ptd->index + 1;
- ptd->conns[i].ip_addr[3] = i * 2 + 2;
-
- memset (&args, 0, sizeof (args));
-
- args.socket = ptd->socket_handle;
- ptd->conns[i].id = i;
- args.interface_id = i;
-
- rv = memif_create (&ptd->conns[i].handle, &args, on_connect,
- on_disconnect, on_interrupt, ptd);
- if (rv < 0)
- {
- printf ("%s\n", memif_strerror (rv));
- return NULL;
- }
- }
-
- /* Poll cancel file descriptor. When an event is received on this fd, exit thread
- * loop in respective thread.
- */
- ptd->pcfd = eventfd (0, EFD_NONBLOCK);
- if (ptd->pcfd < 0)
- {
- printf ("eventfd: %s\n", strerror (errno));
- return NULL;
- }
- if (add_epoll_fd (ptd->epfd, ptd->pcfd, EPOLLIN) < 0)
- {
- printf ("Failed to add poll cancel fd to epfd.");
- return NULL;
- }
-
- /* Thread loop */
- ptd->running = true;
- while (ptd->running)
- {
- rv = poll_event (ptd->pt_main, ptd->pcfd, ptd->epfd, -1);
- if (rv != 0)
- ptd->running = false;
- }
-
- /* Clean up */
- for (i = 0; i < ptd->if_num; i++)
- memif_delete (&ptd->conns[i].handle);
-
- memif_delete_socket (&ptd->socket_handle);
-
- memif_per_thread_cleanup (&ptd->pt_main);
-
- free (ptd->conns);
- close (ptd->pcfd);
-
- return NULL;
-}
-
-static void
-icmpr_print_help ()
-{
- printf
- ("exit - Exits the application.\nhelp - Print this help.\nshow - Show memif interfaces\n");
-}
-
-static void
-icmpr_show_memifs ()
-{
- struct icmpr_main *im = &icmpr_main;
- int i, j;
- memif_socket_handle_t sh;
-
- printf ("%u Threads %u Memifs (per thread)\n", im->threads,
- im->per_thread_if_num);
- printf ("=================================\n");
-
- for (i = 0; i < im->threads; i++)
- {
- sh = im->ptd[i].socket_handle;
- printf ("Thread %u %s\n", i, memif_get_socket_filename (sh));
- for (j = 0; j < im->per_thread_if_num; j++)
- {
- printf ("\tMemif id %u\n\t%s\n", im->ptd[i].conns[j].id,
- im->ptd[i].conns[j].connected ? "Link up" : "Link down");
- }
- }
-}
-
-int
-main (int argc, char **argv)
-{
- struct icmpr_main *im = &icmpr_main;
- int rv, i;
- int option_index = 0;
- bool running;
- char buffer[ICMPR_BUFFER_LENGTH];
- uint64_t b = 1;
-
- memset (im, 0, sizeof (struct icmpr_main));
-
- /* Default args */
- im->threads = 4;
- im->per_thread_if_num = 1;
-
- /* Parse args */
- while ((rv =
- getopt_long (argc, argv, "t:i:", options, &option_index)) != (-1))
- {
- switch (rv)
- {
- case 't':
- im->threads = strtoul (optarg, NULL, 10);
- break;
- case 'i':
- im->per_thread_if_num = strtoul (optarg, NULL, 10);
- break;
- default:
- break;
- }
- }
-
- /* Check args */
- if (im->threads < 1)
- {
- printf ("threads < 1\n");
- exit (EXIT_FAILURE);
- }
-
- if (im->per_thread_if_num < 1)
- {
- printf ("if_num < 1\n");
- exit (EXIT_FAILURE);
- }
-
- /* Allocate memory */
- im->ptd = malloc (sizeof (struct per_thread_data) * im->threads);
- if (im->ptd == NULL)
- {
- printf ("%s\n", strerror (errno));
- return -1;
- }
- im->pthread = malloc (sizeof (pthread_t) * im->threads);
- if (im->pthread == NULL)
- {
- printf ("%s\n", strerror (errno));
- return -1;
- }
-
- /* Initialize and create threads */
- for (i = 0; i < im->threads; i++)
- {
- im->ptd[i].index = i;
- im->ptd[i].if_num = im->per_thread_if_num;
- pthread_create (&im->pthread[i], NULL, icmpr_thread_fn, &im->ptd[i]);
- }
-
- icmpr_print_help ();
-
- /* Main loop */
- running = true;
- while (running)
- {
- printf ("cmd: ");
- memset (buffer, 0, ICMPR_BUFFER_LENGTH);
- if (fgets (buffer, ICMPR_BUFFER_LENGTH, stdin) != buffer)
- {
- printf ("%s\n", strerror (errno));
- running = false;
- }
-
- if (strncmp (buffer, "exit", 4) == 0)
- running = false;
- else if (strncmp (buffer, "help", 4) == 0)
- icmpr_print_help ();
- else if (strncmp (buffer, "show", 4) == 0)
- icmpr_show_memifs ();
- }
-
- for (i = 0; i < im->threads; i++)
- {
- /* Stop polling */
- rv = write (im->ptd[i].pcfd, &b, sizeof (b));
- if (rv < 0)
- {
- printf ("Failed to cancel polling. %s\n", strerror (errno));
- exit (EXIT_FAILURE);
- }
- pthread_join (im->pthread[i], NULL);
- }
-
- free (im->pthread);
- free (im->ptd);
-
- return 0;
-}
diff --git a/extras/libmemif/examples/icmp_responder-zero-copy-slave/main.c b/extras/libmemif/examples/icmp_responder-zero-copy-slave/main.c
deleted file mode 100644
index 3f871d7f23f..00000000000
--- a/extras/libmemif/examples/icmp_responder-zero-copy-slave/main.c
+++ /dev/null
@@ -1,1270 +0,0 @@
-/*
- *------------------------------------------------------------------
- * 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 <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 <time.h>
-
-#include <libmemif.h>
-#include <icmp_proto.h>
-
-#define APP_NAME "ICMP_Responder"
-#define IF_NAME "memif_connection"
-
-#define HEADROOM 0x80 /* 128b */
-#define ENCAP 0x60
-
-#ifdef ICMP_DBG
-#define DBG(...) do { \
- printf (APP_NAME":%s:%d: ", __func__, __LINE__); \
- printf (__VA_ARGS__); \
- printf ("\n"); \
- } while (0)
-#define LOG(...) do { \
- if (enable_log) { \
- dprintf (out_fd, __VA_ARGS__); \
- dprintf (out_fd, "\n"); \
- } \
- } while (0)
-#define LOG_FILE "/tmp/memif_time_test.txt"
-#else
-#define DBG(...)
-#define LOG(...)
-#endif
-
-#define INFO(...) do { \
- printf ("INFO: "__VA_ARGS__); \
- printf ("\n"); \
- } while (0)
-
-
-/* maximum tx/rx memif buffers */
-#define MAX_MEMIF_BUFS 256
-#define MAX_CONNS 50
-
-int epfd;
-int out_fd;
-uint8_t enable_log;
-
-typedef struct
-{
- uint16_t index;
- /* memif connection handle */
- memif_conn_handle_t conn;
- /* buffers */
- memif_buffer_t *bufs;
- /* allocated tx buffers counter */
- /* number of tx buffers pointing to shared memory */
- uint16_t tx_buf_num;
- /* allocated rx buffers counter */
- /* number of rx buffers pointing to shared memory */
- uint16_t rx_buf_num;
- /* interface ip address */
- 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;
- icmpr_flow_mode_t mode;
- uint64_t packet_count;
- uint16_t sequence;
- uint64_t tx;
- uint8_t ip_daddr[4];
- uint8_t hw_daddr[6];
- struct timespec *start, end;
-} icmpr_flow_t;
-
-memif_connection_t memif_connection[MAX_CONNS];
-long ctx[MAX_CONNS];
-icmpr_flow_t *flow;
-
-/* print details for all memif connections */
-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 ("\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);
-}
-
-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 modified 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;
-}
-
-/* informs user about connected status. private_ctx is used by user to identify connection
- (multiple connections WIP) */
-int
-on_connect (memif_conn_handle_t conn, void *private_ctx)
-{
- INFO ("memif connected!");
- memif_refill_queue (conn, 0, -1, HEADROOM);
- enable_log = 1;
- return 0;
-}
-
-/* informs user about disconnected status. private_ctx is used by user to identify connection
- (multiple connections WIP) */
-int
-on_disconnect (memif_conn_handle_t conn, void *private_ctx)
-{
- INFO ("memif disconnected!");
- return 0;
-}
-
-/* user needs to watch new fd or stop watching fd that is about to be closed.
- control fd will be modified during connection establishment to minimize CPU usage */
-int
-control_fd_update (int fd, uint8_t events, void *ctx)
-{
- /* 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);
-}
-
-/* called when event is polled on interrupt file descriptor.
- there are packets in shared memory ready to be received */
-/* handle packet processing in rx buffer then enqueue this buffer to tx and transmit */
-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;
- int i = 0; /* rx buffer iterator */
-
- /* loop while there are packets in shm */
- do
- {
- /* receive data from shared memory buffers (dequeue rx buffers) */
- err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
- ret_val = err;
- c->rx_counter += rx;
- c->rx_buf_num += rx;
- if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
- {
- INFO ("memif_rx_burst: %s", memif_strerror (err));
- goto error;
- }
-
- /* process buffers in place */
- for (i = 0; i < rx; i++)
- {
- resolve_packet2 ((void *) (c->bufs + i)->data,
- &(c->bufs + i)->len, c->ip_addr);
- }
-
- /* enqueue processed buffers to tx ring */
- err = memif_buffer_enq_tx (c->conn, qid, c->bufs, i, &tx);
- if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
- {
- INFO ("memif_buffer_alloc: %s", memif_strerror (err));
- goto error;
- }
- c->rx_buf_num -= tx;
- c->tx_buf_num += tx;
- c->tx_err_counter += i - tx;
-
- /* mark memif buffers and shared memory buffers as free */
- err = memif_refill_queue (c->conn, qid, rx, HEADROOM);
- 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",
- rx, rx, MAX_MEMIF_BUFS - rx);
-
- /* transmit allocated buffers */
- err = memif_tx_burst (c->conn, qid, c->bufs, rx, &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, -1, HEADROOM);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_buffer_free: %s", memif_strerror (err));
- c->rx_buf_num = 0;
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
- return 0;
-}
-
-/* add ethernet encap to packet in rx buffer then enqueue this buffer to tx and transmit */
-int
-on_interrupt0 (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;
- int i = 0; /* rx buffer iterator */
-
- /* loop while there are packets in shm */
- do
- {
- /* receive data from shared memory buffers (dequeue rx buffers) */
- err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
- ret_val = err;
- c->rx_counter += rx;
- c->rx_buf_num += rx;
- if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
- {
- INFO ("memif_rx_burst: %s", memif_strerror (err));
- goto error;
- }
-
- /* process bufers in place */
- for (i = 0; i < rx; i++)
- {
- resolve_packet3 (&c->bufs[i].data, &c->bufs[i].len, c->ip_addr);
- }
- /* enque processed buffers to tx ring */
- err = memif_buffer_enq_tx (c->conn, qid, c->bufs, rx, &tx);
- if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
- {
- INFO ("memif_buffer_alloc: %s", memif_strerror (err));
- goto error;
- }
- c->rx_buf_num -= tx;
- c->tx_buf_num += tx;
- c->tx_err_counter += i - tx;
-
- /* mark memif buffers and shared memory buffers as free */
- err = memif_refill_queue (c->conn, qid, rx, HEADROOM);
- 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",
- rx, rx, MAX_MEMIF_BUFS - rx);
-
- /* transmit allocated buffers */
- err = memif_tx_burst (c->conn, qid, c->bufs, i, &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, -1, HEADROOM);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_buffer_free: %s", memif_strerror (err));
- c->rx_buf_num = 0;
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
- return 0;
-}
-
-/* called when event is polled on interrupt file descriptor.
- there are packets in shared memory ready to be received */
-/* dev test modification: handle only ARP requests */
-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;
-
- do
- {
- /* receive data from shared memory buffers */
- err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
- ret_val = err;
- c->rx_buf_num += rx;
- c->rx_counter += rx;
- if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
- {
- INFO ("memif_rx_burst: %s", memif_strerror (err));
- goto error;
- }
-
- for (i = 0; i < rx; i++)
- {
- if (((struct ether_header *) (c->bufs + i)->data)->ether_type ==
- 0x0608)
- {
- /* process data in place */
- resolve_packet2 ((void *) (c->bufs + i)->data,
- &(c->bufs + i)->len, c->ip_addr);
- /* enque buffer to tx ring */
- memif_buffer_enq_tx (c->conn, qid, c->bufs, 1, &tx);
- c->rx_buf_num -= tx;
- memif_tx_burst (c->conn, qid, c->bufs, 1, &tx);
- }
- }
-
- err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
- 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, -1, HEADROOM);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_buffer_free: %s", memif_strerror (err));
- c->rx_buf_num = 0;
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- rx, 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 ("don't even try...");
- return 0;
- }
- memif_connection_t *c = &memif_connection[index];
-
- /* setting memif connection arguments */
- memif_conn_args_t args;
- 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;
- /* socket filename is not specified, because this app is supposed to
- connect to VPP over memif. so default socket filename will be used */
- /* default socketfile = /run/vpp/memif.sock */
-
- args.interface_id = index;
- /* last argument for memif_create (void * private_ctx) is used by user
- to identify connection. this context is returned with callbacks */
- int err;
- /* default interrupt */
- 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, "0", 1) == 0)
- {
- err = memif_create (&c->conn,
- &args, on_connect, on_disconnect, on_interrupt0,
- &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;
- /* alloc memif buffers */
- c->rx_buf_num = 0;
- c->tx_buf_num = 0;
- c->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;
-
- c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
-
-done:
- return 0;
-}
-
-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->bufs)
- free (c->bufs);
- c->bufs = NULL;
- c->tx_buf_num = 0;
- c->rx_buf_num = 0;
-
- int err;
- /* disconnect 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;
-}
-
-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-desc>] - create memif. index is also used as interface id, mode 0 = slave 1 = master, interrupt-desc none = default 0 = if ring is full wait 1 = handle only ARP requests\n");
- printf ("\tdel <index> - delete memif\n");
- printf ("\tshow - show connection details\n");
- printf ("\tip-set <index> <ip-addr> - set interface ip address\n");
- printf
- ("\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
- printf ("\tsh-count - print counters\n");
- printf ("\tcl-count - clear counters\n");
- printf
- ("\tsend <index> <tx> <ip> <mac> - send icmp, ommit mac to transmit on ip layer\n");
-}
-
-int
-icmpr_free ()
-{
- /* application cleanup */
- int err;
- long i;
- if (out_fd > 0)
- close (out_fd);
- out_fd = -1;
- 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_set_ip (long index, char *ip)
-{
- 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->conn == NULL)
- {
- INFO ("no connection at index %ld", index);
- return 0;
- }
-
- char *end;
- char *ui;
- uint8_t tmp[4];
- 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);
-
- c->ip_addr[0] = tmp[0];
- c->ip_addr[1] = tmp[1];
- c->ip_addr[2] = tmp[2];
- c->ip_addr[3] = tmp[3];
-
- INFO ("memif %ld ip address set to %u.%u.%u.%u",
- index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
-
- return 0;
-
-error:
- INFO ("invalid ip address");
- return 0;
-}
-
-int
-icmpr_set_rx_mode (long index, long qid, char *mode)
-{
- 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->conn == NULL)
- {
- INFO ("no connection at index %ld", index);
- return 0;
- }
-
- if (strncmp (mode, "interrupt", 9) == 0)
- {
- memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid);
- }
-
- else if (strncmp (mode, "polling", 7) == 0)
- {
- memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid);
- }
- else
- INFO ("expected rx mode <interrupt|polling>");
- return 0;
-}
-
-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);
- printf ("\tts: %lus %luns\n", c->t_sec, c->t_nsec);
- }
-}
-
-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->t_sec = c->t_nsec = c->tx_err_counter = c->tx_counter =
- c->rx_counter = 0;
- }
-}
-
-void
-icmpr_send_proc ()
-{
- memif_connection_t *c = &memif_connection[flow->index];
- if (c->conn == NULL)
- {
- INFO ("No connection at index %d. Stopping flow...\n", flow->index);
- goto stop_flow;
- }
- uint16_t tx, i;
- int err = MEMIF_ERR_SUCCESS;
-
- if (!flow->start)
- {
- flow->start = malloc (sizeof (struct timespec));
- memset (flow->start, 0, sizeof (struct timespec));
- timespec_get (flow->start, TIME_UTC);
- }
-
- i = 0;
- err = memif_buffer_alloc (c->conn, 0, c->bufs,
- MAX_MEMIF_BUFS >
- flow->packet_count ? flow->packet_count :
- MAX_MEMIF_BUFS, &tx, 64);
- if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
- {
- INFO ("memif_buffer_alloc: %s Stopping flow...\n",
- memif_strerror (err));
- goto stop_flow;
- }
- c->tx_buf_num += tx;
-
- while (tx)
- {
- generate_packet2 ((void *) c->bufs[i].data,
- &c->bufs[i].len, c->ip_addr,
- flow->ip_daddr, flow->hw_daddr, (flow->sequence)++,
- flow->mode);
- i++;
- tx--;
- }
- err = memif_tx_burst (c->conn, 0, c->bufs, i, &tx);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("memif_tx_burst: %s Stopping flow...\n", memif_strerror (err));
- goto stop_flow;
- }
- c->tx_buf_num -= tx;
- c->tx_counter += tx;
- flow->tx += tx;
- flow->packet_count -= tx;
-
- if (flow->packet_count == 0)
- {
- timespec_get (&flow->end, TIME_UTC);
- INFO ("Flow finished!");
- INFO ("Flow length: %lu", flow->tx);
- uint64_t t1 = flow->end.tv_sec - flow->start->tv_sec;
- uint64_t t2;
- if (flow->end.tv_nsec > flow->start->tv_nsec)
- {
- t2 = flow->end.tv_nsec - flow->start->tv_nsec;
- }
- else
- {
- t2 = flow->start->tv_nsec - flow->end.tv_nsec;
- t1--;
- }
- c->t_sec = t1;
- c->t_nsec = t2;
- INFO ("Flow time: %lus %luns", t1, t2);
- double tmp = t1;
- tmp += t2 / 1e+9;
- tmp = flow->tx / tmp;
- INFO ("Average pps: %f", tmp);
- INFO ("Stopping flow...");
- goto stop_flow;
- }
-
- return;
-
-stop_flow:
- if (flow)
- {
- if (flow->start)
- free (flow->start);
- free (flow);
- }
- flow = NULL;
- return;
-}
-
-int
-icmpr_send (long index, long packet_num, char *input)
-{
- if (flow)
- {
- printf ("only one flow allowed\n");
- return 0;
- }
-
- memif_connection_t *c = &memif_connection[index];
- char *end;
- char *ui;
- uint8_t tmp[6];
- if (c->conn == NULL)
- return -1;
-
- flow = malloc (sizeof (icmpr_flow_t));
- flow->index = index;
- flow->packet_count = packet_num;
- flow->sequence = 0;
- flow->tx = 0;
- flow->start = NULL;
- memset (&flow->end, 0, sizeof (struct timespec));
-
- INFO ("packet count: %lu", flow->packet_count);
- printf ("%s\n", input);
-
- ui = strtok (input, ".");
- 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);
-
- flow->ip_daddr[0] = tmp[0];
- flow->ip_daddr[1] = tmp[1];
- flow->ip_daddr[2] = tmp[2];
- flow->ip_daddr[3] = tmp[3];
-
- ui = strtok (NULL, " ");
- if (ui == NULL)
- {
- flow->mode = ICMPR_FLOW_MODE_IP;
- return 0;
- }
-
- ui = strtok (NULL, ":");
- 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);
-
- flow->hw_daddr[0] = tmp[0];
- flow->hw_daddr[1] = tmp[1];
- flow->hw_daddr[2] = tmp[2];
- flow->hw_daddr[3] = tmp[3];
- flow->hw_daddr[4] = tmp[4];
- flow->hw_daddr[5] = tmp[5];
-
- flow->mode = ICMPR_FLOW_MODE_ETH;
-
- return 0;
-
-error:
- INFO ("Invalid input\n");
- if (flow)
- free (flow);
- flow = NULL;
- 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, "show", 4) == 0)
- {
- print_memif_details ();
- goto done;
- }
- else if (strncmp (ui, "ip-set", 6) == 0)
- {
- ui = strtok (NULL, " ");
- if (ui != NULL)
- icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " "));
- else
- INFO ("expected id");
- goto done;
- }
- else if (strncmp (ui, "rx-mode", 7) == 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_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " "));
- else
- INFO ("expected qid");
- goto done;
- }
- else if (strncmp (ui, "sh-count", 8) == 0)
- {
- icmpr_print_counters ();
- }
- else if (strncmp (ui, "cl-count", 8) == 0)
- {
- icmpr_reset_counters ();
- }
- 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, " "));
- else
- INFO ("expected count");
- goto done;
- }
- else
- {
- INFO ("unknown command: %s", ui);
- goto done;
- }
-
- return 0;
-done:
- free (in);
- return 0;
-}
-
-int
-poll_event (int timeout)
-{
- struct epoll_event evt;
- int app_err = 0, memif_err = 0, en = 0;
- uint32_t events = 0;
- struct timespec start, end;
- 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 */
- timespec_get (&start, TIME_UTC);
- 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 epoll 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);
- }
- }
-
- timespec_get (&end, TIME_UTC);
- LOG ("interrupt: %ld", end.tv_nsec - start.tv_nsec);
-
- 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);
-
- flow = NULL;
-
-#ifdef LOG_FILE
- remove (LOG_FILE);
- enable_log = 0;
-
- out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
- if (out_fd < 0)
- INFO ("Error opening log file: %s", strerror (errno));
-#endif /* LOG_FILE */
-
- /* 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++)
- {
- memif_connection[i].conn = NULL;
- ctx[i] = i;
- }
-
- print_help ();
-
- /* main loop */
- while (1)
- {
- if (poll_event (0) < 0)
- {
- DBG ("poll_event error!");
- }
- if (flow)
- {
- icmpr_send_proc ();
- }
- }
-}
diff --git a/extras/libmemif/examples/icmp_responder/icmp_proto.c b/extras/libmemif/examples/icmp_responder/icmp_proto.c
deleted file mode 100644
index 785aebd5567..00000000000
--- a/extras/libmemif/examples/icmp_responder/icmp_proto.c
+++ /dev/null
@@ -1,524 +0,0 @@
-/*
- *------------------------------------------------------------------
- * 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 <assert.h>
-
-#include <icmp_proto.h>
-
-static uint16_t
-cksum (void *addr, ssize_t len)
-{
- char *data = (char *) addr;
-
- uint32_t acc = 0xffff;
-
- ssize_t i;
- for (i = 0; (i + 1) < len; i += 2)
- {
- uint16_t word;
- memcpy (&word, data + i, 2);
- acc += ntohs (word);
- if (acc > 0xffff)
- acc -= 0xffff;
- }
-
- if (len & 1)
- {
- uint16_t word = 0;
- memcpy (&word, data + len - 1, 1);
- acc += ntohs (word);
- if (acc > 0xffff)
- acc -= 0xffff;
- }
- return htons (~acc);
-}
-
-int
-print_packet (void *pck)
-{
- if (pck == NULL)
- {
- printf ("ICMP_PROTO: no data\n");
- return -1;
- }
- struct iphdr *ip;
- struct icmphdr *icmp;
- ip = (struct iphdr *) pck;
- icmp = (struct icmphdr *) (pck + sizeof (struct iphdr));
- printf ("received packet:\n");
- printf ("\tiphdr:\n");
- printf ("\t\tihl: %u\n\t\tversion: %u\n\t\tlen: %u\n\t\tid: %u\n",
- ip->ihl, ip->version, __bswap_16 (ip->tot_len), ip->id);
- printf ("\t\tprotocol: %u\n", ip->protocol);
-
- printf ("\t\tsaddr: ");
- int i;
- for (i = 0; i < 4; i++)
- {
- printf ("%u.", ((uint8_t *) & ip->saddr)[i]);
- }
- printf ("\n");
-
- printf ("\t\tdaddr: ");
- for (i = 0; i < 4; i++)
- {
- printf ("%u.", ((uint8_t *) & ip->daddr)[i]);
- }
- printf ("\n");
- printf ("\ticmphdr:\n");
- printf ("\t\ttype: %s\n",
- (icmp->type == ICMP_ECHO) ? "ICMP_ECHO" : "ICMP_ECHOREPLY");
-
- return 0;
-}
-
-static ssize_t
-resolve_arp (void *arp)
-{
- struct arphdr *resp = (struct arphdr *) arp;
-
- resp->ar_hrd = __bswap_16 (ARPHRD_ETHER);
-
- resp->ar_pro = __bswap_16 (0x0800);
-
- resp->ar_hln = 6;
- resp->ar_pln = 4;
-
- resp->ar_op = __bswap_16 (ARPOP_REPLY);
-
- return sizeof (struct arphdr);
-}
-
-static ssize_t
-resolve_eth_arp (struct ether_arp *eth_arp, void *eth_arp_resp,
- uint8_t ip_addr[4])
-{
- struct ether_arp *resp = (struct ether_arp *) eth_arp_resp;
-
- resolve_arp (&resp->ea_hdr);
-
- memcpy (resp->arp_tha, eth_arp->arp_sha, 6);
- memcpy (resp->arp_tpa, eth_arp->arp_spa, 4);
-
- memcpy (resp->arp_sha,
- (((struct ether_header *) (eth_arp_resp -
- sizeof (struct
- ether_header)))->ether_shost),
- 6);
-
- memcpy (resp->arp_spa, ip_addr, 4);
-
- return sizeof (struct ether_arp);
-}
-
-static ssize_t
-resolve_eth (struct ether_header *eth, void *eth_resp)
-{
- struct ether_header *resp = (struct ether_header *) eth_resp;
- memcpy (resp->ether_dhost, eth->ether_shost, 6);
-
- uint8_t hw_addr[6];
- int i;
- for (i = 0; i < 6; i++)
- {
- hw_addr[i] = 'a';
- }
- memcpy (resp->ether_shost, hw_addr, 6);
-
- resp->ether_type = eth->ether_type;
-
- return sizeof (struct ether_header);
-}
-
-static ssize_t
-resolve_ip (struct iphdr *ip, void *ip_resp, uint8_t ip_addr[4])
-{
- struct iphdr *resp = (struct iphdr *) ip_resp;
- resp->ihl = 5;
- resp->version = 4;
- resp->tos = 0;
- /*len updated later */
- resp->tot_len = 0x0000;
- resp->id = 0;
- resp->frag_off = 0;
- resp->ttl = 0x40;
- resp->protocol = 1;
- ((uint8_t *) & resp->saddr)[0] = ip_addr[0];
- ((uint8_t *) & resp->saddr)[1] = ip_addr[1];
- ((uint8_t *) & resp->saddr)[2] = ip_addr[2];
- ((uint8_t *) & resp->saddr)[3] = ip_addr[3];
- resp->daddr = ip->saddr;
-
- /* resp->check = cksum (resp, sizeof (struct iphdr)); */
-
- return sizeof (struct iphdr);
-}
-
-static ssize_t
-resolve_icmp (struct icmphdr *icmp, void *icmp_resp)
-{
- struct icmphdr *resp = (struct icmphdr *) icmp_resp;
- resp->type = 0x00;
- resp->code = 0;
- resp->un.echo.id = icmp->un.echo.id;
- resp->un.echo.sequence = icmp->un.echo.sequence;
-
- /*resp->checksum = cksum (resp, sizeof (struct icmphdr)); */
-
- return sizeof (struct icmphdr);
-}
-
-int
-resolve_packet (void *in_pck, ssize_t in_size,
- void *out_pck, uint32_t * out_size, uint8_t ip_addr[4])
-{
- struct ether_header *eh;
- struct ether_arp *eah;
- struct iphdr *ip, *ip_out;
- struct icmphdr *icmp;
- *out_size = 0;
-
- if ((in_pck == NULL) || (out_pck == NULL))
- return -1;
-
- eh = (struct ether_header *) in_pck;
- *out_size = resolve_eth (eh, out_pck);
-
- if (eh->ether_type == 0x0608)
- {
- eah = (struct ether_arp *) (in_pck + *out_size);
- *out_size += resolve_eth_arp (eah, out_pck + *out_size, ip_addr);
-
- }
- else if (eh->ether_type == 0x0008)
- {
-#ifdef ICMP_DBG
- print_packet (in_pck + *out_size);
-#endif
- ip = (struct iphdr *) (in_pck + *out_size);
- ip_out = (struct iphdr *) (out_pck + *out_size);
- *out_size += resolve_ip (ip, out_pck + *out_size, ip_addr);
- if (ip->protocol == 1)
- {
- icmp = (struct icmphdr *) (in_pck + *out_size);
- *out_size += resolve_icmp (icmp, out_pck + *out_size);
- ((struct icmphdr *) (out_pck + *out_size -
- sizeof (struct icmphdr)))->checksum =
- cksum (out_pck + *out_size - sizeof (struct icmphdr),
- sizeof (struct icmphdr));
- /* payload */
- memcpy (out_pck + *out_size, in_pck + *out_size,
- in_size - *out_size);
- *out_size = in_size;
- ip_out->tot_len =
- __bswap_16 (*out_size - sizeof (struct ether_header));
- ip_out->check = cksum (ip_out, sizeof (struct iphdr));
- }
- }
- return 0;
-}
-
-static ssize_t
-generate_eth (struct ether_header *eh, uint8_t hw_daddr[6])
-{
- uint8_t hw_addr[6];
- int i;
- for (i = 0; i < 6; i++)
- {
- hw_addr[i] = 'a';
- }
- memcpy (eh->ether_shost, hw_addr, 6);
- memcpy (eh->ether_dhost, hw_daddr, 6);
-
- eh->ether_type = 0x0008;
-
- return sizeof (struct ether_header);
-}
-
-static ssize_t
-generate_ip (struct iphdr *ip, uint8_t saddr[4], uint8_t daddr[4])
-{
- ip->ihl = 5;
- ip->version = 4;
- ip->tos = 0;
- /*len updated later */
- ip->tot_len = 0x5400;
- ip->id = 0;
- ip->frag_off = 0;
- ip->ttl = 0x40;
- ip->protocol = 1;
- /* saddr */
- ((uint8_t *) & ip->saddr)[0] = saddr[0];
- ((uint8_t *) & ip->saddr)[1] = saddr[1];
- ((uint8_t *) & ip->saddr)[2] = saddr[2];
- ((uint8_t *) & ip->saddr)[3] = saddr[3];
- /* daddr */
- ((uint8_t *) & ip->daddr)[0] = daddr[0];
- ((uint8_t *) & ip->daddr)[1] = daddr[1];
- ((uint8_t *) & ip->daddr)[2] = daddr[2];
- ((uint8_t *) & ip->daddr)[3] = daddr[3];
-
- ip->check = cksum (ip, sizeof (struct iphdr));
-
- return sizeof (struct iphdr);
-}
-
-static ssize_t
-generate_icmp (struct icmphdr *icmp, uint32_t seq)
-{
- icmp->type = ICMP_ECHO;
- icmp->code = 0;
- icmp->un.echo.id = 0;
- icmp->un.echo.sequence = seq;
-
- return sizeof (struct icmphdr);
-}
-
-int
-generate_packet (void *pck, uint32_t * size, uint8_t saddr[4],
- uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq)
-{
- struct ether_header *eh;
- struct iphdr *ip;
- struct icmphdr *icmp;
-
- *size = 0;
-
- eh = (struct ether_header *) pck;
- *size += generate_eth (eh, hw_daddr);
-
- ip = (struct iphdr *) (pck + *size);
- *size += generate_ip (ip, saddr, daddr);
-
- icmp = (struct icmphdr *) (pck + *size);
- *size += generate_icmp (icmp, seq);
-
- ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
- cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));
-
- ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
- ip->check = 0;
- ip->check = cksum (ip, sizeof (struct iphdr));
-
- return 0;
-}
-
-int
-generate_packet2 (void *pck, uint32_t * size, uint8_t saddr[4],
- uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq,
- icmpr_flow_mode_t mode)
-{
- struct ether_header *eh;
- struct iphdr *ip;
- struct icmphdr *icmp;
-
- *size = 0;
-
- if (mode == ICMPR_FLOW_MODE_ETH)
- {
- eh = (struct ether_header *) pck;
- *size += generate_eth (eh, hw_daddr);
- }
-
- ip = (struct iphdr *) (pck + *size);
- *size += generate_ip (ip, saddr, daddr);
-
- icmp = (struct icmphdr *) (pck + *size);
- *size += generate_icmp (icmp, seq);
-
- ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
- cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));
-
- ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
- ip->check = 0;
- ip->check = cksum (ip, sizeof (struct iphdr));
-
- return 0;
-}
-
-#define GET_HEADER(out,hdr,src,off) do { \
- out = (hdr*)(src + off); \
- off += sizeof (hdr); \
- } while (0)
-
-int
-resolve_packet2 (void *pck, uint32_t * size, uint8_t ip_addr[4])
-{
- struct ether_header *eh;
- struct ether_arp *eah;
- struct iphdr *ip;
- struct icmphdr *icmp;
- uint32_t offset = 0;
-
- if (pck == NULL)
- return 0;
-
- GET_HEADER (eh, struct ether_header, pck, offset);
-
- memcpy (eh->ether_dhost, eh->ether_shost, 6);
- memcpy (eh->ether_shost, "aaaaaa", 6);
-
- if (eh->ether_type == 0x0608)
- {
- GET_HEADER (eah, struct ether_arp, pck, offset);
- struct arphdr *arp = &eah->ea_hdr;
-
- arp->ar_hrd = __bswap_16 (ARPHRD_ETHER);
- arp->ar_pro = __bswap_16 (0x0800);
-
- arp->ar_hln = 6;
- arp->ar_pln = 4;
-
- arp->ar_op = __bswap_16 (ARPOP_REPLY);
-
- memcpy (eah->arp_tha, eah->arp_sha, 6);
- memcpy (eah->arp_tpa, eah->arp_spa, 4);
-
- memcpy (eah->arp_sha, eh->ether_shost, 6);
- memcpy (eah->arp_spa, ip_addr, 4);
- }
-
- else if (eh->ether_type == 0x0008)
- {
- GET_HEADER (ip, struct iphdr, pck, offset);
-
- if (ip->protocol == 1)
- {
- ip->ihl = 5;
- ip->version = 4;
- ip->tos = 0;
- ip->tot_len = 0x0000;
- ip->id = 0;
- ip->frag_off = 0;
- ip->ttl = 0x40;
- ip->protocol = 1;
- ip->check = 0x0000;
- ip->daddr = ip->saddr;
- ((uint8_t *) & ip->saddr)[0] = ip_addr[0];
- ((uint8_t *) & ip->saddr)[1] = ip_addr[1];
- ((uint8_t *) & ip->saddr)[2] = ip_addr[2];
- ((uint8_t *) & ip->saddr)[3] = ip_addr[3];
-
- GET_HEADER (icmp, struct icmphdr, pck, offset);
-
- icmp->type = 0x00;
- icmp->code = 0;
- icmp->checksum = cksum (icmp, sizeof (struct icmphdr));
-
- /* rest is payload */
- offset = *size;
-
- ip->tot_len = __bswap_16 (offset - sizeof (struct ether_header));
- ip->check = cksum (ip, sizeof (struct iphdr));
- }
- }
-
- assert (offset == *size && "unsupported protocol");
- return 0;
-}
-
-
-int
-resolve_packet3 (void **pck_, uint32_t * size, uint8_t ip_addr[4])
-{
- struct ether_header *eh;
- struct iphdr *ip;
- struct icmphdr *icmp;
- int32_t offset = 0;
- uint16_t encap_size = sizeof (struct ether_header);
- void *pck = *pck_;
-
- if (pck == NULL)
- return 0;
-
- *pck_ -= encap_size;
- offset -= encap_size;
-
- GET_HEADER (eh, struct ether_header, pck, offset);
-
- uint8_t hw_daddr[6];
- memset (hw_daddr, 0, sizeof (uint8_t) * 6);
-
- generate_eth (eh, hw_daddr);
-
- if (eh->ether_type == 0x0008)
- {
- GET_HEADER (ip, struct iphdr, pck, offset);
-
- if (ip->protocol == 1)
- {
- ip->ihl = 5;
- ip->version = 4;
- ip->tos = 0;
- ip->tot_len = 0x0000;
- ip->id = 0;
- ip->frag_off = 0;
- ip->ttl = 0x40;
- ip->protocol = 1;
- ip->check = 0x0000;
- ip->daddr = ip->saddr;
- ((uint8_t *) & ip->saddr)[0] = ip_addr[0];
- ((uint8_t *) & ip->saddr)[1] = ip_addr[1];
- ((uint8_t *) & ip->saddr)[2] = ip_addr[2];
- ((uint8_t *) & ip->saddr)[3] = ip_addr[3];
-
- GET_HEADER (icmp, struct icmphdr, pck, offset);
-
- icmp->type = 0x00;
- icmp->code = 0;
- icmp->checksum = cksum (icmp, sizeof (struct icmphdr));
-
- /* rest is payload */
- offset = *size;
-
- ip->tot_len = __bswap_16 (offset - sizeof (struct ether_header));
- ip->check = cksum (ip, sizeof (struct iphdr));
- }
- }
-
- offset += encap_size;
-
- assert (offset != *size &&
- "new packet length must be increased by encap size");
-
- /* overwrite packet size */
- *size = offset;
-
- return 0;
-}
diff --git a/extras/libmemif/examples/icmp_responder/icmp_proto.h b/extras/libmemif/examples/icmp_responder/icmp_proto.h
deleted file mode 100644
index 1575719bd6a..00000000000
--- a/extras/libmemif/examples/icmp_responder/icmp_proto.h
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- *------------------------------------------------------------------
- * 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 _ICMP_PROTO_H_
-#define _ICMP_PROTO_H_
-
-typedef enum
-{
- ICMPR_FLOW_MODE_ETH = 0,
- ICMPR_FLOW_MODE_IP,
-} icmpr_flow_mode_t;
-
-int resolve_packet (void *in_pck, ssize_t in_size, void *out_pck,
- uint32_t * out_size, uint8_t ip_addr[4]);
-
-/* resolve packet in place */
-int resolve_packet2 (void *pck, uint32_t * size, uint8_t ip_addr[4]);
-
-/* resolve packet in place and add eth encap */
-int resolve_packet3 (void **pck, uint32_t * size, uint8_t ip_addr[4]);
-
-int generate_packet (void *pck, uint32_t * size, uint8_t saddr[4],
- uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq);
-
-int generate_packet2 (void *pck, uint32_t * size, uint8_t saddr[4],
- uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq,
- icmpr_flow_mode_t);
-
-int print_packet (void *pck);
-
-#endif /* _ICMP_PROTO_H_ */
diff --git a/extras/libmemif/examples/icmp_responder/main.c b/extras/libmemif/examples/icmp_responder/main.c
deleted file mode 100644
index 517512340e0..00000000000
--- a/extras/libmemif/examples/icmp_responder/main.c
+++ /dev/null
@@ -1,417 +0,0 @@
-/*
- *------------------------------------------------------------------
- * 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 <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 <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
-
-#define INFO(...) do { \
- printf ("INFO: "__VA_ARGS__); \
- printf ("\n"); \
- } while (0)
-
-/* maximum tx/rx memif buffers */
-#define MAX_MEMIF_BUFS 256
-
-typedef struct
-{
- uint16_t index;
- /* memif conenction handle */
- memif_conn_handle_t conn;
- /* transmit queue id */
- uint16_t tx_qid;
- /* tx buffers */
- memif_buffer_t *tx_bufs;
- /* allocated tx buffers counter */
- /* number of tx buffers pointing to shared memory */
- uint16_t tx_buf_num;
- /* rx buffers */
- memif_buffer_t *rx_bufs;
- /* allcoated rx buffers counter */
- /* number of rx buffers pointing to shared memory */
- uint16_t rx_buf_num;
- /* interface ip address */
- uint8_t ip_addr[4];
-} memif_connection_t;
-
-memif_connection_t memif_connection;
-int epfd;
-
-static void
-print_memif_details ()
-{
- memif_connection_t *c = &memif_connection;
- printf ("MEMIF DETAILS\n");
- printf ("==============================\n");
-
-
- memif_details_t md;
- memset (&md, 0, sizeof (md));
- ssize_t buflen = 2048;
- char *buf = malloc (buflen);
- memset (buf, 0, buflen);
- int err, e;
-
- err = memif_get_details (c->conn, &md, buf, buflen);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("%s", memif_strerror (err));
- if (err == MEMIF_ERR_NOCONN)
- {
- free (buf);
- return;
- }
- }
-
- 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 ("\tsocket filename: %s\n", (char *) md.socket_filename);
- 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\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\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);
-}
-
-/* informs user about connected status. private_ctx is used by user to identify connection
- (multiple connections WIP) */
-int
-on_connect (memif_conn_handle_t conn, void *private_ctx)
-{
- INFO ("memif connected!");
- return 0;
-}
-
-/* informs user about disconnected status. private_ctx is used by user to identify connection
- (multiple connections WIP) */
-int
-on_disconnect (memif_conn_handle_t conn, void *private_ctx)
-{
- INFO ("memif disconnected!");
- return 0;
-}
-
-int
-icmpr_memif_delete ()
-{
- int err;
- /* disconnect then delete memif connection */
- err = memif_delete (&(&memif_connection)->conn);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_delete: %s", memif_strerror (err));
- return 0;
-}
-
-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 ("\tuse CTRL+C to exit\n");
-}
-
-int
-icmpr_buffer_alloc (long n, uint16_t qid)
-{
- memif_connection_t *c = &memif_connection;
- int err;
- uint16_t r;
- /* set data pointer to shared memory and set buffer_len to shared memory buffer len */
- err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r, 0);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("memif_buffer_alloc: %s", memif_strerror (err));
- c->tx_buf_num += r;
- return -1;
- }
- c->tx_buf_num += r;
- DBG ("allocated %d/%ld buffers, %u free buffers", r, n,
- MAX_MEMIF_BUFS - c->tx_buf_num);
- return 0;
-}
-
-int
-icmpr_tx_burst (uint16_t qid)
-{
- memif_connection_t *c = &memif_connection;
- int err;
- uint16_t r;
- /* inform peer memif interface about data in shared memory buffers */
- /* mark memif buffers as free */
- err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_tx_burst: %s", memif_strerror (err));
- DBG ("tx: %d/%u", r, c->tx_buf_num);
- c->tx_buf_num -= r;
- return 0;
-}
-
-int
-icmpr_free ()
-{
- /* application cleanup */
- int err;
- memif_connection_t *c = &memif_connection;
- free (c->tx_bufs);
- c->tx_bufs = NULL;
- free (c->rx_bufs);
- c->rx_bufs = NULL;
-
- err = memif_cleanup ();
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_delete: %s", memif_strerror (err));
-
- return 0;
-}
-
-void
-icmpr_exit (int sig)
-{
- printf ("\n");
- icmpr_memif_delete ();
- icmpr_free ();
- exit (EXIT_SUCCESS);
-}
-
-/* called when event is polled on interrupt file descriptor.
- there are packets in shared memory ready to be received */
-int
-on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
-{
- DBG ("interrupted");
- memif_connection_t *c = &memif_connection;
- int err;
- uint16_t rx;
- /* receive data from shared memory buffers */
- err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
- c->rx_buf_num += rx;
-
- DBG ("received %d buffers. %u/%u alloc/free buffers",
- rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
-
- if (icmpr_buffer_alloc (rx, c->tx_qid) < 0)
- {
- INFO ("buffer_alloc error");
- goto error;
- }
- int i;
- for (i = 0; i < rx; i++)
- {
- 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);
- }
-
- /* mark memif buffers and shared memory buffers as free */
- err = memif_refill_queue (c->conn, qid, rx, 0);
- /*
- * In this example we can assert that c->conn points to valid connection
- * and 'rx <= c->rx_buf_num'.
- */
- c->rx_buf_num -= rx;
-
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
-
- icmpr_tx_burst (c->tx_qid);
-
- 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",
- rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
- return 0;
-}
-
-int
-icmpr_memif_create (int is_master)
-{
- /* setting memif connection arguments */
- memif_conn_args_t args;
- memset (&args, 0, sizeof (args));
- args.is_master = is_master;
- args.log2_ring_size = 10;
- args.buffer_size = 2048;
- args.num_s2m_rings = 2;
- args.num_m2s_rings = 2;
- strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
- args.mode = 0;
- /* socket filename is not specified, because this app is supposed to
- connect to VPP over memif. so default socket filename will be used */
- /* default socketfile = /run/vpp/memif.sock */
-
- args.interface_id = 0;
- /* last argument for memif_create (void * private_ctx) is used by user
- to identify connection. this context is returned with callbacks */
- int err = memif_create (&(&memif_connection)->conn,
- &args, on_connect, on_disconnect, on_interrupt,
- NULL);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_create: %s", memif_strerror (err));
- return 0;
-}
-
-int
-main (int argc, char *argv[])
-{
- memif_connection_t *c = &memif_connection;
-
- signal (SIGINT, icmpr_exit);
-
- /* initialize global memif connection handle */
- c->conn = NULL;
- if (argc == 1)
- c->tx_qid = 0;
- else
- {
- char *end;
- c->tx_qid = strtol (argv[1], &end, 10);
- }
- INFO ("tx qid: %u", c->tx_qid);
- /* alloc memif buffers */
- 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] = 1;
- c->ip_addr[3] = 2;
- /* initialize memory interface */
- int err;
- /* 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, NULL);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_init: %s", memif_strerror (err));
-
- print_help ();
-
- icmpr_memif_create (0);
- print_memif_details ();
-
- /* main loop */
- while (1)
- {
- if (memif_poll_event (-1) < 0)
- {
- DBG ("poll_event error!");
- }
- }
-}