aboutsummaryrefslogtreecommitdiff
path: root/samples/bpf/test_ipip.sh
blob: 196925403ab4f307562a92f2303d8c7bd1bcbd65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash

function config_device {
	ip netns add at_ns0
	ip netns add at_ns1
	ip netns add at_ns2
	ip link add veth0 type veth peer name veth0b
	ip link add veth1 type veth peer name veth1b
	ip link add veth2 type veth peer name veth2b
	ip link set veth0b up
	ip link set veth1b up
	ip link set veth2b up
	ip link set dev veth0b mtu 1500
	ip link set dev veth1b mtu 1500
	ip link set dev veth2b mtu 1500
	ip link set veth0 netns at_ns0
	ip link set veth1 netns at_ns1
	ip link set veth2 netns at_ns2
	ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
	ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad
	ip netns exec at_ns0 ip link set dev veth0 up
	ip netns exec at_ns1 ip addr add 172.16.1.101/24 dev veth1
	ip netns exec at_ns1 ip addr add 2401:db00::2/64 dev veth1 nodad
	ip netns exec at_ns1 ip link set dev veth1 up
	ip netns exec at_ns2 ip addr add 172.16.1.200/24 dev veth2
	ip netns exec at_ns2 ip addr add 2401:db00::3/64 dev veth2 nodad
	ip netns exec at_ns2 ip link set dev veth2 up
	ip link add br0 type bridge
	ip link set br0 up
	ip link set dev br0 mtu 1500
	ip link set veth0b master br0
	ip link set veth1b master br0
	ip link set veth2b master br0
}

function add_ipip_tunnel {
	ip netns exec at_ns0 \
		ip link add dev $DEV_NS type ipip local 172.16.1.100 remote 172.16.1.200
	ip netns exec at_ns0 ip link set dev $DEV_NS up
	ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
	ip netns exec at_ns1 \
		ip link add dev $DEV_NS type ipip local 172.16.1.101 remote 172.16.1.200
	ip netns exec at_ns1 ip link set dev $DEV_NS up
	# same inner IP address in at_ns0 and at_ns1
	ip netns exec at_ns1 ip addr add dev $DEV_NS 10.1.1.100/24

	ip netns exec at_ns2 ip link add dev $DEV type ipip external
	ip netns exec at_ns2 ip link set dev $DEV up
	ip netns exec at_ns2 ip addr add dev $DEV 10.1.1.200/24
}

function add_ipip6_tunnel {
	ip netns exec at_ns0 \
		ip link add dev $DEV_NS type ip6tnl mode ipip6 local 2401:db00::1/64 remote 2401:db00::3/64
	ip netns exec at_ns0 ip link set dev $DEV_NS up
	ip netns exec at_ns0 ip addr add dev $DEV_NS 10.1.1.100/24
	ip netns exec at_ns1 \
		ip link add dev $DEV_NS type ip6tnl mode ipip6 local 2401:db00::2/64 remote 2401:db00::3/64
	ip netns exec at_ns1 ip link set dev $DEV_NS up
	# same inner IP address in at_ns0 and at_ns1
	ip netns exec at_ns1 ip addr add dev $DEV_NS 10.1.1.100/24

	ip netns exec at_ns2 ip link add dev $DEV type ip6tnl mode ipip6 external
	ip netns exec at_ns2 ip link set dev $DEV up
	ip netns exec at_ns2 ip addr add dev $DEV 10.1.1.200/24
}

function add_ip6ip6_tunnel {
	ip netns exec at_ns0 \
		ip link add dev $DEV_NS type ip6tnl mode ip6ip6 local 2401:db00::1/64 remote 2401:db00::3/64
	ip netns exec at_ns0 ip link set dev $DEV_NS up
	ip netns exec at_ns0 ip addr add dev $DEV_NS 2601:646::1/64
	ip netns exec at_ns1 \
		ip link add dev $DEV_NS type ip6tnl mode ip6ip6 local 2401:db00::2/64 remote 2401:db00::3/64
	ip netns exec at_ns1 ip link set dev $DEV_NS up
	# same inner IP address in at_ns0 and at_ns1
	ip netns exec at_ns1 ip addr add dev $DEV_NS 2601:646::1/64

	ip netns exec at_ns2 ip link add dev $DEV type ip6tnl mode ip6ip6 external
	ip netns exec at_ns2 ip link set dev $DEV up
	ip netns exec at_ns2 ip addr add dev $DEV 2601:646::2/64
}

