Message ID | 20241118-vsock-bpf-poll-close-v1-2-f1b9669cacdc@rbox.co (mailing list archive) |
---|---|
State | Accepted |
Commit | 9c2a2a45136de428b73907195a4a99eb78dc3aca |
Headers | show |
Series | bpf, vsock: Fix poll() and close() | expand |
On Mon, Nov 18, 2024 at 10:03:42PM +0100, Michal Luczaj wrote: >Verify that vsock's poll() notices when sk_psock::ingress_msg isn't empty. > >Signed-off-by: Michal Luczaj <mhal@rbox.co> >--- > .../selftests/bpf/prog_tests/sockmap_basic.c | 46 ++++++++++++++++++++++ > 1 file changed, 46 insertions(+) Acked-by: Stefano Garzarella <sgarzare@redhat.com> > >diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c >index 82bfb266741cfaceafa2a407cd2ccc937708c613..21d1e2e2308433e7475952dcab034e92f2f6101a 100644 >--- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c >+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c >@@ -885,6 +885,50 @@ static void test_sockmap_same_sock(void) > test_sockmap_pass_prog__destroy(skel); > } > >+static void test_sockmap_skb_verdict_vsock_poll(void) >+{ >+ struct test_sockmap_pass_prog *skel; >+ int err, map, conn, peer; >+ struct bpf_program *prog; >+ struct bpf_link *link; >+ char buf = 'x'; >+ int zero = 0; >+ >+ skel = test_sockmap_pass_prog__open_and_load(); >+ if (!ASSERT_OK_PTR(skel, "open_and_load")) >+ return; >+ >+ if (create_pair(AF_VSOCK, SOCK_STREAM, &conn, &peer)) >+ goto destroy; >+ >+ prog = skel->progs.prog_skb_verdict; >+ map = bpf_map__fd(skel->maps.sock_map_rx); >+ link = bpf_program__attach_sockmap(prog, map); >+ if (!ASSERT_OK_PTR(link, "bpf_program__attach_sockmap")) >+ goto close; >+ >+ err = bpf_map_update_elem(map, &zero, &conn, BPF_ANY); >+ if (!ASSERT_OK(err, "bpf_map_update_elem")) >+ goto detach; >+ >+ if (xsend(peer, &buf, 1, 0) != 1) >+ goto detach; >+ >+ err = poll_read(conn, IO_TIMEOUT_SEC); >+ if (!ASSERT_OK(err, "poll")) >+ goto detach; >+ >+ if (xrecv_nonblock(conn, &buf, 1, 0) != 1) >+ FAIL("xrecv_nonblock"); >+detach: >+ bpf_link__detach(link); >+close: >+ xclose(conn); >+ xclose(peer); >+destroy: >+ test_sockmap_pass_prog__destroy(skel); >+} >+ > void test_sockmap_basic(void) > { > if (test__start_subtest("sockmap create_update_free")) >@@ -943,4 +987,6 @@ void test_sockmap_basic(void) > test_skmsg_helpers_with_link(BPF_MAP_TYPE_SOCKMAP); > if (test__start_subtest("sockhash sk_msg attach sockhash helpers with link")) > test_skmsg_helpers_with_link(BPF_MAP_TYPE_SOCKHASH); >+ if (test__start_subtest("sockmap skb_verdict vsock poll")) >+ test_sockmap_skb_verdict_vsock_poll(); > } > >-- >2.46.2 >
diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c index 82bfb266741cfaceafa2a407cd2ccc937708c613..21d1e2e2308433e7475952dcab034e92f2f6101a 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c @@ -885,6 +885,50 @@ static void test_sockmap_same_sock(void) test_sockmap_pass_prog__destroy(skel); } +static void test_sockmap_skb_verdict_vsock_poll(void) +{ + struct test_sockmap_pass_prog *skel; + int err, map, conn, peer; + struct bpf_program *prog; + struct bpf_link *link; + char buf = 'x'; + int zero = 0; + + skel = test_sockmap_pass_prog__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + if (create_pair(AF_VSOCK, SOCK_STREAM, &conn, &peer)) + goto destroy; + + prog = skel->progs.prog_skb_verdict; + map = bpf_map__fd(skel->maps.sock_map_rx); + link = bpf_program__attach_sockmap(prog, map); + if (!ASSERT_OK_PTR(link, "bpf_program__attach_sockmap")) + goto close; + + err = bpf_map_update_elem(map, &zero, &conn, BPF_ANY); + if (!ASSERT_OK(err, "bpf_map_update_elem")) + goto detach; + + if (xsend(peer, &buf, 1, 0) != 1) + goto detach; + + err = poll_read(conn, IO_TIMEOUT_SEC); + if (!ASSERT_OK(err, "poll")) + goto detach; + + if (xrecv_nonblock(conn, &buf, 1, 0) != 1) + FAIL("xrecv_nonblock"); +detach: + bpf_link__detach(link); +close: + xclose(conn); + xclose(peer); +destroy: + test_sockmap_pass_prog__destroy(skel); +} + void test_sockmap_basic(void) { if (test__start_subtest("sockmap create_update_free")) @@ -943,4 +987,6 @@ void test_sockmap_basic(void) test_skmsg_helpers_with_link(BPF_MAP_TYPE_SOCKMAP); if (test__start_subtest("sockhash sk_msg attach sockhash helpers with link")) test_skmsg_helpers_with_link(BPF_MAP_TYPE_SOCKHASH); + if (test__start_subtest("sockmap skb_verdict vsock poll")) + test_sockmap_skb_verdict_vsock_poll(); }
Verify that vsock's poll() notices when sk_psock::ingress_msg isn't empty. Signed-off-by: Michal Luczaj <mhal@rbox.co> --- .../selftests/bpf/prog_tests/sockmap_basic.c | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+)