fix typos using codespell utility
[dpdk.git] / examples / performance-thread / l3fwd-thread / main.c
index 61c023e..8237ac6 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  *
- *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
  *   All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
 #define APP_LOOKUP_METHOD             APP_LOOKUP_LPM
 #endif
 
+#ifndef __GLIBC__ /* sched_getcpu() is glibc specific */
+#define sched_getcpu() rte_lcore_id()
+#endif
+
+static int
+check_ptype(int portid)
+{
+       int i, ret;
+       int ipv4 = 0, ipv6 = 0;
+
+       ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK, NULL,
+                       0);
+       if (ret <= 0)
+               return 0;
+
+       uint32_t ptypes[ret];
+
+       ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK,
+                       ptypes, ret);
+       for (i = 0; i < ret; ++i) {
+               if (ptypes[i] & RTE_PTYPE_L3_IPV4)
+                       ipv4 = 1;
+               if (ptypes[i] & RTE_PTYPE_L3_IPV6)
+                       ipv6 = 1;
+       }
+
+       if (ipv4 && ipv6)
+               return 1;
+
+       return 0;
+}
+
+static inline void
+parse_ptype(struct rte_mbuf *m)
+{
+       struct ether_hdr *eth_hdr;
+       uint32_t packet_type = RTE_PTYPE_UNKNOWN;
+       uint16_t ether_type;
+
+       eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
+       ether_type = eth_hdr->ether_type;
+       if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
+               packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
+       else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
+               packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
+
+       m->packet_type = packet_type;
+}
+
+static uint16_t
+cb_parse_ptype(__rte_unused uint8_t port, __rte_unused uint16_t queue,
+               struct rte_mbuf *pkts[], uint16_t nb_pkts,
+               __rte_unused uint16_t max_pkts, __rte_unused void *user_param)
+{
+       unsigned int i;
+
+       for (i = 0; i < nb_pkts; i++)
+               parse_ptype(pkts[i]);
+
+       return nb_pkts;
+}
+
 /*
  *  When set to zero, simple forwaring path is eanbled.
  *  When set to one, optimized forwarding path is enabled.
@@ -170,8 +232,9 @@ static __m128i val_eth[RTE_MAX_ETHPORTS];
 
 /* mask of enabled ports */
 static uint32_t enabled_port_mask;
-static int promiscuous_on; /**< $et in promiscuous mode off by default. */
+static int promiscuous_on; /**< Set in promiscuous mode off by default. */
 static int numa_on = 1;    /**< NUMA is enabled by default. */
+static int parse_ptype_on;
 
 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
 static int ipv6;           /**< ipv6 is false by default. */
