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 	char error[4096];
14 	int err, i, filter_fd;
15 
16 	const struct bpf_insn trace_program[] = {
17 		BPF_MOV64_IMM(BPF_REG_0, 0),
18 		BPF_EXIT_INSN(),
19 	};
20 
21 	LIBBPF_OPTS(bpf_prog_load_opts, trace_opts,
22 		.expected_attach_type = BPF_TRACE_FEXIT,
23 		.log_buf = error,
24 		.log_size = sizeof(error),
25 	);
26 
27 	const struct bpf_insn skb_program[] = {
28 		BPF_MOV64_IMM(BPF_REG_0, 0),
29 		BPF_EXIT_INSN(),
30 	};
31 
32 	LIBBPF_OPTS(bpf_prog_load_opts, skb_opts,
33 		.log_buf = error,
34 		.log_size = sizeof(error),
35 	);
36 
37 	LIBBPF_OPTS(bpf_test_run_opts, topts,
38 		.data_in = test_skb,
39 		.data_size_in = sizeof(test_skb),
40 		.repeat = 1,
41 	);
42 
43 	err = libbpf_find_vmlinux_btf_id("bpf_fentry_test1",
44 					 trace_opts.expected_attach_type);
45 	if (!ASSERT_GT(err, 0, "find_vmlinux_btf_id"))
46 		goto out;
47 	trace_opts.attach_btf_id = err;
48 
49 	for (i = 0; i < CNT; i++) {
50 		fexit_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL",
51 					    trace_program,
52 					    sizeof(trace_program) / sizeof(struct bpf_insn),
53 					    &trace_opts);
54 		if (!ASSERT_GE(fexit_fd[i], 0, "fexit load"))
55 			goto out;
56 		link_fd[i] = bpf_raw_tracepoint_open(NULL, fexit_fd[i]);
57 		if (!ASSERT_GE(link_fd[i], 0, "fexit attach"))
58 			goto out;
59 	}
60 
61 	filter_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
62 				  skb_program, sizeof(skb_program) / sizeof(struct bpf_insn),
63 				  &skb_opts);
64 	if (!ASSERT_GE(filter_fd, 0, "test_program_loaded"))
65 		goto out;
66 
67 	err = bpf_prog_test_run_opts(filter_fd, &topts);
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