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 #include "arena_htab_asm.skel.h" 12 #include "arena_htab.skel.h" 13 14 #include "bpf_arena_htab.h" 15 16 static void test_arena_htab_common(struct htab *htab) 17 { 18 int i; 19 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) { 23 /* 24 * Walk htab buckets and link lists since all pointers are correct, 25 * though they were written by bpf program. 26 */ 27 int val = htab_lookup_elem(htab, i); 28 29 ASSERT_EQ(i, val, "key == value"); 30 } 31 } 32 33 static void test_arena_htab_llvm(void) 34 { 35 LIBBPF_OPTS(bpf_test_run_opts, opts); 36 struct arena_htab *skel; 37 struct htab *htab; 38 size_t arena_sz; 39 void *area; 40 int ret; 41 42 skel = arena_htab__open_and_load(); 43 if (!ASSERT_OK_PTR(skel, "arena_htab__open_and_load")) 44 return; 45 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; 49 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__); 56 test__skip(); 57 goto out; 58 } 59 htab = skel->bss->htab_for_user; 60 test_arena_htab_common(htab); 61 out: 62 arena_htab__destroy(skel); 63 } 64 65 static void test_arena_htab_asm(void) 66 { 67 LIBBPF_OPTS(bpf_test_run_opts, opts); 68 struct arena_htab_asm *skel; 69 struct htab *htab; 70 int ret; 71 72 skel = arena_htab_asm__open_and_load(); 73 if (!ASSERT_OK_PTR(skel, "arena_htab_asm__open_and_load")) 74 return; 75 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); 82 } 83 84 void test_arena_htab(void) 85 { 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(); 90 } 91