xref: /linux/tools/testing/selftests/bpf/prog_tests/map_kptr.c (revision 0d8eae7b124e2ddaee00f186fe922450faad0ed7)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 
5 #include "map_kptr.skel.h"
6 #include "map_kptr_fail.skel.h"
7 
8 static void test_map_kptr_success(bool test_run)
9 {
10 	LIBBPF_OPTS(bpf_test_run_opts, opts,
11 		.data_in = &pkt_v4,
12 		.data_size_in = sizeof(pkt_v4),
13 		.repeat = 1,
14 	);
15 	struct map_kptr *skel;
16 	int key = 0, ret;
17 	char buf[16];
18 
19 	skel = map_kptr__open_and_load();
20 	if (!ASSERT_OK_PTR(skel, "map_kptr__open_and_load"))
21 		return;
22 
23 	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.test_map_kptr_ref), &opts);
24 	ASSERT_OK(ret, "test_map_kptr_ref refcount");
25 	ASSERT_OK(opts.retval, "test_map_kptr_ref retval");
26 	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.test_map_kptr_ref2), &opts);
27 	ASSERT_OK(ret, "test_map_kptr_ref2 refcount");
28 	ASSERT_OK(opts.retval, "test_map_kptr_ref2 retval");
29 
30 	if (test_run)
31 		goto exit;
32 
33 	ret = bpf_map__update_elem(skel->maps.array_map,
34 				   &key, sizeof(key), buf, sizeof(buf), 0);
35 	ASSERT_OK(ret, "array_map update");
36 	ret = bpf_map__update_elem(skel->maps.array_map,
37 				   &key, sizeof(key), buf, sizeof(buf), 0);
38 	ASSERT_OK(ret, "array_map update2");
39 
40 	ret = bpf_map__update_elem(skel->maps.hash_map,
41 				   &key, sizeof(key), buf, sizeof(buf), 0);
42 	ASSERT_OK(ret, "hash_map update");
43 	ret = bpf_map__delete_elem(skel->maps.hash_map, &key, sizeof(key), 0);
44 	ASSERT_OK(ret, "hash_map delete");
45 
46 	ret = bpf_map__update_elem(skel->maps.hash_malloc_map,
47 				   &key, sizeof(key), buf, sizeof(buf), 0);
48 	ASSERT_OK(ret, "hash_malloc_map update");
49 	ret = bpf_map__delete_elem(skel->maps.hash_malloc_map, &key, sizeof(key), 0);
50 	ASSERT_OK(ret, "hash_malloc_map delete");
51 
52 	ret = bpf_map__update_elem(skel->maps.lru_hash_map,
53 				   &key, sizeof(key), buf, sizeof(buf), 0);
54 	ASSERT_OK(ret, "lru_hash_map update");
55 	ret = bpf_map__delete_elem(skel->maps.lru_hash_map, &key, sizeof(key), 0);
56 	ASSERT_OK(ret, "lru_hash_map delete");
57 
58 exit:
59 	map_kptr__destroy(skel);
60 }
61 
62 void test_map_kptr(void)
63 {
64 	if (test__start_subtest("success")) {
65 		test_map_kptr_success(false);
66 		/* Do test_run twice, so that we see refcount going back to 1
67 		 * after we leave it in map from first iteration.
68 		 */
69 		test_map_kptr_success(true);
70 	}
71 
72 	RUN_TESTS(map_kptr_fail);
73 }
74