samples/bpf: Add tunnel set/get tests.
[cascardo/linux.git] / samples / bpf / test_tunnel_bpf.sh
1 #!/bin/bash
2 # In Namespace 0 (at_ns0) using native tunnel
3 # Overlay IP: 10.1.1.100
4 # local 192.16.1.100 remote 192.16.1.200
5 # veth0 IP: 172.16.1.100, tunnel dev <type>00
6
7 # Out of Namespace using BPF set/get on lwtunnel
8 # Overlay IP: 10.1.1.200
9 # local 172.16.1.200 remote 172.16.1.100
10 # veth1 IP: 172.16.1.200, tunnel dev <type>11
11
12 set -e
13
14 function config_device {
15         ip netns add at_ns0
16         ip link add veth0 type veth peer name veth1
17         ip link set veth0 netns at_ns0
18         ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
19         ip netns exec at_ns0 ip link set dev veth0 up
20         ip link set dev veth1 up
21         ip addr add dev veth1 172.16.1.200/24
22 }
23
24 function add_gre_tunnel {
25         # in namespace
26         ip netns exec at_ns0 \
27                 ip link add dev $DEV_NS type $TYPE key 2 local 172.16.1.100 remote 172.16.1.200
28         ip netns exec at_ns0 ip link set dev $DEV_NS up
29         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
30
31         # out of namespace
32         ip link add dev $DEV type $TYPE key 2 external
33         ip link set dev $DEV up
34         ip addr add dev $DEV 10.1.1.200/24
35 }
36
37 function add_vxlan_tunnel {
38         # Set static ARP entry here because iptables set-mark works
39         # on L3 packet, as a result not applying to ARP packets,
40         # causing errors at get_tunnel_{key/opt}.
41
42         # in namespace
43         ip netns exec at_ns0 \
44                 ip link add dev $DEV_NS type $TYPE id 2 dstport 4789 gbp remote 172.16.1.200
45         ip netns exec at_ns0 ip link set dev $DEV_NS address 52:54:00:d9:01:00 up
46         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
47         ip netns exec at_ns0 arp -s 10.1.1.200 52:54:00:d9:02:00
48         ip netns exec at_ns0 iptables -A OUTPUT -j MARK --set-mark 0x800FF
49
50         # out of namespace
51         ip link add dev $DEV type $TYPE external gbp dstport 4789
52         ip link set dev $DEV address 52:54:00:d9:02:00 up
53         ip addr add dev $DEV 10.1.1.200/24
54         arp -s 10.1.1.100 52:54:00:d9:01:00
55 }
56
57 function add_geneve_tunnel {
58         # in namespace
59         ip netns exec at_ns0 \
60                 ip link add dev $DEV_NS type $TYPE id 2 dstport 6081 remote 172.16.1.200
61         ip netns exec at_ns0 ip link set dev $DEV_NS up
62         ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
63
64         # out of namespace
65         ip link add dev $DEV type $TYPE dstport 6081 external
66         ip link set dev $DEV up
67         ip addr add dev $DEV 10.1.1.200/24
68 }
69
70 function attach_bpf {
71         DEV=$1
72         SET_TUNNEL=$2
73         GET_TUNNEL=$3
74         tc qdisc add dev $DEV clsact
75         tc filter add dev $DEV egress bpf da obj tcbpf2_kern.o sec $SET_TUNNEL
76         tc filter add dev $DEV ingress bpf da obj tcbpf2_kern.o sec $GET_TUNNEL
77 }
78
79 function test_gre {
80         TYPE=gretap
81         DEV_NS=gretap00
82         DEV=gretap11
83         config_device
84         add_gre_tunnel
85         attach_bpf $DEV gre_set_tunnel gre_get_tunnel
86         ping -c 1 10.1.1.100
87         ip netns exec at_ns0 ping -c 1 10.1.1.200
88 }
89
90 function test_vxlan {
91         TYPE=vxlan
92         DEV_NS=vxlan00
93         DEV=vxlan11
94         config_device
95         add_vxlan_tunnel
96         attach_bpf $DEV vxlan_set_tunnel vxlan_get_tunnel
97         ping -c 1 10.1.1.100
98         ip netns exec at_ns0 ping -c 1 10.1.1.200
99 }
100
101 function test_geneve {
102         TYPE=geneve
103         DEV_NS=geneve00
104         DEV=geneve11
105         config_device
106         add_geneve_tunnel
107         attach_bpf $DEV geneve_set_tunnel geneve_get_tunnel
108         ping -c 1 10.1.1.100
109         ip netns exec at_ns0 ping -c 1 10.1.1.200
110 }
111
112 function cleanup {
113         ip netns delete at_ns0
114         ip link del veth1
115         ip link del $DEV
116 }
117
118 echo "Testing GRE tunnel..."
119 test_gre
120 cleanup
121 echo "Testing VXLAN tunnel..."
122 test_vxlan
123 cleanup
124 echo "Testing GENEVE tunnel..."
125 test_geneve
126 cleanup
127 echo "Success"