Commit 4cd729fa authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

selftests/bpf: Make tcp_rtt test more robust to failures

Switch to non-blocking accept and wait for server thread to exit before
proceeding. I noticed that sometimes tcp_rtt server thread failure would
"spill over" into other tests (that would run after tcp_rtt), probably just
because server thread exits much later and tcp_rtt doesn't wait for it.

v1->v2:
  - add usleep() while waiting on initial non-blocking accept() (Stanislav);

Fixes: 8a03222f ("selftests/bpf: test_progs: fix client/server race in tcp_rtt")
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Reviewed-by: default avatarStanislav Fomichev <sdf@google.com>
Link: https://lore.kernel.org/bpf/20200311222749.458015-1-andriin@fb.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 2b5cf9fb
...@@ -188,7 +188,7 @@ static int start_server(void) ...@@ -188,7 +188,7 @@ static int start_server(void)
}; };
int fd; int fd;
fd = socket(AF_INET, SOCK_STREAM, 0); fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (fd < 0) { if (fd < 0) {
log_err("Failed to create server socket"); log_err("Failed to create server socket");
return -1; return -1;
...@@ -205,6 +205,7 @@ static int start_server(void) ...@@ -205,6 +205,7 @@ static int start_server(void)
static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER; static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
static volatile bool server_done = false;
static void *server_thread(void *arg) static void *server_thread(void *arg)
{ {
...@@ -222,23 +223,24 @@ static void *server_thread(void *arg) ...@@ -222,23 +223,24 @@ static void *server_thread(void *arg)
if (CHECK_FAIL(err < 0)) { if (CHECK_FAIL(err < 0)) {
perror("Failed to listed on socket"); perror("Failed to listed on socket");
return NULL; return ERR_PTR(err);
} }
while (!server_done) {
client_fd = accept(fd, (struct sockaddr *)&addr, &len); client_fd = accept(fd, (struct sockaddr *)&addr, &len);
if (client_fd == -1 && errno == EAGAIN) {
usleep(50);
continue;
}
break;
}
if (CHECK_FAIL(client_fd < 0)) { if (CHECK_FAIL(client_fd < 0)) {
perror("Failed to accept client"); perror("Failed to accept client");
return NULL; return ERR_PTR(err);
} }
/* Wait for the next connection (that never arrives) while (!server_done)
* to keep this thread alive to prevent calling usleep(50);
* close() on client_fd.
*/
if (CHECK_FAIL(accept(fd, (struct sockaddr *)&addr, &len) >= 0)) {
perror("Unexpected success in second accept");
return NULL;
}
close(client_fd); close(client_fd);
...@@ -249,6 +251,7 @@ void test_tcp_rtt(void) ...@@ -249,6 +251,7 @@ void test_tcp_rtt(void)
{ {
int server_fd, cgroup_fd; int server_fd, cgroup_fd;
pthread_t tid; pthread_t tid;
void *server_res;
cgroup_fd = test__join_cgroup("/tcp_rtt"); cgroup_fd = test__join_cgroup("/tcp_rtt");
if (CHECK_FAIL(cgroup_fd < 0)) if (CHECK_FAIL(cgroup_fd < 0))
...@@ -267,6 +270,11 @@ void test_tcp_rtt(void) ...@@ -267,6 +270,11 @@ void test_tcp_rtt(void)
pthread_mutex_unlock(&server_started_mtx); pthread_mutex_unlock(&server_started_mtx);
CHECK_FAIL(run_test(cgroup_fd, server_fd)); CHECK_FAIL(run_test(cgroup_fd, server_fd));
server_done = true;
pthread_join(tid, &server_res);
CHECK_FAIL(IS_ERR(server_res));
close_server_fd: close_server_fd:
close(server_fd); close(server_fd);
close_cgroup_fd: close_cgroup_fd:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment