From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <thomas@monjalon.net>
Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com
 [66.111.4.28]) by dpdk.org (Postfix) with ESMTP id 19C531B4F7
 for <dev@dpdk.org>; Fri, 30 Nov 2018 01:27:51 +0100 (CET)
Received: from compute1.internal (compute1.nyi.internal [10.202.2.41])
 by mailout.nyi.internal (Postfix) with ESMTP id 4360E231D3;
 Thu, 29 Nov 2018 19:27:50 -0500 (EST)
Received: from mailfrontend2 ([10.202.2.163])
 by compute1.internal (MEProxy); Thu, 29 Nov 2018 19:27:50 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=monjalon.net; h=
 from:to:cc:subject:date:message-id:mime-version
 :content-transfer-encoding; s=mesmtp; bh=crBkwYMRDDXjt9MARj3u38k
 8iuLvtwXxzh4CTBWL1gs=; b=hWyIXFQCW9gr42NlCxMwsIwFGxcfLXP7uI16mo4
 yo7fsPzi5DtAdwigvMHREfxtiIWdHooSKRgACGKGrczXMSXC8BypUmwOMQAzNS2p
 NCkQfaiW9PVAD00metkWuoCEguGBfT8xsrcBj4QyJAU9Gwp2s0GN/FG5NewXM33q
 ilDE=
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
 messagingengine.com; h=cc:content-transfer-encoding:date:from
 :message-id:mime-version:subject:to:x-me-proxy:x-me-proxy
 :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=crBkwYMRDDXjt9MAR
 j3u38k8iuLvtwXxzh4CTBWL1gs=; b=nUfxeGhkth/HATuYQDWqN9BpRRRDRgUSX
 eI7gAH6rST/ozgiiQo5YUD8BYIYT4uaSuUisBpQi7JBpbIpFp/zCGvFuzrjTM1F+
 KUVZmUEp7dWpgNmQhZqMPitoz3RFFTfROdyJ054mXSuS/lXA4v//yVH9mBK8FB4C
 S5PXcUTBn1nG9TBANJ/mXpM9miidyH9xSAuhq7yHujXhsx0ikUV4PsXzyzxiD0Dp
 u7OoihlvpVTgreXgt5uHLhC/cnP2KE40zf94wR5uvLXclCBipI+5RpxrSIcTBe+c
 FHqO94EmLX+eaSYHbyu023wJWQ75cXd2kAq3PW/UhAOS+iAEMeGLQ==
X-ME-Sender: <xms:BYQAXNbiX2ZnMGPYhrH2RxmU8Pt7Gzui51WR2vsQ8N-3EAXBh1KgDw>
X-ME-Proxy: <xmx:BYQAXKtE4IfRRExG263q44MPZODOOmeRAOlFqlO72VLUhQh4iwZc3w>
 <xmx:BYQAXHXpfQqWeCjsDA9hopKfYxbnKVomV5KEVE-vKVdr2-zLohmxXw>
 <xmx:BYQAXFLk6vjdBKU_zZGATRgVQLYRY9jVT0bezQi4EooOngmueL4x6Q>
 <xmx:BYQAXM9qDrFzWx50fXT0-APnOoHNNzUKDTUT6nd261N0hNLwd7cMdA>
 <xmx:BYQAXDOycy_ZUCzKabZUKWw3KqyEMjTlqPE5-mIox-y4aHhWP22IUQ>
 <xmx:BoQAXGFtAU7QYLMUCJiholVqEdmHOuWmAD_aplpYUBPh5C6gvB2okQ>
Received: from xps.monjalon.net (unknown [37.170.151.138])
 by mail.messagingengine.com (Postfix) with ESMTPA id 3EAE3102EE;
 Thu, 29 Nov 2018 19:27:46 -0500 (EST)
From: Thomas Monjalon <thomas@monjalon.net>
To: ferruh.yigit@intel.com,
	arybchenko@solarflare.com
