| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- #!/bin/bash
- # SPDX-License-Identifier: GPL-2.0
- lib_dir=$(dirname $0)/../../../net/forwarding
- ALL_TESTS="
- ipv4_route_addition_test
- ipv4_route_deletion_test
- ipv4_route_replacement_test
- ipv4_route_offload_failed_test
- ipv6_route_addition_test
- ipv6_route_deletion_test
- ipv6_route_replacement_test
- ipv6_route_offload_failed_test
- "
- NETDEVSIM_PATH=/sys/bus/netdevsim/
- DEV_ADDR=1337
- DEV=netdevsim${DEV_ADDR}
- DEVLINK_DEV=netdevsim/${DEV}
- SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV/net/
- DEBUGFS_DIR=/sys/kernel/debug/netdevsim/$DEV/
- NUM_NETIFS=0
- source $lib_dir/lib.sh
- check_rt_offload_failed()
- {
- local outfile=$1; shift
- local line
- # Make sure that the first notification was emitted without
- # RTM_F_OFFLOAD_FAILED flag and the second with RTM_F_OFFLOAD_FAILED
- # flag
- head -n 1 $outfile | grep -q "rt_offload_failed"
- if [[ $? -eq 0 ]]; then
- return 1
- fi
- head -n 2 $outfile | tail -n 1 | grep -q "rt_offload_failed"
- }
- check_rt_trap()
- {
- local outfile=$1; shift
- local line
- # Make sure that the first notification was emitted without RTM_F_TRAP
- # flag and the second with RTM_F_TRAP flag
- head -n 1 $outfile | grep -q "rt_trap"
- if [[ $? -eq 0 ]]; then
- return 1
- fi
- head -n 2 $outfile | tail -n 1 | grep -q "rt_trap"
- }
- route_notify_check()
- {
- local outfile=$1; shift
- local expected_num_lines=$1; shift
- local offload_failed=${1:-0}; shift
- # check the monitor results
- lines=`wc -l $outfile | cut "-d " -f1`
- test $lines -eq $expected_num_lines
- check_err $? "$expected_num_lines notifications were expected but $lines were received"
- if [[ $expected_num_lines -eq 1 ]]; then
- return
- fi
- if [[ $offload_failed -eq 0 ]]; then
- check_rt_trap $outfile
- check_err $? "Wrong RTM_F_TRAP flags in notifications"
- else
- check_rt_offload_failed $outfile
- check_err $? "Wrong RTM_F_OFFLOAD_FAILED flags in notifications"
- fi
- }
- route_addition_check()
- {
- local ip=$1; shift
- local notify=$1; shift
- local route=$1; shift
- local expected_num_notifications=$1; shift
- local offload_failed=${1:-0}; shift
- ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
- local outfile=$(mktemp)
- $IP monitor route &> $outfile &
- sleep 1
- $IP route add $route dev dummy1
- sleep 1
- kill_process %%
- route_notify_check $outfile $expected_num_notifications $offload_failed
- rm -f $outfile
- $IP route del $route dev dummy1
- }
- ipv4_route_addition_test()
- {
- RET=0
- local ip="ipv4"
- local route=192.0.2.0/24
- # Make sure a single notification will be emitted for the programmed
- # route.
- local notify=0
- local expected_num_notifications=1
- # route_addition_check will assign value to RET.
- route_addition_check $ip $notify $route $expected_num_notifications
- # Make sure two notifications will be emitted for the programmed route.
- notify=1
- expected_num_notifications=2
- route_addition_check $ip $notify $route $expected_num_notifications
- # notify=2 means emit notifications only for failed route installation,
- # make sure a single notification will be emitted for the programmed
- # route.
- notify=2
- expected_num_notifications=1
- route_addition_check $ip $notify $route $expected_num_notifications
- log_test "IPv4 route addition"
- }
- route_deletion_check()
- {
- local ip=$1; shift
- local notify=$1; shift
- local route=$1; shift
- local expected_num_notifications=$1; shift
- ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
- $IP route add $route dev dummy1
- sleep 1
- local outfile=$(mktemp)
- $IP monitor route &> $outfile &
- sleep 1
- $IP route del $route dev dummy1
- sleep 1
- kill_process %%
- route_notify_check $outfile $expected_num_notifications
- rm -f $outfile
- }
- ipv4_route_deletion_test()
- {
- RET=0
- local ip="ipv4"
- local route=192.0.2.0/24
- local expected_num_notifications=1
- # Make sure a single notification will be emitted for the deleted route,
- # regardless of fib_notify_on_flag_change value.
- local notify=0
- # route_deletion_check will assign value to RET.
- route_deletion_check $ip $notify $route $expected_num_notifications
- notify=1
- route_deletion_check $ip $notify $route $expected_num_notifications
- log_test "IPv4 route deletion"
- }
- route_replacement_check()
- {
- local ip=$1; shift
- local notify=$1; shift
- local route=$1; shift
- local expected_num_notifications=$1; shift
- ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
- $IP route add $route dev dummy1
- sleep 1
- local outfile=$(mktemp)
- $IP monitor route &> $outfile &
- sleep 1
- $IP route replace $route dev dummy2
- sleep 1
- kill_process %%
- route_notify_check $outfile $expected_num_notifications
- rm -f $outfile
- $IP route del $route dev dummy2
- }
- ipv4_route_replacement_test()
- {
- RET=0
- local ip="ipv4"
- local route=192.0.2.0/24
- $IP link add name dummy2 type dummy
- $IP link set dev dummy2 up
- # Make sure a single notification will be emitted for the new route.
- local notify=0
- local expected_num_notifications=1
- # route_replacement_check will assign value to RET.
- route_replacement_check $ip $notify $route $expected_num_notifications
- # Make sure two notifications will be emitted for the new route.
- notify=1
- expected_num_notifications=2
- route_replacement_check $ip $notify $route $expected_num_notifications
- # notify=2 means emit notifications only for failed route installation,
- # make sure a single notification will be emitted for the new route.
- notify=2
- expected_num_notifications=1
- route_replacement_check $ip $notify $route $expected_num_notifications
- $IP link del name dummy2
- log_test "IPv4 route replacement"
- }
- ipv4_route_offload_failed_test()
- {
- RET=0
- local ip="ipv4"
- local route=192.0.2.0/24
- local offload_failed=1
- echo "y"> $DEBUGFS_DIR/fib/fail_route_offload
- check_err $? "Failed to setup route offload to fail"
- # Make sure a single notification will be emitted for the programmed
- # route.
- local notify=0
- local expected_num_notifications=1
- route_addition_check $ip $notify $route $expected_num_notifications \
- $offload_failed
- # Make sure two notifications will be emitted for the new route.
- notify=1
- expected_num_notifications=2
- route_addition_check $ip $notify $route $expected_num_notifications \
- $offload_failed
- # notify=2 means emit notifications only for failed route installation,
- # make sure two notifications will be emitted for the new route.
- notify=2
- expected_num_notifications=2
- route_addition_check $ip $notify $route $expected_num_notifications \
- $offload_failed
- echo "n"> $DEBUGFS_DIR/fib/fail_route_offload
- check_err $? "Failed to setup route offload not to fail"
- log_test "IPv4 route offload failed"
- }
- ipv6_route_addition_test()
- {
- RET=0
- local ip="ipv6"
- local route=2001:db8:1::/64
- # Make sure a single notification will be emitted for the programmed
- # route.
- local notify=0
- local expected_num_notifications=1
- route_addition_check $ip $notify $route $expected_num_notifications
- # Make sure two notifications will be emitted for the programmed route.
- notify=1
- expected_num_notifications=2
- route_addition_check $ip $notify $route $expected_num_notifications
- # notify=2 means emit notifications only for failed route installation,
- # make sure a single notification will be emitted for the programmed
- # route.
- notify=2
- expected_num_notifications=1
- route_addition_check $ip $notify $route $expected_num_notifications
- log_test "IPv6 route addition"
- }
- ipv6_route_deletion_test()
- {
- RET=0
- local ip="ipv6"
- local route=2001:db8:1::/64
- local expected_num_notifications=1
- # Make sure a single notification will be emitted for the deleted route,
- # regardless of fib_notify_on_flag_change value.
- local notify=0
- route_deletion_check $ip $notify $route $expected_num_notifications
- notify=1
- route_deletion_check $ip $notify $route $expected_num_notifications
- log_test "IPv6 route deletion"
- }
- ipv6_route_replacement_test()
- {
- RET=0
- local ip="ipv6"
- local route=2001:db8:1::/64
- $IP link add name dummy2 type dummy
- $IP link set dev dummy2 up
- # Make sure a single notification will be emitted for the new route.
- local notify=0
- local expected_num_notifications=1
- route_replacement_check $ip $notify $route $expected_num_notifications
- # Make sure two notifications will be emitted for the new route.
- notify=1
- expected_num_notifications=2
- route_replacement_check $ip $notify $route $expected_num_notifications
- # notify=2 means emit notifications only for failed route installation,
- # make sure a single notification will be emitted for the new route.
- notify=2
- expected_num_notifications=1
- route_replacement_check $ip $notify $route $expected_num_notifications
- $IP link del name dummy2
- log_test "IPv6 route replacement"
- }
- ipv6_route_offload_failed_test()
- {
- RET=0
- local ip="ipv6"
- local route=2001:db8:1::/64
- local offload_failed=1
- echo "y"> $DEBUGFS_DIR/fib/fail_route_offload
- check_err $? "Failed to setup route offload to fail"
- # Make sure a single notification will be emitted for the programmed
- # route.
- local notify=0
- local expected_num_notifications=1
- route_addition_check $ip $notify $route $expected_num_notifications \
- $offload_failed
- # Make sure two notifications will be emitted for the new route.
- notify=1
- expected_num_notifications=2
- route_addition_check $ip $notify $route $expected_num_notifications \
- $offload_failed
- # notify=2 means emit notifications only for failed route installation,
- # make sure two notifications will be emitted for the new route.
- notify=2
- expected_num_notifications=2
- route_addition_check $ip $notify $route $expected_num_notifications \
- $offload_failed
- echo "n"> $DEBUGFS_DIR/fib/fail_route_offload
- check_err $? "Failed to setup route offload not to fail"
- log_test "IPv6 route offload failed"
- }
- setup_prepare()
- {
- modprobe netdevsim &> /dev/null
- echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
- while [ ! -d $SYSFS_NET_DIR ] ; do :; done
- ip netns add testns1
- if [ $? -ne 0 ]; then
- echo "Failed to add netns \"testns1\""
- exit 1
- fi
- devlink dev reload $DEVLINK_DEV netns testns1
- if [ $? -ne 0 ]; then
- echo "Failed to reload into netns \"testns1\""
- exit 1
- fi
- IP="ip -n testns1"
- $IP link add name dummy1 type dummy
- $IP link set dev dummy1 up
- }
- cleanup()
- {
- pre_cleanup
- $IP link del name dummy1
- ip netns del testns1
- echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
- modprobe -r netdevsim &> /dev/null
- }
- trap cleanup EXIT
- setup_prepare
- tests_run
- exit $EXIT_STATUS
|