diff options
Diffstat (limited to 'extras/libmemif/examples/icmp_responder/main.c')
-rw-r--r-- | extras/libmemif/examples/icmp_responder/main.c | 502 |
1 files changed, 170 insertions, 332 deletions
diff --git a/extras/libmemif/examples/icmp_responder/main.c b/extras/libmemif/examples/icmp_responder/main.c index 517512340e0..d70ecb5647e 100644 --- a/extras/libmemif/examples/icmp_responder/main.c +++ b/extras/libmemif/examples/icmp_responder/main.c @@ -1,6 +1,6 @@ /* *------------------------------------------------------------------ - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2020 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: @@ -16,187 +16,71 @@ */ #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 <getopt.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) +#include <common.h> -/* maximum tx/rx memif buffers */ -#define MAX_MEMIF_BUFS 256 +#define APP_NAME "icmp_responder_example" -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; +#define IF_NAME "libmemif0" +#define IF_ID 0 +#define SOCKET_PATH "/run/vpp/memif.sock" +const uint8_t HW_ADDR[6] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; +const uint8_t IP_ADDR[4] = { 192, 168, 1, 1 }; -memif_connection_t memif_connection; +memif_connection_t intf; int epfd; -static void -print_memif_details () +/* informs user about connected status. private_ctx is used by user to identify + * connection */ +int +on_connect (memif_conn_handle_t conn, void *private_ctx) { - memif_connection_t *c = &memif_connection; - printf ("MEMIF DETAILS\n"); - printf ("==============================\n"); + INFO ("memif connected!"); + int err; + memif_connection_t *c = (memif_connection_t *) private_ctx; - memif_details_t md; - memset (&md, 0, sizeof (md)); - ssize_t buflen = 2048; - char *buf = malloc (buflen); - memset (buf, 0, buflen); - int err, e; + c->is_connected = 1; + alloc_memif_buffers (c); - err = memif_get_details (c->conn, &md, buf, buflen); + err = memif_refill_queue (conn, 0, -1, c->headroom_size); if (err != MEMIF_ERR_SUCCESS) { - INFO ("%s", memif_strerror (err)); - if (err == MEMIF_ERR_NOCONN) - { - free (buf); - return; - } + INFO ("memif_refill_queue: %s", memif_strerror (err)); + return err; } - 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); -} + print_memif_details (c); -/* 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) */ +/* informs user about disconnected status. private_ctx is used by user to + * identify connection */ 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); + memif_connection_t *c = (memif_connection_t *) private_ctx; + + c->is_connected = 0; + free_memif_buffers (c); + + /* stop event polling thread */ + int err = memif_cancel_poll_event (memif_get_socket_handle (conn)); if (err != MEMIF_ERR_SUCCESS) - INFO ("memif_delete: %s", memif_strerror (err)); + INFO ("We are doomed..."); + return 0; } @@ -209,209 +93,163 @@ print_help () #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"); + print_version (); + printf ("==============================\n"); + printf ( + "In this example, memif endpoint connects to an external application.\n"); + printf ( + "The example application can resolve ARP and reply to ICMPv4 packets.\n"); + printf ("The program will exit once the interface is disconnected.\n"); + printf ("==============================\n"); + printf ("Usage: icmp_responder [OPTIONS]\n\n"); + printf ("Options:\n"); + printf ("\t-r\tInterface role <slave|master>. Default: slave\n"); + printf ("\t-s\tSocket path. Supports abstract socket using @ before the " + "path. Default: /run/vpp/memif.sock\n"); + printf ("\t-b\tBuffer Size. Default: 2048\n"); + printf ("\t-h\tHeadroom Size. Default: 0\n"); + printf ("\t-i\tInterface id. Default: 0\n"); + printf ("\t-a\tIPv4 address. Default: 192.168.1.1\n"); + printf ("\t-m\tMac address. Default: aa:aa:aa:aa:aa:aa\n"); + printf ("\t-?\tShow help and exit.\n"); + printf ("\t-v\tShow libmemif and memif version information and exit.\n"); } int -icmpr_buffer_alloc (long n, uint16_t qid) +main (int argc, char *argv[]) { - 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) + memif_socket_args_t memif_socket_args = { 0 }; + memif_socket_handle_t memif_socket; + memif_conn_args_t memif_conn_args = { 0 }; + int opt, err, ret = 0; + uint8_t is_master = 0; + char socket_path[108]; + int id = IF_ID; + + strncpy (socket_path, SOCKET_PATH, sizeof (SOCKET_PATH)); + + /* prepare the private data */ + memset (&intf, 0, sizeof (intf)); + intf.packet_handler = icmp_packet_handler; + memcpy (intf.ip_addr, IP_ADDR, 4); + memcpy (intf.hw_addr, HW_ADDR, 6); + + while ((opt = getopt (argc, argv, "r:s:b:h:i:a:m:?v")) != -1) { - INFO ("memif_buffer_alloc: %s", memif_strerror (err)); - c->tx_buf_num += r; - return -1; + switch (opt) + { + case 'r': + if (strncmp (optarg, "master", sizeof (optarg)) == 0) + { + is_master = 1; + } + else if (strncmp (optarg, "slave", sizeof (optarg)) == 0) + { + is_master = 0; + } + else + { + INFO ("Invalid role value: '%s'", optarg); + return -1; + } + break; + case 's': + sprintf (socket_path, "%s", optarg); + break; + case 'b': + intf.buffer_size = atoi (optarg); + break; + case 'h': + intf.headroom_size = atoi (optarg); + break; + case 'i': + id = atoi (optarg); + break; + case 'a': + if (parse_ip4 (optarg, intf.ip_addr) != 0) + { + INFO ("Invalid ipv4 address: %s", optarg); + return -1; + } + break; + case 'm': + if (parse_mac (optarg, intf.hw_addr) != 0) + { + INFO ("Invalid mac address: %s", optarg); + return -1; + } + break; + case '?': + print_help (); + return 0; + case 'v': + print_version (); + return 0; + } } - 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; -} + /** Create memif socket + * + * Interfaces are internally stored in a database referenced by memif socket. + */ + sprintf (memif_socket_args.path, "%s", socket_path); + /* Set application name */ + strncpy (memif_socket_args.app_name, APP_NAME, strlen (APP_NAME)); -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; + /* configure autoconnect timer */ + if (is_master == 0) + { + memif_socket_args.connection_request_timer.it_value.tv_sec = 2; + memif_socket_args.connection_request_timer.it_value.tv_nsec = 0; + memif_socket_args.connection_request_timer.it_interval.tv_sec = 2; + memif_socket_args.connection_request_timer.it_interval.tv_nsec = 0; + } - err = memif_cleanup (); + err = memif_create_socket (&memif_socket, &memif_socket_args, NULL); 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"); + INFO ("memif_create_socket: %s", memif_strerror (err)); 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'. + /** Create memif interfaces + * + * Both interaces are assigned the same socket and same id to create a + * loopback. */ - 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; -} + if (intf.buffer_size) + memif_conn_args.buffer_size = intf.buffer_size; + else + intf.buffer_size = 2048; -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 */ + memif_conn_args.socket = memif_socket; + memif_conn_args.interface_id = id; + strncpy (memif_conn_args.interface_name, IF_NAME, + sizeof (memif_conn_args.interface_name)); + memif_conn_args.is_master = is_master; - 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); + err = + memif_create (&intf.conn, &memif_conn_args, on_connect, on_disconnect, + is_master ? responder : responder_zero_copy, (void *) &intf); 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 ("memif_create_socket: %s", memif_strerror (err)); + return err; } - 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) + do { - if (memif_poll_event (-1) < 0) - { - DBG ("poll_event error!"); - } + err = memif_poll_event (memif_socket, -1); } + while (err == MEMIF_ERR_SUCCESS); + + return 0; + +error: + ret = -1; +done: + free_memif_buffers (&intf); + memif_delete (&intf.conn); + memif_delete_socket (&memif_socket); + return ret; } |