From 5588909af21b69abf0ef076d2bd0758edd1dce70 Mon Sep 17 00:00:00 2001 From: Gaetan Rivet Date: Fri, 31 Mar 2017 14:04:38 +0200 Subject: [PATCH] ethdev: add device iterator This iterator helps applications iterate over the device list and skip holes caused by invalid or detached devices. Signed-off-by: Gaetan Rivet --- lib/librte_ether/rte_ethdev.c | 17 ++++++++++++++--- lib/librte_ether/rte_ethdev.h | 21 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/librte_ether/rte_ethdev.c b/lib/librte_ether/rte_ethdev.c index d737ca6cdb..4e1e6dc178 100644 --- a/lib/librte_ether/rte_ethdev.c +++ b/lib/librte_ether/rte_ethdev.c @@ -138,6 +138,19 @@ enum { STAT_QMAP_RX }; +uint8_t +rte_eth_find_next(uint8_t port_id) +{ + while (port_id < RTE_MAX_ETHPORTS && + rte_eth_devices[port_id].state != RTE_ETH_DEV_ATTACHED) + port_id++; + + if (port_id >= RTE_MAX_ETHPORTS) + return RTE_MAX_ETHPORTS; + + return port_id; +} + static void rte_eth_dev_data_alloc(void) { @@ -424,9 +437,7 @@ rte_eth_dev_get_port_by_name(const char *name, uint8_t *port_id) return -ENODEV; *port_id = RTE_MAX_ETHPORTS; - - for (i = 0; i < RTE_MAX_ETHPORTS; i++) { - + RTE_ETH_FOREACH_DEV(i) { if (!strncmp(name, rte_eth_dev_data[i].name, strlen(name))) { diff --git a/lib/librte_ether/rte_ethdev.h b/lib/librte_ether/rte_ethdev.h index 266434ed48..d07253874e 100644 --- a/lib/librte_ether/rte_ethdev.h +++ b/lib/librte_ether/rte_ethdev.h @@ -1745,6 +1745,25 @@ struct rte_eth_dev_data { */ extern struct rte_eth_dev rte_eth_devices[]; +/** + * Iterates over valid ethdev ports. + * + * @param port_id + * The id of the next possible valid port. + * @return + * Next valid port id, RTE_MAX_ETHPORTS if there is none. + */ +uint8_t rte_eth_find_next(uint8_t port_id); + +/** + * Macro to iterate over all enabled ethdev ports. + */ +#define RTE_ETH_FOREACH_DEV(p) \ + for (p = rte_eth_find_next(0); \ + (unsigned int)p < (unsigned int)RTE_MAX_ETHPORTS; \ + p = rte_eth_find_next(p + 1)) + + /** * Get the total number of Ethernet devices that have been successfully * initialized by the [matching] Ethernet driver during the PCI probing phase. @@ -1753,7 +1772,7 @@ extern struct rte_eth_dev rte_eth_devices[]; * immediately after invoking rte_eal_init(). * If the application unplugs a port using hotplug function, The enabled port * numbers may be noncontiguous. In the case, the applications need to manage - * enabled port by themselves. + * enabled port by using the ``RTE_ETH_FOREACH_DEV()`` macro. * * @return * - The total number of usable Ethernet devices. -- 2.20.1