Skip to content

Commit 434bb9d

Browse files
Yonghong SongKernel Patches Daemon
Yonghong Song
authored and
Kernel Patches Daemon
committed
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 <[email protected]>
1 parent 1814aab commit 434bb9d

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,66 @@ static int test_current_pid_tgid_cgrp(void *args)
119119
return ret;
120120
}
121121

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+
122182
static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg)
123183
{
124184
int wstatus;
@@ -156,4 +216,6 @@ void serial_test_ns_current_pid_tgid(void)
156216
close(cgroup_fd);
157217
}
158218
}
219+
if (test__start_subtest("new_ns_sk_msg"))
220+
test_ns_current_pid_tgid_new_ns(test_current_pid_tgid_sk_msg, NULL);
159221
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
#include <stdint.h>
66
#include <bpf/bpf_helpers.h>
77

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+
815
__u64 user_pid = 0;
916
__u64 user_tgid = 0;
1017
__u64 dev = 0;
@@ -35,4 +42,11 @@ int cgroup_bind4(struct bpf_sock_addr *ctx)
3542
return 1;
3643
}
3744

45+
SEC("?sk_msg")
46+
int sk_msg(struct sk_msg_md *msg)
47+
{
48+
get_pid_tgid();
49+
return SK_PASS;
50+
}
51+
3852
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)