From 7718bd104226b2444e1a3a7405e4daed9ac046f9 Mon Sep 17 00:00:00 2001
From: hkk <hankangkang5@huawei.com>
Date: Wed, 25 Sep 2024 15:01:02 +0800
Subject: [PATCH] Fill in a portion of mbuf to send_ring, when mbuf is
 insufficient.

---
 src/lstack/core/lstack_lwip.c              | 31 +++++++++++++++++++---
 src/lstack/include/lstack_protocol_stack.h |  2 +-
 2 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c
index 91f4838..7677e46 100644
--- a/src/lstack/core/lstack_lwip.c
+++ b/src/lstack/core/lstack_lwip.c
@@ -109,18 +109,43 @@ static struct pbuf *init_mbuf_to_pbuf(struct rte_mbuf *mbuf, pbuf_layer layer, u
     return pbuf;
 }
 
+static uint32_t update_replenish_mbuf_cnt(struct protocol_stack *stack, struct lwip_sock *sock)
+{
+    const uint32_t min_alloc_mbuf_num = 4;
+    struct rte_ring *ring = sock->send_ring;
+
+    uint32_t replenish_cnt = gazelle_ring_free_count(ring);
+    if (replenish_cnt <= min_alloc_mbuf_num) {
+        return replenish_cnt;
+    }
+
+    uint32_t resu = replenish_cnt;
+    uint32_t tcp_conn_count = get_global_cfg_params()->tcp_conn_count;
+    uint16_t send_ring_size = get_global_cfg_params()->send_ring_size;
+    uint16_t proportion = stack->conn_num / tcp_conn_count;
+    uint32_t replenish_mbuf_cnt_cal = (send_ring_size >> proportion);
+
+    if (replenish_mbuf_cnt_cal <= min_alloc_mbuf_num) {
+        resu = min_alloc_mbuf_num;
+    } else if (replenish_mbuf_cnt_cal < replenish_cnt) {
+        resu = replenish_mbuf_cnt_cal;
+    } else {
+        resu = replenish_cnt + 1;
+    }
+
+    return resu - 1;
+}
+
 /* true: need replenish again */
 static bool replenish_send_idlembuf(struct protocol_stack *stack, struct lwip_sock *sock)
 {
     void *pbuf[SOCK_SEND_RING_SIZE_MAX];
-
     struct rte_ring *ring = sock->send_ring;
 
-    uint32_t replenish_cnt = gazelle_ring_free_count(ring);
+    uint32_t replenish_cnt = update_replenish_mbuf_cnt(stack, sock);
     if (replenish_cnt == 0) {
         return false;
     }
-
     if (dpdk_alloc_pktmbuf(stack->rxtx_mbuf_pool, (struct rte_mbuf **)pbuf, replenish_cnt, true) != 0) {
         stack->stats.tx_allocmbuf_fail++;
         return true;
diff --git a/src/lstack/include/lstack_protocol_stack.h b/src/lstack/include/lstack_protocol_stack.h
index 8cb0020..4d10ac2 100644
--- a/src/lstack/include/lstack_protocol_stack.h
+++ b/src/lstack/include/lstack_protocol_stack.h
@@ -34,7 +34,7 @@
 #define SOCK_RECV_RING_SIZE_MAX     (2048)
 #define SOCK_SEND_RING_SIZE_MAX     (2048)
 
-#define MBUFPOOL_RESERVE_NUM (get_global_cfg_params()->rxqueue_size + 1024)
+#define MBUFPOOL_RESERVE_NUM (2 * get_global_cfg_params()->rxqueue_size + 1024)
 
 struct protocol_stack {
     uint32_t tid;
-- 
2.33.0

