diff options
| author | Roman Gushchin <guro@fb.com> | 2018-09-28 10:45:58 -0400 |
|---|---|---|
| committer | Daniel Borkmann <daniel@iogearbox.net> | 2018-10-01 10:18:33 -0400 |
| commit | 5fcbd29b372402c96b182e0e80b3d95463431490 (patch) | |
| tree | 460af9949d1e63fb16bcf8ffde5b9c833b5fcb9e /samples | |
| parent | 919646d2a3a90465cf8b30a7616c63005bef7570 (diff) | |
samples/bpf: extend test_cgrp2_attach2 test to use per-cpu cgroup storage
This commit extends the test_cgrp2_attach2 test to cover per-cpu
cgroup storage. Bpf program will use shared and per-cpu cgroup
storages simultaneously, so a better coverage of corresponding
core code will be achieved.
Expected output:
$ ./test_cgrp2_attach2
Attached DROP prog. This ping in cgroup /foo should fail...
ping: sendmsg: Operation not permitted
Attached DROP prog. This ping in cgroup /foo/bar should fail...
ping: sendmsg: Operation not permitted
Attached PASS prog. This ping in cgroup /foo/bar should pass...
Detached PASS from /foo/bar while DROP is attached to /foo.
This ping in cgroup /foo/bar should fail...
ping: sendmsg: Operation not permitted
Attached PASS from /foo/bar and detached DROP from /foo.
This ping in cgroup /foo/bar should pass...
### override:PASS
### multi:PASS
Signed-off-by: Roman Gushchin <guro@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'samples')
| -rw-r--r-- | samples/bpf/test_cgrp2_attach2.c | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/samples/bpf/test_cgrp2_attach2.c b/samples/bpf/test_cgrp2_attach2.c index 180f9d813bca..d7b68ef5ba79 100644 --- a/samples/bpf/test_cgrp2_attach2.c +++ b/samples/bpf/test_cgrp2_attach2.c | |||
| @@ -209,7 +209,7 @@ static int map_fd = -1; | |||
| 209 | 209 | ||
| 210 | static int prog_load_cnt(int verdict, int val) | 210 | static int prog_load_cnt(int verdict, int val) |
| 211 | { | 211 | { |
| 212 | int cgroup_storage_fd; | 212 | int cgroup_storage_fd, percpu_cgroup_storage_fd; |
| 213 | 213 | ||
| 214 | if (map_fd < 0) | 214 | if (map_fd < 0) |
| 215 | map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1, 0); | 215 | map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1, 0); |
| @@ -225,6 +225,14 @@ static int prog_load_cnt(int verdict, int val) | |||
| 225 | return -1; | 225 | return -1; |
| 226 | } | 226 | } |
| 227 | 227 | ||
| 228 | percpu_cgroup_storage_fd = bpf_create_map( | ||
| 229 | BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE, | ||
| 230 | sizeof(struct bpf_cgroup_storage_key), 8, 0, 0); | ||
| 231 | if (percpu_cgroup_storage_fd < 0) { | ||
| 232 | printf("failed to create map '%s'\n", strerror(errno)); | ||
| 233 | return -1; | ||
| 234 | } | ||
| 235 | |||
| 228 | struct bpf_insn prog[] = { | 236 | struct bpf_insn prog[] = { |
| 229 | BPF_MOV32_IMM(BPF_REG_0, 0), | 237 | BPF_MOV32_IMM(BPF_REG_0, 0), |
| 230 | BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */ | 238 | BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */ |
| @@ -235,11 +243,20 @@ static int prog_load_cnt(int verdict, int val) | |||
| 235 | BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2), | 243 | BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2), |
| 236 | BPF_MOV64_IMM(BPF_REG_1, val), /* r1 = 1 */ | 244 | 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 */ | 245 | BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0), /* xadd r0 += r1 */ |
| 246 | |||
| 238 | BPF_LD_MAP_FD(BPF_REG_1, cgroup_storage_fd), | 247 | BPF_LD_MAP_FD(BPF_REG_1, cgroup_storage_fd), |
| 239 | BPF_MOV64_IMM(BPF_REG_2, 0), | 248 | BPF_MOV64_IMM(BPF_REG_2, 0), |
| 240 | BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_local_storage), | 249 | BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_local_storage), |
| 241 | BPF_MOV64_IMM(BPF_REG_1, val), | 250 | 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), | 251 | BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_W, BPF_REG_0, BPF_REG_1, 0, 0), |
| 252 | |||
| 253 | BPF_LD_MAP_FD(BPF_REG_1, percpu_cgroup_storage_fd), | ||
| 254 | BPF_MOV64_IMM(BPF_REG_2, 0), | ||
| 255 | BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_local_storage), | ||
| 256 | BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_0, 0), | ||
| 257 | BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, 0x1), | ||
| 258 | BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_3, 0), | ||
| 259 | |||
| 243 | BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */ | 260 | BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */ |
| 244 | BPF_EXIT_INSN(), | 261 | BPF_EXIT_INSN(), |
| 245 | }; | 262 | }; |
