1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <test_progs.h>
4 #include <linux/nbd.h>
5 
test_raw_tp_writable_test_run(void)6 void test_raw_tp_writable_test_run(void)
7 {
8 	__u32 duration = 0;
9 	char error[4096];
10 
11 	const struct bpf_insn trace_program[] = {
12 		BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
13 		BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6, 0),
14 		BPF_MOV64_IMM(BPF_REG_0, 42),
15 		BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_0, 0),
16 		BPF_EXIT_INSN(),
17 	};
18 
19 	struct bpf_load_program_attr load_attr = {
20 		.prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
21 		.license = "GPL v2",
22 		.insns = trace_program,
23 		.insns_cnt = sizeof(trace_program) / sizeof(struct bpf_insn),
24 		.log_level = 2,
25 	};
26 
27 	int bpf_fd = bpf_load_program_xattr(&load_attr, error, sizeof(error));
28 	if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable loaded",
29 		  "failed: %d errno %d\n", bpf_fd, errno))
30 		return;
31 
32 	const struct bpf_insn skb_program[] = {
33 		BPF_MOV64_IMM(BPF_REG_0, 0),
34 		BPF_EXIT_INSN(),
35 	};
36 
37 	struct bpf_load_program_attr skb_load_attr = {
38 		.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
39 		.license = "GPL v2",
40 		.insns = skb_program,
41 		.insns_cnt = sizeof(skb_program) / sizeof(struct bpf_insn),
42 	};
43 
44 	int filter_fd =
45 		bpf_load_program_xattr(&skb_load_attr, error, sizeof(error));
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 	__u32 prog_ret;
60 	int err = bpf_prog_test_run(filter_fd, 1, test_skb, sizeof(test_skb), 0,
61 				    0, &prog_ret, 0);
62 	CHECK(err != 42, "test_run",
63 	      "tracepoint did not modify return value\n");
64 	CHECK(prog_ret != 0, "test_run_ret",
65 	      "socket_filter did not return 0\n");
66 
67 	close(tp_fd);
68 
69 	err = bpf_prog_test_run(filter_fd, 1, test_skb, sizeof(test_skb), 0, 0,
70 				&prog_ret, 0);
71 	CHECK(err != 0, "test_run_notrace",
72 	      "test_run failed with %d errno %d\n", err, errno);
73 	CHECK(prog_ret != 0, "test_run_ret_notrace",
74 	      "socket_filter did not return 0\n");
75 
76 out_filterfd:
77 	close(filter_fd);
78 out_bpffd:
79 	close(bpf_fd);
80 }
81