summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/stack_arg.c
blob: 57193543f2602a6070dda8b3f2f273f3c881c870 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */

#include <test_progs.h>
#include <network_helpers.h>
#include "stack_arg.skel.h"
#include "stack_arg_kfunc.skel.h"

static void run_subtest(struct bpf_program *prog, int expected)
{
	int err, prog_fd;
	LIBBPF_OPTS(bpf_test_run_opts, topts,
		.data_in = &pkt_v4,
		.data_size_in = sizeof(pkt_v4),
		.repeat = 1,
	);

	prog_fd = bpf_program__fd(prog);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	ASSERT_OK(err, "test_run");
	ASSERT_EQ(topts.retval, expected, "retval");
}

static void test_global_many(void)
{
	struct stack_arg *skel;

	skel = stack_arg__open();
	if (!ASSERT_OK_PTR(skel, "open"))
		return;

	if (!skel->rodata->has_stack_arg) {
		test__skip();
		goto out;
	}

	if (!ASSERT_OK(stack_arg__load(skel), "load"))
		goto out;

	run_subtest(skel->progs.test_global_many_args, 55);

out:
	stack_arg__destroy(skel);
}

static void test_async_cb_many(void)
{
	struct stack_arg *skel;

	skel = stack_arg__open();
	if (!ASSERT_OK_PTR(skel, "open"))
		return;

	if (!skel->rodata->has_stack_arg) {
		test__skip();
		goto out;
	}

	if (!ASSERT_OK(stack_arg__load(skel), "load"))
		goto out;

	run_subtest(skel->progs.test_async_cb_many_args, 0);

	/* Wait for the timer callback to fire and verify the result.
	 * 10+20+30+40+50+60+70+80+90+100 = 550
	 */
	usleep(50);
	ASSERT_EQ(skel->bss->timer_result, 550, "timer_result");

out:
	stack_arg__destroy(skel);
}

static void test_bpf2bpf(void)
{
	struct stack_arg *skel;

	skel = stack_arg__open();
	if (!ASSERT_OK_PTR(skel, "open"))
		return;

	if (!skel->rodata->has_stack_arg) {
		test__skip();
		goto out;
	}

	if (!ASSERT_OK(stack_arg__load(skel), "load"))
		goto out;

	run_subtest(skel->progs.test_bpf2bpf_ptr_stack_arg, 75);
	run_subtest(skel->progs.test_bpf2bpf_mix_stack_args, 66);
	run_subtest(skel->progs.test_bpf2bpf_nesting_stack_arg, 84);
	run_subtest(skel->progs.test_bpf2bpf_dynptr_stack_arg, 99);
	run_subtest(skel->progs.test_two_callees, 133);

out:
	stack_arg__destroy(skel);
}

static void test_kfunc(void)
{
	struct stack_arg_kfunc *skel;

	skel = stack_arg_kfunc__open();
	if (!ASSERT_OK_PTR(skel, "open"))
		return;

	if (!skel->rodata->has_stack_arg) {
		test__skip();
		goto out;
	}

	if (!ASSERT_OK(stack_arg_kfunc__load(skel), "load"))
		goto out;

	run_subtest(skel->progs.test_stack_arg_scalar, 55);
	run_subtest(skel->progs.test_stack_arg_ptr, 75);
	run_subtest(skel->progs.test_stack_arg_mix, 66);
	run_subtest(skel->progs.test_stack_arg_dynptr, 99);
	run_subtest(skel->progs.test_stack_arg_mem, 151);
	run_subtest(skel->progs.test_stack_arg_iter, 145);
	run_subtest(skel->progs.test_stack_arg_const_str, 45);
	run_subtest(skel->progs.test_stack_arg_timer, 45);

out:
	stack_arg_kfunc__destroy(skel);
}

void test_stack_arg(void)
{
	if (test__start_subtest("global_many_args"))
		test_global_many();
	if (test__start_subtest("async_cb_many_args"))
		test_async_cb_many();
	if (test__start_subtest("bpf2bpf"))
		test_bpf2bpf();
	if (test__start_subtest("kfunc"))
		test_kfunc();
}