Path: blob/master/tools/testing/selftests/drivers/net/netdevsim/fib_notifications.sh
26299 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.023lib_dir=$(dirname $0)/../../../net/forwarding45ALL_TESTS="6ipv4_route_addition_test7ipv4_route_deletion_test8ipv4_route_replacement_test9ipv4_route_offload_failed_test10ipv6_route_addition_test11ipv6_route_deletion_test12ipv6_route_replacement_test13ipv6_route_offload_failed_test14"1516NETDEVSIM_PATH=/sys/bus/netdevsim/17DEV_ADDR=133718DEV=netdevsim${DEV_ADDR}19DEVLINK_DEV=netdevsim/${DEV}20SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV/net/21DEBUGFS_DIR=/sys/kernel/debug/netdevsim/$DEV/22NUM_NETIFS=023source $lib_dir/lib.sh2425check_rt_offload_failed()26{27local outfile=$1; shift28local line2930# Make sure that the first notification was emitted without31# RTM_F_OFFLOAD_FAILED flag and the second with RTM_F_OFFLOAD_FAILED32# flag33head -n 1 $outfile | grep -q "rt_offload_failed"34if [[ $? -eq 0 ]]; then35return 136fi3738head -n 2 $outfile | tail -n 1 | grep -q "rt_offload_failed"39}4041check_rt_trap()42{43local outfile=$1; shift44local line4546# Make sure that the first notification was emitted without RTM_F_TRAP47# flag and the second with RTM_F_TRAP flag48head -n 1 $outfile | grep -q "rt_trap"49if [[ $? -eq 0 ]]; then50return 151fi5253head -n 2 $outfile | tail -n 1 | grep -q "rt_trap"54}5556route_notify_check()57{58local outfile=$1; shift59local expected_num_lines=$1; shift60local offload_failed=${1:-0}; shift6162# check the monitor results63lines=`wc -l $outfile | cut "-d " -f1`64test $lines -eq $expected_num_lines65check_err $? "$expected_num_lines notifications were expected but $lines were received"6667if [[ $expected_num_lines -eq 1 ]]; then68return69fi7071if [[ $offload_failed -eq 0 ]]; then72check_rt_trap $outfile73check_err $? "Wrong RTM_F_TRAP flags in notifications"74else75check_rt_offload_failed $outfile76check_err $? "Wrong RTM_F_OFFLOAD_FAILED flags in notifications"77fi78}7980route_addition_check()81{82local ip=$1; shift83local notify=$1; shift84local route=$1; shift85local expected_num_notifications=$1; shift86local offload_failed=${1:-0}; shift8788ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify8990local outfile=$(mktemp)9192$IP monitor route &> $outfile &93sleep 194$IP route add $route dev dummy195sleep 196kill_process %%9798route_notify_check $outfile $expected_num_notifications $offload_failed99rm -f $outfile100101$IP route del $route dev dummy1102}103104ipv4_route_addition_test()105{106RET=0107108local ip="ipv4"109local route=192.0.2.0/24110111# Make sure a single notification will be emitted for the programmed112# route.113local notify=0114local expected_num_notifications=1115# route_addition_check will assign value to RET.116route_addition_check $ip $notify $route $expected_num_notifications117118# Make sure two notifications will be emitted for the programmed route.119notify=1120expected_num_notifications=2121route_addition_check $ip $notify $route $expected_num_notifications122123# notify=2 means emit notifications only for failed route installation,124# make sure a single notification will be emitted for the programmed125# route.126notify=2127expected_num_notifications=1128route_addition_check $ip $notify $route $expected_num_notifications129130log_test "IPv4 route addition"131}132133route_deletion_check()134{135local ip=$1; shift136local notify=$1; shift137local route=$1; shift138local expected_num_notifications=$1; shift139140ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify141$IP route add $route dev dummy1142sleep 1143144local outfile=$(mktemp)145146$IP monitor route &> $outfile &147sleep 1148$IP route del $route dev dummy1149sleep 1150kill_process %%151152route_notify_check $outfile $expected_num_notifications153rm -f $outfile154}155156ipv4_route_deletion_test()157{158RET=0159160local ip="ipv4"161local route=192.0.2.0/24162local expected_num_notifications=1163164# Make sure a single notification will be emitted for the deleted route,165# regardless of fib_notify_on_flag_change value.166local notify=0167# route_deletion_check will assign value to RET.168route_deletion_check $ip $notify $route $expected_num_notifications169170notify=1171route_deletion_check $ip $notify $route $expected_num_notifications172173log_test "IPv4 route deletion"174}175176route_replacement_check()177{178local ip=$1; shift179local notify=$1; shift180local route=$1; shift181local expected_num_notifications=$1; shift182183ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify184$IP route add $route dev dummy1185sleep 1186187local outfile=$(mktemp)188189$IP monitor route &> $outfile &190sleep 1191$IP route replace $route dev dummy2192sleep 1193kill_process %%194195route_notify_check $outfile $expected_num_notifications196rm -f $outfile197198$IP route del $route dev dummy2199}200201ipv4_route_replacement_test()202{203RET=0204205local ip="ipv4"206local route=192.0.2.0/24207208$IP link add name dummy2 type dummy209$IP link set dev dummy2 up210211# Make sure a single notification will be emitted for the new route.212local notify=0213local expected_num_notifications=1214# route_replacement_check will assign value to RET.215route_replacement_check $ip $notify $route $expected_num_notifications216217# Make sure two notifications will be emitted for the new route.218notify=1219expected_num_notifications=2220route_replacement_check $ip $notify $route $expected_num_notifications221222# notify=2 means emit notifications only for failed route installation,223# make sure a single notification will be emitted for the new route.224notify=2225expected_num_notifications=1226route_replacement_check $ip $notify $route $expected_num_notifications227228$IP link del name dummy2229230log_test "IPv4 route replacement"231}232233ipv4_route_offload_failed_test()234{235236RET=0237238local ip="ipv4"239local route=192.0.2.0/24240local offload_failed=1241242echo "y"> $DEBUGFS_DIR/fib/fail_route_offload243check_err $? "Failed to setup route offload to fail"244245# Make sure a single notification will be emitted for the programmed246# route.247local notify=0248local expected_num_notifications=1249route_addition_check $ip $notify $route $expected_num_notifications \250$offload_failed251252# Make sure two notifications will be emitted for the new route.253notify=1254expected_num_notifications=2255route_addition_check $ip $notify $route $expected_num_notifications \256$offload_failed257258# notify=2 means emit notifications only for failed route installation,259# make sure two notifications will be emitted for the new route.260notify=2261expected_num_notifications=2262route_addition_check $ip $notify $route $expected_num_notifications \263$offload_failed264265echo "n"> $DEBUGFS_DIR/fib/fail_route_offload266check_err $? "Failed to setup route offload not to fail"267268log_test "IPv4 route offload failed"269}270271ipv6_route_addition_test()272{273RET=0274275local ip="ipv6"276local route=2001:db8:1::/64277278# Make sure a single notification will be emitted for the programmed279# route.280local notify=0281local expected_num_notifications=1282route_addition_check $ip $notify $route $expected_num_notifications283284# Make sure two notifications will be emitted for the programmed route.285notify=1286expected_num_notifications=2287route_addition_check $ip $notify $route $expected_num_notifications288289# notify=2 means emit notifications only for failed route installation,290# make sure a single notification will be emitted for the programmed291# route.292notify=2293expected_num_notifications=1294route_addition_check $ip $notify $route $expected_num_notifications295296log_test "IPv6 route addition"297}298299ipv6_route_deletion_test()300{301RET=0302303local ip="ipv6"304local route=2001:db8:1::/64305local expected_num_notifications=1306307# Make sure a single notification will be emitted for the deleted route,308# regardless of fib_notify_on_flag_change value.309local notify=0310route_deletion_check $ip $notify $route $expected_num_notifications311312notify=1313route_deletion_check $ip $notify $route $expected_num_notifications314315log_test "IPv6 route deletion"316}317318ipv6_route_replacement_test()319{320RET=0321322local ip="ipv6"323local route=2001:db8:1::/64324325$IP link add name dummy2 type dummy326$IP link set dev dummy2 up327328# Make sure a single notification will be emitted for the new route.329local notify=0330local expected_num_notifications=1331route_replacement_check $ip $notify $route $expected_num_notifications332333# Make sure two notifications will be emitted for the new route.334notify=1335expected_num_notifications=2336route_replacement_check $ip $notify $route $expected_num_notifications337338# notify=2 means emit notifications only for failed route installation,339# make sure a single notification will be emitted for the new route.340notify=2341expected_num_notifications=1342route_replacement_check $ip $notify $route $expected_num_notifications343344$IP link del name dummy2345346log_test "IPv6 route replacement"347}348349ipv6_route_offload_failed_test()350{351352RET=0353354local ip="ipv6"355local route=2001:db8:1::/64356local offload_failed=1357358echo "y"> $DEBUGFS_DIR/fib/fail_route_offload359check_err $? "Failed to setup route offload to fail"360361# Make sure a single notification will be emitted for the programmed362# route.363local notify=0364local expected_num_notifications=1365route_addition_check $ip $notify $route $expected_num_notifications \366$offload_failed367368# Make sure two notifications will be emitted for the new route.369notify=1370expected_num_notifications=2371route_addition_check $ip $notify $route $expected_num_notifications \372$offload_failed373374# notify=2 means emit notifications only for failed route installation,375# make sure two notifications will be emitted for the new route.376notify=2377expected_num_notifications=2378route_addition_check $ip $notify $route $expected_num_notifications \379$offload_failed380381echo "n"> $DEBUGFS_DIR/fib/fail_route_offload382check_err $? "Failed to setup route offload not to fail"383384log_test "IPv6 route offload failed"385}386387setup_prepare()388{389modprobe netdevsim &> /dev/null390echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device391while [ ! -d $SYSFS_NET_DIR ] ; do :; done392393ip netns add testns1394395if [ $? -ne 0 ]; then396echo "Failed to add netns \"testns1\""397exit 1398fi399400devlink dev reload $DEVLINK_DEV netns testns1401402if [ $? -ne 0 ]; then403echo "Failed to reload into netns \"testns1\""404exit 1405fi406407IP="ip -n testns1"408409$IP link add name dummy1 type dummy410$IP link set dev dummy1 up411}412413cleanup()414{415pre_cleanup416417$IP link del name dummy1418ip netns del testns1419echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device420modprobe -r netdevsim &> /dev/null421}422423trap cleanup EXIT424425setup_prepare426427tests_run428429exit $EXIT_STATUS430431432