From 5e315791df0bcdaa3383e14e7b93a5297fe0b49e Mon Sep 17 00:00:00 2001
From: Huisong Li <lihuisong@huawei.com>
Date: Fri, 3 Nov 2023 18:27:57 +0800
Subject: [PATCH 392/394] ethdev: add maximum Rx buffer size
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

[ upstream commit 75c7849a9dcca356985fdb87f2d11cae135dfb1a ]

The "min_rx_bufsize" in struct rte_eth_dev_info stands for the minimum
Rx buffer size supported by hardware. Actually, some engines also have
the maximum Rx buffer specification, like, hns3, i40e and so on. If mbuf
data room size in mempool is greater then the maximum Rx buffer size
per descriptor supported by HW, the data size application used in each
mbuf is just as much as the maximum Rx buffer size instead of the whole
data room size.

So introduce maximum Rx buffer size which is not enforced just to
report user to avoid memory waste. In addition, fix the comment for
the "min_rx_bufsize" to make it be more specific.

Signed-off-by: Huisong Li <lihuisong@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Ferruh Yigit <ferruh.yigit@amd.com>
---
 app/test-pmd/config.c   |  2 ++
 lib/ethdev/rte_ethdev.c |  8 ++++++++
 lib/ethdev/rte_ethdev.h | 10 +++++++++-
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/app/test-pmd/config.c b/app/test-pmd/config.c
index 9d7b10548e..fbb0cabf3d 100644
--- a/app/test-pmd/config.c
+++ b/app/test-pmd/config.c
@@ -848,6 +848,8 @@ port_infos_display(portid_t port_id)
 	}
 
 	printf("Minimum size of RX buffer: %u\n", dev_info.min_rx_bufsize);
+	if (dev_info.max_rx_bufsize != UINT32_MAX)
+		printf("Maximum size of RX buffer: %u\n", dev_info.max_rx_bufsize);
 	printf("Maximum configurable length of RX packet: %u\n",
 		dev_info.max_rx_pktlen);
 	printf("Maximum configurable size of LRO aggregated packet: %u\n",
diff --git a/lib/ethdev/rte_ethdev.c b/lib/ethdev/rte_ethdev.c
index 289fe45e6c..4702515240 100644
--- a/lib/ethdev/rte_ethdev.c
+++ b/lib/ethdev/rte_ethdev.c
@@ -2126,6 +2126,7 @@ rte_eth_rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
 	struct rte_eth_dev *dev;
 	struct rte_eth_dev_info dev_info;
 	struct rte_eth_rxconf local_conf;
+	uint32_t buf_data_size;
 
 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
 	dev = &rte_eth_devices[port_id];
@@ -2162,6 +2163,12 @@ rte_eth_rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
 			return -ENOSPC;
 		}
 		mbp_buf_size = rte_pktmbuf_data_room_size(mp);
+		buf_data_size = mbp_buf_size - RTE_PKTMBUF_HEADROOM;
+		if (buf_data_size > dev_info.max_rx_bufsize)
+			RTE_ETHDEV_LOG(DEBUG,
+				"For port_id=%u, the mbuf data buffer size (%u) is bigger than "
+				"max buffer size (%u) device can utilize, so mbuf size can be reduced.\n",
+				port_id, buf_data_size, dev_info.max_rx_bufsize);
 		if (mbp_buf_size < dev_info.min_rx_bufsize +
 				   RTE_PKTMBUF_HEADROOM) {
 			RTE_ETHDEV_LOG(ERR,
@@ -3531,6 +3538,7 @@ rte_eth_dev_info_get(uint16_t port_id, struct rte_eth_dev_info *dev_info)
 		RTE_ETHER_CRC_LEN;
 	dev_info->max_mtu = UINT16_MAX;
 	dev_info->rss_algo_capa = RTE_ETH_HASH_ALGO_CAPA_MASK(DEFAULT);
+	dev_info->max_rx_bufsize = UINT32_MAX;
 
 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
 	diag = (*dev->dev_ops->dev_infos_get)(dev, dev_info);
diff --git a/lib/ethdev/rte_ethdev.h b/lib/ethdev/rte_ethdev.h
index 09a546a48b..2880a55890 100644
--- a/lib/ethdev/rte_ethdev.h
+++ b/lib/ethdev/rte_ethdev.h
@@ -1825,7 +1825,15 @@ struct rte_eth_dev_info {
 	uint16_t min_mtu;	/**< Minimum MTU allowed */
 	uint16_t max_mtu;	/**< Maximum MTU allowed */
 	const uint32_t *dev_flags; /**< Device flags */
-	uint32_t min_rx_bufsize; /**< Minimum size of Rx buffer. */
+	/** Minimum Rx buffer size per descriptor supported by HW. */
+	uint32_t min_rx_bufsize;
+	/**
+	 * Maximum Rx buffer size per descriptor supported by HW.
+	 * The value is not enforced, information only to application to
+	 * optimize mbuf size.
+	 * Its value is UINT32_MAX when not specified by the driver.
+	 */
+	uint32_t max_rx_bufsize;
 	uint32_t max_rx_pktlen; /**< Maximum configurable length of Rx pkt. */
 	/** Maximum configurable size of LRO aggregated packet. */
 	uint32_t max_lro_pkt_size;
-- 
2.23.0

