From aa0bd688a6dae115e136771fda747c9428e65561 Mon Sep 17 00:00:00 2001
From: yangchen <yangchen145@huawei.com>
Date: Tue, 2 Jan 2024 09:32:00 +0800
Subject: [PATCH] lstack_dpdk: limit mbuf max num

---
 src/lstack/core/lstack_dpdk.c    |  5 +++++
 src/lstack/include/lstack_dpdk.h | 18 ++++++++----------
 src/ltran/main.c                 |  2 +-
 3 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c
index 729a84c..580dd1d 100644
--- a/src/lstack/core/lstack_dpdk.c
+++ b/src/lstack/core/lstack_dpdk.c
@@ -155,6 +155,11 @@ struct rte_mempool *create_pktmbuf_mempool(const char *name, uint32_t nb_mbuf,
         LSTACK_LOG(ERR, LSTACK, "snprintf_s fail ret=%d \n", ret);
         return NULL;
     }
+    /* limit mbuf max num based on the dpdk capability */
+    if (nb_mbuf > MBUF_MAX_NUM) {
+        LSTACK_LOG(ERR, LSTACK, "out of the dpdk mbuf quantity range\n");
+        return NULL;
+    }
 
     /* time stamp before pbuf_custom as priv_data */
     uint16_t private_size = RTE_ALIGN(sizeof(struct mbuf_private), RTE_CACHE_LINE_SIZE);
diff --git a/src/lstack/include/lstack_dpdk.h b/src/lstack/include/lstack_dpdk.h
index 4a160e0..2a44c6e 100644
--- a/src/lstack/include/lstack_dpdk.h
+++ b/src/lstack/include/lstack_dpdk.h
@@ -17,22 +17,20 @@
 #include "gazelle_opt.h"
 #include "gazelle_dfx_msg.h"
 
-#define RXTX_CACHE_SZ       (VDEV_RX_QUEUE_SZ)
-#define KNI_NB_MBUF         (DEFAULT_RING_SIZE << 4)
+#define RXTX_CACHE_SZ        (VDEV_RX_QUEUE_SZ)
 
-#define RESERVE_NIC_RECV    (1024)
+#define KNI_NB_MBUF          (DEFAULT_RING_SIZE << 4)
 
-#define MBUF_HEADER_LEN     64
-
-#define MAX_PACKET_SZ       2048
+#define MAX_PACKET_SZ        2048
 
 #define RING_SIZE(x)         ((x) - 1)
 
-#define MBUF_SZ (MAX_PACKET_SZ + RTE_PKTMBUF_HEADROOM)
+#define MBUF_SZ              (MAX_PACKET_SZ + RTE_PKTMBUF_HEADROOM)
 
-#define MAX_CORE_NUM            256
-#define CALL_MSG_RING_SIZE      (unsigned long long)32
-#define CALL_CACHE_SZ           0
+/* DPDK limit ring head-tail distance in rte_ring_init.
+ * Max value is RTE_RING_SZ_MASK / HTD_MAX_DEF, RTE_RING_SZ_MASK is 0x7fffffff, HTD_MAX_DEF is 8.
+ */
+#define MBUF_MAX_NUM         0xfffffff
 
 int thread_affinity_default(void);
 int thread_affinity_init(int cpu_id);
diff --git a/src/ltran/main.c b/src/ltran/main.c
index 87f1e14..c2598a7 100644
--- a/src/ltran/main.c
+++ b/src/ltran/main.c
@@ -55,7 +55,7 @@ static void print_stack(void)
 
 static void sig_default_handler(int32_t sig)
 {
-    LTRAN_ERR("ltran dumped，caught signal：%d.\n", sig);
+    LTRAN_ERR("ltran dumped, caught signal: %d.\n", sig);
     print_stack();
     dpdk_kni_release();
     int ret = 0;
-- 
2.33.0