@@ -282,7 +345,7 @@ static struct rte_eth_conf port_conf = {
                .hw_ip_checksum = 1, /**< IP checksum offload enabled */
                .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 */
        },
        .rx_adv_conf = {
                .rss_conf = {
@@ -657,7 +720,7 @@ send_single_packet(struct rte_mbuf *m, uint8_t port)
 
 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
        (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
-static inline __attribute__((always_inline)) void
+static __rte_always_inline void
 send_packetsx4(uint8_t port,
        struct rte_mbuf *m[], uint32_t num)
 {
@@ -698,12 +761,15 @@ send_packetsx4(uint8_t port,
        case 0:
                qconf->tx_mbufs[port].m_table[len + j] = m[j];
                j++;
+               /* fall-through */
        case 3:
                qconf->tx_mbufs[port].m_table[len + j] = m[j];
                j++;
+               /* fall-through */
        case 2:
                qconf->tx_mbufs[port].m_table[len + j] = m[j];
                j++;
+               /* fall-through */
        case 1:
                qconf->tx_mbufs[port].m_table[len + j] = m[j];
                j++;
@@ -725,12 +791,15 @@ send_packetsx4(uint8_t port,
                case 0:
                        qconf->tx_mbufs[port].m_table[j] = m[n + j];
                        j++;
+                       /* fall-through */
                case 3:
                        qconf->tx_mbufs[port].m_table[j] = m[n + j];
                        j++;
+                       /* fall-through */
                case 2:
                        qconf->tx_mbufs[port].m_table[j] = m[n + j];
                        j++;
+                       /* fall-through */
                case 1:
                        qconf->tx_mbufs[port].m_table[j] = m[n + j];
                        j++;
@@ -850,7 +919,7 @@ static inline uint8_t
 get_ipv6_dst_port(void *ipv6_hdr,  uint8_t portid,
                lookup6_struct_t *ipv6_l3fwd_lookup_struct)
 {
-       uint8_t next_hop;
+       uint32_t next_hop;
 
        return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
                        ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
@@ -996,7 +1065,7 @@ simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
        const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
                        &key[4], &key[5], &key[6], &key[7]};
 
-       rte_hash_lookup_multi(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
+       rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
                        &key_array[0], 8, ret);
        dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
        dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
@@ -1150,7 +1219,7 @@ simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
        const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
                        &key[4], &key[5], &key[6], &key[7]};
 
-       rte_hash_lookup_multi(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
+       rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
                        &key_array[0], 4, ret);
        dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
        dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
@@ -1218,7 +1287,7 @@ simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
 }
 #endif /* APP_LOOKUP_METHOD */
 
-static inline __attribute__((always_inline)) void
+static __rte_always_inline void
 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
 {
        struct ether_hdr *eth_hdr;
@@ -1306,8 +1375,8 @@ l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
  * If we encounter invalid IPV4 packet, then set destination port for it
  * to BAD_PORT value.
  */
-static inline __attribute__((always_inline)) void
-rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint32_t *dp, uint32_t ptype)
+static __rte_always_inline void
+rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
 {
        uint8_t ihl;
 
@@ -1326,7 +1395,7 @@ rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint32_t *dp, uint32_t ptype)
 }
 
 #else
-#define        rfc1812_process(mb, dp) do { } while (0)
+#define        rfc1812_process(mb, dp, ptype)  do { } while (0)
 #endif /* DO_RFC_1812_CHECKS */
 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
 
@@ -1334,37 +1403,35 @@ rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint32_t *dp, uint32_t ptype)
 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
        (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
 
-static inline __attribute__((always_inline)) uint16_t
+static __rte_always_inline uint16_t
 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint8_t portid)
 {
-       uint32_t next_hop_ipv4;
-       uint8_t next_hop_ipv6;
+       uint32_t next_hop;
        struct ipv6_hdr *ipv6_hdr;
        struct ether_hdr *eth_hdr;
 
        if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
-               if (rte_lpm_lookup(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
-                               dst_ipv4, &next_hop_ipv4) != 0) {
-                       next_hop_ipv4 = portid;
-                       return next_hop_ipv4;
-               }
+               return (uint16_t) ((rte_lpm_lookup(
+                               RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
+                               &next_hop) == 0) ? next_hop : portid);
+
        } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
+
                eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
                ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
-               if (rte_lpm6_lookup(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
-                               ipv6_hdr->dst_addr, &next_hop_ipv6) != 0) {
-                       next_hop_ipv6 = portid;
-                       return next_hop_ipv6;
-               }
-       } else {
-               next_hop_ipv4 = portid;
-               return next_hop_ipv4;
+
+               return (uint16_t) ((rte_lpm6_lookup(
+                               RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
+                               ipv6_hdr->dst_addr, &next_hop) == 0) ?
+                               next_hop : portid);
+
        }
 
+       return portid;
 }
 
 static inline void
