1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 
5 #include "test_pkt_access.skel.h"
6 
7 static const __u32 duration;
8 
9 static void check_run_cnt(int prog_fd, __u64 run_cnt)
10 {
11 	struct bpf_prog_info info = {};
12 	__u32 info_len = sizeof(info);
13 	int err;
14 
15 	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
16 	if (CHECK(err, "get_prog_info", "failed to get bpf_prog_info for fd %d\n", prog_fd))
17 		return;
18 
19 	CHECK(run_cnt != info.run_cnt, "run_cnt",
20 	      "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt);
21 }
22 
23 void test_prog_run_opts(void)
24 {
25 	struct test_pkt_access *skel;
26 	int err, stats_fd = -1, prog_fd;
27 	char buf[10] = {};
28 	__u64 run_cnt = 0;
29 
30 	LIBBPF_OPTS(bpf_test_run_opts, topts,
31 		.repeat = 1,
32 		.data_in = &pkt_v4,
33 		.data_size_in = sizeof(pkt_v4),
34 		.data_out = buf,
35 		.data_size_out = 5,
36 	);
37 
38 	stats_fd = bpf_enable_stats(BPF_STATS_RUN_TIME);
39 	if (!ASSERT_GE(stats_fd, 0, "enable_stats good fd"))
40 		return;
41 
42 	skel = test_pkt_access__open_and_load();
43 	if (!ASSERT_OK_PTR(skel, "open_and_load"))
44 		goto cleanup;
45 
46 	prog_fd = bpf_program__fd(skel->progs.test_pkt_access);
47 
48 	err = bpf_prog_test_run_opts(prog_fd, &topts);
49 	ASSERT_EQ(errno, ENOSPC, "test_run errno");
50 	ASSERT_ERR(err, "test_run");
51 	ASSERT_OK(topts.retval, "test_run retval");
52 
53 	ASSERT_EQ(topts.data_size_out, sizeof(pkt_v4), "test_run data_size_out");
54 	ASSERT_EQ(buf[5], 0, "overflow, BPF_PROG_TEST_RUN ignored size hint");
55 
56 	run_cnt += topts.repeat;
57 	check_run_cnt(prog_fd, run_cnt);
58 
59 	topts.data_out = NULL;
60 	topts.data_size_out = 0;
61 	topts.repeat = 2;
62 	errno = 0;
63 
64 	err = bpf_prog_test_run_opts(prog_fd, &topts);
65 	ASSERT_OK(errno, "run_no_output errno");
66 	ASSERT_OK(err, "run_no_output err");
67 	ASSERT_OK(topts.retval, "run_no_output retval");
68 
69 	run_cnt += topts.repeat;
70 	check_run_cnt(prog_fd, run_cnt);
71 
72 cleanup:
73 	if (skel)
74 		test_pkt_access__destroy(skel);
75 	if (stats_fd >= 0)
76 		close(stats_fd);
77 }
78