From 24deab28953f33ab4dc1596291289b91b805b4de Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Thu, 26 Oct 2023 19:36:02 +0800
Subject: [PATCH] stack: add semaphore to ensure all stack threads setup
 success in rtw mode before call main()

---
 src/lstack/core/lstack_protocol_stack.c    | 19 +++++++++++++++++--
 src/lstack/include/lstack_protocol_stack.h |  2 ++
 src/lstack/netif/lstack_ethdev.c           |  4 ++--
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c
index 0726054..27e5005 100644
--- a/src/lstack/core/lstack_protocol_stack.c
+++ b/src/lstack/core/lstack_protocol_stack.c
@@ -289,6 +289,7 @@ static void* gazelle_kernelevent_thread(void *arg)
 
     LSTACK_LOG(INFO, LSTACK, "kernelevent_%02hu start\n", idx);
     free(arg);
+    sem_post(&g_stack_group.sem_stack_setup);
 
     for (;;) {
         stack->kernel_event_num = posix_api->epoll_wait_fn(stack->epollfd, stack->kernel_events, KERNEL_EPOLL_MAX, -1);
@@ -493,9 +494,12 @@ static void* gazelle_stack_thread(void *arg)
     free(arg);
     if (stack == NULL) {
         LSTACK_LOG(ERR, LSTACK, "stack_thread_init failed queue_id=%hu\n", queue_id);
-        /* exit in signal thread */
-        raise(SIGTERM);
+        g_stack_group.stack_setup_fail = 1;
+        sem_post(&g_stack_group.sem_stack_setup);
+        return NULL;
     }
+    sem_post(&g_stack_group.sem_stack_setup);
+
     if (!use_ltran() && queue_id == 0) {
         init_listen_and_user_ports();
     }
@@ -531,6 +535,11 @@ int32_t stack_group_init(void)
     init_list_node(&stack_group->poll_list);
     pthread_spin_init(&stack_group->poll_list_lock, PTHREAD_PROCESS_PRIVATE);
     pthread_spin_init(&stack_group->socket_lock, PTHREAD_PROCESS_PRIVATE);
+    if (sem_init(&stack_group->sem_stack_setup, 0, 0) < 0) {
+        LSTACK_LOG(ERR, LSTACK, "sem_init failed errno=%d\n", errno);
+        return -1;
+    }
+    stack_group->stack_setup_fail = 0;
 
     if (get_global_cfg_params()->is_primary) {
         uint32_t total_mbufs = get_global_cfg_params()->mbuf_count_per_conn * get_global_cfg_params()->tcp_conn_count;
@@ -622,6 +631,12 @@ 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);
+    if (g_stack_group.stack_setup_fail) {
+        return -1;
+    }
+
     return 0;
 }
 
diff --git a/src/lstack/include/lstack_protocol_stack.h b/src/lstack/include/lstack_protocol_stack.h
index 4f1b127..2c581b3 100644
--- a/src/lstack/include/lstack_protocol_stack.h
+++ b/src/lstack/include/lstack_protocol_stack.h
@@ -96,6 +96,8 @@ struct protocol_stack_group {
     pthread_spinlock_t poll_list_lock;
     sem_t sem_listen_thread;
     struct rte_mempool *total_rxtx_pktmbuf_pool[PROTOCOL_STACK_MAX];
+    sem_t sem_stack_setup;
+    bool stack_setup_fail;
 
     /* dfx stats */
     bool latency_start;
diff --git a/src/lstack/netif/lstack_ethdev.c b/src/lstack/netif/lstack_ethdev.c
index 30c3c9a..b0dbba4 100644
--- a/src/lstack/netif/lstack_ethdev.c
+++ b/src/lstack/netif/lstack_ethdev.c
@@ -319,7 +319,7 @@ struct rte_flow *create_flow_director(uint16_t port_id, uint16_t queue_id,
     if (!res) {
         flow = rte_flow_create(port_id, &attr, pattern, action, error);
     } else {
-        LSTACK_LOG(ERR, PORT, "rte_flow_create.rte_flow_validate error, res %d \n", res);
+        LSTACK_LOG(ERR, LSTACK, "rte_flow_create.rte_flow_validate error, res %d \n", res);
     }
 
     return flow;
@@ -364,7 +364,7 @@ void delete_flow_director(uint32_t dst_ip, uint16_t src_port, uint16_t dst_port)
         struct rte_flow_error error;
         int ret = rte_flow_destroy(port_id, fl->flow, &error);
         if(ret != 0){
-            LSTACK_LOG(ERR, PORT, "Flow can't be delete %d message: %s\n",
+            LSTACK_LOG(ERR, LSTACK, "Flow can't be delete %d message: %s\n",
                        error.type, error.message ? error.message : "(no stated reason)");
         }
         delete_rule(rule_key);
-- 
2.27.0

