Skip to content

Commit 232164e

Browse files
cneiraborkmann
authored andcommitted
bpf, selftests: Fold test_current_pid_tgid_new_ns into test_progs.
Currently tests for bpf_get_ns_current_pid_tgid() are outside test_progs. This change folds test cases into test_progs. Changes from v11: - Fixed test failure is not detected. - Removed EXIT(3) call as it will stop test_progs execution. Signed-off-by: Carlos Neira <cneirabustos@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20210114141033.GA17348@localhost
1 parent 9ab7e76 commit 232164e

File tree

5 files changed

+70
-240
lines changed

5 files changed

+70
-240
lines changed

tools/testing/selftests/bpf/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ test_tcpnotify_user
2626
test_libbpf
2727
test_tcp_check_syncookie_user
2828
test_sysctl
29-
test_current_pid_tgid_new_ns
3029
xdping
3130
test_cpp
3231
*.skel.h

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
3636
test_sock test_sockmap get_cgroup_id_user test_socket_cookie \
3737
test_cgroup_storage \
3838
test_netcnt test_tcpnotify_user test_sysctl \
39-
test_progs-no_alu32 \
40-
test_current_pid_tgid_new_ns
39+
test_progs-no_alu32
4140

4241
# Also test bpf-gcc, if present
4342
ifneq ($(BPF_GCC),)
Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,87 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright (c) 2020 Carlos Neira cneirabustos@gmail.com */
3+
4+
#define _GNU_SOURCE
35
#include <test_progs.h>
6+
#include "test_ns_current_pid_tgid.skel.h"
47
#include <sys/stat.h>
58
#include <sys/types.h>
69
#include <unistd.h>
710
#include <sys/syscall.h>
11+
#include <sched.h>
12+
#include <sys/wait.h>
13+
#include <sys/mount.h>
14+
#include <sys/fcntl.h>
815

9-
struct bss {
10-
__u64 dev;
11-
__u64 ino;
12-
__u64 pid_tgid;
13-
__u64 user_pid_tgid;
14-
};
16+
#define STACK_SIZE (1024 * 1024)
17+
static char child_stack[STACK_SIZE];
1518

16-
void test_ns_current_pid_tgid(void)
19+
static int test_current_pid_tgid(void *args)
1720
{
18-
const char *probe_name = "raw_tracepoint/sys_enter";
19-
const char *file = "test_ns_current_pid_tgid.o";
20-
int err, key = 0, duration = 0;
21-
struct bpf_link *link = NULL;
22-
struct bpf_program *prog;
23-
struct bpf_map *bss_map;
24-
struct bpf_object *obj;
25-
struct bss bss;
21+
struct test_ns_current_pid_tgid__bss *bss;
22+
struct test_ns_current_pid_tgid *skel;
23+
int err = -1, duration = 0;
24+
pid_t tgid, pid;
2625
struct stat st;
27-
__u64 id;
28-
29-
obj = bpf_object__open_file(file, NULL);
30-
if (CHECK(IS_ERR(obj), "obj_open", "err %ld\n", PTR_ERR(obj)))
31-
return;
3226

33-
err = bpf_object__load(obj);
34-
if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno))
27+
skel = test_ns_current_pid_tgid__open_and_load();
28+
if (CHECK(!skel, "skel_open_load", "failed to load skeleton\n"))
3529
goto cleanup;
3630

37-
bss_map = bpf_object__find_map_by_name(obj, "test_ns_.bss");
38-
if (CHECK(!bss_map, "find_bss_map", "failed\n"))
31+
pid = syscall(SYS_gettid);
32+
tgid = getpid();
33+
34+
err = stat("/proc/self/ns/pid", &st);
35+
if (CHECK(err, "stat", "failed /proc/self/ns/pid: %d\n", err))
3936
goto cleanup;
4037

41-
prog = bpf_object__find_program_by_title(obj, probe_name);
42-
if (CHECK(!prog, "find_prog", "prog '%s' not found\n",
43-
probe_name))
38+
bss = skel->bss;
39+
bss->dev = st.st_dev;
40+
bss->ino = st.st_ino;
41+
bss->user_pid = 0;
42+
bss->user_tgid = 0;
43+
44+
err = test_ns_current_pid_tgid__attach(skel);
45+
if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
4446
goto cleanup;
4547

46-
memset(&bss, 0, sizeof(bss));
47-
pid_t tid = syscall(SYS_gettid);
48-
pid_t pid = getpid();
48+
/* trigger tracepoint */
49+
usleep(1);
50+
ASSERT_EQ(bss->user_pid, pid, "pid");
51+
ASSERT_EQ(bss->user_tgid, tgid, "tgid");
52+
err = 0;
4953

