diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c index 97e927acae1f914e1b686f666e279ab4d9364711..b625c3cb4cbf964903017ab6455842407222fe0b 100644 --- a/src/lstack/api/lstack_wrap.c +++ b/src/lstack/api/lstack_wrap.c @@ -153,6 +153,26 @@ static int32_t do_accept4(int32_t s, struct sockaddr *addr, socklen_t *addrlen, return fd; } +static inline bool check_if_kernel_bind(int fd, ip_addr_t *addr) +{ + struct lwip_sock *sock = lwip_get_socket(fd); + + if (ip_addr_isany_val(*addr)) { + return true; + } + + /* lwip and kernel share IP, and exchange mbuf through virtual-NIC. + * lstack not sense if ltran enable kni, so only checks use_ltran. + */ + if (!get_global_cfg_params()->use_ltran && !get_global_cfg_params()->kni_switch && + !get_global_cfg_params()->flow_bifurcation) { + POSIX_SET_TYPE(sock, POSIX_LWIP); + return false; + } + + return true; +} + static inline int sock_set_nonblocking(int fd) { int flags = posix_api->fcntl_fn(fd, F_GETFL, 0); @@ -174,25 +194,14 @@ static int kernel_bind_process(int32_t s, const struct sockaddr *name, socklen_t struct lwip_sock *sock = lwip_get_socket(s); int times = 10; int ret = 0; - bool share_ip = true; - - /* lwip and kernel share IP, and exchange mbuf through virtual-NIC. - * lstack not sense if ltran enable kni, so only checks use_ltran. */ - - if (!get_global_cfg_params()->use_ltran && !get_global_cfg_params()->kni_switch && - !get_global_cfg_params()->flow_bifurcation) { - share_ip = false; - } ret = posix_api->bind_fn(s, name, namelen); - if (ret < 0 && errno == EADDRNOTAVAIL) { - /* ipv6 addr of virtual-NIC maybe is tentative, need to wait a few seconds */ - if (name->sa_family == AF_INET6 && share_ip) { - LSTACK_LOG(WARNING, LSTACK, "virtio_user addr is tentative, please wait... \n"); - while (ret != 0 && times-- > 0) { - sleep(1); - ret = posix_api->bind_fn(s, name, namelen); - } + /* ipv6 addr of virtual-NIC maybe is tentative, need to wait a few seconds */ + if (ret < 0 && name->sa_family == AF_INET6 && errno == EADDRNOTAVAIL) { + LSTACK_LOG(WARNING, LSTACK, "virtio_user addr is tentative, please wait... \n"); + while (ret != 0 && times-- > 0) { + sleep(1); + ret = posix_api->bind_fn(s, name, namelen); } } @@ -250,9 +259,10 @@ static int32_t do_bind(int32_t s, const struct sockaddr *name, socklen_t namelen return posix_api->bind_fn(s, name, namelen); } - if (kernel_bind_process(s, name, namelen) < 0) { + if (check_if_kernel_bind(s, &sock_addr) && (kernel_bind_process(s, name, namelen) < 0)) { return -1; } + return g_wrap_api->bind_fn(s, name, namelen); }