fold test_current_pid_tgid_new_ns into into test_progs
Subsystems (5)
PATCH v2 1/1 fold test_current_pid_tgid_new_ns into into test_progs
folds tests from test_current_pid_tgid_new_ns into test_progs.
--- tools/testing/selftests/bpf/Makefile | 3 +- .../bpf/prog_tests/ns_current_pid_tgid.c | 112 +++++++++++- .../bpf/test_current_pid_tgid_new_ns.c | 159 ------------------ 3 files changed, 112 insertions(+), 162 deletions(-) delete mode 100644 tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c
tools/testing/selftests/bpf/Makefile +1 -2
@@ -36,8 +36,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_cgroup_storage \ test_netcnt test_tcpnotify_user test_sock_fields test_sysctl \ - test_progs-no_alu32 \ - test_current_pid_tgid_new_ns + test_progs-no_alu32 # Also test bpf-gcc, if present ifneq ($(BPF_GCC),)
tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c +111 -1
131 lines not on this page · raw message
tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c deleted +0 -159
159 lines not on this page · raw message
Signature · 1 line
2.20.1
Quoted 16 lines · Carlos Neira
folds tests from
test_current_pid_tgid_new_nsintotest_progs.Signed-off-by Carlos Neira <[email protected]>--- tools/testing/selftests/bpf/Makefile | 3 +- .../bpf/prog_tests/ns_current_pid_tgid.c | 112 +++++++++++- .../bpf/test_current_pid_tgid_new_ns.c | 159 ------------------ 3 files changed, 112 insertions(+), 162 deletions(-) delete mode 100644 tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.ctools/testing/selftests/bpf/Makefile +0
-0
tools/testing/selftests/bpf/Makefile TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test · line 39
Quoted 6 lines
tools/testing/selftests/bpf/Makefile +1
-2@@ -36,8 +36,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_cgroup_storage \ test_netcnt test_tcpnotify_user test_sock_fields test_sysctl \ - test_progs-no_alu32 \ - test_current_pid_tgid_new_ns + test_progs-no_alu32
Please update .gitignore as well.
Quoted 3 lines
tools/testing/selftests/bpf/Makefile +0
-0# Also test bpf-gcc, if present ifneq ($(BPF_GCC),)
[...]
tools/testing/selftests/bpf/Makefile
Quoted 10 lines
tools/testing/selftests/bpf/Makefile +10
-0+ + snprintf(nspath, sizeof(nspath) - 1, "/proc/%d/ns/pid", ppid); + pidns_fd = open(nspath, O_RDONLY); + + if (CHECK(unshare(CLONE_NEWPID), + "unshare CLONE_NEWPID", + "error: %s\n", strerror(errno))) + return; + + pid = vfork();
is vfork necessary()? Maybe just stick to fork(), as in original implementation?
tools/testing/selftests/bpf/Makefile
Quoted 6 lines
tools/testing/selftests/bpf/Makefile +6
-0+ if (CHECK(pid < 0, "ns_current_pid_tgid_new_ns", "vfork error: %s\n", + strerror(errno))) { + return; + } + if (pid > 0) { + printf("waiting pid is %u\n", pid);
indentation off?
tools/testing/selftests/bpf/Makefile
Quoted 2 lines
tools/testing/selftests/bpf/Makefile +2
-0+ usleep(5); + wait(NULL);
waitpid() for specific child would be more reliable, no?
tools/testing/selftests/bpf/Makefile
Quoted 2 lines
tools/testing/selftests/bpf/Makefile +2
-0+ return; + } else {
what if fork failed?
Quoted 11 lines
tools/testing/selftests/bpf/Makefile +11
-0+ const char *probe_name = "raw_tracepoint/sys_enter"; + const char *file = "test_ns_current_pid_tgid.o"; + int err, key = 0, duration = 0; + struct bpf_link *link = NULL; + struct bpf_program *prog; + struct bpf_map *bss_map; + struct bpf_object *obj; + struct bss bss; + struct stat st; + __u64 id; +
[...]
tools/testing/selftests/bpf/Makefile
Quoted 7 lines
tools/testing/selftests/bpf/Makefile +7
-0+ err = bpf_map_lookup_elem(bpf_map__fd(bss_map), &key, &bss); + if (CHECK(err, "set_bss", "failed to get bss : %d\n", err)) + goto cleanup; + + if (CHECK(id != bss.pid_tgid, "Compare user pid/tgid vs bpf pid/tgid", + "User pid/tgid %llu BPF pid/tgid %llu\n", id, bss.pid_tgid)) + goto cleanup;
Good half of all this code could be removed if you used BPF skeleton, see other tests utilizing *.skel.h for inspiration.
Quoted 14 lines
tools/testing/selftests/bpf/Makefile +14
-0+cleanup: + setns(pidns_fd, CLONE_NEWPID); + bpf_link__destroy(link); + bpf_object__close(obj); + } +} + +void test_ns_current_pid_tgid(void) +{ + if (test__start_subtest("ns_current_pid_tgid_global_ns")) + test_ns_current_pid_tgid_global_ns(); + if (test__start_subtest("ns_current_pid_tgid_new_ns")) + test_ns_current_pid_tgid_new_ns(); +}
[...]
Quoted 90 lines · Andrii Nakryiko
Quoted 16 lines · Carlos Neira
folds tests from
test_current_pid_tgid_new_nsintotest_progs.Signed-off-by Carlos Neira <[email protected]>--- tools/testing/selftests/bpf/Makefile | 3 +- .../bpf/prog_tests/ns_current_pid_tgid.c | 112 +++++++++++- .../bpf/test_current_pid_tgid_new_ns.c | 159 ------------------ 3 files changed, 112 insertions(+), 162 deletions(-) delete mode 100644 tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.ctools/testing/selftests/bpf/Makefile +0
-0tools/testing/selftests/bpf/Makefile TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test · line 39
Quoted 6 lines
tools/testing/selftests/bpf/Makefile +1
-2@@ -36,8 +36,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_cgroup_storage \ test_netcnt test_tcpnotify_user test_sock_fields test_sysctl \ - test_progs-no_alu32 \ - test_current_pid_tgid_new_ns + test_progs-no_alu32Please update .gitignore as well.
Quoted 3 lines
tools/testing/selftests/bpf/Makefile +0
-0# Also test bpf-gcc, if present ifneq ($(BPF_GCC),)[...]
tools/testing/selftests/bpf/Makefile
Quoted 10 lines
tools/testing/selftests/bpf/Makefile +10
-0+ + snprintf(nspath, sizeof(nspath) - 1, "/proc/%d/ns/pid", ppid); + pidns_fd = open(nspath, O_RDONLY); + + if (CHECK(unshare(CLONE_NEWPID), + "unshare CLONE_NEWPID", + "error: %s\n", strerror(errno))) + return; + + pid = vfork();is vfork
necessary()? Maybe just stick tofork(), as in original implementation?tools/testing/selftests/bpf/Makefile
Quoted 6 lines
tools/testing/selftests/bpf/Makefile +6
-0+ if (CHECK(pid < 0, "ns_current_pid_tgid_new_ns", "vfork error: %s\n", + strerror(errno))) { + return; + } + if (pid > 0) { + printf("waiting pid is %u\n", pid);indentation off?
tools/testing/selftests/bpf/Makefile
Quoted 2 lines
tools/testing/selftests/bpf/Makefile +2
-0+ usleep(5); + wait(NULL);
waitpid()for specific child would be more reliable, no?tools/testing/selftests/bpf/Makefile
Quoted 2 lines
tools/testing/selftests/bpf/Makefile +2
-0+ return; + } else {what if fork failed?
Quoted 11 lines
tools/testing/selftests/bpf/Makefile +11
-0+ const char *probe_name = "raw_tracepoint/sys_enter"; + const char *file = "test_ns_current_pid_tgid.o"; + int err, key = 0, duration = 0; + struct bpf_link *link = NULL; + struct bpf_program *prog; + struct bpf_map *bss_map; + struct bpf_object *obj; + struct bss bss; + struct stat st; + __u64 id; +[...]
tools/testing/selftests/bpf/Makefile
Quoted 7 lines
tools/testing/selftests/bpf/Makefile +7
-0+ err = bpf_map_lookup_elem(bpf_map__fd(bss_map), &key, &bss); + if (CHECK(err, "set_bss", "failed to get bss : %d\n", err)) + goto cleanup; + + if (CHECK(id != bss.pid_tgid, "Compare user pid/tgid vs bpf pid/tgid", + "User pid/tgid %llu BPF pid/tgid %llu\n", id, bss.pid_tgid)) + goto cleanup;Good half of all this code could be removed if you used BPF skeleton, see other tests utilizing *.skel.h for inspiration.
Quoted 16 lines
Quoted 14 lines
tools/testing/selftests/bpf/Makefile +14
-0+cleanup: + setns(pidns_fd, CLONE_NEWPID); + bpf_link__destroy(link); + bpf_object__close(obj); + } +} + +void test_ns_current_pid_tgid(void) +{ + if (test__start_subtest("ns_current_pid_tgid_global_ns")) + test_ns_current_pid_tgid_global_ns(); + if (test__start_subtest("ns_current_pid_tgid_new_ns")) + test_ns_current_pid_tgid_new_ns(); +}[...]
Thanks!.
I'll include your feedback on the next version, thanks for reviewing this.
Bests