From 85e907b495391a89096bfb7e5c9cb901d0f9f56b Mon Sep 17 00:00:00 2001
From: Lemmy Huang <huangliming5@huawei.com>
Date: Sat, 31 Aug 2024 11:31:13 +0800
Subject: [PATCH] cleancode: refactor rtc_api rtw_api and dummy_api

Signed-off-by: Lemmy Huang <huangliming5@huawei.com>
---
 src/lstack/api/lstack_dummy_api.c             |  32 ++++--
 src/lstack/api/lstack_epoll.c                 |   2 +-
 src/lstack/api/lstack_fork.c                  |  28 -----
 src/lstack/api/lstack_rtc_api.c               |  92 +++++++++++-----
 src/lstack/api/lstack_rtw_api.c               | 104 +++++++++++-------
 .../api/{lstack_signal.c => lstack_unistd.c}  |  31 +++++-
 src/lstack/api/lstack_wrap.c                  |  91 ++-------------
 src/lstack/core/lstack_init.c                 |  31 ++----
 src/lstack/core/lstack_lwip.c                 |   2 +-
 src/lstack/core/lstack_protocol_stack.c       |   2 +-
 src/lstack/core/lstack_stack_stat.c           |   2 +-
 src/lstack/include/{posix => }/lstack_epoll.h |   0
 src/lstack/include/lstack_rtc_api.h           |  43 +-------
 src/lstack/include/lstack_rtw_api.h           |  35 +-----
 .../include/{posix => }/lstack_unistd.h       |  13 +--
 src/lstack/include/lstack_wrap.h              |   2 +-
 16 files changed, 217 insertions(+), 293 deletions(-)
 delete mode 100644 src/lstack/api/lstack_fork.c
 rename src/lstack/api/{lstack_signal.c => lstack_unistd.c} (89%)
 rename src/lstack/include/{posix => }/lstack_epoll.h (100%)
 rename src/lstack/include/{posix => }/lstack_unistd.h (78%)

diff --git a/src/lstack/api/lstack_dummy_api.c b/src/lstack/api/lstack_dummy_api.c
index f327916..3a867b3 100644
--- a/src/lstack/api/lstack_dummy_api.c
+++ b/src/lstack/api/lstack_dummy_api.c
@@ -13,6 +13,10 @@
 #include <unistd.h>
 #include <errno.h>
 
+#include <rte_config.h>
+#include <rte_atomic.h>
+#include <lwip/lwipgz_posix_api.h>
+
 #define DUMMY_SLEEP_S 5
 
 static inline ssize_t dummy_exit(void)
@@ -22,34 +26,48 @@ static inline ssize_t dummy_exit(void)
     return -1;
 }
 
-int dummy_socket(int domain, int type, int protocol)
+static int dummy_socket(int domain, int type, int protocol)
 {
     sleep(DUMMY_SLEEP_S);
     return -1;
 }
 
-ssize_t dummy_write(int s, const void *mem, size_t size)
+static ssize_t dummy_write(int s, const void *mem, size_t size)
 {
     return dummy_exit();
 }
 
-ssize_t dummy_writev(int s, const struct iovec *iov, int iovcnt)
+static ssize_t dummy_writev(int s, const struct iovec *iov, int iovcnt)
 {
     return dummy_exit();
 }
 
-ssize_t dummy_send(int sockfd, const void *buf, size_t len, int flags)
+static ssize_t dummy_send(int sockfd, const void *buf, size_t len, int flags)
 {
     return dummy_exit();
 }
 
-ssize_t dummy_sendmsg(int s, const struct msghdr *message, int flags)
+static ssize_t dummy_sendmsg(int s, const struct msghdr *message, int flags)
 {
     return dummy_exit();
 }
 
-ssize_t dummy_sendto(int sockfd, const void *buf, size_t len, int flags,
-                     const struct sockaddr *addr, socklen_t addrlen)
+static ssize_t dummy_sendto(int sockfd, const void *buf, size_t len, int flags,
+                            const struct sockaddr *addr, socklen_t addrlen)
 {
     return dummy_exit();
 }
+
+void dummy_api_init(posix_api_t *api)
+{
+    api->socket_fn  = dummy_socket;
+    api->send_fn    = dummy_send;
+    api->write_fn   = dummy_write;
+    api->writev_fn  = dummy_writev;
+    api->sendmsg_fn = dummy_sendmsg;
+    api->sendto_fn  = dummy_sendto;
+
+    rte_wmb();
+    /* 1: wait until app thread call send functio complete */
+    sleep(1);
+}
diff --git a/src/lstack/api/lstack_epoll.c b/src/lstack/api/lstack_epoll.c
index 5b0bee4..1c13076 100644
--- a/src/lstack/api/lstack_epoll.c
+++ b/src/lstack/api/lstack_epoll.c
@@ -35,7 +35,7 @@
 #include "common/gazelle_base_func.h"
 #include "lstack_lwip.h"
 #include "lstack_protocol_stack.h"
-#include "posix/lstack_epoll.h"
+#include "lstack_epoll.h"
 
 #define EPOLL_KERNEL_INTERVAL   10 /* ms */
 #define SEC_TO_NSEC             1000000000
