summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/local_kptr_stash.c
blob: 827e713f6cf1e5ab20e390d218058fdbd1baed7c (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */

#include <test_progs.h>
#include <network_helpers.h>

#include "local_kptr_stash.skel.h"
#include "local_kptr_stash_fail.skel.h"
static void test_local_kptr_stash_simple(void)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts,
		    .data_in = &pkt_v4,
		    .data_size_in = sizeof(pkt_v4),
		    .repeat = 1,
	);
	struct local_kptr_stash *skel;
	int ret;

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

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.stash_rb_nodes), &opts);
	ASSERT_OK(ret, "local_kptr_stash_add_nodes run");
	ASSERT_OK(opts.retval, "local_kptr_stash_add_nodes retval");

	local_kptr_stash__destroy(skel);
}

static void test_local_kptr_stash_plain(void)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts,
		    .data_in = &pkt_v4,
		    .data_size_in = sizeof(pkt_v4),
		    .repeat = 1,
	);
	struct local_kptr_stash *skel;
	int ret;

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

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.stash_plain), &opts);
	ASSERT_OK(ret, "local_kptr_stash_add_plain run");
	ASSERT_OK(opts.retval, "local_kptr_stash_add_plain retval");

	local_kptr_stash__destroy(skel);
}

static void test_local_kptr_stash_local_with_root(void)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts,
		    .data_in = &pkt_v4,
		    .data_size_in = sizeof(pkt_v4),
		    .repeat = 1,
	);
	struct local_kptr_stash *skel;
	int ret;

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

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.stash_local_with_root), &opts);
	ASSERT_OK(ret, "local_kptr_stash_add_local_with_root run");
	ASSERT_OK(opts.retval, "local_kptr_stash_add_local_with_root retval");

	local_kptr_stash__destroy(skel);
}

static void test_local_kptr_stash_unstash(void)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts,
		    .data_in = &pkt_v4,
		    .data_size_in = sizeof(pkt_v4),
		    .repeat = 1,
	);
	struct local_kptr_stash *skel;
	int ret;

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

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.stash_rb_nodes), &opts);
	ASSERT_OK(ret, "local_kptr_stash_add_nodes run");
	ASSERT_OK(opts.retval, "local_kptr_stash_add_nodes retval");

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.unstash_rb_node), &opts);
	ASSERT_OK(ret, "local_kptr_stash_add_nodes run");
	ASSERT_EQ(opts.retval, 42, "local_kptr_stash_add_nodes retval");

	local_kptr_stash__destroy(skel);
}

static void test_refcount_acquire_without_unstash(void)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts,
		    .data_in = &pkt_v4,
		    .data_size_in = sizeof(pkt_v4),
		    .repeat = 1,
	);
	struct local_kptr_stash *skel;
	int ret;

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

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.refcount_acquire_without_unstash),
				     &opts);
	ASSERT_OK(ret, "refcount_acquire_without_unstash run");
	ASSERT_EQ(opts.retval, 2, "refcount_acquire_without_unstash retval");

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.stash_refcounted_node), &opts);
	ASSERT_OK(ret, "stash_refcounted_node run");
	ASSERT_OK(opts.retval, "stash_refcounted_node retval");

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.refcount_acquire_without_unstash),
				     &opts);
	ASSERT_OK(ret, "refcount_acquire_without_unstash (2) run");
	ASSERT_EQ(opts.retval, 42, "refcount_acquire_without_unstash (2) retval");

	local_kptr_stash__destroy(skel);
}

static void test_local_kptr_stash_fail(void)
{
	RUN_TESTS(local_kptr_stash_fail);
}

void test_local_kptr_stash(void)
{
	if (test__start_subtest("local_kptr_stash_simple"))
		test_local_kptr_stash_simple();
	if (test__start_subtest("local_kptr_stash_plain"))
		test_local_kptr_stash_plain();
	if (test__start_subtest("local_kptr_stash_local_with_root"))
		test_local_kptr_stash_local_with_root();
	if (test__start_subtest("local_kptr_stash_unstash"))
		test_local_kptr_stash_unstash();
	if (test__start_subtest("refcount_acquire_without_unstash"))
		test_refcount_acquire_without_unstash();
	if (test__start_subtest("local_kptr_stash_fail"))
		test_local_kptr_stash_fail();
}