blob: 9a9f6836e5e97674f883ddbc562c5eb718a1238d [file] [log] [blame]
Sargun Dhillon9b474ec2016-12-02 02:42:32 -08001/* 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/"
Alexei Starovoitov39323e72017-10-02 22:50:25 -070033#define PING_CMD "ping -c1 -w1 127.0.0.1 > /dev/null"
Sargun Dhillon9b474ec2016-12-02 02:42:32 -080034
Joe Stringerd40fc182016-12-14 14:43:38 -080035char bpf_log_buf[BPF_LOG_BUF_SIZE];
36
Sargun Dhillon9b474ec2016-12-02 02:42:32 -080037static int prog_load(int verdict)
38{
39 int ret;
40 struct bpf_insn prog[] = {
41 BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
42 BPF_EXIT_INSN(),
43 };
Joe Stringer43371c82016-12-14 14:43:39 -080044 size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
Sargun Dhillon9b474ec2016-12-02 02:42:32 -080045
Joe Stringerd40fc182016-12-14 14:43:38 -080046 ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB,
Joe Stringer43371c82016-12-14 14:43:39 -080047 prog, insns_cnt, "GPL", 0,
Joe Stringerd40fc182016-12-14 14:43:38 -080048 bpf_log_buf, BPF_LOG_BUF_SIZE);
Sargun Dhillon9b474ec2016-12-02 02:42:32 -080049
50 if (ret < 0) {
51 log_err("Loading program");
52 printf("Output from verifier:\n%s\n-------\n", bpf_log_buf);
53 return 0;
54 }
55 return ret;
56}
57
Alexei Starovoitov39323e72017-10-02 22:50:25 -070058static int test_foo_bar(void)
Sargun Dhillon9b474ec2016-12-02 02:42:32 -080059{
60 int drop_prog, allow_prog, foo = 0, bar = 0, rc = 0;
61
62 allow_prog = prog_load(1);
63 if (!allow_prog)
64 goto err;
65
66 drop_prog = prog_load(0);
67 if (!drop_prog)
68 goto err;
69
70 if (setup_cgroup_environment())
71 goto err;
72
73 /* Create cgroup /foo, get fd, and join it */
74 foo = create_and_get_cgroup(FOO);
75 if (!foo)
76 goto err;
77
78 if (join_cgroup(FOO))
79 goto err;
80
Alexei Starovoitov7f677632017-02-10 20:28:24 -080081 if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS, 1)) {
Sargun Dhillon9b474ec2016-12-02 02:42:32 -080082 log_err("Attaching prog to /foo");
83 goto err;
84 }
85
Alexei Starovoitov7f677632017-02-10 20:28:24 -080086 printf("Attached DROP prog. This ping in cgroup /foo should fail...\n");
Sargun Dhillon9b474ec2016-12-02 02:42:32 -080087 assert(system(PING_CMD) != 0);
88
89 /* Create cgroup /foo/bar, get fd, and join it */
90 bar = create_and_get_cgroup(BAR);
91 if (!bar)
92 goto err;
93
94 if (join_cgroup(BAR))
95 goto err;
96
Alexei Starovoitov7f677632017-02-10 20:28:24 -080097 printf("Attached DROP prog. This ping in cgroup /foo/bar should fail...\n");
Sargun Dhillon9b474ec2016-12-02 02:42:32 -080098 assert(system(PING_CMD) != 0);
99
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800100 if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
Sargun Dhillon9b474ec2016-12-02 02:42:32 -0800101 log_err("Attaching prog to /foo/bar");
102 goto err;
103 }
104
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800105 printf("Attached PASS prog. This ping in cgroup /foo/bar should pass...\n");
Sargun Dhillon9b474ec2016-12-02 02:42:32 -0800106 assert(system(PING_CMD) == 0);
107
Sargun Dhillon9b474ec2016-12-02 02:42:32 -0800108 if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) {
109 log_err("Detaching program from /foo/bar");
110 goto err;
111 }
112
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800113 printf("Detached PASS from /foo/bar while DROP is attached to /foo.\n"
114 "This ping in cgroup /foo/bar should fail...\n");
Sargun Dhillon9b474ec2016-12-02 02:42:32 -0800115 assert(system(PING_CMD) != 0);
116
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800117 if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
Sargun Dhillon9b474ec2016-12-02 02:42:32 -0800118 log_err("Attaching prog to /foo/bar");
119 goto err;
120 }
121
122 if (bpf_prog_detach(foo, BPF_CGROUP_INET_EGRESS)) {
123 log_err("Detaching program from /foo");
124 goto err;
125 }
126
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800127 printf("Attached PASS from /foo/bar and detached DROP from /foo.\n"
128 "This ping in cgroup /foo/bar should pass...\n");
Sargun Dhillon9b474ec2016-12-02 02:42:32 -0800129 assert(system(PING_CMD) == 0);
130
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800131 if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
132 log_err("Attaching prog to /foo/bar");
133 goto err;
134 }
135
136 if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 0)) {
137 errno = 0;
138 log_err("Unexpected success attaching prog to /foo/bar");
139 goto err;
140 }
141
142 if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) {
143 log_err("Detaching program from /foo/bar");
144 goto err;
145 }
146
147 if (!bpf_prog_detach(foo, BPF_CGROUP_INET_EGRESS)) {
148 errno = 0;
149 log_err("Unexpected success in double detach from /foo");
150 goto err;
151 }
152
153 if (bpf_prog_attach(allow_prog, foo, BPF_CGROUP_INET_EGRESS, 0)) {
154 log_err("Attaching non-overridable prog to /foo");
155 goto err;
156 }
157
158 if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 0)) {
159 errno = 0;
160 log_err("Unexpected success attaching non-overridable prog to /foo/bar");
161 goto err;
162 }
163
164 if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
165 errno = 0;
166 log_err("Unexpected success attaching overridable prog to /foo/bar");
167 goto err;
168 }
169
170 if (!bpf_prog_attach(allow_prog, foo, BPF_CGROUP_INET_EGRESS, 1)) {
171 errno = 0;
172 log_err("Unexpected success attaching overridable prog to /foo");
173 goto err;
174 }
175
176 if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS, 0)) {
177 log_err("Attaching different non-overridable prog to /foo");
178 goto err;
179 }
180
Sargun Dhillon9b474ec2016-12-02 02:42:32 -0800181 goto out;
182
183err:
184 rc = 1;
185
186out:
187 close(foo);
188 close(bar);
189 cleanup_cgroup_environment();
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800190 if (!rc)
Alexei Starovoitov39323e72017-10-02 22:50:25 -0700191 printf("### override:PASS\n");
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800192 else
Alexei Starovoitov39323e72017-10-02 22:50:25 -0700193 printf("### override:FAIL\n");
Sargun Dhillon9b474ec2016-12-02 02:42:32 -0800194 return rc;
195}
Alexei Starovoitov39323e72017-10-02 22:50:25 -0700196
197static int map_fd = -1;
198
199static int prog_load_cnt(int verdict, int val)
200{
201 if (map_fd < 0)
202 map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1, 0);
203 if (map_fd < 0) {
204 printf("failed to create map '%s'\n", strerror(errno));
205 return -1;
206 }
207
208 struct bpf_insn prog[] = {
209 BPF_MOV32_IMM(BPF_REG_0, 0),
210 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */
211 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
212 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */
213 BPF_LD_MAP_FD(BPF_REG_1, map_fd),
214 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
215 BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
216 BPF_MOV64_IMM(BPF_REG_1, val), /* r1 = 1 */
217 BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0), /* xadd r0 += r1 */
218 BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
219 BPF_EXIT_INSN(),
220 };
221 size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
222 int ret;
223
224 ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB,
225 prog, insns_cnt, "GPL", 0,
226 bpf_log_buf, BPF_LOG_BUF_SIZE);
227
228 if (ret < 0) {
229 log_err("Loading program");
230 printf("Output from verifier:\n%s\n-------\n", bpf_log_buf);
231 return 0;
232 }
233 return ret;
234}
235
236
237static int test_multiprog(void)
238{
239 int cg1 = 0, cg2 = 0, cg3 = 0, cg4 = 0, cg5 = 0, key = 0;
240 int drop_prog, allow_prog[6] = {}, rc = 0;
241 unsigned long long value;
242 int i = 0;
243
244 for (i = 0; i < 6; i++) {
245 allow_prog[i] = prog_load_cnt(1, 1 << i);
246 if (!allow_prog[i])
247 goto err;
248 }
249 drop_prog = prog_load_cnt(0, 1);
250 if (!drop_prog)
251 goto err;
252
253 if (setup_cgroup_environment())
254 goto err;
255
256 cg1 = create_and_get_cgroup("/cg1");
257 if (!cg1)
258 goto err;
259 cg2 = create_and_get_cgroup("/cg1/cg2");
260 if (!cg2)
261 goto err;
262 cg3 = create_and_get_cgroup("/cg1/cg2/cg3");
263 if (!cg3)
264 goto err;
265 cg4 = create_and_get_cgroup("/cg1/cg2/cg3/cg4");
266 if (!cg4)
267 goto err;
268 cg5 = create_and_get_cgroup("/cg1/cg2/cg3/cg4/cg5");
269 if (!cg5)
270 goto err;
271
272 if (join_cgroup("/cg1/cg2/cg3/cg4/cg5"))
273 goto err;
274
275 if (bpf_prog_attach(allow_prog[0], cg1, BPF_CGROUP_INET_EGRESS, 2)) {
276 log_err("Attaching prog to cg1");
277 goto err;
278 }
279 if (!bpf_prog_attach(allow_prog[0], cg1, BPF_CGROUP_INET_EGRESS, 2)) {
280 log_err("Unexpected success attaching the same prog to cg1");
281 goto err;
282 }
283 if (bpf_prog_attach(allow_prog[1], cg1, BPF_CGROUP_INET_EGRESS, 2)) {
284 log_err("Attaching prog2 to cg1");
285 goto err;
286 }
287 if (bpf_prog_attach(allow_prog[2], cg2, BPF_CGROUP_INET_EGRESS, 1)) {
288 log_err("Attaching prog to cg2");
289 goto err;
290 }
291 if (bpf_prog_attach(allow_prog[3], cg3, BPF_CGROUP_INET_EGRESS, 2)) {
292 log_err("Attaching prog to cg3");
293 goto err;
294 }
295 if (bpf_prog_attach(allow_prog[4], cg4, BPF_CGROUP_INET_EGRESS, 1)) {
296 log_err("Attaching prog to cg4");
297 goto err;
298 }
299 if (bpf_prog_attach(allow_prog[5], cg5, BPF_CGROUP_INET_EGRESS, 0)) {
300 log_err("Attaching prog to cg5");
301 goto err;
302 }
303 assert(system(PING_CMD) == 0);
304 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
305 assert(value == 1 + 2 + 8 + 32);
306
307 /* detach bottom program and ping again */
308 if (bpf_prog_detach2(-1, cg5, BPF_CGROUP_INET_EGRESS)) {
309 log_err("Detaching prog from cg5");
310 goto err;
311 }
312 value = 0;
313 assert(bpf_map_update_elem(map_fd, &key, &value, 0) == 0);
314 assert(system(PING_CMD) == 0);
315 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
316 assert(value == 1 + 2 + 8 + 16);
317
318 /* detach 3rd from bottom program and ping again */
319 errno = 0;
320 if (!bpf_prog_detach2(0, cg3, BPF_CGROUP_INET_EGRESS)) {
321 log_err("Unexpected success on detach from cg3");
322 goto err;
323 }
324 if (bpf_prog_detach2(allow_prog[3], cg3, BPF_CGROUP_INET_EGRESS)) {
325 log_err("Detaching from cg3");
326 goto err;
327 }
328 value = 0;
329 assert(bpf_map_update_elem(map_fd, &key, &value, 0) == 0);
330 assert(system(PING_CMD) == 0);
331 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
332 assert(value == 1 + 2 + 16);
333
334 /* detach 2nd from bottom program and ping again */
335 if (bpf_prog_detach2(-1, cg4, BPF_CGROUP_INET_EGRESS)) {
336 log_err("Detaching prog from cg4");
337 goto err;
338 }
339 value = 0;
340 assert(bpf_map_update_elem(map_fd, &key, &value, 0) == 0);
341 assert(system(PING_CMD) == 0);
342 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
343 assert(value == 1 + 2 + 4);
344 goto out;
345err:
346 rc = 1;
347
348out:
349 for (i = 0; i < 6; i++)
350 if (allow_prog[i] > 0)
351 close(allow_prog[i]);
352 close(cg1);
353 close(cg2);
354 close(cg3);
355 close(cg4);
356 close(cg5);
357 cleanup_cgroup_environment();
358 if (!rc)
359 printf("### multi:PASS\n");
360 else
361 printf("### multi:FAIL\n");
362 return rc;
363}
364
365int main(int argc, char **argv)
366{
367 int rc = 0;
368
369 rc = test_foo_bar();
370 if (rc)
371 return rc;
372
373 return test_multiprog();
374}