diff --git a/src/lstack/api/lstack_fork.c b/src/lstack/api/lstack_fork.c
deleted file mode 100644
index f5d0e95..0000000
--- a/src/lstack/api/lstack_fork.c
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
-* Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
-* gazelle is licensed under the Mulan PSL v2.
-* You can use this software according to the terms and conditions of the Mulan PSL v2.
-* You may obtain a copy of Mulan PSL v2 at:
-*     http://license.coscl.org.cn/MulanPSL2
-* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
-* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
-* PURPOSE.
-* See the Mulan PSL v2 for more details.
-*/
-
-#include <sys/socket.h>
-#include <unistd.h>
-
-#include <lwip/lwipgz_posix_api.h>
-
-pid_t lstack_fork(void)
-{
-    pid_t pid;
-
-    pid = posix_api->fork_fn();
-    /* child not support lwip */
-    if (pid == 0) {
-        posix_api->use_kernel = 1;
-    }
-    return pid;
-}
diff --git a/src/lstack/api/lstack_rtc_api.c b/src/lstack/api/lstack_rtc_api.c
index e30718c..7689c83 100644
--- a/src/lstack/api/lstack_rtc_api.c
+++ b/src/lstack/api/lstack_rtc_api.c
@@ -10,37 +10,16 @@
 * See the Mulan PSL v2 for more details.
 */
 
-#include <fcntl.h>
-#include <sys/epoll.h>
-#include <sys/socket.h>
-#include <unistd.h>
-#include <lwip/lwipgz_posix_api.h>
 #include <lwip/lwipgz_sock.h>
-#include "posix/lstack_epoll.h"
+#include <lwip/sockets.h>
+
+#include "lstack_epoll.h"
 #include "lstack_log.h"
 #include "lstack_cfg.h"
 #include "lstack_protocol_stack.h"
-#include "lstack_thread_rpc.h"
 #include "lstack_rtc_api.h"
 
-int rtc_poll(struct pollfd *fds, nfds_t nfds, int timeout)
-{
-    LSTACK_LOG(ERR, LSTACK, "rtc_poll: rtc currently does not support poll\n");
-    return -1;
-}
-
-int rtc_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
-{
-    LSTACK_LOG(ERR, LSTACK, "rtc_select: rtc currently does not support select\n");
-    return -1;
-}
-
-int rtc_epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout)
-{
-    return lstack_rtc_epoll_wait(epfd, events, maxevents, timeout);
-}
-
-int rtc_socket(int domain, int type, int protocol)
+static int rtc_socket(int domain, int type, int protocol)
 {
     int ret;
 
@@ -53,7 +32,7 @@ int rtc_socket(int domain, int type, int protocol)
     return ret;
 }
 
