Linux 4.19.133
[linux/fpc-iii.git] / samples / bpf / test_cgrp2_attach2.c
blob180f9d813bcaafdc8ead9f83c4c09a13e4e05e69
1 /* eBPF example program:
3 * - Creates arraymap in kernel with 4 bytes keys and 8 byte values
5 * - Loads eBPF program
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. The program also stores the number of received bytes
12 * in the cgroup storage.
14 * - Attaches the new program to a cgroup using BPF_PROG_ATTACH
16 * - Every second, reads map[0] and map[1] to see how many bytes and
17 * packets were seen on any socket of tasks in the given cgroup.
20 #define _GNU_SOURCE
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <sys/resource.h>
26 #include <sys/time.h>
27 #include <unistd.h>
29 #include <linux/bpf.h>
30 #include <bpf/bpf.h>
32 #include "bpf_insn.h"
33 #include "bpf_rlimit.h"
34 #include "cgroup_helpers.h"
36 #define FOO "/foo"
37 #define BAR "/foo/bar/"
38 #define PING_CMD "ping -c1 -w1 127.0.0.1 > /dev/null"
40 char bpf_log_buf[BPF_LOG_BUF_SIZE];
42 static int prog_load(int verdict)
44 int ret;
45 struct bpf_insn prog[] = {
46 BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
47 BPF_EXIT_INSN(),
49 size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
51 ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB,
52 prog, insns_cnt, "GPL", 0,
53 bpf_log_buf, BPF_LOG_BUF_SIZE);
55 if (ret < 0) {
56 log_err("Loading program");
57 printf("Output from verifier:\n%s\n-------\n", bpf_log_buf);
58 return 0;
60 return ret;
63 static int test_foo_bar(void)
65 int drop_prog, allow_prog, foo = 0, bar = 0, rc = 0;
67 allow_prog = prog_load(1);
68 if (!allow_prog)
69 goto err;
71 drop_prog = prog_load(0);
72 if (!drop_prog)
73 goto err;
75 if (setup_cgroup_environment())
76 goto err;
78 /* Create cgroup /foo, get fd, and join it */
79 foo = create_and_get_cgroup(FOO);
80 if (!foo)
81 goto err;
83 if (join_cgroup(FOO))
84 goto err;
86 if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS,
87 BPF_F_ALLOW_OVERRIDE)) {
88 log_err("Attaching prog to /foo");
89 goto err;
92 printf("Attached DROP prog. This ping in cgroup /foo should fail...\n");
93 assert(system(PING_CMD) != 0);
95 /* Create cgroup /foo/bar, get fd, and join it */
96 bar = create_and_get_cgroup(BAR);
97 if (!bar)
98 goto err;
100 if (join_cgroup(BAR))
101 goto err;
103 printf("Attached DROP prog. This ping in cgroup /foo/bar should fail...\n");
104 assert(system(PING_CMD) != 0);
106 if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS,
107 BPF_F_ALLOW_OVERRIDE)) {
108 log_err("Attaching prog to /foo/bar");
109 goto err;
112 printf("Attached PASS prog. This ping in cgroup /foo/bar should pass...\n");
113 assert(system(PING_CMD) == 0);
115 if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) {
116 log_err("Detaching program from /foo/bar");
117 goto err;
120 printf("Detached PASS from /foo/bar while DROP is attached to /foo.\n"
121 "This ping in cgroup /foo/bar should fail...\n");
122 assert(system(PING_CMD) != 0);
124 if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS,
125 BPF_F_ALLOW_OVERRIDE)) {
126 log_err("Attaching prog to /foo/bar");
127 goto err;
130 if (bpf_prog_detach(foo, BPF_CGROUP_INET_EGRESS)) {
131 log_err("Detaching program from /foo");
132 goto err;
135 printf("Attached PASS from /foo/bar and detached DROP from /foo.\n"
136 "This ping in cgroup /foo/bar should pass...\n");
137 assert(system(PING_CMD) == 0);
139 if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS,
140 BPF_F_ALLOW_OVERRIDE)) {
141 log_err("Attaching prog to /foo/bar");
142 goto err;
145 if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 0)) {
146 errno = 0;
147 log_err("Unexpected success attaching prog to /foo/bar");
148 goto err;
151 if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) {
152 log_err("Detaching program from /foo/bar");
153 goto err;
156 if (!bpf_prog_detach(foo, BPF_CGROUP_INET_EGRESS)) {
157 errno = 0;
158 log_err("Unexpected success in double detach from /foo");
159 goto err;
162 if (bpf_prog_attach(allow_prog, foo, BPF_CGROUP_INET_EGRESS, 0)) {
163 log_err("Attaching non-overridable prog to /foo");
164 goto err;
167 if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 0)) {
168 errno = 0;
169 log_err("Unexpected success attaching non-overridable prog to /foo/bar");
170 goto err;
173 if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS,
174 BPF_F_ALLOW_OVERRIDE)) {
175 errno = 0;
176 log_err("Unexpected success attaching overridable prog to /foo/bar");
177 goto err;
180 if (!bpf_prog_attach(allow_prog, foo, BPF_CGROUP_INET_EGRESS,
181 BPF_F_ALLOW_OVERRIDE)) {
182 errno = 0;
183 log_err("Unexpected success attaching overridable prog to /foo");
184 goto err;
187 if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS, 0)) {
188 log_err("Attaching different non-overridable prog to /foo");
189 goto err;
192 goto out;
194 err:
195 rc = 1;
197 out:
198 close(foo);
199 close(bar);
200 cleanup_cgroup_environment();
201 if (!rc)
202 printf("### override:PASS\n");
203 else
204 printf("### override:FAIL\n");
205 return rc;
208 static int map_fd = -1;
210 static int prog_load_cnt(int verdict, int val)
212 int cgroup_storage_fd;
214 if (map_fd < 0)
215 map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1, 0);
216 if (map_fd < 0) {
217 printf("failed to create map '%s'\n", strerror(errno));
218 return -1;
221 cgroup_storage_fd = bpf_create_map(BPF_MAP_TYPE_CGROUP_STORAGE,
222 sizeof(struct bpf_cgroup_storage_key), 8, 0, 0);
223 if (cgroup_storage_fd < 0) {
224 printf("failed to create map '%s'\n", strerror(errno));
225 return -1;
228 struct bpf_insn prog[] = {
229 BPF_MOV32_IMM(BPF_REG_0, 0),
230 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */
231 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
232 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */
233 BPF_LD_MAP_FD(BPF_REG_1, map_fd),
234 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
235 BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
236 BPF_MOV64_IMM(BPF_REG_1, val), /* r1 = 1 */
237 BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0), /* xadd r0 += r1 */
238 BPF_LD_MAP_FD(BPF_REG_1, cgroup_storage_fd),
239 BPF_MOV64_IMM(BPF_REG_2, 0),
240 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_local_storage),
241 BPF_MOV64_IMM(BPF_REG_1, val),
242 BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_W, BPF_REG_0, BPF_REG_1, 0, 0),
243 BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
244 BPF_EXIT_INSN(),
246 size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
247 int ret;
249 ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB,
250 prog, insns_cnt, "GPL", 0,
251 bpf_log_buf, BPF_LOG_BUF_SIZE);
253 if (ret < 0) {
254 log_err("Loading program");
255 printf("Output from verifier:\n%s\n-------\n", bpf_log_buf);
256 return 0;
258 close(cgroup_storage_fd);
259 return ret;
263 static int test_multiprog(void)
265 __u32 prog_ids[4], prog_cnt = 0, attach_flags, saved_prog_id;
266 int cg1 = 0, cg2 = 0, cg3 = 0, cg4 = 0, cg5 = 0, key = 0;
267 int drop_prog, allow_prog[6] = {}, rc = 0;
268 unsigned long long value;
269 int i = 0;
271 for (i = 0; i < 6; i++) {
272 allow_prog[i] = prog_load_cnt(1, 1 << i);
273 if (!allow_prog[i])
274 goto err;
276 drop_prog = prog_load_cnt(0, 1);
277 if (!drop_prog)
278 goto err;
280 if (setup_cgroup_environment())
281 goto err;
283 cg1 = create_and_get_cgroup("/cg1");
284 if (!cg1)
285 goto err;
286 cg2 = create_and_get_cgroup("/cg1/cg2");
287 if (!cg2)
288 goto err;
289 cg3 = create_and_get_cgroup("/cg1/cg2/cg3");
290 if (!cg3)
291 goto err;
292 cg4 = create_and_get_cgroup("/cg1/cg2/cg3/cg4");
293 if (!cg4)
294 goto err;
295 cg5 = create_and_get_cgroup("/cg1/cg2/cg3/cg4/cg5");
296 if (!cg5)
297 goto err;
299 if (join_cgroup("/cg1/cg2/cg3/cg4/cg5"))
300 goto err;
302 if (bpf_prog_attach(allow_prog[0], cg1, BPF_CGROUP_INET_EGRESS,
303 BPF_F_ALLOW_MULTI)) {
304 log_err("Attaching prog to cg1");
305 goto err;
307 if (!bpf_prog_attach(allow_prog[0], cg1, BPF_CGROUP_INET_EGRESS,
308 BPF_F_ALLOW_MULTI)) {
309 log_err("Unexpected success attaching the same prog to cg1");
310 goto err;
312 if (bpf_prog_attach(allow_prog[1], cg1, BPF_CGROUP_INET_EGRESS,
313 BPF_F_ALLOW_MULTI)) {
314 log_err("Attaching prog2 to cg1");
315 goto err;
317 if (bpf_prog_attach(allow_prog[2], cg2, BPF_CGROUP_INET_EGRESS,
318 BPF_F_ALLOW_OVERRIDE)) {
319 log_err("Attaching prog to cg2");
320 goto err;
322 if (bpf_prog_attach(allow_prog[3], cg3, BPF_CGROUP_INET_EGRESS,
323 BPF_F_ALLOW_MULTI)) {
324 log_err("Attaching prog to cg3");
325 goto err;
327 if (bpf_prog_attach(allow_prog[4], cg4, BPF_CGROUP_INET_EGRESS,
328 BPF_F_ALLOW_OVERRIDE)) {
329 log_err("Attaching prog to cg4");
330 goto err;
332 if (bpf_prog_attach(allow_prog[5], cg5, BPF_CGROUP_INET_EGRESS, 0)) {
333 log_err("Attaching prog to cg5");
334 goto err;
336 assert(system(PING_CMD) == 0);
337 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
338 assert(value == 1 + 2 + 8 + 32);
340 /* query the number of effective progs in cg5 */
341 assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, BPF_F_QUERY_EFFECTIVE,
342 NULL, NULL, &prog_cnt) == 0);
343 assert(prog_cnt == 4);
344 /* retrieve prog_ids of effective progs in cg5 */
345 assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, BPF_F_QUERY_EFFECTIVE,
346 &attach_flags, prog_ids, &prog_cnt) == 0);
347 assert(prog_cnt == 4);
348 assert(attach_flags == 0);
349 saved_prog_id = prog_ids[0];
350 /* check enospc handling */
351 prog_ids[0] = 0;
352 prog_cnt = 2;
353 assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, BPF_F_QUERY_EFFECTIVE,
354 &attach_flags, prog_ids, &prog_cnt) == -1 &&
355 errno == ENOSPC);
356 assert(prog_cnt == 4);
357 /* check that prog_ids are returned even when buffer is too small */
358 assert(prog_ids[0] == saved_prog_id);
359 /* retrieve prog_id of single attached prog in cg5 */
360 prog_ids[0] = 0;
361 assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, 0,
362 NULL, prog_ids, &prog_cnt) == 0);
363 assert(prog_cnt == 1);
364 assert(prog_ids[0] == saved_prog_id);
366 /* detach bottom program and ping again */
367 if (bpf_prog_detach2(-1, cg5, BPF_CGROUP_INET_EGRESS)) {
368 log_err("Detaching prog from cg5");
369 goto err;
371 value = 0;
372 assert(bpf_map_update_elem(map_fd, &key, &value, 0) == 0);
373 assert(system(PING_CMD) == 0);
374 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
375 assert(value == 1 + 2 + 8 + 16);
377 /* detach 3rd from bottom program and ping again */
378 errno = 0;
379 if (!bpf_prog_detach2(0, cg3, BPF_CGROUP_INET_EGRESS)) {
380 log_err("Unexpected success on detach from cg3");
381 goto err;
383 if (bpf_prog_detach2(allow_prog[3], cg3, BPF_CGROUP_INET_EGRESS)) {
384 log_err("Detaching from cg3");
385 goto err;
387 value = 0;
388 assert(bpf_map_update_elem(map_fd, &key, &value, 0) == 0);
389 assert(system(PING_CMD) == 0);
390 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
391 assert(value == 1 + 2 + 16);
393 /* detach 2nd from bottom program and ping again */
394 if (bpf_prog_detach2(-1, cg4, BPF_CGROUP_INET_EGRESS)) {
395 log_err("Detaching prog from cg4");
396 goto err;
398 value = 0;
399 assert(bpf_map_update_elem(map_fd, &key, &value, 0) == 0);
400 assert(system(PING_CMD) == 0);
401 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
402 assert(value == 1 + 2 + 4);
404 prog_cnt = 4;
405 assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, BPF_F_QUERY_EFFECTIVE,
406 &attach_flags, prog_ids, &prog_cnt) == 0);
407 assert(prog_cnt == 3);
408 assert(attach_flags == 0);
409 assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, 0,
410 NULL, prog_ids, &prog_cnt) == 0);
411 assert(prog_cnt == 0);
412 goto out;
413 err:
414 rc = 1;
416 out:
417 for (i = 0; i < 6; i++)
418 if (allow_prog[i] > 0)
419 close(allow_prog[i]);
420 close(cg1);
421 close(cg2);
422 close(cg3);
423 close(cg4);
424 close(cg5);
425 cleanup_cgroup_environment();
426 if (!rc)
427 printf("### multi:PASS\n");
428 else
429 printf("### multi:FAIL\n");
430 return rc;
433 int main(int argc, char **argv)
435 int rc = 0;
437 rc = test_foo_bar();
438 if (rc)
439 return rc;
441 return test_multiprog();