mbuf: use offset macro
[dpdk.git] / examples / l3fwd / main.c
index 7871038..5c22ed1 100644 (file)
@@ -72,6 +72,9 @@
 #include <rte_udp.h>
 #include <rte_string_fns.h>
 
+#include <cmdline_parse.h>
+#include <cmdline_parse_etheraddr.h>
+
 #define APP_LOOKUP_EXACT_MATCH          0
 #define APP_LOOKUP_LPM                  1
 #define DO_RFC_1812_CHECKS
 
 #define MEMPOOL_CACHE_SIZE 256
 
-#define MBUF_DATA_SIZE (2048 + RTE_PKTMBUF_HEADROOM)
-
 /*
  * This expression is used to calculate the number of mbufs needed depending on user input, taking
  *  into account memory for rx and tx hardware rings, cache per lcore and mtable per port per lcore.
@@ -161,6 +162,7 @@ static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
 
 /* ethernet addresses of ports */
+static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
 
 static __m128i val_eth[RTE_MAX_ETHPORTS];
@@ -740,7 +742,6 @@ simple_ipv4_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *
 {
        struct ether_hdr *eth_hdr[4];
        struct ipv4_hdr *ipv4_hdr[4];
-       void *d_addr_bytes[4];
        uint8_t dst_port[4];
        int32_t ret[4];
        union ipv4_5tuple_host key[4];
@@ -752,14 +753,14 @@ simple_ipv4_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *
        eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
 
        /* Handle IPv4 headers.*/
-       ipv4_hdr[0] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[0], unsigned char *) +
-                       sizeof(struct ether_hdr));
-       ipv4_hdr[1] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[1], unsigned char *) +
-                       sizeof(struct ether_hdr));
-       ipv4_hdr[2] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[2], unsigned char *) +
-                       sizeof(struct ether_hdr));
-       ipv4_hdr[3] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[3], unsigned char *) +
-                       sizeof(struct ether_hdr));
+       ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
+                                             sizeof(struct ether_hdr));
+       ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
+                                             sizeof(struct ether_hdr));
+       ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
+                                             sizeof(struct ether_hdr));
+       ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
+                                             sizeof(struct ether_hdr));
 
 #ifdef DO_RFC_1812_CHECKS
        /* Check to make sure the packet is valid (RFC1812) */
@@ -795,14 +796,10 @@ simple_ipv4_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *
        }
 #endif // End of #ifdef DO_RFC_1812_CHECKS
 
-       data[0] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[0], unsigned char *) +
-               sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
-       data[1] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[1], unsigned char *) +
-               sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
-       data[2] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[2], unsigned char *) +
-               sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
-       data[3] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[3], unsigned char *) +
-               sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
+       data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
+       data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
+       data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
+       data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
 
        key[0].xmm = _mm_and_si128(data[0], mask0);
        key[1].xmm = _mm_and_si128(data[1], mask0);
@@ -825,16 +822,6 @@ simple_ipv4_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *
        if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
                dst_port[3] = portid;
 
-       /* 02:00:00:00:00:xx */
-       d_addr_bytes[0] = &eth_hdr[0]->d_addr.addr_bytes[0];
-       d_addr_bytes[1] = &eth_hdr[1]->d_addr.addr_bytes[0];
-       d_addr_bytes[2] = &eth_hdr[2]->d_addr.addr_bytes[0];
-       d_addr_bytes[3] = &eth_hdr[3]->d_addr.addr_bytes[0];
-       *((uint64_t *)d_addr_bytes[0]) = 0x000000000002 + ((uint64_t)dst_port[0] << 40);
-       *((uint64_t *)d_addr_bytes[1]) = 0x000000000002 + ((uint64_t)dst_port[1] << 40);
-       *((uint64_t *)d_addr_bytes[2]) = 0x000000000002 + ((uint64_t)dst_port[2] << 40);
-       *((uint64_t *)d_addr_bytes[3]) = 0x000000000002 + ((uint64_t)dst_port[3] << 40);
-
 #ifdef DO_RFC_1812_CHECKS
        /* Update time to live and header checksum */
        --(ipv4_hdr[0]->time_to_live);
@@ -847,6 +834,12 @@ simple_ipv4_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *
        ++(ipv4_hdr[3]->hdr_checksum);
 #endif
 
+       /* dst addr */
+       *(uint64_t *)&eth_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
+       *(uint64_t *)&eth_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
+       *(uint64_t *)&eth_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
+       *(uint64_t *)&eth_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
+
        /* src addr */
        ether_addr_copy(&ports_eth_addr[dst_port[0]], &eth_hdr[0]->s_addr);
        ether_addr_copy(&ports_eth_addr[dst_port[1]], &eth_hdr[1]->s_addr);