-int rtc_close(int s)
+static int rtc_close(int s)
 {
     struct lwip_sock *sock = lwip_get_socket(s);
     if (sock != NULL && sock->wakeup != NULL && sock->wakeup->epollfd == s) {
@@ -63,12 +42,12 @@ int rtc_close(int s)
     return lwip_close(s);
 }
 
-int rtc_shutdown(int fd, int how)
+static int rtc_shutdown(int fd, int how)
 {
     return lwip_shutdown(fd, how);
 }
 
-int rtc_epoll_create(int flags)
+static int rtc_epoll_create(int flags)
 {
     if (stack_setup_app_thread() < 0) {
         exit(1);
@@ -77,7 +56,7 @@ int rtc_epoll_create(int flags)
     return lstack_epoll_create(flags);
 }
 
-int rtc_epoll_create1(int flags)
+static int rtc_epoll_create1(int flags)
 {
     if (stack_setup_app_thread() < 0) {
         exit(1);
@@ -86,7 +65,60 @@ int rtc_epoll_create1(int flags)
     return lstack_epoll_create1(flags);
 }
 
-int rtc_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
+static int rtc_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
 {
     return lstack_rtc_epoll_ctl(epfd, op, fd, event);
 }
+
+static int rtc_epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout)
+{
+    return lstack_rtc_epoll_wait(epfd, events, maxevents, timeout);
+}
+
+static int rtc_poll(struct pollfd *fds, nfds_t nfds, int timeout)
+{
+    LSTACK_LOG(ERR, LSTACK, "rtc_poll: rtc currently does not support poll\n");
+    return -1;
+}
+
+static int rtc_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
+{
+    LSTACK_LOG(ERR, LSTACK, "rtc_select: rtc currently does not support select\n");
+    return -1;
+}
+
+void rtc_api_init(posix_api_t *api)
+{
+    api->close_fn         = rtc_close;
+    api->shutdown_fn      = rtc_shutdown;
+    api->socket_fn        = rtc_socket;
+    api->accept_fn        = lwip_accept;
+    api->accept4_fn       = lwip_accept4;
+    api->bind_fn          = lwip_bind;
+    api->listen_fn        = lwip_listen;
+    api->connect_fn       = lwip_connect;
+
+    api->setsockopt_fn    = lwip_setsockopt;
+    api->getsockopt_fn    = lwip_getsockopt;
+    api->getpeername_fn   = lwip_getpeername;
+    api->getsockname_fn   = lwip_getsockname;
+
+    api->read_fn          = lwip_read;
+    api->readv_fn         = lwip_readv;
+    api->write_fn         = lwip_write;
+    api->writev_fn        = lwip_writev;
+    api->recv_fn          = lwip_recv;
+    api->send_fn          = lwip_send;
+    api->recvmsg_fn       = (ssize_t (*)(int, const struct msghdr *, int))lwip_recvmsg; // TODO: fix unnecessary 'const' in lwipgz_posix_api.h
+    api->sendmsg_fn       = lwip_sendmsg;
+    api->recvfrom_fn      = lwip_recvfrom;
+    api->sendto_fn        = lwip_sendto;
+
+    api->epoll_ctl_fn     = rtc_epoll_ctl;
+    api->epoll_create1_fn = rtc_epoll_create1;
+    api->epoll_create_fn  = rtc_epoll_create;
+    api->epoll_wait_fn    = rtc_epoll_wait;
+
+    api->poll_fn          = rtc_poll;
+    api->select_fn        = rtc_select;
+}
diff --git a/src/lstack/api/lstack_rtw_api.c b/src/lstack/api/lstack_rtw_api.c
index 09c4e11..d8634cc 100644
--- a/src/lstack/api/lstack_rtw_api.c
+++ b/src/lstack/api/lstack_rtw_api.c
@@ -10,23 +10,18 @@
 * See the Mulan PSL v2 for more details.
 */
 
-#include <fcntl.h>
-#include <sys/epoll.h>
-#include <sys/socket.h>
-#include <unistd.h>
-
 #include <lwip/lwipgz_sock.h>
+#include <lwip/sockets.h>
 
 #include "lstack_thread_rpc.h"
-#include "posix/lstack_epoll.h"
+#include "lstack_epoll.h"
 #include "lstack_protocol_stack.h"
 #include "lstack_cfg.h"
 #include "lstack_lwip.h"
 #include "common/gazelle_base_func.h"
 #include "lstack_rtw_api.h"
 
-
-int rtw_socket(int domain, int type, int protocol)
+static int rtw_socket(int domain, int type, int protocol)
 {
     struct protocol_stack *stack = get_bind_protocol_stack();
     if (stack == NULL) {
@@ -35,17 +30,17 @@ int rtw_socket(int domain, int type, int protocol)
     return rpc_call_socket(&stack->rpc_queue, domain, type, protocol);
 }
 
-int rtw_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
+static int rtw_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
 {
     return stack_broadcast_accept(s, addr, addrlen);
 }
 
-int rtw_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
+static int rtw_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
 {
     return stack_broadcast_accept4(s, addr, addrlen, flags);
 }
 
-int rtw_bind(int s, const struct sockaddr *name, socklen_t namelen)
+static int rtw_bind(int s, const struct sockaddr *name, socklen_t namelen)
 {
     struct lwip_sock *sock = lwip_get_socket(s);
 
@@ -56,7 +51,7 @@ int rtw_bind(int s, const struct sockaddr *name, socklen_t namelen)
     }
 }
 
-int rtw_listen(int s, int backlog)
+static int rtw_listen(int s, int backlog)
 {
     if (!get_global_cfg_params()->tuple_filter &&
         !get_global_cfg_params()->listen_shadow) {
@@ -66,7 +61,7 @@ int rtw_listen(int s, int backlog)
     }
 }
 
-int rtw_connect(int s, const struct sockaddr *name, socklen_t namelen)
+static int rtw_connect(int s, const struct sockaddr *name, socklen_t namelen)
 {
     struct protocol_stack *stack = get_protocol_stack_by_fd(s);
     if (stack == NULL) {
@@ -75,7 +70,7 @@ int rtw_connect(int s, const struct sockaddr *name, socklen_t namelen)
     return rpc_call_connect(&stack->rpc_queue, s, name, namelen);
 }
 
-int rtw_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
+static int rtw_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
 {
     struct protocol_stack *stack = get_protocol_stack_by_fd(s);
     if (stack == NULL) {
@@ -84,7 +79,7 @@ int rtw_setsockopt(int s, int level, int optname, const void *optval, socklen_t
     return rpc_call_setsockopt(&stack->rpc_queue, s, level, optname, optval, optlen);
 }
 
-int rtw_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
+static int rtw_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
 {
     struct protocol_stack *stack = get_protocol_stack_by_fd(s);
     if (stack == NULL) {
@@ -93,7 +88,7 @@ int rtw_getsockopt(int s, int level, int optname, void *optval, socklen_t *optle
     return rpc_call_getsockopt(&stack->rpc_queue, s, level, optname, optval, optlen);
 }
 
-int rtw_getpeername(int s, struct sockaddr *name, socklen_t *namelen)
+static int rtw_getpeername(int s, struct sockaddr *name, socklen_t *namelen)
 {
     struct protocol_stack *stack = get_protocol_stack_by_fd(s);
     if (stack == NULL) {
@@ -102,7 +97,7 @@ int rtw_getpeername(int s, struct sockaddr *name, socklen_t *namelen)
     return rpc_call_getpeername(&stack->rpc_queue, s, name, namelen);
 }
 
-int rtw_getsockname(int s, struct sockaddr *name, socklen_t *namelen)
+static int rtw_getsockname(int s, struct sockaddr *name, socklen_t *namelen)
 {
     struct protocol_stack *stack = get_protocol_stack_by_fd(s);
     if (stack == NULL) {
@@ -111,12 +106,12 @@ int rtw_getsockname(int s, struct sockaddr *name, socklen_t *namelen)
     return rpc_call_getsockname(&stack->rpc_queue, s, name, namelen);
 }
 
-ssize_t rtw_read(int s, void *mem, size_t len)
+static ssize_t rtw_read(int s, void *mem, size_t len)
 {
     return do_lwip_read_from_stack(s, mem, len, 0, NULL, NULL);
 }
 
-ssize_t rtw_readv(int s, const struct iovec *iov, int iovcnt)
+static ssize_t rtw_readv(int s, const struct iovec *iov, int iovcnt)
 {
     struct msghdr msg;
 
@@ -130,12 +125,12 @@ ssize_t rtw_readv(int s, const struct iovec *iov, int iovcnt)
     return do_lwip_recvmsg_from_stack(s, &msg, 0);
 }
 
-ssize_t rtw_write(int s, const void *mem, size_t size)
+static ssize_t rtw_write(int s, const void *mem, size_t size)
 {
     return do_lwip_send_to_stack(s, mem, size, 0, NULL, 0);
 }
 
-ssize_t rtw_writev(int s, const struct iovec *iov, int iovcnt)
+static ssize_t rtw_writev(int s, const struct iovec *iov, int iovcnt)
 {
     struct lwip_sock *sock = lwip_get_socket(s);
     struct msghdr msg;
@@ -150,22 +145,22 @@ ssize_t rtw_writev(int s, const struct iovec *iov, int iovcnt)
     return do_lwip_sendmsg_to_stack(sock, s, &msg, 0);
 }
 
-ssize_t rtw_recv(int sockfd, void *buf, size_t len, int flags)
+static ssize_t rtw_recv(int sockfd, void *buf, size_t len, int flags)
 {
     return do_lwip_read_from_stack(sockfd, buf, len, flags, NULL, NULL);
 }
 
-ssize_t rtw_send(int sockfd, const void *buf, size_t len, int flags)
+static ssize_t rtw_send(int sockfd, const void *buf, size_t len, int flags)
 {
     return do_lwip_send_to_stack(sockfd, buf, len, flags, NULL, 0);
 }
 
-ssize_t rtw_recvmsg(int s, const struct msghdr *message, int flags)
+static ssize_t rtw_recvmsg(int s, struct msghdr *message, int flags)
 {
     return do_lwip_recvmsg_from_stack(s, message, flags);
 }
 
-ssize_t rtw_sendmsg(int s, const struct msghdr *message, int flags)
+static ssize_t rtw_sendmsg(int s, const struct msghdr *message, int flags)
 {
     struct lwip_sock *sock = lwip_get_socket(s);
     return do_lwip_sendmsg_to_stack(sock, s, message, flags);
@@ -207,8 +202,8 @@ static inline ssize_t rtw_tcp_recvfrom(int sockfd, void *buf, size_t len, int fl
 }
 
 
-ssize_t rtw_recvfrom(int sockfd, void *buf, size_t len, int flags,
-                     struct sockaddr *addr, socklen_t *addrlen)
+static ssize_t rtw_recvfrom(int sockfd, void *buf, size_t len, int flags,
+                            struct sockaddr *addr, socklen_t *addrlen)
 {
     struct lwip_sock *sock = lwip_get_socket(sockfd);
     if (NETCONN_IS_UDP(sock)) {
@@ -218,28 +213,28 @@ ssize_t rtw_recvfrom(int sockfd, void *buf, size_t len, int flags,
     }
 }
 
-ssize_t rtw_sendto(int sockfd, const void *buf, size_t len, int flags,
-                   const struct sockaddr *addr, socklen_t addrlen)
+static ssize_t rtw_sendto(int sockfd, const void *buf, size_t len, int flags,
+                          const struct sockaddr *addr, socklen_t addrlen)
 {
     return do_lwip_send_to_stack(sockfd, buf, len, flags, addr, addrlen);
 }
 
-int rtw_epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout)
+static int rtw_epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout)
 {
     return lstack_rtw_epoll_wait(epfd, events, maxevents, timeout);
 }
 
-int rtw_poll(struct pollfd *fds, nfds_t nfds, int timeout)
+static int rtw_poll(struct pollfd *fds, nfds_t nfds, int timeout)
 {
     return lstack_poll(fds, nfds, timeout);
 }
 
-int rtw_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
+static int rtw_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
 {
     return lstack_select(nfds, readfds, writefds, exceptfds, timeout);
 }
 
-int rtw_close(int s)
+static int rtw_close(int s)
 {
     struct lwip_sock *sock = lwip_get_socket(s);
     if (sock && sock->wakeup && sock->wakeup->epollfd == s) {
@@ -248,7 +243,7 @@ int rtw_close(int s)
     return stack_broadcast_close(s);
 }
 
-int rtw_shutdown(int fd, int how)
+static int rtw_shutdown(int fd, int how)
 {
     struct lwip_sock *sock = lwip_get_socket(fd);
     if (sock && sock->wakeup && sock->wakeup->epollfd == fd) {
@@ -258,18 +253,53 @@ int rtw_shutdown(int fd, int how)
     return stack_broadcast_shutdown(fd, how);
 }
 
-int rtw_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
+static int rtw_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
 {
     return lstack_rtw_epoll_ctl(epfd, op, fd, event);
 }
 
-int rtw_epoll_create1(int flags)
+static int rtw_epoll_create1(int flags)
 {
     return lstack_epoll_create1(flags);
 }
 
-int rtw_epoll_create(int flags)
+static int rtw_epoll_create(int flags)
 {
     return lstack_epoll_create(flags);
 }
 
+void rtw_api_init(posix_api_t *api)
+{
+    api->close_fn         = rtw_close;
+    api->shutdown_fn      = rtw_shutdown;
+    api->socket_fn        = rtw_socket;
+    api->accept_fn        = rtw_accept;
+    api->accept4_fn       = rtw_accept4;
+    api->bind_fn          = rtw_bind;
+    api->listen_fn        = rtw_listen;
+    api->connect_fn       = rtw_connect;
+
+    api->setsockopt_fn    = rtw_setsockopt;
+    api->getsockopt_fn    = rtw_getsockopt;
+    api->getpeername_fn   = rtw_getpeername;
+    api->getsockname_fn   = rtw_getsockname;
+
+    api->read_fn          = rtw_read;
+    api->readv_fn         = rtw_readv;
+    api->write_fn         = rtw_write;
+    api->writev_fn        = rtw_writev;
+    api->recv_fn          = rtw_recv;
+    api->send_fn          = rtw_send;
+    api->recvmsg_fn       = (ssize_t (*)(int, const struct msghdr *, int))rtw_recvmsg; // TODO: fix unnecessary 'const' in lwipgz_posix_api.h
+    api->sendmsg_fn       = rtw_sendmsg;
+    api->recvfrom_fn      = rtw_recvfrom;
+    api->sendto_fn        = rtw_sendto;
+
+    api->epoll_ctl_fn     = rtw_epoll_ctl;
+    api->epoll_create1_fn = rtw_epoll_create1;
+    api->epoll_create_fn  = rtw_epoll_create;
+    api->epoll_wait_fn    = rtw_epoll_wait;
+
+    api->poll_fn          = rtw_poll;
+    api->select_fn        = rtw_select;
+}
\ No newline at end of file
diff --git a/src/lstack/api/lstack_signal.c b/src/lstack/api/lstack_unistd.c
similarity index 89%
rename from src/lstack/api/lstack_signal.c
rename to src/lstack/api/lstack_unistd.c
index 9d0431b..1f78626 100644
--- a/src/lstack/api/lstack_signal.c
+++ b/src/lstack/api/lstack_unistd.c
@@ -11,22 +11,22 @@
 */
 
 #include <sys/types.h>
-#include <signal.h>
-#include <sys/socket.h>
 #include <execinfo.h>
-#include <unistd.h>
+#include <sys/socket.h>
+
 #include <lwip/lwipgz_sock.h>
 #include <lwip/lwipgz_posix_api.h>
 
+#include "lstack_unistd.h"
 #include "common/gazelle_base_func.h"
-#include "lstack_cfg.h"
-#include "common/dpdk_common.h"
 #include "lstack_log.h"
+#include "lstack_cfg.h"
 #include "lstack_control_plane.h"
 
 static int g_hijack_signal[] = { SIGTERM, SIGINT, SIGSEGV, SIGBUS, SIGFPE, SIGILL, SIGKILL};
 #define HIJACK_SIGNAL_COUNT (sizeof(g_hijack_signal) / sizeof(g_hijack_signal[0]))
 #define BACKTRACE_SIZE 64
+
 static void dump_stack(void)
 {
     char **stack_trace = NULL;
@@ -71,17 +71,24 @@ static void lstack_sig_default_handler(int sig)
     (void)kill(getpid(), sig);
 }
 
-void lstack_signal_init(void)
+int lstack_signal_init(void)
 {
     unsigned int i;
     struct sigaction action;
 
+    /* to prevent crash, just ignore SIGPIPE when socket is closed */
+    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
+        return -1;
+    }
+
     sigemptyset(&action.sa_mask);
     action.sa_flags = (int)(SA_NODEFER | SA_RESETHAND);
     action.sa_handler = lstack_sig_default_handler;
     for (i = 0; i < HIJACK_SIGNAL_COUNT; i++) {
         posix_api->sigaction_fn(g_hijack_signal[i], &action, NULL);
     }
+
+    return 0;
 }
 
 int lstack_sigaction(int sig_num, const struct sigaction *action, struct sigaction *old_action)
@@ -104,3 +111,15 @@ int lstack_sigaction(int sig_num, const struct sigaction *action, struct sigacti
 
     return posix_api->sigaction_fn(sig_num, action, old_action);
 }
+
+pid_t lstack_fork(void)
+{
+    pid_t pid;
+
+    pid = posix_api->fork_fn();
+    /* child not support lwip */
+    if (pid == 0) {
+        posix_api->use_kernel = 1;
+    }
+    return pid;
+}
diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c
index 92ce73b..1d5529d 100644
--- a/src/lstack/api/lstack_wrap.c
+++ b/src/lstack/api/lstack_wrap.c
@@ -10,29 +10,22 @@
 * See the Mulan PSL v2 for more details.
 */
 
-#include <dlfcn.h>
-#include <netdb.h>
+#include <securec.h>
 #include <ifaddrs.h>
-
-#include <signal.h>
-#include <fcntl.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
-#include <sys/epoll.h>
 #include <net/if.h>
-#include <securec.h>
 
 #include <lwip/lwipgz_posix_api.h>
 #include <lwip/lwipgz_sock.h>
 #include <lwip/tcp.h>
 
-#include "posix/lstack_unistd.h"
+#include "common/gazelle_base_func.h"
 #include "lstack_log.h"
 #include "lstack_cfg.h"
 #include "lstack_lwip.h"
-#include "common/gazelle_base_func.h"
 #include "lstack_preload.h"
-
+#include "lstack_unistd.h"
 #include "lstack_rtc_api.h"
 #include "lstack_rtw_api.h"
 #include "lstack_dummy_api.h"
@@ -45,78 +38,18 @@ void wrap_api_init(void)
     if (g_wrap_api != NULL) {
         return;
     }
-
     g_wrap_api = &g_wrap_api_value;
+
     if (get_global_cfg_params()->stack_mode_rtc) {
-        g_wrap_api->socket_fn        = rtc_socket;
-        g_wrap_api->accept_fn        = lwip_accept;
-        g_wrap_api->accept4_fn       = lwip_accept4;
-        g_wrap_api->bind_fn          = lwip_bind;
-        g_wrap_api->listen_fn        = lwip_listen;
-        g_wrap_api->connect_fn       = lwip_connect;
-        g_wrap_api->setsockopt_fn    = lwip_setsockopt;
-        g_wrap_api->getsockopt_fn    = lwip_getsockopt;
-        g_wrap_api->getpeername_fn   = lwip_getpeername;
-        g_wrap_api->getsockname_fn   = lwip_getsockname;
-        g_wrap_api->read_fn          = lwip_read;
-        g_wrap_api->readv_fn         = lwip_readv;
-        g_wrap_api->write_fn         = lwip_write;
-        g_wrap_api->writev_fn        = lwip_writev;
-        g_wrap_api->recv_fn          = lwip_recv;
-        g_wrap_api->send_fn          = lwip_send;
-        g_wrap_api->recvmsg_fn       = lwip_recvmsg;
-        g_wrap_api->sendmsg_fn       = lwip_sendmsg;
-        g_wrap_api->recvfrom_fn      = lwip_recvfrom;
-        g_wrap_api->sendto_fn        = lwip_sendto;
-        g_wrap_api->epoll_wait_fn    = rtc_epoll_wait;
-        g_wrap_api->poll_fn          = rtc_poll;
-        g_wrap_api->close_fn         = rtc_close;
-        g_wrap_api->shutdown_fn      = rtc_shutdown;
-        g_wrap_api->epoll_ctl_fn     = rtc_epoll_ctl;
-        g_wrap_api->epoll_create1_fn = rtc_epoll_create1;
-        g_wrap_api->epoll_create_fn  = rtc_epoll_create;
-        g_wrap_api->select_fn        = rtc_select;
+        rtc_api_init(g_wrap_api);
     } else {
-        g_wrap_api->socket_fn        = rtw_socket;
-        g_wrap_api->accept_fn        = rtw_accept;
-        g_wrap_api->accept4_fn       = rtw_accept4;
-        g_wrap_api->bind_fn          = rtw_bind;
-        g_wrap_api->listen_fn        = rtw_listen;
-        g_wrap_api->connect_fn       = rtw_connect;
-        g_wrap_api->setsockopt_fn    = rtw_setsockopt;
-        g_wrap_api->getsockopt_fn    = rtw_getsockopt;
-        g_wrap_api->getpeername_fn   = rtw_getpeername;
-        g_wrap_api->getsockname_fn   = rtw_getsockname;
-        g_wrap_api->read_fn          = rtw_read;
-        g_wrap_api->readv_fn         = rtw_readv;
-        g_wrap_api->write_fn         = rtw_write;
-        g_wrap_api->writev_fn        = rtw_writev;
-        g_wrap_api->recv_fn          = rtw_recv;
-        g_wrap_api->send_fn          = rtw_send;
-        g_wrap_api->recvmsg_fn       = rtw_recvmsg;
-        g_wrap_api->sendmsg_fn       = rtw_sendmsg;
-        g_wrap_api->recvfrom_fn      = rtw_recvfrom;
-        g_wrap_api->sendto_fn        = rtw_sendto;
-        g_wrap_api->epoll_wait_fn    = rtw_epoll_wait;
-        g_wrap_api->poll_fn          = rtw_poll;
-        g_wrap_api->close_fn         = rtw_close;
-        g_wrap_api->shutdown_fn      = rtw_shutdown;
-        g_wrap_api->epoll_ctl_fn     = rtw_epoll_ctl;
-        g_wrap_api->epoll_create1_fn = rtw_epoll_create1;
-        g_wrap_api->epoll_create_fn  = rtw_epoll_create;
-        g_wrap_api->select_fn        = rtw_select;
-    }
-}
-
-void wrap_api_set_dummy(void)
-{
-    g_wrap_api->socket_fn  = dummy_socket;
-    g_wrap_api->send_fn    = dummy_send;
-    g_wrap_api->write_fn   = dummy_write;
-    g_wrap_api->writev_fn  = dummy_writev;
-    g_wrap_api->sendmsg_fn   = dummy_sendmsg;
-    g_wrap_api->sendto_fn    = dummy_sendto;
-    rte_wmb();
+        rtw_api_init(g_wrap_api);
+    }
+}
+
+void wrap_api_exit(void)
+{
+    dummy_api_init(g_wrap_api);
 }
 
 static inline int32_t do_epoll_create1(int32_t flags)
diff --git a/src/lstack/core/lstack_init.c b/src/lstack/core/lstack_init.c
index e0f7bd7..37264a1 100644
--- a/src/lstack/core/lstack_init.c
+++ b/src/lstack/core/lstack_init.c
@@ -20,27 +20,26 @@
 #include <securec.h>
 #include <numa.h>
 #include <pthread.h>
-#include <rte_pdump.h>
 #include <unistd.h>
 #include <net/if.h>
 #include <net/if_arp.h>
 #include <netinet/in.h>
 
-#include <lwip/def.h>
+#include <rte_pdump.h>
+
 #include <lwip/init.h>
 #include <lwip/lwipgz_sock.h>
 #include <lwip/lwipopts.h>
 #include <lwip/lwipgz_posix_api.h>
 
+#include "lstack_log.h"
 #include "lstack_cfg.h"
 #include "lstack_control_plane.h"
 #include "lstack_ethdev.h"
 #include "lstack_dpdk.h"
 #include "lstack_stack_stat.h"
-#include "lstack_log.h"
 #include "common/dpdk_common.h"
-#include "posix/lstack_epoll.h"
-#include "posix/lstack_unistd.h"
+#include "lstack_unistd.h"
 #include "common/gazelle_base_func.h"
 #include "lstack_protocol_stack.h"
 #include "lstack_preload.h"
@@ -107,9 +106,7 @@ static int32_t check_process_conflict(void)
 
 void gazelle_exit(void)
 {
-    wrap_api_set_dummy();
-    /* 1: wait until app thread call send functio complete */
-    sleep(1);
+    wrap_api_exit();
     stack_group_exit();
 }
 
@@ -170,18 +167,6 @@ static void create_control_thread(void)
     LSTACK_LOG(INFO, LSTACK, "create control_easy_thread success\n");
 }
 
-static void gazelle_signal_init(void)
-{
-    /* to prevent crash , just ignore SIGPIPE when socket is closed */
-    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
-        LSTACK_PRE_LOG(LSTACK_ERR, "signal error, errno:%d.", errno);
-        LSTACK_EXIT(1, "signal SIGPIPE SIG_IGN\n");
-    }
-
-    /* register core sig handler func to dumped stack */
-    lstack_signal_init();
-}
-
 #if RTE_VERSION < RTE_VERSION_NUM(23, 11, 0, 0)
 static void set_kni_ip_mac()
 {
@@ -278,7 +263,11 @@ __attribute__((constructor)) void gazelle_network_init(void)
         }
     }
 
-    gazelle_signal_init();
+    /* register core sig handler func to dumped stack */
+    if (lstack_signal_init() != 0) {
+        LSTACK_PRE_LOG(LSTACK_ERR, "signal init failed, errno %d\n", errno);
+        LSTACK_EXIT(1, "signal init failed, errno %d\n", errno);
+    }
 
     /* Init control plane and dpdk init */
     create_control_thread();
diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c
index a7b7202..89142a4 100644
--- a/src/lstack/core/lstack_lwip.c
+++ b/src/lstack/core/lstack_lwip.c
@@ -34,7 +34,7 @@
 #include "lstack_log.h"
 #include "lstack_dpdk.h"
 #include "lstack_stack_stat.h"
-#include "posix/lstack_epoll.h"
+#include "lstack_epoll.h"
 #include "lstack_thread_rpc.h"
 #include "common/dpdk_common.h"
 #include "lstack_cfg.h"
diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c
index 00900e7..f56e911 100644
--- a/src/lstack/core/lstack_protocol_stack.c
+++ b/src/lstack/core/lstack_protocol_stack.c
@@ -31,7 +31,7 @@
 #include "lstack_lwip.h"
 #include "lstack_cfg.h"
 #include "lstack_control_plane.h"
-#include "posix/lstack_epoll.h"
+#include "lstack_epoll.h"
 #include "lstack_stack_stat.h"
 #include "lstack_virtio.h"
 #include "lstack_protocol_stack.h"
diff --git a/src/lstack/core/lstack_stack_stat.c b/src/lstack/core/lstack_stack_stat.c
index 406e27c..b6619f6 100644
--- a/src/lstack/core/lstack_stack_stat.c
+++ b/src/lstack/core/lstack_stack_stat.c
@@ -26,7 +26,7 @@
 #include "common/gazelle_dfx_msg.h"
 #include "lstack_thread_rpc.h"
 #include "lstack_protocol_stack.h"
-#include "posix/lstack_epoll.h"
+#include "lstack_epoll.h"
 #include "lstack_dpdk.h"
 #include "lstack_stack_stat.h"
 #include "lstack_virtio.h"
diff --git a/src/lstack/include/posix/lstack_epoll.h b/src/lstack/include/lstack_epoll.h
similarity index 100%
rename from src/lstack/include/posix/lstack_epoll.h
rename to src/lstack/include/lstack_epoll.h
diff --git a/src/lstack/include/lstack_rtc_api.h b/src/lstack/include/lstack_rtc_api.h
index 3a41e6f..b4b7e1c 100644
--- a/src/lstack/include/lstack_rtc_api.h
+++ b/src/lstack/include/lstack_rtc_api.h
@@ -12,47 +12,14 @@
 
 #ifndef _LSTACK_RTC_API_H_
 #define _LSTACK_RTC_API_H_
-#include <sys/epoll.h>
-#include <sys/select.h>
-#include <sys/socket.h>
+
+#include <lwip/lwipgz_posix_api.h>
 
 /* don't include lwip/sockets.h, conflict with sys/socket.h */
-/* extern lwip_api here */
 extern int lwip_fcntl(int s, int cmd, int val);
-extern int lwip_ioctl(int s, long cmd, ...);
-extern int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
-extern int lwip_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags);
-extern int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
-extern int lwip_shutdown(int s, int how);
-extern int lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen);
-extern int lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen);
-extern int lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen);
-extern int lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen);
-extern int lwip_close(int s);
-extern int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
-extern int lwip_listen(int s, int backlog);
-extern ssize_t lwip_recv(int s, void *mem, size_t len, int flags);
-extern ssize_t lwip_read(int s, void *mem, size_t len);
-extern ssize_t lwip_readv(int s, const struct iovec *iov, int iovcnt);
-extern ssize_t lwip_recvfrom(int s, void *mem, size_t len, int flags,
-                             struct sockaddr *from, socklen_t *fromlen);
-extern ssize_t lwip_recvmsg(int s, const struct msghdr *message, int flags);
-extern ssize_t lwip_send(int s, const void *dataptr, size_t size, int flags);
-extern ssize_t lwip_sendmsg(int s, const struct msghdr *message, int flags);
-extern ssize_t lwip_sendto(int s, const void *dataptr, size_t size, int flags,
-                           const struct sockaddr *to, socklen_t tolen);
-extern int lwip_socket(int domain, int type, int protocol);
-extern ssize_t lwip_write(int s, const void *dataptr, size_t size);
-extern ssize_t lwip_writev(int s, const struct iovec *iov, int iovcnt);
+extern int lwip_ioctl(int s, long cmd, void *argp);
 
