examples: add eal cleanup to examples
[dpdk.git] / examples / ethtool / ethtool-app / main.c
index 2c655d8..21ed85c 100644 (file)
@@ -1,34 +1,5 @@
-/*-
- *   BSD LICENSE
- *
- *   Copyright(c) 2015 Intel Corporation. All rights reserved.
- *   All rights reserved.
- *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of Intel Corporation nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2015 Intel Corporation
  */
 
 
@@ -49,8 +20,8 @@
 
 #define MAX_PORTS RTE_MAX_ETHPORTS
 #define MAX_BURST_LENGTH 32
-#define PORT_RX_QUEUE_SIZE 128
-#define PORT_TX_QUEUE_SIZE 256
+#define PORT_RX_QUEUE_SIZE 1024
+#define PORT_TX_QUEUE_SIZE 1024
 #define PKTPOOL_EXTRA_SIZE 512
 #define PKTPOOL_CACHE 32
 
@@ -61,7 +32,7 @@ struct txq_port {
 };
 
 struct app_port {
-       struct ether_addr mac_addr;
+       struct rte_ether_addr mac_addr;
        struct txq_port txq;
        rte_spinlock_t lock;
        int port_active;
@@ -122,6 +93,9 @@ static void setup_ports(struct app_config *app_cfg, int cnt_ports)
        struct rte_eth_conf cfg_port;
        struct rte_eth_dev_info dev_info;
        char str_name[16];
+       uint16_t nb_rxd = PORT_RX_QUEUE_SIZE;
+       uint16_t nb_txd = PORT_TX_QUEUE_SIZE;
+       int ret;
 
        memset(&cfg_port, 0, sizeof(cfg_port));
        cfg_port.txmode.mq_mode = ETH_MQ_TX_NONE;
@@ -129,7 +103,12 @@ static void setup_ports(struct app_config *app_cfg, int cnt_ports)
        for (idx_port = 0; idx_port < cnt_ports; idx_port++) {
                struct app_port *ptr_port = &app_cfg->ports[idx_port];
 
-               rte_eth_dev_info_get(idx_port, &dev_info);
+               ret = rte_eth_dev_info_get(idx_port, &dev_info);
+               if (ret != 0)
+                       rte_exit(EXIT_FAILURE,
+                               "Error during getting device (port %u) info: %s\n",
+                               idx_port, strerror(-ret));
+
                size_pktpool = dev_info.rx_desc_lim.nb_max +
                        dev_info.tx_desc_lim.nb_max + PKTPOOL_EXTRA_SIZE;
 
@@ -154,15 +133,20 @@ static void setup_ports(struct app_config *app_cfg, int cnt_ports)
                if (rte_eth_dev_configure(idx_port, 1, 1, &cfg_port) < 0)
                        rte_exit(EXIT_FAILURE,
                                 "rte_eth_dev_configure failed");
+               if (rte_eth_dev_adjust_nb_rx_tx_desc(idx_port, &nb_rxd,
+                                                    &nb_txd) < 0)
+                       rte_exit(EXIT_FAILURE,
+                                "rte_eth_dev_adjust_nb_rx_tx_desc failed");
+
                if (rte_eth_rx_queue_setup(
-                           idx_port, 0, PORT_RX_QUEUE_SIZE,
+                           idx_port, 0, nb_rxd,
                            rte_eth_dev_socket_id(idx_port), NULL,
                            ptr_port->pkt_pool) < 0)
                        rte_exit(EXIT_FAILURE,
                                 "rte_eth_rx_queue_setup failed"
                                );
                if (rte_eth_tx_queue_setup(
-                           idx_port, 0, PORT_TX_QUEUE_SIZE,
+                           idx_port, 0, nb_txd,
                            rte_eth_dev_socket_id(idx_port), NULL) < 0)
                        rte_exit(EXIT_FAILURE,
                                 "rte_eth_tx_queue_setup failed"
@@ -172,8 +156,12 @@ static void setup_ports(struct app_config *app_cfg, int cnt_ports)
                                 "%s:%i: rte_eth_dev_start failed",
                                 __FILE__, __LINE__
                                );
-               rte_eth_promiscuous_enable(idx_port);
-               rte_eth_macaddr_get(idx_port, &ptr_port->mac_addr);
+               ret = rte_eth_macaddr_get(idx_port, &ptr_port->mac_addr);
+               if (ret != 0)
+                       rte_exit(EXIT_FAILURE,
+                               "rte_eth_macaddr_get failed (port %u): %s\n",
+                               idx_port, rte_strerror(-ret));
+
                rte_spinlock_init(&ptr_port->lock);
        }
 }
