From bc2b6b2163b3bc2ef6754d13f362c23f720994dd Mon Sep 17 00:00:00 2001
From: Lemmy Huang <huangliming5@huawei.com>
Date: Wed, 10 Jul 2024 16:08:46 +0800
Subject: [PATCH] cleancode: refactor posix type and get_socket

  Changed:
    get_socket_by_fd -> lwip_get_socket
    get_socket

    SET_CONN_TYPE_LIBOS_OR_HOST   -> POSIX_SET_TYPE
    SET_CONN_TYPE_LIBOS
    SET_CONN_TYPE_HOST

    CONN_TYPE_IS_LIBOS            -> POSIX_IS_TYPE
    CONN_TYPE_IS_HOST

    CONN_TYPE_HAS_LIBOS_AND_HOST  -> POSIX_HAS_TYPE
    CONN_TYPE_HAS_LIBOS
    CONN_TYPE_HAS_HOST

Signed-off-by: Lemmy Huang <huangliming5@huawei.com>
---
 src/api/lwipgz_sock.c     |  2 +-
 src/api/sockets.c         | 13 +++----------
 src/core/tcp.c            |  2 +-
 src/include/lwip/api.h    | 35 -----------------------------------
 src/include/lwipgz_sock.h | 22 ++++++++++++++++++++--
 5 files changed, 25 insertions(+), 49 deletions(-)

diff --git a/src/api/lwipgz_sock.c b/src/api/lwipgz_sock.c
index 4c1ecd0..5a5a5fd 100644
--- a/src/api/lwipgz_sock.c
+++ b/src/api/lwipgz_sock.c
@@ -86,7 +86,7 @@ int gazelle_alloc_socket(struct netconn *newconn, int accepted, int flags)
   fd = socket_new_sysfd(newconn, flags);
   if (fd < 0)
     return -1;
-  sock = get_socket_by_fd(fd);
+  sock = lwip_get_socket(fd);
   if (sock == NULL)
     goto out;
 
diff --git a/src/api/sockets.c b/src/api/sockets.c
index c52f286..3c0c982 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -523,7 +523,7 @@ tryget_socket(int fd)
  * @param fd externally used socket index
  * @return struct lwip_sock for the socket or NULL if not found
  */
-struct lwip_sock *
+static struct lwip_sock *
 get_socket(int fd)
 {
   struct lwip_sock *sock = tryget_socket(fd);
@@ -538,15 +538,8 @@ get_socket(int fd)
 }
 
 #if GAZELLE_ENABLE
-/**
- * Map a externally used socket index to the internal socket representation.
- *
- * @param s externally used socket index
- * @return struct lwip_sock for the socket or NULL if not found without
- * checking.
- */
 struct lwip_sock *
-get_socket_by_fd(int fd)
+lwip_get_socket(int fd)
 {
   return tryget_socket_unconn_nouse(fd);
 }
@@ -4115,7 +4108,7 @@ lwip_ioctl(int s, long cmd, ...)
   if (!sock) {
     return posix_api->ioctl_fn(s, cmd, argp);
   }
