1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
4 
5 /* that's kernel internal BPF_MAX_TRAMP_PROGS define */
6 #define CNT 38
7 
8 void test_fexit_stress(void)
9 {
10 	char test_skb[128] = {};
11 	int fexit_fd[CNT] = {};
12 	int link_fd[CNT] = {};
13 	__u32 duration = 0;
14 	char error[4096];
15 	__u32 prog_ret;
16 	int err, i, filter_fd;
17 
18 	const struct bpf_insn trace_program[] = {
19 		BPF_MOV64_IMM(BPF_REG_0, 0),
20 		BPF_EXIT_INSN(),
21 	};
22 
23 	LIBBPF_OPTS(bpf_prog_load_opts, trace_opts,
24 		.expected_attach_type = BPF_TRACE_FEXIT,
25 		.log_buf = error,
26 		.log_size = sizeof(error),
27 	);
28 
29 	const struct bpf_insn skb_program[] = {
30 		BPF_MOV64_IMM(BPF_REG_0, 0),
31 		BPF_EXIT_INSN(),
32 	};
33 
34 	LIBBPF_OPTS(bpf_prog_load_opts, skb_opts,
35 		.log_buf = error,
36 		.log_size = sizeof(error),
37 	);
38 
39 	err = libbpf_find_vmlinux_btf_id("bpf_fentry_test1",
40 					 trace_opts.expected_attach_type);
41 	if (CHECK(err <= 0, "find_vmlinux_btf_id", "failed: %d\n", err))
42 		goto out;
43 	trace_opts.attach_btf_id = err;
44 
45 	for (i = 0; i < CNT; i++) {
46 		fexit_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL",
47 					    trace_program,
48 					    sizeof(trace_program) / sizeof(struct bpf_insn),
49 					    &trace_opts);
50 		if (CHECK(fexit_fd[i] < 0, "fexit loaded",
51 			  "failed: %d errno %d\n", fexit_fd[i], errno))
52 			goto out;
53 		link_fd[i] = bpf_raw_tracepoint_open(NULL, fexit_fd[i]);
54 		if (CHECK(link_fd[i] < 0, "fexit attach failed",
55 			  "prog %d failed: %d err %d\n", i, link_fd[i], errno))
56 			goto out;
57 	}
58 
59 	filter_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
60 				  skb_program, sizeof(skb_program) / sizeof(struct bpf_insn),
61 				  &skb_opts);
62 	if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n",
63 		  filter_fd, errno))
64 		goto out;
65 
66 	err = bpf_prog_test_run(filter_fd, 1, test_skb, sizeof(test_skb), 0,
67 				0, &prog_ret, 0);
68 	close(filter_fd);
69 	CHECK_FAIL(err);
70 out:
71 	for (i = 0; i < CNT; i++) {
72 		if (link_fd[i])
73 			close(link_fd[i]);
74 		if (fexit_fd[i])
75 			close(fexit_fd[i]);
76 	}
77 }
78