Skip to content

Commit bdbca46

Browse files
jrfastabMartin KaFai Lau
authored andcommitted
bpf: sockmap, add tests for proto updates replace socket
Add test that replaces the same socket with itself. This exercises a corner case where old element and new element have the same posck. Test protocols: TCP, UDP, stream af_unix and dgram af_unix. Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Reviewed-by: Jakub Sitnicki <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent f130046 commit bdbca46

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,73 @@ static void test_sockmap_many_maps(void)
695695
test_sockmap_pass_prog__destroy(skel);
696696
}
697697

698+
static void test_sockmap_same_sock(void)
699+
{
700+
struct test_sockmap_pass_prog *skel;
701+
int stream[2], dgram, udp, tcp;
702+
int i, err, map, zero = 0;
703+
704+
skel = test_sockmap_pass_prog__open_and_load();
705+
if (!ASSERT_OK_PTR(skel, "open_and_load"))
706+
return;
707+
708+
map = bpf_map__fd(skel->maps.sock_map_rx);
709+
710+
dgram = xsocket(AF_UNIX, SOCK_DGRAM, 0);
711+
if (dgram < 0) {
712+
test_sockmap_pass_prog__destroy(skel);
713+
return;
714+
}
715+
716+
tcp = connected_socket_v4();
717+
if (!ASSERT_GE(tcp, 0, "connected_socket_v4")) {
718+
close(dgram);
719+
test_sockmap_pass_prog__destroy(skel);
720+
return;
721+
}
722+
723+
udp = xsocket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
724+
if (udp < 0) {
725+
close(dgram);
726+
close(tcp);
727+
test_sockmap_pass_prog__destroy(skel);
728+
return;
729+
}
730+
731+
err = socketpair(AF_UNIX, SOCK_STREAM, 0, stream);
732+
ASSERT_OK(err, "socketpair(af_unix, sock_stream)");
733+
if (err)
734+
goto out;
735+
736+
for (i = 0; i < 2; i++) {
737+
err = bpf_map_update_elem(map, &zero, &stream[0], BPF_ANY);
738+
ASSERT_OK(err, "bpf_map_update_elem(stream)");
739+
}
740+
for (i = 0; i < 2; i++) {
741+
err = bpf_map_update_elem(map, &zero, &dgram, BPF_ANY);
742+
ASSERT_OK(err, "bpf_map_update_elem(dgram)");
743+
}
744+
for (i = 0; i < 2; i++) {
745+
err = bpf_map_update_elem(map, &zero, &udp, BPF_ANY);
746+
ASSERT_OK(err, "bpf_map_update_elem(udp)");
747+
}
748+
for (i = 0; i < 2; i++) {
749+
err = bpf_map_update_elem(map, &zero, &tcp, BPF_ANY);
750+
ASSERT_OK(err, "bpf_map_update_elem(tcp)");
751+
}
752+
753+
err = bpf_map_delete_elem(map, &zero);
754+
ASSERT_OK(err, "bpf_map_delete_elem(entry)");
755+
756+
close(stream[0]);
757+
close(stream[1]);
758+
out:
759+
close(dgram);
760+
close(tcp);
761+
close(udp);
762+
test_sockmap_pass_prog__destroy(skel);
763+
}
764+
698765
void test_sockmap_basic(void)
699766
{
700767
if (test__start_subtest("sockmap create_update_free"))
@@ -743,4 +810,6 @@ void test_sockmap_basic(void)
743810
test_sockmap_many_socket();
744811
if (test__start_subtest("sockmap one socket to many maps"))
745812
test_sockmap_many_maps();
813+
if (test__start_subtest("sockmap same socket replace"))
814+
test_sockmap_same_sock();
746815
}

0 commit comments

Comments
 (0)