@@ -863,14 +856,9 @@ simple_ipv4_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *
 static inline void get_ipv6_5tuple(struct rte_mbuf* m0, __m128i mask0, __m128i mask1,
                                 union ipv6_5tuple_host * key)
 {
-        __m128i tmpdata0 = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m0, unsigned char *)
-                       + sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)));
-        __m128i tmpdata1 = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m0, unsigned char *)
-                       + sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)
-                       +  sizeof(__m128i)));
-        __m128i tmpdata2 = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m0, unsigned char *)
-                       + sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)
-                       + sizeof(__m128i) + sizeof(__m128i)));
+        __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0, __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)));
+        __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0, __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
+        __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0, __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) + sizeof(__m128i)));
         key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
         key->xmm[1] = tmpdata1;
         key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
@@ -882,7 +870,6 @@ simple_ipv6_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *
 {
        struct ether_hdr *eth_hdr[4];
        __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[4];
-       void *d_addr_bytes[4];
        uint8_t dst_port[4];
        int32_t ret[4];
        union ipv6_5tuple_host key[4];
@@ -893,14 +880,14 @@ simple_ipv6_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *
        eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
 
        /* Handle IPv6 headers.*/
-       ipv6_hdr[0] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[0], unsigned char *) +
-                       sizeof(struct ether_hdr));
-       ipv6_hdr[1] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[1], unsigned char *) +
-                       sizeof(struct ether_hdr));
-       ipv6_hdr[2] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[2], unsigned char *) +
-                       sizeof(struct ether_hdr));
-       ipv6_hdr[3] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[3], unsigned char *) +
-                       sizeof(struct ether_hdr));
+       ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
+                                             sizeof(struct ether_hdr));
+       ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
+                                             sizeof(struct ether_hdr));
+       ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
+                                             sizeof(struct ether_hdr));
+       ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
+                                             sizeof(struct ether_hdr));
 
        get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
        get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
@@ -923,15 +910,11 @@ simple_ipv6_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *
        if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
                dst_port[3] = portid;
 
-       /* 02:00:00:00:00:xx */
-       d_addr_bytes[0] = &eth_hdr[0]->d_addr.addr_bytes[0];
-       d_addr_bytes[1] = &eth_hdr[1]->d_addr.addr_bytes[0];
-       d_addr_bytes[2] = &eth_hdr[2]->d_addr.addr_bytes[0];
-       d_addr_bytes[3] = &eth_hdr[3]->d_addr.addr_bytes[0];
-       *((uint64_t *)d_addr_bytes[0]) = 0x000000000002 + ((uint64_t)dst_port[0] << 40);
-       *((uint64_t *)d_addr_bytes[1]) = 0x000000000002 + ((uint64_t)dst_port[1] << 40);
-       *((uint64_t *)d_addr_bytes[2]) = 0x000000000002 + ((uint64_t)dst_port[2] << 40);
-       *((uint64_t *)d_addr_bytes[3]) = 0x000000000002 + ((uint64_t)dst_port[3] << 40);
+       /* dst addr */
+       *(uint64_t *)&eth_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
+       *(uint64_t *)&eth_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
+       *(uint64_t *)&eth_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
+       *(uint64_t *)&eth_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
 
        /* src addr */
        ether_addr_copy(&ports_eth_addr[dst_port[0]], &eth_hdr[0]->s_addr);
@@ -952,15 +935,14 @@ l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, struct lcore_conf *qcon
 {
        struct ether_hdr *eth_hdr;
        struct ipv4_hdr *ipv4_hdr;
-       void *d_addr_bytes;
        uint8_t dst_port;
 
        eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
 
        if (m->ol_flags & PKT_RX_IPV4_HDR) {
                /* Handle IPv4 headers.*/
-               ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, unsigned char *) +
-                               sizeof(struct ether_hdr));
+               ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
+                                                  sizeof(struct ether_hdr));
 
 #ifdef DO_RFC_1812_CHECKS
                /* Check to make sure the packet is valid (RFC1812) */
@@ -976,16 +958,13 @@ l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, struct lcore_conf *qcon
                                (enabled_port_mask & 1 << dst_port) == 0)
                        dst_port = portid;
 
-               /* 02:00:00:00:00:xx */
-               d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
-               *((uint64_t *)d_addr_bytes) = ETHER_LOCAL_ADMIN_ADDR +
-                       ((uint64_t)dst_port << 40);
-
 #ifdef DO_RFC_1812_CHECKS
                /* Update time to live and header checksum */
                --(ipv4_hdr->time_to_live);
                ++(ipv4_hdr->hdr_checksum);
 #endif
+               /* dst addr */
+               *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
 
                /* src addr */
                ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
@@ -996,18 +975,16 @@ l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, struct lcore_conf *qcon
                /* Handle IPv6 headers.*/
                struct ipv6_hdr *ipv6_hdr;
 