-int rtc_poll(struct pollfd *fds, nfds_t nfds, int timeout);
-int rtc_epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout);
-int rtc_socket(int domain, int type, int protocol);
-int rtc_close(int s);
-int rtc_shutdown(int fd, int how);
-int rtc_epoll_create(int flags);
-int rtc_epoll_create1(int flags);
-int rtc_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
-int rtc_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
+void dummy_api_init(posix_api_t *api);
+void rtc_api_init(posix_api_t *api);
 
 #endif /* __LSTACK_RTC_API_H_  */
diff --git a/src/lstack/include/lstack_rtw_api.h b/src/lstack/include/lstack_rtw_api.h
index a38b656..437901a 100644
--- a/src/lstack/include/lstack_rtw_api.h
+++ b/src/lstack/include/lstack_rtw_api.h
@@ -13,39 +13,8 @@
 #ifndef _LSTACK_RTW_API_H_
 #define _LSTACK_RTW_API_H_
 
-#include <sys/epoll.h>
-#include <sys/select.h>
-#include <sys/socket.h>
+#include <lwip/lwipgz_posix_api.h>
 
-int rtw_socket(int domain, int type, int protocol);
-int rtw_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
-int rtw_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags);
-int rtw_bind(int s, const struct sockaddr *name, socklen_t namelen);
-int rtw_listen(int s, int backlog);
-int rtw_connect(int s, const struct sockaddr *name, socklen_t namelen);
-int rtw_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen);
-int rtw_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen);
-int rtw_getpeername(int s, struct sockaddr *name, socklen_t *namelen);
-int rtw_getsockname(int s, struct sockaddr *name, socklen_t *namelen);
-ssize_t rtw_read(int s, void *mem, size_t len);
-ssize_t rtw_readv(int s, const struct iovec *iov, int iovcnt);
-ssize_t rtw_write(int s, const void *mem, size_t size);
-ssize_t rtw_writev(int s, const struct iovec *iov, int iovcnt);
-ssize_t rtw_recv(int sockfd, void *buf, size_t len, int flags);
-ssize_t rtw_send(int sockfd, const void *buf, size_t len, int flags);
-ssize_t rtw_recvmsg(int s, const struct msghdr *message, int flags);
-ssize_t rtw_sendmsg(int s, const struct msghdr *message, int flags);
-ssize_t rtw_recvfrom(int sockfd, void *buf, size_t len, int flags,
-                     struct sockaddr *addr, socklen_t *addrlen);
-ssize_t rtw_sendto(int sockfd, const void *buf, size_t len, int flags,
-                   const struct sockaddr *addr, socklen_t addrlen);
-int rtw_epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout);
-int rtw_poll(struct pollfd *fds, nfds_t nfds, int timeout);
-int rtw_close(int s);
-int rtw_shutdown(int fd, int how);
-int rtw_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
-int rtw_epoll_create1(int flags);
-int rtw_epoll_create(int flags);
-int rtw_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
+void rtw_api_init(posix_api_t *api);
 
 #endif /* _LSTACK_RTW_API_H_ */
