Skip to content

Commit a4ae5c3

Browse files
TropicaoMartin KaFai Lau
authored andcommitted
selftests/bpf: convert get_current_cgroup_id_user to test_progs
get_current_cgroup_id_user allows testing for bpf_get_current_cgroup_id() bpf API but is not integrated into test_progs, and so is not tested automatically in CI. Convert it to the test_progs framework to allow running it automatically. The most notable differences with the old test are the following: - the new test relies on autoattach instead of manually hooking/enabling the targeted tracepoint through perf_event, which reduces quite a lot the test code size - it also accesses bpf prog data through global variables instead of maps - sleep duration passed to nanosleep syscall has been reduced to its minimum to not impact overall CI duration (we only care about the syscall being properly triggered, not about the passed duration) Signed-off-by: Alexis Lothoré (eBPF Foundation) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent 4a4c013 commit a4ae5c3

File tree

5 files changed

+51
-176
lines changed

5 files changed

+51
-176
lines changed

tools/testing/selftests/bpf/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ test_sock
1919
urandom_read
2020
test_sockmap
2121
test_lirc_mode2_user
22-
get_cgroup_id_user
2322
test_skb_cgroup_id_user
2423
test_cgroup_storage
2524
test_flow_dissector

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ endif
6767

6868
# Order correspond to 'make run_tests' order
6969
TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
70-
test_sock test_sockmap get_cgroup_id_user \
70+
test_sock test_sockmap \
7171
test_cgroup_storage \
7272
test_tcpnotify_user test_sysctl \
7373
test_progs-no_alu32
@@ -295,7 +295,6 @@ $(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
295295
$(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS)
296296
$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS)
297297
$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS)
298-
$(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
299298
$(OUTPUT)/test_cgroup_storage: $(CGROUP_HELPERS) $(TESTING_HELPERS)
300299
$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS)
301300
$(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS)

tools/testing/selftests/bpf/get_cgroup_id_user.c

Lines changed: 0 additions & 151 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <sys/stat.h>
4+
#include <sys/sysmacros.h>
5+
#include "test_progs.h"
6+
#include "cgroup_helpers.h"
7+
#include "get_cgroup_id_kern.skel.h"
8+
9+
#define TEST_CGROUP "/test-bpf-get-cgroup-id/"
10+
11+
void test_cgroup_get_current_cgroup_id(void)
12+
{
13+
struct get_cgroup_id_kern *skel;
14+
const struct timespec req = {
15+
.tv_sec = 0,
16+
.tv_nsec = 1,
17+
};
18+
int cgroup_fd;
19+
__u64 ucgid;
20+
21+
cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
22+
if (!ASSERT_OK_FD(cgroup_fd, "cgroup switch"))
23+
return;
24+
25+
skel = get_cgroup_id_kern__open_and_load();
26+
if (!ASSERT_OK_PTR(skel, "load program"))
27+
goto cleanup_cgroup;
28+
29+
if (!ASSERT_OK(get_cgroup_id_kern__attach(skel), "attach bpf program"))
30+
goto cleanup_progs;
31+
32+
skel->bss->expected_pid = getpid();
33+
/* trigger the syscall on which is attached the tested prog */
34+
if (!ASSERT_OK(syscall(__NR_nanosleep, &req, NULL), "nanosleep"))
35+
goto cleanup_progs;
36+
37+
ucgid = get_cgroup_id(TEST_CGROUP);
38+
39+
ASSERT_EQ(skel->bss->cg_id, ucgid, "compare cgroup ids");
40+
41+
cleanup_progs:
42+
get_cgroup_id_kern__destroy(skel);
43+
cleanup_cgroup:
44+
close(cgroup_fd);
45+
cleanup_cgroup_environment();
46+
}

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

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,16 @@
44
#include <linux/bpf.h>
55
#include <bpf/bpf_helpers.h>
66

7-
struct {
8-
__uint(type, BPF_MAP_TYPE_ARRAY);
9-
__uint(max_entries, 1);
10-
__type(key, __u32);
11-
__type(value, __u64);
12-
} cg_ids SEC(".maps");
13-
14-
struct {
15-
__uint(type, BPF_MAP_TYPE_ARRAY);
16-
__uint(max_entries, 1);
17-
__type(key, __u32);
18-
__type(value, __u32);
19-
} pidmap SEC(".maps");
7+
__u64 cg_id;
8+
__u64 expected_pid;
209

2110
SEC("tracepoint/syscalls/sys_enter_nanosleep")
2211
int trace(void *ctx)
2312
{
2413
__u32 pid = bpf_get_current_pid_tgid();
25-
__u32 key = 0, *expected_pid;
26-
__u64 *val;
27-
28-
expected_pid = bpf_map_lookup_elem(&pidmap, &key);
29-
if (!expected_pid || *expected_pid != pid)
30-
return 0;
3114

32-
val = bpf_map_lookup_elem(&cg_ids, &key);
33-
if (val)
34-
*val = bpf_get_current_cgroup_id();
15+
if (expected_pid == pid)
16+
cg_id = bpf_get_current_cgroup_id();
3517

3618
return 0;
3719
}

0 commit comments

Comments
 (0)