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