examples: add eal cleanup to examples
[dpdk.git] / examples / l3fwd-acl / main.c
index c974a35..a1f457b 100644 (file)
@@ -39,6 +39,9 @@
 #include <rte_string_fns.h>
 #include <rte_acl.h>
 
+#include <cmdline_parse.h>
+#include <cmdline_parse_etheraddr.h>
+
 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
 #define L3FWDACL_DEBUG
 #endif
@@ -81,9 +84,6 @@
 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 struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
-
 /* mask of enabled ports */
 static uint32_t enabled_port_mask;
 static int promiscuous_on; /**< Ports set in promiscuous mode off by default. */
@@ -143,6 +143,43 @@ static struct rte_eth_conf port_conf = {
 
 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
 
+/* ethernet addresses of ports */
+static struct rte_ether_hdr port_l2hdr[RTE_MAX_ETHPORTS];
+
+static const struct {
+       const char *name;
+       enum rte_acl_classify_alg alg;
+} acl_alg[] = {
+       {
+               .name = "scalar",
+               .alg = RTE_ACL_CLASSIFY_SCALAR,
+       },
+       {
+               .name = "sse",
+               .alg = RTE_ACL_CLASSIFY_SSE,
+       },
+       {
+               .name = "avx2",
+               .alg = RTE_ACL_CLASSIFY_AVX2,
+       },
+       {
+               .name = "neon",
+               .alg = RTE_ACL_CLASSIFY_NEON,
+       },
+       {
+               .name = "altivec",
+               .alg = RTE_ACL_CLASSIFY_ALTIVEC,
+       },
+       {
+               .name = "avx512x16",
+               .alg = RTE_ACL_CLASSIFY_AVX512X16,
+       },
+       {
+               .name = "avx512x32",
+               .alg = RTE_ACL_CLASSIFY_AVX512X32,
+       },
+};
+
 /***********************start of ACL part******************************/
 #ifdef DO_RFC_1812_CHECKS
 static inline int
@@ -158,12 +195,24 @@ send_single_packet(struct rte_mbuf *m, uint16_t port);
 #define ACL_LEAD_CHAR          ('@')
 #define ROUTE_LEAD_CHAR                ('R')
 #define COMMENT_LEAD_CHAR      ('#')
-#define OPTION_CONFIG          "config"
-#define OPTION_NONUMA          "no-numa"
-#define OPTION_ENBJMO          "enable-jumbo"
-#define OPTION_RULE_IPV4       "rule_ipv4"
-#define OPTION_RULE_IPV6       "rule_ipv6"
-#define OPTION_SCALAR          "scalar"
+
+enum {
+#define OPT_CONFIG      "config"
+       OPT_CONFIG_NUM = 256,
+#define OPT_NONUMA      "no-numa"
+       OPT_NONUMA_NUM,
+#define OPT_ENBJMO      "enable-jumbo"
+       OPT_ENBJMO_NUM,
+#define OPT_RULE_IPV4   "rule_ipv4"
+       OPT_RULE_IPV4_NUM,
+#define OPT_RULE_IPV6  "rule_ipv6"
+       OPT_RULE_IPV6_NUM,
+#define OPT_ALG         "alg"
+       OPT_ALG_NUM,
+#define OPT_ETH_DEST    "eth-dest"
+       OPT_ETH_DEST_NUM,
+};
+
 #define ACL_DENY_SIGNATURE     0xf0000000
 #define RTE_LOGTYPE_L3FWDACL   RTE_LOGTYPE_USER3
 #define acl_log(format, ...)   RTE_LOG(ERR, L3FWDACL, format, ##__VA_ARGS__)
@@ -437,7 +486,7 @@ static struct {
 static struct{
        const char *rule_ipv4_name;
        const char *rule_ipv6_name;
-       int scalar;
+       enum rte_acl_classify_alg alg;
 } parm_config;
 
 const char cb_port_delim[] = ":";
@@ -1090,13 +1139,58 @@ add_rules(const char *rule_path,
        return 0;
 }
 
+static int
+usage_acl_alg(char *buf, size_t sz)
+{
+       uint32_t i, n, rc, tn;
+
+       n = 0;
+       tn = 0;
+       for (i = 0; i < RTE_DIM(acl_alg); i++) {
+               rc = snprintf(buf + n, sz - n,
+                       i == RTE_DIM(acl_alg) - 1 ? "%s" : "%s|",
+                       acl_alg[i].name);
+               tn += rc;
+               if (rc < sz - n)
+                       n += rc;
+       }
+
+       return tn;
+}
+
+static const char *
+str_acl_alg(enum rte_acl_classify_alg alg)
+{
+       uint32_t i;
+
+       for (i = 0; i != RTE_DIM(acl_alg); i++) {
+               if (alg == acl_alg[i].alg)
+                       return acl_alg[i].name;
+       }
+
+       return "default";
+}
+
+static enum rte_acl_classify_alg
+parse_acl_alg(const char *alg)
+{
+       uint32_t i;
+
+       for (i = 0; i != RTE_DIM(acl_alg); i++) {
+               if (strcmp(alg, acl_alg[i].name) == 0)
+                       return acl_alg[i].alg;
+       }
+
+       return RTE_ACL_CLASSIFY_DEFAULT;
+}
+
 static void
 dump_acl_config(void)
 {
        printf("ACL option are:\n");
-       printf(OPTION_RULE_IPV4": %s\n", parm_config.rule_ipv4_name);
-       printf(OPTION_RULE_IPV6": %s\n", parm_config.rule_ipv6_name);
-       printf(OPTION_SCALAR": %d\n", parm_config.scalar);
+       printf(OPT_RULE_IPV4": %s\n", parm_config.rule_ipv4_name);
+       printf(OPT_RULE_IPV6": %s\n", parm_config.rule_ipv6_name);
+       printf(OPT_ALG": %s\n", str_acl_alg(parm_config.alg));
 }
 
 static int
@@ -1137,8 +1231,8 @@ setup_acl(struct rte_acl_rule *route_base,
        if ((context = rte_acl_create(&acl_param)) == NULL)
                rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
 
-       if (parm_config.scalar && rte_acl_set_ctx_classify(context,
-                       RTE_ACL_CLASSIFY_SCALAR) != 0)
+       if (parm_config.alg != RTE_ACL_CLASSIFY_DEFAULT &&
+                       rte_acl_set_ctx_classify(context, parm_config.alg) != 0)
                rte_exit(EXIT_FAILURE,
                        "Failed to setup classify method for  ACL context\n");
 
@@ -1275,9 +1369,14 @@ send_single_packet(struct rte_mbuf *m, uint16_t port)
 {
        uint32_t lcore_id;
        struct lcore_conf *qconf;
+       struct rte_ether_hdr *eh;
 
        lcore_id = rte_lcore_id();
 
+       /* update src and dst mac*/
+       eh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
+       memcpy(eh, &port_l2hdr[port], sizeof(eh->d_addr) + sizeof(eh->s_addr));
+
        qconf = &lcore_conf[lcore_id];
        rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
                        qconf->tx_buffer[port], m);
@@ -1326,7 +1425,7 @@ is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
 
 /* main processing loop */
 static int
-main_loop(__attribute__((unused)) void *dummy)
+main_loop(__rte_unused void *dummy)
 {
        struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
        unsigned lcore_id;
@@ -1516,29 +1615,32 @@ init_lcore_rx_queues(void)
 static void
 print_usage(const char *prgname)
 {
+       char alg[PATH_MAX];
+
+       usage_acl_alg(alg, sizeof(alg));
        printf("%s [EAL options] -- -p PORTMASK -P"
-               "--"OPTION_RULE_IPV4"=FILE"
-               "--"OPTION_RULE_IPV6"=FILE"
-               "  [--"OPTION_CONFIG" (port,queue,lcore)[,(port,queue,lcore]]"
-               "  [--"OPTION_ENBJMO" [--max-pkt-len PKTLEN]]\n"
+               "--"OPT_RULE_IPV4"=FILE"
+               "--"OPT_RULE_IPV6"=FILE"
+               "  [--"OPT_CONFIG" (port,queue,lcore)[,(port,queue,lcore]]"
+               "  [--"OPT_ENBJMO" [--max-pkt-len PKTLEN]]\n"
                "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
                "  -P : enable promiscuous mode\n"
-               "  --"OPTION_CONFIG": (port,queue,lcore): "
+               "  --"OPT_CONFIG": (port,queue,lcore): "
                "rx queues configuration\n"
-               "  --"OPTION_NONUMA": optional, disable numa awareness\n"
-               "  --"OPTION_ENBJMO": enable jumbo frame"
+               "  --"OPT_NONUMA": optional, disable numa awareness\n"
+               "  --"OPT_ENBJMO": enable jumbo frame"
                " which max packet len is PKTLEN in decimal (64-9600)\n"
-               "  --"OPTION_RULE_IPV4"=FILE: specify the ipv4 rules entries "
+               "  --"OPT_RULE_IPV4"=FILE: specify the ipv4 rules entries "
                "file. "
                "Each rule occupy one line. "
                "2 kinds of rules are supported. "
                "One is ACL entry at while line leads with character '%c', "
                "another is route entry at while line leads with "
                "character '%c'.\n"
-               "  --"OPTION_RULE_IPV6"=FILE: specify the ipv6 rules "
+               "  --"OPT_RULE_IPV6"=FILE: specify the ipv6 rules "
                "entries file.\n"
-               "  --"OPTION_SCALAR": Use scalar function to do lookup\n",
-               prgname, ACL_LEAD_CHAR, ROUTE_LEAD_CHAR);
+               "  --"OPT_ALG": ACL classify method to use, one of: %s\n",
+               prgname, ACL_LEAD_CHAR, ROUTE_LEAD_CHAR, alg);
 }
 
 static int
@@ -1567,10 +1669,7 @@ parse_portmask(const char *portmask)
        /* parse hexadecimal string */
        pm = strtoul(portmask, &end, 16);
        if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
-               return -1;
-
-       if (pm == 0)
-               return -1;
+               return 0;
 
        return pm;
 }
@@ -1630,6 +1729,26 @@ parse_config(const char *q_arg)
        return 0;
 }
 
+static const char *
+parse_eth_dest(const char *optarg)
+{
+       unsigned long portid;
+       char *port_end;
+
+       errno = 0;
+       portid = strtoul(optarg, &port_end, 0);
+       if (errno != 0 || port_end == optarg || *port_end++ != ',')
+               return "Invalid format";
+       else if (portid >= RTE_MAX_ETHPORTS)
+               return "port value exceeds RTE_MAX_ETHPORTS("
+                       RTE_STR(RTE_MAX_ETHPORTS) ")";
+
+       if (cmdline_parse_etheraddr(NULL, port_end, &port_l2hdr[portid].d_addr,
+                       sizeof(port_l2hdr[portid].d_addr)) < 0)
+               return "Invalid ethernet address";
+       return NULL;
+}
+
 /* Parse the argument given in the command line of the application */
 static int
 parse_args(int argc, char **argv)
@@ -1639,13 +1758,14 @@ parse_args(int argc, char **argv)
        int option_index;
        char *prgname = argv[0];
        static struct option lgopts[] = {
-               {OPTION_CONFIG, 1, 0, 0},
-               {OPTION_NONUMA, 0, 0, 0},
-               {OPTION_ENBJMO, 0, 0, 0},
-               {OPTION_RULE_IPV4, 1, 0, 0},
-               {OPTION_RULE_IPV6, 1, 0, 0},
-               {OPTION_SCALAR, 0, 0, 0},
-               {NULL, 0, 0, 0}
+               {OPT_CONFIG,    1, NULL, OPT_CONFIG_NUM    },
+               {OPT_NONUMA,    0, NULL, OPT_NONUMA_NUM    },
+               {OPT_ENBJMO,    0, NULL, OPT_ENBJMO_NUM    },
+               {OPT_RULE_IPV4, 1, NULL, OPT_RULE_IPV4_NUM },
+               {OPT_RULE_IPV6, 1, NULL, OPT_RULE_IPV6_NUM },
+               {OPT_ALG,       1, NULL, OPT_ALG_NUM       },
+               {OPT_ETH_DEST,  1, NULL, OPT_ETH_DEST_NUM  },
+               {NULL,          0, 0,    0                 }
        };
 
        argvopt = argv;
@@ -1663,86 +1783,94 @@ parse_args(int argc, char **argv)
                                return -1;
                        }
                        break;
