summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests
diff options
context:
space:
mode:
authorStanislav Fomichev <sdf@google.com>2019-09-23 14:41:12 -0400
committerDaniel Borkmann <daniel@iogearbox.net>2019-09-25 16:13:45 -0400
commit8a03222f508bf09e03cf38f6bd77b34b450c1d60 (patch)
tree700c5bce7174eb03d2fab2c678216603ac985872 /tools/testing/selftests
parent733ef7f056a5e23b66e8e7bb3508ca882db388f0 (diff)
selftests/bpf: test_progs: fix client/server race in tcp_rtt
This is the same problem I found earlier in test_sockopt_inherit: there is a race between server thread doing accept() and client thread doing connect(). Let's explicitly synchronize them via pthread conditional variable. v2: * don't exit from server_thread without signaling condvar, fixes possible issue where main() would wait forever (Andrii Nakryiko) Fixes: b55873984dab ("selftests/bpf: test BPF_SOCK_OPS_RTT_CB") Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/testing/selftests')
-rw-r--r--tools/testing/selftests/bpf/prog_tests/tcp_rtt.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
index fdc0b3614a9e..a82da555b1b0 100644
--- a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
+++ b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
@@ -203,14 +203,24 @@ static int start_server(void)
203 return fd; 203 return fd;
204} 204}
205 205
206static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
207static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
208
206static void *server_thread(void *arg) 209static void *server_thread(void *arg)
207{ 210{
208 struct sockaddr_storage addr; 211 struct sockaddr_storage addr;
209 socklen_t len = sizeof(addr); 212 socklen_t len = sizeof(addr);
210 int fd = *(int *)arg; 213 int fd = *(int *)arg;
211 int client_fd; 214 int client_fd;
215 int err;
216
217 err = listen(fd, 1);
218
219 pthread_mutex_lock(&server_started_mtx);
220 pthread_cond_signal(&server_started);
221 pthread_mutex_unlock(&server_started_mtx);
212 222
213 if (CHECK_FAIL(listen(fd, 1)) < 0) { 223 if (CHECK_FAIL(err < 0)) {
214 perror("Failed to listed on socket"); 224 perror("Failed to listed on socket");
215 return NULL; 225 return NULL;
216 } 226 }
@@ -248,7 +258,14 @@ void test_tcp_rtt(void)
248 if (CHECK_FAIL(server_fd < 0)) 258 if (CHECK_FAIL(server_fd < 0))
249 goto close_cgroup_fd; 259 goto close_cgroup_fd;
250 260
251 pthread_create(&tid, NULL, server_thread, (void *)&server_fd); 261 if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread,
262 (void *)&server_fd)))
263 goto close_cgroup_fd;
264
265 pthread_mutex_lock(&server_started_mtx);
266 pthread_cond_wait(&server_started, &server_started_mtx);
267 pthread_mutex_unlock(&server_started_mtx);
268
252 CHECK_FAIL(run_test(cgroup_fd, server_fd)); 269 CHECK_FAIL(run_test(cgroup_fd, server_fd));
253 close(server_fd); 270 close(server_fd);
254close_cgroup_fd: 271close_cgroup_fd: