lwip/0150-cleancode-refactor-posix_api.patch
Lemmy Huang 6424911a07 cleancode: refactor posix_api
Signed-off-by: Lemmy Huang <huangliming5@huawei.com>
(cherry picked from commit 372566ff31eea6f3eec6aa3434d390503fcbf44a)
2024-08-20 10:52:50 +08:00

272 lines
13 KiB
Diff

From 99a688eb9c398d53dbb11ce4b594662a1eff810d Mon Sep 17 00:00:00 2001
From: Lemmy Huang <huangliming5@huawei.com>
Date: Wed, 10 Jul 2024 17:35:25 +0800
Subject: [PATCH] cleancode: refactor posix_api
Signed-off-by: Lemmy Huang <huangliming5@huawei.com>
---
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 <netinet/in.h>
+// #include <sys/ioctl.h>
+// #include <fcntl.h>
#include <dlfcn.h>
-#include <fcntl.h>
-#include <sys/epoll.h>
#include <sys/socket.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <rte_log.h>
-
-#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 <sys/socket.h>
+#include <unistd.h>
#include <signal.h>
#include <sys/poll.h>
#include <sys/epoll.h>
@@ -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