-  if (CONN_TYPE_HAS_HOST(sock->conn)) {
+  if (POSIX_HAS_TYPE(sock, POSIX_KERNEL)) {
     if ((ret = posix_api->ioctl_fn(s, cmd, argp)) == -1)
       return ret;
   }
diff --git a/src/core/tcp.c b/src/core/tcp.c
index 5c7d317..2f29f1f 100644
--- a/src/core/tcp.c
+++ b/src/core/tcp.c
@@ -258,7 +258,7 @@ tcp_free(struct tcp_pcb *pcb)
     rte_ring_free(pcb->client_rx_ring);
     rte_ring_free(pcb->client_tx_ring);
     netconn = (struct netconn *)pcb->callback_arg;
-    sock = get_socket(netconn->callback_arg.socket);
+    sock = lwip_get_socket(netconn->callback_arg.socket);
     rte_memzone_free(sock->same_node_rx_ring->mz);
     rte_memzone_free(sock->same_node_rx_ring_mz);
     rte_memzone_free(sock->same_node_tx_ring->mz);
diff --git a/src/include/lwip/api.h b/src/include/lwip/api.h
index 319ee09..0322a04 100644
--- a/src/include/lwip/api.h
+++ b/src/include/lwip/api.h
@@ -143,43 +143,8 @@ enum netconn_type {
   /** Raw connection IPv6 (dual-stack by default, unless you call @ref netconn_set_ipv6only) */
   , NETCONN_RAW_IPV6    = NETCONN_RAW | NETCONN_TYPE_IPV6 /* 0x48 */
 #endif /* LWIP_IPV6 */
-
-#if GAZELLE_ENABLE
-  /*here must bigger than 0xff, because (type & 0xff) is for lwip inner use*/
-  , NETCONN_LIBOS = 0x100
-  , NETCONN_HOST = 0x200
-  , NETCONN_INPRG = 0x400
-  , NETCONN_STACK = NETCONN_LIBOS | NETCONN_HOST | NETCONN_INPRG
-#endif /* GAZELLE_ENABLE */
 };
 
-#ifdef GAZELLE_ENABLE
-#define SET_CONN_TYPE_LIBOS_OR_HOST(conn) do { \
-  conn->type &= ~(NETCONN_STACK); \
-  conn->type |= (NETCONN_LIBOS | NETCONN_HOST); } while (0)
-#define SET_CONN_TYPE_LIBOS(conn) do { \
-  conn->type &= ~(NETCONN_STACK); \
-  conn->type |= NETCONN_LIBOS; } while (0)
-#define SET_CONN_TYPE_HOST(conn) do { \
-  conn->type &= ~(NETCONN_STACK); \
-  conn->type |= NETCONN_HOST; } while (0)
-#define ADD_CONN_TYPE_INPRG(conn) do { \
-  conn->type |= NETCONN_INPRG; } while(0)
-#define CONN_TYPE_HAS_LIBOS_AND_HOST(conn) ((conn->type & (NETCONN_LIBOS | NETCONN_HOST)) == (NETCONN_LIBOS | NETCONN_HOST))
-#define CONN_TYPE_HAS_LIBOS(conn) (conn->type & NETCONN_LIBOS)
-#define CONN_TYPE_HAS_HOST(conn) (conn->type & NETCONN_HOST)
-#define CONN_TYPE_HAS_INPRG(conn) (!!(conn->type & NETCONN_INPRG))
-#define CONN_TYPE_IS_LIBOS(conn)  (!!(NETCONN_LIBOS == (conn->type & NETCONN_STACK)))
-#define CONN_TYPE_IS_HOST(conn)   (!!(NETCONN_HOST == (conn->type & NETCONN_STACK)))
-#else
-#define SET_CONN_TYPE_LIBOS_OR_HOST(conn) do {} while (0)
-#define SET_CONN_TYPE_LIBOS(conn) do {} while (0)
-#define SET_CONN_TYPE_HOST(conn) do {} while (0)
-#define CONN_TYPE_HAS_LIBOS_AND_HOST(conn) (0)
-#define CONN_TYPE_HAS_LIBOS(conn) (0)
-#define CONN_TYPE_HAS_HOST(conn) (0)
-#endif /* GAZELLE_ENABLE */
-
 /** Current state of the netconn. Non-TCP netconns are always
  * in state NETCONN_NONE! */
 enum netconn_state {
diff --git a/src/include/lwipgz_sock.h b/src/include/lwipgz_sock.h
index f5581cb..2fa9d7a 100644
--- a/src/include/lwipgz_sock.h
+++ b/src/include/lwipgz_sock.h
@@ -44,8 +44,25 @@
 
 #define set_errno(err) do { errno = (err); } while(0)
 
-struct lwip_sock *get_socket(int fd);
-struct lwip_sock *get_socket_by_fd(int fd);
+enum posix_type {
+  POSIX_KERNEL  = 0x100,
+  POSIX_LWIP    = 0x200,
+  POSIX_EPOLL   = 0x400,
+  POSIX_ALL     = POSIX_KERNEL | POSIX_LWIP | POSIX_EPOLL,
+  POSIX_LWIP_OR_KERNEL = POSIX_LWIP | POSIX_KERNEL,
+};
+
+#define POSIX_SET_TYPE(sock, posix_type)  do { \
+  (sock)->type &= ~(POSIX_ALL);                \
+  (sock)->type |= (posix_type);           } while (0)
+
+#define POSIX_HAS_TYPE(sock, posix_type) \
+  ((sock)->type & (posix_type))
+
+#define POSIX_IS_TYPE(sock, posix_type) \
+  (((sock)->type & POSIX_ALL) == (posix_type))
+
+struct lwip_sock *lwip_get_socket(int fd);
 int gazelle_alloc_socket(struct netconn *newconn, int accepted, int flags);
 void gazelle_free_socket(struct lwip_sock *sock, int fd);
 void lwip_sock_init(void);
@@ -156,6 +173,7 @@ struct lwip_sock {
 
   char pad3 __rte_cache_aligned;
   /* nerver change */
+  enum posix_type type;
   struct lwip_sock *listen_next; /* listenfd list */
   struct protocol_stack *stack;
   struct wakeup_poll *wakeup;
-- 
2.33.0