-process_packet(struct rte_mbuf *pkt, uint32_t *dst_port, uint8_t portid)
+process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint8_t portid)
 {
        struct ether_hdr *eth_hdr;
        struct ipv4_hdr *ipv4_hdr;
@@ -1431,9 +1498,9 @@ processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
 static inline void
 processx4_step2(__m128i dip,
                uint32_t ipv4_flag,
-               uint32_t portid,
+               uint8_t portid,
                struct rte_mbuf *pkt[FWDSTEP],
-               uint32_t dprt[FWDSTEP])
+               uint16_t dprt[FWDSTEP])
 {
        rte_xmm_t dst;
        const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
@@ -1445,7 +1512,11 @@ processx4_step2(__m128i dip,
        /* if all 4 packets are IPV4. */
        if (likely(ipv4_flag)) {
                rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
-                               dprt, portid);
+                               dst.u32, portid);
+
+               /* get rid of unused upper 16 bit for each dport. */
+               dst.x = _mm_packs_epi32(dst.x, dst.x);
+               *(uint64_t *)dprt = dst.u64[0];
        } else {
                dst.x = dip;
                dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
@@ -1460,7 +1531,7 @@ processx4_step2(__m128i dip,
  * Perform RFC1812 checks and updates for IPV4 packets.
  */
 static inline void
-processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint32_t dst_port[FWDSTEP])
+processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
 {
        __m128i te[FWDSTEP];
        __m128i ve[FWDSTEP];
@@ -1533,7 +1604,7 @@ processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint32_t dst_port[FWDSTEP])
  * Suppose we have array of destionation ports:
  * dst_port[] = {a, b, c, d,, e, ... }
  * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
- * We doing 4 comparisions at once and the result is 4 bit mask.
+ * We doing 4 comparisons at once and the result is 4 bit mask.
  * This mask is used as an index into prebuild array of pnum values.
  */
 static inline uint16_t *
@@ -1658,9 +1729,9 @@ port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
 
        /* if dest port value has changed. */
        if (v != GRPMSK) {
-               lp = pnum->u16 + gptbl[v].idx;
-               lp[0] = 1;
                pnum->u64 = gptbl[v].pnum;
+               pnum->u16[FWDSTEP] = 1;
+               lp = pnum->u16 + gptbl[v].idx;
        }
 
        return lp;
@@ -1679,7 +1750,7 @@ process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
        int32_t k;
        uint16_t dlp;
        uint16_t *lp;
-       uint32_t dst_port[MAX_PKT_BURST];
+       uint16_t dst_port[MAX_PKT_BURST];
        __m128i dip[MAX_PKT_BURST / FWDSTEP];
        uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
        uint16_t pnum[MAX_PKT_BURST + 1];
@@ -1795,10 +1866,12 @@ process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
                process_packet(pkts_burst[j], dst_port + j, portid);
                GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
                j++;
+               /* fall-through */
        case 2:
                process_packet(pkts_burst[j], dst_port + j, portid);
                GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
                j++;
+               /* fall-through */
        case 1:
                process_packet(pkts_burst[j], dst_port + j, portid);
                GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
@@ -2016,7 +2089,7 @@ lthread_tx_per_ring(void *dummy)
                 */
                SET_CPU_BUSY(tx_conf, CPU_POLL);
                nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
-                               MAX_PKT_BURST);
+                               MAX_PKT_BURST, NULL);
                SET_CPU_IDLE(tx_conf, CPU_POLL);
 
                if (nb_rx > 0) {
@@ -2152,7 +2225,7 @@ lthread_rx(void *dummy)
                                ret = rte_ring_sp_enqueue_burst(
                                                rx_conf->ring[worker_id],
                                                (void **) pkts_burst,
-                                               nb_rx);
+                                               nb_rx, NULL);
 
                                new_len = old_len + ret;
 
@@ -2320,7 +2393,7 @@ pthread_tx(void *dummy)
                 */
                SET_CPU_BUSY(tx_conf, CPU_POLL);
                nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
