Skip to content

Commit 6a2a4ab

Browse files
Yonghong SongKernel Patches Daemon
Yonghong Song
authored and
Kernel Patches Daemon
committed
selftests/bpf: Add a cgroup prog bpf_get_ns_current_pid_tgid() test
Add a cgroup bpf program test where the bpf program is running in a pid namespace. The test is successfully: #165/3 ns_current_pid_tgid/new_ns_cgrp:OK Signed-off-by: Yonghong Song <[email protected]>
1 parent 068499a commit 6a2a4ab

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <sys/wait.h>
1313
#include <sys/mount.h>
1414
#include <sys/fcntl.h>
15+
#include "network_helpers.h"
1516

1617
#define STACK_SIZE (1024 * 1024)
1718
static char child_stack[STACK_SIZE];
@@ -74,6 +75,50 @@ static int test_current_pid_tgid_tp(void *args)
7475
return ret;
7576
}
7677

78+
static int test_current_pid_tgid_cgrp(void *args)
79+
{
80+
struct test_ns_current_pid_tgid__bss *bss;
81+
struct test_ns_current_pid_tgid *skel;
82+
int server_fd = -1, ret = -1, err;
83+
int cgroup_fd = *(int *)args;
84+
pid_t tgid, pid;
85+
86+
skel = test_ns_current_pid_tgid__open();
87+
if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open"))
88+
return ret;
89+
90+
bpf_program__set_autoload(skel->progs.cgroup_bind4, true);
91+
92+
err = test_ns_current_pid_tgid__load(skel);
93+
if (!ASSERT_OK(err, "test_ns_current_pid_tgid__load"))
94+
goto cleanup;
95+
96+
bss = skel->bss;
97+
if (get_pid_tgid(&pid, &tgid, bss))
98+
goto cleanup;
99+
100+
skel->links.cgroup_bind4 = bpf_program__attach_cgroup(
101+
skel->progs.cgroup_bind4, cgroup_fd);
102+
if (!ASSERT_OK_PTR(skel->links.cgroup_bind4, "bpf_program__attach_cgroup"))
103+
goto cleanup;
104+
105+
server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
106+
if (!ASSERT_GE(server_fd, 0, "start_server"))
107+
goto cleanup;
108+
109+
if (!ASSERT_EQ(bss->user_pid, pid, "pid"))
110+
goto cleanup;
111+
if (!ASSERT_EQ(bss->user_tgid, tgid, "tgid"))
112+
goto cleanup;
113+
ret = 0;
114+
115+
cleanup:
116+
if (server_fd >= 0)
117+
close(server_fd);
118+
test_ns_current_pid_tgid__destroy(skel);
119+
return ret;
120+
}
121+
77122
static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg)
78123
{
79124
int wstatus;
@@ -102,4 +147,13 @@ void serial_test_ns_current_pid_tgid(void)
102147
test_current_pid_tgid_tp(NULL);
103148
if (test__start_subtest("new_ns_tp"))
104149
test_ns_current_pid_tgid_new_ns(test_current_pid_tgid_tp, NULL);
150+
if (test__start_subtest("new_ns_cgrp")) {
151+
int cgroup_fd = -1;
152+
153+
cgroup_fd = test__join_cgroup("/sock_addr");
154+
if (ASSERT_GE(cgroup_fd, 0, "join_cgroup")) {
155+
test_ns_current_pid_tgid_new_ns(test_current_pid_tgid_cgrp, &cgroup_fd);
156+
close(cgroup_fd);
157+
}
158+
}
105159
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@ int tp_handler(const void *ctx)
2828
return 0;
2929
}
3030

31+
SEC("?cgroup/bind4")
32+
int cgroup_bind4(struct bpf_sock_addr *ctx)
33+
{
34+
get_pid_tgid();
35+
return 1;
36+
}
37+
3138
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)