50-
id = (__u64) tid << 32 | pid;
51-
bss.user_pid_tgid = id;
54+
cleanup:
55+
test_ns_current_pid_tgid__destroy(skel);
5256

53-
if (CHECK_FAIL(stat("/proc/self/ns/pid", &st))) {
54-
perror("Failed to stat /proc/self/ns/pid");
55-
goto cleanup;
56-
}
57+
return err;
58+
}
5759

58-
bss.dev = st.st_dev;
59-
bss.ino = st.st_ino;
60+
static void test_ns_current_pid_tgid_new_ns(void)
61+
{
62+
int wstatus, duration = 0;
63+
pid_t cpid;
6064

61-
err = bpf_map_update_elem(bpf_map__fd(bss_map), &key, &bss, 0);
62-
if (CHECK(err, "setting_bss", "failed to set bss : %d\n", err))
63-
goto cleanup;
65+
/* Create a process in a new namespace, this process
66+
* will be the init process of this new namespace hence will be pid 1.
67+
*/
68+
cpid = clone(test_current_pid_tgid, child_stack + STACK_SIZE,
69+
CLONE_NEWPID | SIGCHLD, NULL);
6470

65-
link = bpf_program__attach_raw_tracepoint(prog, "sys_enter");
66-
if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n",
67-
PTR_ERR(link))) {
68-
link = NULL;
69-
goto cleanup;
70-
}
71+
if (CHECK(cpid == -1, "clone", strerror(errno)))
72+
return;
7173

72-
/* trigger some syscalls */
73-
usleep(1);
74+
if (CHECK(waitpid(cpid, &wstatus, 0) == -1, "waitpid", strerror(errno)))
75+
return;
7476

75-
err = bpf_map_lookup_elem(bpf_map__fd(bss_map), &key, &bss);
76-
if (CHECK(err, "set_bss", "failed to get bss : %d\n", err))
77-
goto cleanup;
77+
if (CHECK(WEXITSTATUS(wstatus) != 0, "newns_pidtgid", "failed"))
78+
return;
79+
}
7880

79-
if (CHECK(id != bss.pid_tgid, "Compare user pid/tgid vs. bpf pid/tgid",
80-
"User pid/tgid %llu BPF pid/tgid %llu\n", id, bss.pid_tgid))
81-
goto cleanup;
82-
cleanup:
83-
bpf_link__destroy(link);
84-
bpf_object__close(obj);
81+
void test_ns_current_pid_tgid(void)
82+
{
83+
if (test__start_subtest("ns_current_pid_tgid_root_ns"))
84+
test_current_pid_tgid(NULL);
85+
if (test__start_subtest("ns_current_pid_tgid_new_ns"))
86+
test_ns_current_pid_tgid_new_ns();
8587
}

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,21 @@
55
#include <stdint.h>
66
#include <bpf/bpf_helpers.h>
77

8-
static volatile struct {
9-
__u64 dev;
10-
__u64 ino;
11-
__u64 pid_tgid;
12-
__u64 user_pid_tgid;
13-
} res;
8+
__u64 user_pid = 0;
9+
__u64 user_tgid = 0;
10+
__u64 dev = 0;
11+
__u64 ino = 0;
1412

15-
SEC("raw_tracepoint/sys_enter")
16-
int trace(void *ctx)
13+
SEC("tracepoint/syscalls/sys_enter_nanosleep")
14+
int handler(const void *ctx)
1715
{
18-
__u64 ns_pid_tgid, expected_pid;
1916
struct bpf_pidns_info nsdata;
20-
__u32 key = 0;
2117

22-
if (bpf_get_ns_current_pid_tgid(res.dev, res.ino, &nsdata,
23-
sizeof(struct bpf_pidns_info)))
18+
if (bpf_get_ns_current_pid_tgid(dev, ino, &nsdata, sizeof(struct bpf_pidns_info)))
2419
return 0;
2520

26-
ns_pid_tgid = (__u64)nsdata.tgid << 32 | nsdata.pid;
27-
expected_pid = res.user_pid_tgid;
28-
29-
if (expected_pid != ns_pid_tgid)
30-
return 0;
31-
32-
res.pid_tgid = ns_pid_tgid;
21+
user_pid = nsdata.pid;
22+
user_tgid = nsdata.tgid;
3323

3424
return 0;
3525
}

tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c

Lines changed: 0 additions & 160 deletions
This file was deleted.

0 commit comments

Comments
 (0)