From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 8833343894;
	Thu, 11 Jan 2024 16:32:08 +0100 (CET)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 0ED4F40608;
	Thu, 11 Jan 2024 16:32:08 +0100 (CET)
Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com
 [67.231.148.174])
 by mails.dpdk.org (Postfix) with ESMTP id 1C24240266
 for <dev@dpdk.org>; Thu, 11 Jan 2024 16:32:05 +0100 (CET)
Received: from pps.filterd (m0045849.ppops.net [127.0.0.1])
 by mx0a-0016f401.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id
 40BDtCx8018715; Thu, 11 Jan 2024 07:32:00 -0800
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=
 from:to:cc:subject:date:message-id:in-reply-to:references
 :mime-version:content-transfer-encoding:content-type; s=
 pfpt0220; bh=XMVYYSEVNGZhb9l72rurC6qjZMswKaI7Ga0vJwSv4m4=; b=Js7
 HwP4W7YUfbDSQDz0+gFiTsZILJUgv2mwOIy15qzgw6vPmLs3dxWlmhnY45c+qi/m
 O6ULQcKd8smVy87/RraZ+ypKwVZq2Sp/JP+fb24G8QeUGIwxcrA11CZPp/56KKGg
 Cp7iKr1NvgY/VUumsHAw0LP+pfyfRtjh/owc5wqgO4UeB/kcmnHriYECo3VJFnxx
 idRBavAAyj0v6hRzXAgHX4Tl4N8CKoaWt7374+4pGpwBxqR8dOPfdXu6mzPH105p
 RneUoCoszaBQT3q3p/kILt7XJWfwmfn+/A83S0L2xyDkwEhLahsKbXeX6ZxiYrW4
 ZgfOKgG4yte0MfmX2hw==
