1 // SPDX-License-Identifier: GPL-2.0
2 #include <assert.h>
3 #include <bpf/bpf.h>
4 #include <linux/filter.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/sysinfo.h>
8 
9 #include "bpf_rlimit.h"
10 #include "cgroup_helpers.h"
11 #include "testing_helpers.h"
12 
13 char bpf_log_buf[BPF_LOG_BUF_SIZE];
14 
15 #define TEST_CGROUP "/test-bpf-cgroup-storage-buf/"
16 
17 int main(int argc, char **argv)
18 {
19 	struct bpf_insn prog[] = {
20 		BPF_LD_MAP_FD(BPF_REG_1, 0), /* percpu map fd */
21 		BPF_MOV64_IMM(BPF_REG_2, 0), /* flags, not used */
22 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
23 			     BPF_FUNC_get_local_storage),
24 		BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0),
25 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, 0x1),
26 		BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_3, 0),
27 
28 		BPF_LD_MAP_FD(BPF_REG_1, 0), /* map fd */
29 		BPF_MOV64_IMM(BPF_REG_2, 0), /* flags, not used */
30 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
31 			     BPF_FUNC_get_local_storage),
32 		BPF_MOV64_IMM(BPF_REG_1, 1),
33 		BPF_ATOMIC_OP(BPF_DW, BPF_ADD, BPF_REG_0, BPF_REG_1, 0),
34 		BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0),
35 		BPF_ALU64_IMM(BPF_AND, BPF_REG_1, 0x1),
36 		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
37 		BPF_EXIT_INSN(),
38 	};
39 	size_t insns_cnt = ARRAY_SIZE(prog);
40 	int error = EXIT_FAILURE;
41 	int map_fd, percpu_map_fd, prog_fd, cgroup_fd;
42 	struct bpf_cgroup_storage_key key;
43 	unsigned long long value;
44 	unsigned long long *percpu_value;
45 	int cpu, nproc;
46 
47 	nproc = get_nprocs_conf();
48 	percpu_value = malloc(sizeof(*percpu_value) * nproc);
49 	if (!percpu_value) {
50 		printf("Not enough memory for per-cpu area (%d cpus)\n", nproc);
51 		goto err;
52 	}
53 
54 	map_fd = bpf_map_create(BPF_MAP_TYPE_CGROUP_STORAGE, NULL, sizeof(key),
55 				sizeof(value), 0, NULL);
56 	if (map_fd < 0) {
57 		printf("Failed to create map: %s\n", strerror(errno));
58 		goto out;
59 	}
60 
61 	percpu_map_fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE, NULL,
62 				       sizeof(key), sizeof(value), 0, NULL);
63 	if (percpu_map_fd < 0) {
64 		printf("Failed to create map: %s\n", strerror(errno));
65 		goto out;
66 	}
67 
68 	prog[0].imm = percpu_map_fd;
69 	prog[7].imm = map_fd;
70 	prog_fd = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
71 				   prog, insns_cnt, "GPL", 0,
72 				   bpf_log_buf, BPF_LOG_BUF_SIZE);
73 	if (prog_fd < 0) {
74 		printf("Failed to load bpf program: %s\n", bpf_log_buf);
75 		goto out;
76 	}
77 
78 	cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
79 
80 	/* Attach the bpf program */
81 	if (bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_INET_EGRESS, 0)) {
82 		printf("Failed to attach bpf program\n");
83 		goto err;
84 	}
85 
86 	if (bpf_map_get_next_key(map_fd, NULL, &key)) {
87 		printf("Failed to get the first key in cgroup storage\n");
88 		goto err;
89 	}
90 
91 	if (bpf_map_lookup_elem(map_fd, &key, &value)) {
92 		printf("Failed to lookup cgroup storage 0\n");
93 		goto err;
94 	}
95 
96 	for (cpu = 0; cpu < nproc; cpu++)
97 		percpu_value[cpu] = 1000;
98 
99 	if (bpf_map_update_elem(percpu_map_fd, &key, percpu_value, 0)) {
100 		printf("Failed to update the data in the cgroup storage\n");
101 		goto err;
102 	}
103 
104 	/* Every second packet should be dropped */
105 	assert(system("ping localhost -c 1 -W 1 -q > /dev/null") == 0);
106 	assert(system("ping localhost -c 1 -W 1 -q > /dev/null"));
107 	assert(system("ping localhost -c 1 -W 1 -q > /dev/null") == 0);
108 
109 	/* Check the counter in the cgroup local storage */
110 	if (bpf_map_lookup_elem(map_fd, &key, &value)) {
111 		printf("Failed to lookup cgroup storage\n");
112 		goto err;
113 	}
114 
115 	if (value != 3) {
116 		printf("Unexpected data in the cgroup storage: %llu\n", value);
117 		goto err;
118 	}
119 
120 	/* Bump the counter in the cgroup local storage */
121 	value++;
122 	if (bpf_map_update_elem(map_fd, &key, &value, 0)) {
123 		printf("Failed to update the data in the cgroup storage\n");
124 		goto err;
125 	}
126 
127 	/* Every second packet should be dropped */
128 	assert(system("ping localhost -c 1 -W 1 -q > /dev/null") == 0);
129 	assert(system("ping localhost -c 1 -W 1 -q > /dev/null"));
130 	assert(system("ping localhost -c 1 -W 1 -q > /dev/null") == 0);
131 
132 	/* Check the final value of the counter in the cgroup local storage */
133 	if (bpf_map_lookup_elem(map_fd, &key, &value)) {
134 		printf("Failed to lookup the cgroup storage\n");
135 		goto err;
136 	}
137 
138 	if (value != 7) {
139 		printf("Unexpected data in the cgroup storage: %llu\n", value);
140 		goto err;
141 	}
142 
143 	/* Check the final value of the counter in the percpu local storage */
144 
145 	for (cpu = 0; cpu < nproc; cpu++)
146 		percpu_value[cpu] = 0;
147 
148 	if (bpf_map_lookup_elem(percpu_map_fd, &key, percpu_value)) {
149 		printf("Failed to lookup the per-cpu cgroup storage\n");
150 		goto err;
151 	}
152 
153 	value = 0;
154 	for (cpu = 0; cpu < nproc; cpu++)
155 		value += percpu_value[cpu];
156 
157 	if (value != nproc * 1000 + 6) {
158 		printf("Unexpected data in the per-cpu cgroup storage\n");
159 		goto err;
160 	}
161 
162 	error = 0;
163 	printf("test_cgroup_storage:PASS\n");
164 
165 err:
166 	cleanup_cgroup_environment();
167 	free(percpu_value);
168 
169 out:
170 	return error;
171 }
172