Note fuzz refresh in d/changelog
[deb_dpdk.git] / examples / l2fwd / main.c
index 1ad9488..94287df 100644 (file)
 #include <rte_debug.h>
 #include <rte_ether.h>
 #include <rte_ethdev.h>
-#include <rte_ring.h>
 #include <rte_mempool.h>
 #include <rte_mbuf.h>
 
 static volatile bool force_quit;
 
+/* MAC updating enabled by default */
+static int mac_updating = 1;
+
 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
 
 #define NB_MBUF   8192
 
 #define MAX_PKT_BURST 32
 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
+#define MEMPOOL_CACHE_SIZE 256
 
 /*
  * Configurable number of RX/TX ring descriptors
@@ -117,7 +120,7 @@ static const struct rte_eth_conf port_conf = {
                .hw_ip_checksum = 0, /**< IP checksum offload disabled */
                .hw_vlan_filter = 0, /**< VLAN filtering disabled */
                .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
-               .hw_strip_crc   = 0, /**< CRC stripped by hardware */
+               .hw_strip_crc   = 1, /**< CRC stripped by hardware */
        },
        .txmode = {
                .mq_mode = ETH_MQ_TX_NONE,
@@ -134,10 +137,9 @@ struct l2fwd_port_statistics {
 } __rte_cache_aligned;
 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
 
-/* A tsc-based timer responsible for triggering statistics printout */
-#define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
-static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* default period is 10 seconds */
+/* A tsc-based timer responsible for triggering statistics printout */
+static uint64_t timer_period = 10; /* default period is 10 seconds */
 
 /* Print out statistics on packets dropped */
 static void
@@ -186,23 +188,32 @@ print_stats(void)
 }
 
 static void
-l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
+l2fwd_mac_updating(struct rte_mbuf *m, unsigned dest_portid)
 {
        struct ether_hdr *eth;
        void *tmp;
-       unsigned dst_port;
-       int sent;
-       struct rte_eth_dev_tx_buffer *buffer;
 
-       dst_port = l2fwd_dst_ports[portid];
        eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
 
        /* 02:00:00:00:00:xx */
        tmp = &eth->d_addr.addr_bytes[0];
-       *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
+       *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
 
        /* src addr */
-       ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->s_addr);
+       ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], &eth->s_addr);
+}
+
+static void
+l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
+{
+       unsigned dst_port;
+       int sent;
+       struct rte_eth_dev_tx_buffer *buffer;
+
+       dst_port = l2fwd_dst_ports[portid];
+
+       if (mac_updating)
+               l2fwd_mac_updating(m, dst_port);
 
        buffer = tx_buffer[dst_port];
        sent = rte_eth_tx_buffer(dst_port, 0, buffer, m);
@@ -274,7 +285,7 @@ l2fwd_main_loop(void)
                                timer_tsc += diff_tsc;
 
                                /* if timer has reached its timeout */
-                               if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
+                               if (unlikely(timer_tsc >= timer_period)) {
 
                                        /* do this only on master core */
                                        if (lcore_id == rte_get_master_lcore()) {
@@ -322,7 +333,11 @@ l2fwd_usage(const char *prgname)
        printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
               "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
               "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
-                  "  -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
+                  "  -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n"
+                  "  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
+                  "      When enabled:\n"
+                  "       - The source MAC address is replaced by the TX port MAC address\n"
+                  "       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
               prgname);
 }
 
@@ -381,11 +396,13 @@ l2fwd_parse_timer_period(const char *q_arg)
 static int
 l2fwd_parse_args(int argc, char **argv)
 {
-       int opt, ret;
+       int opt, ret, timer_secs;
        char **argvopt;
        int option_index;
        char *prgname = argv[0];
        static struct option lgopts[] = {
+               { "mac-updating", no_argument, &mac_updating, 1},
+               { "no-mac-updating", no_argument, &mac_updating, 0},
                {NULL, 0, 0, 0}
        };
 
@@ -417,18 +434,18 @@ l2fwd_parse_args(int argc, char **argv)
 
                /* timer period */
                case 'T':
-                       timer_period = l2fwd_parse_timer_period(optarg) * 1000 * TIMER_MILLISECOND;
-                       if (timer_period < 0) {
+                       timer_secs = l2fwd_parse_timer_period(optarg);
+                       if (timer_secs < 0) {
                                printf("invalid timer period\n");
                                l2fwd_usage(prgname);
                                return -1;
                        }
+                       timer_period = timer_secs;
                        break;
 
                /* long options */
                case 0:
-                       l2fwd_usage(prgname);
-                       return -1;
+                       break;
 
                default:
                        l2fwd_usage(prgname);
@@ -541,9 +558,15 @@ main(int argc, char **argv)
        if (ret < 0)
                rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");
 
+       printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled");
+
+       /* convert to number of cycles */
+       timer_period *= rte_get_timer_hz();
+
        /* create the mbuf pool */
-       l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32,
-               0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
+       l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF,
+               MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
+               rte_socket_id());
        if (l2fwd_pktmbuf_pool == NULL)
                rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
 
@@ -551,9 +574,6 @@ main(int argc, char **argv)
        if (nb_ports == 0)
                rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
 
-       if (nb_ports > RTE_MAX_ETHPORTS)
-               nb_ports = RTE_MAX_ETHPORTS;
-
        /* reset l2fwd_dst_ports */
        for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
                l2fwd_dst_ports[portid] = 0;