Received: from dc5-exch02.marvell.com ([199.233.59.182])
 by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3vjhmxrcpb-2
 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT);
 Thu, 11 Jan 2024 07:32:00 -0800 (PST)
Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH02.marvell.com
 (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48;
 Thu, 11 Jan 2024 07:31:57 -0800
Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com
 (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.48 via Frontend
 Transport; Thu, 11 Jan 2024 07:31:57 -0800
Received: from jerin-lab.marvell.com (jerin-lab.marvell.com [10.28.34.14])
 by maili.marvell.com (Postfix) with ESMTP id E58F53FAF48;
 Thu, 11 Jan 2024 07:18:12 -0800 (PST)
From: <jerinj@marvell.com>
To: <dev@dpdk.org>, Thomas Monjalon <thomas@monjalon.net>, Ferruh Yigit
 <ferruh.yigit@amd.com>, Andrew Rybchenko <andrew.rybchenko@oktetlabs.ru>
CC: <ferruh.yigit@xilinx.com>, <ajit.khaparde@broadcom.com>,
 <aboyer@pensando.io>, <beilei.xing@intel.com>,
 <bruce.richardson@intel.com>, <chas3@att.com>, <chenbo.xia@intel.com>,
 <ciara.loftus@intel.com>, <dsinghrawat@marvell.com>,
 <ed.czeck@atomicrules.com>, <evgenys@amazon.com>, <grive@u256.net>,
 <g.singh@nxp.com>, <zhouguoyang@huawei.com>, <haiyue.wang@intel.com>,
 <hkalra@marvell.com>, <heinrich.kuhn@corigine.com>,
 <hemant.agrawal@nxp.com>, <hyonkim@cisco.com>, <igorch@amazon.com>,
 <irusskikh@marvell.com>, <jgrajcia@cisco.com>,
 <jasvinder.singh@intel.com>, <jianwang@trustnetic.com>,
 <jiawenwu@trustnetic.com>, <jingjing.wu@intel.com>,
 <johndale@cisco.com>, <john.miller@atomicrules.com>,
 <linville@tuxdriver.com>, <keith.wiles@intel.com>,
 <kirankumark@marvell.com>, <oulijun@huawei.com>, <lironh@marvell.com>,
 <longli@microsoft.com>, <mw@semihalf.com>, <spinler@cesnet.cz>,
 <matan@nvidia.com>, <matt.peters@windriver.com>,
 <maxime.coquelin@redhat.com>, <mk@semihalf.com>, <humin29@huawei.com>,
 <pnalla@marvell.com>, <ndabilpuram@marvell.com>,
 <qiming.yang@intel.com>, <qi.z.zhang@intel.com>, <radhac@marvell.com>,
 <rahul.lakkireddy@chelsio.com>, <rmody@marvell.com>,
 <rosen.xu@intel.com>, <sachin.saxena@oss.nxp.com>,
 <skoteshwar@marvell.com>, <shshaikh@marvell.com>,
 <shaibran@amazon.com>, <shepard.siegel@atomicrules.com>,
 <asomalap@amd.com>, <somnath.kotur@broadcom.com>,
 <sthemmin@microsoft.com>, <steven.webster@windriver.com>,
 <skori@marvell.com>, <mtetsuyah@gmail.com>, <vburru@marvell.com>,
 <viacheslavo@nvidia.com>, <xiao.w.wang@intel.com>,
 <cloud.wangxiaoyun@huawei.com>, <yisen.zhuang@huawei.com>,
 <yongwang@vmware.com>, <xuanziyang2@huawei.com>,
 <cristian.dumitrescu@intel.com>, Jerin Jacob <jerinj@marvell.com>
Subject: [dpdk-dev] [v1] ethdev: support Tx queue used count
Date: Thu, 11 Jan 2024 20:47:44 +0530
Message-ID: <20240111151745.3800170-1-jerinj@marvell.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20231219172948.3909749-1-jerinj@marvell.com>
References: <20231219172948.3909749-1-jerinj@marvell.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain
X-Proofpoint-GUID: FME5Ui3RJSxCRu-SbJ5GNqbcEgZzEdPa
X-Proofpoint-ORIG-GUID: FME5Ui3RJSxCRu-SbJ5GNqbcEgZzEdPa
X-Proofpoint-Virus-Version: vendor=baseguard
 engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26
 definitions=2023-12-09_02,2023-12-07_01,2023-05-22_02
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
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>
Errors-To: dev-bounces@dpdk.org

From: Jerin Jacob <jerinj@marvell.com>

Introduce a new API to retrieve the number of used descriptors
in a Tx queue. Applications can leverage this API in the fast path to
inspect the Tx queue occupancy and take appropriate actions based on the
available free descriptors.

A notable use case could be implementing Random Early Discard (RED)
in software based on Tx queue occupancy.

Signed-off-by: Jerin Jacob <jerinj@marvell.com>
---
 doc/guides/nics/features.rst         | 10 ++++
 doc/guides/nics/features/default.ini |  1 +
 lib/ethdev/ethdev_driver.h           |  2 +
 lib/ethdev/ethdev_private.c          |  1 +
 lib/ethdev/ethdev_trace_points.c     |  3 ++
 lib/ethdev/rte_ethdev.h              | 74 ++++++++++++++++++++++++++++
 lib/ethdev/rte_ethdev_core.h         |  7 ++-
 lib/ethdev/rte_ethdev_trace_fp.h     |  8 +++
 lib/ethdev/version.map               |  3 ++
 9 files changed, 108 insertions(+), 1 deletion(-)

rfc..v1:
- Updated API similar to rte_eth_rx_queue_count() where it returns
"used" count instead of "free" count

diff --git a/doc/guides/nics/features.rst b/doc/guides/nics/features.rst
index f7d9980849..0d5a8733fc 100644
--- a/doc/guides/nics/features.rst
+++ b/doc/guides/nics/features.rst
@@ -962,6 +962,16 @@ management (see :doc:`../prog_guide/power_man` for more details).
 
 * **[implements] eth_dev_ops**: ``get_monitor_addr``
 
+.. _nic_features_tx_queue_used_count:
+
+Tx queue count
+--------------
+
+Supports to get the number of used descriptors of a Tx queue.
+
+* **[implements] eth_dev_ops**: ``tx_queue_count``.
+* **[related] API**: ``rte_eth_tx_queue_count()``.
+
 .. _nic_features_other:
 
 Other dev ops not represented by a Feature
diff --git a/doc/guides/nics/features/default.ini b/doc/guides/nics/features/default.ini
index 806cb033ff..3ef6d45c0e 100644
--- a/doc/guides/nics/features/default.ini
+++ b/doc/guides/nics/features/default.ini
@@ -59,6 +59,7 @@ Packet type parsing  =
 Timesync             =
 Rx descriptor status =
 Tx descriptor status =
+Tx queue count       =
 Basic stats          =
 Extended stats       =
 Stats per queue      =
diff --git a/lib/ethdev/ethdev_driver.h b/lib/ethdev/ethdev_driver.h
index b482cd12bb..f05f68a67c 100644
--- a/lib/ethdev/ethdev_driver.h
+++ b/lib/ethdev/ethdev_driver.h
@@ -58,6 +58,8 @@ struct rte_eth_dev {
 	eth_rx_queue_count_t rx_queue_count;
 	/** Check the status of a Rx descriptor */
 	eth_rx_descriptor_status_t rx_descriptor_status;
+	/** Get the number of used Tx descriptors */
+	eth_tx_queue_count_t tx_queue_count;
 	/** Check the status of a Tx descriptor */
 	eth_tx_descriptor_status_t tx_descriptor_status;
 	/** Pointer to PMD transmit mbufs reuse function */
diff --git a/lib/ethdev/ethdev_private.c b/lib/ethdev/ethdev_private.c
index a656df293c..626524558a 100644
--- a/lib/ethdev/ethdev_private.c
+++ b/lib/ethdev/ethdev_private.c
@@ -273,6 +273,7 @@ eth_dev_fp_ops_setup(struct rte_eth_fp_ops *fpo,
 	fpo->tx_pkt_prepare = dev->tx_pkt_prepare;
 	fpo->rx_queue_count = dev->rx_queue_count;
 	fpo->rx_descriptor_status = dev->rx_descriptor_status;
+	fpo->tx_queue_count = dev->tx_queue_count;
 	fpo->tx_descriptor_status = dev->tx_descriptor_status;
 	fpo->recycle_tx_mbufs_reuse = dev->recycle_tx_mbufs_reuse;
 	fpo->recycle_rx_descriptors_refill = dev->recycle_rx_descriptors_refill;
diff --git a/lib/ethdev/ethdev_trace_points.c b/lib/ethdev/ethdev_trace_points.c
index 91f71d868b..e618414392 100644
--- a/lib/ethdev/ethdev_trace_points.c
+++ b/lib/ethdev/ethdev_trace_points.c
@@ -481,6 +481,9 @@ RTE_TRACE_POINT_REGISTER(rte_eth_trace_count_aggr_ports,
 RTE_TRACE_POINT_REGISTER(rte_eth_trace_map_aggr_tx_affinity,
 	lib.ethdev.map_aggr_tx_affinity)
 
+RTE_TRACE_POINT_REGISTER(rte_eth_trace_tx_queue_count,
+	lib.ethdev.tx_queue_count)
+
 RTE_TRACE_POINT_REGISTER(rte_flow_trace_copy,
 	lib.ethdev.flow.copy)
 
diff --git a/lib/ethdev/rte_ethdev.h b/lib/ethdev/rte_ethdev.h
index 21e3a21903..af59da9652 100644
--- a/lib/ethdev/rte_ethdev.h
+++ b/lib/ethdev/rte_ethdev.h
@@ -6803,6 +6803,80 @@ rte_eth_recycle_mbufs(uint16_t rx_port_id, uint16_t rx_queue_id,
 __rte_experimental
 int rte_eth_buffer_split_get_supported_hdr_ptypes(uint16_t port_id, uint32_t *ptypes, int num);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
+ *
+ * Get the number of used descriptors of a Tx queue
+ *
+ * This function retrieves the number of used descriptors of a transmit queue.
+ * Applications can use this API in the fast path to inspect Tx queue occupancy and take
+ * appropriate actions based on the available free descriptors.
+ * An example action could be implementing the Random Early Discard (RED).
+ *
+ * Since it's a fast-path function, no check is performed on port_id and
+ * tx_queue_id. The caller must therefore ensure that the port is enabled
+ * and the queue is configured and running.
+ *
+ * @param port_id
+ *   The port identifier of the device.
+ * @param tx_queue_id
+ *   The index of the transmit queue.
+ *   The value must be in the range [0, nb_tx_queue - 1] previously supplied
+ *   to rte_eth_dev_configure().
+ * @return
+ *  The number of used descriptors in the specific queue, or:
+ *   - (-ENODEV) if *port_id* is invalid. Enabled only when RTE_ETHDEV_DEBUG_TX is enabled
+ *   - (-EINVAL) if *queue_id* is invalid. Enabled only when RTE_ETHDEV_DEBUG_TX is enabled
+ *   - (-ENOTSUP) if the device does not support this function.
+ *
+ * @note This function is designed for fast-path use.
+ */
+__rte_experimental
+static inline int
+rte_eth_tx_queue_count(uint16_t port_id, uint16_t tx_queue_id)
+{
+	struct rte_eth_fp_ops *fops;
+	void *qd;
+	int rc;
+
+#ifdef RTE_ETHDEV_DEBUG_TX
+	if (port_id >= RTE_MAX_ETHPORTS || !rte_eth_dev_is_valid_port(port_id)) {
+		RTE_ETHDEV_LOG_LINE(ERR, "Invalid port_id=%u", port_id);
+		rc = -ENODEV;
+		rte_eth_trace_tx_queue_count(port_id, tx_queue_id, rc);
+		return rc;
+	}
+
+	rc = -EINVAL;
+	if (tx_queue_id >= RTE_MAX_QUEUES_PER_PORT) {
+		RTE_ETHDEV_LOG_LINE(ERR, "Invalid Tx queue_id=%u for port_id=%u",
+				    tx_queue_id, port_id);
+		rte_eth_trace_tx_queue_count(port_id, tx_queue_id, rc);
+		return rc;
+	}
+#endif
+
+	/* Fetch pointer to Tx queue data */
+	fops = &rte_eth_fp_ops[port_id];
+	qd = fops->txq.data[tx_queue_id];
+
+#ifdef RTE_ETHDEV_DEBUG_TX
+	if (qd == NULL) {
+		RTE_ETHDEV_LOG_LINE(ERR, "Invalid Tx queue_id=%u for port_id=%u",
+				    tx_queue_id, port_id);
+		rte_eth_trace_tx_queue_count(port_id, tx_queue_id, rc);
+		return rc;
+	}
+#endif
+	if (fops->tx_queue_count == NULL)
+		return -ENOTSUP;
+
+	rc = fops->tx_queue_count(qd);
+	rte_eth_trace_tx_queue_count(port_id, tx_queue_id, rc);
+
+	return rc;
+}
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/ethdev/rte_ethdev_core.h b/lib/ethdev/rte_ethdev_core.h
index 4bfaf79c6c..d3f09f390d 100644
--- a/lib/ethdev/rte_ethdev_core.h
+++ b/lib/ethdev/rte_ethdev_core.h
@@ -60,6 +60,9 @@ typedef uint16_t (*eth_recycle_tx_mbufs_reuse_t)(void *txq,
 /** @internal Refill Rx descriptors with the recycling mbufs */
 typedef void (*eth_recycle_rx_descriptors_refill_t)(void *rxq, uint16_t nb);
 
+/** @internal Get number of used descriptors on a transmit queue. */
+typedef int (*eth_tx_queue_count_t)(void *txq);
+
 /**
  * @internal
  * Structure used to hold opaque pointers to internal ethdev Rx/Tx
@@ -116,7 +119,9 @@ struct rte_eth_fp_ops {
 	eth_tx_descriptor_status_t tx_descriptor_status;
 	/** Copy used mbufs from Tx mbuf ring into Rx. */
 	eth_recycle_tx_mbufs_reuse_t recycle_tx_mbufs_reuse;
-	uintptr_t reserved2[2];
+	/** Get the number of used Tx descriptors. */
+	eth_tx_queue_count_t tx_queue_count;
+	uintptr_t reserved2[1];
 	/**@}*/
 
 } __rte_cache_aligned;
diff --git a/lib/ethdev/rte_ethdev_trace_fp.h b/lib/ethdev/rte_ethdev_trace_fp.h
index 186271c9ff..c98c488433 100644
--- a/lib/ethdev/rte_ethdev_trace_fp.h
+++ b/lib/ethdev/rte_ethdev_trace_fp.h
@@ -73,6 +73,14 @@ RTE_TRACE_POINT_FP(
 	rte_trace_point_emit_u64(count);
 )
 
+RTE_TRACE_POINT_FP(
+	rte_eth_trace_tx_queue_count,
+	RTE_TRACE_POINT_ARGS(uint16_t port_id, uint16_t tx_queue_id, int rc),
+	rte_trace_point_emit_u16(port_id);
+	rte_trace_point_emit_u16(tx_queue_id);
+	rte_trace_point_emit_int(rc);
+)
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/ethdev/version.map b/lib/ethdev/version.map
index 5c4917c020..e03830902a 100644
--- a/lib/ethdev/version.map
+++ b/lib/ethdev/version.map
@@ -316,6 +316,9 @@ EXPERIMENTAL {
 	rte_eth_recycle_rx_queue_info_get;
 	rte_flow_group_set_miss_actions;
 	rte_flow_calc_table_hash;
+
+	# added in 24.03
+	rte_eth_tx_queue_count;
 };
 
 INTERNAL {
-- 
2.43.0