selftests/bpf: convert three other cgroup tests to test_progs
Subsystems (3)
v2 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 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 | 154 +++++++++++++++++ .../bpf/prog_tests/cgroup_get_current_cgroup_id.c | 45 +++++ .../selftests/bpf/prog_tests/cgroup_storage.c | 65 ++++++++ ...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, 297 insertions(+), 613 deletions(-)
---
base-commit: 34dbece299dfc462db4504268a697f29750d2932
change-id: 20240725-convert_cgroup_tests-d07c66053225
Best regards,
Signature · 3 lines
Alexis Lothoré, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
PATCH v2 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 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 | 45 ++++++ .../selftests/bpf/progs/get_cgroup_id_kern.c | 26 +--- 5 files changed, 50 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 +45 -0
45 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
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v2 1/4] selftests/bpf: convert get_current_cgroup_id_user to test_progs
tools/testing/selftests/bpf/prog_tests/cgroup_get_current_cgroup_id.c line 21
Quoted 11 lines · Alexis Lothoré (eBPF Foundation)
tools/testing/selftests/bpf/prog_tests/cgroup_get_current_cgroup_id.c new file +11
-0+void test_cgroup_get_current_cgroup_id (void ) +{ +struct get_cgroup_id_kern *skel; +const struct timespec req = { + .tv_sec =0 , + .tv_nsec =1 , + }; +int cgroup_fd; +__u64 ucgid; + + cgroup_fd =cgroup_setup_and_join (TEST_CGROUP );
missing close(cgroup_fd)
pw-bot: cr
Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/cgroup_get_current_cgroup_id.c new file +24
-0+if (!ASSERT_OK_FD (cgroup_fd,"cgroup switch" )) +return ; + + skel =get_cgroup_id_kern__open_and_load (); +if (!ASSERT_OK_PTR (skel,"load program" )) + goto cleanup_cgroup; + +if (!ASSERT_OK (get_cgroup_id_kern__attach (skel),"attach bpf program" )) + goto cleanup_progs; + + skel->bss->expected_pid =getpid (); +/* trigger the syscall on which is attached the tested prog */ +if (!ASSERT_OK (syscall (__NR_nanosleep, &req,NULL ),"nanosleep" )) + goto cleanup_progs; + + ucgid =get_cgroup_id (TEST_CGROUP ); + +ASSERT_EQ (skel->bss->cg_id, ucgid,"compare cgroup ids" ); + +cleanup_progs: +get_cgroup_id_kern__destroy (skel); +cleanup_cgroup: +cleanup_cgroup_environment (); +}
PATCH v2 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.
---
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 | 65 ++++++++ tools/testing/selftests/bpf/progs/cgroup_storage.c | 24 +++ tools/testing/selftests/bpf/test_cgroup_storage.c | 174 --------------------- 5 files changed, 89 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 +65 -0
65 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
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v2 2/4] selftests/bpf: convert test_cgroup_storage to test_progs
Quoted 10 lines · Alexis Lothoré (eBPF Foundation)
+++
b/tools/testing/selftests/bpf/prog_tests/cgroup_storage.ctools/testing/selftests/bpf/prog_tests/cgroup_storage.c new file +8
-0@@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "cgroup_helpers.h" +#include "cgroup_storage.skel.h" + +#define TEST_CGROUP "/test-bpf-cgroup-storage-buf/" +#define PING_CMD "ping localhost -c 1 -W 1 -q"
tools/testing/selftests/bpf/prog_tests/cgroup_storage.c line 18
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_storage.c new file +10
-0+ +void test_cgroup_storage (void ) +{ +struct bpf_cgroup_storage_key key; +struct cgroup_storage *skel; +unsigned long long value; +int cgroup_fd; +int err; + + cgroup_fd =cgroup_setup_and_join (TEST_CGROUP );
Same. cgroup_fd is leaked.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/cgroup_storage.c new file +4
-0+if (!ASSERT_OK_FD (cgroup_fd,"create cgroup" )) +return ; + + skel =cgroup_storage__open_and_load ();
tools/testing/selftests/bpf/prog_tests/cgroup_storage.c line 32
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_storage.c new file +10
-0+if (!ASSERT_OK_PTR (skel,"load program" )) + goto cleanup_cgroup; + + skel->links.bpf_prog = +bpf_program__attach_cgroup (skel->progs.bpf_prog, cgroup_fd); +if (!ASSERT_OK_PTR (skel->links.bpf_prog,"attach program" )) + goto cleanup_progs; + +/* Check that one out of every two packets is dropped */ + err =SYS_NOFAIL (PING_CMD );
Better create a netns for network traffic test. It is pretty simple and there are existing examples to borrow, e.g. the setup_test_env() in prog_tests/sock_addr.c. The netns setup should be done after the cgroup_setup_and_join().
PATCH v2 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 v2 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 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 | 154 +++++++++++++++++ .../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, 157 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 +154 -0
154 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 v2 4/4] selftests/bpf: convert test_skb_cgroup_id_user to test_progs
Quoted 32 lines · Alexis Lothoré (eBPF Foundation)
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c new file +26
-0@@ -0,0 +1,154 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "test_progs.h" +#include "network_helpers.h" +#include "cgroup_helpers.h" +#include "cgroup_ancestor.skel.h" + +#define VETH_PREFIX "test_cgid_" +#define VETH_1 VETH_PREFIX "1" +#define VETH_2 VETH_PREFIX "2" +#define CGROUP_PATH "/skb_cgroup_test" +#define NUM_CGROUP_LEVELS 4 +#define WAIT_AUTO_IP_MAX_ATTEMPT 10 +#define DST_ADDR "ff02::1" +#define DST_PORT 1234 +#define MAX_ASSERT_NAME 32 + +struct test_data { +struct cgroup_ancestor *skel; +struct bpf_tc_hook qdisc; +struct bpf_tc_opts tc_attach; +}; + +static int send_datagram (void ) +{ +unsigned char buf[] ="some random test data" ;
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c line 36
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c new file +10
-0+struct sockaddr_in6 addr = { .sin6_family =AF_INET6 , + .sin6_port =htons (DST_PORT ), + .sin6_scope_id =if_nametoindex (VETH_1 ) }; +int sock, n; + +if (!ASSERT_EQ (inet_pton (AF_INET6 ,DST_ADDR , &addr.sin6_addr),1 , +"inet_pton" )) +return -1 ; + + sock =socket (AF_INET6 ,SOCK_DGRAM ,0 );
sock is leaked.
Quoted 23 lines
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c new file +23
-0+if (!ASSERT_OK_FD (sock,"create socket" )) +return sock; + + n =sendto (sock, buf,sizeof (buf),0 , (const struct sockaddr *)&addr, +sizeof (addr)); +if (!ASSERT_EQ (n,sizeof (buf),"send data" )) +return n; + +return 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 ); +if (!err) +break ; + } +
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c line 69
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c new file +10
-0+return err; +} + +static int setup_network (struct test_data *t) +{ +int ret; + +SYS (fail,"ip link add dev %s type veth peer name %s" ,VETH_1 ,VETH_2 ); +SYS (fail,"ip link set %s up" ,VETH_1 ); +SYS (fail,"ip link set %s up" ,VETH_2 );
Same. Do it under a new netns.
Quoted 60 lines
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c new file +60
-0+ + ret =wait_local_ip (); +if (!ASSERT_EQ (ret,0 ,"wait local ip" )) + goto fail; + +memset (&t->qdisc,0 ,sizeof (t->qdisc)); + t->qdisc.sz =sizeof (t->qdisc); + t->qdisc.attach_point =BPF_TC_EGRESS ; + t->qdisc.ifindex =if_nametoindex (VETH_1 ); +if (!ASSERT_NEQ (t->qdisc.ifindex,0 ,"if_nametoindex" )) + goto cleanup_interfaces; +if (!ASSERT_OK (bpf_tc_hook_create (&t->qdisc),"qdisc add" )) + goto cleanup_interfaces; + +memset (&t->tc_attach,0 ,sizeof (t->tc_attach)); + t->tc_attach.sz =sizeof (t->tc_attach); + t->tc_attach.prog_fd =bpf_program__fd (t->skel->progs.log_cgroup_id); +if (!ASSERT_OK (bpf_tc_attach (&t->qdisc, &t->tc_attach),"filter add" )) + goto cleanup_qdisc; + +return 0 ; + +cleanup_qdisc: +bpf_tc_hook_destroy (&t->qdisc); +cleanup_interfaces: +SYS_NOFAIL ("ip link del %s" ,VETH_1 ); +fail: +return 1 ; +} + +static void cleanup_network (struct test_data *t) +{ +bpf_tc_detach (&t->qdisc, &t->tc_attach); +bpf_tc_hook_destroy (&t->qdisc); +/* Deleting first interface will also delete peer interface */ +SYS_NOFAIL ("ip link del %s" ,VETH_1 ); +} + +static void check_ancestors_ids (struct test_data *t) +{ +__u64 expected_ids[NUM_CGROUP_LEVELS ]; +char assert_name[MAX_ASSERT_NAME ]; +__u32 level; + + expected_ids[0 ] =get_cgroup_id ("/.." );/* root cgroup */ + expected_ids[1 ] =get_cgroup_id ("" ); + expected_ids[2 ] =get_cgroup_id (CGROUP_PATH ); + expected_ids[3 ] =0 ;/* non-existent cgroup */ + +for (level =0 ; level <NUM_CGROUP_LEVELS ; level++) { +snprintf (assert_name,MAX_ASSERT_NAME , +"ancestor id at level %d" , level); +ASSERT_EQ (t->skel->bss->cgroup_ids[level], expected_ids[level], + assert_name); + } +} + +void test_cgroup_ancestor (void ) +{ +struct test_data t;
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c line 139
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c new file +10
-0+int cgroup_fd; + + t.skel =cgroup_ancestor__open_and_load (); +if (!ASSERT_OK_PTR (t.skel,"open and load" )) +return ; + +if (setup_network (&t)) + goto cleanup_progs; + + cgroup_fd =cgroup_setup_and_join (CGROUP_PATH );
cgroup_fd is leaked.
Thanks for working on this.
Quoted 15 lines
tools/testing/selftests/bpf/prog_tests/cgroup_ancestor.c new file +15
-0+if (cgroup_fd <0 ) + goto cleanup_network; + +if (send_datagram ()) + goto cleanup_cgroups; + +check_ancestors_ids (&t); + +cleanup_cgroups: +cleanup_cgroup_environment (); +cleanup_network: +cleanup_network (&t); +cleanup_progs: +cgroup_ancestor__destroy (t.skel); +}
Re: [PATCH bpf-next v2 4/4] selftests/bpf: convert test_skb_cgroup_id_user to test_progs
Hello Martin,
Quoted 1 line · Martin KaFai Lau
On 8/6/24 12:55 AM, Alexis Lothoré (eBPF Foundation) wrote:
[...]
Quoted 7 lines
Quoted 3 lines
+
SYS(fail, "ip link add dev %s type veth peer name %s", VETH_1, VETH_2); +SYS(fail, "ip link set %s up", VETH_1);
+SYS(fail, "ip link set %s up", VETH_2);Same. Do it under a new netns.
Quoted 1 line
+
[...]
Quoted 6 lines
Quoted 4 lines
+ if (
setup_network(&t))
+ gotocleanup_progs;
+
+cgroup_fd=cgroup_setup_and_join(CGROUP_PATH);
cgroup_fdis leaked.
ACK, I will prepare a new revision with all the leaked file descriptors you have spotted fixed, and with tests performed in a dedicated netns.
Quoted 1 line
Thanks for working on this.
Thanks for the review !
Alexis
Signature · 3 lines
Alexis Lothoré, Bootlin Embedded Linux and Kernel engineering https://bootlin.com