Skip to content

Commit 9b474ec

Browse files
sargundavem330
authored andcommitted
samples, bpf: Add automated test for cgroup filter attachments
This patch adds the sample program test_cgrp2_attach2. This program is similar to test_cgrp2_attach, but it performs automated testing of the cgroupv2 BPF attached filters. It runs the following checks: * Simple filter attachment * Application of filters to child cgroups * Overriding filters on child cgroups * Checking that this still works when the parent filter is removed The filters that are used here are simply allow all / deny all filters, so it isn't checking the actual functionality of the filters, but rather the behaviour around detachment / attachment. If net_cls is enabled, this test will fail. Signed-off-by: Sargun Dhillon <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1a922fe commit 9b474ec

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

samples/bpf/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ hostprogs-y += map_perf_test
2323
hostprogs-y += test_overhead
2424
hostprogs-y += test_cgrp2_array_pin
2525
hostprogs-y += test_cgrp2_attach
26+
hostprogs-y += test_cgrp2_attach2
2627
hostprogs-y += test_cgrp2_sock
2728
hostprogs-y += test_cgrp2_sock2
2829
hostprogs-y += xdp1
@@ -54,6 +55,7 @@ map_perf_test-objs := bpf_load.o libbpf.o map_perf_test_user.o
5455
test_overhead-objs := bpf_load.o libbpf.o test_overhead_user.o
5556
test_cgrp2_array_pin-objs := libbpf.o test_cgrp2_array_pin.o
5657
test_cgrp2_attach-objs := libbpf.o test_cgrp2_attach.o
58+
test_cgrp2_attach2-objs := libbpf.o test_cgrp2_attach2.o cgroup_helpers.o
5759
test_cgrp2_sock-objs := libbpf.o test_cgrp2_sock.o
5860
test_cgrp2_sock2-objs := bpf_load.o libbpf.o test_cgrp2_sock2.o
5961
xdp1-objs := bpf_load.o libbpf.o xdp1_user.o

samples/bpf/test_cgrp2_attach2.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/* eBPF example program:
2+
*
3+
* - Creates arraymap in kernel with 4 bytes keys and 8 byte values
4+
*
5+
* - Loads eBPF program
6+
*
7+
* The eBPF program accesses the map passed in to store two pieces of
8+
* information. The number of invocations of the program, which maps
9+
* to the number of packets received, is stored to key 0. Key 1 is
10+
* incremented on each iteration by the number of bytes stored in
11+
* the skb.
12+
*
13+
* - Attaches the new program to a cgroup using BPF_PROG_ATTACH
14+
*
15+
* - Every second, reads map[0] and map[1] to see how many bytes and
16+
* packets were seen on any socket of tasks in the given cgroup.
17+
*/
18+
19+
#define _GNU_SOURCE
20+
21+
#include <stdio.h>
22+
#include <stdlib.h>
23+
#include <assert.h>
24+
#include <unistd.h>
25+
26+
#include <linux/bpf.h>
27+
28+
#include "libbpf.h"
29+
#include "cgroup_helpers.h"
30+
31+
#define FOO "/foo"
32+
#define BAR "/foo/bar/"
33+
#define PING_CMD "ping -c1 -w1 127.0.0.1"
34+
35+
static int prog_load(int verdict)
36+
{
37+
int ret;
38+
struct bpf_insn prog[] = {
39+
BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
40+
BPF_EXIT_INSN(),
41+
};
42+
43+
ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SKB,
44+
prog, sizeof(prog), "GPL", 0);
45+
46+
if (ret < 0) {
47+
log_err("Loading program");
48+
printf("Output from verifier:\n%s\n-------\n", bpf_log_buf);
49+
return 0;
50+
}
51+
return ret;
52+
}
53+
54+
55+
int main(int argc, char **argv)
56+
{
57+
int drop_prog, allow_prog, foo = 0, bar = 0, rc = 0;
58+
59+
allow_prog = prog_load(1);
60+
if (!allow_prog)
61+
goto err;
62+
63+
drop_prog = prog_load(0);
64+
if (!drop_prog)
65+
goto err;
66+
67+
if (setup_cgroup_environment())
68+
goto err;
69+
70+
/* Create cgroup /foo, get fd, and join it */
71+
foo = create_and_get_cgroup(FOO);
72+
if (!foo)
73+
goto err;
74+
75+
if (join_cgroup(FOO))
76+
goto err;
77+
78+
if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS)) {
79+
log_err("Attaching prog to /foo");
80+
goto err;
81+
}
82+
83+
assert(system(PING_CMD) != 0);
84+
85+
/* Create cgroup /foo/bar, get fd, and join it */
86+
bar = create_and_get_cgroup(BAR);
87+
if (!bar)
88+
goto err;
89+
90+
if (join_cgroup(BAR))
91+
goto err;
92+
93+
assert(system(PING_CMD) != 0);
94+
95+
if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS)) {
96+
log_err("Attaching prog to /foo/bar");
97+
goto err;
98+
}
99+
100+
assert(system(PING_CMD) == 0);
101+
102+
103+
if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) {
104+
log_err("Detaching program from /foo/bar");
105+
goto err;
106+
}
107+
108+
assert(system(PING_CMD) != 0);
109+
110+
if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS)) {
111+
log_err("Attaching prog to /foo/bar");
112+
goto err;
113+
}
114+
115+
if (bpf_prog_detach(foo, BPF_CGROUP_INET_EGRESS)) {
116+
log_err("Detaching program from /foo");
117+
goto err;
118+
}
119+
120+
assert(system(PING_CMD) == 0);
121+
122+
goto out;
123+
124+
err:
125+
rc = 1;
126+
127+
out:
128+
close(foo);
129+
close(bar);
130+
cleanup_cgroup_environment();
131+
return rc;
132+
}

0 commit comments

Comments
 (0)