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()
11 #include "arena_htab_asm.skel.h"
12 #include "arena_htab.skel.h"
14 #include "bpf_arena_htab.h"
16 static void test_arena_htab_common(struct htab *htab)
20 printf("htab %p buckets %p n_buckets %d\n", htab, htab->buckets, htab->n_buckets);
21 ASSERT_OK_PTR(htab->buckets, "htab->buckets shouldn't be NULL");
22 for (i = 0; htab->buckets && i < 16; i += 4) {
24 * Walk htab buckets and link lists since all pointers are correct,
25 * though they were written by bpf program.
27 int val = htab_lookup_elem(htab, i);
29 ASSERT_EQ(i, val, "key == value");
33 static void test_arena_htab_llvm(void)
35 LIBBPF_OPTS(bpf_test_run_opts, opts);
36 struct arena_htab *skel;
42 skel = arena_htab__open_and_load();
43 if (!ASSERT_OK_PTR(skel, "arena_htab__open_and_load"))
46 area = bpf_map__initial_value(skel->maps.arena, &arena_sz);
47 /* fault-in a page with pgoff == 0 as sanity check */
48 *(volatile int *)area = 0x55aa;
50 /* bpf prog will allocate more pages */
51 ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.arena_htab_llvm), &opts);
52 ASSERT_OK(ret, "ret");
53 ASSERT_OK(opts.retval, "retval");
54 if (skel->bss->skip) {
55 printf("%s:SKIP:compiler doesn't support arena_cast\n", __func__);
59 htab = skel->bss->htab_for_user;
60 test_arena_htab_common(htab);
62 arena_htab__destroy(skel);
65 static void test_arena_htab_asm(void)
67 LIBBPF_OPTS(bpf_test_run_opts, opts);
68 struct arena_htab_asm *skel;
72 skel = arena_htab_asm__open_and_load();
73 if (!ASSERT_OK_PTR(skel, "arena_htab_asm__open_and_load"))
76 ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.arena_htab_asm), &opts);
77 ASSERT_OK(ret, "ret");
78 ASSERT_OK(opts.retval, "retval");
79 htab = skel->bss->htab_for_user;
80 test_arena_htab_common(htab);
81 arena_htab_asm__destroy(skel);
84 void test_arena_htab(void)
86 if (test__start_subtest("arena_htab_llvm"))
87 test_arena_htab_llvm();
88 if (test__start_subtest("arena_htab_asm"))
89 test_arena_htab_asm();