summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/prog_tests/arena_htab.c
blob: 0766702de84657460995af4b791374c941d5e285 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>
#include <sys/mman.h>
#include <network_helpers.h>

#include "arena_htab_asm.skel.h"
#include "arena_htab.skel.h"

#define PAGE_SIZE 4096

#include "bpf_arena_htab.h"

static void test_arena_htab_common(struct htab *htab)
{
	int i;

	printf("htab %p buckets %p n_buckets %d\n", htab, htab->buckets, htab->n_buckets);
	ASSERT_OK_PTR(htab->buckets, "htab->buckets shouldn't be NULL");
	for (i = 0; htab->buckets && i < 16; i += 4) {
		/*
		 * Walk htab buckets and link lists since all pointers are correct,
		 * though they were written by bpf program.
		 */
		int val = htab_lookup_elem(htab, i);

		ASSERT_EQ(i, val, "key == value");
	}
}

static void test_arena_htab_llvm(void)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts);
	struct arena_htab *skel;
	struct htab *htab;
	size_t arena_sz;
	void *area;
	int ret;

	skel = arena_htab__open_and_load();
	if (!ASSERT_OK_PTR(skel, "arena_htab__open_and_load"))
		return;

	area = bpf_map__initial_value(skel->maps.arena, &arena_sz);
	/* fault-in a page with pgoff == 0 as sanity check */
	*(volatile int *)area = 0x55aa;

	/* bpf prog will allocate more pages */
	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.arena_htab_llvm), &opts);
	ASSERT_OK(ret, "ret");
	ASSERT_OK(opts.retval, "retval");
	if (skel->bss->skip) {
		printf("%s:SKIP:compiler doesn't support arena_cast\n", __func__);
		test__skip();
		goto out;
	}
	htab = skel->bss->htab_for_user;
	test_arena_htab_common(htab);
out:
	arena_htab__destroy(skel);
}

static void test_arena_htab_asm(void)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts);
	struct arena_htab_asm *skel;
	struct htab *htab;
	int ret;

	skel = arena_htab_asm__open_and_load();
	if (!ASSERT_OK_PTR(skel, "arena_htab_asm__open_and_load"))
		return;

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.arena_htab_asm), &opts);
	ASSERT_OK(ret, "ret");
	ASSERT_OK(opts.retval, "retval");
	htab = skel->bss->htab_for_user;
	test_arena_htab_common(htab);
	arena_htab_asm__destroy(skel);
}

void test_arena_htab(void)
{
	if (test__start_subtest("arena_htab_llvm"))
		test_arena_htab_llvm();
	if (test__start_subtest("arena_htab_asm"))
		test_arena_htab_asm();
}