From 47ab261c2b533975a59b34f06da0ccf83692bb78 Mon Sep 17 00:00:00 2001
From: compile_success <980965867@qq.com>
Date: Tue, 26 Dec 2023 16:53:58 +0000
Subject: [PATCH] use default nonblock mode

---
 src/lstack/core/lstack_cfg.c            | 12 ++++++++++++
 src/lstack/core/lstack_protocol_stack.c |  5 ++++-
 src/lstack/include/lstack_cfg.h         |  1 +
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c
index a292070..d9c23fb 100644
--- a/src/lstack/core/lstack_cfg.c
+++ b/src/lstack/core/lstack_cfg.c
@@ -80,6 +80,7 @@ static int32_t parse_nic_rxqueue_size(void);
 static int32_t parse_nic_txqueue_size(void);
 static int32_t parse_stack_thread_mode(void);
 static int32_t parse_nic_vlan_mode(void);
+static int32_t parse_defaule_nonblock_mode(void);
 
 #define PARSE_ARG(_arg, _arg_string, _default_val, _min_val, _max_val, _ret) \
     do { \
@@ -142,6 +143,7 @@ static struct config_vector_t g_config_tbl[] = {
     { "nic_txqueue_size", parse_nic_txqueue_size},
     { "stack_thread_mode", parse_stack_thread_mode },
     { "nic_vlan_mode", parse_nic_vlan_mode },
+    { "nonblock_mode", parse_defaule_nonblock_mode },
     { NULL,           NULL }
 };
 
@@ -1280,3 +1282,13 @@ static int32_t parse_nic_vlan_mode(void)
     return ret;
 }
 
+static int32_t parse_defaule_nonblock_mode(void)
+{
+    int32_t ret;
+    PARSE_ARG(g_config_params.nonblock_mode, "nonblock_mode", 1, 0, 1, ret);
+    if (ret != 0) {
+        LSTACK_PRE_LOG(LSTACK_ERR, "cfg: invalid nonblock mode value %d. only support 0 or 1\n", \
+                            g_config_params.nonblock_mode);
+    }
+    return ret;
+}
diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c
index 9158d20..a000224 100644
--- a/src/lstack/core/lstack_protocol_stack.c
+++ b/src/lstack/core/lstack_protocol_stack.c
@@ -1287,7 +1287,10 @@ int32_t stack_broadcast_accept4(int32_t fd, struct sockaddr *addr, socklen_t *ad
 
 int32_t stack_broadcast_accept(int32_t fd, struct sockaddr *addr, socklen_t *addrlen)
 {
-    return stack_broadcast_accept4(fd, addr, addrlen, 0);
+    if (get_global_cfg_params()->nonblock_mode)
+        return stack_broadcast_accept4(fd, addr, addrlen, O_NONBLOCK);
+    else
+        return stack_broadcast_accept4(fd, addr, addrlen, 0);
 }
 
 static void stack_all_fds_close(void)
diff --git a/src/lstack/include/lstack_cfg.h b/src/lstack/include/lstack_cfg.h
index a6bdfd5..82e96d8 100644
--- a/src/lstack/include/lstack_cfg.h
+++ b/src/lstack/include/lstack_cfg.h
@@ -118,6 +118,7 @@ struct cfg_params {
     bool udp_enable;
     struct cfg_nic_params nic;
     bool stack_mode_rtc;
+    bool nonblock_mode;
 };
 
 struct cfg_params *get_global_cfg_params(void);
-- 
2.27.0

