From 840d501625839cc9c837f65c98baa0def5af3616 Mon Sep 17 00:00:00 2001 From: compile_success <980965867@qq.com> Date: Sat, 15 Oct 2022 12:06:56 +0000 Subject: [PATCH 1/2] add accept4 and epoll_create1 --- src/lstack/api/lstack_epoll.c | 15 +++++++++-- src/lstack/api/lstack_wrap.c | 31 ++++++++++++++++++---- src/lstack/core/lstack_lwip.c | 7 ++++- src/lstack/core/lstack_protocol_stack.c | 11 +++++--- src/lstack/core/lstack_thread_rpc.c | 3 ++- src/lstack/include/lstack_protocol_stack.h | 1 + src/lstack/include/lstack_thread_rpc.h | 2 +- src/lstack/include/posix/lstack_epoll.h | 1 + 8 files changed, 58 insertions(+), 13 deletions(-) diff --git a/src/lstack/api/lstack_epoll.c b/src/lstack/api/lstack_epoll.c index 4da88b0..1206e75 100644 --- a/src/lstack/api/lstack_epoll.c +++ b/src/lstack/api/lstack_epoll.c @@ -105,9 +105,8 @@ static void raise_pending_events(struct wakeup_poll *wakeup, struct lwip_sock *s } } -int32_t lstack_epoll_create(int32_t size) +int32_t lstack_do_epoll_create(int32_t fd) { - int32_t fd = posix_api->epoll_create_fn(size); if (fd < 0) { return fd; } @@ -154,6 +153,18 @@ int32_t lstack_epoll_create(int32_t size) return fd; } +int32_t lstack_epoll_create1(int32_t flags) +{ + int32_t fd = posix_api->epoll_create1_fn(flags); + return lstack_do_epoll_create(fd); +} + +int32_t lstack_epoll_create(int32_t flags) +{ + int32_t fd = posix_api->epoll_create_fn(flags); + return lstack_do_epoll_create(fd); +} + int32_t lstack_epoll_close(int32_t fd) { posix_api->close_fn(fd); diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c index d88513b..4669a30 100644 --- a/src/lstack/api/lstack_wrap.c +++ b/src/lstack/api/lstack_wrap.c @@ -80,6 +80,23 @@ static enum KERNEL_LWIP_PATH select_path(int fd) return PATH_UNKNOW; } +static inline int32_t do_epoll_create1(int32_t flags) +{ + if (posix_api == NULL) { + /* posix api maybe call before gazelle init */ + if (posix_api_init() != 0) { + LSTACK_PRE_LOG(LSTACK_ERR, "posix_api_init failed\n"); + } + return posix_api->epoll_create1_fn(flags); + } + + if (unlikely(posix_api->ues_posix)) { + return posix_api->epoll_create1_fn(flags); + } + + return lstack_epoll_create1(flags); +} + static inline int32_t do_epoll_create(int32_t size) { if (posix_api == NULL) { @@ -147,11 +164,7 @@ static int32_t do_accept4(int32_t s, struct sockaddr *addr, socklen_t *addrlen, return posix_api->accept4_fn(s, addr, addrlen, flags); } - if ((flags & SOCK_CLOEXEC) == 0) { - return 0; - } - - int32_t fd = stack_broadcast_accept(s, addr, addrlen); + int32_t fd = stack_broadcast_accept4(s, addr, addrlen, flags); if (fd >= 0) { return fd; } @@ -432,6 +445,10 @@ static int32_t do_sigaction(int32_t signum, const struct sigaction *act, struct * ------- LD_PRELOAD mode replacement interface -------- * -------------------------------------------------------- */ +int32_t epoll_create1(int32_t flags) +{ + return do_epoll_create1(flags); +} int32_t epoll_create(int32_t size) { return do_epoll_create(size); @@ -550,6 +567,10 @@ pid_t fork(void) * -------------------------------------------------------- */ +int32_t __wrap_epoll_create1(int32_t size) +{ + return do_epoll_create1(size); +} int32_t __wrap_epoll_create(int32_t size) { return do_epoll_create(size); diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c index 5ca0cc3..94f95fa 100644 --- a/src/lstack/core/lstack_lwip.c +++ b/src/lstack/core/lstack_lwip.c @@ -790,7 +790,12 @@ static inline void clone_lwip_socket_opt(struct lwip_sock *dst_sock, struct lwip int32_t gazelle_socket(int domain, int type, int protocol) { - int32_t fd = lwip_socket(AF_INET, SOCK_STREAM, 0); + if (((type & SOCK_TYPE_MASK) & ~SOCK_STREAM) != 0){ + LSTACK_LOG(ERR, LSTACK, "sock type error:%d, only support SOCK_STREAM \n", type); + return -1; + } + + int32_t fd = lwip_socket(AF_INET, type, 0); if (fd < 0) { return fd; } diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c index 71412b4..c381187 100644 --- a/src/lstack/core/lstack_protocol_stack.c +++ b/src/lstack/core/lstack_protocol_stack.c @@ -531,7 +531,7 @@ void stack_accept(struct rpc_msg *msg) int32_t fd = msg->args[MSG_ARG_0].i; msg->result = -1; - int32_t accept_fd = lwip_accept(fd, msg->args[MSG_ARG_1].p, msg->args[MSG_ARG_2].p); + int32_t accept_fd = lwip_accept4(fd, msg->args[MSG_ARG_1].p, msg->args[MSG_ARG_2].p, msg->args[MSG_ARG_3].i); if (accept_fd < 0) { LSTACK_LOG(ERR, LSTACK, "fd %d ret %d\n", fd, accept_fd); return; @@ -749,7 +749,7 @@ static void inline del_accept_in_event(struct lwip_sock *sock) } /* ergodic the protocol stack thread to find the connection, because all threads are listening */ -int32_t stack_broadcast_accept(int32_t fd, struct sockaddr *addr, socklen_t *addrlen) +int32_t stack_broadcast_accept4(int32_t fd, struct sockaddr *addr, socklen_t *addrlen, int flags) { int32_t ret = -1; @@ -761,7 +761,7 @@ int32_t stack_broadcast_accept(int32_t fd, struct sockaddr *addr, socklen_t *add struct lwip_sock *min_sock = get_min_accept_sock(fd); if (min_sock && min_sock->conn) { - ret = rpc_call_accept(min_sock->conn->socket, addr, addrlen); + ret = rpc_call_accept(min_sock->conn->socket, addr, addrlen, flags); } if (min_sock && min_sock->wakeup && min_sock->wakeup->type == WAKEUP_EPOLL) { @@ -773,3 +773,8 @@ int32_t stack_broadcast_accept(int32_t fd, struct sockaddr *addr, socklen_t *add } return ret; } + +int32_t stack_broadcast_accept(int32_t fd, struct sockaddr *addr, socklen_t *addrlen) +{ + return stack_broadcast_accept4(fd, addr, addrlen, 0); +} diff --git a/src/lstack/core/lstack_thread_rpc.c b/src/lstack/core/lstack_thread_rpc.c index ad967e9..bc77909 100644 --- a/src/lstack/core/lstack_thread_rpc.c +++ b/src/lstack/core/lstack_thread_rpc.c @@ -299,7 +299,7 @@ int32_t rpc_call_listen(int s, int backlog) return rpc_sync_call(&stack->rpc_queue, msg); } -int32_t rpc_call_accept(int fd, struct sockaddr *addr, socklen_t *addrlen) +int32_t rpc_call_accept(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags) { struct protocol_stack *stack = get_protocol_stack_by_fd(fd); struct rpc_msg *msg = rpc_msg_alloc(stack, stack_accept); @@ -310,6 +310,7 @@ int32_t rpc_call_accept(int fd, struct sockaddr *addr, socklen_t *addrlen) msg->args[MSG_ARG_0].i = fd; msg->args[MSG_ARG_1].p = addr; msg->args[MSG_ARG_2].p = addrlen; + msg->args[MSG_ARG_3].i = flags; return rpc_sync_call(&stack->rpc_queue, msg); } diff --git a/src/lstack/include/lstack_protocol_stack.h b/src/lstack/include/lstack_protocol_stack.h index 4249217..0eda45d 100644 --- a/src/lstack/include/lstack_protocol_stack.h +++ b/src/lstack/include/lstack_protocol_stack.h @@ -115,6 +115,7 @@ int32_t stack_single_listen(int32_t fd, int32_t backlog); /* ergodic the protocol stack thread to find the connection, because all threads are listening */ int32_t stack_broadcast_accept(int32_t fd, struct sockaddr *addr, socklen_t *addrlen); +int32_t stack_broadcast_accept4(int32_t fd, struct sockaddr *addr, socklen_t *addrlen, int32_t flags); struct rpc_msg; void stack_arp(struct rpc_msg *msg); diff --git a/src/lstack/include/lstack_thread_rpc.h b/src/lstack/include/lstack_thread_rpc.h index 175c8c9..f95bc72 100644 --- a/src/lstack/include/lstack_thread_rpc.h +++ b/src/lstack/include/lstack_thread_rpc.h @@ -64,7 +64,7 @@ int32_t rpc_call_socket(int32_t domain, int32_t type, int32_t protocol); int32_t rpc_call_close(int32_t fd); int32_t rpc_call_bind(int32_t fd, const struct sockaddr *addr, socklen_t addrlen); int32_t rpc_call_listen(int s, int backlog); -int32_t rpc_call_accept(int fd, struct sockaddr *addr, socklen_t *addrlen); +int32_t rpc_call_accept(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags); int32_t rpc_call_connect(int fd, const struct sockaddr *addr, socklen_t addrlen); int32_t rpc_call_send(int fd, const void *buf, size_t len, int flags); int32_t rpc_call_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen); diff --git a/src/lstack/include/posix/lstack_epoll.h b/src/lstack/include/posix/lstack_epoll.h index 5799028..3c8fd1b 100644 --- a/src/lstack/include/posix/lstack_epoll.h +++ b/src/lstack/include/posix/lstack_epoll.h @@ -65,6 +65,7 @@ struct lwip_sock; void add_sock_event(struct lwip_sock *sock, uint32_t event); void wakeup_epoll(struct protocol_stack *stack, struct wakeup_poll *wakeup); int32_t lstack_epoll_create(int32_t size); +int32_t lstack_epoll_create1(int32_t flags); int32_t lstack_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_event *event); int32_t lstack_epoll_wait(int32_t epfd, struct epoll_event *events, int32_t maxevents, int32_t timeout); int32_t lstack_poll(struct pollfd *fds, nfds_t nfds, int32_t timeout); -- 2.23.0