From 94e60e452e11fcc785fe44a66358608b5e2d90a7 Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Mon, 27 Nov 2023 21:41:13 +0800
Subject: [PATCH] stack: fix coredump caused by gazelleclt in rtc mode when
 stack num defined is greater than the hijacked thread num

---
 src/lstack/core/lstack_dpdk.c           |  7 +------
 src/lstack/core/lstack_protocol_stack.c | 20 ++++++++------------
 src/lstack/include/lstack_dpdk.h        |  2 +-
 3 files changed, 10 insertions(+), 19 deletions(-)

diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c
index 19a7bf4..839516e 100644
--- a/src/lstack/core/lstack_dpdk.c
+++ b/src/lstack/core/lstack_dpdk.c
@@ -205,13 +205,8 @@ static struct reg_ring_msg *create_reg_mempool(const char *name, uint16_t queue_
     return reg_buf;
 }
 
-int32_t pktmbuf_pool_init(struct protocol_stack *stack, uint16_t stack_num)
+int32_t pktmbuf_pool_init(struct protocol_stack *stack)
 {
-    if (stack_num == 0) {
-        LSTACK_LOG(ERR, LSTACK, "stack_num=0.\n");
-        return -1;
-    }
-
     stack->rxtx_pktmbuf_pool = get_pktmbuf_mempool("rxtx_mbuf", stack->queue_id);
     if (stack->rxtx_pktmbuf_pool == NULL) {
         LSTACK_LOG(ERR, LSTACK, "rxtx_pktmbuf_pool is NULL\n");
diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c
index dfebfcc..baacaa9 100644
--- a/src/lstack/core/lstack_protocol_stack.c
+++ b/src/lstack/core/lstack_protocol_stack.c
@@ -349,7 +349,7 @@ static int32_t init_stack_value(struct protocol_stack *stack, void *arg)
         return -1;
     }
 
-    if (pktmbuf_pool_init(stack, stack_group->stack_num) != 0) {
+    if (pktmbuf_pool_init(stack) != 0) {
         LSTACK_LOG(ERR, LSTACK, "pktmbuf_pool_init failed\n");
         return -1;
     }
@@ -529,7 +529,7 @@ static void gazelle_listen_thread(void *arg)
 int32_t stack_group_init_mempool(void)
 {
     struct cfg_params *global_cfg_parmas = get_global_cfg_params();
-    struct protocol_stack_group *stack_group = get_protocol_stack_group();
+    uint32_t total_mbufs = get_global_cfg_params()->mbuf_count_per_conn * get_global_cfg_params()->tcp_conn_count;
     struct rte_mempool *rxtx_mbuf = NULL;
     uint32_t cpu_id = 0;
     unsigned numa_id = 0;
@@ -537,10 +537,8 @@ int32_t stack_group_init_mempool(void)
 
     LSTACK_LOG(INFO, LSTACK,
         "config::num_cpu=%d num_process=%d \n", global_cfg_parmas->num_cpu, global_cfg_parmas->num_process);
-
-    uint32_t total_mbufs = get_global_cfg_params()->mbuf_count_per_conn * get_global_cfg_params()->tcp_conn_count;
     
-    for (int cpu_idx = 0; cpu_idx < global_cfg_parmas->num_cpu; cpu_idx++) {
+    for (int cpu_idx = 0; cpu_idx < get_global_cfg_params()->num_queue; cpu_idx++) {
         cpu_id = global_cfg_parmas->cpus[cpu_idx];
         numa_id = numa_node_of_cpu(cpu_id);
         
@@ -552,7 +550,7 @@ int32_t stack_group_init_mempool(void)
             }
             
             rxtx_mbuf = create_pktmbuf_mempool(
-                "rxtx_mbuf", total_mbufs / stack_group->stack_num, RXTX_CACHE_SZ, queue_id, numa_id);
+                "rxtx_mbuf", total_mbufs / get_global_cfg_params()->num_queue, RXTX_CACHE_SZ, queue_id, numa_id);
             if (rxtx_mbuf == NULL) {
                 LSTACK_LOG(ERR, LSTACK, "cpuid=%u, numid=%d , rxtx_mbuf idx= %d create_pktmbuf_mempool fail\n",
                     cpu_id, numa_id, queue_id);
@@ -569,11 +567,7 @@ int32_t stack_group_init_mempool(void)
 int32_t stack_group_init(void)
 {
     struct protocol_stack_group *stack_group = get_protocol_stack_group();
-    if (!get_global_cfg_params()->seperate_send_recv) {
-        stack_group->stack_num = get_global_cfg_params()->num_cpu;
-    } else {
-        stack_group->stack_num = get_global_cfg_params()->num_cpu * 2;
-    }
+    stack_group->stack_num = 0;
 
     init_list_node(&stack_group->poll_list);
     pthread_spin_init(&stack_group->poll_list_lock, PTHREAD_PROCESS_PRIVATE);
@@ -627,6 +621,7 @@ int32_t stack_setup_app_thread(void)
         LSTACK_LOG(INFO, LSTACK, "stack setup failed in app thread\n");
         return -1;
     }
+    atomic_fetch_add(&g_stack_group.stack_num, 1);
 
     return 0;
 }
@@ -670,10 +665,11 @@ int32_t stack_setup_thread(void)
     }
 
     /* 2: wait stack thread and kernel_event thread init finish */
-    wait_sem_value(&g_stack_group.sem_stack_setup, g_stack_group.stack_num * 2);
+    wait_sem_value(&g_stack_group.sem_stack_setup, queue_num * 2);
     if (g_stack_group.stack_setup_fail) {
         return -1;
     }
+    g_stack_group.stack_num = queue_num;
 
     return 0;
 }
diff --git a/src/lstack/include/lstack_dpdk.h b/src/lstack/include/lstack_dpdk.h
index e3daefa..6554a7e 100644
--- a/src/lstack/include/lstack_dpdk.h
+++ b/src/lstack/include/lstack_dpdk.h
@@ -43,7 +43,7 @@ struct rte_ring;
 struct rte_mbuf;
 int32_t fill_mbuf_to_ring(struct rte_mempool *mempool, struct rte_ring *ring, uint32_t mbuf_num);
 int32_t dpdk_eal_init(void);
-int32_t pktmbuf_pool_init(struct protocol_stack *stack, uint16_t stack_num);
+int32_t pktmbuf_pool_init(struct protocol_stack *stack);
 struct rte_ring *create_ring(const char *name, uint32_t count, uint32_t flags, int32_t queue_id);
 struct rte_mempool *create_mempool(const char *name, uint32_t count, uint32_t size,
                                    uint32_t flags, int32_t idx);
-- 
2.27.0

