Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
0219-cleancode-refactor-posix_api.patch 29.47 KB
Copy Edit Raw Blame History
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
From a1523d108581cfc8aaf89b1116dde071ca5656d8 Mon Sep 17 00:00:00 2001
From: Lemmy Huang <huangliming5@huawei.com>
Date: Wed, 10 Jul 2024 17:40:43 +0800
Subject: [PATCH] cleancode: refactor posix_api
Signed-off-by: Lemmy Huang <huangliming5@huawei.com>
---
src/lstack/api/lstack_epoll.c | 16 +-
src/lstack/api/lstack_fork.c | 3 +-
src/lstack/api/lstack_rtw_api.c | 2 +-
src/lstack/api/lstack_wrap.c | 234 ++++++++++--------------
src/lstack/core/lstack_control_plane.c | 4 +-
src/lstack/core/lstack_init.c | 4 +-
src/lstack/core/lstack_lwip.c | 5 +-
src/lstack/core/lstack_preload.c | 23 ++-
src/lstack/core/lstack_protocol_stack.c | 8 +-
src/lstack/include/lstack_preload.h | 6 +-
src/lstack/netif/lstack_flow.c | 1 +
11 files changed, 139 insertions(+), 167 deletions(-)
diff --git a/src/lstack/api/lstack_epoll.c b/src/lstack/api/lstack_epoll.c
index 8146b4f..f8fd6d4 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 && POSIX_IS_TYPE(sock, POSIX_LWIP)) {
+ if (!POSIX_IS_CLOSED(sock) && 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 && POSIX_IS_TYPE(sock, POSIX_LWIP)) {
+ if (!POSIX_IS_CLOSED(sock) && 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 && POSIX_IS_TYPE(sock, POSIX_LWIP)) {
+ if (!POSIX_IS_CLOSED(sock) && POSIX_IS_TYPE(sock, POSIX_LWIP)) {
event |= EPOLLOUT;
}
}
@@ -398,7 +398,7 @@ int32_t lstack_rtc_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_
struct wakeup_poll *wakeup = epoll_sock->wakeup;
struct lwip_sock *sock = lwip_get_socket(fd);
- if (sock == NULL || sock->conn == NULL) {
+ if (POSIX_IS_CLOSED(sock)) {
return posix_api->epoll_ctl_fn(epfd, op, fd, event);
}
@@ -438,7 +438,7 @@ int32_t lstack_rtw_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_
struct wakeup_poll *wakeup = epoll_sock->wakeup;
struct lwip_sock *sock = lwip_get_socket(fd);
- if (sock == NULL || sock->conn == NULL) {
+ if (POSIX_IS_CLOSED(sock)) {
return posix_api->epoll_ctl_fn(epfd, op, fd, event);
}
@@ -536,7 +536,7 @@ static int32_t poll_lwip_event(struct pollfd *fds, nfds_t nfds)
/* sock->listen_next pointerto next stack listen */
int32_t fd = fds[i].fd;
struct lwip_sock *sock = lwip_get_socket(fd);
- while (sock && sock->conn) {
+ while (!POSIX_IS_CLOSED(sock)) {
uint32_t events = update_events(sock);
if (events) {
fds[i].revents = events;
@@ -838,7 +838,7 @@ static void poll_init(struct wakeup_poll *wakeup, struct pollfd *fds, nfds_t nfd
}
}
- if (sock == NULL || sock->conn == NULL || POSIX_HAS_TYPE(sock, POSIX_KERNEL)) {
+ if (POSIX_IS_CLOSED(sock) || POSIX_HAS_TYPE(sock, POSIX_KERNEL)) {
update_kernel_poll(wakeup, i, fds + i);
}
@@ -846,7 +846,7 @@ static void poll_init(struct wakeup_poll *wakeup, struct pollfd *fds, nfds_t nfd
wakeup->last_fds[i].events = fds[i].events;
poll_change = 1;
- while (sock && sock->conn) {
+ while (!POSIX_IS_CLOSED(sock)) {
sock->epoll_events = fds[i].events | POLLERR;
sock->wakeup = wakeup;
stack_count[sock->stack->stack_idx]++;
diff --git a/src/lstack/api/lstack_fork.c b/src/lstack/api/lstack_fork.c
index 5cddee2..f5d0e95 100644
--- a/src/lstack/api/lstack_fork.c
+++ b/src/lstack/api/lstack_fork.c
@@ -20,8 +20,9 @@ pid_t lstack_fork(void)
pid_t pid;
pid = posix_api->fork_fn();
+ /* child not support lwip */
if (pid == 0) {
- posix_api_fork();
+ posix_api->use_kernel = 1;
}
return pid;
}
diff --git a/src/lstack/api/lstack_rtw_api.c b/src/lstack/api/lstack_rtw_api.c
index f59b0cd..09c4e11 100644
--- a/src/lstack/api/lstack_rtw_api.c
+++ b/src/lstack/api/lstack_rtw_api.c
@@ -186,7 +186,7 @@ static ssize_t rtw_udp_recvfrom(int sockfd, void *buf, size_t len, int flags,
return -1;
}
sock = sock->listen_next;
- if (sock != NULL && sock->conn != NULL) {
+ if (!POSIX_IS_CLOSED(sock)) {
sockfd = sock->conn->callback_arg.socket;
} else {
if (sock == NULL) {
diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c
index d3e1027..b19990e 100644
--- a/src/lstack/api/lstack_wrap.c
+++ b/src/lstack/api/lstack_wrap.c
@@ -37,12 +37,8 @@
#include "lstack_rtw_api.h"
#include "lstack_dummy_api.h"
-#ifndef SOCK_TYPE_MASK
-#define SOCK_TYPE_MASK 0xf
-#endif
-
-posix_api_t g_wrap_api_value;
-posix_api_t *g_wrap_api;
+static posix_api_t g_wrap_api_value;
+static posix_api_t *g_wrap_api;
void wrap_api_init(void)
{
@@ -68,10 +64,10 @@ void wrap_api_init(void)
g_wrap_api->writev_fn = lwip_writev;
g_wrap_api->recv_fn = lwip_recv;
g_wrap_api->send_fn = lwip_send;
- g_wrap_api->recv_msg = lwip_recvmsg;
- g_wrap_api->send_msg = lwip_sendmsg;
- g_wrap_api->recv_from = lwip_recvfrom;
- g_wrap_api->send_to = lwip_sendto;
+ g_wrap_api->recvmsg_fn = lwip_recvmsg;
+ g_wrap_api->sendmsg_fn = lwip_sendmsg;
+ g_wrap_api->recvfrom_fn = lwip_recvfrom;
+ g_wrap_api->sendto_fn = lwip_sendto;
g_wrap_api->epoll_wait_fn = rtc_epoll_wait;
g_wrap_api->poll_fn = rtc_poll;
g_wrap_api->close_fn = rtc_close;
@@ -97,10 +93,10 @@ void wrap_api_init(void)
g_wrap_api->writev_fn = rtw_writev;
g_wrap_api->recv_fn = rtw_recv;
g_wrap_api->send_fn = rtw_send;
- g_wrap_api->recv_msg = rtw_recvmsg;
- g_wrap_api->send_msg = rtw_sendmsg;
- g_wrap_api->recv_from = rtw_recvfrom;
- g_wrap_api->send_to = rtw_sendto;
+ g_wrap_api->recvmsg_fn = rtw_recvmsg;
+ g_wrap_api->sendmsg_fn = rtw_sendmsg;
+ g_wrap_api->recvfrom_fn = rtw_recvfrom;
+ g_wrap_api->sendto_fn = rtw_sendto;
g_wrap_api->epoll_wait_fn = rtw_epoll_wait;
g_wrap_api->poll_fn = rtw_poll;
g_wrap_api->close_fn = rtw_close;
@@ -118,8 +114,8 @@ void wrap_api_set_dummy(void)
g_wrap_api->send_fn = dummy_send;
g_wrap_api->write_fn = dummy_write;
g_wrap_api->writev_fn = dummy_writev;
- g_wrap_api->send_msg = dummy_sendmsg;
- g_wrap_api->send_to = dummy_sendto;
+ g_wrap_api->sendmsg_fn = dummy_sendmsg;
+ g_wrap_api->sendto_fn = dummy_sendto;
rte_wmb();
}
@@ -169,7 +165,7 @@ 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) == POSIX_KERNEL) {
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_KERNEL) {
return posix_api->accept_fn(s, addr, addrlen);
}
@@ -189,7 +185,7 @@ static int32_t do_accept4(int32_t s, struct sockaddr *addr, socklen_t *addrlen,
GAZELLE_RETURN(EINVAL);
}
- if (select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_KERNEL) {
return posix_api->accept4_fn(s, addr, addrlen, flags);
}
@@ -209,8 +205,8 @@ static int32_t do_bind(int32_t s, const struct sockaddr *name, socklen_t namelen
GAZELLE_RETURN(EINVAL);
}
- struct lwip_sock *sock = NULL;
- if (select_fd_posix_path(s, &sock) == POSIX_KERNEL) {
+ struct lwip_sock *sock = lwip_get_socket(s);
+ if (select_sock_posix_path(sock) == POSIX_KERNEL) {
return posix_api->bind_fn(s, name, namelen);
}
@@ -230,30 +226,31 @@ static int32_t do_bind(int32_t s, const struct sockaddr *name, socklen_t namelen
((struct sockaddr_in6 *)name)->sin6_addr.s6_addr, IPV6_ADDR_LEN);
}
- if (match_host_addr(&sock_addr)) {
- /* maybe kni addr */
- if (posix_api->bind_fn(s, name, namelen) != 0) {
- POSIX_SET_TYPE(sock, POSIX_LWIP);
- } else {
- /* reuse the port allocated by kernel when port == 0 */
- if (((struct sockaddr_in *)name)->sin_port == 0) {
- struct sockaddr_in kerneladdr;
- socklen_t len = sizeof(kerneladdr);
- if (posix_api->getsockname_fn(s, (struct sockaddr *)&kerneladdr, &len) < 0) {
- LSTACK_LOG(ERR, LSTACK, "kernel getsockname failed, fd=%d, errno=%d\n", s, errno);
- return -1;
- }
- ((struct sockaddr_in *)name)->sin_port = kerneladdr.sin_port;
+ if (!match_host_addr(&sock_addr)) {
+ POSIX_SET_TYPE(sock, POSIX_KERNEL);
+ return posix_api->bind_fn(s, name, namelen);
+ }
+
+ /* maybe kni addr */
+ if (posix_api->bind_fn(s, name, namelen) == 0) {
+ /* reuse the port allocated by kernel when port == 0 */
+ if (((struct sockaddr_in *)name)->sin_port == 0) {
+ struct sockaddr_in kerneladdr;
+ socklen_t len = sizeof(kerneladdr);
+ if (posix_api->getsockname_fn(s, (struct sockaddr *)&kerneladdr, &len) < 0) {
+ LSTACK_LOG(ERR, LSTACK, "kernel getsockname failed, fd=%d, errno=%d\n", s, errno);
+ return -1;
}
+ ((struct sockaddr_in *)name)->sin_port = kerneladdr.sin_port;
}
- return g_wrap_api->bind_fn(s, name, namelen);
+ /* not sure POSIX_LWIP or POSIX_KERNEL */
} else {
- POSIX_SET_TYPE(sock, POSIX_KERNEL);
- return posix_api->bind_fn(s, name, namelen);
+ POSIX_SET_TYPE(sock, POSIX_LWIP);
}
+ return g_wrap_api->bind_fn(s, name, namelen);
}
-bool is_dst_ip_localhost(const struct sockaddr *addr)
+static bool is_dst_ip_localhost(const struct sockaddr *addr)
{
struct ifaddrs *ifap;
struct ifaddrs *ifa;
@@ -302,13 +299,8 @@ static int32_t do_connect(int32_t s, const struct sockaddr *name, socklen_t name
GAZELLE_RETURN(EINVAL);
}
- struct lwip_sock *sock = NULL;
- if (select_fd_posix_path(s, &sock) == POSIX_KERNEL) {
- return posix_api->connect_fn(s, name, namelen);
- }
-
- sock = lwip_get_socket(s);
- if (sock == NULL) {
+ struct lwip_sock *sock = lwip_get_socket(s);
+ if (select_sock_posix_path(sock) == POSIX_KERNEL) {
return posix_api->connect_fn(s, name, namelen);
}
@@ -334,7 +326,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) == POSIX_KERNEL) {
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_KERNEL) {
return posix_api->listen_fn(s, backlog);
}
@@ -352,7 +344,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) == POSIX_LWIP) {
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
return g_wrap_api->getpeername_fn(s, name, namelen);
}
@@ -365,7 +357,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) == POSIX_LWIP) {
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
return g_wrap_api->getsockname_fn(s, name, namelen);
}
@@ -422,7 +414,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) == POSIX_LWIP && !unsupport_optname(level, optname)) {
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP && !unsupport_optname(level, optname)) {
return g_wrap_api->getsockopt_fn(s, level, optname, optval, optlen);
}
@@ -431,7 +423,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) == POSIX_KERNEL || unsupport_optname(level, optname)) {
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_KERNEL || unsupport_optname(level, optname)) {
return posix_api->setsockopt_fn(s, level, optname, optval, optlen);
}
@@ -473,17 +465,14 @@ static inline ssize_t do_recv(int32_t sockfd, void *buf, size_t len, int32_t fla
if (buf == NULL) {
GAZELLE_RETURN(EINVAL);
}
-
if (len == 0) {
return 0;
}
- 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);
+ if (select_sock_posix_path(lwip_get_socket(sockfd)) == POSIX_LWIP) {
+ return g_wrap_api->recv_fn(sockfd, buf, len, flags);
}
-
- return g_wrap_api->recv_fn(sockfd, buf, len, flags);
+ return posix_api->recv_fn(sockfd, buf, len, flags);
}
static inline ssize_t do_read(int32_t s, void *mem, size_t len)
@@ -491,58 +480,46 @@ static inline ssize_t do_read(int32_t s, void *mem, size_t len)
if (mem == NULL) {
GAZELLE_RETURN(EINVAL);
}
-
if (len == 0) {
return 0;
}
- if (select_posix_path() == POSIX_KERNEL ||
- select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
- return posix_api->read_fn(s, mem, len);
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+ return g_wrap_api->read_fn(s, mem, len);
}
-
- return g_wrap_api->read_fn(s, mem, len);
+ return posix_api->read_fn(s, mem, len);
}
static inline ssize_t do_readv(int32_t s, const struct iovec *iov, int iovcnt)
{
- if (select_posix_path() == POSIX_KERNEL ||
- select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
- return posix_api->readv_fn(s, iov, iovcnt);
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+ return g_wrap_api->readv_fn(s, iov, iovcnt);
}
-
- return g_wrap_api->readv_fn(s, iov, iovcnt);
+ return posix_api->readv_fn(s, iov, iovcnt);
}
static inline ssize_t do_send(int32_t sockfd, const void *buf, size_t len, int32_t flags)
{
- if (select_posix_path() == POSIX_KERNEL ||
- select_fd_posix_path(sockfd, NULL) == POSIX_KERNEL) {
- return posix_api->send_fn(sockfd, buf, len, flags);
+ if (select_sock_posix_path(lwip_get_socket(sockfd)) == POSIX_LWIP) {
+ return g_wrap_api->send_fn(sockfd, buf, len, flags);
}
-
- return g_wrap_api->send_fn(sockfd, buf, len, flags);
+ return posix_api->send_fn(sockfd, buf, len, flags);
}
static inline ssize_t do_write(int32_t s, const void *mem, size_t size)
{
- if (select_posix_path() == POSIX_KERNEL ||
- select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
- return posix_api->write_fn(s, mem, size);
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+ return g_wrap_api->write_fn(s, mem, size);
}
-
- return g_wrap_api->write_fn(s, mem, size);
+ return posix_api->write_fn(s, mem, size);
}
static inline ssize_t do_writev(int32_t s, const struct iovec *iov, int iovcnt)
{
- struct lwip_sock *sock;
- if (select_posix_path() == POSIX_KERNEL ||
- select_fd_posix_path(s, &sock) == POSIX_KERNEL) {
- return posix_api->writev_fn(s, iov, iovcnt);
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+ return g_wrap_api->writev_fn(s, iov, iovcnt);
}
-
- return g_wrap_api->writev_fn(s, iov, iovcnt);
+ return posix_api->writev_fn(s, iov, iovcnt);
}
static inline ssize_t do_recvmsg(int32_t s, struct msghdr *message, int32_t flags)
@@ -551,12 +528,10 @@ static inline ssize_t do_recvmsg(int32_t s, struct msghdr *message, int32_t flag
GAZELLE_RETURN(EINVAL);
}
- if (select_posix_path() == POSIX_KERNEL ||
- select_fd_posix_path(s, NULL) == POSIX_KERNEL) {
- return posix_api->recv_msg(s, message, flags);
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+ return g_wrap_api->recvmsg_fn(s, message, flags);
}
-
- return g_wrap_api->recv_msg(s, message, flags);
+ return posix_api->recvmsg_fn(s, message, flags);
}
static inline ssize_t do_sendmsg(int32_t s, const struct msghdr *message, int32_t flags)
@@ -565,13 +540,10 @@ static inline ssize_t do_sendmsg(int32_t s, const struct msghdr *message, int32_
GAZELLE_RETURN(EINVAL);
}
- struct lwip_sock *sock;
- if (select_posix_path() == POSIX_KERNEL ||
- select_fd_posix_path(s, &sock) == POSIX_KERNEL) {
- return posix_api->send_msg(s, message, flags);
+ if (select_sock_posix_path(lwip_get_socket(s)) == POSIX_LWIP) {
+ return g_wrap_api->sendmsg_fn(s, message, flags);
}
-
- return g_wrap_api->send_msg(s, message, flags);
+ return posix_api->sendmsg_fn(s, message, flags);
}
static inline ssize_t do_recvfrom(int32_t sockfd, void *buf, size_t len, int32_t flags,
@@ -580,58 +552,48 @@ static inline ssize_t do_recvfrom(int32_t sockfd, void *buf, size_t len, int32_t
if (buf == NULL) {
GAZELLE_RETURN(EINVAL);
}
-
if (len == 0) {
return 0;
}
- struct lwip_sock *sock = NULL;
- if (select_fd_posix_path(sockfd, &sock) == POSIX_LWIP) {
- return g_wrap_api->recv_from(sockfd, buf, len, flags, addr, addrlen);
+ if (select_sock_posix_path(lwip_get_socket(sockfd)) == POSIX_LWIP) {
+ return g_wrap_api->recvfrom_fn(sockfd, buf, len, flags, addr, addrlen);
}
-
- return posix_api->recv_from(sockfd, buf, len, flags, addr, addrlen);
+ return posix_api->recvfrom_fn(sockfd, buf, len, flags, addr, addrlen);
}
static inline ssize_t do_sendto(int32_t sockfd, const void *buf, size_t len, int32_t flags,
const struct sockaddr *addr, socklen_t addrlen)
{
- struct lwip_sock *sock = NULL;
- if (select_fd_posix_path(sockfd, &sock) != POSIX_LWIP) {
- return posix_api->send_to(sockfd, buf, len, flags, addr, addrlen);
+ if (select_sock_posix_path(lwip_get_socket(sockfd)) == POSIX_LWIP) {
+ return g_wrap_api->sendto_fn(sockfd, buf, len, flags, addr, addrlen);
}
-
- return g_wrap_api->send_to(sockfd, buf, len, flags, addr, addrlen);
+ return posix_api->sendto_fn(sockfd, buf, len, flags, addr, addrlen);
}
-static inline int32_t do_close(int32_t s)
+static inline int32_t do_close(int fd)
{
- struct lwip_sock *sock = NULL;
+ /* Can not use select_sock_posix_path() !
+ * When fd created by lwip_stocket() set as POSIX_KERNEL,
+ * lwip_close() is still required.
+ */
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 */
- g_wrap_api->close_fn(s) == 0) {
- return 0;
- } else {
- return posix_api->close_fn(s);
- }
+ POSIX_IS_CLOSED(lwip_get_socket(fd))) {
+ return posix_api->close_fn(fd);
}
- return g_wrap_api->close_fn(s);
+ return g_wrap_api->close_fn(fd);
}
static int32_t do_shutdown(int fd, int how)
{
- struct lwip_sock *sock = NULL;
- 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 {
- return posix_api->shutdown_fn(fd, how);
- }
+ /* Can not use select_sock_posix_path() !
+ * When fd created by lwip_stocket() set as POSIX_KERNEL,
+ * lwip_close() is still required.
+ */
+ if (select_posix_path() == POSIX_KERNEL ||
+ POSIX_IS_CLOSED(lwip_get_socket(fd))) {
+ return posix_api->shutdown_fn(fd, how);
}
-
return g_wrap_api->shutdown_fn(fd, how);
}
@@ -660,15 +622,13 @@ static int32_t do_ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *
return ready;
}
-typedef int32_t (*sigaction_fn)(int32_t signum, const struct sigaction *act, struct sigaction *oldact);
static int32_t do_sigaction(int32_t signum, const struct sigaction *act, struct sigaction *oldact)
{
- if (posix_api == NULL) {
- sigaction_fn sf = (sigaction_fn)dlsym(RTLD_NEXT, "sigaction");
- if (sf == NULL) {
- return -1;
+ if (unlikely(posix_api == NULL)) {
+ if (posix_api_init() != 0) {
+ GAZELLE_RETURN(EAGAIN);
}
- return sf(signum, act, oldact);
+ return posix_api->sigaction_fn(signum, act, oldact);
}
return lstack_sigaction(signum, act, oldact);
@@ -676,10 +636,14 @@ 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() == POSIX_KERNEL) || !(readfds || writefds || exceptfds) || nfds == 0) {
+ if (nfds <= 0 || !(readfds || writefds || exceptfds)) {
+ GAZELLE_RETURN(EINVAL);
+ }
+
+ if (select_posix_path() == POSIX_KERNEL) {
return posix_api->select_fn(nfds, readfds, writefds, exceptfds, timeout);
}
-
+
return g_wrap_api->select_fn(nfds, readfds, writefds, exceptfds, timeout);
}
@@ -690,9 +654,9 @@ static int32_t do_select(int32_t nfds, fd_set *readfds, fd_set *writefds, fd_set
va_start(ap, _cmd); \
val = va_arg(ap, typeof(val)); \
va_end(ap); \
- struct lwip_sock *sock = NULL; \
+ struct lwip_sock *sock = lwip_get_socket(_fd); \
if (select_posix_path() == POSIX_KERNEL || \
- select_fd_posix_path(_fd, &sock) == POSIX_KERNEL) \
+ select_sock_posix_path(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_control_plane.c b/src/lstack/core/lstack_control_plane.c
index 5545b9f..f1e3064 100644
--- a/src/lstack/core/lstack_control_plane.c
+++ b/src/lstack/core/lstack_control_plane.c
@@ -764,7 +764,7 @@ void control_server_thread(void *arg)
struct epoll_event evt_array;
while (1) {
/* wait init finish */
- if (posix_api->ues_posix) {
+ if (posix_api->use_kernel) {
usleep(GAZELLE_10MS);
continue;
}
@@ -813,7 +813,7 @@ void control_client_thread(void *arg)
while (1) {
/* wait init finish */
- if (posix_api->ues_posix) {
+ if (posix_api->use_kernel) {
usleep(GAZELLE_10MS);
continue;
}
diff --git a/src/lstack/core/lstack_init.c b/src/lstack/core/lstack_init.c
index 1b3882e..54ee97e 100644
--- a/src/lstack/core/lstack_init.c
+++ b/src/lstack/core/lstack_init.c
@@ -123,7 +123,7 @@ void gazelle_exit(void)
__attribute__((destructor)) void gazelle_network_exit(void)
{
- if (posix_api != NULL && !posix_api->ues_posix) {
+ if (posix_api != NULL && !posix_api->use_kernel) {
lwip_exit();
gazelle_exit();
}
@@ -324,7 +324,7 @@ __attribute__((constructor)) void gazelle_network_init(void)
LSTACK_EXIT(1, "set_process_start_flag failed\n");
}
- posix_api->ues_posix = 0;
+ posix_api->use_kernel = 0;
LSTACK_LOG(INFO, LSTACK, "gazelle_network_init success\n");
rte_smp_mb();
}
diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c
index 84ef782..2b39d05 100644
--- a/src/lstack/core/lstack_lwip.c
+++ b/src/lstack/core/lstack_lwip.c
@@ -79,6 +79,7 @@ static void reset_sock_data(struct lwip_sock *sock)
sock->send_pre_del = NULL;
}
+ sock->type = 0;
sock->stack = NULL;
sock->wakeup = NULL;
sock->listen_next = NULL;
@@ -1167,7 +1168,7 @@ void do_lwip_connected_callback(struct netconn *conn)
int32_t fd = conn->callback_arg.socket;
struct lwip_sock *sock = lwip_get_socket(fd);
- if (sock == NULL || sock->conn == NULL) {
+ if (POSIX_IS_CLOSED(sock)) {
return;
}
@@ -1208,7 +1209,7 @@ static void copy_pcb_to_conn(struct gazelle_stat_lstack_conn_info *conn, const s
conn->fd = netconn->callback_arg.socket;
conn->recv_cnt = (netconn->recvmbox == NULL) ? 0 : rte_ring_count(netconn->recvmbox->ring);
struct lwip_sock *sock = lwip_get_socket(netconn->callback_arg.socket);
- if (sock != NULL && sock->conn != NULL) {
+ if (!POSIX_IS_CLOSED(sock)) {
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);
diff --git a/src/lstack/core/lstack_preload.c b/src/lstack/core/lstack_preload.c
index 8cf4657..689d2bf 100644
--- a/src/lstack/core/lstack_preload.c
+++ b/src/lstack/core/lstack_preload.c
@@ -79,17 +79,22 @@ static void preload_get_thrdname(void)
LSTACK_PRE_LOG(LSTACK_INFO, "thread name=%s ok\n", g_preload_info.env_thrdname);
}
-enum posix_type select_fd_posix_path(int32_t fd, struct lwip_sock **socket)
+enum posix_type select_sock_posix_path(struct lwip_sock *sock)
{
- struct lwip_sock *sock = lwip_get_socket(fd);
-
- /* AF_UNIX case */
- if (!sock || !sock->conn || POSIX_IS_TYPE(sock, POSIX_KERNEL)) {
+ if (unlikely(posix_api == NULL)) {
+ /*
+ * read/write/readv/writev may not be sockfd,
+ * posix api maybe not init.
+ */
+ if (posix_api_init() != 0) {
+ LSTACK_PRE_LOG(LSTACK_ERR, "posix_api_init failed\n");
+ }
return POSIX_KERNEL;
}
- if (socket) {
- *socket = sock;
+ /* CLOSED means not sockfd, such as file fd or unix fd */
+ if (POSIX_IS_CLOSED(sock) || POSIX_IS_TYPE(sock, POSIX_KERNEL)) {
+ return POSIX_KERNEL;
}
if (likely(POSIX_IS_TYPE(sock, POSIX_LWIP))) {
@@ -112,11 +117,11 @@ enum posix_type select_posix_path(void)
return POSIX_KERNEL;
}
- if (unlikely(posix_api->ues_posix)) {
+ if (unlikely(posix_api->use_kernel)) {
return POSIX_KERNEL;
}
- if (g_preload_thrdpath != POSIX_ALL) {
+ if (likely(g_preload_thrdpath != POSIX_ALL)) {
return g_preload_thrdpath;
}
diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c
index d17cb67..a809d8c 100644
--- a/src/lstack/core/lstack_protocol_stack.c
+++ b/src/lstack/core/lstack_protocol_stack.c
@@ -127,7 +127,7 @@ struct protocol_stack *get_protocol_stack(void)
struct protocol_stack *get_protocol_stack_by_fd(int32_t fd)
{
struct lwip_sock *sock = lwip_get_socket(fd);
- if (sock == NULL || sock->conn == NULL) {
+ if (POSIX_IS_CLOSED(sock)) {
return NULL;
}
@@ -1126,7 +1126,7 @@ int32_t stack_broadcast_close(int32_t fd)
ret = -1;
}
- if (sock == NULL || sock->conn == NULL) {
+ if (POSIX_IS_CLOSED(sock)) {
break;
}
fd = sock->conn->callback_arg.socket;
@@ -1151,7 +1151,7 @@ int stack_broadcast_shutdown(int fd, int how)
ret = -1;
}
- if (sock == NULL || sock->conn == NULL) {
+ if (POSIX_IS_CLOSED(sock)) {
break;
}
fd = sock->conn->callback_arg.socket;
@@ -1357,7 +1357,7 @@ static void stack_all_fds_close(void)
{
for (int i = 3; i < GAZELLE_MAX_CLIENTS + GAZELLE_RESERVED_CLIENTS; i++) {
struct lwip_sock *sock = lwip_get_socket(i);
- if (sock && sock->conn && sock->stack == get_protocol_stack()) {
+ if (!POSIX_IS_CLOSED(sock) && sock->stack == get_protocol_stack()) {
lwip_close(i);
}
}
diff --git a/src/lstack/include/lstack_preload.h b/src/lstack/include/lstack_preload.h
index c30736a..d4ad385 100644
--- a/src/lstack/include/lstack_preload.h
+++ b/src/lstack/include/lstack_preload.h
@@ -11,9 +11,9 @@
*/
#ifndef __LSTACK_PRELOAD_H__
#define __LSTACK_PRELOAD_H__
-#include <stdbool.h>
enum posix_type select_posix_path(void);
-enum posix_type select_fd_posix_path(int32_t fd, struct lwip_sock **socket);
+enum posix_type select_sock_posix_path(struct lwip_sock *sock);
int preload_info_init(void);
-#endif
+
+#endif /* __LSTACK_PRELOAD_H__ */
diff --git a/src/lstack/netif/lstack_flow.c b/src/lstack/netif/lstack_flow.c
index 84c5c61..1ca3314 100644
--- a/src/lstack/netif/lstack_flow.c
+++ b/src/lstack/netif/lstack_flow.c
@@ -19,6 +19,7 @@
#include <uthash.h>
#include <lwip/lwipgz_posix_api.h>
+#include <lwip/sys.h>
#include <lwip/tcp.h>
#include <lwip/prot/tcp.h>
--
2.33.0
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化