1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4function config_device {
5	ip netns add at_ns0
6	ip netns add at_ns1
7	ip netns add at_ns2
8	ip link add veth0 type veth peer name veth0b
9	ip link add veth1 type veth peer name veth1b
10	ip link add veth2 type veth peer name veth2b
11	ip link set veth0b up
12	ip link set veth1b up
13	ip link set veth2b up
14	ip link set dev veth0b mtu 1500
15	ip link set dev veth1b mtu 1500
16	ip link set dev veth2b mtu 1500
17	ip link set veth0 netns at_ns0
18	ip link set veth1 netns at_ns1
19	ip link set veth2 netns at_ns2
20	ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
21	ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad
22	ip netns exec at_ns0 ip link set dev veth0 up
23	ip netns exec at_ns1 ip addr add 172.16.1.101/24 dev veth1
24	ip netns exec at_ns1 ip addr add 2401:db00::2/64 dev veth1 nodad
25	ip netns exec at_ns1 ip link set dev veth1 up
26	ip netns exec at_ns2 ip addr add 172.16.1.200/24 dev veth2
27	ip netns exec at_ns2 ip addr add 2401:db00::3/64 dev veth2 nodad
28	ip netns exec at_ns2 ip link set dev veth2 up
29	ip link add br0 type bridge
30	ip link set br0 up
31	ip link set dev br0 mtu 1500
32	ip link set veth0b master br0
33	ip link set veth1b master br0
34	ip link set veth2b master br0
35}
36
37function add_ipip_tunnel {
38	ip netns exec at_ns0 \
39		ip link add dev $DEV_NS type ipip local 172.16.1.100 remote 172.16.1.200
40	ip netns exec at_ns0 ip link set dev $DEV_NS up
41	ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
42	ip netns exec at_ns1 \
43		ip link add dev $DEV_NS type ipip local 172.16.1.101 remote 172.16.1.200
44	ip netns exec at_ns1 ip link set dev $DEV_NS up
45	# same inner IP address in at_ns0 and at_ns1
46	ip netns exec at_ns1 ip addr add dev $DEV_NS 10.1.1.100/24
47
48	ip netns exec at_ns2 ip link add dev $DEV type ipip external
49	ip netns exec at_ns2 ip link set dev $DEV up
50	ip netns exec at_ns2 ip addr add dev $DEV 10.1.1.200/24
51}
52
53function add_ipip6_tunnel {
54	ip netns exec at_ns0 \
55		ip link add dev $DEV_NS type ip6tnl mode ipip6 local 2401:db00::1/64 remote 2401:db00::3/64
56	ip netns exec at_ns0 ip link set dev $DEV_NS up
57	ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
58	ip netns exec at_ns1 \
59		ip link add dev $DEV_NS type ip6tnl mode ipip6 local 2401:db00::2/64 remote 2401:db00::3/64
60	ip netns exec at_ns1 ip link set dev $DEV_NS up
61	# same inner IP address in at_ns0 and at_ns1
62	ip netns exec at_ns1 ip addr add dev $DEV_NS 10.1.1.100/24
63
64	ip netns exec at_ns2 ip link add dev $DEV type ip6tnl mode ipip6 external
65	ip netns exec at_ns2 ip link set dev $DEV up
66	ip netns exec at_ns2 ip addr add dev $DEV 10.1.1.200/24
67}
68
69function add_ip6ip6_tunnel {
70	ip netns exec at_ns0 \
71		ip link add dev $DEV_NS type ip6tnl mode ip6ip6 local 2401:db00::1/64 remote 2401:db00::3/64
72	ip netns exec at_ns0 ip link set dev $DEV_NS up
73	ip netns exec at_ns0 ip addr add dev $DEV_NS 2601:646::1/64
74	ip netns exec at_ns1 \
75		ip link add dev $DEV_NS type ip6tnl mode ip6ip6 local 2401:db00::2/64 remote 2401:db00::3/64
76	ip netns exec at_ns1 ip link set dev $DEV_NS up
77	# same inner IP address in at_ns0 and at_ns1
78	ip netns exec at_ns1 ip addr add dev $DEV_NS 2601:646::1/64
79
80	ip netns exec at_ns2 ip link add dev $DEV type ip6tnl mode ip6ip6 external
81	ip netns exec at_ns2 ip link set dev $DEV up
82	ip netns exec at_ns2 ip addr add dev $DEV 2601:646::2/64
83}
84
85function attach_bpf {
86	DEV=$1
87	SET_TUNNEL=$2
88	GET_TUNNEL=$3
89	ip netns exec at_ns2 tc qdisc add dev $DEV clsact
90	ip netns exec at_ns2 tc filter add dev $DEV egress bpf da obj tcbpf2_kern.o sec $SET_TUNNEL
91	ip netns exec at_ns2 tc filter add dev $DEV ingress bpf da obj tcbpf2_kern.o sec $GET_TUNNEL
92}
93
94function test_ipip {
95	DEV_NS=ipip_std
96	DEV=ipip_bpf
97	config_device
98#	tcpdump -nei br0 &
99	cat /sys/kernel/debug/tracing/trace_pipe &
100
101	add_ipip_tunnel
102	attach_bpf $DEV ipip_set_tunnel ipip_get_tunnel
103
104	ip netns exec at_ns0 ping -c 1 10.1.1.200
105	ip netns exec at_ns2 ping -c 1 10.1.1.100
106	ip netns exec at_ns0 iperf -sD -p 5200 > /dev/null
107	ip netns exec at_ns1 iperf -sD -p 5201 > /dev/null
108	sleep 0.2
109	# tcp check _same_ IP over different tunnels
110	ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5200
111	ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5201
112	cleanup
113}
114
115# IPv4 over IPv6 tunnel
116function test_ipip6 {
117	DEV_NS=ipip_std
118	DEV=ipip_bpf
119	config_device
120#	tcpdump -nei br0 &
121	cat /sys/kernel/debug/tracing/trace_pipe &
122
123	add_ipip6_tunnel
124	attach_bpf $DEV ipip6_set_tunnel ipip6_get_tunnel
125
126	ip netns exec at_ns0 ping -c 1 10.1.1.200
127	ip netns exec at_ns2 ping -c 1 10.1.1.100
128	ip netns exec at_ns0 iperf -sD -p 5200 > /dev/null
129	ip netns exec at_ns1 iperf -sD -p 5201 > /dev/null
130	sleep 0.2
131	# tcp check _same_ IP over different tunnels
132	ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5200
133	ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5201
134	cleanup
135}
136
137# IPv6 over IPv6 tunnel
138function test_ip6ip6 {
139	DEV_NS=ipip_std
140	DEV=ipip_bpf
141	config_device
142#	tcpdump -nei br0 &
143	cat /sys/kernel/debug/tracing/trace_pipe &
144
145	add_ip6ip6_tunnel
146	attach_bpf $DEV ip6ip6_set_tunnel ip6ip6_get_tunnel
147
148	ip netns exec at_ns0 ping -6 -c 1 2601:646::2
149	ip netns exec at_ns2 ping -6 -c 1 2601:646::1
150	ip netns exec at_ns0 iperf -6sD -p 5200 > /dev/null
151	ip netns exec at_ns1 iperf -6sD -p 5201 > /dev/null
152	sleep 0.2
153	# tcp check _same_ IP over different tunnels
154	ip netns exec at_ns2 iperf -6c 2601:646::1 -n 5k -p 5200
155	ip netns exec at_ns2 iperf -6c 2601:646::1 -n 5k -p 5201
156	cleanup
157}
158
159function cleanup {
160	set +ex
161	pkill iperf
162	ip netns delete at_ns0
163	ip netns delete at_ns1
164	ip netns delete at_ns2
165	ip link del veth0
166	ip link del veth1
167	ip link del veth2
168	ip link del br0
169	pkill tcpdump
170	pkill cat
171	set -ex
172}
173
174cleanup
175echo "Testing IP tunnels..."
176test_ipip
177test_ipip6
178test_ip6ip6
179echo "*** PASS ***"
180