Skip to content

Commit bb516f9

Browse files
jrfastabborkmann
authored andcommitted
bpf, sockmap: Test FIONREAD returns correct bytes in rx buffer
A bug was reported where ioctl(FIONREAD) returned zero even though the socket with a SK_SKB verdict program attached had bytes in the msg queue. The result is programs may hang or more likely try to recover, but use suboptimal buffer sizes. Add a test to check that ioctl(FIONREAD) returns the correct number of bytes. Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: Jakub Sitnicki <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 1fa1fe8 commit bb516f9

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,52 @@ static void test_sockmap_skb_verdict_shutdown(void)
410410
test_sockmap_pass_prog__destroy(skel);
411411
}
412412

413+
static void test_sockmap_skb_verdict_fionread(void)
414+
{
415+
int err, map, verdict, s, c0, c1, p0, p1;
416+
struct test_sockmap_pass_prog *skel;
417+
int zero = 0, sent, recvd, avail;
418+
char buf[256] = "0123456789";
419+
420+
skel = test_sockmap_pass_prog__open_and_load();
421+
if (!ASSERT_OK_PTR(skel, "open_and_load"))
422+
return;
423+
424+
verdict = bpf_program__fd(skel->progs.prog_skb_verdict);
425+
map = bpf_map__fd(skel->maps.sock_map_rx);
426+
427+
err = bpf_prog_attach(verdict, map, BPF_SK_SKB_STREAM_VERDICT, 0);
428+
if (!ASSERT_OK(err, "bpf_prog_attach"))
429+
goto out;
430+
431+
s = socket_loopback(AF_INET, SOCK_STREAM);
432+
if (!ASSERT_GT(s, -1, "socket_loopback(s)"))
433+
goto out;
434+
err = create_socket_pairs(s, AF_INET, SOCK_STREAM, &c0, &c1, &p0, &p1);
435+
if (!ASSERT_OK(err, "create_socket_pairs(s)"))
436+
goto out;
437+
438+
err = bpf_map_update_elem(map, &zero, &c1, BPF_NOEXIST);
439+
if (!ASSERT_OK(err, "bpf_map_update_elem(c1)"))
440+
goto out_close;
441+
442+
sent = xsend(p1, &buf, sizeof(buf), 0);
443+
ASSERT_EQ(sent, sizeof(buf), "xsend(p0)");
444+
err = ioctl(c1, FIONREAD, &avail);
445+
ASSERT_OK(err, "ioctl(FIONREAD) error");
446+
ASSERT_EQ(avail, sizeof(buf), "ioctl(FIONREAD)");
447+
recvd = recv_timeout(c1, &buf, sizeof(buf), SOCK_NONBLOCK, IO_TIMEOUT_SEC);
448+
ASSERT_EQ(recvd, sizeof(buf), "recv_timeout(c0)");
449+
450+
out_close:
451+
close(c0);
452+
close(p0);
453+
close(c1);
454+
close(p1);
455+
out:
456+
test_sockmap_pass_prog__destroy(skel);
457+
}
458+
413459
void test_sockmap_basic(void)
414460
{
415461
if (test__start_subtest("sockmap create_update_free"))
@@ -446,4 +492,6 @@ void test_sockmap_basic(void)
446492
test_sockmap_progs_query(BPF_SK_SKB_VERDICT);
447493
if (test__start_subtest("sockmap skb_verdict shutdown"))
448494
test_sockmap_skb_verdict_shutdown();
495+
if (test__start_subtest("sockmap skb_verdict fionread"))
496+
test_sockmap_skb_verdict_fionread();
449497
}

0 commit comments

Comments
 (0)