function attach_bpf {
	DEV=$1
	SET_TUNNEL=$2
	GET_TUNNEL=$3
	ip netns exec at_ns2 tc qdisc add dev $DEV clsact
	ip netns exec at_ns2 tc filter add dev $DEV egress bpf da obj tcbpf2_kern.o sec $SET_TUNNEL
	ip netns exec at_ns2 tc filter add dev $DEV ingress bpf da obj tcbpf2_kern.o sec $GET_TUNNEL
}

function test_ipip {
	DEV_NS=ipip_std
	DEV=ipip_bpf
	config_device
#	tcpdump -nei br0 &
	cat /sys/kernel/debug/tracing/trace_pipe &

	add_ipip_tunnel
	attach_bpf $DEV ipip_set_tunnel ipip_get_tunnel

	ip netns exec at_ns0 ping -c 1 10.1.1.200
	ip netns exec at_ns2 ping -c 1 10.1.1.100
	ip netns exec at_ns0 iperf -sD -p 5200 > /dev/null
	ip netns exec at_ns1 iperf -sD -p 5201 > /dev/null
	sleep 0.2
	# tcp check _same_ IP over different tunnels
	ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5200
	ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5201
	cleanup
}

# IPv4 over IPv6 tunnel
function test_ipip6 {
	DEV_NS=ipip_std
	DEV=ipip_bpf
	config_device
#	tcpdump -nei br0 &
	cat /sys/kernel/debug/tracing/trace_pipe &

	add_ipip6_tunnel
	attach_bpf $DEV ipip6_set_tunnel ipip6_get_tunnel

	ip netns exec at_ns0 ping -c 1 10.1.1.200
	ip netns exec at_ns2 ping -c 1 10.1.1.100
	ip netns exec at_ns0 iperf -sD -p 5200 > /dev/null
	ip netns exec at_ns1 iperf -sD -p 5201 > /dev/null
	sleep 0.2
	# tcp check _same_ IP over different tunnels
	ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5200
	ip netns exec at_ns2 iperf -c 10.1.1.100 -n 5k -p 5201
	cleanup
}

# IPv6 over IPv6 tunnel
function test_ip6ip6 {
	DEV_NS=ipip_std
	DEV=ipip_bpf
	config_device
#	tcpdump -nei br0 &
	cat /sys/kernel/debug/tracing/trace_pipe &

	add_ip6ip6_tunnel
	attach_bpf $DEV ip6ip6_set_tunnel ip6ip6_get_tunnel

	ip netns exec at_ns0 ping -6 -c 1 2601:646::2
	ip netns exec at_ns2 ping -6 -c 1 2601:646::1
	ip netns exec at_ns0 iperf -6sD -p 5200 > /dev/null
	ip netns exec at_ns1 iperf -6sD -p 5201 > /dev/null
	sleep 0.2
	# tcp check _same_ IP over different tunnels
	ip netns exec at_ns2 iperf -6c 2601:646::1 -n 5k -p 5200
	ip netns exec at_ns2 iperf -6c 2601:646::1 -n 5k -p 5201
	cleanup
}

function cleanup {
	set +ex
	pkill iperf
	ip netns delete at_ns0
	ip netns delete at_ns1
	ip netns delete at_ns2
	ip link del veth0
	ip link del veth1
	ip link del veth2
	ip link del br0
	pkill tcpdump
	pkill cat
	set -ex
}

cleanup
echo "Testing IP tunnels..."
test_ipip
test_ipip6
test_ip6ip6
echo "*** PASS ***"