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