xref: /linux/tools/testing/selftests/bpf/prog_tests/xdp.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 
5 void test_xdp(void)
6 {
7 	struct vip key4 = {.protocol = 6, .family = AF_INET};
8 	struct vip key6 = {.protocol = 6, .family = AF_INET6};
9 	struct iptnl_info value4 = {.family = AF_INET};
10 	struct iptnl_info value6 = {.family = AF_INET6};
11 	const char *file = "./test_xdp.o";
12 	struct bpf_object *obj;
13 	char buf[128];
14 	struct ipv6hdr iph6;
15 	struct iphdr iph;
16 	__u32 duration, retval, size;
17 	int err, prog_fd, map_fd;
18 
19 	err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
20 	if (CHECK_FAIL(err))
21 		return;
22 
23 	map_fd = bpf_find_map(__func__, obj, "vip2tnl");
24 	if (map_fd < 0)
25 		goto out;
26 	bpf_map_update_elem(map_fd, &key4, &value4, 0);
27 	bpf_map_update_elem(map_fd, &key6, &value6, 0);
28 
29 	err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
30 				buf, &size, &retval, &duration);
31 	memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph));
32 	CHECK(err || retval != XDP_TX || size != 74 ||
33 	      iph.protocol != IPPROTO_IPIP, "ipv4",
34 	      "err %d errno %d retval %d size %d\n",
35 	      err, errno, retval, size);
36 
37 	err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6),
38 				buf, &size, &retval, &duration);
39 	memcpy(&iph6, buf + sizeof(struct ethhdr), sizeof(iph6));
40 	CHECK(err || retval != XDP_TX || size != 114 ||
41 	      iph6.nexthdr != IPPROTO_IPV6, "ipv6",
42 	      "err %d errno %d retval %d size %d\n",
43 	      err, errno, retval, size);
44 out:
45 	bpf_object__close(obj);
46 }
47