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 7 #include "arena_htab_asm.skel.h" 8 #include "arena_htab.skel.h" 9 10 #define PAGE_SIZE 4096 11 12 #include "bpf_arena_htab.h" 13 14 static void test_arena_htab_common(struct htab *htab) 15 { 16 int i; 17 18 printf("htab %p buckets %p n_buckets %d\n", htab, htab->buckets, htab->n_buckets); 19 ASSERT_OK_PTR(htab->buckets, "htab->buckets shouldn't be NULL"); 20 for (i = 0; htab->buckets && i < 16; i += 4) { 21 /* 22 * Walk htab buckets and link lists since all pointers are correct, 23 * though they were written by bpf program. 24 */ 25 int val = htab_lookup_elem(htab, i); 26 27 ASSERT_EQ(i, val, "key == value"); 28 } 29 } 30 31 static void test_arena_htab_llvm(void) 32 { 33 LIBBPF_OPTS(bpf_test_run_opts, opts); 34 struct arena_htab *skel; 35 struct htab *htab; 36 size_t arena_sz; 37 void *area; 38 int ret; 39 40 skel = arena_htab__open_and_load(); 41 if (!ASSERT_OK_PTR(skel, "arena_htab__open_and_load")) 42 return; 43 44 area = bpf_map__initial_value(skel->maps.arena, &arena_sz); 45 /* fault-in a page with pgoff == 0 as sanity check */ 46 *(volatile int *)area = 0x55aa; 47 48 /* bpf prog will allocate more pages */ 49 ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.arena_htab_llvm), &opts); 50 ASSERT_OK(ret, "ret"); 51 ASSERT_OK(opts.retval, "retval"); 52 if (skel->bss->skip) { 53 printf("%s:SKIP:compiler doesn't support arena_cast\n", __func__); 54 test__skip(); 55 goto out; 56 } 57 htab = skel->bss->htab_for_user; 58 test_arena_htab_common(htab); 59 out: 60 arena_htab__destroy(skel); 61 } 62 63 static void test_arena_htab_asm(void) 64 { 65 LIBBPF_OPTS(bpf_test_run_opts, opts); 66 struct arena_htab_asm *skel; 67 struct htab *htab; 68 int ret; 69 70 skel = arena_htab_asm__open_and_load(); 71 if (!ASSERT_OK_PTR(skel, "arena_htab_asm__open_and_load")) 72 return; 73 74 ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.arena_htab_asm), &opts); 75 ASSERT_OK(ret, "ret"); 76 ASSERT_OK(opts.retval, "retval"); 77 htab = skel->bss->htab_for_user; 78 test_arena_htab_common(htab); 79 arena_htab_asm__destroy(skel); 80 } 81 82 void test_arena_htab(void) 83 { 84 if (test__start_subtest("arena_htab_llvm")) 85 test_arena_htab_llvm(); 86 if (test__start_subtest("arena_htab_asm")) 87 test_arena_htab_asm(); 88 } 89