brintos

brintos / linux-shallow public Read only

0
0
Text · 5.8 KiB · e852486 Raw
291 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03#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#               h18#            /9#    h0 - r1 -  h210#            \11#               h312#13# routing in h0 to hN is done with nexthop objects.14 15source lib.sh16PAUSE_ON_FAIL=no17VERBOSE=018 19which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)20 21################################################################################22# helpers23 24log_test()25{26	local rc=$127	local expected=$228	local msg="$3"29 30	if [ ${rc} -eq ${expected} ]; then31		printf "TEST: %-60s  [ OK ]\n" "${msg}"32		nsuccess=$((nsuccess+1))33	else34		ret=135		nfail=$((nfail+1))36		printf "TEST: %-60s  [FAIL]\n" "${msg}"37		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then38			echo39			echo "hit enter to continue, 'q' to quit"40			read a41			[ "$a" = "q" ] && exit 142		fi43	fi44 45	[ "$VERBOSE" = "1" ] && echo46}47 48run_cmd()49{50	local cmd="$*"51	local out52	local rc53 54	if [ "$VERBOSE" = "1" ]; then55		echo "COMMAND: $cmd"56	fi57 58	out=$(eval $cmd 2>&1)59	rc=$?60	if [ "$VERBOSE" = "1" -a -n "$out" ]; then61		echo "$out"62	fi63 64	[ "$VERBOSE" = "1" ] && echo65 66	return $rc67}68 69################################################################################70# config71 72create_ns()73{74	local ns=${1}75 76	ip netns exec ${ns} sysctl -q -w net.ipv6.conf.all.keep_addr_on_down=177	case ${ns} in78	h*)79		ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=080		;;81	r*)82		ip netns exec $ns sysctl -q -w net.ipv4.ip_forward=183		ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=184		;;85	esac86}87 88setup()89{90	local ns91	local i92 93	#set -e94 95	setup_ns h0 r1 h1 h2 h396	h[0]=$h097	h[1]=$h198	h[2]=$h299	h[3]=$h3100	r[1]=$r1101	for ns in ${h[0]} ${r[1]} ${h[1]} ${h[2]} ${h[3]}102	do103		create_ns ${ns}104	done105 106	#107	# create interconnects108	#109 110	for i in 0 1 2 3111	do112		ip -netns ${h[$i]} li add eth0 type veth peer name r1h${i}113		ip -netns ${h[$i]} li set eth0 up114		ip -netns ${h[$i]} li set r1h${i} netns ${r[1]} name eth${i} up115 116		ip -netns ${h[$i]}    addr add dev eth0 172.16.10${i}.1/24117		ip -netns ${h[$i]} -6 addr add dev eth0 2001:db8:10${i}::1/64118		ip -netns ${r[1]}    addr add dev eth${i} 172.16.10${i}.254/24119		ip -netns ${r[1]} -6 addr add dev eth${i} 2001:db8:10${i}::64/64120	done121 122	ip -netns ${h[0]} nexthop add id 4 via 172.16.100.254 dev eth0123	ip -netns ${h[0]} nexthop add id 6 via 2001:db8:100::64 dev eth0124 125	# routing from ${h[0]} to h1-h3 and back126	for i in 1 2 3127	do128		ip -netns ${h[0]}    ro add 172.16.10${i}.0/24 nhid 4129		ip -netns ${h[$i]} ro add 172.16.100.0/24 via 172.16.10${i}.254130 131		ip -netns ${h[0]}    -6 ro add 2001:db8:10${i}::/64 nhid 6132		ip -netns ${h[$i]} -6 ro add 2001:db8:100::/64 via 2001:db8:10${i}::64133	done134 135	if [ "$VERBOSE" = "1" ]; then136		echo137		echo "host 1 config"138		ip -netns ${h[0]} li sh139		ip -netns ${h[0]} ro sh140		ip -netns ${h[0]} -6 ro sh141	fi142 143	#set +e144}145 146cleanup()147{148	cleanup_all_ns149}150 151change_mtu()152{153	local hostid=$1154	local mtu=$2155 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 exceptions162 163validate_v4_exception()164{165	local i=$1166	local mtu=$2167	local ping_sz=$3168	local dst="172.16.10${i}.1"169	local h0_ip=172.16.100.1170	local r1_ip=172.16.100.254171	local rc172 173	if [ ${ping_sz} != "0" ]; then174		run_cmd ip netns exec ${h0} ping -s ${ping_sz} -c5 -w5 ${dst}175	fi176 177	if [ "$VERBOSE" = "1" ]; then178		echo "Route get"179		ip -netns ${h0} ro get ${dst}180		echo "Searching for:"181		echo "    cache .* mtu ${mtu}"182		echo183	fi184 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=$1195	local mtu=$2196	local ping_sz=$3197	local dst="2001:db8:10${i}::1"198	local h0_ip=2001:db8:100::1199	local r1_ip=2001:db8:100::64200	local rc201 202	if [ ${ping_sz} != "0" ]; then203		run_cmd ip netns exec ${h0} ${ping6} -s ${ping_sz} -c5 -w5 ${dst}204	fi205 206	if [ "$VERBOSE" = "1" ]; then207		echo "Route get"208		ip -netns ${h0} -6 ro get ${dst}209		echo "Searching for:"210		echo "    ${dst}.* via ${r1_ip} dev eth0 src ${h0_ip} .* mtu ${mtu}"211		echo212	fi213 214	ip -netns ${h0} -6 ro get ${dst} | \215	grep -q "${dst}.* via ${r1_ip} dev eth0 src ${h0_ip} .* mtu ${mtu}"216	rc=$?217 218	log_test $rc 0 "IPv6: host 0 to host ${i}, mtu ${mtu}"219}220 221################################################################################222# main223 224while getopts :pv o225do226	case $o in227		p) PAUSE_ON_FAIL=yes;;228		v) VERBOSE=1;;229	esac230done231 232cleanup233setup234sleep 2235 236cpus=$(cat  /sys/devices/system/cpu/online)237cpus="$(seq ${cpus/-/ })"238ret=0239for i in 1 2 3240do241	# generate a cached route per-cpu242	for c in ${cpus}; do243		run_cmd taskset -c ${c} ip netns exec ${h0} ping -c1 -w1 172.16.10${i}.1244		[ $? -ne 0 ] && printf "\nERROR: ping to ${h[$i]} failed\n" && ret=1245 246		run_cmd taskset -c ${c} ip netns exec ${h0} ${ping6} -c1 -w1 2001:db8:10${i}::1247		[ $? -ne 0 ] && printf "\nERROR: ping6 to ${h[$i]} failed\n" && ret=1248 249		[ $ret -ne 0 ] && break250	done251	[ $ret -ne 0 ] && break252done253 254if [ $ret -eq 0 ]; then255	# generate different exceptions in h0 for h1, h2 and h3256	change_mtu 1 1300257	validate_v4_exception 1 1300 1350258	validate_v6_exception 1 1300 1350259	echo260 261	change_mtu 2 1350262	validate_v4_exception 2 1350 1400263	validate_v6_exception 2 1350 1400264	echo265 266	change_mtu 3 1400267	validate_v4_exception 3 1400 1450268	validate_v6_exception 3 1400 1450269	echo270 271	validate_v4_exception 1 1300 0272	validate_v6_exception 1 1300 0273	echo274 275	validate_v4_exception 2 1350 0276	validate_v6_exception 2 1350 0277	echo278 279	validate_v4_exception 3 1400 0280	validate_v6_exception 3 1400 0281 282	# targeted deletes to trigger cleanup paths in kernel283	ip -netns ${h0} ro del 172.16.102.0/24 nhid 4284	ip -netns ${h0} -6 ro del 2001:db8:102::/64 nhid 6285 286	ip -netns ${h0} nexthop del id 4287	ip -netns ${h0} nexthop del id 6288fi289 290cleanup291