1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <net/if.h>
4 #include "test_xdp.skel.h"
5 #include "test_xdp_bpf2bpf.skel.h"
6 
7 void test_xdp_bpf2bpf(void)
8 {
9 	__u32 duration = 0, retval, size;
10 	char buf[128];
11 	int err, pkt_fd, map_fd;
12 	struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
13 	struct iptnl_info value4 = {.family = AF_INET};
14 	struct test_xdp *pkt_skel = NULL;
15 	struct test_xdp_bpf2bpf *ftrace_skel = NULL;
16 	struct vip key4 = {.protocol = 6, .family = AF_INET};
17 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
18 
19 	/* Load XDP program to introspect */
20 	pkt_skel = test_xdp__open_and_load();
21 	if (CHECK(!pkt_skel, "pkt_skel_load", "test_xdp skeleton failed\n"))
22 		return;
23 
24 	pkt_fd = bpf_program__fd(pkt_skel->progs._xdp_tx_iptunnel);
25 
26 	map_fd = bpf_map__fd(pkt_skel->maps.vip2tnl);
27 	bpf_map_update_elem(map_fd, &key4, &value4, 0);
28 
29 	/* Load trace program */
30 	opts.attach_prog_fd = pkt_fd,
31 	ftrace_skel = test_xdp_bpf2bpf__open_opts(&opts);
32 	if (CHECK(!ftrace_skel, "__open", "ftrace skeleton failed\n"))
33 		goto out;
34 
35 	err = test_xdp_bpf2bpf__load(ftrace_skel);
36 	if (CHECK(err, "__load", "ftrace skeleton failed\n"))
37 		goto out;
38 
39 	err = test_xdp_bpf2bpf__attach(ftrace_skel);
40 	if (CHECK(err, "ftrace_attach", "ftrace attach failed: %d\n", err))
41 		goto out;
42 
43 	/* Run test program */
44 	err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4),
45 				buf, &size, &retval, &duration);
46 
47 	if (CHECK(err || retval != XDP_TX || size != 74 ||
48 		  iph->protocol != IPPROTO_IPIP, "ipv4",
49 		  "err %d errno %d retval %d size %d\n",
50 		  err, errno, retval, size))
51 		goto out;
52 
53 	/* Verify test results */
54 	if (CHECK(ftrace_skel->bss->test_result_fentry != if_nametoindex("lo"),
55 		  "result", "fentry failed err %llu\n",
56 		  ftrace_skel->bss->test_result_fentry))
57 		goto out;
58 
59 	CHECK(ftrace_skel->bss->test_result_fexit != XDP_TX, "result",
60 	      "fexit failed err %llu\n", ftrace_skel->bss->test_result_fexit);
61 
62 out:
63 	test_xdp__destroy(pkt_skel);
64 	test_xdp_bpf2bpf__destroy(ftrace_skel);
65 }
66