From dcfb07a30a3e4f8eece68d1ad8c17cb8c2a58945 Mon Sep 17 00:00:00 2001 From: Lemmy Huang Date: Wed, 10 Jul 2024 16:37:01 +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 --- src/lstack/api/lstack_epoll.c | 34 ++++---- src/lstack/api/lstack_rtc_api.c | 6 +- src/lstack/api/lstack_rtw_api.c | 14 ++-- src/lstack/api/lstack_wrap.c | 104 ++++++++++++------------ src/lstack/core/lstack_lwip.c | 22 ++--- src/lstack/core/lstack_preload.c | 44 +++++----- src/lstack/core/lstack_protocol_stack.c | 40 ++++----- src/lstack/core/lstack_stack_stat.c | 2 +- src/lstack/core/lstack_thread_rpc.c | 4 +- src/lstack/include/lstack_preload.h | 10 +-- 10 files changed, 137 insertions(+), 143 deletions(-) diff --git a/src/lstack/api/lstack_epoll.c b/src/lstack/api/lstack_epoll.c index 7d00de7..8146b4f 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 && CONN_TYPE_IS_LIBOS(sock->conn)) { + if (sock->conn && 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 && CONN_TYPE_IS_LIBOS(sock->conn)) { + if (sock->conn && 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 && CONN_TYPE_IS_LIBOS(sock->conn)) { + if (sock->conn && POSIX_IS_TYPE(sock, POSIX_LWIP)) { event |= EPOLLOUT; } } @@ -240,7 +240,7 @@ int32_t lstack_do_epoll_create(int32_t fd) return fd; } - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock == NULL) { LSTACK_LOG(ERR, LSTACK, "fd=%d sock is NULL errno=%d\n", fd, errno); posix_api->close_fn(fd); @@ -307,7 +307,7 @@ int32_t lstack_epoll_create(int32_t flags) int32_t lstack_epoll_close(int32_t fd) { - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock == NULL) { LSTACK_LOG(ERR, LSTACK, "fd=%d sock is NULL errno=%d\n", fd, errno); GAZELLE_RETURN(EINVAL); @@ -391,14 +391,14 @@ int32_t lstack_rtc_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_ GAZELLE_RETURN(EINVAL); } - struct lwip_sock *epoll_sock = get_socket_by_fd(epfd); + struct lwip_sock *epoll_sock = lwip_get_socket(epfd); if (epoll_sock == NULL || epoll_sock->wakeup == NULL) { return posix_api->epoll_ctl_fn(epfd, op, fd, event); } struct wakeup_poll *wakeup = epoll_sock->wakeup; - struct lwip_sock *sock = get_socket(fd); - if (sock == NULL) { + struct lwip_sock *sock = lwip_get_socket(fd); + if (sock == NULL || sock->conn == NULL) { return posix_api->epoll_ctl_fn(epfd, op, fd, event); } @@ -431,18 +431,18 @@ int32_t lstack_rtw_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_ GAZELLE_RETURN(EINVAL); } - struct lwip_sock *epoll_sock = get_socket_by_fd(epfd); + struct lwip_sock *epoll_sock = lwip_get_socket(epfd); if (epoll_sock == NULL || epoll_sock->wakeup == NULL) { return posix_api->epoll_ctl_fn(epfd, op, fd, event); } struct wakeup_poll *wakeup = epoll_sock->wakeup; - struct lwip_sock *sock = get_socket(fd); - if (sock == NULL) { + struct lwip_sock *sock = lwip_get_socket(fd); + if (sock == NULL || sock->conn == NULL) { return posix_api->epoll_ctl_fn(epfd, op, fd, event); } - if (CONN_TYPE_HAS_HOST(sock->conn)) { + if (POSIX_HAS_TYPE(sock, POSIX_KERNEL)) { int32_t ret = posix_api->epoll_ctl_fn(epfd, op, fd, event); if (ret < 0) { LSTACK_LOG(ERR, LSTACK, "fd=%d epfd=%d op=%d errno=%d\n", fd, epfd, op, errno); @@ -535,7 +535,7 @@ static int32_t poll_lwip_event(struct pollfd *fds, nfds_t nfds) for (uint32_t i = 0; i < nfds; i++) { /* sock->listen_next pointerto next stack listen */ int32_t fd = fds[i].fd; - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); while (sock && sock->conn) { uint32_t events = update_events(sock); if (events) { @@ -624,7 +624,7 @@ int32_t lstack_block_wait(struct wakeup_poll *wakeup, int32_t timeout) int32_t lstack_rtc_epoll_wait(int32_t epfd, struct epoll_event* events, int32_t maxevents, int32_t timeout) { - struct lwip_sock *sock = get_socket_by_fd(epfd); + struct lwip_sock *sock = lwip_get_socket(epfd); if (sock == NULL || sock->wakeup == NULL) { return posix_api->epoll_wait_fn(epfd, events, maxevents, timeout); @@ -673,7 +673,7 @@ int32_t lstack_rtc_epoll_wait(int32_t epfd, struct epoll_event* events, int32_t int32_t lstack_rtw_epoll_wait(int32_t epfd, struct epoll_event* events, int32_t maxevents, int32_t timeout) { - struct lwip_sock *sock = get_socket_by_fd(epfd); + struct lwip_sock *sock = lwip_get_socket(epfd); if (sock == NULL || sock->wakeup == NULL) { return posix_api->epoll_wait_fn(epfd, events, maxevents, timeout); } @@ -829,7 +829,7 @@ static void poll_init(struct wakeup_poll *wakeup, struct pollfd *fds, nfds_t nfd for (uint32_t i = 0; i < nfds; i++) { int32_t fd = fds[i].fd; fds[i].revents = 0; - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (fd == wakeup->last_fds[i].fd && fds[i].events == wakeup->last_fds[i].events) { /* fd close then socket may get same fd. */ @@ -838,7 +838,7 @@ static void poll_init(struct wakeup_poll *wakeup, struct pollfd *fds, nfds_t nfd } } - if (sock == NULL || sock->conn == NULL || CONN_TYPE_HAS_HOST(sock->conn)) { + if (sock == NULL || sock->conn == NULL || POSIX_HAS_TYPE(sock, POSIX_KERNEL)) { update_kernel_poll(wakeup, i, fds + i); } diff --git a/src/lstack/api/lstack_rtc_api.c b/src/lstack/api/lstack_rtc_api.c index 57ff89f..97623b3 100644 --- a/src/lstack/api/lstack_rtc_api.c +++ b/src/lstack/api/lstack_rtc_api.c @@ -50,8 +50,8 @@ int rtc_socket(int domain, int type, int protocol) /* need call stack thread init function */ ret = lwip_socket(domain, type, protocol); - struct lwip_sock *sock = get_socket(ret); - if (sock != NULL) { + if (ret >= 0) { + struct lwip_sock *sock = lwip_get_socket(ret); sock->stack = get_protocol_stack(); sock->epoll_events = 0; sock->events = 0; @@ -63,7 +63,7 @@ int rtc_socket(int domain, int type, int protocol) int rtc_close(int s) { - struct lwip_sock *sock = get_socket(s); + struct lwip_sock *sock = lwip_get_socket(s); if (sock != NULL && sock->wakeup != NULL && sock->wakeup->epollfd == s) { return lstack_epoll_close(s); } diff --git a/src/lstack/api/lstack_rtw_api.c b/src/lstack/api/lstack_rtw_api.c index 0f23ffd..f59b0cd 100644 --- a/src/lstack/api/lstack_rtw_api.c +++ b/src/lstack/api/lstack_rtw_api.c @@ -47,7 +47,7 @@ int rtw_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags) int rtw_bind(int s, const struct sockaddr *name, socklen_t namelen) { - struct lwip_sock *sock = get_socket_by_fd(s); + struct lwip_sock *sock = lwip_get_socket(s); if (NETCONN_IS_UDP(sock) && get_global_cfg_params()->listen_shadow) { return stack_broadcast_bind(s, name, namelen); @@ -137,7 +137,7 @@ ssize_t rtw_write(int s, const void *mem, size_t size) ssize_t rtw_writev(int s, const struct iovec *iov, int iovcnt) { - struct lwip_sock *sock = get_socket_by_fd(s); + struct lwip_sock *sock = lwip_get_socket(s); struct msghdr msg; msg.msg_name = NULL; @@ -167,14 +167,14 @@ ssize_t rtw_recvmsg(int s, const struct msghdr *message, int flags) ssize_t rtw_sendmsg(int s, const struct msghdr *message, int flags) { - struct lwip_sock *sock = get_socket_by_fd(s); + struct lwip_sock *sock = lwip_get_socket(s); return do_lwip_sendmsg_to_stack(sock, s, message, flags); } static ssize_t rtw_udp_recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen) { - struct lwip_sock *sock = get_socket_by_fd(sockfd); + struct lwip_sock *sock = lwip_get_socket(sockfd); int ret; while (1) { @@ -210,7 +210,7 @@ static inline ssize_t rtw_tcp_recvfrom(int sockfd, void *buf, size_t len, int fl ssize_t rtw_recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen) { - struct lwip_sock *sock = get_socket_by_fd(sockfd); + struct lwip_sock *sock = lwip_get_socket(sockfd); if (NETCONN_IS_UDP(sock)) { return rtw_udp_recvfrom(sockfd, buf, len, flags, addr, addrlen); } else { @@ -241,7 +241,7 @@ int rtw_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, s int rtw_close(int s) { - struct lwip_sock *sock = get_socket(s); + struct lwip_sock *sock = lwip_get_socket(s); if (sock && sock->wakeup && sock->wakeup->epollfd == s) { return lstack_epoll_close(s); } @@ -250,7 +250,7 @@ int rtw_close(int s) int rtw_shutdown(int fd, int how) { - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock && sock->wakeup && sock->wakeup->epollfd == fd) { GAZELLE_RETURN(ENOTSOCK); } diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c index 16fc876..d3e1027 100644 --- a/src/lstack/api/lstack_wrap.c +++ b/src/lstack/api/lstack_wrap.c @@ -125,7 +125,7 @@ void wrap_api_set_dummy(void) static inline int32_t do_epoll_create1(int32_t flags) { - if (select_posix_path() == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL) { return posix_api->epoll_create1_fn(flags); } @@ -134,7 +134,7 @@ static inline int32_t do_epoll_create1(int32_t flags) static inline int32_t do_epoll_create(int32_t size) { - if (select_posix_path() == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL) { return posix_api->epoll_create_fn(size); } @@ -143,7 +143,7 @@ static inline int32_t do_epoll_create(int32_t size) static inline int32_t do_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_event* event) { - if (select_posix_path() == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL) { return posix_api->epoll_ctl_fn(epfd, op, fd, event); } @@ -152,7 +152,7 @@ static inline int32_t do_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct static inline int32_t do_epoll_wait(int32_t epfd, struct epoll_event* events, int32_t maxevents, int32_t timeout) { - if (select_posix_path() == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL) { return posix_api->epoll_wait_fn(epfd, events, maxevents, timeout); } @@ -169,14 +169,14 @@ 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) == PATH_KERNEL) { + if (select_fd_posix_path(s, NULL) == POSIX_KERNEL) { return posix_api->accept_fn(s, addr, addrlen); } int32_t fd = g_wrap_api->accept_fn(s, addr, addrlen); if (fd >= 0) { - struct lwip_sock *sock = get_socket(fd); - SET_CONN_TYPE_LIBOS(sock->conn); + struct lwip_sock *sock = lwip_get_socket(fd); + POSIX_SET_TYPE(sock, POSIX_LWIP); return fd; } @@ -189,14 +189,14 @@ static int32_t do_accept4(int32_t s, struct sockaddr *addr, socklen_t *addrlen, GAZELLE_RETURN(EINVAL); } - if (select_fd_posix_path(s, NULL) == PATH_KERNEL) { + if (select_fd_posix_path(s, NULL) == POSIX_KERNEL) { return posix_api->accept4_fn(s, addr, addrlen, flags); } int32_t fd = g_wrap_api->accept4_fn(s, addr, addrlen, flags); if (fd >= 0) { - struct lwip_sock *sock = get_socket(fd); - SET_CONN_TYPE_LIBOS(sock->conn); + struct lwip_sock *sock = lwip_get_socket(fd); + POSIX_SET_TYPE(sock, POSIX_LWIP); return fd; } @@ -210,13 +210,13 @@ static int32_t do_bind(int32_t s, const struct sockaddr *name, socklen_t namelen } struct lwip_sock *sock = NULL; - if (select_fd_posix_path(s, &sock) == PATH_KERNEL) { + if (select_fd_posix_path(s, &sock) == POSIX_KERNEL) { return posix_api->bind_fn(s, name, namelen); } /* select user path when udp enable and ip addr is multicast */ if (IN_MULTICAST(ntohl(((struct sockaddr_in *)name)->sin_addr.s_addr))) { - SET_CONN_TYPE_LIBOS(sock->conn); + POSIX_SET_TYPE(sock, POSIX_LWIP); return g_wrap_api->bind_fn(s, name, namelen); } @@ -233,7 +233,7 @@ static int32_t do_bind(int32_t s, const struct sockaddr *name, socklen_t namelen if (match_host_addr(&sock_addr)) { /* maybe kni addr */ if (posix_api->bind_fn(s, name, namelen) != 0) { - SET_CONN_TYPE_LIBOS(sock->conn); + POSIX_SET_TYPE(sock, POSIX_LWIP); } else { /* reuse the port allocated by kernel when port == 0 */ if (((struct sockaddr_in *)name)->sin_port == 0) { @@ -248,7 +248,7 @@ static int32_t do_bind(int32_t s, const struct sockaddr *name, socklen_t namelen } return g_wrap_api->bind_fn(s, name, namelen); } else { - SET_CONN_TYPE_HOST(sock->conn); + POSIX_SET_TYPE(sock, POSIX_KERNEL); return posix_api->bind_fn(s, name, namelen); } } @@ -303,11 +303,11 @@ static int32_t do_connect(int32_t s, const struct sockaddr *name, socklen_t name } struct lwip_sock *sock = NULL; - if (select_fd_posix_path(s, &sock) == PATH_KERNEL) { + if (select_fd_posix_path(s, &sock) == POSIX_KERNEL) { return posix_api->connect_fn(s, name, namelen); } - sock = get_socket(s); + sock = lwip_get_socket(s); if (sock == NULL) { return posix_api->connect_fn(s, name, namelen); } @@ -323,10 +323,10 @@ static int32_t do_connect(int32_t s, const struct sockaddr *name, socklen_t name "listen_rx_ring_%d", remote_port); if (is_local && rte_ring_lookup(listen_ring_name) == NULL) { ret = posix_api->connect_fn(s, name, namelen); - SET_CONN_TYPE_HOST(sock->conn); + POSIX_SET_TYPE(sock, POSIX_KERNEL); } else { ret = g_wrap_api->connect_fn(s, name, namelen); - SET_CONN_TYPE_LIBOS(sock->conn); + POSIX_SET_TYPE(sock, POSIX_LWIP); } return ret; @@ -334,7 +334,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) == PATH_KERNEL) { + if (select_fd_posix_path(s, NULL) == POSIX_KERNEL) { return posix_api->listen_fn(s, backlog); } @@ -352,7 +352,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) == PATH_LWIP) { + if (select_fd_posix_path(s, NULL) == POSIX_LWIP) { return g_wrap_api->getpeername_fn(s, name, namelen); } @@ -365,7 +365,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) == PATH_LWIP) { + if (select_fd_posix_path(s, NULL) == POSIX_LWIP) { return g_wrap_api->getsockname_fn(s, name, namelen); } @@ -422,7 +422,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) == PATH_LWIP && !unsupport_optname(level, optname)) { + if (select_fd_posix_path(s, NULL) == POSIX_LWIP && !unsupport_optname(level, optname)) { return g_wrap_api->getsockopt_fn(s, level, optname, optval, optlen); } @@ -431,7 +431,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) == PATH_KERNEL || unsupport_optname(level, optname)) { + if (select_fd_posix_path(s, NULL) == POSIX_KERNEL || unsupport_optname(level, optname)) { return posix_api->setsockopt_fn(s, level, optname, optval, optlen); } @@ -445,7 +445,7 @@ static inline int32_t do_socket(int32_t domain, int32_t type, int32_t protocol) { int32_t ret; /* process not init completed or not hajacking thread */ - if (select_posix_path() == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL) { return posix_api->socket_fn(domain, type, protocol); } @@ -457,11 +457,11 @@ static inline int32_t do_socket(int32_t domain, int32_t type, int32_t protocol) ret = g_wrap_api->socket_fn(domain, type, protocol); if (ret >= 0) { - struct lwip_sock *sock = get_socket(ret); - SET_CONN_TYPE_LIBOS_OR_HOST(sock->conn); + struct lwip_sock *sock = lwip_get_socket(ret); + POSIX_SET_TYPE(sock, POSIX_LWIP | POSIX_KERNEL); /* if udp_enable = 1 in lstack.conf, udp protocol must be in user path currently */ if (type & SOCK_DGRAM) { - SET_CONN_TYPE_LIBOS(sock->conn); + POSIX_SET_TYPE(sock, POSIX_LWIP); } } @@ -478,8 +478,8 @@ static inline ssize_t do_recv(int32_t sockfd, void *buf, size_t len, int32_t fla return 0; } - if (select_posix_path() == PATH_KERNEL || // maybe fd is created by open before posix_api_init called - select_fd_posix_path(sockfd, NULL) == PATH_KERNEL) { + 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); } @@ -496,8 +496,8 @@ static inline ssize_t do_read(int32_t s, void *mem, size_t len) return 0; } - if (select_posix_path() == PATH_KERNEL || - select_fd_posix_path(s, NULL) == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL || + select_fd_posix_path(s, NULL) == POSIX_KERNEL) { return posix_api->read_fn(s, mem, len); } @@ -506,8 +506,8 @@ static inline ssize_t do_read(int32_t s, void *mem, size_t len) static inline ssize_t do_readv(int32_t s, const struct iovec *iov, int iovcnt) { - if (select_posix_path() == PATH_KERNEL || - select_fd_posix_path(s, NULL) == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL || + select_fd_posix_path(s, NULL) == POSIX_KERNEL) { return posix_api->readv_fn(s, iov, iovcnt); } @@ -516,8 +516,8 @@ static inline ssize_t do_readv(int32_t s, const struct iovec *iov, int iovcnt) static inline ssize_t do_send(int32_t sockfd, const void *buf, size_t len, int32_t flags) { - if (select_posix_path() == PATH_KERNEL || - select_fd_posix_path(sockfd, NULL) == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL || + select_fd_posix_path(sockfd, NULL) == POSIX_KERNEL) { return posix_api->send_fn(sockfd, buf, len, flags); } @@ -526,8 +526,8 @@ static inline ssize_t do_send(int32_t sockfd, const void *buf, size_t len, int32 static inline ssize_t do_write(int32_t s, const void *mem, size_t size) { - if (select_posix_path() == PATH_KERNEL || - select_fd_posix_path(s, NULL) == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL || + select_fd_posix_path(s, NULL) == POSIX_KERNEL) { return posix_api->write_fn(s, mem, size); } @@ -537,8 +537,8 @@ static inline ssize_t do_write(int32_t s, const void *mem, size_t size) static inline ssize_t do_writev(int32_t s, const struct iovec *iov, int iovcnt) { struct lwip_sock *sock; - if (select_posix_path() == PATH_KERNEL || - select_fd_posix_path(s, &sock) == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL || + select_fd_posix_path(s, &sock) == POSIX_KERNEL) { return posix_api->writev_fn(s, iov, iovcnt); } @@ -551,8 +551,8 @@ static inline ssize_t do_recvmsg(int32_t s, struct msghdr *message, int32_t flag GAZELLE_RETURN(EINVAL); } - if (select_posix_path() == PATH_KERNEL || - select_fd_posix_path(s, NULL) == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL || + select_fd_posix_path(s, NULL) == POSIX_KERNEL) { return posix_api->recv_msg(s, message, flags); } @@ -566,8 +566,8 @@ static inline ssize_t do_sendmsg(int32_t s, const struct msghdr *message, int32_ } struct lwip_sock *sock; - if (select_posix_path() == PATH_KERNEL || - select_fd_posix_path(s, &sock) == PATH_KERNEL) { + if (select_posix_path() == POSIX_KERNEL || + select_fd_posix_path(s, &sock) == POSIX_KERNEL) { return posix_api->send_msg(s, message, flags); } @@ -586,7 +586,7 @@ static inline ssize_t do_recvfrom(int32_t sockfd, void *buf, size_t len, int32_t } struct lwip_sock *sock = NULL; - if (select_fd_posix_path(sockfd, &sock) == PATH_LWIP) { + if (select_fd_posix_path(sockfd, &sock) == POSIX_LWIP) { return g_wrap_api->recv_from(sockfd, buf, len, flags, addr, addrlen); } @@ -597,7 +597,7 @@ static inline ssize_t do_sendto(int32_t sockfd, const void *buf, size_t len, int const struct sockaddr *addr, socklen_t addrlen) { struct lwip_sock *sock = NULL; - if (select_fd_posix_path(sockfd, &sock) != PATH_LWIP) { + if (select_fd_posix_path(sockfd, &sock) != POSIX_LWIP) { return posix_api->send_to(sockfd, buf, len, flags, addr, addrlen); } @@ -607,8 +607,8 @@ static inline ssize_t do_sendto(int32_t sockfd, const void *buf, size_t len, int static inline int32_t do_close(int32_t s) { struct lwip_sock *sock = NULL; - if (select_posix_path() == PATH_KERNEL || - select_fd_posix_path(s, &sock) == PATH_KERNEL) { + 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 */ @@ -624,7 +624,7 @@ static inline int32_t do_close(int32_t s) static int32_t do_shutdown(int fd, int how) { struct lwip_sock *sock = NULL; - if (select_posix_path() == PATH_KERNEL || select_fd_posix_path(fd, &sock) == PATH_KERNEL) { + 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 { @@ -637,7 +637,7 @@ static int32_t do_shutdown(int fd, int how) static int32_t do_poll(struct pollfd *fds, nfds_t nfds, int32_t timeout) { - if ((select_posix_path() == PATH_KERNEL) || fds == NULL || nfds == 0) { + if ((select_posix_path() == POSIX_KERNEL) || fds == NULL || nfds == 0) { return posix_api->poll_fn(fds, nfds, timeout); } @@ -676,7 +676,7 @@ 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() == PATH_KERNEL) || !(readfds || writefds || exceptfds) || nfds == 0) { + if ((select_posix_path() == POSIX_KERNEL) || !(readfds || writefds || exceptfds) || nfds == 0) { return posix_api->select_fn(nfds, readfds, writefds, exceptfds, timeout); } @@ -691,8 +691,8 @@ static int32_t do_select(int32_t nfds, fd_set *readfds, fd_set *writefds, fd_set val = va_arg(ap, typeof(val)); \ va_end(ap); \ struct lwip_sock *sock = NULL; \ - if (select_posix_path() == PATH_KERNEL || \ - select_fd_posix_path(_fd, &sock) == PATH_KERNEL) \ + if (select_posix_path() == POSIX_KERNEL || \ + select_fd_posix_path(_fd, &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_lwip.c b/src/lstack/core/lstack_lwip.c index 6fe4055..84ef782 100644 --- a/src/lstack/core/lstack_lwip.c +++ b/src/lstack/core/lstack_lwip.c @@ -147,7 +147,7 @@ static bool replenish_send_idlembuf(struct protocol_stack *stack, struct lwip_so int do_lwip_init_sock(int32_t fd) { struct protocol_stack *stack = get_protocol_stack(); - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock == NULL) { return -1; } @@ -179,7 +179,7 @@ int do_lwip_init_sock(int32_t fd) void do_lwip_clean_sock(int fd) { - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock == NULL || sock->stack == NULL) { return; } @@ -819,7 +819,7 @@ ssize_t do_lwip_send_to_stack(int32_t fd, const void *buf, size_t len, int32_t f GAZELLE_RETURN(EINVAL); } - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (len == 0 && !NETCONN_IS_UDP(sock)) { return 0; } @@ -1059,7 +1059,7 @@ ssize_t do_lwip_read_from_stack(int32_t fd, void *buf, size_t len, int32_t flags struct sockaddr *addr, socklen_t *addrlen) { ssize_t recvd = 0; - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); bool noblock = (flags & MSG_DONTWAIT) || netconn_is_nonblocking(sock->conn); if (recv_break_for_err(sock)) { @@ -1095,7 +1095,7 @@ ssize_t do_lwip_read_from_stack(int32_t fd, void *buf, size_t len, int32_t flags void do_lwip_add_recvlist(int32_t fd) { - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock && sock->stack && list_is_null(&sock->recv_list)) { list_add_node(&sock->stack->recv_list, &sock->recv_list); @@ -1166,7 +1166,7 @@ void do_lwip_connected_callback(struct netconn *conn) } int32_t fd = conn->callback_arg.socket; - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock == NULL || sock->conn == NULL) { return; } @@ -1177,7 +1177,7 @@ void do_lwip_connected_callback(struct netconn *conn) posix_api->shutdown_fn(fd, SHUT_RDWR); - SET_CONN_TYPE_LIBOS(conn); + POSIX_SET_TYPE(sock, POSIX_LWIP); add_sock_event(sock, EPOLLOUT); } @@ -1207,8 +1207,8 @@ static void copy_pcb_to_conn(struct gazelle_stat_lstack_conn_info *conn, const s if (netconn != NULL) { conn->fd = netconn->callback_arg.socket; conn->recv_cnt = (netconn->recvmbox == NULL) ? 0 : rte_ring_count(netconn->recvmbox->ring); - struct lwip_sock *sock = get_socket(netconn->callback_arg.socket); - if (sock != NULL) { + struct lwip_sock *sock = lwip_get_socket(netconn->callback_arg.socket); + if (sock != NULL && sock->conn != NULL) { 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); @@ -1473,7 +1473,7 @@ err_t same_node_ring_create(struct rte_ring **ring, int size, int port, char *na static void init_same_node_ring(struct tcp_pcb *pcb) { struct netconn *netconn = (struct netconn *)pcb->callback_arg; - struct lwip_sock *sock = get_socket(netconn->callback_arg.socket); + struct lwip_sock *sock = lwip_get_socket(netconn->callback_arg.socket); pcb->client_rx_ring = NULL; pcb->client_tx_ring = NULL; @@ -1488,7 +1488,7 @@ static void init_same_node_ring(struct tcp_pcb *pcb) err_t create_same_node_ring(struct tcp_pcb *pcb) { struct netconn *netconn = (struct netconn *)pcb->callback_arg; - struct lwip_sock *sock = get_socket(netconn->callback_arg.socket); + struct lwip_sock *sock = lwip_get_socket(netconn->callback_arg.socket); if (same_node_ring_create(&pcb->client_rx_ring, CLIENT_RING_SIZE, pcb->local_port, "client", "rx") != 0) { goto END; diff --git a/src/lstack/core/lstack_preload.c b/src/lstack/core/lstack_preload.c index 0974e9e..8cf4657 100644 --- a/src/lstack/core/lstack_preload.c +++ b/src/lstack/core/lstack_preload.c @@ -30,7 +30,7 @@ #define EXCLUDE_THRD_CNT 1 const static char *g_exclude_thread[EXCLUDE_THRD_CNT] = {"eal-intr-thread"}; -static PER_THREAD enum KERNEL_LWIP_PATH g_preload_thrdpath = PATH_UNKNOW; +static PER_THREAD enum posix_type g_preload_thrdpath = POSIX_ALL; struct lstack_preload { int32_t preload_switch; @@ -79,27 +79,27 @@ static void preload_get_thrdname(void) LSTACK_PRE_LOG(LSTACK_INFO, "thread name=%s ok\n", g_preload_info.env_thrdname); } -enum KERNEL_LWIP_PATH select_fd_posix_path(int32_t fd, struct lwip_sock **socket) +enum posix_type select_fd_posix_path(int32_t fd, struct lwip_sock **socket) { - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); /* AF_UNIX case */ - if (!sock || !sock->conn || CONN_TYPE_IS_HOST(sock->conn)) { - return PATH_KERNEL; + if (!sock || !sock->conn || POSIX_IS_TYPE(sock, POSIX_KERNEL)) { + return POSIX_KERNEL; } if (socket) { *socket = sock; } - if (likely(CONN_TYPE_IS_LIBOS(sock->conn))) { - return PATH_LWIP; + if (likely(POSIX_IS_TYPE(sock, POSIX_LWIP))) { + return POSIX_LWIP; } - return PATH_UNKNOW; + return POSIX_ALL; } -enum KERNEL_LWIP_PATH select_posix_path(void) +enum posix_type select_posix_path(void) { if (unlikely(posix_api == NULL)) { /* @@ -109,14 +109,14 @@ enum KERNEL_LWIP_PATH select_posix_path(void) if (posix_api_init() != 0) { LSTACK_PRE_LOG(LSTACK_ERR, "posix_api_init failed\n"); } - return PATH_KERNEL; + return POSIX_KERNEL; } if (unlikely(posix_api->ues_posix)) { - return PATH_KERNEL; + return POSIX_KERNEL; } - if (g_preload_thrdpath != PATH_UNKNOW) { + if (g_preload_thrdpath != POSIX_ALL) { return g_preload_thrdpath; } @@ -126,31 +126,31 @@ enum KERNEL_LWIP_PATH select_posix_path(void) char thread_name[PATH_MAX] = {0}; if (pthread_getname_np(pthread_self(), thread_name, PATH_MAX) != 0) { - g_preload_thrdpath = PATH_KERNEL; - return PATH_KERNEL; + g_preload_thrdpath = POSIX_KERNEL; + return POSIX_KERNEL; } /* exclude dpdk thread */ for (int i = 0; i < EXCLUDE_THRD_CNT; i++) { if (strstr(thread_name, g_exclude_thread[i]) != NULL) { - g_preload_thrdpath = PATH_KERNEL; - return PATH_KERNEL; + g_preload_thrdpath = POSIX_KERNEL; + return POSIX_KERNEL; } } /* not set GAZELLE_THREAD_NAME, select all thread */ if (g_preload_info.env_thrdname[0] == '\0') { - g_preload_thrdpath = PATH_LWIP; - return PATH_LWIP; + g_preload_thrdpath = POSIX_LWIP; + return POSIX_LWIP; } if (strstr(thread_name, g_preload_info.env_thrdname) == NULL) { - g_preload_thrdpath = PATH_KERNEL; - return PATH_KERNEL; + g_preload_thrdpath = POSIX_KERNEL; + return POSIX_KERNEL; } - g_preload_thrdpath = PATH_LWIP; - return PATH_LWIP; + g_preload_thrdpath = POSIX_LWIP; + return POSIX_LWIP; } int preload_info_init(void) diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c index c6075d5..d17cb67 100644 --- a/src/lstack/core/lstack_protocol_stack.c +++ b/src/lstack/core/lstack_protocol_stack.c @@ -126,8 +126,8 @@ struct protocol_stack *get_protocol_stack(void) struct protocol_stack *get_protocol_stack_by_fd(int32_t fd) { - struct lwip_sock *sock = get_socket(fd); - if (sock == NULL) { + struct lwip_sock *sock = lwip_get_socket(fd); + if (sock == NULL || sock->conn == NULL) { return NULL; } @@ -719,7 +719,7 @@ void stack_close(struct rpc_msg *msg) { int32_t fd = msg->args[MSG_ARG_0].i; struct protocol_stack *stack = get_protocol_stack_by_fd(fd); - struct lwip_sock *sock = get_socket(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock && __atomic_load_n(&sock->call_num, __ATOMIC_ACQUIRE) > 0) { msg->recall_flag = 1; @@ -738,7 +738,7 @@ void stack_shutdown(struct rpc_msg *msg) int fd = msg->args[MSG_ARG_0].i; int how = msg->args[MSG_ARG_1].i; struct protocol_stack *stack = get_protocol_stack_by_fd(fd); - struct lwip_sock *sock = get_socket(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock && __atomic_load_n(&sock->call_num, __ATOMIC_ACQUIRE) > 0) { msg->recall_flag = 1; @@ -767,7 +767,7 @@ void stack_listen(struct rpc_msg *msg) int32_t fd = msg->args[MSG_ARG_0].i; int32_t backlog = msg->args[MSG_ARG_1].i; - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock == NULL) { msg->result = -1; return; @@ -793,7 +793,7 @@ void stack_accept(struct rpc_msg *msg) return; } - struct lwip_sock *sock = get_socket(accept_fd); + struct lwip_sock *sock = lwip_get_socket(accept_fd); if (sock == NULL || sock->stack == NULL) { lwip_close(accept_fd); LSTACK_LOG(ERR, LSTACK, "fd %d ret %d\n", fd, accept_fd); @@ -880,7 +880,7 @@ void stack_tcp_send(struct rpc_msg *msg) struct protocol_stack *stack = get_protocol_stack(); int replenish_again; - struct lwip_sock *sock = get_socket(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock == NULL) { msg->result = -1; LSTACK_LOG(ERR, LSTACK, "get sock error! fd=%d, len=%ld\n", fd, len); @@ -917,7 +917,7 @@ void stack_udp_send(struct rpc_msg *msg) int replenish_again; uint32_t call_num; - struct lwip_sock *sock = get_socket(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock == NULL) { msg->result = -1; LSTACK_LOG(ERR, LSTACK, "get sock error! fd=%d, len=%ld\n", fd, len); @@ -1030,7 +1030,7 @@ void stack_create_shadow_fd(struct rpc_msg *msg) socklen_t addr_len = msg->args[MSG_ARG_2].socklen; int32_t clone_fd = 0; - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock == NULL) { LSTACK_LOG(ERR, LSTACK, "get sock null fd=%d\n", fd); msg->result = -1; @@ -1046,7 +1046,7 @@ void stack_create_shadow_fd(struct rpc_msg *msg) return; } - struct lwip_sock *clone_sock = get_socket_by_fd(clone_fd); + struct lwip_sock *clone_sock = lwip_get_socket(clone_fd); if (clone_sock == NULL) { LSTACK_LOG(ERR, LSTACK, "get sock null fd=%d clone_fd=%d\n", fd, clone_fd); msg->result = -1; @@ -1114,7 +1114,7 @@ void stack_recvlist_count(struct rpc_msg *msg) int32_t stack_broadcast_close(int32_t fd) { int32_t ret = 0; - struct lwip_sock *sock = get_socket(fd); + struct lwip_sock *sock = lwip_get_socket(fd); struct protocol_stack *stack = get_protocol_stack_by_fd(fd); if (sock == NULL) { GAZELLE_RETURN(EBADF); @@ -1139,7 +1139,7 @@ int32_t stack_broadcast_close(int32_t fd) int stack_broadcast_shutdown(int fd, int how) { int32_t ret = 0; - struct lwip_sock *sock = get_socket(fd); + struct lwip_sock *sock = lwip_get_socket(fd); struct protocol_stack *stack = get_protocol_stack_by_fd(fd); if (sock == NULL) { GAZELLE_RETURN(EBADF); @@ -1186,7 +1186,7 @@ int32_t stack_broadcast_listen(int32_t fd, int32_t backlog) socklen_t addr_len = sizeof(addr); int32_t ret, clone_fd; - struct lwip_sock *sock = get_socket(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock == NULL || cur_stack == NULL) { LSTACK_LOG(ERR, LSTACK, "tid %ld, %d get sock null or stack null\n", get_stack_tid(), fd); GAZELLE_RETURN(EBADF); @@ -1216,9 +1216,9 @@ int32_t stack_broadcast_listen(int32_t fd, int32_t backlog) } if (min_conn_stk_idx == i) { - get_socket_by_fd(clone_fd)->conn->is_master_fd = 1; + lwip_get_socket(clone_fd)->conn->is_master_fd = 1; } else { - get_socket_by_fd(clone_fd)->conn->is_master_fd = 0; + lwip_get_socket(clone_fd)->conn->is_master_fd = 0; } ret = rpc_call_listen(&stack->rpc_queue, clone_fd, backlog); @@ -1232,7 +1232,7 @@ int32_t stack_broadcast_listen(int32_t fd, int32_t backlog) static struct lwip_sock *get_min_accept_sock(int32_t fd) { - struct lwip_sock *sock = get_socket(fd); + struct lwip_sock *sock = lwip_get_socket(fd); struct lwip_sock *min_sock = NULL; while (sock) { @@ -1282,7 +1282,7 @@ int32_t stack_broadcast_bind(int32_t fd, const struct sockaddr *name, socklen_t struct protocol_stack *stack = NULL; int32_t ret, clone_fd; - struct lwip_sock *sock = get_socket(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (sock == NULL || cur_stack == NULL) { LSTACK_LOG(ERR, LSTACK, "tid %ld, %d get sock null or stack null\n", get_stack_tid(), fd); GAZELLE_RETURN(EBADF); @@ -1313,7 +1313,7 @@ int32_t stack_broadcast_accept4(int32_t fd, struct sockaddr *addr, socklen_t *ad { int32_t ret = -1; struct lwip_sock *min_sock = NULL; - struct lwip_sock *sock = get_socket(fd); + struct lwip_sock *sock = lwip_get_socket(fd); struct protocol_stack *stack = NULL; if (sock == NULL) { GAZELLE_RETURN(EBADF); @@ -1356,8 +1356,8 @@ int32_t stack_broadcast_accept(int32_t fd, struct sockaddr *addr, socklen_t *add static void stack_all_fds_close(void) { for (int i = 3; i < GAZELLE_MAX_CLIENTS + GAZELLE_RESERVED_CLIENTS; i++) { - struct lwip_sock *sock = get_socket(i); - if (sock && sock->stack == get_protocol_stack()) { + struct lwip_sock *sock = lwip_get_socket(i); + if (sock && sock->conn && sock->stack == get_protocol_stack()) { lwip_close(i); } } diff --git a/src/lstack/core/lstack_stack_stat.c b/src/lstack/core/lstack_stack_stat.c index e77f21a..e42efde 100644 --- a/src/lstack/core/lstack_stack_stat.c +++ b/src/lstack/core/lstack_stack_stat.c @@ -81,7 +81,7 @@ void time_stamp_into_recvmbox(struct lwip_sock *sock) void time_stamp_record(int fd, struct pbuf *pbuf) { - struct lwip_sock *sock = get_socket_by_fd(fd); + struct lwip_sock *sock = lwip_get_socket(fd); if (get_protocol_stack_group()->latency_start && sock && pbuf) { calculate_lstack_latency(&sock->stack->latency, pbuf, GAZELLE_LATENCY_INTO_MBOX, 0); diff --git a/src/lstack/core/lstack_thread_rpc.c b/src/lstack/core/lstack_thread_rpc.c index a6d2dbf..8ac06cb 100644 --- a/src/lstack/core/lstack_thread_rpc.c +++ b/src/lstack/core/lstack_thread_rpc.c @@ -474,7 +474,7 @@ int32_t rpc_call_tcp_send(rpc_queue *queue, int fd, size_t len, int flags) } if (get_protocol_stack_group()->latency_start) { - time_stamp_into_rpcmsg(get_socket_by_fd(fd)); + time_stamp_into_rpcmsg(lwip_get_socket(fd)); } msg->args[MSG_ARG_0].i = fd; @@ -495,7 +495,7 @@ int32_t rpc_call_udp_send(rpc_queue *queue, int fd, size_t len, int flags) } if (get_protocol_stack_group()->latency_start) { - time_stamp_into_rpcmsg(get_socket_by_fd(fd)); + time_stamp_into_rpcmsg(lwip_get_socket(fd)); } msg->args[MSG_ARG_0].i = fd; diff --git a/src/lstack/include/lstack_preload.h b/src/lstack/include/lstack_preload.h index 0ad7877..c30736a 100644 --- a/src/lstack/include/lstack_preload.h +++ b/src/lstack/include/lstack_preload.h @@ -13,13 +13,7 @@ #define __LSTACK_PRELOAD_H__ #include -enum KERNEL_LWIP_PATH { - PATH_KERNEL = 0, - PATH_LWIP, - PATH_UNKNOW, -}; - -enum KERNEL_LWIP_PATH select_posix_path(void); -enum KERNEL_LWIP_PATH select_fd_posix_path(int32_t fd, struct lwip_sock **socket); +enum posix_type select_posix_path(void); +enum posix_type select_fd_posix_path(int32_t fd, struct lwip_sock **socket); int preload_info_init(void); #endif -- 2.33.0