From patchwork Fri Mar 9 22:22:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Monjalon X-Patchwork-Id: 35921 X-Patchwork-Delegate: ferruh.yigit@amd.com Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id A00854C8C; Fri, 9 Mar 2018 23:23:11 +0100 (CET) Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by dpdk.org (Postfix) with ESMTP id 751C54C7F for ; Fri, 9 Mar 2018 23:23:10 +0100 (CET) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id E32D320C3A; Fri, 9 Mar 2018 17:23:09 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute1.internal (MEProxy); Fri, 09 Mar 2018 17:23:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=monjalon.net; h= date:from:message-id:subject:to:x-me-sender:x-me-sender :x-sasl-enc; s=mesmtp; bh=33CfLH29IpMgmQdmAXdwa5rkWfs7RUZUYgo23l 614B8=; b=WLwfsomQzsyjayUqp2vO4PHrTkyaamuLZOsg3gkoDeKDBK9Tcfsxph yrfhNIvmPmpkaVG4k4NwRGGB4UttECKSCxqJaIORO4eiBA9Q95+Z9j07YjXdNmO5 LplLOJeajvlfBikMezYzvBSfe2nz5NUokGLcuWuWa6M8R/Xn6i9rw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=date:from:message-id:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=33CfLH29IpMgmQdmA Xdwa5rkWfs7RUZUYgo23l614B8=; b=Pe07LN6F6IYbXszGcbCFQFrUHNVtJ0JWO eXN3HXDm5bVLL19nqAu8HvbwIzCA98AwJ1/5pBLSn+5s90n0LXZQaGKLbAU8/eTp HlwL9vvq98uDfifGS3w4yPl7oVcZHq2/K4vEIRa1jsNaJbgTZXqAClayc37JQjBi UYlDZqVpOUbODGdgxwW7wBQCzPDxlOtfrk0fABd9GQPlTfwTc0daZ4rOKU+ZRY8r QxsnzvrBtHQ9GT7Uzt0/fHAcXlBi+RV+V6CGDvvfQgq7xH3r1QCtHvrJEwcD9T/f d3suQA5HYLF+zsC/i7n4qo/YaZcxOhOQTw4t4dVoSPjbGKgJLYUSA== X-ME-Sender: Received: from xps.monjalon.net (184.203.134.77.rev.sfr.net [77.134.203.184]) by mail.messagingengine.com (Postfix) with ESMTPA id 64FB77E0D4 for ; Fri, 9 Mar 2018 17:23:09 -0500 (EST) From: Thomas Monjalon To: dev@dpdk.org Date: Fri, 9 Mar 2018 23:22:55 +0100 Message-Id: <20180309222255.27663-1-thomas@monjalon.net> X-Mailer: git-send-email 2.16.2 Subject: [dpdk-dev] [RFC PATCH] ethdev: fix ports enumeration 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: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Some DPDK applications wrongly assume these requirements: - no hotplug, i.e. ports are never detached - all allocated ports are available to the application Such application iterates over ports by its own mean. The most common pattern is to request the port count and assume ports with index in the range [0..count[ can be used. There are three consequences when using such wrong design: - new ports having an index higher than the port count won't be seen - old ports being detached (RTE_ETH_DEV_UNUSED) can be seen as ghosts - failsafe sub-devices (RTE_ETH_DEV_DEFERRED) will be seen by the application Such mistake will be less common with growing hotplug awareness. All applications and examples inside this repository - except testpmd - must be fixed to use the iterator RTE_ETH_FOREACH_DEV. In order to fix this common mistake in all external applications, the function rte_eth_dev_count is deprecated and replaced by the new functions rte_eth_dev_count_avail and rte_eth_dev_count_total. Signed-off-by: Thomas Monjalon --- This RFC fixes only procinfo and skeleton example. Next version of this patch should fix all occurences. This RFC gather all ideas in one patch, but next version will be more detailed and split. --- app/proc_info/main.c | 2 +- examples/skeleton/Makefile | 3 +++ examples/skeleton/basicfwd.c | 12 ++++-------- lib/librte_ether/rte_ethdev.c | 18 ++++++++++++++++++ lib/librte_ether/rte_ethdev.h | 23 +++++++++++++++++++++++ lib/librte_ether/rte_ethdev_version.map | 4 +++- 6 files changed, 52 insertions(+), 10 deletions(-) diff --git a/app/proc_info/main.c b/app/proc_info/main.c index 2f53e3caa..d508db957 100644 --- a/app/proc_info/main.c +++ b/app/proc_info/main.c @@ -636,7 +636,7 @@ main(int argc, char **argv) if (enabled_port_mask == 0) enabled_port_mask = 0xffff; - for (i = 0; i < nb_ports; i++) { + RTE_ETH_FOREACH_DEV(i) { if (enabled_port_mask & (1 << i)) { if (enable_stats) nic_stats_display(i); diff --git a/examples/skeleton/Makefile b/examples/skeleton/Makefile index bd980ec9b..a4a1860cb 100644 --- a/examples/skeleton/Makefile +++ b/examples/skeleton/Makefile @@ -23,6 +23,8 @@ CFLAGS += -O3 $(shell pkg-config --cflags libdpdk) LDFLAGS_SHARED = $(shell pkg-config --libs libdpdk) LDFLAGS_STATIC = -Wl,-Bstatic $(shell pkg-config --static --libs libdpdk) +CFLAGS += -DALLOW_EXPERIMENTAL_API + build/$(APP)-shared: $(SRCS-y) Makefile $(PC_FILE) | build $(CC) $(CFLAGS) $(SRCS-y) -o $@ $(LDFLAGS) $(LDFLAGS_SHARED) @@ -48,6 +50,7 @@ RTE_TARGET ?= x86_64-native-linuxapp-gcc include $(RTE_SDK)/mk/rte.vars.mk +CFLAGS += -DALLOW_EXPERIMENTAL_API CFLAGS += $(WERROR_FLAGS) # workaround for a gcc bug with noreturn attribute diff --git a/examples/skeleton/basicfwd.c b/examples/skeleton/basicfwd.c index e62cc0a59..1c1eb08ef 100644 --- a/examples/skeleton/basicfwd.c +++ b/examples/skeleton/basicfwd.c @@ -42,9 +42,6 @@ port_init(uint16_t port, struct rte_mempool *mbuf_pool) struct rte_eth_dev_info dev_info; struct rte_eth_txconf txconf; - if (port >= rte_eth_dev_count()) - return -1; - rte_eth_dev_info_get(port, &dev_info); if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) port_conf.txmode.offloads |= @@ -106,14 +103,13 @@ port_init(uint16_t port, struct rte_mempool *mbuf_pool) static __attribute__((noreturn)) void lcore_main(void) { - const uint16_t nb_ports = rte_eth_dev_count(); uint16_t port; /* * Check that the port is on the same NUMA node as the polling thread * for best performance. */ - for (port = 0; port < nb_ports; port++) + RTE_ETH_FOREACH_DEV(port) if (rte_eth_dev_socket_id(port) > 0 && rte_eth_dev_socket_id(port) != (int)rte_socket_id()) @@ -130,7 +126,7 @@ lcore_main(void) * Receive packets on a port and forward them on the paired * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc. */ - for (port = 0; port < nb_ports; port++) { + RTE_ETH_FOREACH_DEV(port) { /* Get burst of RX packets, from first port of pair. */ struct rte_mbuf *bufs[BURST_SIZE]; @@ -174,7 +170,7 @@ main(int argc, char *argv[]) argv += ret; /* Check that there is an even number of ports to send/receive on. */ - nb_ports = rte_eth_dev_count(); + nb_ports = rte_eth_dev_count_avail(); if (nb_ports < 2 || (nb_ports & 1)) rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n"); @@ -186,7 +182,7 @@ main(int argc, char *argv[]) rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); /* Initialize all ports. */ - for (portid = 0; portid < nb_ports; portid++) + RTE_ETH_FOREACH_DEV(portid) if (port_init(portid, mbuf_pool) != 0) rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16 "\n", portid); diff --git a/lib/librte_ether/rte_ethdev.c b/lib/librte_ether/rte_ethdev.c index 0590f0c10..583427559 100644 --- a/lib/librte_ether/rte_ethdev.c +++ b/lib/librte_ether/rte_ethdev.c @@ -529,6 +529,12 @@ rte_eth_dev_get_sec_ctx(uint8_t port_id) uint16_t rte_eth_dev_count(void) +{ + return rte_eth_dev_count_avail(); +} + +uint16_t +rte_eth_dev_count_avail(void) { uint16_t p; uint16_t count; @@ -541,6 +547,18 @@ rte_eth_dev_count(void) return count; } +uint16_t +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++; + + return count; +} + int rte_eth_dev_get_name_by_port(uint16_t port_id, char *name) { diff --git a/lib/librte_ether/rte_ethdev.h b/lib/librte_ether/rte_ethdev.h index 036153306..cba37fe34 100644 --- a/lib/librte_ether/rte_ethdev.h +++ b/lib/librte_ether/rte_ethdev.h @@ -1362,8 +1362,31 @@ int __rte_experimental rte_eth_dev_owner_get(const uint16_t port_id, * @return * - The total number of usable Ethernet devices. */ +__rte_deprecated uint16_t rte_eth_dev_count(void); +/** + * Get the number of ports which are usable for the application. + * + * These devices must be iterated by using the macro + * ``RTE_ETH_FOREACH_DEV`` or ``RTE_ETH_FOREACH_DEV_OWNED_BY`` + * to deal with non-contiguous ranges of devices. + * + * @return + * The count of available Ethernet devices. + */ +uint16_t __rte_experimental rte_eth_dev_count_avail(void); + +/** + * Get the total number of ports which are allocated. + * + * Some devices may not be available for the application. + * + * @return + * The total count of Ethernet devices. + */ +uint16_t __rte_experimental rte_eth_dev_count_total(void); + /** * Attach a new Ethernet device specified by arguments. * diff --git a/lib/librte_ether/rte_ethdev_version.map b/lib/librte_ether/rte_ethdev_version.map index 87f02fb74..c9a18ab5a 100644 --- a/lib/librte_ether/rte_ethdev_version.map +++ b/lib/librte_ether/rte_ethdev_version.map @@ -207,6 +207,8 @@ DPDK_18.02 { EXPERIMENTAL { global: + rte_eth_dev_count_avail; + rte_eth_dev_count_total; rte_eth_dev_is_removed; rte_eth_dev_owner_delete; rte_eth_dev_owner_get; @@ -229,4 +231,4 @@ EXPERIMENTAL { rte_mtr_stats_read; rte_mtr_stats_update; -} DPDK_17.11; +} DPDK_18.02;