-               ipv6_hdr = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m, unsigned char *) +
-                               sizeof(struct ether_hdr));
+               ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
+                                                  sizeof(struct ether_hdr));
 
                dst_port = get_ipv6_dst_port(ipv6_hdr, portid, qconf->ipv6_lookup_struct);
 
                if (dst_port >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
                        dst_port = portid;
 
-               /* 02:00:00:00:00:xx */
-               d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
-               *((uint64_t *)d_addr_bytes) = ETHER_LOCAL_ADMIN_ADDR +
-                       ((uint64_t)dst_port << 40);
+               /* dst addr */
+               *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
 
                /* src addr */
                ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
@@ -1188,10 +1165,10 @@ processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
        __m128i ve[FWDSTEP];
        __m128i *p[FWDSTEP];
 
-       p[0] = (rte_pktmbuf_mtod(pkt[0], __m128i *));
-       p[1] = (rte_pktmbuf_mtod(pkt[1], __m128i *));
-       p[2] = (rte_pktmbuf_mtod(pkt[2], __m128i *));
-       p[3] = (rte_pktmbuf_mtod(pkt[3], __m128i *));
+       p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
+       p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
+       p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
+       p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
 
        ve[0] = val_eth[dst_port[0]];
        te[0] = _mm_load_si128(p[0]);
@@ -1744,6 +1721,7 @@ print_usage(const char *prgname)
                "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
                "  -P : enable promiscuous mode\n"
                "  --config (port,queue,lcore): rx queues configuration\n"
+               "  --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
                "  --no-numa: optional, disable numa awareness\n"
                "  --ipv6: optional, specify it if running ipv6 packets\n"
                "  --enable-jumbo: enable jumbo frame"
@@ -1854,7 +1832,36 @@ parse_config(const char *q_arg)
        return 0;
 }
 
+static void
+parse_eth_dest(const char *optarg)
+{
+       uint8_t portid;
+       char *port_end;
+       uint8_t c, *dest, peer_addr[6];
+
+       errno = 0;
+       portid = strtoul(optarg, &port_end, 10);
+       if (errno != 0 || port_end == optarg || *port_end++ != ',')
+               rte_exit(EXIT_FAILURE,
+               "Invalid eth-dest: %s", optarg);
+       if (portid >= RTE_MAX_ETHPORTS)
+               rte_exit(EXIT_FAILURE,
+               "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
+               portid, RTE_MAX_ETHPORTS);
+
+       if (cmdline_parse_etheraddr(NULL, port_end,
+               &peer_addr, sizeof(peer_addr)) < 0)
+               rte_exit(EXIT_FAILURE,
+               "Invalid ethernet address: %s\n",
+               port_end);
+       dest = (uint8_t *)&dest_eth_addr[portid];
+       for (c = 0; c < 6; c++)
+               dest[c] = peer_addr[c];
+       *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
+}
+
 #define CMD_LINE_OPT_CONFIG "config"
+#define CMD_LINE_OPT_ETH_DEST "eth-dest"
 #define CMD_LINE_OPT_NO_NUMA "no-numa"
 #define CMD_LINE_OPT_IPV6 "ipv6"
 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
@@ -1870,6 +1877,7 @@ parse_args(int argc, char **argv)
        char *prgname = argv[0];
        static struct option lgopts[] = {
                {CMD_LINE_OPT_CONFIG, 1, 0, 0},
+               {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
                {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
                {CMD_LINE_OPT_IPV6, 0, 0, 0},
                {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
@@ -1909,6 +1917,11 @@ parse_args(int argc, char **argv)
                                }
                        }
 
+                       if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
+                               sizeof(CMD_LINE_OPT_CONFIG))) {
+                                       parse_eth_dest(optarg);
+                       }
+
                        if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
                                sizeof(CMD_LINE_OPT_NO_NUMA))) {
                                printf("numa is disabled \n");
@@ -2316,8 +2329,8 @@ init_mem(unsigned nb_mbuf)
                        snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
                        pktmbuf_pool[socketid] =
                                rte_pktmbuf_pool_create(s, nb_mbuf,
-                                       MEMPOOL_CACHE_SIZE, 0, MBUF_DATA_SIZE,
-                                       socketid);
+                                       MEMPOOL_CACHE_SIZE, 0,
+                                       RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
                        if (pktmbuf_pool[socketid] == NULL)
                                rte_exit(EXIT_FAILURE,
                                                "Cannot init mbuf pool on socket %d\n", socketid);
@@ -2412,6 +2425,12 @@ main(int argc, char **argv)
        argc -= ret;
        argv += ret;
 
+       /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
+       for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
+               dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
+               *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
+       }
+
        /* parse application arguments (after the EAL ones) */
        ret = parse_args(argc, argv);
        if (ret < 0)
@@ -2460,12 +2479,13 @@ main(int argc, char **argv)
                rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
                print_ethaddr(" Address:", &ports_eth_addr[portid]);
                printf(", ");
+               print_ethaddr("Destination:",
+                       (const struct ether_addr *)&dest_eth_addr[portid]);
+               printf(", ");
 
                /*
-                * prepare dst and src MACs for each port.
+                * prepare src MACs for each port.
                 */
-               *(uint64_t *)(val_eth + portid) =
-                       ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
                ether_addr_copy(&ports_eth_addr[portid],
                        (struct ether_addr *)(val_eth + portid) + 1);