xref: /linux/tools/testing/selftests/bpf/prog_tests/local_kptr_stash.c (revision 34dc1baba215b826e454b8d19e4f24adbeb7d00d)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3 
4 #include <test_progs.h>
5 #include <network_helpers.h>
6 
7 #include "local_kptr_stash.skel.h"
8 #include "local_kptr_stash_fail.skel.h"
9 static void test_local_kptr_stash_simple(void)
10 {
11 	LIBBPF_OPTS(bpf_test_run_opts, opts,
12 		    .data_in = &pkt_v4,
13 		    .data_size_in = sizeof(pkt_v4),
14 		    .repeat = 1,
15 	);
16 	struct local_kptr_stash *skel;
17 	int ret;
18 
19 	skel = local_kptr_stash__open_and_load();
20 	if (!ASSERT_OK_PTR(skel, "local_kptr_stash__open_and_load"))
21 		return;
22 
23 	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.stash_rb_nodes), &opts);
24 	ASSERT_OK(ret, "local_kptr_stash_add_nodes run");
25 	ASSERT_OK(opts.retval, "local_kptr_stash_add_nodes retval");
26 
27 	local_kptr_stash__destroy(skel);
28 }
29 
30 static void test_local_kptr_stash_plain(void)
31 {
32 	LIBBPF_OPTS(bpf_test_run_opts, opts,
33 		    .data_in = &pkt_v4,
34 		    .data_size_in = sizeof(pkt_v4),
35 		    .repeat = 1,
36 	);
37 	struct local_kptr_stash *skel;
38 	int ret;
39 
40 	skel = local_kptr_stash__open_and_load();
41 	if (!ASSERT_OK_PTR(skel, "local_kptr_stash__open_and_load"))
42 		return;
43 
44 	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.stash_plain), &opts);
45 	ASSERT_OK(ret, "local_kptr_stash_add_plain run");
46 	ASSERT_OK(opts.retval, "local_kptr_stash_add_plain retval");
47 
48 	local_kptr_stash__destroy(skel);
49 }
50 
51 static void test_local_kptr_stash_unstash(void)
52 {
53 	LIBBPF_OPTS(bpf_test_run_opts, opts,
54 		    .data_in = &pkt_v4,
55 		    .data_size_in = sizeof(pkt_v4),
56 		    .repeat = 1,
57 	);
58 	struct local_kptr_stash *skel;
59 	int ret;
60 
61 	skel = local_kptr_stash__open_and_load();
62 	if (!ASSERT_OK_PTR(skel, "local_kptr_stash__open_and_load"))
63 		return;
64 
65 	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.stash_rb_nodes), &opts);
66 	ASSERT_OK(ret, "local_kptr_stash_add_nodes run");
67 	ASSERT_OK(opts.retval, "local_kptr_stash_add_nodes retval");
68 
69 	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.unstash_rb_node), &opts);
70 	ASSERT_OK(ret, "local_kptr_stash_add_nodes run");
71 	ASSERT_EQ(opts.retval, 42, "local_kptr_stash_add_nodes retval");
72 
73 	local_kptr_stash__destroy(skel);
74 }
75 
76 static void test_local_kptr_stash_fail(void)
77 {
78 	RUN_TESTS(local_kptr_stash_fail);
79 }
80 
81 void test_local_kptr_stash(void)
82 {
83 	if (test__start_subtest("local_kptr_stash_simple"))
84 		test_local_kptr_stash_simple();
85 	if (test__start_subtest("local_kptr_stash_plain"))
86 		test_local_kptr_stash_plain();
87 	if (test__start_subtest("local_kptr_stash_unstash"))
88 		test_local_kptr_stash_unstash();
89 	if (test__start_subtest("local_kptr_stash_fail"))
90 		test_local_kptr_stash_fail();
91 }
92