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
13ret=0
14
15setup()
16{
17
18	local xdpmode=$1
19
20	ip netns add ns1
21	ip netns add ns2
22
23	ip link add veth1 index 111 type veth peer name veth11 netns ns1
24	ip link add veth2 index 222 type veth peer name veth22 netns ns2
25
26	ip link set veth1 up
27	ip link set veth2 up
28	ip -n ns1 link set dev veth11 up
29	ip -n ns2 link set dev veth22 up
30
31	ip -n ns1 addr add 10.1.1.11/24 dev veth11
32	ip -n ns2 addr add 10.1.1.22/24 dev veth22
33}
34
35cleanup()
36{
37	ip link del veth1 2> /dev/null
38	ip link del veth2 2> /dev/null
39	ip netns del ns1 2> /dev/null
40	ip netns del ns2 2> /dev/null
41}
42
43test_xdp_redirect()
44{
45	local xdpmode=$1
46
47	setup
48
49	ip link set dev veth1 $xdpmode off &> /dev/null
50	if [ $? -ne 0 ];then
51		echo "selftests: test_xdp_redirect $xdpmode [SKIP]"
52		return 0
53	fi
54
55	ip -n ns1 link set veth11 $xdpmode obj xdp_dummy.o sec xdp &> /dev/null
56	ip -n ns2 link set veth22 $xdpmode obj xdp_dummy.o sec xdp &> /dev/null
57	ip link set dev veth1 $xdpmode obj test_xdp_redirect.o sec redirect_to_222 &> /dev/null
58	ip link set dev veth2 $xdpmode obj test_xdp_redirect.o sec redirect_to_111 &> /dev/null
59
60	if ip netns exec ns1 ping -c 1 10.1.1.22 &> /dev/null &&
61	   ip netns exec ns2 ping -c 1 10.1.1.11 &> /dev/null; then
62		echo "selftests: test_xdp_redirect $xdpmode [PASS]";
63	else
64		ret=1
65		echo "selftests: test_xdp_redirect $xdpmode [FAILED]";
66	fi
67
68	cleanup
69}
70
71set -e
72trap cleanup 2 3 6 9
73
74test_xdp_redirect xdpgeneric
75test_xdp_redirect xdpdrv
76
77exit $ret
78