From 7a17ef823bd1087add6c28bad54cc91316b3a5a7 Mon Sep 17 00:00:00 2001
From: Lemmy Huang <huangliming5@huawei.com>
Date: Fri, 12 Jul 2024 10:32:40 +0800
Subject: [PATCH] cleancode: refactor sys_now and lwip_ioctl

Signed-off-by: Lemmy Huang <huangliming5@huawei.com>
---
 src/api/api_msg.c           |  2 --
 src/api/sockets.c           | 28 -----------------
 src/api/sys_arch.c          | 62 +++++++++++++++++++++++--------------
 src/api/tcpip.c             | 34 +++-----------------
 src/core/init.c             |  1 +
 src/core/timeouts.c         | 12 -------
 src/include/arch/sys_arch.h | 17 +++-------
 src/include/lwip/sockets.h  | 23 ++++----------
 src/include/lwip/tcp.h      | 17 ----------
 src/include/lwip/tcpip.h    |  2 +-
 src/include/lwip/timeouts.h |  4 ---
 src/include/lwipgz_sock.h   |  1 -
 12 files changed, 55 insertions(+), 148 deletions(-)

diff --git a/src/api/api_msg.c b/src/api/api_msg.c
index 4234084..acd4697 100644
--- a/src/api/api_msg.c
+++ b/src/api/api_msg.c
@@ -55,9 +55,7 @@
 #include "lwip/priv/tcpip_priv.h"
 
 #if GAZELLE_ENABLE
-#include "lwip/sockets.h"
 #include "lwipgz_sock.h"
-#include "lwipgz_posix_api.h"
 #endif
 
 #include <string.h>
diff --git a/src/api/sockets.c b/src/api/sockets.c
index 3c0c982..3e64f66 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -4086,33 +4086,6 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_
   return err;
 }
 
