selftests/bpf: convert three other cgroup tests to test_progs
Subsystems (3)
v4 cover selftests/bpf: convert three other cgroup tests to test_progs
Hello,
this series brings a new set of test converted to the test_progs framework. Since the tests are quite small, I chose to group three tests conversion in the same series, but feel free to let me know if I should keep one series per test. The series focuses on cgroup testing and converts the following tests:
- get_cgroup_id_user
- cgroup_storage
- test_skb_cgroup_id_user
---
Changes in v4:
- Fix test after netns addition by making sure loopack interface is up
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Fixed multiple leaks on cgroup file descriptors and sockets
- Used dedicated network namespaces for tests involving network
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Use global variables instead of maps when possible
- Collect review tags from Alan
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Alexis Lothoré (eBPF Foundation) (4):
selftests/bpf: convert get_current_cgroup_id_user to test_progs selftests/bpf: convert test_cgroup_storage to test_progs selftests/bpf: add proper section name to bpf prog and rename it selftests/bpf: convert test_skb_cgroup_id_user to test_progs
tools/testing/selftests/bpf/.gitignore | 3 - tools/testing/selftests/bpf/Makefile | 8 +- tools/testing/selftests/bpf/get_cgroup_id_user.c | 151 ----------------- .../selftests/bpf/prog_tests/cgroup_ancestor.c | 169 +++++++++++++++++++ .../bpf/prog_tests/cgroup_get_current_cgroup_id.c | 46 ++++++ .../selftests/bpf/prog_tests/cgroup_storage.c | 96 +++++++++++ ...test_skb_cgroup_id_kern.c => cgroup_ancestor.c} | 14 +- tools/testing/selftests/bpf/progs/cgroup_storage.c | 24 +++ .../selftests/bpf/progs/get_cgroup_id_kern.c | 26 +-- tools/testing/selftests/bpf/test_cgroup_storage.c | 174 -------------------- tools/testing/selftests/bpf/test_skb_cgroup_id.sh | 63 ------- .../selftests/bpf/test_skb_cgroup_id_user.c | 183 --------------------- 12 files changed, 344 insertions(+), 613 deletions(-)
---
base-commit: ab2c4aa104050a184c3411a973b165285549f732
change-id: 20240725-convert_cgroup_tests-d07c66053225
Best regards,
Signature · 3 lines
Alexis Lothoré, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
PATCH v4 1/4 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)
---
Changes in v3:
- close leaking cgroup file descriptor
Changes in v2:
- use global variables instead of maps
The new test_progs part has been tested in a local qemu environment:
./test_progs -a cgroup_get_current_cgroup_id
47 cgroup_get_current_cgroup_id:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
--- tools/testing/selftests/bpf/.gitignore | 1 - tools/testing/selftests/bpf/Makefile | 3 +- tools/testing/selftests/bpf/get_cgroup_id_user.c | 151 --------------------- .../bpf/prog_tests/cgroup_get_current_cgroup_id.c | 46 +++++++ .../selftests/bpf/progs/get_cgroup_id_kern.c | 26 +--- 5 files changed, 51 insertions(+), 176 deletions(-)
tools/testing/selftests/bpf/.gitignore +0 -1
@@ -19,7 +19,6 @@ test_sock urandom_read test_sockmap test_lirc_mode2_user -get_cgroup_id_user test_skb_cgroup_id_user test_cgroup_storage test_flow_dissector
tools/testing/selftests/bpf/Makefile +1 -2
@@ -67,7 +67,7 @@ endif # Order correspond to 'make run_tests' order TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ - test_sock test_sockmap get_cgroup_id_user \ + test_sock test_sockmap \ test_cgroup_storage \ test_tcpnotify_user test_sysctl \ test_progs-no_alu32
@@ -295,7 +295,6 @@ $(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS) -$(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_cgroup_storage: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS)
tools/testing/selftests/bpf/get_cgroup_id_user.c deleted +0 -151
151 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/cgroup_get_current_cgroup_id.c new file +46 -0
46 lines not on this page · raw message
tools/testing/selftests/bpf/progs/get_cgroup_id_kern.c +4 -22
@@ -4,34 +4,16 @@#include <linux/bpf.h> #include <bpf/bpf_helpers.h> -struct { - __uint(type, BPF_MAP_TYPE_ARRAY); - __uint(max_entries, 1); - __type(key, __u32); - __type(value, __u64); -} cg_ids SEC(".maps"); - -struct { - __uint(type, BPF_MAP_TYPE_ARRAY); - __uint(max_entries, 1); - __type(key, __u32); - __type(value, __u32); -} pidmap SEC(".maps"); +__u64 cg_id; +__u64 expected_pid; SEC("tracepoint/syscalls/sys_enter_nanosleep")int trace (void *ctx) {__u32 pid =bpf_get_current_pid_tgid (); -__u32 key =0 , *expected_pid; -__u64 *val; - - expected_pid =bpf_map_lookup_elem (&pidmap, &key); -if (!expected_pid || *expected_pid != pid) -return 0 ; - val =bpf_map_lookup_elem (&cg_ids, &key); -if (val) - *val =bpf_get_current_cgroup_id (); +if (expected_pid == pid) + cg_id =bpf_get_current_cgroup_id ();return 0 ; }
Signature · 1 line
2.46.0
PATCH v4 2/4 selftests/bpf: convert test_cgroup_storage to test_progs
In reply to Alexis Lothoré (eBPF Foundation)test_cgroup_storage is currently a standalone program which is not run when executing test_progs.
Convert it to the test_progs framework so it can be automatically executed in CI. The conversion led to the following changes:
- converted the raw bpf program in the userspace test file into a dedicated test program in progs/ dir
- reduced the scope of cgroup_storage test: the content from this test overlaps with some other tests already present in test_progs, most notably netcnt and cgroup_storage_multi*. Those tests already check extensively local storage, per-cpu local storage, cgroups interaction, etc. So the new test only keep the part testing that the program return code (based on map content) properly leads to packet being passed or dropped.
---
Changes in v4:
- fix test failure by bringing up loopback netif on remote netns
Changes in v3:
- close leaking cgroup file descriptor
- run test in dedicated network namespace
Tested in a local qemu environment:
./test_progs -a cgroup_storage
53 cgroup_storage:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
--- tools/testing/selftests/bpf/.gitignore | 1 - tools/testing/selftests/bpf/Makefile | 2 - .../selftests/bpf/prog_tests/cgroup_storage.c | 96 ++++++++++++ tools/testing/selftests/bpf/progs/cgroup_storage.c | 24 +++ tools/testing/selftests/bpf/test_cgroup_storage.c | 174 --------------------- 5 files changed, 120 insertions(+), 177 deletions(-)
tools/testing/selftests/bpf/.gitignore +0 -1
@@ -20,7 +20,6 @@ urandom_read test_sockmap test_lirc_mode2_user test_skb_cgroup_id_user -test_cgroup_storage test_flow_dissector flow_dissector_load test_tcpnotify_user
tools/testing/selftests/bpf/Makefile +0 -2
@@ -68,7 +68,6 @@ endif # Order correspond to 'make run_tests' order TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ test_sock test_sockmap \ - test_cgroup_storage \ test_tcpnotify_user test_sysctl \ test_progs-no_alu32 TEST_INST_SUBDIRS := no_alu32
@@ -295,7 +294,6 @@ $(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS) -$(OUTPUT)/test_cgroup_storage: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_tag: $(TESTING_HELPERS)
tools/testing/selftests/bpf/prog_tests/cgroup_storage.c new file +96 -0
96 lines not on this page · raw message
tools/testing/selftests/bpf/progs/cgroup_storage.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +struct { + __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE); + __type(key, struct bpf_cgroup_storage_key); + __type(value, __u64); +} cgroup_storage SEC(".maps"); + +SEC("cgroup_skb/egress") +int bpf_prog (struct __sk_buff *skb) +{ +__u64 *counter; + + counter =bpf_get_local_storage (&cgroup_storage,0 ); +__sync_fetch_and_add (counter,1 ); + +/* Drop one out of every two packets */ +return (*counter &1 ); +} + +char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_cgroup_storage.c deleted +0 -174
174 lines not on this page · raw message
Signature · 1 line
2.46.0
PATCH v4 3/4 selftests/bpf: add proper section name to bpf prog and rename it
In reply to Alexis Lothoré (eBPF Foundation)test_skb_cgroup_id_kern.c is currently involved in a manual test. In its current form, it can not be used with the auto-generated skeleton APIs, because the section name is not valid to allow libbpf to deduce the program type.
Update section name to allow skeleton APIs usage. Also rename the program name to make it shorter and more straighforward regarding the API it is testing. While doing so, make sure that test_skb_cgroup_id.sh passes to get a working reference before converting it to test_progs
- update the obj name
- fix loading issue (verifier rejecting the program when loaded through tc, because of map not found), by preloading the whole obj with bpftool
---
.../progs/{test_skb_cgroup_id_kern.c => cgroup_ancestor.c} | 2 +-
tools/testing/selftests/bpf/test_skb_cgroup_id.sh | 12 ++++++++----
2 files changed, 9 insertions(+), 5 deletions(-)tools/testing/selftests/bpf/progs/cgroup_ancestor.c renamed from tools/testing/selftests/bpf/progs/test_skb_cgroup_id_kern.c +1 -1
@@ -28,7 +28,7 @@ static __always_inline void log_nth_level(struct __sk_buff *skb, __u32 level)bpf_map_update_elem (&cgroup_ids, &level, &id,0 ); } -SEC("cgroup_id_logger") +SEC("tc")int log_cgroup_id (struct __sk_buff *skb) {/* Loop unroll can't be used here due to [1]. Unrolling manually.
tools/testing/selftests/bpf/test_skb_cgroup_id.sh +8 -4
@@ -30,8 +30,10 @@ setup() wait_for_ip tc qdisc add dev ${TEST_IF} clsact - tc filter add dev ${TEST_IF} egress bpf obj ${BPF_PROG_OBJ} \ - sec ${BPF_PROG_SECTION} da + mkdir -p /sys/fs/bpf/${BPF_PROG_PIN} + bpftool prog loadall ${BPF_PROG_OBJ} /sys/fs/bpf/${BPF_PROG_PIN} type tc + tc filter add dev ${TEST_IF} egress bpf da object-pinned \ + /sys/fs/bpf/${BPF_PROG_PIN}/${BPF_PROG_NAME} BPF_PROG_ID=$(tc filter show dev ${TEST_IF} egress | \ awk '/ id / {sub(/.* id /, "", $0); print($1)}')
@@ -41,6 +43,7 @@ cleanup() { ip link del ${TEST_IF} 2>/dev/null || : ip link del ${TEST_IF_PEER} 2>/dev/null || : + rm -rf /sys/fs/bpf/${BPF_PROG_PIN} } main()
@@ -54,8 +57,9 @@ DIR=$(dirname $0) TEST_IF="test_cgid_1" TEST_IF_PEER="test_cgid_2" MAX_PING_TRIES=5 -BPF_PROG_OBJ="${DIR}/test_skb_cgroup_id_kern.bpf.o" -BPF_PROG_SECTION="cgroup_id_logger" +BPF_PROG_PIN="cgroup_ancestor" +BPF_PROG_OBJ="${DIR}/${BPF_PROG_PIN}.bpf.o" +BPF_PROG_NAME="log_cgroup_id" BPF_PROG_ID=0 PROG="${DIR}/test_skb_cgroup_id_user" type ping6 >/dev/null 2>&1 && PING6="ping6" || PING6="ping -6"
Signature · 1 line
2.46.0
PATCH v4 4/4 selftests/bpf: convert test_skb_cgroup_id_user to test_progs
In reply to Alexis Lothoré (eBPF Foundation)test_skb_cgroup_id_user allows testing skb cgroup id retrieval at different levels, but is not integrated in test_progs, so it is not run automatically in CI. The test overlaps a bit with cgroup_skb_sk_lookup_kern, which is integrated in test_progs and test extensively skb cgroup helpers, but there is still one major difference between the two tests which justifies the conversion:cgroup_skb_sk_lookup_kern deals with a BPF_PROG_TYPE_CGROUP_SKB (attached on a cgroup), while test_skb_cgroup_id_user deals with a BPF_PROG_TYPE_SCHED_CLS (attached on a qdisc)
Convert test_skb_cgroup_id_user into test_progs framework in order to run it automatically in CI. The main differences with the original test are the following:
- rename the test to make it shorter and more straightforward regarding tested feature
- the wrapping shell script has been dropped since every setup step is now handled in the main C test file
- the test has been renamed for a shorter name and reflecting the tested API
- add dedicated assert log per level to ease test failure debugging
- use global variables instead of maps to access bpf prog data
---
Changes in v3:
- close leaking socket
- close leaking cgroup file descriptor
- run test in dedicated network namespace
Changes in v2:
- use global variables instead of maps
The new test has been tested in a qemu environment:
./test_progs -a cgroup_ancestor
44 cgroup_ancestor:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
--- tools/testing/selftests/bpf/.gitignore | 1 - tools/testing/selftests/bpf/Makefile | 3 +- .../selftests/bpf/prog_tests/cgroup_ancestor.c | 169 +++++++++++++++++++ .../testing/selftests/bpf/progs/cgroup_ancestor.c | 12 +- tools/testing/selftests/bpf/test_skb_cgroup_id.sh | 67 -------- .../selftests/bpf/test_skb_cgroup_id_user.c | 183 --------------------- 6 files changed, 172 insertions(+), 263 deletions(-)
tools/testing/selftests/bpf/.gitignore +0 -1
@@ -19,7 +19,6 @@ test_sock urandom_read test_sockmap test_lirc_mode2_user -test_skb_cgroup_id_user test_flow_dissector flow_dissector_load test_tcpnotify_user
tools/testing/selftests/bpf/Makefile +1 -2
@@ -137,7 +137,7 @@ TEST_PROGS_EXTENDED := with_addr.sh \ test_xdp_vlan.sh test_bpftool.py # Compile but not part of 'make run_tests' -TEST_GEN_PROGS_EXTENDED = test_skb_cgroup_id_user \ +TEST_GEN_PROGS_EXTENDED = \ flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \ xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \
@@ -290,7 +290,6 @@ JSON_WRITER := $(OUTPUT)/json_writer.o CAP_HELPERS := $(OUTPUT)/cap_helpers.o NETWORK_HELPERS := $(OUTPUT)/network_helpers.o -$(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS)
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c new file +169 -0
169 lines not on this page · raw message
tools/testing/selftests/bpf/progs/cgroup_ancestor.c +2 -10
@@ -10,22 +10,14 @@#define NUM_CGROUP_LEVELS 4 -struct { - __uint(type, BPF_MAP_TYPE_ARRAY); - __type(key, __u32); - __type(value, __u64); - __uint(max_entries, NUM_CGROUP_LEVELS); -} cgroup_ids SEC(".maps"); +__u64 cgroup_ids[NUM_CGROUP_LEVELS ]; static __always_inline void log_nth_level(struct __sk_buff *skb, __u32 level) { -__u64 id; -/* [1] &level passed to external function that may change it, it's * incompatible with loop unroll. */ - id =bpf_skb_ancestor_cgroup_id (skb, level); -bpf_map_update_elem (&cgroup_ids, &level, &id,0 ); + cgroup_ids[level] =bpf_skb_ancestor_cgroup_id (skb, level); }SEC ("tc" )
tools/testing/selftests/bpf/test_skb_cgroup_id.sh deleted +0 -67
67 lines not on this page · raw message
tools/testing/selftests/bpf/test_skb_cgroup_id_user.c deleted +0 -183
183 lines not on this page · raw message
Signature · 1 line
2.46.0
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v4 4/4] selftests/bpf: convert test_skb_cgroup_id_user to test_progs
Quoted 1 line · Alexis Lothoré (eBPF Foundation)
+#define
DST_ADDR"ff02::1"
[ ... ]
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c line 57
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c new file +8
-0+static int wait_local_ip (void ) +{ +char *ping_cmd =ping_command (AF_INET6 ); +int i, err; + +for (i =0 ; i <WAIT_AUTO_IP_MAX_ATTEMPT ; i++) { + err =SYS_NOFAIL ("%s -c 1 -W 1 %s%%%s" , ping_cmd,DST_ADDR , +VETH_1 );
I tried in my qemu. This loop takes at least 3-4 iteration to get the ping through. This test could become flaky if the CI is busy.
I have been always wondering why some of the (non) test_progs has this practice.
I traced a little. I think it has something to do with the "ff02::1" used in the test and/or the local link address is not ready. I have not further nailed it down but I think it is close enough.
It will be easier to use a nodad configured v6 addr.
I take this chance to use an easier "::1" address for the test here instead of ff02::1. This also removed the need to add veth pair and no need to ping first.
Applied with the "::1" changes mentioned above.
Thanks for migrating the tests to test_progs. This is long overdue.
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c new file +7
-0+if (!err) +break ; + } + +return err; +} +
Re: [PATCH bpf-next v4 4/4] selftests/bpf: convert test_skb_cgroup_id_user to test_progs
Hello Martin,
Quoted 23 lines · Martin KaFai Lau
Quoted 1 line · Alexis Lothoré (eBPF Foundation)
+#define
DST_ADDR"ff02::1"[ ... ]
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c line 57
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c new file +8
-0+static int wait_local_ip (void ) +{ + char *ping_cmd = ping_command(AF_INET6); + int i, err; + + for (i = 0; i < WAIT_AUTO_IP_MAX_ATTEMPT; i++) { + err = SYS_NOFAIL("%s -c 1 -W 1 %s%%%s", ping_cmd, DST_ADDR, + VETH_1);I tried in my qemu. This loop takes at least 3-4 iteration to get the ping through. This test could become flaky if the CI is busy.
I have been always wondering why some of the (non)
test_progshas this practice. I traced a little. I think it has something to do with the "ff02::1" used in the test and/or the local link address is not ready. I have not further nailed it down but I think it is close enough.
Quoted 6 lines
It will be easier to use a nodad configured v6 addr.
I take this chance to use an easier "::1" address for the test here instead of ff02::1. This also removed the need to add veth pair and no need to ping first.
Applied with the "::1" changes mentioned above.
Thanks for the improvement. So far I tried to preserve as much as possible the original test behavior to avoid accidentally removing some important features, but I guess the more tests I work on, the more confident I will be to "challenge" some parts ;)
Quoted 1 line
Thanks for migrating the tests to
test_progs. This is long overdue.
For the record, the next test I am targeting is test_xdp_features.sh. I have undertaken the conversion and have a working base but it still needs some work.
Thanks,
Alexis
Signature · 3 lines
Alexis Lothoré, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Quoted 11 lines · On Tue, 13 Aug 2024 14:45:04 +0200 you wrote:
Hello,
this series brings a new set of test converted to thetest_progsframework. Since the tests are quite small, I chose to group three tests conversion in the same series, but feel free to let me know if I should keep one series per test. The series focuses on cgroup testing and converts the following tests:
-get_cgroup_id_user
-cgroup_storage
-test_skb_cgroup_id_user[...]
Here is the summary with links:
- [bpf-next,v4,1/4] selftests/bpf: convert get_current_cgroup_id_user to test_progs
https://git.kernel.org/bpf/bpf-next/c/a4ae5c31e0f2 - [bpf-next,v4,2/4] selftests/bpf: convert test_cgroup_storage to test_progs https://git.kernel.org/bpf/bpf-next/c/37a14cfd667a - [bpf-next,v4,3/4] selftests/bpf: add proper section name to bpf prog and rename it https://git.kernel.org/bpf/bpf-next/c/7b4400a0a69b - [bpf-next,v4,4/4] selftests/bpf: convert test_skb_cgroup_id_user to test_progs https://git.kernel.org/bpf/bpf-next/c/f957c230e173
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html