xref: /linux/tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_run.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <test_progs.h>
4 #include <linux/nbd.h>
5 #include "bpf_util.h"
6 
7 /* NOTE: conflict with other tests. */
serial_test_raw_tp_writable_test_run(void)8 void serial_test_raw_tp_writable_test_run(void)
9 {
10 	__u32 duration = 0;
11 	char error[4096];
12 
13 	const struct bpf_insn trace_program[] = {
14 		BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
15 		BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6, 0),
16 		BPF_MOV64_IMM(BPF_REG_0, 42),
17 		BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_0, 0),
18 		BPF_EXIT_INSN(),
19 	};
20 
21 	LIBBPF_OPTS(bpf_prog_load_opts, trace_opts,
22 		.log_level = 2,
23 		.log_buf = error,
24 		.log_size = sizeof(error),
25 	);
26 
27 	int bpf_fd = bpf_prog_load(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, NULL, "GPL v2",
28 				   trace_program, ARRAY_SIZE(trace_program),
29 				   &trace_opts);
30 	if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable loaded",
31 		  "failed: %d errno %d\n", bpf_fd, errno))
32 		return;
33 
34 	const struct bpf_insn skb_program[] = {
35 		BPF_MOV64_IMM(BPF_REG_0, 0),
36 		BPF_EXIT_INSN(),
37 	};
38 
39 	LIBBPF_OPTS(bpf_prog_load_opts, skb_opts,
40 		.log_buf = error,
41 		.log_size = sizeof(error),
42 	);
43 
44 	int filter_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL v2",
45 				      skb_program, ARRAY_SIZE(skb_program),
46 				      &skb_opts);
47 	if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n",
48 		  filter_fd, errno))
49 		goto out_bpffd;
50 
51 	int tp_fd = bpf_raw_tracepoint_open("bpf_test_finish", bpf_fd);
52 	if (CHECK(tp_fd < 0, "bpf_raw_tracepoint_writable opened",
53 		  "failed: %d errno %d\n", tp_fd, errno))
54 		goto out_filterfd;
55 
56 	char test_skb[128] = {
57 		0,
58 	};
59 
60 	LIBBPF_OPTS(bpf_test_run_opts, topts,
61 		.data_in = test_skb,
62 		.data_size_in = sizeof(test_skb),
63 		.repeat = 1,
64 	);
65 	int err = bpf_prog_test_run_opts(filter_fd, &topts);
66 	CHECK(err != 42, "test_run",
67 	      "tracepoint did not modify return value\n");
68 	CHECK(topts.retval != 0, "test_run_ret",
69 	      "socket_filter did not return 0\n");
70 
71 	close(tp_fd);
72 
73 	err = bpf_prog_test_run_opts(filter_fd, &topts);
74 	CHECK(err != 0, "test_run_notrace",
75 	      "test_run failed with %d errno %d\n", err, errno);
76 	CHECK(topts.retval != 0, "test_run_ret_notrace",
77 	      "socket_filter did not return 0\n");
78 
79 out_filterfd:
80 	close(filter_fd);
81 out_bpffd:
82 	close(bpf_fd);
83 }
84