Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <yonghong.song@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20240315184910.2976522-1-yonghong.song@linux.dev

authored by

Yonghong Song and committed by
Andrii Nakryiko
87ade6cd 4d4bd29e

+80
+73
tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
··· 12 12 #include <sys/wait.h> 13 13 #include <sys/mount.h> 14 14 #include <sys/fcntl.h> 15 + #include "network_helpers.h" 15 16 16 17 #define STACK_SIZE (1024 * 1024) 17 18 static char child_stack[STACK_SIZE]; ··· 75 74 return ret; 76 75 } 77 76 77 + static int test_current_pid_tgid_cgrp(void *args) 78 + { 79 + struct test_ns_current_pid_tgid__bss *bss; 80 + struct test_ns_current_pid_tgid *skel; 81 + int server_fd = -1, ret = -1, err; 82 + int cgroup_fd = *(int *)args; 83 + pid_t tgid, pid; 84 + 85 + skel = test_ns_current_pid_tgid__open(); 86 + if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open")) 87 + return ret; 88 + 89 + bpf_program__set_autoload(skel->progs.cgroup_bind4, true); 90 + 91 + err = test_ns_current_pid_tgid__load(skel); 92 + if (!ASSERT_OK(err, "test_ns_current_pid_tgid__load")) 93 + goto cleanup; 94 + 95 + bss = skel->bss; 96 + if (get_pid_tgid(&pid, &tgid, bss)) 97 + goto cleanup; 98 + 99 + skel->links.cgroup_bind4 = bpf_program__attach_cgroup( 100 + skel->progs.cgroup_bind4, cgroup_fd); 101 + if (!ASSERT_OK_PTR(skel->links.cgroup_bind4, "bpf_program__attach_cgroup")) 102 + goto cleanup; 103 + 104 + server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0); 105 + if (!ASSERT_GE(server_fd, 0, "start_server")) 106 + goto cleanup; 107 + 108 + if (!ASSERT_EQ(bss->user_pid, pid, "pid")) 109 + goto cleanup; 110 + if (!ASSERT_EQ(bss->user_tgid, tgid, "tgid")) 111 + goto cleanup; 112 + ret = 0; 113 + 114 + cleanup: 115 + if (server_fd >= 0) 116 + close(server_fd); 117 + test_ns_current_pid_tgid__destroy(skel); 118 + return ret; 119 + } 120 + 78 121 static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg) 79 122 { 80 123 int wstatus; ··· 140 95 return; 141 96 } 142 97 98 + static void test_in_netns(int (*fn)(void *), void *arg) 99 + { 100 + struct nstoken *nstoken = NULL; 101 + 102 + SYS(cleanup, "ip netns add ns_current_pid_tgid"); 103 + SYS(cleanup, "ip -net ns_current_pid_tgid link set dev lo up"); 104 + 105 + nstoken = open_netns("ns_current_pid_tgid"); 106 + if (!ASSERT_OK_PTR(nstoken, "open_netns")) 107 + goto cleanup; 108 + 109 + test_ns_current_pid_tgid_new_ns(fn, arg); 110 + 111 + cleanup: 112 + if (nstoken) 113 + close_netns(nstoken); 114 + SYS_NOFAIL("ip netns del ns_current_pid_tgid"); 115 + } 116 + 143 117 /* TODO: use a different tracepoint */ 144 118 void serial_test_ns_current_pid_tgid(void) 145 119 { ··· 166 102 test_current_pid_tgid_tp(NULL); 167 103 if (test__start_subtest("new_ns_tp")) 168 104 test_ns_current_pid_tgid_new_ns(test_current_pid_tgid_tp, NULL); 105 + if (test__start_subtest("new_ns_cgrp")) { 106 + int cgroup_fd = -1; 107 + 108 + cgroup_fd = test__join_cgroup("/sock_addr"); 109 + if (ASSERT_GE(cgroup_fd, 0, "join_cgroup")) { 110 + test_in_netns(test_current_pid_tgid_cgrp, &cgroup_fd); 111 + close(cgroup_fd); 112 + } 113 + } 169 114 }
+7
tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
··· 28 28 return 0; 29 29 } 30 30 31 + SEC("?cgroup/bind4") 32 + int cgroup_bind4(struct bpf_sock_addr *ctx) 33 + { 34 + get_pid_tgid(); 35 + return 1; 36 + } 37 + 31 38 char _license[] SEC("license") = "GPL";