examples: fix port mask parsing failure handling
[dpdk.git] / examples / vm_power_manager / main.c
index f8cab11..3a89580 100644 (file)
@@ -54,7 +54,7 @@ static volatile bool force_quit;
 /****************/
 static const struct rte_eth_conf port_conf_default = {
        .rxmode = {
-               .max_rx_pkt_len = ETHER_MAX_LEN,
+               .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
        },
 };
 
@@ -71,7 +71,13 @@ port_init(uint16_t port, struct rte_mempool *mbuf_pool)
        if (!rte_eth_dev_is_valid_port(port))
                return -1;
 
-       rte_eth_dev_info_get(port, &dev_info);
+       retval = rte_eth_dev_info_get(port, &dev_info);
+       if (retval != 0) {
+               printf("Error during getting device (port %u) info: %s\n",
+                               port, strerror(-retval));
+               return retval;
+       }
+
        if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
                port_conf.txmode.offloads |=
                        DEV_TX_OFFLOAD_MBUF_FAST_FREE;
@@ -106,7 +112,13 @@ port_init(uint16_t port, struct rte_mempool *mbuf_pool)
 
        /* Display the port MAC address. */
        struct rte_ether_addr addr;
-       rte_eth_macaddr_get(port, &addr);
+       retval = rte_eth_macaddr_get(port, &addr);
+       if (retval != 0) {
+               printf("Failed to get device (port %u) MAC address: %s\n",
+                               port, rte_strerror(-retval));
+               return retval;
+       }
+
        printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
                           " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
                        (unsigned int)port,
@@ -115,7 +127,9 @@ port_init(uint16_t port, struct rte_mempool *mbuf_pool)
                        addr.addr_bytes[4], addr.addr_bytes[5]);
 
        /* Enable RX in promiscuous mode for the Ethernet device. */
-       rte_eth_promiscuous_enable(port);
+       retval = rte_eth_promiscuous_enable(port);
+       if (retval != 0)
+               return retval;
 
 
        return 0;
@@ -130,10 +144,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;
 }
@@ -151,15 +162,14 @@ parse_args(int argc, char **argv)
        static struct option lgopts[] = {
                { "mac-updating", no_argument, 0, 1},
                { "no-mac-updating", no_argument, 0, 0},
-               { "core-list", optional_argument, 0, 'l'},
+               { "core-branch-ratio", optional_argument, 0, 'b'},
                { "port-list", optional_argument, 0, 'p'},
-               { "branch-ratio", optional_argument, 0, 'b'},
                {NULL, 0, 0, 0}
        };
        argvopt = argv;
        ci = get_core_info();
 
-       while ((opt = getopt_long(argc, argvopt, "l:p:q:T:b:",
+       while ((opt = getopt_long(argc, argvopt, "p:q:T:b:",
                                  lgopts, &option_index)) != EOF) {
 
                switch (opt) {
@@ -171,7 +181,8 @@ parse_args(int argc, char **argv)
                                return -1;
                        }
                        break;
-               case 'l':
+               case 'b':
+                       branch_ratio = BRANCH_RATIO_THRESHOLD;
                        oob_enable = malloc(ci->core_count * sizeof(uint16_t));
                        if (oob_enable == NULL) {
                                printf("Error - Unable to allocate memory\n");
@@ -179,32 +190,37 @@ parse_args(int argc, char **argv)
                        }
                        cnt = parse_set(optarg, oob_enable, ci->core_count);
                        if (cnt < 0) {
-                               printf("Invalid core-list - [%s]\n",
+                               printf("Invalid core-list section in "
+                                      "core-branch-ratio matrix - [%s]\n",
+                                               optarg);
+                               free(oob_enable);
+                               break;
+                       }
+                       cnt = parse_branch_ratio(optarg, &branch_ratio);
+                       if (cnt < 0) {
+                               printf("Invalid branch-ratio section in "
+                                      "core-branch-ratio matrix - [%s]\n",
                                                optarg);
                                free(oob_enable);
                                break;
                        }
+                       if (branch_ratio <= 0.0 || branch_ratio > 100.0) {
+                               printf("invalid branch ratio specified\n");
+                               return -1;
+                       }
                        for (i = 0; i < ci->core_count; i++) {
                                if (oob_enable[i]) {
-                                       printf("***Using core %d\n", i);
+                                       printf("***Using core %d "
+                                              "with branch ratio %f\n",
+                                              i, branch_ratio);
                                        ci->cd[i].oob_enabled = 1;
                                        ci->cd[i].global_enabled_cpus = 1;
+                                       ci->cd[i].branch_ratio_threshold =
+                                                               branch_ratio;
                                }
                        }
                        free(oob_enable);
                        break;
-               case 'b':
-                       branch_ratio = 0.0;
-                       if (strlen(optarg))
-                               branch_ratio = atof(optarg);
-                       if (branch_ratio <= 0.0) {
-                               printf("invalid branch ratio specified\n");
-                               return -1;
-                       }
-                       ci->branch_ratio_threshold = branch_ratio;
-                       printf("***Setting branch ratio to %f\n",
-                                       branch_ratio);
-                       break;
                /* long options */
                case 0:
                        break;
@@ -229,6 +245,7 @@ check_all_ports_link_status(uint32_t port_mask)
 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
        uint16_t portid, count, all_ports_up, print_flag = 0;
        struct rte_eth_link link;
+       int ret;
 
        printf("\nChecking link status");
        fflush(stdout);
@@ -242,7 +259,14 @@ 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)
@@ -250,7 +274,7 @@ check_all_ports_link_status(uint32_t port_mask)
                                                "Mbps - %s\n", (uint16_t)portid,
                                                (unsigned int)link.link_speed,
                                (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
-                                       ("full-duplex") : ("half-duplex\n"));
+                                       ("full-duplex") : ("half-duplex"));
                                else
                                        printf("Port %d Link Down\n",
                                                (uint16_t)portid);
@@ -280,7 +304,7 @@ check_all_ports_link_status(uint32_t port_mask)
        }
 }
 static int
-run_monitor(__attribute__((unused)) void *arg)
+run_monitor(__rte_unused void *arg)
 {
        if (channel_monitor_init() < 0) {
                printf("Unable to initialize channel monitor\n");
@@ -291,7 +315,7 @@ run_monitor(__attribute__((unused)) void *arg)
 }
 
 static int
-run_core_monitor(__attribute__((unused)) void *arg)
+run_core_monitor(__rte_unused void *arg)
 {
        if (branch_monitor_init() < 0) {
                printf("Unable to initialize core monitor\n");
@@ -434,7 +458,7 @@ main(int argc, char **argv)
                return -1;
        }
 
-       add_host_channel();
+       add_host_channels();
 
        printf("Running core monitor on lcore id %d\n", lcore_id);
        rte_eal_remote_launch(run_core_monitor, NULL, lcore_id);