-                               (void **)pkts_burst, MAX_PKT_BURST);
+                               (void **)pkts_burst, MAX_PKT_BURST, NULL);
                SET_CPU_IDLE(tx_conf, CPU_POLL);
 
                if (unlikely(nb_rx == 0)) {
@@ -2392,7 +2465,7 @@ pthread_rx(void *dummy)
                        SET_CPU_BUSY(rx_conf, CPU_PROCESS);
                        worker_id = (worker_id + 1) % rx_conf->n_ring;
                        n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
-                                       (void **)pkts_burst, nb_rx);
+                                       (void **)pkts_burst, nb_rx, NULL);
 
                        if (unlikely(n != nb_rx)) {
                                uint32_t k;
@@ -2607,6 +2680,7 @@ print_usage(const char *prgname)
                "  [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
                "  [--tx (lcore,thread)[,(lcore,thread]]"
                "  [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
+               "  [--parse-ptype]\n\n"
                "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
                "  -P : enable promiscuous mode\n"
                "  --rx (port,queue,lcore,thread): rx queues configuration\n"
@@ -2618,7 +2692,8 @@ print_usage(const char *prgname)
                "  --enable-jumbo: enable jumbo frame"
                " which max packet len is PKTLEN in decimal (64-9600)\n"
                "  --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
-               "  --no-lthreads: turn off lthread model\n",
+               "  --no-lthreads: turn off lthread model\n"
+               "  --parse-ptype: set to use software to analyze packet type\n\n",
                prgname);
 }
 
@@ -2837,6 +2912,7 @@ parse_eth_dest(const char *optarg)
 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
+#define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
 
 /* Parse the argument given in the command line of the application */
 static int
@@ -2856,6 +2932,7 @@ parse_args(int argc, char **argv)
                {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
                {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
                {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
+               {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
                {NULL, 0, 0, 0}
        };
 
@@ -2932,6 +3009,12 @@ parse_args(int argc, char **argv)
                                lthreads_on = 0;
                        }
 
+                       if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_PARSE_PTYPE,
+                                       sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
+                               printf("software packet type parsing enabled\n");
+                               parse_ptype_on = 1;
+                       }
+
                        if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
                                sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
                                struct option lenopts = {"max-pkt-len", required_argument, 0,
@@ -2980,7 +3063,7 @@ parse_args(int argc, char **argv)
                argv[optind-1] = prgname;
 
        ret = optind-1;
-       optind = 0; /* reset getopt lib */
+       optind = 1; /* reset getopt lib */
        return ret;
 }
 
@@ -3408,7 +3491,7 @@ check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
                                continue;
                        }
                        /* clear all_ports_up flag if any link down */
-                       if (link.link_status == 0) {
+                       if (link.link_status == ETH_LINK_DOWN) {
                                all_ports_up = 0;
                                break;
                        }
@@ -3482,8 +3565,6 @@ main(int argc, char **argv)
                rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
 
        nb_ports = rte_eth_dev_count();
-       if (nb_ports > RTE_MAX_ETHPORTS)
-               nb_ports = RTE_MAX_ETHPORTS;
 
        if (check_port_config(nb_ports) < 0)
                rte_exit(EXIT_FAILURE, "check_port_config failed\n");
@@ -3622,6 +3703,31 @@ main(int argc, char **argv)
                        rte_eth_promiscuous_enable(portid);
        }
 
+       for (i = 0; i < n_rx_thread; i++) {
+               lcore_id = rx_thread[i].conf.lcore_id;
+               if (rte_lcore_is_enabled(lcore_id) == 0)
+                       continue;
+
+               /* check if hw packet type is supported */
+               for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
+                       portid = rx_thread[i].rx_queue_list[queue].port_id;
+                       queueid = rx_thread[i].rx_queue_list[queue].queue_id;
+
+                       if (parse_ptype_on) {
+                               if (!rte_eth_add_rx_callback(portid, queueid,
+                                               cb_parse_ptype, NULL))
+                                       rte_exit(EXIT_FAILURE,
+                                               "Failed to add rx callback: "
+                                               "port=%d\n", portid);
+                       } else if (!check_ptype(portid))
+                               rte_exit(EXIT_FAILURE,
+                                       "Port %d cannot parse packet type.\n\n"
+                                       "Please add --parse-ptype to use sw "
+                                       "packet type analyzer.\n\n",
+                                       portid);
+               }
+       }
+
        check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
 
        if (lthreads_on) {