-#if GAZELLE_ENABLE
-int
-lwip_ioctl(int s, long cmd, ...)
-{
-  struct lwip_sock *sock = get_socket(s);
-  u8_t val;
-
-#if LWIP_SO_RCVBUF
-  int recv_avail;
-#endif /* LWIP_SO_RCVBUF */
-
-  int ret = -1;
-  void *argp;
-  va_list ap;
-
-  va_start(ap, cmd);
-  argp = va_arg(ap, void *);
-  va_end(ap);
-
-  if (!sock) {
-    return posix_api->ioctl_fn(s, cmd, argp);
-  }
-  if (POSIX_HAS_TYPE(sock, POSIX_KERNEL)) {
-    if ((ret = posix_api->ioctl_fn(s, cmd, argp)) == -1)
-      return ret;
-  }
-#else
 int
 lwip_ioctl(int s, long cmd, void *argp)
 {
@@ -4125,7 +4098,6 @@ lwip_ioctl(int s, long cmd, void *argp)
   if (!sock) {
     return -1;
   }
-#endif /* GAZELLE_ENABLE */
 
   switch (cmd) {
 #if LWIP_SO_RCVBUF || LWIP_FIONREAD_LINUXMODE
diff --git a/src/api/sys_arch.c b/src/api/sys_arch.c
index 3586357..dfcdc2f 100644
--- a/src/api/sys_arch.c
+++ b/src/api/sys_arch.c
@@ -37,15 +37,52 @@
 
 #include <rte_memzone.h>
 #include <rte_malloc.h>
+#include <rte_cycles.h>
 
 #include "lwip/err.h"
-#include "lwip/mem.h"
 #include "lwip/memp.h"
 #include "lwip/opt.h"
 #include "lwip/sys.h"
 #include "lwip/timeouts.h"
 #include "arch/sys_arch.h"
 
+static u64_t g_sys_cycles_per_ms = 0;
+static u64_t g_sys_cycles_per_us = 0;
+
+/*
+ * Timer
+ * */
+void sys_timer_init(void)
+{
+    u64_t freq = rte_get_tsc_hz();
+    if (g_sys_cycles_per_ms == 0) {
+        g_sys_cycles_per_ms = (freq + MS_PER_S - 1) / MS_PER_S;
+    }
+    if (g_sys_cycles_per_us == 0) {
+        g_sys_cycles_per_us = (freq + US_PER_S - 1) / US_PER_S;;
+    }
+}
+
+u32_t sys_now(void)
+{
+    return (u32_t)(rte_rdtsc() / g_sys_cycles_per_ms);
+}
+
+u64_t sys_now_us(void)
+{
+    return (rte_rdtsc() / g_sys_cycles_per_us);
+}
+
+void sys_timer_run(void)
+{
+  u32_t sleeptime;
+
+  sleeptime = sys_timeouts_sleeptime();
+  if (sleeptime == 0) {
+    sys_check_timeouts();
+  }
+}
+
 struct sys_mutex {
     volatile unsigned int m;
 };
@@ -75,9 +112,6 @@ struct sys_mem_stats {
 
 static PER_THREAD struct sys_mem_stats hugepage_stats;
 
-static uint64_t cycles_per_ms __attribute__((aligned(64)));
-static uint64_t sys_start_ms __attribute__((aligned(64)));
-
 /*
  * Mailbox
  * */
@@ -369,26 +403,6 @@ void sys_mutex_free(struct sys_mutex **mutex)
 {
 }
 
-/* Timer from DPDK */
-void sys_calibrate_tsc(void)
-{
-#define MS_PER_SEC  1E3
-    uint64_t freq = rte_get_tsc_hz();
-
-    if (cycles_per_ms == 0) {
-        cycles_per_ms = (freq + MS_PER_SEC - 1) / MS_PER_SEC;
-    }
-    if (sys_start_ms == 0) {
-        sys_start_ms = rte_rdtsc() / cycles_per_ms;
-    }
-}
-
-uint32_t sys_now(void)
-{
-    uint64_t cur_ms = rte_rdtsc() / cycles_per_ms;
-    return (uint32_t)(cur_ms - sys_start_ms);
-}
-
 /*
  * Critical section
  * */
diff --git a/src/api/tcpip.c b/src/api/tcpip.c
index d0d3cba..3aecbd4 100644
--- a/src/api/tcpip.c
+++ b/src/api/tcpip.c
@@ -56,13 +56,13 @@
 #define TCPIP_MSG_VAR_FREE(name)    API_VAR_FREE(MEMP_TCPIP_MSG_API, name)
 
 /* global variables */
-static PER_THREAD tcpip_init_done_fn tcpip_init_done;
-static PER_THREAD void *tcpip_init_done_arg;
-static PER_THREAD sys_mbox_t tcpip_mbox;
+static tcpip_init_done_fn tcpip_init_done;
+static void *tcpip_init_done_arg;
+static sys_mbox_t tcpip_mbox;
 
 #if LWIP_TCPIP_CORE_LOCKING
 /** The global semaphore to lock the stack. */
-PER_THREAD sys_mutex_t lock_tcpip_core;
+sys_mutex_t lock_tcpip_core;
 #endif /* LWIP_TCPIP_CORE_LOCKING */
 
 static void tcpip_thread_handle_msg(struct tcpip_msg *msg);
@@ -123,13 +123,8 @@ again:
  *
  * @param arg unused argument
  */
-#if GAZELLE_ENABLE
-__attribute__((unused)) static void
-tcpip_thread(void *arg)
-#else
 static void
 tcpip_thread(void *arg)
-#endif /* GAZELLE_ENABLE */
 {
   struct tcpip_msg *msg;
   LWIP_UNUSED_ARG(arg);
@@ -252,9 +247,6 @@ tcpip_inpkt(struct pbuf *p, struct netif *inp, netif_input_fn input_fn)
 #if LWIP_TCPIP_CORE_LOCKING_INPUT
   err_t ret;
   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_inpkt: PACKET %p/%p\n", (void *)p, (void *)inp));
-#if GAZELLE_ENABLE && LWIP_TIMERS
-  sys_timer_run();
-#endif
   LOCK_TCPIP_CORE();
   ret = input_fn(p, inp);
   UNLOCK_TCPIP_CORE();
@@ -334,9 +326,6 @@ tcpip_callback(tcpip_callback_fn function, void *ctx)
   msg->msg.cb.function = function;
   msg->msg.cb.ctx = ctx;
 
-#if GAZELLE_ENABLE && LWIP_TIMER
-  sys_timer_run();
-#endif
   sys_mbox_post(&tcpip_mbox, msg);
   return ERR_OK;
 }
@@ -373,9 +362,6 @@ tcpip_try_callback(tcpip_callback_fn function, void *ctx)
   msg->msg.cb.function = function;
   msg->msg.cb.ctx = ctx;
 
-#if GAZELLE_ENABLE && LWIP_TIMER
-  sys_timer_run();
-#endif
   if (sys_mbox_trypost(&tcpip_mbox, msg) != ERR_OK) {
     memp_free(MEMP_TCPIP_MSG_API, msg);
     return ERR_MEM;
@@ -457,9 +443,6 @@ tcpip_send_msg_wait_sem(tcpip_callback_fn fn, void *apimsg, sys_sem_t *sem)
 {
 #if LWIP_TCPIP_CORE_LOCKING
   LWIP_UNUSED_ARG(sem);
-#if GAZELLE_ENABLE && LWIP_TIMERS
-  sys_timer_run();
-#endif
   LOCK_TCPIP_CORE();
   fn(apimsg);
   UNLOCK_TCPIP_CORE();
@@ -497,9 +480,6 @@ tcpip_api_call(tcpip_api_call_fn fn, struct tcpip_api_call_data *call)
 #if LWIP_TCPIP_CORE_LOCKING
   err_t err;
   LOCK_TCPIP_CORE();
-#if GAZELLE_ENABLE && LWIP_TIMERS
-  sys_timer_run();
-#endif
   err = fn(call);
   UNLOCK_TCPIP_CORE();
   return err;
@@ -562,10 +542,6 @@ tcpip_callbackmsg_new(tcpip_callback_fn function, void *ctx)
   msg->type = TCPIP_MSG_CALLBACK_STATIC;
   msg->msg.cb.function = function;
   msg->msg.cb.ctx = ctx;
-
-#if GAZELLE_ENABLE && LWIP_TIMER
-  sys_timer_run();
-#endif
   return (struct tcpip_callback_msg *)msg;
 }
 
@@ -686,9 +662,7 @@ tcpip_init(tcpip_init_done_fn initfunc, void *arg)
   }
 #endif /* LWIP_TCPIP_CORE_LOCKING */
 
-#if !GAZELLE_ENABLE
   sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
-#endif
 }
 
 /**
diff --git a/src/core/init.c b/src/core/init.c
index 9f130a6..bb24092 100644
--- a/src/core/init.c
+++ b/src/core/init.c
@@ -356,6 +356,7 @@ lwip_init(void)
 #endif
 
 #if GAZELLE_ENABLE
+  sys_timer_init();
   if (hugepage_init() != 0) {
     rte_exit(EXIT_FAILURE, "hugepage init failed\n");
   }
diff --git a/src/core/timeouts.c b/src/core/timeouts.c
index 610a2d7..50fec3e 100644
--- a/src/core/timeouts.c
+++ b/src/core/timeouts.c
@@ -442,18 +442,6 @@ sys_timeouts_sleeptime(void)
   }
 }
 
-#if GAZELLE_ENABLE
-void sys_timer_run(void)
-{
-  u32_t sleeptime;
-
-  sleeptime = sys_timeouts_sleeptime();
-  if (sleeptime == 0) {
-    sys_check_timeouts();
-  }
-}
-#endif /* GAZELLE_ENABLE */
-
 #else /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */
 /* Satisfy the TCP code which calls this function */
 void
diff --git a/src/include/arch/sys_arch.h b/src/include/arch/sys_arch.h
index 0699d76..04ef282 100644
--- a/src/include/arch/sys_arch.h
+++ b/src/include/arch/sys_arch.h
@@ -76,6 +76,11 @@ int sys_mbox_empty(struct sys_mbox *);
 struct sys_thread;
 typedef struct sys_thread *sys_thread_t;
 
+void sys_timer_init(void);
+void sys_timer_run(void);
+u32_t sys_now(void);
+u64_t sys_now_us(void);
+
 #if GAZELLE_ENABLE
 extern int eth_dev_poll(void);
 #include <rte_ring.h>
@@ -129,16 +134,4 @@ struct rte_ring *gazelle_ring_create_fast(const char *name, uint32_t size, uint3
 
 #endif
 
-void sys_calibrate_tsc(void);
-uint32_t sys_now(void);
-__attribute__((always_inline)) inline int update_timeout(int timeout, uint32_t poll_ts)
-{
-    uint32_t used_ms = sys_now() - poll_ts;
-    if (timeout > 0 && used_ms < timeout) {
-        return timeout;
-    } else {
-        return 0;
-    }
-}
-
 #endif /* _LWIP_ARCH_SYS_ARCH_H_ */
diff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h
index 2b2d1ef..a5364c7 100644
--- a/src/include/lwip/sockets.h
+++ b/src/include/lwip/sockets.h
@@ -607,7 +607,7 @@ struct pollfd
   short revents;
 };
 #endif
-#endif /* LWIP_SOCKET_POLL */
+#endif /* GAZELLE_ENABLE */
 
 /** LWIP_TIMEVAL_PRIVATE: if you want to use the struct timeval provided
  * by your system, set this to 0 and include <sys/time.h> in cc.h */
@@ -680,8 +680,12 @@ int fcntl(int s, int cmd, ...);
 #endif /* LWIP_POSIX_SOCKETS_IO_NAMES */
 #endif /* LWIP_COMPAT_SOCKETS == 2 */
 
-int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
+#if GAZELLE_ENABLE
+int lwip_sock_make_addr(struct netconn *conn, ip_addr_t *fromaddr, u16_t port,
+                    struct sockaddr *from, socklen_t *fromlen);
 int lwip_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags);
+#endif
+int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
 int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
 int lwip_shutdown(int s, int how);
 int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen);
@@ -711,16 +715,8 @@ int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptse
 #if LWIP_SOCKET_POLL
 int lwip_poll(struct pollfd *fds, nfds_t nfds, int timeout);
 #endif
-
-#if GAZELLE_ENABLE
-int lwip_ioctl(int s, long cmd, ...);
-int lwip_fcntl(int s, int cmd, int val);
-int lwip_sock_make_addr(struct netconn *conn, ip_addr_t *fromaddr, u16_t port,
-                    struct sockaddr *from, socklen_t *fromlen);
-#else
 int lwip_ioctl(int s, long cmd, void *argp);
 int lwip_fcntl(int s, int cmd, int val);
-#endif /* GAZELLE_ENABLE */
 
 #if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
 /* Protocol Independent Multicast API [RFC3678] */
@@ -894,17 +890,10 @@ int lwip_inet_pton(int af, const char *src, void *dst);
 #define writev(s,iov,iovcnt)                      lwip_writev(s,iov,iovcnt)
 /** @ingroup socket */
 #define close(s)                                  lwip_close(s)
-
-#if GAZELLE_ENABLE
-#define fcntl(s,cmd...)                           lwip_fcntl(s,cmd)
-#define ioctl(s,cmd...)                           lwip_ioctl(s,cmd)
-#else
 /** @ingroup socket */
 #define fcntl(s,cmd,val)                          lwip_fcntl(s,cmd,val)
 /** @ingroup socket */
 #define ioctl(s,cmd,argp)                         lwip_ioctl(s,cmd,argp)
-#endif /* GAZELLE_ENABLE */
-
 #endif /* LWIP_POSIX_SOCKETS_IO_NAMES */
 #endif /* LWIP_COMPAT_SOCKETS != 2 */
 
diff --git a/src/include/lwip/tcp.h b/src/include/lwip/tcp.h
index 44a9a5a..9c8a2ca 100644
--- a/src/include/lwip/tcp.h
+++ b/src/include/lwip/tcp.h
@@ -513,23 +513,6 @@ err_t            tcp_tcp_get_tcp_addrinfo(struct tcp_pcb *pcb, int local, ip_add
 
 #define tcp_dbg_get_tcp_state(pcb) ((pcb)->state)
 
-enum tcp_list_state {
-  ACTIVE_LIST,
-  LISTEN_LIST,
-  TIME_WAIT_LIST,
-};
-
-struct tcp_pcb_dp {
-  uint32_t state;
-  uint32_t lip;
-  uint32_t rip;
-  uint16_t l_port;
-  uint16_t r_port;
-  uint32_t r_next;
-  uint32_t s_next;
-  uint32_t tcp_sub_state;
-};
-
 /* for compatibility with older implementation */
 #define tcp_new_ip6() tcp_new_ip_type(IPADDR_TYPE_V6)
 
diff --git a/src/include/lwip/tcpip.h b/src/include/lwip/tcpip.h
index 76e99c1..30ce4fe 100644
--- a/src/include/lwip/tcpip.h
+++ b/src/include/lwip/tcpip.h
@@ -51,7 +51,7 @@ extern "C" {
 
 #if LWIP_TCPIP_CORE_LOCKING
 /** The global semaphore to lock the stack. */
-extern PER_THREAD sys_mutex_t lock_tcpip_core;
+extern sys_mutex_t lock_tcpip_core;
 #if !defined LOCK_TCPIP_CORE || defined __DOXYGEN__
 /** Lock lwIP core mutex (needs @ref LWIP_TCPIP_CORE_LOCKING 1) */
 #define LOCK_TCPIP_CORE()     sys_mutex_lock(&lock_tcpip_core)
diff --git a/src/include/lwip/timeouts.h b/src/include/lwip/timeouts.h
index f7ffc5e..b601f9e 100644
--- a/src/include/lwip/timeouts.h
+++ b/src/include/lwip/timeouts.h
@@ -119,10 +119,6 @@ struct sys_timeo** sys_timeouts_get_next_timeout(void);
 void lwip_cyclic_timer(void *arg);
 #endif
 
-#if GAZELLE_ENABLE
-void sys_timer_run(void);
-#endif /* GAZELLE_ENABLE */
-
 #endif /* LWIP_TIMERS */
 
 #ifdef __cplusplus
diff --git a/src/include/lwipgz_sock.h b/src/include/lwipgz_sock.h
index 3845453..16b5e85 100644
--- a/src/include/lwipgz_sock.h
+++ b/src/include/lwipgz_sock.h
@@ -72,7 +72,6 @@ void gazelle_free_socket(struct lwip_sock *sock, int fd);
 void lwip_sock_init(void);
 void lwip_exit(void);
 
-
 extern int do_lwip_init_sock(int fd);
 extern void do_lwip_clean_sock(int fd);
 
-- 
2.33.0

