Move accept_timeout and recv_timeout to the common place so they can be reused by the other tests. Switch to accept_timeout() in test_progs instead of doing while loop around accept(). This prevents the tests that use start_server_thread/stop_server_thread from being stuck when the error occurs. Cc: Andrey Ignatov <rdna@xxxxxx> Signed-off-by: Stanislav Fomichev <sdf@xxxxxxxxxx> --- tools/testing/selftests/bpf/network_helpers.c | 43 +++++++++++++++---- tools/testing/selftests/bpf/network_helpers.h | 4 ++ .../selftests/bpf/prog_tests/sockmap_listen.c | 35 +-------------- 3 files changed, 40 insertions(+), 42 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index ee9386b033ed..30ef0c7edebf 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -82,14 +82,7 @@ static void *server_thread(void *arg) return ERR_PTR(err); } - while (true) { - client_fd = accept(fd, (struct sockaddr *)&addr, &len); - if (client_fd == -1 && errno == EAGAIN) { - usleep(50); - continue; - } - break; - } + client_fd = accept_timeout(fd, (struct sockaddr *)&addr, &len, 3); if (CHECK_FAIL(client_fd < 0)) { perror("Failed to accept client"); return ERR_PTR(err); @@ -162,3 +155,37 @@ int connect_to_fd(int family, int server_fd) close(fd); return -1; } + +static int poll_read(int fd, unsigned int timeout_sec) +{ + struct timeval timeout = { .tv_sec = timeout_sec }; + fd_set rfds; + int r; + + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + + r = select(fd + 1, &rfds, NULL, NULL, &timeout); + if (r == 0) + errno = ETIME; + + return r == 1 ? 0 : -1; +} + +int accept_timeout(int fd, struct sockaddr *addr, socklen_t *len, + unsigned int timeout_sec) +{ + if (poll_read(fd, timeout_sec)) + return -1; + + return accept(fd, addr, len); +} + +int recv_timeout(int fd, void *buf, size_t len, int flags, + unsigned int timeout_sec) +{ + if (poll_read(fd, timeout_sec)) + return -1; + + return recv(fd, buf, len, flags); +} diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 1f3942160287..74e2c40667a2 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -7,5 +7,9 @@ int start_server_thread(int family); void stop_server_thread(int fd); int connect_to_fd(int family, int server_fd); +int accept_timeout(int fd, struct sockaddr *addr, socklen_t *len, + unsigned int timeout_sec); +int recv_timeout(int fd, void *buf, size_t len, int flags, + unsigned int timeout_sec); #endif diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c index d7d65a700799..c2a78d8a110e 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c @@ -24,6 +24,7 @@ #include "bpf_util.h" #include "test_progs.h" +#include "network_helpers.h" #include "test_sockmap_listen.skel.h" #define IO_TIMEOUT_SEC 30 @@ -195,40 +196,6 @@ __ret; \ }) -static int poll_read(int fd, unsigned int timeout_sec) -{ - struct timeval timeout = { .tv_sec = timeout_sec }; - fd_set rfds; - int r; - - FD_ZERO(&rfds); - FD_SET(fd, &rfds); - - r = select(fd + 1, &rfds, NULL, NULL, &timeout); - if (r == 0) - errno = ETIME; - - return r == 1 ? 0 : -1; -} - -static int accept_timeout(int fd, struct sockaddr *addr, socklen_t *len, - unsigned int timeout_sec) -{ - if (poll_read(fd, timeout_sec)) - return -1; - - return accept(fd, addr, len); -} - -static int recv_timeout(int fd, void *buf, size_t len, int flags, - unsigned int timeout_sec) -{ - if (poll_read(fd, timeout_sec)) - return -1; - - return recv(fd, buf, len, flags); -} - static void init_addr_loopback4(struct sockaddr_storage *ss, socklen_t *len) { struct sockaddr_in *addr4 = memset(ss, 0, sizeof(*ss)); -- 2.26.2.526.g744177e7f7-goog