aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/prog_tests/arena_list.c
blob: d15867cddde06ab5c49bfdca8df1c1f69d0931d9 (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
// 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 <sys/user.h>
#ifndef PAGE_SIZE /* on some archs it comes in sys/user.h */
#include <unistd.h>
#define PAGE_SIZE getpagesize()
#endif

#include "bpf_arena_list.h"
#include "arena_list.skel.h"

struct elem {
	struct arena_list_node node;
	__u64 value;
};

static int list_sum(struct arena_list_head *head)
{
	struct elem __arena *n;
	int sum = 0;

	list_for_each_entry(n, head, node)
		sum += n->value;
	return sum;
}

static void test_arena_list_add_del(int cnt)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts);
	struct arena_list *skel;
	int expected_sum = (u64)cnt * (cnt - 1) / 2;
	int ret, sum;

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

	skel->bss->cnt = cnt;
	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.arena_list_add), &opts);
	ASSERT_OK(ret, "ret_add");
	ASSERT_OK(opts.retval, "retval");
	if (skel->bss->skip) {
		printf("%s:SKIP:compiler doesn't support arena_cast\n", __func__);
		test__skip();
		goto out;
	}
	sum = list_sum(skel->bss->list_head);
	ASSERT_EQ(sum, expected_sum, "sum of elems");
	ASSERT_EQ(skel->arena->arena_sum, expected_sum, "__arena sum of elems");
	ASSERT_EQ(skel->arena->test_val, cnt + 1, "num of elems");

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.arena_list_del), &opts);
	ASSERT_OK(ret, "ret_del");
	sum = list_sum(skel->bss->list_head);
	ASSERT_EQ(sum, 0, "sum of list elems after del");
	ASSERT_EQ(skel->bss->list_sum, expected_sum, "sum of list elems computed by prog");
	ASSERT_EQ(skel->arena->arena_sum, expected_sum, "__arena sum of elems");
out:
	arena_list__destroy(skel);
}

void test_arena_list(void)
{
	if (test__start_subtest("arena_list_1"))
		test_arena_list_add_del(1);
	if (test__start_subtest("arena_list_1000"))
		test_arena_list_add_del(1000);
}