Cc: dev@dpdk.org
Date: Fri, 30 Nov 2018 01:27:16 +0100
Message-Id: <20181130002716.27325-1-thomas@monjalon.net>
X-Mailer: git-send-email 2.19.0
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Subject: [dpdk-dev] [PATCH] ethdev: add siblings iterator
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Fri, 30 Nov 2018 00:27:51 -0000

If multiple ports share the same hardware device (rte_device),
they are siblings and can be found thanks to the new function
and loop macro.

The ownership is not checked because siblings may have
different owners.

Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
---
 lib/librte_ethdev/rte_ethdev.c           | 15 +++++++++++++++
 lib/librte_ethdev/rte_ethdev.h           | 23 +++++++++++++++++++++++
 lib/librte_ethdev/rte_ethdev_version.map |  1 +
 3 files changed, 39 insertions(+)

diff --git a/lib/librte_ethdev/rte_ethdev.c b/lib/librte_ethdev/rte_ethdev.c
index 5f858174b..11e0ade6e 100644
--- a/lib/librte_ethdev/rte_ethdev.c
+++ b/lib/librte_ethdev/rte_ethdev.c
@@ -341,6 +341,21 @@ rte_eth_find_next(uint16_t port_id)
 	return port_id;
 }
 
+uint16_t __rte_experimental
+rte_eth_find_next_sibling(uint16_t port_id, uint16_t ref)
+{
+	while (port_id < RTE_MAX_ETHPORTS &&
+			rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED &&
+			rte_eth_devices[port_id].device !=
+				rte_eth_devices[ref].device)
+		port_id++;
+
+	if (port_id >= RTE_MAX_ETHPORTS)
+		return RTE_MAX_ETHPORTS;
+
+	return port_id;
+}
+
 static void
 rte_eth_dev_shared_data_prepare(void)
 {
diff --git a/lib/librte_ethdev/rte_ethdev.h b/lib/librte_ethdev/rte_ethdev.h
index 1960f3a2d..647e6634d 100644
--- a/lib/librte_ethdev/rte_ethdev.h
+++ b/lib/librte_ethdev/rte_ethdev.h
@@ -1383,6 +1383,29 @@ uint16_t rte_eth_find_next(uint16_t port_id);
 #define RTE_ETH_FOREACH_DEV(p) \
 	RTE_ETH_FOREACH_DEV_OWNED_BY(p, RTE_ETH_DEV_NO_OWNER)
 
+/**
+ * Iterates over sibling ethdev ports (i.e. sharing the same rte_device).
+ *
+ * @param port_id_start
+ *   The id of the next possible valid sibling port.
+ * @param ref
+ *   The id of a reference port to compare rte_device with.
+ * @return
+ *   Next sibling port id (or ref itself), RTE_MAX_ETHPORTS if there is none.
+ */
+__rte_experimental
+uint16_t rte_eth_find_next_sibling(uint16_t port_id_start, uint16_t ref);
+
+/**
+ * Macro to iterate over all ethdev ports sharing the same rte_device
+ * as the specified port.
+ * Note: the specified port is part of the loop iterations.
+ */
+#define RTE_ETH_FOREACH_DEV_SIBLING(p, ref) \
+	for (p = rte_eth_find_next_sibling(0, ref); \
+		p < RTE_MAX_ETHPORTS; \
+		p = rte_eth_find_next_sibling(p + 1, ref))
+
 
 /**
  * @warning
diff --git a/lib/librte_ethdev/rte_ethdev_version.map b/lib/librte_ethdev/rte_ethdev_version.map
index 92ac3de25..a0c930d04 100644
--- a/lib/librte_ethdev/rte_ethdev_version.map
+++ b/lib/librte_ethdev/rte_ethdev_version.map
@@ -245,6 +245,7 @@ EXPERIMENTAL {
 	rte_eth_dev_owner_set;
 	rte_eth_dev_owner_unset;
 	rte_eth_dev_rx_intr_ctl_q_get_fd;
+	rte_eth_find_next_sibling;
 	rte_eth_switch_domain_alloc;
 	rte_eth_switch_domain_free;
 	rte_flow_conv;
-- 
2.19.0