bpf: sockmap, add tests for proto updates many to single map

Add test with a single map where each socket is inserted multiple
times. Test protocols: TCP, UDP, stream af_unix and dgram af_unix.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com>
Link: https://lore.kernel.org/r/20231221232327.43678-4-john.fastabend@gmail.com
This commit is contained in:
John Fastabend 2023-12-21 15:23:25 -08:00 committed by Martin KaFai Lau
parent 7865dfb1eb
commit 8c1b382a55

View File

@ -555,6 +555,74 @@ static void test_sockmap_unconnected_unix(void)
close(dgram);
}
static void test_sockmap_many_socket(void)
{
struct test_sockmap_pass_prog *skel;
int stream[2], dgram, udp, tcp;
int i, err, map, entry = 0;
skel = test_sockmap_pass_prog__open_and_load();
if (!ASSERT_OK_PTR(skel, "open_and_load"))
return;
map = bpf_map__fd(skel->maps.sock_map_rx);
dgram = xsocket(AF_UNIX, SOCK_DGRAM, 0);
if (dgram < 0) {
test_sockmap_pass_prog__destroy(skel);
return;
}
tcp = connected_socket_v4();
if (!ASSERT_GE(tcp, 0, "connected_socket_v4")) {
close(dgram);
test_sockmap_pass_prog__destroy(skel);
return;
}
udp = xsocket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
if (udp < 0) {
close(dgram);
close(tcp);
test_sockmap_pass_prog__destroy(skel);
return;
}
err = socketpair(AF_UNIX, SOCK_STREAM, 0, stream);
ASSERT_OK(err, "socketpair(af_unix, sock_stream)");
if (err)
goto out;
for (i = 0; i < 2; i++, entry++) {
err = bpf_map_update_elem(map, &entry, &stream[0], BPF_ANY);
ASSERT_OK(err, "bpf_map_update_elem(stream)");
}
for (i = 0; i < 2; i++, entry++) {
err = bpf_map_update_elem(map, &entry, &dgram, BPF_ANY);
ASSERT_OK(err, "bpf_map_update_elem(dgram)");
}
for (i = 0; i < 2; i++, entry++) {
err = bpf_map_update_elem(map, &entry, &udp, BPF_ANY);
ASSERT_OK(err, "bpf_map_update_elem(udp)");
}
for (i = 0; i < 2; i++, entry++) {
err = bpf_map_update_elem(map, &entry, &tcp, BPF_ANY);
ASSERT_OK(err, "bpf_map_update_elem(tcp)");
}
for (entry--; entry >= 0; entry--) {
err = bpf_map_delete_elem(map, &entry);
ASSERT_OK(err, "bpf_map_delete_elem(entry)");
}
close(stream[0]);
close(stream[1]);
out:
close(dgram);
close(tcp);
close(udp);
test_sockmap_pass_prog__destroy(skel);
}
void test_sockmap_basic(void)
{
if (test__start_subtest("sockmap create_update_free"))
@ -597,7 +665,8 @@ void test_sockmap_basic(void)
test_sockmap_skb_verdict_fionread(false);
if (test__start_subtest("sockmap skb_verdict msg_f_peek"))
test_sockmap_skb_verdict_peek();
if (test__start_subtest("sockmap unconnected af_unix"))
test_sockmap_unconnected_unix();
if (test__start_subtest("sockmap one socket to many map entries"))
test_sockmap_many_socket();
}