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

selftests/bpf: Add a sk_msg prog bpf_get_ns_current_pid_tgid() test

Add a sk_msg bpf program test where the program is running in a pid
namespace. The test is successful:
#165/4 ns_current_pid_tgid/new_ns_sk_msg:OK

Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20240315184915.2976718-1-yonghong.song@linux.dev

authored by

Yonghong Song and committed by
Andrii Nakryiko
4c195ee4 87ade6cd

+76
+62
tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
··· 119 119 return ret; 120 120 } 121 121 122 + static int test_current_pid_tgid_sk_msg(void *args) 123 + { 124 + int verdict, map, server_fd = -1, client_fd = -1; 125 + struct test_ns_current_pid_tgid__bss *bss; 126 + static const char send_msg[] = "message"; 127 + struct test_ns_current_pid_tgid *skel; 128 + int ret = -1, err, key = 0; 129 + pid_t tgid, pid; 130 + 131 + skel = test_ns_current_pid_tgid__open(); 132 + if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open")) 133 + return ret; 134 + 135 + bpf_program__set_autoload(skel->progs.sk_msg, true); 136 + 137 + err = test_ns_current_pid_tgid__load(skel); 138 + if (!ASSERT_OK(err, "test_ns_current_pid_tgid__load")) 139 + goto cleanup; 140 + 141 + bss = skel->bss; 142 + if (get_pid_tgid(&pid, &tgid, skel->bss)) 143 + goto cleanup; 144 + 145 + verdict = bpf_program__fd(skel->progs.sk_msg); 146 + map = bpf_map__fd(skel->maps.sock_map); 147 + err = bpf_prog_attach(verdict, map, BPF_SK_MSG_VERDICT, 0); 148 + if (!ASSERT_OK(err, "prog_attach")) 149 + goto cleanup; 150 + 151 + server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0); 152 + if (!ASSERT_GE(server_fd, 0, "start_server")) 153 + goto cleanup; 154 + 155 + client_fd = connect_to_fd(server_fd, 0); 156 + if (!ASSERT_GE(client_fd, 0, "connect_to_fd")) 157 + goto cleanup; 158 + 159 + err = bpf_map_update_elem(map, &key, &client_fd, BPF_ANY); 160 + if (!ASSERT_OK(err, "bpf_map_update_elem")) 161 + goto cleanup; 162 + 163 + err = send(client_fd, send_msg, sizeof(send_msg), 0); 164 + if (!ASSERT_EQ(err, sizeof(send_msg), "send(msg)")) 165 + goto cleanup; 166 + 167 + if (!ASSERT_EQ(bss->user_pid, pid, "pid")) 168 + goto cleanup; 169 + if (!ASSERT_EQ(bss->user_tgid, tgid, "tgid")) 170 + goto cleanup; 171 + ret = 0; 172 + 173 + cleanup: 174 + if (server_fd >= 0) 175 + close(server_fd); 176 + if (client_fd >= 0) 177 + close(client_fd); 178 + test_ns_current_pid_tgid__destroy(skel); 179 + return ret; 180 + } 181 + 122 182 static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg) 123 183 { 124 184 int wstatus; ··· 235 175 close(cgroup_fd); 236 176 } 237 177 } 178 + if (test__start_subtest("new_ns_sk_msg")) 179 + test_in_netns(test_current_pid_tgid_sk_msg, NULL); 238 180 }
+14
tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
··· 5 5 #include <stdint.h> 6 6 #include <bpf/bpf_helpers.h> 7 7 8 + struct { 9 + __uint(type, BPF_MAP_TYPE_SOCKMAP); 10 + __uint(max_entries, 2); 11 + __type(key, __u32); 12 + __type(value, __u32); 13 + } sock_map SEC(".maps"); 14 + 8 15 __u64 user_pid = 0; 9 16 __u64 user_tgid = 0; 10 17 __u64 dev = 0; ··· 40 33 { 41 34 get_pid_tgid(); 42 35 return 1; 36 + } 37 + 38 + SEC("?sk_msg") 39 + int sk_msg(struct sk_msg_md *msg) 40 + { 41 + get_pid_tgid(); 42 + return SK_PASS; 43 43 } 44 44 45 45 char _license[] SEC("license") = "GPL";