Skip to content

Commit 4cd729f

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
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: 8a03222 ("selftests/bpf: test_progs: fix client/server race in tcp_rtt") Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Reviewed-by: Stanislav Fomichev <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 2b5cf9f commit 4cd729f

File tree

1 file changed

+20
-12
lines changed
  • tools/testing/selftests/bpf/prog_tests

1 file changed

+20
-12
lines changed

tools/testing/selftests/bpf/prog_tests/tcp_rtt.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static int start_server(void)
188188
};
189189
int fd;
190190

191-
fd = socket(AF_INET, SOCK_STREAM, 0);
191+
fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
192192
if (fd < 0) {
193193
log_err("Failed to create server socket");
194194
return -1;
@@ -205,6 +205,7 @@ static int start_server(void)
205205

206206
static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
207207
static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
208+
static volatile bool server_done = false;
208209

209210
static void *server_thread(void *arg)
210211
{
@@ -222,23 +223,24 @@ static void *server_thread(void *arg)
222223

223224
if (CHECK_FAIL(err < 0)) {
224225
perror("Failed to listed on socket");
225-
return NULL;
226+
return ERR_PTR(err);
226227
}
227228

228-
client_fd = accept(fd, (struct sockaddr *)&addr, &len);
229+
while (!server_done) {
230+
client_fd = accept(fd, (struct sockaddr *)&addr, &len);
231+
if (client_fd == -1 && errno == EAGAIN) {
232+
usleep(50);
233+
continue;
234+
}
235+
break;
236+
}
229237
if (CHECK_FAIL(client_fd < 0)) {
230238
perror("Failed to accept client");
231-
return NULL;
239+
return ERR_PTR(err);
232240
}
233241

234-
/* Wait for the next connection (that never arrives)
235-
* to keep this thread alive to prevent calling
236-
* close() on client_fd.
237-
*/
238-
if (CHECK_FAIL(accept(fd, (struct sockaddr *)&addr, &len) >= 0)) {
239-
perror("Unexpected success in second accept");
240-
return NULL;
241-
}
242+
while (!server_done)
243+
usleep(50);
242244

243245
close(client_fd);
244246

@@ -249,6 +251,7 @@ void test_tcp_rtt(void)
249251
{
250252
int server_fd, cgroup_fd;
251253
pthread_t tid;
254+
void *server_res;
252255

253256
cgroup_fd = test__join_cgroup("/tcp_rtt");
254257
if (CHECK_FAIL(cgroup_fd < 0))
@@ -267,6 +270,11 @@ void test_tcp_rtt(void)
267270
pthread_mutex_unlock(&server_started_mtx);
268271

269272
CHECK_FAIL(run_test(cgroup_fd, server_fd));
273+
274+
server_done = true;
275+
pthread_join(tid, &server_res);
276+
CHECK_FAIL(IS_ERR(server_res));
277+
270278
close_server_fd:
271279
close(server_fd);
272280
close_cgroup_fd:

0 commit comments

Comments
 (0)