]> git.droids-corp.org - dpdk.git/commitdiff
ethdev: avoid explicit check of valid port state
authorThomas Monjalon <thomas@monjalon.net>
Wed, 17 Apr 2019 22:59:27 +0000 (00:59 +0200)
committerFerruh Yigit <ferruh.yigit@intel.com>
Fri, 19 Apr 2019 12:51:55 +0000 (14:51 +0200)
Some port iterations are manually checking against RTE_ETH_DEV_UNUSED
instead of using the iterators based on rte_eth_find_next().

A new macro RTE_ETH_FOREACH_VALID_DEV() is introduced, but kept private
because there should be no need of iterating over all devices in the
API. The public iterators have additional filters for ownership, parent
device or sibling ports.

Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
drivers/net/mlx5/mlx5.c
lib/librte_ethdev/rte_ethdev.c

index 9ff50dfbe42cfdef3bfb9bc8115f56e471c239fe..4deaada5c88956c841951a05cf516b7ede55cf58 100644 (file)
@@ -1964,14 +1964,9 @@ static int
 mlx5_pci_remove(struct rte_pci_device *pci_dev)
 {
        uint16_t port_id;
-       struct rte_eth_dev *port;
 
-       for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
-               port = &rte_eth_devices[port_id];
-               if (port->state != RTE_ETH_DEV_UNUSED &&
-                               port->device == &pci_dev->device)
-                       rte_eth_dev_close(port_id);
-       }
+       RTE_ETH_FOREACH_DEV_OF(port_id, &pci_dev->device)
+               rte_eth_dev_close(port_id);
        return 0;
 }
 
index 243beb4dd5ef6f2755de6b0ac252626f81f2df81..ba6fc91d1e6b9264e465f04a3598a3e44038a5c9 100644 (file)
@@ -339,6 +339,15 @@ rte_eth_find_next(uint16_t port_id)
        return port_id;
 }
 
+/*
+ * Macro to iterate over all valid ports for internal usage.
+ * Note: RTE_ETH_FOREACH_DEV is different because filtering owned ports.
+ */
+#define RTE_ETH_FOREACH_VALID_DEV(port_id) \
+       for (port_id = rte_eth_find_next(0); \
+            port_id < RTE_MAX_ETHPORTS; \
+            port_id = rte_eth_find_next(port_id + 1))
+
 uint16_t
 rte_eth_find_next_of(uint16_t port_id, const struct rte_device *parent)
 {
@@ -584,13 +593,10 @@ rte_eth_is_valid_owner_id(uint64_t owner_id)
 uint64_t
 rte_eth_find_next_owned_by(uint16_t port_id, const uint64_t owner_id)
 {
+       port_id = rte_eth_find_next(port_id);
        while (port_id < RTE_MAX_ETHPORTS &&
-              (rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED ||
-              rte_eth_devices[port_id].data->owner.id != owner_id))
-               port_id++;
-
-       if (port_id >= RTE_MAX_ETHPORTS)
-               return RTE_MAX_ETHPORTS;
+                       rte_eth_devices[port_id].data->owner.id != owner_id)
+               port_id = rte_eth_find_next(port_id + 1);
 
        return port_id;
 }
@@ -768,9 +774,8 @@ rte_eth_dev_count_total(void)
 {
        uint16_t port, count = 0;
 
-       for (port = 0; port < RTE_MAX_ETHPORTS; port++)
-               if (rte_eth_devices[port].state != RTE_ETH_DEV_UNUSED)
-                       count++;
+       RTE_ETH_FOREACH_VALID_DEV(port)
+               count++;
 
        return count;
 }
@@ -804,13 +809,11 @@ rte_eth_dev_get_port_by_name(const char *name, uint16_t *port_id)
                return -EINVAL;
        }
 
-       for (pid = 0; pid < RTE_MAX_ETHPORTS; pid++) {
-               if (rte_eth_devices[pid].state != RTE_ETH_DEV_UNUSED &&
-                   !strcmp(name, rte_eth_dev_shared_data->data[pid].name)) {
+       RTE_ETH_FOREACH_VALID_DEV(pid)
+               if (!strcmp(name, rte_eth_dev_shared_data->data[pid].name)) {
                        *port_id = pid;
                        return 0;
                }
-       }
 
        return -ENODEV;
 }