Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 
0004 lib_dir=$(dirname $0)/../../../net/forwarding
0005 
0006 ALL_TESTS="
0007         ipv4_route_addition_test
0008         ipv4_route_deletion_test
0009         ipv4_route_replacement_test
0010         ipv4_route_offload_failed_test
0011         ipv6_route_addition_test
0012         ipv6_route_deletion_test
0013         ipv6_route_replacement_test
0014         ipv6_route_offload_failed_test
0015 "
0016 
0017 NETDEVSIM_PATH=/sys/bus/netdevsim/
0018 DEV_ADDR=1337
0019 DEV=netdevsim${DEV_ADDR}
0020 DEVLINK_DEV=netdevsim/${DEV}
0021 SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV/net/
0022 DEBUGFS_DIR=/sys/kernel/debug/netdevsim/$DEV/
0023 NUM_NETIFS=0
0024 source $lib_dir/lib.sh
0025 
0026 check_rt_offload_failed()
0027 {
0028         local outfile=$1; shift
0029         local line
0030 
0031         # Make sure that the first notification was emitted without
0032         # RTM_F_OFFLOAD_FAILED flag and the second with RTM_F_OFFLOAD_FAILED
0033         # flag
0034         head -n 1 $outfile | grep -q "rt_offload_failed"
0035         if [[ $? -eq 0 ]]; then
0036                 return 1
0037         fi
0038 
0039         head -n 2 $outfile | tail -n 1 | grep -q "rt_offload_failed"
0040 }
0041 
0042 check_rt_trap()
0043 {
0044         local outfile=$1; shift
0045         local line
0046 
0047         # Make sure that the first notification was emitted without RTM_F_TRAP
0048         # flag and the second with RTM_F_TRAP flag
0049         head -n 1 $outfile | grep -q "rt_trap"
0050         if [[ $? -eq 0 ]]; then
0051                 return 1
0052         fi
0053 
0054         head -n 2 $outfile | tail -n 1 | grep -q "rt_trap"
0055 }
0056 
0057 route_notify_check()
0058 {
0059         local outfile=$1; shift
0060         local expected_num_lines=$1; shift
0061         local offload_failed=${1:-0}; shift
0062 
0063         # check the monitor results
0064         lines=`wc -l $outfile | cut "-d " -f1`
0065         test $lines -eq $expected_num_lines
0066         check_err $? "$expected_num_lines notifications were expected but $lines were received"
0067 
0068         if [[ $expected_num_lines -eq 1 ]]; then
0069                 return
0070         fi
0071 
0072         if [[ $offload_failed -eq 0 ]]; then
0073                 check_rt_trap $outfile
0074                 check_err $? "Wrong RTM_F_TRAP flags in notifications"
0075         else
0076                 check_rt_offload_failed $outfile
0077                 check_err $? "Wrong RTM_F_OFFLOAD_FAILED flags in notifications"
0078         fi
0079 }
0080 
0081 route_addition_check()
0082 {
0083         local ip=$1; shift
0084         local notify=$1; shift
0085         local route=$1; shift
0086         local expected_num_notifications=$1; shift
0087         local offload_failed=${1:-0}; shift
0088 
0089         ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
0090 
0091         local outfile=$(mktemp)
0092 
0093         $IP monitor route &> $outfile &
0094         sleep 1
0095         $IP route add $route dev dummy1
0096         sleep 1
0097         kill %% && wait %% &> /dev/null
0098 
0099         route_notify_check $outfile $expected_num_notifications $offload_failed
0100         rm -f $outfile
0101 
0102         $IP route del $route dev dummy1
0103 }
0104 
0105 ipv4_route_addition_test()
0106 {
0107         RET=0
0108 
0109         local ip="ipv4"
0110         local route=192.0.2.0/24
0111 
0112         # Make sure a single notification will be emitted for the programmed
0113         # route.
0114         local notify=0
0115         local expected_num_notifications=1
0116         # route_addition_check will assign value to RET.
0117         route_addition_check $ip $notify $route $expected_num_notifications
0118 
0119         # Make sure two notifications will be emitted for the programmed route.
0120         notify=1
0121         expected_num_notifications=2
0122         route_addition_check $ip $notify $route $expected_num_notifications
0123 
0124         # notify=2 means emit notifications only for failed route installation,
0125         # make sure a single notification will be emitted for the programmed
0126         # route.
0127         notify=2
0128         expected_num_notifications=1
0129         route_addition_check $ip $notify $route $expected_num_notifications
0130 
0131         log_test "IPv4 route addition"
0132 }
0133 
0134 route_deletion_check()
0135 {
0136         local ip=$1; shift
0137         local notify=$1; shift
0138         local route=$1; shift
0139         local expected_num_notifications=$1; shift
0140 
0141         ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
0142         $IP route add $route dev dummy1
0143         sleep 1
0144 
0145         local outfile=$(mktemp)
0146 
0147         $IP monitor route &> $outfile &
0148         sleep 1
0149         $IP route del $route dev dummy1
0150         sleep 1
0151         kill %% && wait %% &> /dev/null
0152 
0153         route_notify_check $outfile $expected_num_notifications
0154         rm -f $outfile
0155 }
0156 
0157 ipv4_route_deletion_test()
0158 {
0159         RET=0
0160 
0161         local ip="ipv4"
0162         local route=192.0.2.0/24
0163         local expected_num_notifications=1
0164 
0165         # Make sure a single notification will be emitted for the deleted route,
0166         # regardless of fib_notify_on_flag_change value.
0167         local notify=0
0168         # route_deletion_check will assign value to RET.
0169         route_deletion_check $ip $notify $route $expected_num_notifications
0170 
0171         notify=1
0172         route_deletion_check $ip $notify $route $expected_num_notifications
0173 
0174         log_test "IPv4 route deletion"
0175 }
0176 
0177 route_replacement_check()
0178 {
0179         local ip=$1; shift
0180         local notify=$1; shift
0181         local route=$1; shift
0182         local expected_num_notifications=$1; shift
0183 
0184         ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
0185         $IP route add $route dev dummy1
0186         sleep 1
0187 
0188         local outfile=$(mktemp)
0189 
0190         $IP monitor route &> $outfile &
0191         sleep 1
0192         $IP route replace $route dev dummy2
0193         sleep 1
0194         kill %% && wait %% &> /dev/null
0195 
0196         route_notify_check $outfile $expected_num_notifications
0197         rm -f $outfile
0198 
0199         $IP route del $route dev dummy2
0200 }
0201 
0202 ipv4_route_replacement_test()
0203 {
0204         RET=0
0205 
0206         local ip="ipv4"
0207         local route=192.0.2.0/24
0208 
0209         $IP link add name dummy2 type dummy
0210         $IP link set dev dummy2 up
0211 
0212         # Make sure a single notification will be emitted for the new route.
0213         local notify=0
0214         local expected_num_notifications=1
0215         # route_replacement_check will assign value to RET.
0216         route_replacement_check $ip $notify $route $expected_num_notifications
0217 
0218         # Make sure two notifications will be emitted for the new route.
0219         notify=1
0220         expected_num_notifications=2
0221         route_replacement_check $ip $notify $route $expected_num_notifications
0222 
0223         # notify=2 means emit notifications only for failed route installation,
0224         # make sure a single notification will be emitted for the new route.
0225         notify=2
0226         expected_num_notifications=1
0227         route_replacement_check $ip $notify $route $expected_num_notifications
0228 
0229         $IP link del name dummy2
0230 
0231         log_test "IPv4 route replacement"
0232 }
0233 
0234 ipv4_route_offload_failed_test()
0235 {
0236 
0237         RET=0
0238 
0239         local ip="ipv4"
0240         local route=192.0.2.0/24
0241         local offload_failed=1
0242 
0243         echo "y"> $DEBUGFS_DIR/fib/fail_route_offload
0244         check_err $? "Failed to setup route offload to fail"
0245 
0246         # Make sure a single notification will be emitted for the programmed
0247         # route.
0248         local notify=0
0249         local expected_num_notifications=1
0250         route_addition_check $ip $notify $route $expected_num_notifications \
0251                 $offload_failed
0252 
0253         # Make sure two notifications will be emitted for the new route.
0254         notify=1
0255         expected_num_notifications=2
0256         route_addition_check $ip $notify $route $expected_num_notifications \
0257                 $offload_failed
0258 
0259         # notify=2 means emit notifications only for failed route installation,
0260         # make sure two notifications will be emitted for the new route.
0261         notify=2
0262         expected_num_notifications=2
0263         route_addition_check $ip $notify $route $expected_num_notifications \
0264                 $offload_failed
0265 
0266         echo "n"> $DEBUGFS_DIR/fib/fail_route_offload
0267         check_err $? "Failed to setup route offload not to fail"
0268 
0269         log_test "IPv4 route offload failed"
0270 }
0271 
0272 ipv6_route_addition_test()
0273 {
0274         RET=0
0275 
0276         local ip="ipv6"
0277         local route=2001:db8:1::/64
0278 
0279         # Make sure a single notification will be emitted for the programmed
0280         # route.
0281         local notify=0
0282         local expected_num_notifications=1
0283         route_addition_check $ip $notify $route $expected_num_notifications
0284 
0285         # Make sure two notifications will be emitted for the programmed route.
0286         notify=1
0287         expected_num_notifications=2
0288         route_addition_check $ip $notify $route $expected_num_notifications
0289 
0290         # notify=2 means emit notifications only for failed route installation,
0291         # make sure a single notification will be emitted for the programmed
0292         # route.
0293         notify=2
0294         expected_num_notifications=1
0295         route_addition_check $ip $notify $route $expected_num_notifications
0296 
0297         log_test "IPv6 route addition"
0298 }
0299 
0300 ipv6_route_deletion_test()
0301 {
0302         RET=0
0303 
0304         local ip="ipv6"
0305         local route=2001:db8:1::/64
0306         local expected_num_notifications=1
0307 
0308         # Make sure a single notification will be emitted for the deleted route,
0309         # regardless of fib_notify_on_flag_change value.
0310         local notify=0
0311         route_deletion_check $ip $notify $route $expected_num_notifications
0312 
0313         notify=1
0314         route_deletion_check $ip $notify $route $expected_num_notifications
0315 
0316         log_test "IPv6 route deletion"
0317 }
0318 
0319 ipv6_route_replacement_test()
0320 {
0321         RET=0
0322 
0323         local ip="ipv6"
0324         local route=2001:db8:1::/64
0325 
0326         $IP link add name dummy2 type dummy
0327         $IP link set dev dummy2 up
0328 
0329         # Make sure a single notification will be emitted for the new route.
0330         local notify=0
0331         local expected_num_notifications=1
0332         route_replacement_check $ip $notify $route $expected_num_notifications
0333 
0334         # Make sure two notifications will be emitted for the new route.
0335         notify=1
0336         expected_num_notifications=2
0337         route_replacement_check $ip $notify $route $expected_num_notifications
0338 
0339         # notify=2 means emit notifications only for failed route installation,
0340         # make sure a single notification will be emitted for the new route.
0341         notify=2
0342         expected_num_notifications=1
0343         route_replacement_check $ip $notify $route $expected_num_notifications
0344 
0345         $IP link del name dummy2
0346 
0347         log_test "IPv6 route replacement"
0348 }
0349 
0350 ipv6_route_offload_failed_test()
0351 {
0352 
0353         RET=0
0354 
0355         local ip="ipv6"
0356         local route=2001:db8:1::/64
0357         local offload_failed=1
0358 
0359         echo "y"> $DEBUGFS_DIR/fib/fail_route_offload
0360         check_err $? "Failed to setup route offload to fail"
0361 
0362         # Make sure a single notification will be emitted for the programmed
0363         # route.
0364         local notify=0
0365         local expected_num_notifications=1
0366         route_addition_check $ip $notify $route $expected_num_notifications \
0367                 $offload_failed
0368 
0369         # Make sure two notifications will be emitted for the new route.
0370         notify=1
0371         expected_num_notifications=2
0372         route_addition_check $ip $notify $route $expected_num_notifications \
0373                 $offload_failed
0374 
0375         # notify=2 means emit notifications only for failed route installation,
0376         # make sure two notifications will be emitted for the new route.
0377         notify=2
0378         expected_num_notifications=2
0379         route_addition_check $ip $notify $route $expected_num_notifications \
0380                 $offload_failed
0381 
0382         echo "n"> $DEBUGFS_DIR/fib/fail_route_offload
0383         check_err $? "Failed to setup route offload not to fail"
0384 
0385         log_test "IPv6 route offload failed"
0386 }
0387 
0388 setup_prepare()
0389 {
0390         modprobe netdevsim &> /dev/null
0391         echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
0392         while [ ! -d $SYSFS_NET_DIR ] ; do :; done
0393 
0394         ip netns add testns1
0395 
0396         if [ $? -ne 0 ]; then
0397                 echo "Failed to add netns \"testns1\""
0398                 exit 1
0399         fi
0400 
0401         devlink dev reload $DEVLINK_DEV netns testns1
0402 
0403         if [ $? -ne 0 ]; then
0404                 echo "Failed to reload into netns \"testns1\""
0405                 exit 1
0406         fi
0407 
0408         IP="ip -n testns1"
0409 
0410         $IP link add name dummy1 type dummy
0411         $IP link set dev dummy1 up
0412 }
0413 
0414 cleanup()
0415 {
0416         pre_cleanup
0417 
0418         $IP link del name dummy1
0419         ip netns del testns1
0420         echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
0421         modprobe -r netdevsim &> /dev/null
0422 }
0423 
0424 trap cleanup EXIT
0425 
0426 setup_prepare
0427 
0428 tests_run
0429 
0430 exit $EXIT_STATUS