1#!/bin/bash
2# Create 2 namespaces with two veth peers, and
3# forward packets in-between using generic XDP
4#
5# NS1(veth11)     NS2(veth22)
6#     |               |
7#     |               |
8#   (veth1, ------ (veth2,
9#   id:111)         id:222)
10#     | xdp forwarding |
11#     ------------------
12
13readonly NS1="ns1-$(mktemp -u XXXXXX)"
14readonly NS2="ns2-$(mktemp -u XXXXXX)"
15ret=0
16
17setup()
18{
19
20	local xdpmode=$1
21
22	ip netns add ${NS1}
23	ip netns add ${NS2}
24
25	ip link add veth1 index 111 type veth peer name veth11 netns ${NS1}
26	ip link add veth2 index 222 type veth peer name veth22 netns ${NS2}
27
28	ip link set veth1 up
29	ip link set veth2 up
30	ip -n ${NS1} link set dev veth11 up
31	ip -n ${NS2} link set dev veth22 up
32
33	ip -n ${NS1} addr add 10.1.1.11/24 dev veth11
34	ip -n ${NS2} addr add 10.1.1.22/24 dev veth22
35}
36
37cleanup()
38{
39	ip link del veth1 2> /dev/null
40	ip link del veth2 2> /dev/null
41	ip netns del ${NS1} 2> /dev/null
42	ip netns del ${NS2} 2> /dev/null
43}
44
45test_xdp_redirect()
46{
47	local xdpmode=$1
48
49	setup
50
51	ip link set dev veth1 $xdpmode off &> /dev/null
52	if [ $? -ne 0 ];then
53		echo "selftests: test_xdp_redirect $xdpmode [SKIP]"
54		return 0
55	fi
56
57	ip -n ${NS1} link set veth11 $xdpmode obj xdp_dummy.bpf.o sec xdp &> /dev/null
58	ip -n ${NS2} link set veth22 $xdpmode obj xdp_dummy.bpf.o sec xdp &> /dev/null
59	ip link set dev veth1 $xdpmode obj test_xdp_redirect.bpf.o sec redirect_to_222 &> /dev/null
60	ip link set dev veth2 $xdpmode obj test_xdp_redirect.bpf.o sec redirect_to_111 &> /dev/null
61
62	if ip netns exec ${NS1} ping -c 1 10.1.1.22 &> /dev/null &&
63	   ip netns exec ${NS2} ping -c 1 10.1.1.11 &> /dev/null; then
64		echo "selftests: test_xdp_redirect $xdpmode [PASS]";
65	else
66		ret=1
67		echo "selftests: test_xdp_redirect $xdpmode [FAILED]";
68	fi
69
70	cleanup
71}
72
73set -e
74trap cleanup 2 3 6 9
75
76test_xdp_redirect xdpgeneric
77test_xdp_redirect xdpdrv
78
79exit $ret
80