1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
4 
5 /* x86-64 fits 55 JITed and 43 interpreted progs into half page */
6 #define CNT 40
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 	struct bpf_load_program_attr load_attr = {
24 		.prog_type = BPF_PROG_TYPE_TRACING,
25 		.license = "GPL",
26 		.insns = trace_program,
27 		.insns_cnt = sizeof(trace_program) / sizeof(struct bpf_insn),
28 		.expected_attach_type = BPF_TRACE_FEXIT,
29 	};
30 
31 	const struct bpf_insn skb_program[] = {
32 		BPF_MOV64_IMM(BPF_REG_0, 0),
33 		BPF_EXIT_INSN(),
34 	};
35 
36 	struct bpf_load_program_attr skb_load_attr = {
37 		.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
38 		.license = "GPL",
39 		.insns = skb_program,
40 		.insns_cnt = sizeof(skb_program) / sizeof(struct bpf_insn),
41 	};
42 
43 	err = libbpf_find_vmlinux_btf_id("bpf_fentry_test1",
44 					 load_attr.expected_attach_type);
45 	if (CHECK(err <= 0, "find_vmlinux_btf_id", "failed: %d\n", err))
46 		goto out;
47 	load_attr.attach_btf_id = err;
48 
49 	for (i = 0; i < CNT; i++) {
50 		fexit_fd[i] = bpf_load_program_xattr(&load_attr, error, sizeof(error));
51 		if (CHECK(fexit_fd[i] < 0, "fexit loaded",
52 			  "failed: %d errno %d\n", fexit_fd[i], errno))
53 			goto out;
54 		link_fd[i] = bpf_raw_tracepoint_open(NULL, fexit_fd[i]);
55 		if (CHECK(link_fd[i] < 0, "fexit attach failed",
56 			  "prog %d failed: %d err %d\n", i, link_fd[i], errno))
57 			goto out;
58 	}
59 
60 	filter_fd = bpf_load_program_xattr(&skb_load_attr, error, sizeof(error));
61 	if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n",
62 		  filter_fd, errno))
63 		goto out;
64 
65 	err = bpf_prog_test_run(filter_fd, 1, test_skb, sizeof(test_skb), 0,
66 				0, &prog_ret, 0);
67 	close(filter_fd);
68 	CHECK_FAIL(err);
69 out:
70 	for (i = 0; i < CNT; i++) {
71 		if (link_fd[i])
72 			close(link_fd[i]);
73 		if (fexit_fd[i])
74 			close(fexit_fd[i]);
75 	}
76 }
77