Skip to content

Commit c2fcbf8

Browse files
iamkafaiborkmann
authored andcommitted
bpf, selftests: Fix racing issue in btf_skc_cls_ingress test
The libbpf CI reported occasional failure in btf_skc_cls_ingress: test_syncookie:FAIL:Unexpected syncookie states gen_cookie:80326634 recv_cookie:0 bpf prog error at line 97 "error at line 97" means the bpf prog cannot find the listening socket when the final ack is received. It then skipped processing the syncookie in the final ack which then led to "recv_cookie:0". The problem is the userspace program did not do accept() and went ahead to close(listen_fd) before the kernel (and the bpf prog) had a chance to process the final ack. The fix is to add accept() call so that the userspace will wait for the kernel to finish processing the final ack first before close()-ing everything. Fixes: 9a856ca ("bpf: selftest: Add test_btf_skc_cls_ingress") Reported-by: Andrii Nakryiko <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 7edc3fc commit c2fcbf8

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static void print_err_line(void)
9090

9191
static void test_conn(void)
9292
{
93-
int listen_fd = -1, cli_fd = -1, err;
93+
int listen_fd = -1, cli_fd = -1, srv_fd = -1, err;
9494
socklen_t addrlen = sizeof(srv_sa6);
9595
int srv_port;
9696

@@ -112,6 +112,10 @@ static void test_conn(void)
112112
if (CHECK_FAIL(cli_fd == -1))
113113
goto done;
114114

115+
srv_fd = accept(listen_fd, NULL, NULL);
116+
if (CHECK_FAIL(srv_fd == -1))
117+
goto done;
118+
115119
if (CHECK(skel->bss->listen_tp_sport != srv_port ||
116120
skel->bss->req_sk_sport != srv_port,
117121
"Unexpected sk src port",
@@ -134,11 +138,13 @@ static void test_conn(void)
134138
close(listen_fd);
135139
if (cli_fd != -1)
136140
close(cli_fd);
141+
if (srv_fd != -1)
142+
close(srv_fd);
137143
}
138144

139145
static void test_syncookie(void)
140146
{
141-
int listen_fd = -1, cli_fd = -1, err;
147+
int listen_fd = -1, cli_fd = -1, srv_fd = -1, err;
142148
socklen_t addrlen = sizeof(srv_sa6);
143149
int srv_port;
144150

@@ -161,6 +167,10 @@ static void test_syncookie(void)
161167
if (CHECK_FAIL(cli_fd == -1))
162168
goto done;
163169

170+
srv_fd = accept(listen_fd, NULL, NULL);
171+
if (CHECK_FAIL(srv_fd == -1))
172+
goto done;
173+
164174
if (CHECK(skel->bss->listen_tp_sport != srv_port,
165175
"Unexpected tp src port",
166176
"listen_tp_sport:%u expected:%u\n",
@@ -188,6 +198,8 @@ static void test_syncookie(void)
188198
close(listen_fd);
189199
if (cli_fd != -1)
190200
close(cli_fd);
201+
if (srv_fd != -1)
202+
close(srv_fd);
191203
}
192204

193205
struct test {

0 commit comments

Comments
 (0)