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"
test_local_kptr_stash_simple(void)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
test_local_kptr_stash_plain(void)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
test_local_kptr_stash_local_with_root(void)51 static void test_local_kptr_stash_local_with_root(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_local_with_root), &opts);
66 ASSERT_OK(ret, "local_kptr_stash_add_local_with_root run");
67 ASSERT_OK(opts.retval, "local_kptr_stash_add_local_with_root retval");
68
69 local_kptr_stash__destroy(skel);
70 }
71
test_local_kptr_stash_unstash(void)72 static void test_local_kptr_stash_unstash(void)
73 {
74 LIBBPF_OPTS(bpf_test_run_opts, opts,
75 .data_in = &pkt_v4,
76 .data_size_in = sizeof(pkt_v4),
77 .repeat = 1,
78 );
79 struct local_kptr_stash *skel;
80 int ret;
81
82 skel = local_kptr_stash__open_and_load();
83 if (!ASSERT_OK_PTR(skel, "local_kptr_stash__open_and_load"))
84 return;
85
86 ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.stash_rb_nodes), &opts);
87 ASSERT_OK(ret, "local_kptr_stash_add_nodes run");
88 ASSERT_OK(opts.retval, "local_kptr_stash_add_nodes retval");
89
90 ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.unstash_rb_node), &opts);
91 ASSERT_OK(ret, "local_kptr_stash_add_nodes run");
92 ASSERT_EQ(opts.retval, 42, "local_kptr_stash_add_nodes retval");
93
94 local_kptr_stash__destroy(skel);
95 }
96
test_refcount_acquire_without_unstash(void)97 static void test_refcount_acquire_without_unstash(void)
98 {
99 LIBBPF_OPTS(bpf_test_run_opts, opts,
100 .data_in = &pkt_v4,
101 .data_size_in = sizeof(pkt_v4),
102 .repeat = 1,
103 );
104 struct local_kptr_stash *skel;
105 int ret;
106
107 skel = local_kptr_stash__open_and_load();
108 if (!ASSERT_OK_PTR(skel, "local_kptr_stash__open_and_load"))
109 return;
110
111 ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.refcount_acquire_without_unstash),
112 &opts);
113 ASSERT_OK(ret, "refcount_acquire_without_unstash run");
114 ASSERT_EQ(opts.retval, 2, "refcount_acquire_without_unstash retval");
115
116 ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.stash_refcounted_node), &opts);
117 ASSERT_OK(ret, "stash_refcounted_node run");
118 ASSERT_OK(opts.retval, "stash_refcounted_node retval");
119
120 ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.refcount_acquire_without_unstash),
121 &opts);
122 ASSERT_OK(ret, "refcount_acquire_without_unstash (2) run");
123 ASSERT_EQ(opts.retval, 42, "refcount_acquire_without_unstash (2) retval");
124
125 local_kptr_stash__destroy(skel);
126 }
127
test_local_kptr_stash_fail(void)128 static void test_local_kptr_stash_fail(void)
129 {
130 RUN_TESTS(local_kptr_stash_fail);
131 }
132
test_local_kptr_stash(void)133 void test_local_kptr_stash(void)
134 {
135 if (test__start_subtest("local_kptr_stash_simple"))
136 test_local_kptr_stash_simple();
137 if (test__start_subtest("local_kptr_stash_plain"))
138 test_local_kptr_stash_plain();
139 if (test__start_subtest("local_kptr_stash_local_with_root"))
140 test_local_kptr_stash_local_with_root();
141 if (test__start_subtest("local_kptr_stash_unstash"))
142 test_local_kptr_stash_unstash();
143 if (test__start_subtest("refcount_acquire_without_unstash"))
144 test_refcount_acquire_without_unstash();
145 if (test__start_subtest("local_kptr_stash_fail"))
146 test_local_kptr_stash_fail();
147 }
148