From 6424911a074a84941e7c1bb98b40b9f8683418f4 Mon Sep 17 00:00:00 2001 From: Lemmy Huang Date: Thu, 18 Jul 2024 09:38:04 +0800 Subject: [PATCH] cleancode: refactor posix_api Signed-off-by: Lemmy Huang (cherry picked from commit 372566ff31eea6f3eec6aa3434d390503fcbf44a) --- 0150-cleancode-refactor-posix_api.patch | 271 ++++++++++++++++++++++++ lwip.spec | 6 +- 2 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 0150-cleancode-refactor-posix_api.patch diff --git a/0150-cleancode-refactor-posix_api.patch b/0150-cleancode-refactor-posix_api.patch new file mode 100644 index 0000000..1aa5e28 --- /dev/null +++ b/0150-cleancode-refactor-posix_api.patch @@ -0,0 +1,271 @@ +From 99a688eb9c398d53dbb11ce4b594662a1eff810d Mon Sep 17 00:00:00 2001 +From: Lemmy Huang +Date: Wed, 10 Jul 2024 17:35:25 +0800 +Subject: [PATCH] cleancode: refactor posix_api + +Signed-off-by: Lemmy Huang +--- + src/api/lwipgz_posix_api.c | 112 +++++++++++++++------------------ + src/include/lwipgz_posix_api.h | 68 ++++++++++---------- + src/include/lwipgz_sock.h | 4 ++ + 3 files changed, 89 insertions(+), 95 deletions(-) + +diff --git a/src/api/lwipgz_posix_api.c b/src/api/lwipgz_posix_api.c +index c045496..b4d53a2 100644 +--- a/src/api/lwipgz_posix_api.c ++++ b/src/api/lwipgz_posix_api.c +@@ -30,39 +30,21 @@ + * + */ + ++// #include ++// #include ++// #include + #include +-#include +-#include + #include +-#include +-#include + +-#include +- +-#include "lwip/err.h" +-#include "lwipgz_sock.h" + #include "lwipgz_posix_api.h" ++#include "lwipgz_sock.h" + +-posix_api_t *posix_api; +-posix_api_t posix_api_val; +- +-static int chld_is_epfd(int fd) +-{ +- return 0; +-} +- +-void posix_api_fork(void) +-{ +- /* lstack helper api */ +- posix_api->ues_posix = 1; +- posix_api->is_epfd = chld_is_epfd; +-} +- ++posix_api_t *posix_api = NULL; ++static posix_api_t posix_api_val; + + int posix_api_init(void) + { +-/* the symbol we use here won't be NULL, so we don't need dlerror() +- to test error */ ++/* the symbol we use here won't be NULL, so we don't need dlerror() to test error */ + #define CHECK_DLSYM_RET_RETURN(ret) do { \ + if ((ret) == NULL) \ + goto err_out; \ +@@ -73,48 +55,52 @@ int posix_api_init(void) + void *__restrict handle = RTLD_NEXT; + + /* glibc standard api */ +- CHECK_DLSYM_RET_RETURN(posix_api->socket_fn = dlsym(handle, "socket")); +- CHECK_DLSYM_RET_RETURN(posix_api->accept_fn = dlsym(handle, "accept")); +- CHECK_DLSYM_RET_RETURN(posix_api->accept4_fn = dlsym(handle, "accept4")); +- CHECK_DLSYM_RET_RETURN(posix_api->bind_fn = dlsym(handle, "bind")); +- CHECK_DLSYM_RET_RETURN(posix_api->listen_fn = dlsym(handle, "listen")); +- CHECK_DLSYM_RET_RETURN(posix_api->connect_fn = dlsym(handle, "connect")); +- CHECK_DLSYM_RET_RETURN(posix_api->setsockopt_fn = dlsym(handle, "setsockopt")); +- CHECK_DLSYM_RET_RETURN(posix_api->getsockopt_fn = dlsym(handle, "getsockopt")); +- CHECK_DLSYM_RET_RETURN(posix_api->getpeername_fn = dlsym(handle, "getpeername")); +- CHECK_DLSYM_RET_RETURN(posix_api->getsockname_fn = dlsym(handle, "getsockname")); +- CHECK_DLSYM_RET_RETURN(posix_api->shutdown_fn = dlsym(handle, "shutdown")); +- CHECK_DLSYM_RET_RETURN(posix_api->close_fn = dlsym(handle, "close")); +- CHECK_DLSYM_RET_RETURN(posix_api->read_fn = dlsym(handle, "read")); +- CHECK_DLSYM_RET_RETURN(posix_api->readv_fn = dlsym(handle, "readv")); +- CHECK_DLSYM_RET_RETURN(posix_api->write_fn = dlsym(handle, "write")); +- CHECK_DLSYM_RET_RETURN(posix_api->writev_fn = dlsym(handle, "writev")); +- CHECK_DLSYM_RET_RETURN(posix_api->recv_fn = dlsym(handle, "recv")); +- CHECK_DLSYM_RET_RETURN(posix_api->send_fn = dlsym(handle, "send")); +- CHECK_DLSYM_RET_RETURN(posix_api->recv_msg = dlsym(handle, "recvmsg")); +- CHECK_DLSYM_RET_RETURN(posix_api->send_msg = dlsym(handle, "sendmsg")); +- CHECK_DLSYM_RET_RETURN(posix_api->recv_from = dlsym(handle, "recvfrom")); +- CHECK_DLSYM_RET_RETURN(posix_api->send_to = dlsym(handle, "sendto")); +- CHECK_DLSYM_RET_RETURN(posix_api->fcntl_fn = dlsym(handle, "fcntl")); +- CHECK_DLSYM_RET_RETURN(posix_api->fcntl64_fn = dlsym(handle, "fcntl64")); +- CHECK_DLSYM_RET_RETURN(posix_api->pipe_fn = dlsym(handle, "pipe")); +- CHECK_DLSYM_RET_RETURN(posix_api->epoll_create_fn = dlsym(handle, "epoll_create")); +- CHECK_DLSYM_RET_RETURN(posix_api->epoll_create1_fn = dlsym(handle, "epoll_create1")); +- CHECK_DLSYM_RET_RETURN(posix_api->epoll_ctl_fn = dlsym(handle, "epoll_ctl")); +- CHECK_DLSYM_RET_RETURN(posix_api->epoll_wait_fn = dlsym(handle, "epoll_wait")); +- CHECK_DLSYM_RET_RETURN(posix_api->fork_fn = dlsym(handle, "fork")); +- CHECK_DLSYM_RET_RETURN(posix_api->eventfd_fn = dlsym(handle, "eventfd")); +- CHECK_DLSYM_RET_RETURN(posix_api->sigaction_fn = dlsym(handle, "sigaction")); +- CHECK_DLSYM_RET_RETURN(posix_api->poll_fn = dlsym(handle, "poll")); +- CHECK_DLSYM_RET_RETURN(posix_api->ioctl_fn = dlsym(handle, "ioctl")); +- CHECK_DLSYM_RET_RETURN(posix_api->select_fn = dlsym(handle, "select")); ++ CHECK_DLSYM_RET_RETURN(posix_api->shutdown_fn = dlsym(handle, "shutdown")); ++ CHECK_DLSYM_RET_RETURN(posix_api->close_fn = dlsym(handle, "close")); ++ CHECK_DLSYM_RET_RETURN(posix_api->socket_fn = dlsym(handle, "socket")); ++ CHECK_DLSYM_RET_RETURN(posix_api->connect_fn = dlsym(handle, "connect")); ++ CHECK_DLSYM_RET_RETURN(posix_api->bind_fn = dlsym(handle, "bind")); ++ CHECK_DLSYM_RET_RETURN(posix_api->listen_fn = dlsym(handle, "listen")); ++ CHECK_DLSYM_RET_RETURN(posix_api->accept_fn = dlsym(handle, "accept")); ++ CHECK_DLSYM_RET_RETURN(posix_api->accept4_fn = dlsym(handle, "accept4")); ++ ++ CHECK_DLSYM_RET_RETURN(posix_api->getpeername_fn = dlsym(handle, "getpeername")); ++ CHECK_DLSYM_RET_RETURN(posix_api->getsockname_fn = dlsym(handle, "getsockname")); ++ CHECK_DLSYM_RET_RETURN(posix_api->getsockopt_fn = dlsym(handle, "getsockopt")); ++ CHECK_DLSYM_RET_RETURN(posix_api->setsockopt_fn = dlsym(handle, "setsockopt")); ++ ++ CHECK_DLSYM_RET_RETURN(posix_api->read_fn = dlsym(handle, "read")); ++ CHECK_DLSYM_RET_RETURN(posix_api->write_fn = dlsym(handle, "write")); ++ CHECK_DLSYM_RET_RETURN(posix_api->readv_fn = dlsym(handle, "readv")); ++ CHECK_DLSYM_RET_RETURN(posix_api->writev_fn = dlsym(handle, "writev")); ++ CHECK_DLSYM_RET_RETURN(posix_api->recv_fn = dlsym(handle, "recv")); ++ CHECK_DLSYM_RET_RETURN(posix_api->send_fn = dlsym(handle, "send")); ++ CHECK_DLSYM_RET_RETURN(posix_api->recvmsg_fn = dlsym(handle, "recvmsg")); ++ CHECK_DLSYM_RET_RETURN(posix_api->sendmsg_fn = dlsym(handle, "sendmsg")); ++ CHECK_DLSYM_RET_RETURN(posix_api->recvfrom_fn = dlsym(handle, "recvfrom")); ++ CHECK_DLSYM_RET_RETURN(posix_api->sendto_fn = dlsym(handle, "sendto")); ++ ++ CHECK_DLSYM_RET_RETURN(posix_api->select_fn = dlsym(handle, "select")); ++ CHECK_DLSYM_RET_RETURN(posix_api->poll_fn = dlsym(handle, "poll")); ++ CHECK_DLSYM_RET_RETURN(posix_api->epoll_create_fn = dlsym(handle, "epoll_create")); ++ CHECK_DLSYM_RET_RETURN(posix_api->epoll_create1_fn = dlsym(handle, "epoll_create1")); ++ CHECK_DLSYM_RET_RETURN(posix_api->epoll_ctl_fn = dlsym(handle, "epoll_ctl")); ++ CHECK_DLSYM_RET_RETURN(posix_api->epoll_wait_fn = dlsym(handle, "epoll_wait")); ++ CHECK_DLSYM_RET_RETURN(posix_api->eventfd_fn = dlsym(handle, "eventfd")); ++ ++ CHECK_DLSYM_RET_RETURN(posix_api->ioctl_fn = dlsym(handle, "ioctl")); ++ CHECK_DLSYM_RET_RETURN(posix_api->fcntl_fn = dlsym(handle, "fcntl")); ++ CHECK_DLSYM_RET_RETURN(posix_api->fcntl64_fn = dlsym(handle, "fcntl64")); ++ ++ CHECK_DLSYM_RET_RETURN(posix_api->sigaction_fn = dlsym(handle, "sigaction")); ++ CHECK_DLSYM_RET_RETURN(posix_api->fork_fn = dlsym(handle, "fork")); + + /* support fork */ +- posix_api->ues_posix = 1; ++ posix_api->use_kernel = 1; + lwip_sock_init(); +- return ERR_OK; ++ return 0; + + err_out: +- return ERR_MEM; ++ return -1; + #undef CHECK_DLSYM_RET_RETURN + } +diff --git a/src/include/lwipgz_posix_api.h b/src/include/lwipgz_posix_api.h +index d64d854..5474592 100644 +--- a/src/include/lwipgz_posix_api.h ++++ b/src/include/lwipgz_posix_api.h +@@ -33,6 +33,8 @@ + #ifndef __LWIPGZ_POSIX_API_H__ + #define __LWIPGZ_POSIX_API_H__ + ++// #include ++#include + #include + #include + #include +@@ -40,51 +42,53 @@ + + typedef struct { + void *handle; +- int (*socket_fn)(int domain, int type, int protocol); +- int (*accept_fn)(int s, struct sockaddr*, socklen_t*); +- int (*accept4_fn)(int s, struct sockaddr *addr, socklen_t *addrlen, int flags); +- int (*bind_fn)(int s, const struct sockaddr*, socklen_t); +- int (*listen_fn)(int s, int backlog); +- int (*connect_fn)(int s, const struct sockaddr *name, socklen_t namelen); +- int (*getpeername_fn)(int s, struct sockaddr *name, socklen_t *namelen); +- int (*getsockname_fn)(int s, struct sockaddr *name, socklen_t *namelen); +- int (*setsockopt_fn)(int s, int level, int optname, const void *optval, socklen_t optlen); +- int (*getsockopt_fn)(int s, int level, int optname, void *optval, socklen_t *optlen); +- int (*shutdown_fn)(int s, int how); ++ int use_kernel; ++ ++ /* API */ ++ int (*shutdown_fn)(int fd, int how); + int (*close_fn)(int fd); +- pid_t (*fork_fn)(void); ++ ++ int (*socket_fn)(int domain, int type, int protocol); ++ int (*connect_fn)(int fd, const struct sockaddr *name, socklen_t namelen); ++ int (*bind_fn)(int fd, const struct sockaddr*, socklen_t); ++ int (*listen_fn)(int fd, int backlog); ++ int (*accept_fn)(int fd, struct sockaddr*, socklen_t*); ++ int (*accept4_fn)(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags); ++ ++ int (*getpeername_fn)(int fd, struct sockaddr *name, socklen_t *namelen); ++ int (*getsockname_fn)(int fd, struct sockaddr *name, socklen_t *namelen); ++ int (*getsockopt_fn)(int fd, int level, int optname, void *optval, socklen_t *optlen); ++ int (*setsockopt_fn)(int fd, int level, int optname, const void *optval, socklen_t optlen); ++ + ssize_t (*read_fn)(int fd, void *mem, size_t len); +- ssize_t (*readv_fn)(int s, const struct iovec *iov, int iovcnt); + ssize_t (*write_fn)(int fd, const void *data, size_t len); +- ssize_t (*writev_fn)(int s, const struct iovec *iov, int iovcnt); +- ssize_t (*recv_fn)(int sockfd, void *buf, size_t len, int flags); +- ssize_t (*send_fn)(int sockfd, const void *buf, size_t len, int flags); +- ssize_t (*recv_msg)(int sockfd, const struct msghdr *msg, int flags); +- ssize_t (*send_msg)(int sockfd, const struct msghdr *msg, int flags); +- ssize_t (*recv_from)(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); +- ssize_t (*send_to)(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, +- socklen_t addrlen); +- int (*fcntl_fn)(int fd, int cmd, ...); +- int (*fcntl64_fn)(int fd, int cmd, ...); +- int (*pipe_fn)(int pipefd[2]); ++ ssize_t (*readv_fn)(int fd, const struct iovec *iov, int iovcnt); ++ ssize_t (*writev_fn)(int fd, const struct iovec *iov, int iovcnt); ++ ssize_t (*recv_fn)(int fd, void *buf, size_t len, int flags); ++ ssize_t (*send_fn)(int fd, const void *buf, size_t len, int flags); ++ ssize_t (*recvmsg_fn)(int fd, const struct msghdr *msg, int flags); ++ ssize_t (*sendmsg_fn)(int fd, const struct msghdr *msg, int flags); ++ ssize_t (*recvfrom_fn)(int fd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); ++ ssize_t (*sendto_fn)(int fd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); ++ ++ int (*select_fn)(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); ++ int (*poll_fn)(struct pollfd *fds, nfds_t nfds, int timeout); + int (*epoll_create_fn)(int size); + int (*epoll_create1_fn)(int size); + int (*epoll_ctl_fn)(int epfd, int op, int fd, struct epoll_event *event); + int (*epoll_wait_fn)(int epfd, struct epoll_event *events, int maxevents, int timeout); ++ int (*epoll_close_fn)(int epfd); + int (*eventfd_fn)(unsigned int initval, int flags); +- int (*is_epfd)(int fd); +- int (*sigaction_fn)(int signum, const struct sigaction *act, struct sigaction *oldact); +- int (*poll_fn)(struct pollfd *fds, nfds_t nfds, int timeout); ++ + int (*ioctl_fn)(int fd, int cmd, ...); +- int (*select_fn)(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); ++ int (*fcntl_fn)(int fd, int cmd, ...); ++ int (*fcntl64_fn)(int fd, int cmd, ...); + +- int ues_posix; ++ int (*sigaction_fn)(int signum, const struct sigaction *act, struct sigaction *oldact); ++ pid_t (*fork_fn)(void); + } posix_api_t; + + extern posix_api_t *posix_api; +- + int posix_api_init(void); +-void posix_api_free(void); +-void posix_api_fork(void); + + #endif /* __LWIPGZ_POSIX_API_H__ */ +diff --git a/src/include/lwipgz_sock.h b/src/include/lwipgz_sock.h +index 2fa9d7a..3845453 100644 +--- a/src/include/lwipgz_sock.h ++++ b/src/include/lwipgz_sock.h +@@ -62,6 +62,10 @@ enum posix_type { + #define POSIX_IS_TYPE(sock, posix_type) \ + (((sock)->type & POSIX_ALL) == (posix_type)) + ++/* CLOSED means not lwip sock-fd, such as kernel sock-fd or file-fd or unix-fd */ ++#define POSIX_IS_CLOSED(sock) \ ++ ((sock) == NULL || (sock)->conn == NULL) ++ + 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); +-- +2.33.0 + diff --git a/lwip.spec b/lwip.spec index 4c2c088..0659e98 100644 --- a/lwip.spec +++ b/lwip.spec @@ -4,7 +4,7 @@ Summary: lwip is a small independent implementation of the TCP/IP protocol suite Name: lwip Version: 2.2.0 -Release: 41 +Release: 42 License: BSD URL: http://savannah.nongnu.org/projects/lwip/ Source0: http://download.savannah.nongnu.org/releases/lwip/%{name}-%{version}.zip @@ -161,6 +161,7 @@ Patch9145: 0146-cleancode-remove-perf.patch Patch9146: 0147-cleancode-rename-gazelle-files-in-lwip.patch Patch9147: 0148-cleancode-refactor-lwipsock.h.patch Patch9148: 0149-cleancode-refactor-posix-type-and-get_socket.patch +Patch9149: 0150-cleancode-refactor-posix_api.patch BuildRequires: gcc-c++ dos2unix dpdk-devel @@ -190,6 +191,9 @@ cd %{_builddir}/%{name}-%{version}/src %{_libdir}/liblwip.a %changelog +* Thu Jul 18 2024 LemmyHuang - 2.2.0-42 +- cleancode: refactor posix_api + * Wed Jul 17 2024 LemmyHuang - 2.2.0-41 - cleancode: refactor posix type and get_socket