Skip to content

Commit 37a14cf

Browse files
TropicaoMartin KaFai Lau
authored and
Martin KaFai Lau
committed
selftests/bpf: convert test_cgroup_storage to test_progs
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. Reviewed-by: Alan Maguire <[email protected]> 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 a4ae5c3 commit 37a14cf

File tree

5 files changed

+120
-177
lines changed

5 files changed

+120
-177
lines changed

tools/testing/selftests/bpf/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ urandom_read
2020
test_sockmap
2121
test_lirc_mode2_user
2222
test_skb_cgroup_id_user
23-
test_cgroup_storage
2423
test_flow_dissector
2524
flow_dissector_load
2625
test_tcpnotify_user

tools/testing/selftests/bpf/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ endif
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 \
7070
test_sock test_sockmap \
71-
test_cgroup_storage \
7271
test_tcpnotify_user test_sysctl \
7372
test_progs-no_alu32
7473
TEST_INST_SUBDIRS := no_alu32
@@ -295,7 +294,6 @@ $(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
295294
$(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS)
296295
$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS)
297296
$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS)
298-
$(OUTPUT)/test_cgroup_storage: $(CGROUP_HELPERS) $(TESTING_HELPERS)
299297
$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS)
300298
$(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS)
301299
$(OUTPUT)/test_tag: $(TESTING_HELPERS)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
#include "cgroup_helpers.h"
5+
#include "network_helpers.h"
6+
#include "cgroup_storage.skel.h"
7+
8+
#define TEST_CGROUP "/test-bpf-cgroup-storage-buf/"
9+
#define TEST_NS "cgroup_storage_ns"
10+
#define PING_CMD "ping localhost -c 1 -W 1 -q"
11+
12+
static int setup_network(struct nstoken **token)
13+
{
14+
SYS(fail, "ip netns add %s", TEST_NS);
15+
*token = open_netns(TEST_NS);
16+
if (!ASSERT_OK_PTR(*token, "open netns"))
17+
goto cleanup_ns;
18+
SYS(cleanup_ns, "ip link set lo up");
19+
20+
return 0;
21+
22+
cleanup_ns:
23+
SYS_NOFAIL("ip netns del %s", TEST_NS);
24+
fail:
25+
return -1;
26+
}
27+
28+
static void cleanup_network(struct nstoken *ns)
29+
{
30+
close_netns(ns);
31+
SYS_NOFAIL("ip netns del %s", TEST_NS);
32+
}
33+
34+
void test_cgroup_storage(void)
35+
{
36+
struct bpf_cgroup_storage_key key;
37+
struct cgroup_storage *skel;
38+
struct nstoken *ns = NULL;
39+
unsigned long long value;
40+
int cgroup_fd;
41+
int err;
42+
43+
cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
44+
if (!ASSERT_OK_FD(cgroup_fd, "create cgroup"))
45+
return;
46+
47+
if (!ASSERT_OK(setup_network(&ns), "setup network"))
48+
goto cleanup_cgroup;
49+
50+
skel = cgroup_storage__open_and_load();
51+
if (!ASSERT_OK_PTR(skel, "load program"))
52+
goto cleanup_network;
53+
54+
skel->links.bpf_prog =
55+
bpf_program__attach_cgroup(skel->progs.bpf_prog, cgroup_fd);
56+
if (!ASSERT_OK_PTR(skel->links.bpf_prog, "attach program"))
57+
goto cleanup_progs;
58+
59+
/* Check that one out of every two packets is dropped */
60+
err = SYS_NOFAIL(PING_CMD);
61+
ASSERT_OK(err, "first ping");
62+
err = SYS_NOFAIL(PING_CMD);
63+
ASSERT_NEQ(err, 0, "second ping");
64+
err = SYS_NOFAIL(PING_CMD);
65+
ASSERT_OK(err, "third ping");
66+
67+
err = bpf_map__get_next_key(skel->maps.cgroup_storage, NULL, &key,
68+
sizeof(key));
69+
if (!ASSERT_OK(err, "get first key"))
70+
goto cleanup_progs;
71+
err = bpf_map__lookup_elem(skel->maps.cgroup_storage, &key, sizeof(key),
72+
&value, sizeof(value), 0);
73+
if (!ASSERT_OK(err, "first packet count read"))
74+
goto cleanup_progs;
75+
76+
/* Add one to the packet counter, check again packet filtering */
77+
value++;
78+
err = bpf_map__update_elem(skel->maps.cgroup_storage, &key, sizeof(key),
79+
&value, sizeof(value), 0);
80+
if (!ASSERT_OK(err, "increment packet counter"))
81+
goto cleanup_progs;
82+
err = SYS_NOFAIL(PING_CMD);
83+
ASSERT_OK(err, "fourth ping");
84+
err = SYS_NOFAIL(PING_CMD);
85+
ASSERT_NEQ(err, 0, "fifth ping");
86+
err = SYS_NOFAIL(PING_CMD);
87+
ASSERT_OK(err, "sixth ping");
88+
89+
cleanup_progs:
90+
cgroup_storage__destroy(skel);
91+
cleanup_network:
92+
cleanup_network(ns);
93+
cleanup_cgroup:
94+
close(cgroup_fd);
95+
cleanup_cgroup_environment();
96+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/bpf.h>
4+
#include <bpf/bpf_helpers.h>
5+
6+
struct {
7+
__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
8+
__type(key, struct bpf_cgroup_storage_key);
9+
__type(value, __u64);
10+
} cgroup_storage SEC(".maps");
11+
12+
SEC("cgroup_skb/egress")
13+
int bpf_prog(struct __sk_buff *skb)
14+
{
15+
__u64 *counter;
16+
17+
counter = bpf_get_local_storage(&cgroup_storage, 0);
18+
__sync_fetch_and_add(counter, 1);
19+
20+
/* Drop one out of every two packets */
21+
return (*counter & 1);
22+
}
23+
24+
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/test_cgroup_storage.c

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

0 commit comments

Comments
 (0)