1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3 #include <test_progs.h>
5 #include <network_helpers.h>
7 #ifndef PAGE_SIZE /* on some archs it comes in sys/user.h */
9 #define PAGE_SIZE getpagesize()
12 #include "bpf_arena_list.h"
13 #include "arena_list.skel.h"
16 struct arena_list_node node;
20 static int list_sum(struct arena_list_head *head)
22 struct elem __arena *n;
25 list_for_each_entry(n, head, node)
30 static void test_arena_list_add_del(int cnt)
32 LIBBPF_OPTS(bpf_test_run_opts, opts);
33 struct arena_list *skel;
34 int expected_sum = (u64)cnt * (cnt - 1) / 2;
37 skel = arena_list__open_and_load();
38 if (!ASSERT_OK_PTR(skel, "arena_list__open_and_load"))
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__);
50 sum = list_sum(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");
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(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");
62 arena_list__destroy(skel);
65 void test_arena_list(void)
67 if (test__start_subtest("arena_list_1"))
68 test_arena_list_add_del(1);
69 if (test__start_subtest("arena_list_1000"))
70 test_arena_list_add_del(1000);