1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021. Huawei Technologies Co., Ltd */
3 #include <test_progs.h>
4 #include "dummy_st_ops_success.skel.h"
5 #include "dummy_st_ops_fail.skel.h"
6 #include "trace_dummy_st_ops.skel.h"
8 /* Need to keep consistent with definition in include/linux/bpf.h */
9 struct bpf_dummy_ops_state {
13 static void test_dummy_st_ops_attach(void)
15 struct dummy_st_ops_success *skel;
16 struct bpf_link *link;
18 skel = dummy_st_ops_success__open_and_load();
19 if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
22 link = bpf_map__attach_struct_ops(skel->maps.dummy_1);
23 ASSERT_EQ(libbpf_get_error(link), -EOPNOTSUPP, "dummy_st_ops_attach");
25 dummy_st_ops_success__destroy(skel);
28 static void test_dummy_init_ret_value(void)
31 LIBBPF_OPTS(bpf_test_run_opts, attr,
33 .ctx_size_in = sizeof(args),
35 struct dummy_st_ops_success *skel;
38 skel = dummy_st_ops_success__open_and_load();
39 if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
42 fd = bpf_program__fd(skel->progs.test_1);
43 err = bpf_prog_test_run_opts(fd, &attr);
44 ASSERT_OK(err, "test_run");
45 ASSERT_EQ(attr.retval, 0xf2f3f4f5, "test_ret");
47 dummy_st_ops_success__destroy(skel);
50 static void test_dummy_init_ptr_arg(void)
52 int exp_retval = 0xbeef;
53 struct bpf_dummy_ops_state in_state = {
56 __u64 args[1] = {(unsigned long)&in_state};
57 LIBBPF_OPTS(bpf_test_run_opts, attr,
59 .ctx_size_in = sizeof(args),
61 struct trace_dummy_st_ops *trace_skel;
62 struct dummy_st_ops_success *skel;
65 skel = dummy_st_ops_success__open_and_load();
66 if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
69 fd = bpf_program__fd(skel->progs.test_1);
71 trace_skel = trace_dummy_st_ops__open();
72 if (!ASSERT_OK_PTR(trace_skel, "trace_dummy_st_ops__open"))
75 err = bpf_program__set_attach_target(trace_skel->progs.fentry_test_1,
77 if (!ASSERT_OK(err, "set_attach_target(fentry_test_1)"))
80 err = trace_dummy_st_ops__load(trace_skel);
81 if (!ASSERT_OK(err, "load(trace_skel)"))
84 err = trace_dummy_st_ops__attach(trace_skel);
85 if (!ASSERT_OK(err, "attach(trace_skel)"))
88 err = bpf_prog_test_run_opts(fd, &attr);
89 ASSERT_OK(err, "test_run");
90 ASSERT_EQ(in_state.val, 0x5a, "test_ptr_ret");
91 ASSERT_EQ(attr.retval, exp_retval, "test_ret");
92 ASSERT_EQ(trace_skel->bss->val, exp_retval, "fentry_val");
95 dummy_st_ops_success__destroy(skel);
96 trace_dummy_st_ops__destroy(trace_skel);
99 static void test_dummy_multiple_args(void)
101 struct bpf_dummy_ops_state st = { 7 };
102 __u64 args[5] = {(__u64)&st, -100, 0x8a5f, 'c', 0x1234567887654321ULL};
103 LIBBPF_OPTS(bpf_test_run_opts, attr,
105 .ctx_size_in = sizeof(args),
107 struct dummy_st_ops_success *skel;
112 skel = dummy_st_ops_success__open_and_load();
113 if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
116 fd = bpf_program__fd(skel->progs.test_2);
117 err = bpf_prog_test_run_opts(fd, &attr);
118 ASSERT_OK(err, "test_run");
120 for (i = 0; i < ARRAY_SIZE(args); i++) {
121 snprintf(name, sizeof(name), "arg %zu", i);
122 ASSERT_EQ(skel->bss->test_2_args[i], args[i], name);
125 dummy_st_ops_success__destroy(skel);
128 static void test_dummy_sleepable(void)
130 struct bpf_dummy_ops_state st;
131 __u64 args[1] = {(__u64)&st};
132 LIBBPF_OPTS(bpf_test_run_opts, attr,
134 .ctx_size_in = sizeof(args),
136 struct dummy_st_ops_success *skel;
139 skel = dummy_st_ops_success__open_and_load();
140 if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
143 fd = bpf_program__fd(skel->progs.test_sleepable);
144 err = bpf_prog_test_run_opts(fd, &attr);
145 ASSERT_OK(err, "test_run");
147 dummy_st_ops_success__destroy(skel);
150 /* dummy_st_ops.test_sleepable() parameter is not marked as nullable,
151 * thus bpf_prog_test_run_opts() below should be rejected as it tries
152 * to pass NULL for this parameter.
154 static void test_dummy_sleepable_reject_null(void)
157 LIBBPF_OPTS(bpf_test_run_opts, attr,
159 .ctx_size_in = sizeof(args),
161 struct dummy_st_ops_success *skel;
164 skel = dummy_st_ops_success__open_and_load();
165 if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
168 fd = bpf_program__fd(skel->progs.test_sleepable);
169 err = bpf_prog_test_run_opts(fd, &attr);
170 ASSERT_EQ(err, -EINVAL, "test_run");
172 dummy_st_ops_success__destroy(skel);
175 void test_dummy_st_ops(void)
177 if (test__start_subtest("dummy_st_ops_attach"))
178 test_dummy_st_ops_attach();
179 if (test__start_subtest("dummy_init_ret_value"))
180 test_dummy_init_ret_value();
181 if (test__start_subtest("dummy_init_ptr_arg"))
182 test_dummy_init_ptr_arg();
183 if (test__start_subtest("dummy_multiple_args"))
184 test_dummy_multiple_args();
185 if (test__start_subtest("dummy_sleepable"))
186 test_dummy_sleepable();
187 if (test__start_subtest("dummy_sleepable_reject_null"))
188 test_dummy_sleepable_reject_null();
190 RUN_TESTS(dummy_st_ops_fail);