1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Bytedance */
3 
4 #include <test_progs.h>
5 #include "test_map_lookup_percpu_elem.skel.h"
6 
test_map_lookup_percpu_elem(void)7 void test_map_lookup_percpu_elem(void)
8 {
9 	struct test_map_lookup_percpu_elem *skel;
10 	__u64 key = 0, sum;
11 	int ret, i, nr_cpus = libbpf_num_possible_cpus();
12 	__u64 *buf;
13 
14 	buf = malloc(nr_cpus*sizeof(__u64));
15 	if (!ASSERT_OK_PTR(buf, "malloc"))
16 		return;
17 
18 	for (i = 0; i < nr_cpus; i++)
19 		buf[i] = i;
20 	sum = (nr_cpus - 1) * nr_cpus / 2;
21 
22 	skel = test_map_lookup_percpu_elem__open();
23 	if (!ASSERT_OK_PTR(skel, "test_map_lookup_percpu_elem__open"))
24 		goto exit;
25 
26 	skel->rodata->my_pid = getpid();
27 	skel->rodata->nr_cpus = nr_cpus;
28 
29 	ret = test_map_lookup_percpu_elem__load(skel);
30 	if (!ASSERT_OK(ret, "test_map_lookup_percpu_elem__load"))
31 		goto cleanup;
32 
33 	ret = test_map_lookup_percpu_elem__attach(skel);
34 	if (!ASSERT_OK(ret, "test_map_lookup_percpu_elem__attach"))
35 		goto cleanup;
36 
37 	ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_array_map), &key, buf, 0);
38 	ASSERT_OK(ret, "percpu_array_map update");
39 
40 	ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_hash_map), &key, buf, 0);
41 	ASSERT_OK(ret, "percpu_hash_map update");
42 
43 	ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_lru_hash_map), &key, buf, 0);
44 	ASSERT_OK(ret, "percpu_lru_hash_map update");
45 
46 	syscall(__NR_getuid);
47 
48 	test_map_lookup_percpu_elem__detach(skel);
49 
50 	ASSERT_EQ(skel->bss->percpu_array_elem_sum, sum, "percpu_array lookup percpu elem");
51 	ASSERT_EQ(skel->bss->percpu_hash_elem_sum, sum, "percpu_hash lookup percpu elem");
52 	ASSERT_EQ(skel->bss->percpu_lru_hash_elem_sum, sum, "percpu_lru_hash lookup percpu elem");
53 
54 cleanup:
55 	test_map_lookup_percpu_elem__destroy(skel);
56 exit:
57 	free(buf);
58 }
59