1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
4 
5 void test_fexit_test(void)
6 {
7 	struct bpf_prog_load_attr attr = {
8 		.file = "./fexit_test.o",
9 	};
10 
11 	char prog_name[] = "fexit/bpf_fentry_testX";
12 	struct bpf_object *obj = NULL, *pkt_obj;
13 	int err, pkt_fd, kfree_skb_fd, i;
14 	struct bpf_link *link[6] = {};
15 	struct bpf_program *prog[6];
16 	__u32 duration, retval;
17 	struct bpf_map *data_map;
18 	const int zero = 0;
19 	u64 result[6];
20 
21 	err = bpf_prog_load("./test_pkt_access.o", BPF_PROG_TYPE_SCHED_CLS,
22 			    &pkt_obj, &pkt_fd);
23 	if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno))
24 		return;
25 	err = bpf_prog_load_xattr(&attr, &obj, &kfree_skb_fd);
26 	if (CHECK(err, "prog_load fail", "err %d errno %d\n", err, errno))
27 		goto close_prog;
28 
29 	for (i = 0; i < 6; i++) {
30 		prog_name[sizeof(prog_name) - 2] = '1' + i;
31 		prog[i] = bpf_object__find_program_by_title(obj, prog_name);
32 		if (CHECK(!prog[i], "find_prog", "prog %s not found\n", prog_name))
33 			goto close_prog;
34 		link[i] = bpf_program__attach_trace(prog[i]);
35 		if (CHECK(IS_ERR(link[i]), "attach_trace", "failed to link\n"))
36 			goto close_prog;
37 	}
38 	data_map = bpf_object__find_map_by_name(obj, "fexit_te.bss");
39 	if (CHECK(!data_map, "find_data_map", "data map not found\n"))
40 		goto close_prog;
41 
42 	err = bpf_prog_test_run(pkt_fd, 1, &pkt_v6, sizeof(pkt_v6),
43 				NULL, NULL, &retval, &duration);
44 	CHECK(err || retval, "ipv6",
45 	      "err %d errno %d retval %d duration %d\n",
46 	      err, errno, retval, duration);
47 
48 	err = bpf_map_lookup_elem(bpf_map__fd(data_map), &zero, &result);
49 	if (CHECK(err, "get_result",
50 		  "failed to get output data: %d\n", err))
51 		goto close_prog;
52 
53 	for (i = 0; i < 6; i++)
54 		if (CHECK(result[i] != 1, "result", "bpf_fentry_test%d failed err %ld\n",
55 			  i + 1, result[i]))
56 			goto close_prog;
57 
58 close_prog:
59 	for (i = 0; i < 6; i++)
60 		if (!IS_ERR_OR_NULL(link[i]))
61 			bpf_link__destroy(link[i]);
62 	bpf_object__close(obj);
63 	bpf_object__close(pkt_obj);
64 }
65