From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f174.google.com (mail-wr0-f174.google.com [209.85.128.174]) by dpdk.org (Postfix) with ESMTP id 9385BD586 for ; Fri, 3 Mar 2017 16:40:27 +0100 (CET) Received: by mail-wr0-f174.google.com with SMTP id l37so76289749wrc.1 for ; Fri, 03 Mar 2017 07:40:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=rnBYlHALLGmrVlf9QQOxSVj3+ZrVxdbcCQ0UUNI2YZ8=; b=egYiAChh2mW7tzZflez6MC4AE/kOQIHuqMG3xWVFW5swdioRI0FQtP3kAE2eaHL6E4 QObzJwr23FQ7KT2ol1r2JFw4/2OtKk2fy1YDIbUWfVrY7duXmgPf+ggMUi/xCAa6gHuB 1BA6Lvhf8SN+vXwXj2nX3heJhillfKa8GappDZSMLG4nimWzEQywipA7x82KV0GZxnKG hXlQ9np7WBVxJ3V3oMPcDdonKVpvpSBHtXYQXle37/8xfztaYPj2qSDweQd0Rg+zKQz9 MIEcf3Kp2b11rV6A0kwS/IwkteT5g9DFxtrwKlc+2xVYiw9SU8alHgcPQSxJOA8J9BKL 7q+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=rnBYlHALLGmrVlf9QQOxSVj3+ZrVxdbcCQ0UUNI2YZ8=; b=QjF59Keu04/eInoBv57QOLhs/NzxmgQfnD7510SZBlyN2qwhg5GqqiHCmSX3xth/QM YITyBBQl3RCZerRtWwG7tHE02s3ZhHdVOIZ34dbGIolxbs0W3FCHeGGQ9quI0EPKf2Iw ndXmGnXrWU1VR29A8IMOFGEWJlkq6PwJ4hx6jcbj2PQrgPqz+Vfzyfe1o6jEAv4XTwXs TTmIwKeZPRRum70MogNpLcg2NVQKyG0ODPZcr/8OIymFys762Q71sdYbvg+57Ly45eX1 ITL0ULdtChvOgBbIHYBrSN5fHHssYeo6BQIndZJ+hOnZuGfx1FRpynVfkHu0CHFODpR9 ASjQ== X-Gm-Message-State: AMke39lHQJGyo0D08wpN6h1ggTACtJFxFKIiZYuPLHVlSOWViLM8GMQ4TZ1fIhj6Tf1j58ut X-Received: by 10.223.135.215 with SMTP id c23mr3124823wrc.135.1488555626914; Fri, 03 Mar 2017 07:40:26 -0800 (PST) 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 k195sm3513175wmd.7.2017.03.03.07.40.26 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 03 Mar 2017 07:40:26 -0800 (PST) From: Gaetan Rivet To: dev@dpdk.org Date: Fri, 3 Mar 2017 16:40:12 +0100 Message-Id: <08811b2c92fa8c802a13000186aaebd5db5ee2ca.1488550937.git.gaetan.rivet@6wind.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: References: Subject: [dpdk-dev] [PATCH 3/4] ethdev: count devices consistently 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, 03 Mar 2017 15:40:27 -0000 Make the rte_eth_dev_count() return the correct number of devices even after some are detached by the hotplug API. This change does not affect existing applications that do not use hotplug API calls. Those that do are already aware that port IDs are not necessarily contiguous. Signed-off-by: Gaetan Rivet --- lib/librte_ether/rte_ethdev.c | 20 ++++++++++---------- lib/librte_ether/rte_ethdev.h | 14 ++++++++------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/librte_ether/rte_ethdev.c b/lib/librte_ether/rte_ethdev.c index fcb9933..3a52d0a 100644 --- a/lib/librte_ether/rte_ethdev.c +++ b/lib/librte_ether/rte_ethdev.c @@ -72,7 +72,6 @@ static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data"; struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS]; static struct rte_eth_dev_data *rte_eth_dev_data; static uint8_t eth_dev_last_created_port; -static uint8_t nb_ports; /* spinlock for eth device callbacks */ static rte_spinlock_t rte_eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER; @@ -207,7 +206,6 @@ eth_dev_get(uint8_t port_id) TAILQ_INIT(&(eth_dev->link_intr_cbs)); eth_dev_last_created_port = port_id; - nb_ports++; return eth_dev; } @@ -280,7 +278,6 @@ rte_eth_dev_release_port(struct rte_eth_dev *eth_dev) return -EINVAL; eth_dev->state = RTE_ETH_DEV_UNUSED; - nb_ports--; return 0; } @@ -401,7 +398,15 @@ rte_eth_dev_socket_id(uint8_t port_id) uint8_t rte_eth_dev_count(void) { - return nb_ports; + uint8_t p; + uint8_t count; + + count = 0; + + RTE_ETH_FOREACH_DEV(p) + count++; + + return count; } int @@ -433,13 +438,8 @@ rte_eth_dev_get_port_by_name(const char *name, uint8_t *port_id) return -EINVAL; } - if (!nb_ports) - 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 59c4123..bdad81b 100644 --- a/lib/librte_ether/rte_ethdev.h +++ b/lib/librte_ether/rte_ethdev.h @@ -1743,9 +1743,9 @@ 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); \ - p < RTE_MAX_ETHPORTS; \ +#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)) @@ -1755,9 +1755,11 @@ uint8_t rte_eth_find_next(uint8_t port_id); * All devices whose port identifier is in the range * [0, rte_eth_dev_count() - 1] can be operated on by network applications * 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. + * If the application unplugs a port using a hotplug function, the range of + * enabled ports may be non-contiguous. In this case, this function returns + * the actual number of enabled ports and the application must keep track + * of possible gaps in the enabled range, or use the ``RTE_ETH_FOREACH_DEV()`` + * macro. * * @return * - The total number of usable Ethernet devices. -- 2.1.4