xref: /linux/tools/testing/selftests/bpf/prog_tests/map_init.c (revision 7732ad2412fd402913976e490921f7e792a0a33b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Tessares SA <http://www.tessares.net> */
3 
4 #include <test_progs.h>
5 #include "test_map_init.skel.h"
6 
7 #define TEST_VALUE 0x1234
8 #define FILL_VALUE 0xdeadbeef
9 
10 static int nr_cpus;
11 static int duration;
12 
13 typedef unsigned long long map_key_t;
14 typedef unsigned long long map_value_t;
15 typedef struct {
16 	map_value_t v; /* padding */
17 } __bpf_percpu_val_align pcpu_map_value_t;
18 
19 
20 static int map_populate(int map_fd, int num)
21 {
22 	pcpu_map_value_t value[nr_cpus];
23 	int i, err;
24 	map_key_t key;
25 
26 	for (i = 0; i < nr_cpus; i++)
27 		bpf_percpu(value, i) = FILL_VALUE;
28 
29 	for (key = 1; key <= num; key++) {
30 		err = bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST);
31 		if (!ASSERT_OK(err, "bpf_map_update_elem"))
32 			return -1;
33 	}
34 
35 	return 0;
36 }
37 
38 static struct test_map_init *setup(enum bpf_map_type map_type, int map_sz,
39 			    int *map_fd, int populate)
40 {
41 	struct test_map_init *skel;
42 	int err;
43 
44 	skel = test_map_init__open();
45 	if (!ASSERT_OK_PTR(skel, "skel_open"))
46 		return NULL;
47 
48 	err = bpf_map__set_type(skel->maps.hashmap1, map_type);
49 	if (!ASSERT_OK(err, "bpf_map__set_type"))
50 		goto error;
51 
52 	err = bpf_map__set_max_entries(skel->maps.hashmap1, map_sz);
53 	if (!ASSERT_OK(err, "bpf_map__set_max_entries"))
54 		goto error;
55 
56 	err = test_map_init__load(skel);
57 	if (!ASSERT_OK(err, "skel_load"))
58 		goto error;
59 
60 	*map_fd = bpf_map__fd(skel->maps.hashmap1);
61 	if (CHECK(*map_fd < 0, "bpf_map__fd", "failed\n"))
62 		goto error;
63 
64 	err = map_populate(*map_fd, populate);
65 	if (!ASSERT_OK(err, "map_populate"))
66 		goto error_map;
67 
68 	return skel;
69 
70 error_map:
71 	close(*map_fd);
72 error:
73 	test_map_init__destroy(skel);
74 	return NULL;
75 }
76 
77 /* executes bpf program that updates map with key, value */
78 static int prog_run_insert_elem(struct test_map_init *skel, map_key_t key,
79 				map_value_t value)
80 {
81 	struct test_map_init__bss *bss;
82 
83 	bss = skel->bss;
84 
85 	bss->inKey = key;
86 	bss->inValue = value;
87 	bss->inPid = getpid();
88 
89 	if (!ASSERT_OK(test_map_init__attach(skel), "skel_attach"))
90 		return -1;
91 
92 	/* Let tracepoint trigger */
93 	syscall(__NR_getpgid);
94 
95 	test_map_init__detach(skel);
96 
97 	return 0;
98 }
99 
100 static int check_values_one_cpu(pcpu_map_value_t *value, map_value_t expected)
101 {
102 	int i, nzCnt = 0;
103 	map_value_t val;
104 
105 	for (i = 0; i < nr_cpus; i++) {
106 		val = bpf_percpu(value, i);
107 		if (val) {
108 			if (CHECK(val != expected, "map value",
109 				  "unexpected for cpu %d: 0x%llx\n", i, val))
110 				return -1;
111 			nzCnt++;
112 		}
113 	}
114 
115 	if (CHECK(nzCnt != 1, "map value", "set for %d CPUs instead of 1!\n",
116 		  nzCnt))
117 		return -1;
118 
119 	return 0;
120 }
121 
122 /* Add key=1 elem with values set for all CPUs
123  * Delete elem key=1
124  * Run bpf prog that inserts new key=1 elem with value=0x1234
125  *   (bpf prog can only set value for current CPU)
126  * Lookup Key=1 and check value is as expected for all CPUs:
127  *   value set by bpf prog for one CPU, 0 for all others
128  */
129 static void test_pcpu_map_init(void)
130 {
131 	pcpu_map_value_t value[nr_cpus];
132 	struct test_map_init *skel;
133 	int map_fd, err;
134 	map_key_t key;
135 
136 	/* max 1 elem in map so insertion is forced to reuse freed entry */
137 	skel = setup(BPF_MAP_TYPE_PERCPU_HASH, 1, &map_fd, 1);
138 	if (!ASSERT_OK_PTR(skel, "prog_setup"))
139 		return;
140 
141 	/* delete element so the entry can be re-used*/
142 	key = 1;
143 	err = bpf_map_delete_elem(map_fd, &key);
144 	if (!ASSERT_OK(err, "bpf_map_delete_elem"))
145 		goto cleanup;
146 
147 	/* run bpf prog that inserts new elem, re-using the slot just freed */
148 	err = prog_run_insert_elem(skel, key, TEST_VALUE);
149 	if (!ASSERT_OK(err, "prog_run_insert_elem"))
150 		goto cleanup;
151 
152 	/* check that key=1 was re-created by bpf prog */
153 	err = bpf_map_lookup_elem(map_fd, &key, value);
154 	if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
155 		goto cleanup;
156 
157 	/* and has expected values */
158 	check_values_one_cpu(value, TEST_VALUE);
159 
160 cleanup:
161 	test_map_init__destroy(skel);
162 }
163 
164 /* Add key=1 and key=2 elems with values set for all CPUs
165  * Run bpf prog that inserts new key=3 elem
166  *   (only for current cpu; other cpus should have initial value = 0)
167  * Lookup Key=1 and check value is as expected for all CPUs
168  */
169 static void test_pcpu_lru_map_init(void)
170 {
171 	pcpu_map_value_t value[nr_cpus];
172 	struct test_map_init *skel;
173 	int map_fd, err;
174 	map_key_t key;
175 
176 	/* Set up LRU map with 2 elements, values filled for all CPUs.
177 	 * With these 2 elements, the LRU map is full
178 	 */
179 	skel = setup(BPF_MAP_TYPE_LRU_PERCPU_HASH, 2, &map_fd, 2);
180 	if (!ASSERT_OK_PTR(skel, "prog_setup"))
181 		return;
182 
183 	/* run bpf prog that inserts new key=3 element, re-using LRU slot */
184 	key = 3;
185 	err = prog_run_insert_elem(skel, key, TEST_VALUE);
186 	if (!ASSERT_OK(err, "prog_run_insert_elem"))
187 		goto cleanup;
188 
189 	/* check that key=3 replaced one of earlier elements */
190 	err = bpf_map_lookup_elem(map_fd, &key, value);
191 	if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
192 		goto cleanup;
193 
194 	/* and has expected values */
195 	check_values_one_cpu(value, TEST_VALUE);
196 
197 cleanup:
198 	test_map_init__destroy(skel);
199 }
200 
201 void test_map_init(void)
202 {
203 	nr_cpus = bpf_num_possible_cpus();
204 	if (nr_cpus <= 1) {
205 		printf("%s:SKIP: >1 cpu needed for this test\n", __func__);
206 		test__skip();
207 		return;
208 	}
209 
210 	if (test__start_subtest("pcpu_map_init"))
211 		test_pcpu_map_init();
212 	if (test__start_subtest("pcpu_lru_map_init"))
213 		test_pcpu_lru_map_init();
214 }
215 
216 static void test_map_create(enum bpf_map_type map_type, const char *map_name,
217 			    struct bpf_map_create_opts *opts, const char *exp_msg)
218 {
219 	const int key_size = 4, value_size = 4, max_entries = 1;
220 	char log_buf[128];
221 	int fd;
222 	LIBBPF_OPTS(bpf_log_opts, log_opts);
223 
224 	log_buf[0] = '\0';
225 	log_opts.buf = log_buf;
226 	log_opts.size = sizeof(log_buf);
227 	log_opts.level = 1;
228 	opts->log_opts = &log_opts;
229 	fd = bpf_map_create(map_type, map_name, key_size, value_size, max_entries, opts);
230 	if (!ASSERT_LT(fd, 0, "bpf_map_create")) {
231 		close(fd);
232 		return;
233 	}
234 
235 	ASSERT_STREQ(log_buf, exp_msg, "log_buf");
236 	ASSERT_EQ(log_opts.true_size, strlen(exp_msg) + 1, "true_size");
237 }
238 
239 static void test_map_create_array(struct bpf_map_create_opts *opts, const char *exp_msg)
240 {
241 	test_map_create(BPF_MAP_TYPE_ARRAY, "test_map_create", opts, exp_msg);
242 }
243 
244 static void test_invalid_vmlinux_value_type_id_struct_ops(void)
245 {
246 	const char *msg = "btf_vmlinux_value_type_id can only be used with struct_ops maps.\n";
247 	LIBBPF_OPTS(bpf_map_create_opts, opts,
248 		    .btf_vmlinux_value_type_id = 1,
249 	);
250 
251 	test_map_create_array(&opts, msg);
252 }
253 
254 static void test_invalid_vmlinux_value_type_id_kv_type_id(void)
255 {
256 	const char *msg = "btf_vmlinux_value_type_id is mutually exclusive with btf_key_type_id and btf_value_type_id.\n";
257 	LIBBPF_OPTS(bpf_map_create_opts, opts,
258 		    .btf_vmlinux_value_type_id = 1,
259 		    .btf_key_type_id = 1,
260 	);
261 
262 	test_map_create(BPF_MAP_TYPE_STRUCT_OPS, "test_map_create", &opts, msg);
263 }
264 
265 static void test_invalid_value_type_id(void)
266 {
267 	const char *msg = "Invalid btf_value_type_id.\n";
268 	LIBBPF_OPTS(bpf_map_create_opts, opts,
269 		    .btf_key_type_id = 1,
270 	);
271 
272 	test_map_create_array(&opts, msg);
273 }
274 
275 static void test_invalid_map_extra(void)
276 {
277 	const char *msg = "Invalid map_extra.\n";
278 	LIBBPF_OPTS(bpf_map_create_opts, opts,
279 		    .map_extra = 1,
280 	);
281 
282 	test_map_create_array(&opts, msg);
283 }
284 
285 static void test_invalid_numa_node(void)
286 {
287 	const char *msg = "Invalid numa_node.\n";
288 	LIBBPF_OPTS(bpf_map_create_opts, opts,
289 		    .map_flags = BPF_F_NUMA_NODE,
290 		    .numa_node = 0xFF,
291 	);
292 
293 	test_map_create_array(&opts, msg);
294 }
295 
296 static void test_invalid_map_type(void)
297 {
298 	const char *msg = "Invalid map_type.\n";
299 	LIBBPF_OPTS(bpf_map_create_opts, opts);
300 
301 	test_map_create(__MAX_BPF_MAP_TYPE, "test_map_create", &opts, msg);
302 }
303 
304 static void test_invalid_token_fd(void)
305 {
306 	const char *msg = "Invalid map_token_fd.\n";
307 	LIBBPF_OPTS(bpf_map_create_opts, opts,
308 		    .map_flags = BPF_F_TOKEN_FD,
309 		    .token_fd = -1,
310 	);
311 
312 	test_map_create_array(&opts, msg);
313 }
314 
315 static void test_invalid_map_name(void)
316 {
317 	const char *msg = "Invalid map_name.\n";
318 	LIBBPF_OPTS(bpf_map_create_opts, opts);
319 
320 	test_map_create(BPF_MAP_TYPE_ARRAY, "test-!@#", &opts, msg);
321 }
322 
323 static void test_invalid_btf_fd(void)
324 {
325 	const char *msg = "Invalid btf_fd.\n";
326 	LIBBPF_OPTS(bpf_map_create_opts, opts,
327 		    .btf_fd = -1,
328 		    .btf_key_type_id = 1,
329 		    .btf_value_type_id = 1,
330 	);
331 
332 	test_map_create_array(&opts, msg);
333 }
334 
335 static void test_excl_prog_hash_size_1(void)
336 {
337 	const char *msg = "Invalid excl_prog_hash_size.\n";
338 	const char *hash = "DEADCODE";
339 	LIBBPF_OPTS(bpf_map_create_opts, opts,
340 		    .excl_prog_hash = hash,
341 	);
342 
343 	test_map_create_array(&opts, msg);
344 }
345 
346 static void test_excl_prog_hash_size_2(void)
347 {
348 	const char *msg = "Invalid excl_prog_hash_size.\n";
349 	LIBBPF_OPTS(bpf_map_create_opts, opts,
350 		    .excl_prog_hash_size = 1,
351 	);
352 
353 	test_map_create_array(&opts, msg);
354 }
355 
356 static void test_common_attr_padding(void)
357 {
358 	struct bpf_common_attr_fake {
359 		__u8 attrs[offsetofend(struct bpf_common_attr, log_true_size)];
360 		__u32 pad;
361 	} attr_common = {
362 		.pad = 1,
363 	};
364 	union bpf_attr attr = {
365 		.map_type    = BPF_MAP_TYPE_ARRAY,
366 		.key_size    = 4,
367 		.value_size  = 4,
368 		.max_entries = 1,
369 	};
370 	int fd;
371 
372 	fd = syscall(__NR_bpf, BPF_MAP_CREATE | BPF_COMMON_ATTRS, &attr, sizeof(attr), &attr_common,
373 		     sizeof(attr_common));
374 	if (!ASSERT_LT(fd, 0, "syscall"))
375 		close(fd);
376 	else
377 		ASSERT_EQ(errno, E2BIG, "errno");
378 }
379 
380 void test_map_create_failure(void)
381 {
382 	if (test__start_subtest("invalid_vmlinux_value_type_id_struct_ops"))
383 		test_invalid_vmlinux_value_type_id_struct_ops();
384 	if (test__start_subtest("invalid_vmlinux_value_type_id_kv_type_id"))
385 		test_invalid_vmlinux_value_type_id_kv_type_id();
386 	if (test__start_subtest("invalid_value_type_id"))
387 		test_invalid_value_type_id();
388 	if (test__start_subtest("invalid_map_extra"))
389 		test_invalid_map_extra();
390 	if (test__start_subtest("invalid_numa_node"))
391 		test_invalid_numa_node();
392 	if (test__start_subtest("invalid_map_type"))
393 		test_invalid_map_type();
394 	if (test__start_subtest("invalid_token_fd"))
395 		test_invalid_token_fd();
396 	if (test__start_subtest("invalid_map_name"))
397 		test_invalid_map_name();
398 	if (test__start_subtest("invalid_btf_fd"))
399 		test_invalid_btf_fd();
400 	if (test__start_subtest("invalid_excl_prog_hash_size_1"))
401 		test_excl_prog_hash_size_1();
402 	if (test__start_subtest("invalid_excl_prog_hash_size_2"))
403 		test_excl_prog_hash_size_2();
404 	if (test__start_subtest("common_attr_padding"))
405 		test_common_attr_padding();
406 }
407