1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Validate cached routes in fib{6}_nh that is used by multiple prefixes.
5# Validate a different # exception is generated in h0 for each remote host.
6#
7#               h1
8#            /
9#    h0 - r1 -  h2
10#            \
11#               h3
12#
13# routing in h0 to hN is done with nexthop objects.
14
15PAUSE_ON_FAIL=no
16VERBOSE=0
17
18################################################################################
19# helpers
20
21log_test()
22{
23	local rc=$1
24	local expected=$2
25	local msg="$3"
26
27	if [ ${rc} -eq ${expected} ]; then
28		printf "TEST: %-60s  [ OK ]\n" "${msg}"
29		nsuccess=$((nsuccess+1))
30	else
31		ret=1
32		nfail=$((nfail+1))
33		printf "TEST: %-60s  [FAIL]\n" "${msg}"
34		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
35			echo
36			echo "hit enter to continue, 'q' to quit"
37			read a
38			[ "$a" = "q" ] && exit 1
39		fi
40	fi
41
42	[ "$VERBOSE" = "1" ] && echo
43}
44
45run_cmd()
46{
47	local cmd="$*"
48	local out
49	local rc
50
51	if [ "$VERBOSE" = "1" ]; then
52		echo "COMMAND: $cmd"
53	fi
54
55	out=$(eval $cmd 2>&1)
56	rc=$?
57	if [ "$VERBOSE" = "1" -a -n "$out" ]; then
58		echo "$out"
59	fi
60
61	[ "$VERBOSE" = "1" ] && echo
62
63	return $rc
64}
65
66################################################################################
67# config
68
69create_ns()
70{
71	local ns=${1}
72
73	ip netns del ${ns} 2>/dev/null
74
75	ip netns add ${ns}
76	ip -netns ${ns} addr add 127.0.0.1/8 dev lo
77	ip -netns ${ns} link set lo up
78
79	ip netns exec ${ns} sysctl -q -w net.ipv6.conf.all.keep_addr_on_down=1
80	case ${ns} in
81	h*)
82		ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=0
83		;;
84	r*)
85		ip netns exec $ns sysctl -q -w net.ipv4.ip_forward=1
86		ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=1
87		;;
88	esac
89}
90
91setup()
92{
93	local ns
94	local i
95
96	#set -e
97
98	for ns in h0 r1 h1 h2 h3
99	do
100		create_ns ${ns}
101	done
102
103	#
104	# create interconnects
105	#
106
107	for i in 0 1 2 3
108	do
109		ip -netns h${i} li add eth0 type veth peer name r1h${i}
110		ip -netns h${i} li set eth0 up
111		ip -netns h${i} li set r1h${i} netns r1 name eth${i} up
112
113		ip -netns h${i}    addr add dev eth0 172.16.10${i}.1/24
114		ip -netns h${i} -6 addr add dev eth0 2001:db8:10${i}::1/64
115		ip -netns r1    addr add dev eth${i} 172.16.10${i}.254/24
116		ip -netns r1 -6 addr add dev eth${i} 2001:db8:10${i}::64/64
117	done
118
119	ip -netns h0 nexthop add id 4 via 172.16.100.254 dev eth0
120	ip -netns h0 nexthop add id 6 via 2001:db8:100::64 dev eth0
121
122	# routing from h0 to h1-h3 and back
123	for i in 1 2 3
124	do
125		ip -netns h0    ro add 172.16.10${i}.0/24 nhid 4
126		ip -netns h${i} ro add 172.16.100.0/24 via 172.16.10${i}.254
127
128		ip -netns h0    -6 ro add 2001:db8:10${i}::/64 nhid 6
129		ip -netns h${i} -6 ro add 2001:db8:100::/64 via 2001:db8:10${i}::64
130	done
131
132	if [ "$VERBOSE" = "1" ]; then
133		echo
134		echo "host 1 config"
135		ip -netns h0 li sh
136		ip -netns h0 ro sh
137		ip -netns h0 -6 ro sh
138	fi
139
140	#set +e
141}
142
143cleanup()
144{
145	for n in h1 r1 h2 h3 h4
146	do
147		ip netns del ${n} 2>/dev/null
148	done
149}
150
151change_mtu()
152{
153	local hostid=$1
154	local mtu=$2
155
156	run_cmd ip -netns h${hostid} li set eth0 mtu ${mtu}
157	run_cmd ip -netns r1 li set eth${hostid} mtu ${mtu}
158}
159
160################################################################################
161# validate exceptions
162
163validate_v4_exception()
164{
165	local i=$1
166	local mtu=$2
167	local ping_sz=$3
168	local dst="172.16.10${i}.1"
169	local h0=172.16.100.1
170	local r1=172.16.100.254
171	local rc
172
173	if [ ${ping_sz} != "0" ]; then
174		run_cmd ip netns exec h0 ping -s ${ping_sz} -c5 -w5 ${dst}
175	fi
176
177	if [ "$VERBOSE" = "1" ]; then
178		echo "Route get"
179		ip -netns h0 ro get ${dst}
180		echo "Searching for:"
181		echo "    cache .* mtu ${mtu}"
182		echo
183	fi
184
185	ip -netns h0 ro get ${dst} | \
186	grep -q "cache .* mtu ${mtu}"
187	rc=$?
188
189	log_test $rc 0 "IPv4: host 0 to host ${i}, mtu ${mtu}"
190}
191
192validate_v6_exception()
193{
194	local i=$1
195	local mtu=$2
196	local ping_sz=$3
197	local dst="2001:db8:10${i}::1"
198	local h0=2001:db8:100::1
199	local r1=2001:db8:100::64
200	local rc
201
202	if [ ${ping_sz} != "0" ]; then
203		run_cmd ip netns exec h0 ping6 -s ${ping_sz} -c5 -w5 ${dst}
204	fi
205
206	if [ "$VERBOSE" = "1" ]; then
207		echo "Route get"
208		ip -netns h0 -6 ro get ${dst}
209		echo "Searching for:"
210		echo "    ${dst} from :: via ${r1} dev eth0 src ${h0} .* mtu ${mtu}"
211		echo
212	fi
213
214	ip -netns h0 -6 ro get ${dst} | \
215	grep -q "${dst} from :: via ${r1} dev eth0 src ${h0} .* mtu ${mtu}"
216	rc=$?
217
218	log_test $rc 0 "IPv6: host 0 to host ${i}, mtu ${mtu}"
219}
220
221################################################################################
222# main
223
224while getopts :pv o
225do
226	case $o in
227		p) PAUSE_ON_FAIL=yes;;
228		v) VERBOSE=1;;
229	esac
230done
231
232cleanup
233setup
234sleep 2
235
236cpus=$(cat  /sys/devices/system/cpu/online)
237cpus="$(seq ${cpus/-/ })"
238ret=0
239for i in 1 2 3
240do
241	# generate a cached route per-cpu
242	for c in ${cpus}; do
243		run_cmd taskset -c ${c} ip netns exec h0 ping -c1 -w1 172.16.10${i}.1
244		[ $? -ne 0 ] && printf "\nERROR: ping to h${i} failed\n" && ret=1
245
246		run_cmd taskset -c ${c} ip netns exec h0 ping6 -c1 -w1 2001:db8:10${i}::1
247		[ $? -ne 0 ] && printf "\nERROR: ping6 to h${i} failed\n" && ret=1
248
249		[ $ret -ne 0 ] && break
250	done
251	[ $ret -ne 0 ] && break
252done
253
254if [ $ret -eq 0 ]; then
255	# generate different exceptions in h0 for h1, h2 and h3
256	change_mtu 1 1300
257	validate_v4_exception 1 1300 1350
258	validate_v6_exception 1 1300 1350
259	echo
260
261	change_mtu 2 1350
262	validate_v4_exception 2 1350 1400
263	validate_v6_exception 2 1350 1400
264	echo
265
266	change_mtu 3 1400
267	validate_v4_exception 3 1400 1450
268	validate_v6_exception 3 1400 1450
269	echo
270
271	validate_v4_exception 1 1300 0
272	validate_v6_exception 1 1300 0
273	echo
274
275	validate_v4_exception 2 1350 0
276	validate_v6_exception 2 1350 0
277	echo
278
279	validate_v4_exception 3 1400 0
280	validate_v6_exception 3 1400 0
281
282	# targeted deletes to trigger cleanup paths in kernel
283	ip -netns h0 ro del 172.16.102.0/24 nhid 4
284	ip -netns h0 -6 ro del 2001:db8:102::/64 nhid 6
285
286	ip -netns h0 nexthop del id 4
287	ip -netns h0 nexthop del id 6
288fi
289
290cleanup
291