1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ |
3 | #include <test_progs.h> |
4 | #include <sys/mman.h> |
5 | #include <network_helpers.h> |
6 | #include <sys/user.h> |
7 | #ifndef PAGE_SIZE /* on some archs it comes in sys/user.h */ |
8 | #include <unistd.h> |
9 | #define PAGE_SIZE getpagesize() |
10 | #endif |
11 | |
12 | #include "bpf_arena_list.h" |
13 | #include "arena_list.skel.h" |
14 | |
15 | struct elem { |
16 | struct arena_list_node node; |
17 | __u64 value; |
18 | }; |
19 | |
20 | static int list_sum(struct arena_list_head *head) |
21 | { |
22 | struct elem __arena *n; |
23 | int sum = 0; |
24 | |
25 | list_for_each_entry(n, head, node) |
26 | sum += n->value; |
27 | return sum; |
28 | } |
29 | |
30 | static void test_arena_list_add_del(int cnt) |
31 | { |
32 | LIBBPF_OPTS(bpf_test_run_opts, opts); |
33 | struct arena_list *skel; |
34 | int expected_sum = (u64)cnt * (cnt - 1) / 2; |
35 | int ret, sum; |
36 | |
37 | skel = arena_list__open_and_load(); |
38 | if (!ASSERT_OK_PTR(skel, "arena_list__open_and_load" )) |
39 | return; |
40 | |
41 | skel->bss->cnt = cnt; |
42 | ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.arena_list_add), &opts); |
43 | ASSERT_OK(ret, "ret_add" ); |
44 | ASSERT_OK(opts.retval, "retval" ); |
45 | if (skel->bss->skip) { |
46 | printf("%s:SKIP:compiler doesn't support arena_cast\n" , __func__); |
47 | test__skip(); |
48 | goto out; |
49 | } |
50 | sum = list_sum(head: skel->bss->list_head); |
51 | ASSERT_EQ(sum, expected_sum, "sum of elems" ); |
52 | ASSERT_EQ(skel->arena->arena_sum, expected_sum, "__arena sum of elems" ); |
53 | ASSERT_EQ(skel->arena->test_val, cnt + 1, "num of elems" ); |
54 | |
55 | ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.arena_list_del), &opts); |
56 | ASSERT_OK(ret, "ret_del" ); |
57 | sum = list_sum(head: skel->bss->list_head); |
58 | ASSERT_EQ(sum, 0, "sum of list elems after del" ); |
59 | ASSERT_EQ(skel->bss->list_sum, expected_sum, "sum of list elems computed by prog" ); |
60 | ASSERT_EQ(skel->arena->arena_sum, expected_sum, "__arena sum of elems" ); |
61 | out: |
62 | arena_list__destroy(skel); |
63 | } |
64 | |
65 | void test_arena_list(void) |
66 | { |
67 | if (test__start_subtest("arena_list_1" )) |
68 | test_arena_list_add_del(cnt: 1); |
69 | if (test__start_subtest("arena_list_1000" )) |
70 | test_arena_list_add_del(cnt: 1000); |
71 | } |
72 | |