xref: /linux/tools/testing/selftests/bpf/prog_tests/stack_arg.c (revision b2128290c29902315e632ea59e0504d6bc9e9b42)
179e7ec00SYonghong Song // SPDX-License-Identifier: GPL-2.0
279e7ec00SYonghong Song /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
379e7ec00SYonghong Song 
479e7ec00SYonghong Song #include <test_progs.h>
579e7ec00SYonghong Song #include <network_helpers.h>
679e7ec00SYonghong Song #include "stack_arg.skel.h"
779e7ec00SYonghong Song #include "stack_arg_kfunc.skel.h"
879e7ec00SYonghong Song 
979e7ec00SYonghong Song static void run_subtest(struct bpf_program *prog, int expected)
1079e7ec00SYonghong Song {
1179e7ec00SYonghong Song 	int err, prog_fd;
1279e7ec00SYonghong Song 	LIBBPF_OPTS(bpf_test_run_opts, topts,
1379e7ec00SYonghong Song 		.data_in = &pkt_v4,
1479e7ec00SYonghong Song 		.data_size_in = sizeof(pkt_v4),
1579e7ec00SYonghong Song 		.repeat = 1,
1679e7ec00SYonghong Song 	);
1779e7ec00SYonghong Song 
1879e7ec00SYonghong Song 	prog_fd = bpf_program__fd(prog);
1979e7ec00SYonghong Song 	err = bpf_prog_test_run_opts(prog_fd, &topts);
2079e7ec00SYonghong Song 	ASSERT_OK(err, "test_run");
2179e7ec00SYonghong Song 	ASSERT_EQ(topts.retval, expected, "retval");
2279e7ec00SYonghong Song }
2379e7ec00SYonghong Song 
2479e7ec00SYonghong Song static void test_global_many(void)
2579e7ec00SYonghong Song {
2679e7ec00SYonghong Song 	struct stack_arg *skel;
2779e7ec00SYonghong Song 
2879e7ec00SYonghong Song 	skel = stack_arg__open();
2979e7ec00SYonghong Song 	if (!ASSERT_OK_PTR(skel, "open"))
3079e7ec00SYonghong Song 		return;
3179e7ec00SYonghong Song 
3279e7ec00SYonghong Song 	if (!skel->rodata->has_stack_arg) {
3379e7ec00SYonghong Song 		test__skip();
3479e7ec00SYonghong Song 		goto out;
3579e7ec00SYonghong Song 	}
3679e7ec00SYonghong Song 
3779e7ec00SYonghong Song 	if (!ASSERT_OK(stack_arg__load(skel), "load"))
3879e7ec00SYonghong Song 		goto out;
3979e7ec00SYonghong Song 
40*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_global_many_args, 55);
4179e7ec00SYonghong Song 
4279e7ec00SYonghong Song out:
4379e7ec00SYonghong Song 	stack_arg__destroy(skel);
4479e7ec00SYonghong Song }
4579e7ec00SYonghong Song 
4679e7ec00SYonghong Song static void test_async_cb_many(void)
4779e7ec00SYonghong Song {
4879e7ec00SYonghong Song 	struct stack_arg *skel;
4979e7ec00SYonghong Song 
5079e7ec00SYonghong Song 	skel = stack_arg__open();
5179e7ec00SYonghong Song 	if (!ASSERT_OK_PTR(skel, "open"))
5279e7ec00SYonghong Song 		return;
5379e7ec00SYonghong Song 
5479e7ec00SYonghong Song 	if (!skel->rodata->has_stack_arg) {
5579e7ec00SYonghong Song 		test__skip();
5679e7ec00SYonghong Song 		goto out;
5779e7ec00SYonghong Song 	}
5879e7ec00SYonghong Song 
5979e7ec00SYonghong Song 	if (!ASSERT_OK(stack_arg__load(skel), "load"))
6079e7ec00SYonghong Song 		goto out;
6179e7ec00SYonghong Song 
6279e7ec00SYonghong Song 	run_subtest(skel->progs.test_async_cb_many_args, 0);
6379e7ec00SYonghong Song 
6479e7ec00SYonghong Song 	/* Wait for the timer callback to fire and verify the result.
65*157317baSPuranjay Mohan 	 * 10+20+30+40+50+60+70+80+90+100 = 550
6679e7ec00SYonghong Song 	 */
6779e7ec00SYonghong Song 	usleep(50);
68*157317baSPuranjay Mohan 	ASSERT_EQ(skel->bss->timer_result, 550, "timer_result");
6979e7ec00SYonghong Song 
7079e7ec00SYonghong Song out:
7179e7ec00SYonghong Song 	stack_arg__destroy(skel);
7279e7ec00SYonghong Song }
7379e7ec00SYonghong Song 
7479e7ec00SYonghong Song static void test_bpf2bpf(void)
7579e7ec00SYonghong Song {
7679e7ec00SYonghong Song 	struct stack_arg *skel;
7779e7ec00SYonghong Song 
7879e7ec00SYonghong Song 	skel = stack_arg__open();
7979e7ec00SYonghong Song 	if (!ASSERT_OK_PTR(skel, "open"))
8079e7ec00SYonghong Song 		return;
8179e7ec00SYonghong Song 
8279e7ec00SYonghong Song 	if (!skel->rodata->has_stack_arg) {
8379e7ec00SYonghong Song 		test__skip();
8479e7ec00SYonghong Song 		goto out;
8579e7ec00SYonghong Song 	}
8679e7ec00SYonghong Song 
8779e7ec00SYonghong Song 	if (!ASSERT_OK(stack_arg__load(skel), "load"))
8879e7ec00SYonghong Song 		goto out;
8979e7ec00SYonghong Song 
90*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_bpf2bpf_ptr_stack_arg, 75);
91*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_bpf2bpf_mix_stack_args, 66);
92*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_bpf2bpf_nesting_stack_arg, 84);
93*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_bpf2bpf_dynptr_stack_arg, 99);
94*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_two_callees, 133);
9579e7ec00SYonghong Song 
9679e7ec00SYonghong Song out:
9779e7ec00SYonghong Song 	stack_arg__destroy(skel);
9879e7ec00SYonghong Song }
9979e7ec00SYonghong Song 
10079e7ec00SYonghong Song static void test_kfunc(void)
10179e7ec00SYonghong Song {
10279e7ec00SYonghong Song 	struct stack_arg_kfunc *skel;
10379e7ec00SYonghong Song 
10479e7ec00SYonghong Song 	skel = stack_arg_kfunc__open();
10579e7ec00SYonghong Song 	if (!ASSERT_OK_PTR(skel, "open"))
10679e7ec00SYonghong Song 		return;
10779e7ec00SYonghong Song 
10879e7ec00SYonghong Song 	if (!skel->rodata->has_stack_arg) {
10979e7ec00SYonghong Song 		test__skip();
11079e7ec00SYonghong Song 		goto out;
11179e7ec00SYonghong Song 	}
11279e7ec00SYonghong Song 
11379e7ec00SYonghong Song 	if (!ASSERT_OK(stack_arg_kfunc__load(skel), "load"))
11479e7ec00SYonghong Song 		goto out;
11579e7ec00SYonghong Song 
116*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_stack_arg_scalar, 55);
117*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_stack_arg_ptr, 75);
118*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_stack_arg_mix, 66);
119*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_stack_arg_dynptr, 99);
12079e7ec00SYonghong Song 	run_subtest(skel->progs.test_stack_arg_mem, 151);
121*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_stack_arg_iter, 145);
122*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_stack_arg_const_str, 45);
123*157317baSPuranjay Mohan 	run_subtest(skel->progs.test_stack_arg_timer, 45);
12479e7ec00SYonghong Song 
12579e7ec00SYonghong Song out:
12679e7ec00SYonghong Song 	stack_arg_kfunc__destroy(skel);
12779e7ec00SYonghong Song }
12879e7ec00SYonghong Song 
12979e7ec00SYonghong Song void test_stack_arg(void)
13079e7ec00SYonghong Song {
13179e7ec00SYonghong Song 	if (test__start_subtest("global_many_args"))
13279e7ec00SYonghong Song 		test_global_many();
13379e7ec00SYonghong Song 	if (test__start_subtest("async_cb_many_args"))
13479e7ec00SYonghong Song 		test_async_cb_many();
13579e7ec00SYonghong Song 	if (test__start_subtest("bpf2bpf"))
13679e7ec00SYonghong Song 		test_bpf2bpf();
13779e7ec00SYonghong Song 	if (test__start_subtest("kfunc"))
13879e7ec00SYonghong Song 		test_kfunc();
13979e7ec00SYonghong Song }
140