xref: /openbmc/linux/tools/testing/selftests/net/fib_tests.sh (revision 9977a8c3497a8f7f7f951994f298a8e4d961234f)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# This test is for checking IPv4 and IPv6 FIB behavior in response to
5# different events.
6
7ret=0
8
9check_err()
10{
11	if [ $ret -eq 0 ]; then
12		ret=$1
13	fi
14}
15
16check_fail()
17{
18	if [ $1 -eq 0 ]; then
19		ret=1
20	fi
21}
22
23netns_create()
24{
25	local testns=$1
26
27	ip netns add $testns
28	ip netns exec $testns ip link set dev lo up
29}
30
31fib_unreg_unicast_test()
32{
33	ret=0
34
35	netns_create "testns"
36
37	ip netns exec testns ip link add dummy0 type dummy
38	ip netns exec testns ip link set dev dummy0 up
39
40	ip netns exec testns ip address add 198.51.100.1/24 dev dummy0
41	ip netns exec testns ip -6 address add 2001:db8:1::1/64 dev dummy0
42
43	ip netns exec testns ip route get fibmatch 198.51.100.2 &> /dev/null
44	check_err $?
45	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::2 &> /dev/null
46	check_err $?
47
48	ip netns exec testns ip link del dev dummy0
49	check_err $?
50
51	ip netns exec testns ip route get fibmatch 198.51.100.2 &> /dev/null
52	check_fail $?
53	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::2 &> /dev/null
54	check_fail $?
55
56	ip netns del testns
57
58	if [ $ret -ne 0 ]; then
59		echo "FAIL: unicast route test"
60		return 1
61	fi
62	echo "PASS: unicast route test"
63}
64
65fib_unreg_multipath_test()
66{
67	ret=0
68
69	netns_create "testns"
70
71	ip netns exec testns ip link add dummy0 type dummy
72	ip netns exec testns ip link set dev dummy0 up
73
74	ip netns exec testns ip link add dummy1 type dummy
75	ip netns exec testns ip link set dev dummy1 up
76
77	ip netns exec testns ip address add 198.51.100.1/24 dev dummy0
78	ip netns exec testns ip -6 address add 2001:db8:1::1/64 dev dummy0
79
80	ip netns exec testns ip address add 192.0.2.1/24 dev dummy1
81	ip netns exec testns ip -6 address add 2001:db8:2::1/64 dev dummy1
82
83	ip netns exec testns ip route add 203.0.113.0/24 \
84		nexthop via 198.51.100.2 dev dummy0 \
85		nexthop via 192.0.2.2 dev dummy1
86	ip netns exec testns ip -6 route add 2001:db8:3::/64 \
87		nexthop via 2001:db8:1::2 dev dummy0 \
88		nexthop via 2001:db8:2::2 dev dummy1
89
90	ip netns exec testns ip route get fibmatch 203.0.113.1 &> /dev/null
91	check_err $?
92	ip netns exec testns ip -6 route get fibmatch 2001:db8:3::1 &> /dev/null
93	check_err $?
94
95	ip netns exec testns ip link del dev dummy0
96	check_err $?
97
98	ip netns exec testns ip route get fibmatch 203.0.113.1 &> /dev/null
99	check_fail $?
100	ip netns exec testns ip -6 route get fibmatch 2001:db8:3::1 &> /dev/null
101	# In IPv6 we do not flush the entire multipath route.
102	check_err $?
103
104	ip netns exec testns ip link del dev dummy1
105
106	ip netns del testns
107
108	if [ $ret -ne 0 ]; then
109		echo "FAIL: multipath route test"
110		return 1
111	fi
112	echo "PASS: multipath route test"
113}
114
115fib_unreg_test()
116{
117	echo "Running netdev unregister tests"
118
119	fib_unreg_unicast_test
120	fib_unreg_multipath_test
121}
122
123fib_down_unicast_test()
124{
125	ret=0
126
127	netns_create "testns"
128
129	ip netns exec testns ip link add dummy0 type dummy
130	ip netns exec testns ip link set dev dummy0 up
131
132	ip netns exec testns ip address add 198.51.100.1/24 dev dummy0
133	ip netns exec testns ip -6 address add 2001:db8:1::1/64 dev dummy0
134
135	ip netns exec testns ip route get fibmatch 198.51.100.2 &> /dev/null
136	check_err $?
137	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::2 &> /dev/null
138	check_err $?
139
140	ip netns exec testns ip link set dev dummy0 down
141	check_err $?
142
143	ip netns exec testns ip route get fibmatch 198.51.100.2 &> /dev/null
144	check_fail $?
145	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::2 &> /dev/null
146	check_fail $?
147
148	ip netns exec testns ip link del dev dummy0
149
150	ip netns del testns
151
152	if [ $ret -ne 0 ]; then
153		echo "FAIL: unicast route test"
154		return 1
155	fi
156	echo "PASS: unicast route test"
157}
158
159fib_down_multipath_test_do()
160{
161	local down_dev=$1
162	local up_dev=$2
163
164	ip netns exec testns ip route get fibmatch 203.0.113.1 \
165		oif $down_dev &> /dev/null
166	check_fail $?
167	ip netns exec testns ip -6 route get fibmatch 2001:db8:3::1 \
168		oif $down_dev &> /dev/null
169	check_fail $?
170
171	ip netns exec testns ip route get fibmatch 203.0.113.1 \
172		oif $up_dev &> /dev/null
173	check_err $?
174	ip netns exec testns ip -6 route get fibmatch 2001:db8:3::1 \
175		oif $up_dev &> /dev/null
176	check_err $?
177
178	ip netns exec testns ip route get fibmatch 203.0.113.1 | \
179		grep $down_dev | grep -q "dead linkdown"
180	check_err $?
181	ip netns exec testns ip -6 route get fibmatch 2001:db8:3::1 | \
182		grep $down_dev | grep -q "dead linkdown"
183	check_err $?
184
185	ip netns exec testns ip route get fibmatch 203.0.113.1 | \
186		grep $up_dev | grep -q "dead linkdown"
187	check_fail $?
188	ip netns exec testns ip -6 route get fibmatch 2001:db8:3::1 | \
189		grep $up_dev | grep -q "dead linkdown"
190	check_fail $?
191}
192
193fib_down_multipath_test()
194{
195	ret=0
196
197	netns_create "testns"
198
199	ip netns exec testns ip link add dummy0 type dummy
200	ip netns exec testns ip link set dev dummy0 up
201
202	ip netns exec testns ip link add dummy1 type dummy
203	ip netns exec testns ip link set dev dummy1 up
204
205	ip netns exec testns ip address add 198.51.100.1/24 dev dummy0
206	ip netns exec testns ip -6 address add 2001:db8:1::1/64 dev dummy0
207
208	ip netns exec testns ip address add 192.0.2.1/24 dev dummy1
209	ip netns exec testns ip -6 address add 2001:db8:2::1/64 dev dummy1
210
211	ip netns exec testns ip route add 203.0.113.0/24 \
212		nexthop via 198.51.100.2 dev dummy0 \
213		nexthop via 192.0.2.2 dev dummy1
214	ip netns exec testns ip -6 route add 2001:db8:3::/64 \
215		nexthop via 2001:db8:1::2 dev dummy0 \
216		nexthop via 2001:db8:2::2 dev dummy1
217
218	ip netns exec testns ip route get fibmatch 203.0.113.1 &> /dev/null
219	check_err $?
220	ip netns exec testns ip -6 route get fibmatch 2001:db8:3::1 &> /dev/null
221	check_err $?
222
223	ip netns exec testns ip link set dev dummy0 down
224	check_err $?
225
226	fib_down_multipath_test_do "dummy0" "dummy1"
227
228	ip netns exec testns ip link set dev dummy0 up
229	check_err $?
230	ip netns exec testns ip link set dev dummy1 down
231	check_err $?
232
233	fib_down_multipath_test_do "dummy1" "dummy0"
234
235	ip netns exec testns ip link set dev dummy0 down
236	check_err $?
237
238	ip netns exec testns ip route get fibmatch 203.0.113.1 &> /dev/null
239	check_fail $?
240	ip netns exec testns ip -6 route get fibmatch 2001:db8:3::1 &> /dev/null
241	check_fail $?
242
243	ip netns exec testns ip link del dev dummy1
244	ip netns exec testns ip link del dev dummy0
245
246	ip netns del testns
247
248	if [ $ret -ne 0 ]; then
249		echo "FAIL: multipath route test"
250		return 1
251	fi
252	echo "PASS: multipath route test"
253}
254
255fib_down_test()
256{
257	echo "Running netdev down tests"
258
259	fib_down_unicast_test
260	fib_down_multipath_test
261}
262
263fib_carrier_local_test()
264{
265	ret=0
266
267	# Local routes should not be affected when carrier changes.
268	netns_create "testns"
269
270	ip netns exec testns ip link add dummy0 type dummy
271	ip netns exec testns ip link set dev dummy0 up
272
273	ip netns exec testns ip link set dev dummy0 carrier on
274
275	ip netns exec testns ip address add 198.51.100.1/24 dev dummy0
276	ip netns exec testns ip -6 address add 2001:db8:1::1/64 dev dummy0
277
278	ip netns exec testns ip route get fibmatch 198.51.100.1 &> /dev/null
279	check_err $?
280	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::1 &> /dev/null
281	check_err $?
282
283	ip netns exec testns ip route get fibmatch 198.51.100.1 | \
284		grep -q "linkdown"
285	check_fail $?
286	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::1 | \
287		grep -q "linkdown"
288	check_fail $?
289
290	ip netns exec testns ip link set dev dummy0 carrier off
291
292	ip netns exec testns ip route get fibmatch 198.51.100.1 &> /dev/null
293	check_err $?
294	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::1 &> /dev/null
295	check_err $?
296
297	ip netns exec testns ip route get fibmatch 198.51.100.1 | \
298		grep -q "linkdown"
299	check_fail $?
300	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::1 | \
301		grep -q "linkdown"
302	check_fail $?
303
304	ip netns exec testns ip address add 192.0.2.1/24 dev dummy0
305	ip netns exec testns ip -6 address add 2001:db8:2::1/64 dev dummy0
306
307	ip netns exec testns ip route get fibmatch 192.0.2.1 &> /dev/null
308	check_err $?
309	ip netns exec testns ip -6 route get fibmatch 2001:db8:2::1 &> /dev/null
310	check_err $?
311
312	ip netns exec testns ip route get fibmatch 192.0.2.1 | \
313		grep -q "linkdown"
314	check_fail $?
315	ip netns exec testns ip -6 route get fibmatch 2001:db8:2::1 | \
316		grep -q "linkdown"
317	check_fail $?
318
319	ip netns exec testns ip link del dev dummy0
320
321	ip netns del testns
322
323	if [ $ret -ne 0 ]; then
324		echo "FAIL: local route carrier test"
325		return 1
326	fi
327	echo "PASS: local route carrier test"
328}
329
330fib_carrier_unicast_test()
331{
332	ret=0
333
334	netns_create "testns"
335
336	ip netns exec testns ip link add dummy0 type dummy
337	ip netns exec testns ip link set dev dummy0 up
338
339	ip netns exec testns ip link set dev dummy0 carrier on
340
341	ip netns exec testns ip address add 198.51.100.1/24 dev dummy0
342	ip netns exec testns ip -6 address add 2001:db8:1::1/64 dev dummy0
343
344	ip netns exec testns ip route get fibmatch 198.51.100.2 &> /dev/null
345	check_err $?
346	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::2 &> /dev/null
347	check_err $?
348
349	ip netns exec testns ip route get fibmatch 198.51.100.2 | \
350		grep -q "linkdown"
351	check_fail $?
352	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::2 | \
353		grep -q "linkdown"
354	check_fail $?
355
356	ip netns exec testns ip link set dev dummy0 carrier off
357
358	ip netns exec testns ip route get fibmatch 198.51.100.2 &> /dev/null
359	check_err $?
360	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::2 &> /dev/null
361	check_err $?
362
363	ip netns exec testns ip route get fibmatch 198.51.100.2 | \
364		grep -q "linkdown"
365	check_err $?
366	ip netns exec testns ip -6 route get fibmatch 2001:db8:1::2 | \
367		grep -q "linkdown"
368	check_err $?
369
370	ip netns exec testns ip address add 192.0.2.1/24 dev dummy0
371	ip netns exec testns ip -6 address add 2001:db8:2::1/64 dev dummy0
372
373	ip netns exec testns ip route get fibmatch 192.0.2.2 &> /dev/null
374	check_err $?
375	ip netns exec testns ip -6 route get fibmatch 2001:db8:2::2 &> /dev/null
376	check_err $?
377
378	ip netns exec testns ip route get fibmatch 192.0.2.2 | \
379		grep -q "linkdown"
380	check_err $?
381	ip netns exec testns ip -6 route get fibmatch 2001:db8:2::2 | \
382		grep -q "linkdown"
383	check_err $?
384
385	ip netns exec testns ip link del dev dummy0
386
387	ip netns del testns
388
389	if [ $ret -ne 0 ]; then
390		echo "FAIL: unicast route carrier test"
391		return 1
392	fi
393	echo "PASS: unicast route carrier test"
394}
395
396fib_carrier_test()
397{
398	echo "Running netdev carrier change tests"
399
400	fib_carrier_local_test
401	fib_carrier_unicast_test
402}
403
404fib_test()
405{
406	fib_unreg_test
407	fib_down_test
408	fib_carrier_test
409}
410
411if [ "$(id -u)" -ne 0 ];then
412	echo "SKIP: Need root privileges"
413	exit 0
414fi
415
416if [ ! -x "$(command -v ip)" ]; then
417	echo "SKIP: Could not run test without ip tool"
418	exit 0
419fi
420
421ip route help 2>&1 | grep -q fibmatch
422if [ $? -ne 0 ]; then
423	echo "SKIP: iproute2 too old, missing fibmatch"
424	exit 0
425fi
426
427fib_test
428
429exit $ret
430