.. | .. |
---|
4 | 4 | #include <linux/filter.h> |
---|
5 | 5 | #include <stdio.h> |
---|
6 | 6 | #include <stdlib.h> |
---|
| 7 | +#include <sys/sysinfo.h> |
---|
7 | 8 | |
---|
8 | 9 | #include "bpf_rlimit.h" |
---|
9 | 10 | #include "cgroup_helpers.h" |
---|
.. | .. |
---|
15 | 16 | int main(int argc, char **argv) |
---|
16 | 17 | { |
---|
17 | 18 | struct bpf_insn prog[] = { |
---|
| 19 | + BPF_LD_MAP_FD(BPF_REG_1, 0), /* percpu map fd */ |
---|
| 20 | + BPF_MOV64_IMM(BPF_REG_2, 0), /* flags, not used */ |
---|
| 21 | + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, |
---|
| 22 | + BPF_FUNC_get_local_storage), |
---|
| 23 | + BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0), |
---|
| 24 | + BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, 0x1), |
---|
| 25 | + BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_3, 0), |
---|
| 26 | + |
---|
18 | 27 | BPF_LD_MAP_FD(BPF_REG_1, 0), /* map fd */ |
---|
19 | 28 | BPF_MOV64_IMM(BPF_REG_2, 0), /* flags, not used */ |
---|
20 | 29 | BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, |
---|
21 | 30 | BPF_FUNC_get_local_storage), |
---|
22 | 31 | BPF_MOV64_IMM(BPF_REG_1, 1), |
---|
23 | 32 | BPF_STX_XADD(BPF_DW, BPF_REG_0, BPF_REG_1, 0), |
---|
24 | | - BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0), |
---|
| 33 | + BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0), |
---|
25 | 34 | BPF_ALU64_IMM(BPF_AND, BPF_REG_1, 0x1), |
---|
26 | 35 | BPF_MOV64_REG(BPF_REG_0, BPF_REG_1), |
---|
27 | 36 | BPF_EXIT_INSN(), |
---|
28 | 37 | }; |
---|
29 | 38 | size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); |
---|
30 | 39 | int error = EXIT_FAILURE; |
---|
31 | | - int map_fd, prog_fd, cgroup_fd; |
---|
| 40 | + int map_fd, percpu_map_fd, prog_fd, cgroup_fd; |
---|
32 | 41 | struct bpf_cgroup_storage_key key; |
---|
33 | 42 | unsigned long long value; |
---|
| 43 | + unsigned long long *percpu_value; |
---|
| 44 | + int cpu, nproc; |
---|
| 45 | + |
---|
| 46 | + nproc = get_nprocs_conf(); |
---|
| 47 | + percpu_value = malloc(sizeof(*percpu_value) * nproc); |
---|
| 48 | + if (!percpu_value) { |
---|
| 49 | + printf("Not enough memory for per-cpu area (%d cpus)\n", nproc); |
---|
| 50 | + goto err; |
---|
| 51 | + } |
---|
34 | 52 | |
---|
35 | 53 | map_fd = bpf_create_map(BPF_MAP_TYPE_CGROUP_STORAGE, sizeof(key), |
---|
36 | 54 | sizeof(value), 0, 0); |
---|
.. | .. |
---|
39 | 57 | goto out; |
---|
40 | 58 | } |
---|
41 | 59 | |
---|
42 | | - prog[0].imm = map_fd; |
---|
| 60 | + percpu_map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE, |
---|
| 61 | + sizeof(key), sizeof(value), 0, 0); |
---|
| 62 | + if (percpu_map_fd < 0) { |
---|
| 63 | + printf("Failed to create map: %s\n", strerror(errno)); |
---|
| 64 | + goto out; |
---|
| 65 | + } |
---|
| 66 | + |
---|
| 67 | + prog[0].imm = percpu_map_fd; |
---|
| 68 | + prog[7].imm = map_fd; |
---|
43 | 69 | prog_fd = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB, |
---|
44 | 70 | prog, insns_cnt, "GPL", 0, |
---|
45 | 71 | bpf_log_buf, BPF_LOG_BUF_SIZE); |
---|
.. | .. |
---|
48 | 74 | goto out; |
---|
49 | 75 | } |
---|
50 | 76 | |
---|
51 | | - if (setup_cgroup_environment()) { |
---|
52 | | - printf("Failed to setup cgroup environment\n"); |
---|
53 | | - goto err; |
---|
54 | | - } |
---|
55 | | - |
---|
56 | | - /* Create a cgroup, get fd, and join it */ |
---|
57 | | - cgroup_fd = create_and_get_cgroup(TEST_CGROUP); |
---|
58 | | - if (!cgroup_fd) { |
---|
59 | | - printf("Failed to create test cgroup\n"); |
---|
60 | | - goto err; |
---|
61 | | - } |
---|
62 | | - |
---|
63 | | - if (join_cgroup(TEST_CGROUP)) { |
---|
64 | | - printf("Failed to join cgroup\n"); |
---|
65 | | - goto err; |
---|
66 | | - } |
---|
| 77 | + cgroup_fd = cgroup_setup_and_join(TEST_CGROUP); |
---|
67 | 78 | |
---|
68 | 79 | /* Attach the bpf program */ |
---|
69 | 80 | if (bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_INET_EGRESS, 0)) { |
---|
.. | .. |
---|
77 | 88 | } |
---|
78 | 89 | |
---|
79 | 90 | if (bpf_map_lookup_elem(map_fd, &key, &value)) { |
---|
80 | | - printf("Failed to lookup cgroup storage\n"); |
---|
| 91 | + printf("Failed to lookup cgroup storage 0\n"); |
---|
| 92 | + goto err; |
---|
| 93 | + } |
---|
| 94 | + |
---|
| 95 | + for (cpu = 0; cpu < nproc; cpu++) |
---|
| 96 | + percpu_value[cpu] = 1000; |
---|
| 97 | + |
---|
| 98 | + if (bpf_map_update_elem(percpu_map_fd, &key, percpu_value, 0)) { |
---|
| 99 | + printf("Failed to update the data in the cgroup storage\n"); |
---|
81 | 100 | goto err; |
---|
82 | 101 | } |
---|
83 | 102 | |
---|
.. | .. |
---|
120 | 139 | goto err; |
---|
121 | 140 | } |
---|
122 | 141 | |
---|
| 142 | + /* Check the final value of the counter in the percpu local storage */ |
---|
| 143 | + |
---|
| 144 | + for (cpu = 0; cpu < nproc; cpu++) |
---|
| 145 | + percpu_value[cpu] = 0; |
---|
| 146 | + |
---|
| 147 | + if (bpf_map_lookup_elem(percpu_map_fd, &key, percpu_value)) { |
---|
| 148 | + printf("Failed to lookup the per-cpu cgroup storage\n"); |
---|
| 149 | + goto err; |
---|
| 150 | + } |
---|
| 151 | + |
---|
| 152 | + value = 0; |
---|
| 153 | + for (cpu = 0; cpu < nproc; cpu++) |
---|
| 154 | + value += percpu_value[cpu]; |
---|
| 155 | + |
---|
| 156 | + if (value != nproc * 1000 + 6) { |
---|
| 157 | + printf("Unexpected data in the per-cpu cgroup storage\n"); |
---|
| 158 | + goto err; |
---|
| 159 | + } |
---|
| 160 | + |
---|
123 | 161 | error = 0; |
---|
124 | 162 | printf("test_cgroup_storage:PASS\n"); |
---|
125 | 163 | |
---|
126 | 164 | err: |
---|
127 | 165 | cleanup_cgroup_environment(); |
---|
| 166 | + free(percpu_value); |
---|
128 | 167 | |
---|
129 | 168 | out: |
---|
130 | 169 | return error; |
---|