From 27af442164cce5ccb7df65435f76262ebb2d810e Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Thu, 9 May 2024 15:27:48 +0800
Subject: [PATCH] fix mbuf leak in dpdk-23.11 due to kni removed

---
 src/lstack/core/lstack_dpdk.c           | 3 +++
 src/lstack/core/lstack_protocol_stack.c | 3 ++-
 src/lstack/netif/lstack_ethdev.c        | 2 ++
 src/ltran/ltran_forward.c               | 5 ++++-
 4 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c
index 016276a..ec35f9c 100644
--- a/src/lstack/core/lstack_dpdk.c
+++ b/src/lstack/core/lstack_dpdk.c
@@ -321,6 +321,9 @@ int32_t fill_mbuf_to_ring(struct rte_mempool *mempool, struct rte_ring *ring, ui
         ret = gazelle_ring_sp_enqueue(ring, (void **)free_buf, batch);
         if (ret == 0) {
             LSTACK_LOG(ERR, LSTACK, "cannot enqueue to ring, count: %u\n", batch);
+            for (int i = 0; i < batch; i++) {
+                rte_pktmbuf_free(free_buf[i]);
+            }
             return -1;
         }
 
diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c
index 04d54c5..892c16d 100644
--- a/src/lstack/core/lstack_protocol_stack.c
+++ b/src/lstack/core/lstack_protocol_stack.c
@@ -937,16 +937,17 @@ void stack_broadcast_arp(struct rte_mbuf *mbuf, struct protocol_stack *cur_stack
 
         ret = rpc_call_arp(&stack->rpc_queue, mbuf_copy);
         if (ret != 0) {
+            rte_pktmbuf_free(mbuf_copy);
             return;
         }
     }
+#if RTE_VERSION < RTE_VERSION_NUM(23, 11, 0, 0)
     ret = dpdk_alloc_pktmbuf(cur_stack->rxtx_mbuf_pool, &mbuf_copy, 1, true);
     if (ret != 0) {
         cur_stack->stats.rx_allocmbuf_fail++;
         return;
     }
     copy_mbuf(mbuf_copy, mbuf);
-#if RTE_VERSION < RTE_VERSION_NUM(23, 11, 0, 0)
     kni_handle_tx(mbuf_copy);
 #endif
     return;
diff --git a/src/lstack/netif/lstack_ethdev.c b/src/lstack/netif/lstack_ethdev.c
index 23edc19..048ea92 100644
--- a/src/lstack/netif/lstack_ethdev.c
+++ b/src/lstack/netif/lstack_ethdev.c
@@ -173,6 +173,8 @@ int32_t eth_dev_poll(void)
         } else if (transfer_type == TRANSFER_KERNEL) {
 #if RTE_VERSION < RTE_VERSION_NUM(23, 11, 0, 0)
             kni_handle_tx(stack->pkts[i]);
+#else
+            rte_pktmbuf_free(stack->pkts[i]);
 #endif
         } else {
             /* transfer to other thread */
diff --git a/src/ltran/ltran_forward.c b/src/ltran/ltran_forward.c
index a5756d7..d27f073 100644
--- a/src/ltran/ltran_forward.c
+++ b/src/ltran/ltran_forward.c
@@ -424,8 +424,11 @@ forward_to_kni:
 #if RTE_VERSION < RTE_VERSION_NUM(23, 11, 0, 0)
     if (get_ltran_config()->dpdk.kni_switch == GAZELLE_ON) {
         enqueue_rx_packet(get_kni_stack(), m);
-    }
+    } else
 #endif
+    {
+        rte_pktmbuf_free(m);
+    }
     return;
 }
 
-- 
2.33.0