+
                case 'P':
                        printf("Promiscuous mode selected\n");
                        promiscuous_on = 1;
                        break;
 
                /* long options */
-               case 0:
-                       if (!strncmp(lgopts[option_index].name,
-                                       OPTION_CONFIG,
-                                       sizeof(OPTION_CONFIG))) {
-                               ret = parse_config(optarg);
-                               if (ret) {
-                                       printf("invalid config\n");
-                                       print_usage(prgname);
-                                       return -1;
-                               }
+               case OPT_CONFIG_NUM:
+                       ret = parse_config(optarg);
+                       if (ret) {
+                               printf("invalid config\n");
+                               print_usage(prgname);
+                               return -1;
                        }
+                       break;
 
-                       if (!strncmp(lgopts[option_index].name,
-                                       OPTION_NONUMA,
-                                       sizeof(OPTION_NONUMA))) {
-                               printf("numa is disabled\n");
-                               numa_on = 0;
-                       }
+               case OPT_NONUMA_NUM:
+                       printf("numa is disabled\n");
+                       numa_on = 0;
+                       break;
 
-                       if (!strncmp(lgopts[option_index].name,
-                                       OPTION_ENBJMO, sizeof(OPTION_ENBJMO))) {
-                               struct option lenopts = {
-                                       "max-pkt-len",
-                                       required_argument,
-                                       0,
-                                       0
-                               };
-
-                               printf("jumbo frame is enabled\n");
-                               port_conf.rxmode.offloads |=
-                                               DEV_RX_OFFLOAD_JUMBO_FRAME;
-                               port_conf.txmode.offloads |=
-                                               DEV_TX_OFFLOAD_MULTI_SEGS;
-
-                               /*
-                                * if no max-pkt-len set, then use the
-                                * default value RTE_ETHER_MAX_LEN
-                                */
-                               if (0 == getopt_long(argc, argvopt, "",
-                                               &lenopts, &option_index)) {
-                                       ret = parse_max_pkt_len(optarg);
-                                       if ((ret < 64) ||
-                                               (ret > MAX_JUMBO_PKT_LEN)) {
-                                               printf("invalid packet "
-                                                       "length\n");
-                                               print_usage(prgname);
-                                               return -1;
-                                       }
-                                       port_conf.rxmode.max_rx_pkt_len = ret;
+               case OPT_ENBJMO_NUM:
+               {
+                       struct option lenopts = {
+                               "max-pkt-len",
+                               required_argument,
+                               0,
+                               0
+                       };
+
+                       printf("jumbo frame is enabled\n");
+                       port_conf.rxmode.offloads |=
+                                       DEV_RX_OFFLOAD_JUMBO_FRAME;
+                       port_conf.txmode.offloads |=
+                                       DEV_TX_OFFLOAD_MULTI_SEGS;
+
+                       /*
+                        * if no max-pkt-len set, then use the
+                        * default value RTE_ETHER_MAX_LEN
+                        */
+                       if (getopt_long(argc, argvopt, "",
+                                       &lenopts, &option_index) == 0) {
+                               ret = parse_max_pkt_len(optarg);
+                               if ((ret < 64) ||
+                                       (ret > MAX_JUMBO_PKT_LEN)) {
+                                       printf("invalid packet "
+                                               "length\n");
+                                       print_usage(prgname);
+                                       return -1;
                                }
-                               printf("set jumbo frame max packet length "
-                                       "to %u\n",
-                                       (unsigned int)
-                                       port_conf.rxmode.max_rx_pkt_len);
+                               port_conf.rxmode.max_rx_pkt_len = ret;
                        }
+                       printf("set jumbo frame max packet length "
+                               "to %u\n",
+                               (unsigned int)
+                               port_conf.rxmode.max_rx_pkt_len);
+                       break;
+               }
+               case OPT_RULE_IPV4_NUM:
+                       parm_config.rule_ipv4_name = optarg;
+                       break;
 
-                       if (!strncmp(lgopts[option_index].name,
-                                       OPTION_RULE_IPV4,
-                                       sizeof(OPTION_RULE_IPV4)))
-                               parm_config.rule_ipv4_name = optarg;
+               case OPT_RULE_IPV6_NUM:
+                       parm_config.rule_ipv6_name = optarg;
+                       break;
 
-                       if (!strncmp(lgopts[option_index].name,
-                                       OPTION_RULE_IPV6,
-                                       sizeof(OPTION_RULE_IPV6))) {
-                               parm_config.rule_ipv6_name = optarg;
+               case OPT_ALG_NUM:
+                       parm_config.alg = parse_acl_alg(optarg);
+                       if (parm_config.alg ==
+                                       RTE_ACL_CLASSIFY_DEFAULT) {
+                               printf("unknown %s value:\"%s\"\n",
+                                       OPT_ALG, optarg);
+                               print_usage(prgname);
+                               return -1;
                        }
-
-                       if (!strncmp(lgopts[option_index].name,
-                                       OPTION_SCALAR, sizeof(OPTION_SCALAR)))
-                               parm_config.scalar = 1;
-
-
                        break;
 
+               case OPT_ETH_DEST_NUM:
+               {
+                       const char *serr = parse_eth_dest(optarg);
+                       if (serr != NULL) {
+                               printf("invalid %s value:\"%s\": %s\n",
+                                       OPT_ETH_DEST, optarg, serr);
+                               print_usage(prgname);
+                               return -1;
+                       }
+                       break;
+               }
                default:
                        print_usage(prgname);
                        return -1;
@@ -1814,6 +1942,8 @@ check_all_ports_link_status(uint32_t port_mask)
        uint16_t portid;
        uint8_t count, all_ports_up, print_flag = 0;
        struct rte_eth_link link;
+       int ret;
+       char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
 
        printf("\nChecking link status");
        fflush(stdout);
@@ -1823,17 +1953,20 @@ check_all_ports_link_status(uint32_t port_mask)
                        if ((port_mask & (1 << portid)) == 0)
                                continue;
                        memset(&link, 0, sizeof(link));
-                       rte_eth_link_get_nowait(portid, &link);
+                       ret = rte_eth_link_get_nowait(portid, &link);
+                       if (ret < 0) {
+                               all_ports_up = 0;
+                               if (print_flag == 1)
+                                       printf("Port %u link get failed: %s\n",
+                                               portid, rte_strerror(-ret));
+                               continue;
+                       }
                        /* print link status if flag set */
                        if (print_flag == 1) {
-                               if (link.link_status)
-                                       printf(
-                                       "Port%d Link Up. Speed %u Mbps %s\n",
-                                               portid, link.link_speed,
-                               (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
-                                       ("full-duplex") : ("half-duplex\n"));
-                               else
-                                       printf("Port %d Link Down\n", portid);
+                               rte_eth_link_to_str(link_status_text,
+                                       sizeof(link_status_text), &link);
+                               printf("Port %d %s\n", portid,
+                                      link_status_text);
                                continue;
                        }
                        /* clear all_ports_up flag if any link down */
@@ -1860,6 +1993,20 @@ check_all_ports_link_status(uint32_t port_mask)
        }
 }
 
+/*
+ * build-up default vaues for dest MACs.
+ */
+static void
+set_default_dest_mac(void)
+{
+       uint32_t i;
+
+       for (i = 0; i != RTE_DIM(port_l2hdr); i++) {
+               port_l2hdr[i].d_addr.addr_bytes[0] = RTE_ETHER_LOCAL_ADMIN_ADDR;
+               port_l2hdr[i].d_addr.addr_bytes[5] = i;
+       }
+}
+
 int
 main(int argc, char **argv)
 {
@@ -1881,6 +2028,8 @@ main(int argc, char **argv)
        argc -= ret;
        argv += ret;
 
+       set_default_dest_mac();
+
        /* parse application arguments (after the EAL ones) */
        ret = parse_args(argc, argv);
        if (ret < 0)
@@ -1960,8 +2109,14 @@ main(int argc, char **argv)
                                "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%d\n",
                                ret, portid);
 
-               rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
-               print_ethaddr(" Address:", &ports_eth_addr[portid]);
+               ret = rte_eth_macaddr_get(portid, &port_l2hdr[portid].s_addr);
+               if (ret < 0)
+                       rte_exit(EXIT_FAILURE,
+                               "rte_eth_macaddr_get: err=%d, port=%d\n",
+                               ret, portid);
+
+               print_ethaddr("Dst MAC:", &port_l2hdr[portid].d_addr);
+               print_ethaddr(", Src MAC:", &port_l2hdr[portid].s_addr);
                printf(", ");
 
                /* init memory */
@@ -2097,11 +2252,14 @@ main(int argc, char **argv)
        check_all_ports_link_status(enabled_port_mask);
 
        /* launch per-lcore init on every lcore */
-       rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
-       RTE_LCORE_FOREACH_SLAVE(lcore_id) {
+       rte_eal_mp_remote_launch(main_loop, NULL, CALL_MAIN);
+       RTE_LCORE_FOREACH_WORKER(lcore_id) {
                if (rte_eal_wait_lcore(lcore_id) < 0)
                        return -1;
        }
 
+       /* clean up the EAL */
+       rte_eal_cleanup();
+
        return 0;
 }