Skip to content

Commit 15654a7

Browse files
selftests/bpf: add tcx netns cookie tests
JIRA: https://issues.redhat.com/browse/RHEL-110279 commit 693fe95 Author: Mahe Tardy <mahe.tardy@gmail.com> Date: Mon Oct 7 09:59:58 2024 +0000 selftests/bpf: add tcx netns cookie tests Add netns cookie test that verifies the helper is now supported and work in the context of tc programs. Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com> Link: https://lore.kernel.org/r/20241007095958.97442-2-mahe.tardy@gmail.com Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org> Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
1 parent 00a4fd1 commit 15654a7

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
#define SO_NETNS_COOKIE 71
99
#endif
1010

11+
#define loopback 1
12+
1113
static int duration;
1214

1315
void test_netns_cookie(void)
1416
{
17+
LIBBPF_OPTS(bpf_prog_attach_opts, opta);
18+
LIBBPF_OPTS(bpf_prog_detach_opts, optd);
1519
int server_fd = -1, client_fd = -1, cgroup_fd = -1;
16-
int err, val, ret, map, verdict;
20+
int err, val, ret, map, verdict, tc_fd;
1721
struct netns_cookie_prog *skel;
1822
uint64_t cookie_expected_value;
1923
socklen_t vallen = sizeof(cookie_expected_value);
@@ -38,36 +42,47 @@ void test_netns_cookie(void)
3842
if (!ASSERT_OK(err, "prog_attach"))
3943
goto done;
4044

45+
tc_fd = bpf_program__fd(skel->progs.get_netns_cookie_tcx);
46+
err = bpf_prog_attach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &opta);
47+
if (!ASSERT_OK(err, "prog_attach"))
48+
goto done;
49+
4150
server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
4251
if (CHECK(server_fd < 0, "start_server", "errno %d\n", errno))
43-
goto done;
52+
goto cleanup_tc;
4453

4554
client_fd = connect_to_fd(server_fd, 0);
4655
if (CHECK(client_fd < 0, "connect_to_fd", "errno %d\n", errno))
47-
goto done;
56+
goto cleanup_tc;
4857

4958
ret = send(client_fd, send_msg, sizeof(send_msg), 0);
5059
if (CHECK(ret != sizeof(send_msg), "send(msg)", "ret:%d\n", ret))
51-
goto done;
60+
goto cleanup_tc;
5261

5362
err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sockops_netns_cookies),
5463
&client_fd, &val);
5564
if (!ASSERT_OK(err, "map_lookup(sockops_netns_cookies)"))
56-
goto done;
65+
goto cleanup_tc;
5766

5867
err = getsockopt(client_fd, SOL_SOCKET, SO_NETNS_COOKIE,
5968
&cookie_expected_value, &vallen);
6069
if (!ASSERT_OK(err, "getsockopt"))
61-
goto done;
70+
goto cleanup_tc;
6271

6372
ASSERT_EQ(val, cookie_expected_value, "cookie_value");
6473

6574
err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sk_msg_netns_cookies),
6675
&client_fd, &val);
6776
if (!ASSERT_OK(err, "map_lookup(sk_msg_netns_cookies)"))
68-
goto done;
77+
goto cleanup_tc;
6978

7079
ASSERT_EQ(val, cookie_expected_value, "cookie_value");
80+
ASSERT_EQ(skel->bss->tcx_init_netns_cookie, cookie_expected_value, "cookie_value");
81+
ASSERT_EQ(skel->bss->tcx_netns_cookie, cookie_expected_value, "cookie_value");
82+
83+
cleanup_tc:
84+
err = bpf_prog_detach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &optd);
85+
ASSERT_OK(err, "prog_detach");
7186

7287
done:
7388
if (server_fd != -1)

tools/testing/selftests/bpf/progs/netns_cookie_prog.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct {
2727
__type(value, __u64);
2828
} sock_map SEC(".maps");
2929

30+
int tcx_init_netns_cookie, tcx_netns_cookie;
31+
3032
SEC("sockops")
3133
int get_netns_cookie_sockops(struct bpf_sock_ops *ctx)
3234
{
@@ -81,4 +83,12 @@ int get_netns_cookie_sk_msg(struct sk_msg_md *msg)
8183
return 1;
8284
}
8385

86+
SEC("tcx/ingress")
87+
int get_netns_cookie_tcx(struct __sk_buff *skb)
88+
{
89+
tcx_init_netns_cookie = bpf_get_netns_cookie(NULL);
90+
tcx_netns_cookie = bpf_get_netns_cookie(skb);
91+
return TCX_PASS;
92+
}
93+
8494
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)