@@ -181,14 +169,14 @@ static void setup_ports(struct app_config *app_cfg, int cnt_ports)
 static void process_frame(struct app_port *ptr_port,
        struct rte_mbuf *ptr_frame)
 {
-       struct ether_hdr *ptr_mac_hdr;
+       struct rte_ether_hdr *ptr_mac_hdr;
 
-       ptr_mac_hdr = rte_pktmbuf_mtod(ptr_frame, struct ether_hdr *);
-       ether_addr_copy(&ptr_mac_hdr->s_addr, &ptr_mac_hdr->d_addr);
-       ether_addr_copy(&ptr_port->mac_addr, &ptr_mac_hdr->s_addr);
+       ptr_mac_hdr = rte_pktmbuf_mtod(ptr_frame, struct rte_ether_hdr *);
+       rte_ether_addr_copy(&ptr_mac_hdr->s_addr, &ptr_mac_hdr->d_addr);
+       rte_ether_addr_copy(&ptr_port->mac_addr, &ptr_mac_hdr->s_addr);
 }
 
-static int slave_main(__attribute__((unused)) void *ptr_data)
+static int worker_main(__rte_unused void *ptr_data)
 {
        struct app_port *ptr_port;
        struct rte_mbuf *ptr_frame;
@@ -199,6 +187,7 @@ static int slave_main(__attribute__((unused)) void *ptr_data)
        uint16_t cnt_sent;
        uint16_t idx_port;
        uint16_t lock_result;
+       int ret;
 
        while (app_cfg.exit_now == 0) {
                for (idx_port = 0; idx_port < app_cfg.cnt_ports; idx_port++) {
@@ -215,8 +204,16 @@ static int slave_main(__attribute__((unused)) void *ptr_data)
 
                        /* MAC address was updated */
                        if (ptr_port->port_dirty == 1) {
-                               rte_eth_macaddr_get(ptr_port->idx_port,
+                               ret = rte_eth_macaddr_get(ptr_port->idx_port,
                                        &ptr_port->mac_addr);
+                               if (ret != 0) {
+                                       rte_spinlock_unlock(&ptr_port->lock);
+                                       printf("Failed to get MAC address (port %u): %s",
+                                              ptr_port->idx_port,
+                                              rte_strerror(-ret));
+                                       return ret;
+                               }
+
                                ptr_port->port_dirty = 0;
                        }
 
@@ -265,12 +262,12 @@ int main(int argc, char **argv)
        uint32_t id_core;
        uint32_t cnt_ports;
 
-       /* Init runtime enviornment */
+       /* Init runtime environment */
        cnt_args_parsed = rte_eal_init(argc, argv);
        if (cnt_args_parsed < 0)
                rte_exit(EXIT_FAILURE, "rte_eal_init(): Failed");
 
-       cnt_ports = rte_eth_dev_count();
+       cnt_ports = rte_eth_dev_count_avail();
        printf("Number of NICs: %i\n", cnt_ports);
        if (cnt_ports == 0)
                rte_exit(EXIT_FAILURE, "No available NIC ports!\n");
@@ -287,19 +284,23 @@ int main(int argc, char **argv)
        app_cfg.cnt_ports = cnt_ports;
 
        if (rte_lcore_count() < 2)
-               rte_exit(EXIT_FAILURE, "No available slave core!\n");
-       /* Assume there is an available slave.. */
+               rte_exit(EXIT_FAILURE, "No available worker core!\n");
+
+       /* Assume there is an available worker.. */
        id_core = rte_lcore_id();
        id_core = rte_get_next_lcore(id_core, 1, 1);
-       rte_eal_remote_launch(slave_main, NULL, id_core);
+       rte_eal_remote_launch(worker_main, NULL, id_core);
 
        ethapp_main();
 
        app_cfg.exit_now = 1;
-       RTE_LCORE_FOREACH_SLAVE(id_core) {
+       RTE_LCORE_FOREACH_WORKER(id_core) {
                if (rte_eal_wait_lcore(id_core) < 0)
                        return -1;
        }
 
+       /* clean up the EAL */
+       rte_eal_cleanup();
+
        return 0;
 }