Skip to content

Commit

Permalink
selftests/bpf: add cgroup_skb netns cookie tests
Browse files Browse the repository at this point in the history
Add netns cookie test that verifies the helper is now supported and work
in the context of cgroup_skb programs.

Signed-off-by: Mahe Tardy <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Alexei Starovoitov <[email protected]>
  • Loading branch information
mtardy authored and Alexei Starovoitov committed Feb 26, 2025
1 parent c221d37 commit 9138048
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
21 changes: 14 additions & 7 deletions tools/testing/selftests/bpf/prog_tests/netns_cookie.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,25 @@ void test_netns_cookie(void)

skel->links.get_netns_cookie_sockops = bpf_program__attach_cgroup(
skel->progs.get_netns_cookie_sockops, cgroup_fd);
if (!ASSERT_OK_PTR(skel->links.get_netns_cookie_sockops, "prog_attach"))
if (!ASSERT_OK_PTR(skel->links.get_netns_cookie_sockops, "prog_attach_sockops"))
goto done;

verdict = bpf_program__fd(skel->progs.get_netns_cookie_sk_msg);
map = bpf_map__fd(skel->maps.sock_map);
err = bpf_prog_attach(verdict, map, BPF_SK_MSG_VERDICT, 0);
if (!ASSERT_OK(err, "prog_attach"))
if (!ASSERT_OK(err, "prog_attach_sk_msg"))
goto done;

tc_fd = bpf_program__fd(skel->progs.get_netns_cookie_tcx);
err = bpf_prog_attach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &opta);
if (!ASSERT_OK(err, "prog_attach"))
if (!ASSERT_OK(err, "prog_attach_tcx"))
goto done;

skel->links.get_netns_cookie_cgroup_skb = bpf_program__attach_cgroup(
skel->progs.get_netns_cookie_cgroup_skb, cgroup_fd);
if (!ASSERT_OK_PTR(skel->links.get_netns_cookie_cgroup_skb, "prog_attach_cgroup_skb"))
goto cleanup_tc;

server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
if (CHECK(server_fd < 0, "start_server", "errno %d\n", errno))
goto cleanup_tc;
Expand All @@ -69,16 +74,18 @@ void test_netns_cookie(void)
if (!ASSERT_OK(err, "getsockopt"))
goto cleanup_tc;

ASSERT_EQ(val, cookie_expected_value, "cookie_value");
ASSERT_EQ(val, cookie_expected_value, "cookie_value_sockops");

err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sk_msg_netns_cookies),
&client_fd, &val);
if (!ASSERT_OK(err, "map_lookup(sk_msg_netns_cookies)"))
goto cleanup_tc;

ASSERT_EQ(val, cookie_expected_value, "cookie_value");
ASSERT_EQ(skel->bss->tcx_init_netns_cookie, cookie_expected_value, "cookie_value");
ASSERT_EQ(skel->bss->tcx_netns_cookie, cookie_expected_value, "cookie_value");
ASSERT_EQ(val, cookie_expected_value, "cookie_value_sk_msg");
ASSERT_EQ(skel->bss->tcx_init_netns_cookie, cookie_expected_value, "cookie_value_init_tcx");
ASSERT_EQ(skel->bss->tcx_netns_cookie, cookie_expected_value, "cookie_value_tcx");
ASSERT_EQ(skel->bss->cgroup_skb_init_netns_cookie, cookie_expected_value, "cookie_value_init_cgroup_skb");
ASSERT_EQ(skel->bss->cgroup_skb_netns_cookie, cookie_expected_value, "cookie_value_cgroup_skb");

cleanup_tc:
err = bpf_prog_detach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &optd);
Expand Down
9 changes: 9 additions & 0 deletions tools/testing/selftests/bpf/progs/netns_cookie_prog.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct {
} sock_map SEC(".maps");

int tcx_init_netns_cookie, tcx_netns_cookie;
int cgroup_skb_init_netns_cookie, cgroup_skb_netns_cookie;

SEC("sockops")
int get_netns_cookie_sockops(struct bpf_sock_ops *ctx)
Expand Down Expand Up @@ -91,4 +92,12 @@ int get_netns_cookie_tcx(struct __sk_buff *skb)
return TCX_PASS;
}

SEC("cgroup_skb/ingress")
int get_netns_cookie_cgroup_skb(struct __sk_buff *skb)
{
cgroup_skb_init_netns_cookie = bpf_get_netns_cookie(NULL);
cgroup_skb_netns_cookie = bpf_get_netns_cookie(skb);
return SK_PASS;
}

char _license[] SEC("license") = "GPL";

0 comments on commit 9138048

Please sign in to comment.