From a1523d108581cfc8aaf89b1116dde071ca5656d8 Mon Sep 17 00:00:00 2001
From: Lemmy Huang <huangliming5@huawei.com>
Date: Wed, 10 Jul 2024 17:40:43 +0800
Subject: [PATCH] cleancode: refactor posix_api

Signed-off-by: Lemmy Huang <huangliming5@huawei.com>
---
 src/lstack/api/lstack_epoll.c           |  16 +-
 src/lstack/api/lstack_fork.c            |   3 +-
 src/lstack/api/lstack_rtw_api.c         |   2 +-
 src/lstack/api/lstack_wrap.c            | 234 ++++++++++--------------
 src/lstack/core/lstack_control_plane.c  |   4 +-
 src/lstack/core/lstack_init.c           |   4 +-
 src/lstack/core/lstack_lwip.c           |   5 +-
 src/lstack/core/lstack_preload.c        |  23 ++-
 src/lstack/core/lstack_protocol_stack.c |   8 +-
 src/lstack/include/lstack_preload.h     |   6 +-
 src/lstack/netif/lstack_flow.c          |   1 +
 11 files changed, 139 insertions(+), 167 deletions(-)

diff --git a/src/lstack/api/lstack_epoll.c b/src/lstack/api/lstack_epoll.c
index 8146b4f..f8fd6d4 100644
--- a/src/lstack/api/lstack_epoll.c
+++ b/src/lstack/api/lstack_epoll.c
@@ -164,7 +164,7 @@ static uint32_t update_events(struct lwip_sock *sock)
 
     if ((sock->epoll_events & EPOLLOUT) && NETCONN_IS_OUTIDLE(sock)) {
         /* lwip_netconn_do_connected set LIBOS FLAGS when connected */
-        if (sock->conn && POSIX_IS_TYPE(sock, POSIX_LWIP)) {
+        if (!POSIX_IS_CLOSED(sock) && POSIX_IS_TYPE(sock, POSIX_LWIP)) {
             event |= EPOLLOUT;
         }
     }
@@ -190,7 +190,7 @@ static void rtc_raise_pending_events(struct wakeup_poll *wakeup, struct lwip_soc
 
     if (sock->sendevent) {
         /* lwip_netconn_do_connected set LIBOS FLAGS when connected */
-        if (sock->conn && POSIX_IS_TYPE(sock, POSIX_LWIP)) {
+        if (!POSIX_IS_CLOSED(sock) && POSIX_IS_TYPE(sock, POSIX_LWIP)) {
             event |= EPOLLOUT;
         }
     }
@@ -219,7 +219,7 @@ static void raise_pending_events(struct wakeup_poll *wakeup, struct lwip_sock *s
     pthread_spin_lock(&wakeup->event_list_lock);
     if (NETCONN_IS_OUTIDLE(sock)) {
         /* lwip_netconn_do_connected set LIBOS FLAGS when connected */
-        if (sock->conn && POSIX_IS_TYPE(sock, POSIX_LWIP)) {
+        if (!POSIX_IS_CLOSED(sock) && POSIX_IS_TYPE(sock, POSIX_LWIP)) {
             event |= EPOLLOUT;
         }
     }
@@ -398,7 +398,7 @@ int32_t lstack_rtc_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_
 
     struct wakeup_poll *wakeup = epoll_sock->wakeup;
     struct lwip_sock *sock = lwip_get_socket(fd);
-    if (sock == NULL || sock->conn == NULL) {
+    if (POSIX_IS_CLOSED(sock)) {
         return posix_api->epoll_ctl_fn(epfd, op, fd, event);
     }
 
@@ -438,7 +438,7 @@ int32_t lstack_rtw_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_
 
     struct wakeup_poll *wakeup = epoll_sock->wakeup;
     struct lwip_sock *sock = lwip_get_socket(fd);
-    if (sock == NULL || sock->conn == NULL) {
+    if (POSIX_IS_CLOSED(sock)) {
         return posix_api->epoll_ctl_fn(epfd, op, fd, event);
     }
 
@@ -536,7 +536,7 @@ static int32_t poll_lwip_event(struct pollfd *fds, nfds_t nfds)
         /* sock->listen_next pointerto next stack listen */
         int32_t fd = fds[i].fd;
         struct lwip_sock *sock = lwip_get_socket(fd);
-        while (sock && sock->conn) {
+        while (!POSIX_IS_CLOSED(sock)) {
             uint32_t events = update_events(sock);
             if (events) {
                 fds[i].revents = events;
@@ -838,7 +838,7 @@ static void poll_init(struct wakeup_poll *wakeup, struct pollfd *fds, nfds_t nfd
             }
         }
 
-        if (sock == NULL || sock->conn == NULL || POSIX_HAS_TYPE(sock, POSIX_KERNEL)) {
+        if (POSIX_IS_CLOSED(sock) || POSIX_HAS_TYPE(sock, POSIX_KERNEL)) {
             update_kernel_poll(wakeup, i, fds + i);
         }
 
@@ -846,7 +846,7 @@ static void poll_init(struct wakeup_poll *wakeup, struct pollfd *fds, nfds_t nfd
         wakeup->last_fds[i].events = fds[i].events;
         poll_change = 1;
 
-        while (sock && sock->conn) {
+        while (!POSIX_IS_CLOSED(sock)) {
             sock->epoll_events = fds[i].events | POLLERR;
             sock->wakeup = wakeup;
             stack_count[sock->stack->stack_idx]++;
diff --git a/src/lstack/api/lstack_fork.c b/src/lstack/api/lstack_fork.c
index 5cddee2..f5d0e95 100644
--- a/src/lstack/api/lstack_fork.c
+++ b/src/lstack/api/lstack_fork.c
@@ -20,8 +20,9 @@ pid_t lstack_fork(void)
     pid_t pid;
 
     pid = posix_api->fork_fn();
+    /* child not support lwip */
     if (pid == 0) {
-        posix_api_fork();
+        posix_api->use_kernel = 1;
     }
     return pid;
 }
diff --git a/src/lstack/api/lstack_rtw_api.c b/src/lstack/api/lstack_rtw_api.c
index f59b0cd..09c4e11 100644
--- a/src/lstack/api/lstack_rtw_api.c
+++ b/src/lstack/api/lstack_rtw_api.c
@@ -186,7 +186,7 @@ static ssize_t rtw_udp_recvfrom(int sockfd, void *buf, size_t len, int flags,
             return -1;
         }
         sock = sock->listen_next;
-        if (sock != NULL && sock->conn != NULL) {
+        if (!POSIX_IS_CLOSED(sock)) {
             sockfd = sock->conn->callback_arg.socket;
         } else {
             if (sock == NULL) {
diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c
index d3e1027..b19990e 100644
--- a/src/lstack/api/lstack_wrap.c
+++ b/src/lstack/api/lstack_wrap.c
@@ -37,12 +37,8 @@
 #include "lstack_rtw_api.h"
 #include "lstack_dummy_api.h"
 
-#ifndef SOCK_TYPE_MASK
-#define SOCK_TYPE_MASK 0xf
-#endif
-
-posix_api_t g_wrap_api_value;
-posix_api_t *g_wrap_api;
+static posix_api_t g_wrap_api_value;
+static posix_api_t *g_wrap_api;
 
 void wrap_api_init(void)
 {
@@ -68,10 +64,10 @@ void wrap_api_init(void)
         g_wrap_api->writev_fn        = lwip_writev;
         g_wrap_api->recv_fn          = lwip_recv;
         g_wrap_api->send_fn          = lwip_send;
-        g_wrap_api->recv_msg         = lwip_recvmsg;
-        g_wrap_api->send_msg         = lwip_sendmsg;
-        g_wrap_api->recv_from        = lwip_recvfrom;
-        g_wrap_api->send_to          = lwip_sendto;
+        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;
@@ -97,10 +93,10 @@ void wrap_api_init(void)
         g_wrap_api->writev_fn        = rtw_writev;
         g_wrap_api->recv_fn          = rtw_recv;
         g_wrap_api->send_fn          = rtw_send;
-        g_wrap_api->recv_msg         = rtw_recvmsg;
-        g_wrap_api->send_msg         = rtw_sendmsg;
-        g_wrap_api->recv_from        = rtw_recvfrom;
-        g_wrap_api->send_to          = rtw_sendto;
+        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;
@@ -118,8 +114,8 @@ void wrap_api_set_dummy(void)
     g_wrap_api->send_fn    = dummy_send;
     g_wrap_api->write_fn   = dummy_write;
     g_wrap_api->writev_fn  = dummy_writev;
-    g_wrap_api->send_msg   = dummy_sendmsg;
-    g_wrap_api->send_to    = dummy_sendto;
+    g_wrap_api->sendmsg_fn   = dummy_sendmsg;
+    g_wrap_api->sendto_fn    = dummy_sendto;
     rte_wmb();
 }
 
@@ -169,7 +165,7 @@ static inline int32_t do_epoll_wait(int32_t epfd, struct epoll_event* events, in
 
 static inline int32_t do_accept(int32_t s, struct sockaddr *addr, socklen_t *addrlen)
 {
-    if (select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_KERNEL) {
         return posix_api->accept_fn(s, addr, addrlen);
     }
 
@@ -189,7 +185,7 @@ static int32_t do_accept4(int32_t s, struct sockaddr *addr, socklen_t *addrlen,
         GAZELLE_RETURN(EINVAL);
     }
 
-    if (select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_KERNEL) {
         return posix_api->accept4_fn(s, addr, addrlen, flags);
     }
 
@@ -209,8 +205,8 @@ static int32_t do_bind(int32_t s, const struct sockaddr *name, socklen_t namelen
         GAZELLE_RETURN(EINVAL);
     }
 
-    struct lwip_sock *sock = NULL;
-    if (select_fd_posix_path(s, &sock) == POSIX_KERNEL) {
+    struct lwip_sock *sock = lwip_get_socket(s);
+    if (select_sock_posix_path(sock) == POSIX_KERNEL) {
         return posix_api->bind_fn(s, name, namelen);
     }
 
@@ -230,30 +226,31 @@ static int32_t do_bind(int32_t s, const struct sockaddr *name, socklen_t namelen
             ((struct sockaddr_in6 *)name)->sin6_addr.s6_addr, IPV6_ADDR_LEN);
     }
 
-    if (match_host_addr(&sock_addr)) {
-        /* maybe kni addr */
-        if (posix_api->bind_fn(s, name, namelen) != 0) {
-            POSIX_SET_TYPE(sock, POSIX_LWIP);
-        } else {
-            /* reuse the port allocated by kernel when port == 0 */
-            if (((struct sockaddr_in *)name)->sin_port == 0) {
-                struct sockaddr_in kerneladdr;
-                socklen_t len = sizeof(kerneladdr);
-                if (posix_api->getsockname_fn(s, (struct sockaddr *)&kerneladdr, &len) < 0) {
-                    LSTACK_LOG(ERR, LSTACK, "kernel getsockname failed, fd=%d, errno=%d\n", s, errno);
-                    return -1;
-                }
-                ((struct sockaddr_in *)name)->sin_port = kerneladdr.sin_port;
+    if (!match_host_addr(&sock_addr)) {
+        POSIX_SET_TYPE(sock, POSIX_KERNEL);
+        return posix_api->bind_fn(s, name, namelen);
+    }
+
+    /* maybe kni addr */
+    if (posix_api->bind_fn(s, name, namelen) == 0) {
+        /* reuse the port allocated by kernel when port == 0 */
+        if (((struct sockaddr_in *)name)->sin_port == 0) {
+            struct sockaddr_in kerneladdr;
+            socklen_t len = sizeof(kerneladdr);
+            if (posix_api->getsockname_fn(s, (struct sockaddr *)&kerneladdr, &len) < 0) {
+                LSTACK_LOG(ERR, LSTACK, "kernel getsockname failed, fd=%d, errno=%d\n", s, errno);
+                return -1;
             }
+            ((struct sockaddr_in *)name)->sin_port = kerneladdr.sin_port;
         }
-        return g_wrap_api->bind_fn(s, name, namelen);
+        /* not sure POSIX_LWIP or POSIX_KERNEL */
     } else {
-        POSIX_SET_TYPE(sock, POSIX_KERNEL);
-        return posix_api->bind_fn(s, name, namelen);
+        POSIX_SET_TYPE(sock, POSIX_LWIP);
     }
+    return g_wrap_api->bind_fn(s, name, namelen);
 }
 
-bool is_dst_ip_localhost(const struct sockaddr *addr)
+static bool is_dst_ip_localhost(const struct sockaddr *addr)
 {
     struct ifaddrs *ifap;
     struct ifaddrs *ifa;
@@ -302,13 +299,8 @@ static int32_t do_connect(int32_t s, const struct sockaddr *name, socklen_t name
         GAZELLE_RETURN(EINVAL);
     }
 
-    struct lwip_sock *sock = NULL;
-    if (select_fd_posix_path(s, &sock) == POSIX_KERNEL) {
-        return posix_api->connect_fn(s, name, namelen);
-    }
-
-    sock = lwip_get_socket(s);
-    if (sock == NULL) {
+    struct lwip_sock *sock = lwip_get_socket(s);
+    if (select_sock_posix_path(sock) == POSIX_KERNEL) {
         return posix_api->connect_fn(s, name, namelen);
     }
 
@@ -334,7 +326,7 @@ static int32_t do_connect(int32_t s, const struct sockaddr *name, socklen_t name
 
 static inline int32_t do_listen(int32_t s, int32_t backlog)
 {
-    if (select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_KERNEL) {
         return posix_api->listen_fn(s, backlog);
     }
 
@@ -352,7 +344,7 @@ static inline int32_t do_getpeername(int32_t s, struct sockaddr *name, socklen_t
         GAZELLE_RETURN(EINVAL);
     }
 
-    if (select_fd_posix_path(s, NULL) == POSIX_LWIP) {
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
         return g_wrap_api->getpeername_fn(s, name, namelen);
     }
 
@@ -365,7 +357,7 @@ static inline int32_t do_getsockname(int32_t s, struct sockaddr *name, socklen_t
         GAZELLE_RETURN(EINVAL);
     }
 
-    if (select_fd_posix_path(s, NULL) == POSIX_LWIP) {
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
         return g_wrap_api->getsockname_fn(s, name, namelen);
     }
 
@@ -422,7 +414,7 @@ static bool unsupport_optname(int32_t level, int32_t optname)
 
 static inline int32_t do_getsockopt(int32_t s, int32_t level, int32_t optname, void *optval, socklen_t *optlen)
 {
-    if (select_fd_posix_path(s, NULL) == POSIX_LWIP && !unsupport_optname(level, optname)) {
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP && !unsupport_optname(level, optname)) {
         return g_wrap_api->getsockopt_fn(s, level, optname, optval, optlen);
     }
 
@@ -431,7 +423,7 @@ static inline int32_t do_getsockopt(int32_t s, int32_t level, int32_t optname, v
 
 static inline int32_t do_setsockopt(int32_t s, int32_t level, int32_t optname, const void *optval, socklen_t optlen)
 {
-    if (select_fd_posix_path(s, NULL) == POSIX_KERNEL || unsupport_optname(level, optname)) {
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_KERNEL || unsupport_optname(level, optname)) {
         return posix_api->setsockopt_fn(s, level, optname, optval, optlen);
     }
 
@@ -473,17 +465,14 @@ static inline ssize_t do_recv(int32_t sockfd, void *buf, size_t len, int32_t fla
     if (buf == NULL) {
         GAZELLE_RETURN(EINVAL);
     }
-
     if (len == 0) {
         return 0;
     }
 
-    if (select_posix_path() == POSIX_KERNEL || // maybe fd is created by open before posix_api_init called
-        select_fd_posix_path(sockfd, NULL) == POSIX_KERNEL) {
-        return posix_api->recv_fn(sockfd, buf, len, flags);
+    if (select_sock_posix_path(lwip_get_socket(sockfd)) == POSIX_LWIP) {
+        return g_wrap_api->recv_fn(sockfd, buf, len, flags);
     }
-
-    return g_wrap_api->recv_fn(sockfd, buf, len, flags);
+    return posix_api->recv_fn(sockfd, buf, len, flags);
 }
 
 static inline ssize_t do_read(int32_t s, void *mem, size_t len)
@@ -491,58 +480,46 @@ static inline ssize_t do_read(int32_t s, void *mem, size_t len)
     if (mem == NULL) {
         GAZELLE_RETURN(EINVAL);
     }
-
     if (len == 0) {
         return 0;
     }
 
-    if (select_posix_path() == POSIX_KERNEL ||
-        select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
-        return posix_api->read_fn(s, mem, len);
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+        return g_wrap_api->read_fn(s, mem, len);
     }
-
-    return g_wrap_api->read_fn(s, mem, len);
+    return posix_api->read_fn(s, mem, len);
 }
 
 static inline ssize_t do_readv(int32_t s, const struct iovec *iov, int iovcnt)
 {
-    if (select_posix_path() == POSIX_KERNEL ||
-        select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
-        return posix_api->readv_fn(s, iov, iovcnt);
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+        return g_wrap_api->readv_fn(s, iov, iovcnt);
     }
-
-    return g_wrap_api->readv_fn(s, iov, iovcnt);
+    return posix_api->readv_fn(s, iov, iovcnt);
 }
 
 static inline ssize_t do_send(int32_t sockfd, const void *buf, size_t len, int32_t flags)
 {
-    if (select_posix_path() == POSIX_KERNEL ||
-        select_fd_posix_path(sockfd, NULL) == POSIX_KERNEL) {
-        return posix_api->send_fn(sockfd, buf, len, flags);
+    if (select_sock_posix_path(lwip_get_socket(sockfd)) == POSIX_LWIP) {
+        return g_wrap_api->send_fn(sockfd, buf, len, flags);
     }
-
-    return g_wrap_api->send_fn(sockfd, buf, len, flags);
+    return posix_api->send_fn(sockfd, buf, len, flags);
 }
 
 static inline ssize_t do_write(int32_t s, const void *mem, size_t size)
 {
-    if (select_posix_path() == POSIX_KERNEL ||
-        select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
-        return posix_api->write_fn(s, mem, size);
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+        return g_wrap_api->write_fn(s, mem, size);
     }
-
-    return g_wrap_api->write_fn(s, mem, size);
+    return posix_api->write_fn(s, mem, size);
 }
 
 static inline ssize_t do_writev(int32_t s, const struct iovec *iov, int iovcnt)
 {
-    struct lwip_sock *sock;
-    if (select_posix_path() == POSIX_KERNEL ||
-        select_fd_posix_path(s, &sock) == POSIX_KERNEL) {
-        return posix_api->writev_fn(s, iov, iovcnt);
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+        return g_wrap_api->writev_fn(s, iov, iovcnt);
     }
-
-    return g_wrap_api->writev_fn(s, iov, iovcnt);
+    return posix_api->writev_fn(s, iov, iovcnt);
 }
 
 static inline ssize_t do_recvmsg(int32_t s, struct msghdr *message, int32_t flags)
@@ -551,12 +528,10 @@ static inline ssize_t do_recvmsg(int32_t s, struct msghdr *message, int32_t flag
         GAZELLE_RETURN(EINVAL);
     }
 
-    if (select_posix_path() == POSIX_KERNEL ||
-        select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
-        return posix_api->recv_msg(s, message, flags);
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+        return g_wrap_api->recvmsg_fn(s, message, flags);
     }
-
-    return g_wrap_api->recv_msg(s, message, flags);
+    return posix_api->recvmsg_fn(s, message, flags);
 }
 
 static inline ssize_t do_sendmsg(int32_t s, const struct msghdr *message, int32_t flags)
@@ -565,13 +540,10 @@ static inline ssize_t do_sendmsg(int32_t s, const struct msghdr *message, int32_
         GAZELLE_RETURN(EINVAL);
     }
 
-    struct lwip_sock *sock;
-    if (select_posix_path() == POSIX_KERNEL ||
-        select_fd_posix_path(s, &sock) == POSIX_KERNEL) {
-        return posix_api->send_msg(s, message, flags);
+    if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+        return g_wrap_api->sendmsg_fn(s, message, flags);
     }
-
-    return g_wrap_api->send_msg(s, message, flags);
+    return posix_api->sendmsg_fn(s, message, flags);
 }
 
 static inline ssize_t do_recvfrom(int32_t sockfd, void *buf, size_t len, int32_t flags,
@@ -580,58 +552,48 @@ static inline ssize_t do_recvfrom(int32_t sockfd, void *buf, size_t len, int32_t
     if (buf == NULL) {
         GAZELLE_RETURN(EINVAL);
     }
-
     if (len == 0) {
         return 0;
     }
 
-    struct lwip_sock *sock = NULL;
-    if (select_fd_posix_path(sockfd, &sock) == POSIX_LWIP) {
-        return g_wrap_api->recv_from(sockfd, buf, len, flags, addr, addrlen);
+    if (select_sock_posix_path(lwip_get_socket(sockfd)) == POSIX_LWIP) {
+        return g_wrap_api->recvfrom_fn(sockfd, buf, len, flags, addr, addrlen);
     }
-
-    return posix_api->recv_from(sockfd, buf, len, flags, addr, addrlen);
+    return posix_api->recvfrom_fn(sockfd, buf, len, flags, addr, addrlen);
 }
 
 static inline ssize_t do_sendto(int32_t sockfd, const void *buf, size_t len, int32_t flags,
                                 const struct sockaddr *addr, socklen_t addrlen)
 {
-    struct lwip_sock *sock = NULL;
-    if (select_fd_posix_path(sockfd, &sock) != POSIX_LWIP) {
-        return posix_api->send_to(sockfd, buf, len, flags, addr, addrlen);
+    if (select_sock_posix_path(lwip_get_socket(sockfd)) == POSIX_LWIP) {
+        return g_wrap_api->sendto_fn(sockfd, buf, len, flags, addr, addrlen);
     }
-
-    return g_wrap_api->send_to(sockfd, buf, len, flags, addr, addrlen);
+    return posix_api->sendto_fn(sockfd, buf, len, flags, addr, addrlen);
 }
 
-static inline int32_t do_close(int32_t s)
+static inline int32_t do_close(int fd)
 {
-    struct lwip_sock *sock = NULL;
+    /* Can not use select_sock_posix_path() !
+     * When fd created by lwip_stocket() set as POSIX_KERNEL,
+     * lwip_close() is still required.
+     */
     if (select_posix_path() == POSIX_KERNEL ||
-        select_fd_posix_path(s, &sock) == POSIX_KERNEL) {
-        /* we called lwip_socket, even if kernel fd */
-        if (posix_api != NULL && !posix_api->ues_posix &&
-            /* contain posix_api->close_fn if success */
-            g_wrap_api->close_fn(s) == 0) {
-            return 0;
-        } else {
-            return posix_api->close_fn(s);
-        }
+        POSIX_IS_CLOSED(lwip_get_socket(fd))) {
+        return posix_api->close_fn(fd);
     }
-    return g_wrap_api->close_fn(s);
+    return g_wrap_api->close_fn(fd);
 }
 
 static int32_t do_shutdown(int fd, int how)
 {
-    struct lwip_sock *sock = NULL;
-    if (select_posix_path() == POSIX_KERNEL || select_fd_posix_path(fd, &sock) == POSIX_KERNEL) {
-        if (posix_api != NULL && !posix_api->ues_posix && g_wrap_api->shutdown_fn(fd, how) == 0) {
-            return 0;
-        } else {
-            return posix_api->shutdown_fn(fd, how);
-        }
+    /* Can not use select_sock_posix_path() !
+     * When fd created by lwip_stocket() set as POSIX_KERNEL,
+     * lwip_close() is still required.
+     */
+    if (select_posix_path() == POSIX_KERNEL ||
+        POSIX_IS_CLOSED(lwip_get_socket(fd))) {
+        return posix_api->shutdown_fn(fd, how);
     }
-
     return g_wrap_api->shutdown_fn(fd, how);
 }
 
@@ -660,15 +622,13 @@ static int32_t do_ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *
     return ready;
 }
 
-typedef int32_t (*sigaction_fn)(int32_t signum, const struct sigaction *act, struct sigaction *oldact);
 static int32_t do_sigaction(int32_t signum, const struct sigaction *act, struct sigaction *oldact)
 {
-    if (posix_api == NULL) {
-        sigaction_fn sf = (sigaction_fn)dlsym(RTLD_NEXT, "sigaction");
-        if (sf == NULL) {
-            return -1;
+    if (unlikely(posix_api == NULL)) {
+        if (posix_api_init() != 0) {
+            GAZELLE_RETURN(EAGAIN);
         }
-        return sf(signum, act, oldact);
+        return posix_api->sigaction_fn(signum, act, oldact);
     }
 
     return lstack_sigaction(signum, act, oldact);
@@ -676,10 +636,14 @@ static int32_t do_sigaction(int32_t signum, const struct sigaction *act, struct
 
 static int32_t do_select(int32_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
 {
-    if ((select_posix_path() == POSIX_KERNEL) || !(readfds || writefds || exceptfds) || nfds == 0) {
+    if (nfds <= 0 || !(readfds || writefds || exceptfds)) {
+        GAZELLE_RETURN(EINVAL);
+    }
+
+    if (select_posix_path() == POSIX_KERNEL) {
         return posix_api->select_fn(nfds, readfds, writefds, exceptfds, timeout);
     }
-    
+
     return g_wrap_api->select_fn(nfds, readfds, writefds, exceptfds, timeout);
 }
 
@@ -690,9 +654,9 @@ static int32_t do_select(int32_t nfds, fd_set *readfds, fd_set *writefds, fd_set
         va_start(ap, _cmd); \
         val = va_arg(ap, typeof(val)); \
         va_end(ap); \
-        struct lwip_sock *sock = NULL; \
+        struct lwip_sock *sock = lwip_get_socket(_fd); \
         if (select_posix_path() == POSIX_KERNEL || \
-            select_fd_posix_path(_fd, &sock) == POSIX_KERNEL) \
+            select_sock_posix_path(sock) == POSIX_KERNEL) \
             return _fcntl_fn(_fd, _cmd, val); \
         int32_t ret1 = _fcntl_fn(_fd, _cmd, val); \
         if (ret1 == -1) { \
diff --git a/src/lstack/core/lstack_control_plane.c b/src/lstack/core/lstack_control_plane.c
index 5545b9f..f1e3064 100644
--- a/src/lstack/core/lstack_control_plane.c
+++ b/src/lstack/core/lstack_control_plane.c
@@ -764,7 +764,7 @@ void control_server_thread(void *arg)
     struct epoll_event evt_array;
     while (1) {
         /* wait init finish */
-        if (posix_api->ues_posix) {
+        if (posix_api->use_kernel) {
             usleep(GAZELLE_10MS);
             continue;
         }
@@ -813,7 +813,7 @@ void control_client_thread(void *arg)
 
     while (1) {
         /* wait init finish */
-        if (posix_api->ues_posix) {
+        if (posix_api->use_kernel) {
             usleep(GAZELLE_10MS);
             continue;
         }
diff --git a/src/lstack/core/lstack_init.c b/src/lstack/core/lstack_init.c
index 1b3882e..54ee97e 100644
--- a/src/lstack/core/lstack_init.c
+++ b/src/lstack/core/lstack_init.c
@@ -123,7 +123,7 @@ void gazelle_exit(void)
 
 __attribute__((destructor)) void gazelle_network_exit(void)
 {
-    if (posix_api != NULL && !posix_api->ues_posix) {
+    if (posix_api != NULL && !posix_api->use_kernel) {
         lwip_exit();
         gazelle_exit();
     }
@@ -324,7 +324,7 @@ __attribute__((constructor)) void gazelle_network_init(void)
         LSTACK_EXIT(1, "set_process_start_flag failed\n");
     }
 
-    posix_api->ues_posix = 0;
+    posix_api->use_kernel = 0;
     LSTACK_LOG(INFO, LSTACK, "gazelle_network_init success\n");
     rte_smp_mb();
 }
diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c
index 84ef782..2b39d05 100644
--- a/src/lstack/core/lstack_lwip.c
+++ b/src/lstack/core/lstack_lwip.c
@@ -79,6 +79,7 @@ static void reset_sock_data(struct lwip_sock *sock)
         sock->send_pre_del = NULL;
     }
 
+    sock->type = 0;
     sock->stack = NULL;
     sock->wakeup = NULL;
     sock->listen_next = NULL;
@@ -1167,7 +1168,7 @@ void do_lwip_connected_callback(struct netconn *conn)
 
     int32_t fd = conn->callback_arg.socket;
     struct lwip_sock *sock = lwip_get_socket(fd);
-    if (sock == NULL || sock->conn == NULL) {
+    if (POSIX_IS_CLOSED(sock)) {
         return;
     }
 
@@ -1208,7 +1209,7 @@ static void copy_pcb_to_conn(struct gazelle_stat_lstack_conn_info *conn, const s
         conn->fd = netconn->callback_arg.socket;
         conn->recv_cnt = (netconn->recvmbox == NULL) ? 0 : rte_ring_count(netconn->recvmbox->ring);
         struct lwip_sock *sock = lwip_get_socket(netconn->callback_arg.socket);
-        if (sock != NULL && sock->conn != NULL) {
+        if (!POSIX_IS_CLOSED(sock)) {
             conn->recv_ring_cnt = (sock->recv_ring == NULL) ? 0 : gazelle_ring_readable_count(sock->recv_ring);
             conn->recv_ring_cnt += (sock->recv_lastdata) ? 1 : 0;
             conn->send_ring_cnt = (sock->send_ring == NULL) ? 0 : gazelle_ring_readover_count(sock->send_ring);
diff --git a/src/lstack/core/lstack_preload.c b/src/lstack/core/lstack_preload.c
index 8cf4657..689d2bf 100644
--- a/src/lstack/core/lstack_preload.c
+++ b/src/lstack/core/lstack_preload.c
@@ -79,17 +79,22 @@ static void preload_get_thrdname(void)
     LSTACK_PRE_LOG(LSTACK_INFO, "thread name=%s ok\n", g_preload_info.env_thrdname);
 }
 
-enum posix_type select_fd_posix_path(int32_t fd, struct lwip_sock **socket)
+enum posix_type select_sock_posix_path(struct lwip_sock *sock)
 {
-    struct lwip_sock *sock = lwip_get_socket(fd);
-
-    /* AF_UNIX case */
-    if (!sock || !sock->conn || POSIX_IS_TYPE(sock, POSIX_KERNEL)) {
+    if (unlikely(posix_api == NULL)) {
+        /*
+        * read/write/readv/writev may not be sockfd,
+        * posix api maybe not init.
+        */
+        if (posix_api_init() != 0) {
+            LSTACK_PRE_LOG(LSTACK_ERR, "posix_api_init failed\n");
+        }
         return POSIX_KERNEL;
     }
 
-    if (socket) {
-        *socket = sock;
+    /* CLOSED means not sockfd, such as file fd or unix fd */
+    if (POSIX_IS_CLOSED(sock) || POSIX_IS_TYPE(sock, POSIX_KERNEL)) {
+        return POSIX_KERNEL;
     }
 
     if (likely(POSIX_IS_TYPE(sock, POSIX_LWIP))) {
@@ -112,11 +117,11 @@ enum posix_type select_posix_path(void)
         return POSIX_KERNEL;
     }
 
-    if (unlikely(posix_api->ues_posix)) {
+    if (unlikely(posix_api->use_kernel)) {
         return POSIX_KERNEL;
     }
 
-    if (g_preload_thrdpath != POSIX_ALL) {
+    if (likely(g_preload_thrdpath != POSIX_ALL)) {
         return g_preload_thrdpath;
     }
 
diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c
index d17cb67..a809d8c 100644
--- a/src/lstack/core/lstack_protocol_stack.c
+++ b/src/lstack/core/lstack_protocol_stack.c
@@ -127,7 +127,7 @@ struct protocol_stack *get_protocol_stack(void)
 struct protocol_stack *get_protocol_stack_by_fd(int32_t fd)
 {
     struct lwip_sock *sock = lwip_get_socket(fd);
-    if (sock == NULL || sock->conn == NULL) {
+    if (POSIX_IS_CLOSED(sock)) {
         return NULL;
     }
 
@@ -1126,7 +1126,7 @@ int32_t stack_broadcast_close(int32_t fd)
             ret = -1;
         }
 
-        if (sock == NULL || sock->conn == NULL) {
+        if (POSIX_IS_CLOSED(sock)) {
             break;
         }
         fd = sock->conn->callback_arg.socket;
@@ -1151,7 +1151,7 @@ int stack_broadcast_shutdown(int fd, int how)
             ret = -1;
         }
 
-        if (sock == NULL || sock->conn == NULL) {
+        if (POSIX_IS_CLOSED(sock)) {
             break;
         }
         fd = sock->conn->callback_arg.socket;
@@ -1357,7 +1357,7 @@ static void stack_all_fds_close(void)
 {
     for (int i = 3; i < GAZELLE_MAX_CLIENTS + GAZELLE_RESERVED_CLIENTS; i++) {
         struct lwip_sock *sock = lwip_get_socket(i);
-        if (sock && sock->conn && sock->stack == get_protocol_stack()) {
+        if (!POSIX_IS_CLOSED(sock) && sock->stack == get_protocol_stack()) {
             lwip_close(i);
         }
     }
diff --git a/src/lstack/include/lstack_preload.h b/src/lstack/include/lstack_preload.h
index c30736a..d4ad385 100644
--- a/src/lstack/include/lstack_preload.h
+++ b/src/lstack/include/lstack_preload.h
@@ -11,9 +11,9 @@
 */
 #ifndef __LSTACK_PRELOAD_H__
 #define __LSTACK_PRELOAD_H__
-#include <stdbool.h>
 
 enum posix_type select_posix_path(void);
-enum posix_type select_fd_posix_path(int32_t fd, struct lwip_sock **socket);
+enum posix_type select_sock_posix_path(struct lwip_sock *sock);
 int preload_info_init(void);
-#endif
+
+#endif /* __LSTACK_PRELOAD_H__ */
diff --git a/src/lstack/netif/lstack_flow.c b/src/lstack/netif/lstack_flow.c
index 84c5c61..1ca3314 100644
--- a/src/lstack/netif/lstack_flow.c
+++ b/src/lstack/netif/lstack_flow.c
@@ -19,6 +19,7 @@
 #include <uthash.h>
 
 #include <lwip/lwipgz_posix_api.h>
+#include <lwip/sys.h>
 #include <lwip/tcp.h>
 #include <lwip/prot/tcp.h>
 
-- 
2.33.0

