Linux Audio

Check our new training course

Loading...
Note: File does not exist in v5.4.
  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"
  7
  8/* Need to keep consistent with definition in include/linux/bpf.h */
  9struct bpf_dummy_ops_state {
 10	int val;
 11};
 12
 13static void test_dummy_st_ops_attach(void)
 14{
 15	struct dummy_st_ops_success *skel;
 16	struct bpf_link *link;
 17
 18	skel = dummy_st_ops_success__open_and_load();
 19	if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
 20		return;
 21
 22	link = bpf_map__attach_struct_ops(skel->maps.dummy_1);
 23	ASSERT_EQ(libbpf_get_error(link), -EOPNOTSUPP, "dummy_st_ops_attach");
 24
 25	dummy_st_ops_success__destroy(skel);
 26}
 27
 28static void test_dummy_init_ret_value(void)
 29{
 30	__u64 args[1] = {0};
 31	LIBBPF_OPTS(bpf_test_run_opts, attr,
 32		.ctx_in = args,
 33		.ctx_size_in = sizeof(args),
 34	);
 35	struct dummy_st_ops_success *skel;
 36	int fd, err;
 37
 38	skel = dummy_st_ops_success__open_and_load();
 39	if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
 40		return;
 41
 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");
 46
 47	dummy_st_ops_success__destroy(skel);
 48}
 49
 50static void test_dummy_init_ptr_arg(void)
 51{
 52	int exp_retval = 0xbeef;
 53	struct bpf_dummy_ops_state in_state = {
 54		.val = exp_retval,
 55	};
 56	__u64 args[1] = {(unsigned long)&in_state};
 57	LIBBPF_OPTS(bpf_test_run_opts, attr,
 58		.ctx_in = args,
 59		.ctx_size_in = sizeof(args),
 60	);
 61	struct trace_dummy_st_ops *trace_skel;
 62	struct dummy_st_ops_success *skel;
 63	int fd, err;
 64
 65	skel = dummy_st_ops_success__open_and_load();
 66	if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
 67		return;
 68
 69	fd = bpf_program__fd(skel->progs.test_1);
 70
 71	trace_skel = trace_dummy_st_ops__open();
 72	if (!ASSERT_OK_PTR(trace_skel, "trace_dummy_st_ops__open"))
 73		goto done;
 74
 75	err = bpf_program__set_attach_target(trace_skel->progs.fentry_test_1,
 76					     fd, "test_1");
 77	if (!ASSERT_OK(err, "set_attach_target(fentry_test_1)"))
 78		goto done;
 79
 80	err = trace_dummy_st_ops__load(trace_skel);
 81	if (!ASSERT_OK(err, "load(trace_skel)"))
 82		goto done;
 83
 84	err = trace_dummy_st_ops__attach(trace_skel);
 85	if (!ASSERT_OK(err, "attach(trace_skel)"))
 86		goto done;
 87
 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");
 93
 94done:
 95	dummy_st_ops_success__destroy(skel);
 96	trace_dummy_st_ops__destroy(trace_skel);
 97}
 98
 99static void test_dummy_multiple_args(void)
100{
101	__u64 args[5] = {0, -100, 0x8a5f, 'c', 0x1234567887654321ULL};
102	LIBBPF_OPTS(bpf_test_run_opts, attr,
103		.ctx_in = args,
104		.ctx_size_in = sizeof(args),
105	);
106	struct dummy_st_ops_success *skel;
107	int fd, err;
108	size_t i;
109	char name[8];
110
111	skel = dummy_st_ops_success__open_and_load();
112	if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
113		return;
114
115	fd = bpf_program__fd(skel->progs.test_2);
116	err = bpf_prog_test_run_opts(fd, &attr);
117	ASSERT_OK(err, "test_run");
118	for (i = 0; i < ARRAY_SIZE(args); i++) {
119		snprintf(name, sizeof(name), "arg %zu", i);
120		ASSERT_EQ(skel->bss->test_2_args[i], args[i], name);
121	}
122
123	dummy_st_ops_success__destroy(skel);
124}
125
126static void test_dummy_sleepable(void)
127{
128	__u64 args[1] = {0};
129	LIBBPF_OPTS(bpf_test_run_opts, attr,
130		.ctx_in = args,
131		.ctx_size_in = sizeof(args),
132	);
133	struct dummy_st_ops_success *skel;
134	int fd, err;
135
136	skel = dummy_st_ops_success__open_and_load();
137	if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
138		return;
139
140	fd = bpf_program__fd(skel->progs.test_sleepable);
141	err = bpf_prog_test_run_opts(fd, &attr);
142	ASSERT_OK(err, "test_run");
143
144	dummy_st_ops_success__destroy(skel);
145}
146
147void test_dummy_st_ops(void)
148{
149	if (test__start_subtest("dummy_st_ops_attach"))
150		test_dummy_st_ops_attach();
151	if (test__start_subtest("dummy_init_ret_value"))
152		test_dummy_init_ret_value();
153	if (test__start_subtest("dummy_init_ptr_arg"))
154		test_dummy_init_ptr_arg();
155	if (test__start_subtest("dummy_multiple_args"))
156		test_dummy_multiple_args();
157	if (test__start_subtest("dummy_sleepable"))
158		test_dummy_sleepable();
159
160	RUN_TESTS(dummy_st_ops_fail);
161}