diff --git a/src/lstack/include/posix/lstack_unistd.h b/src/lstack/include/lstack_unistd.h
similarity index 78%
rename from src/lstack/include/posix/lstack_unistd.h
rename to src/lstack/include/lstack_unistd.h
index 484a792..3bcee5a 100644
--- a/src/lstack/include/posix/lstack_unistd.h
+++ b/src/lstack/include/lstack_unistd.h
@@ -13,16 +13,11 @@
 #ifndef _GAZELLE_UNISTD_H_
 #define _GAZELLE_UNISTD_H_
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+#include <unistd.h>
+#include <signal.h>
 
+int lstack_signal_init(void);
+int lstack_sigaction(int sig_num, const struct sigaction *action, struct sigaction *old_action);
 pid_t lstack_fork(void);
-void lstack_signal_init(void);
-int lstack_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
-
-#ifdef __cplusplus
-}
-#endif
 
 #endif /* _GAZELLE_UNISTD_H_ */
diff --git a/src/lstack/include/lstack_wrap.h b/src/lstack/include/lstack_wrap.h
index dab5222..a45b0d5 100644
--- a/src/lstack/include/lstack_wrap.h
+++ b/src/lstack/include/lstack_wrap.h
@@ -14,7 +14,7 @@
 #define _LSTACK_WRAP_H_
 
 void wrap_api_init(void);
-void wrap_api_set_dummy(void);
+void wrap_api_exit(void);
 
 #endif
 
-- 
2.33.0

