From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f169.google.com (mail-wr0-f169.google.com [209.85.128.169]) by dpdk.org (Postfix) with ESMTP id D037F2B93 for ; Fri, 31 Mar 2017 14:04:57 +0200 (CEST) Received: by mail-wr0-f169.google.com with SMTP id k6so97654824wre.2 for ; Fri, 31 Mar 2017 05:04:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=tvRcz8JhUvAGU2sNM2h3iysh88glRhkHtuYHCUv1dos=; b=y4/T5S5YfqAzDTbUBXu+U7ueBNO8KJOX979aY8StMnf53cxuUvK2zWTQao8/HWpUyc Df0j00GFRLSORG4YsxM35PdI2DvCwcm+phhODleF0MO24iH09i5mKoYH3t6tXZVftZ7W sLICiEWAKa12t1d1OuNScCfFpaDtSdaxTF0bvCnu8mdacx4FUG3JGRFcYwyPGftzyQh2 hzL28o+54JFEaZc/adidtq9x4DiRXiI/Sgo1oUqqHFhOugLl3+6TS3OxOMnGh/byrXbQ S3Rrg+1iGAHBNBzFk76tp7ohnoxbV17ZDejL4XeYbsJ4qVf2fC/tQItfEZC4DTER1c/T qjvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=tvRcz8JhUvAGU2sNM2h3iysh88glRhkHtuYHCUv1dos=; b=iSNNyUC1FXrfdlFTfNZC4LGFduY44buy9a2VZ8CcITT0/N9TgtFyh1ottHd0nSHfq1 r6PJjgjD85dn4ZYDvYnO75AHeUtXYP0NTEObaQT7jYOXe3L2Nrx3CQIDqa8GKhm7OCrH a1Uq5pVQogftUJckduau5/4b039lq6NEY1rp10JILfFXrkRpVn9kLEoGcyfAh0g762Z1 9fCjSFHzWbqxW4tIgvs1LOpMNgTdL1O+GJQfYbU/Wm8DmS6srWVLTPwBwHPOiJ4Ly1H9 UZ0RthlynFnPMQ3+6exV5oeBMvml0ioJczSQPYZ4vy8RH5FqtgaPQx0+dlQpZ4aPnqjE 97hg== X-Gm-Message-State: AFeK/H0Kth0gFwKbzHAeUqOPxcm6Qb92jO35NFxfyF2L/XFIhuujBOEQiouFtmDnafeHonpv X-Received: by 10.28.51.67 with SMTP id z64mr3033831wmz.22.1490961897467; Fri, 31 Mar 2017 05:04:57 -0700 (PDT) Received: from bidouze.dev.6wind.com (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id l41sm6487536wrl.59.2017.03.31.05.04.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 31 Mar 2017 05:04:56 -0700 (PDT) From: Gaetan Rivet To: dev@dpdk.org Cc: Thomas Monjalon , Jingjing Wu Date: Fri, 31 Mar 2017 14:04:38 +0200 Message-Id: <3ae96511a71fa9f79be09eb518e7837c9a637599.1490961533.git.gaetan.rivet@6wind.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH v2 2/3] ethdev: add device iterator X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 31 Mar 2017 12:04:58 -0000 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 90d0713..ca7af35 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 b9531f3..bed7dff 100644 --- a/lib/librte_ether/rte_ethdev.h +++ b/lib/librte_ether/rte_ethdev.h @@ -1736,6 +1736,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. * All devices whose port identifier is in the range @@ -1743,7 +1762,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.1.4