2 # SPDX-License-Identifier: GPL-2.0
4 lib_dir=$(dirname $0)/../../../net/forwarding
7 ipv4_route_addition_test
8 ipv4_route_deletion_test
9 ipv4_route_replacement_test
10 ipv4_route_offload_failed_test
11 ipv6_route_addition_test
12 ipv6_route_deletion_test
13 ipv6_route_replacement_test
14 ipv6_route_offload_failed_test
17 NETDEVSIM_PATH=/sys/bus/netdevsim/
19 DEV=netdevsim${DEV_ADDR}
20 DEVLINK_DEV=netdevsim/${DEV}
21 SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV/net/
22 DEBUGFS_DIR=/sys/kernel/debug/netdevsim/$DEV/
24 source $lib_dir/lib.sh
26 check_rt_offload_failed()
28 local outfile=$1; shift
31 # Make sure that the first notification was emitted without
32 # RTM_F_OFFLOAD_FAILED flag and the second with RTM_F_OFFLOAD_FAILED
34 head -n 1 $outfile | grep -q "rt_offload_failed"
35 if [[ $? -eq 0 ]]; then
39 head -n 2 $outfile | tail -n 1 | grep -q "rt_offload_failed"
44 local outfile=$1; shift
47 # Make sure that the first notification was emitted without RTM_F_TRAP
48 # flag and the second with RTM_F_TRAP flag
49 head -n 1 $outfile | grep -q "rt_trap"
50 if [[ $? -eq 0 ]]; then
54 head -n 2 $outfile | tail -n 1 | grep -q "rt_trap"
59 local outfile=$1; shift
60 local expected_num_lines=$1; shift
61 local offload_failed=${1:-0}; shift
63 # check the monitor results
64 lines=`wc -l $outfile | cut "-d " -f1`
65 test $lines -eq $expected_num_lines
66 check_err $? "$expected_num_lines notifications were expected but $lines were received"
68 if [[ $expected_num_lines -eq 1 ]]; then
72 if [[ $offload_failed -eq 0 ]]; then
73 check_rt_trap $outfile
74 check_err $? "Wrong RTM_F_TRAP flags in notifications"
76 check_rt_offload_failed $outfile
77 check_err $? "Wrong RTM_F_OFFLOAD_FAILED flags in notifications"
81 route_addition_check()
84 local notify=$1; shift
86 local expected_num_notifications=$1; shift
87 local offload_failed=${1:-0}; shift
89 ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
91 local outfile=$(mktemp)
93 $IP monitor route &> $outfile &
95 $IP route add $route dev dummy1
97 kill %% && wait %% &> /dev/null
99 route_notify_check $outfile $expected_num_notifications $offload_failed
102 $IP route del $route dev dummy1
105 ipv4_route_addition_test()
110 local route=192.0.2.0/24
112 # Make sure a single notification will be emitted for the programmed
115 local expected_num_notifications=1
116 # route_addition_check will assign value to RET.
117 route_addition_check $ip $notify $route $expected_num_notifications
119 # Make sure two notifications will be emitted for the programmed route.
121 expected_num_notifications=2
122 route_addition_check $ip $notify $route $expected_num_notifications
124 # notify=2 means emit notifications only for failed route installation,
125 # make sure a single notification will be emitted for the programmed
128 expected_num_notifications=1
129 route_addition_check $ip $notify $route $expected_num_notifications
131 log_test "IPv4 route addition"
134 route_deletion_check()
137 local notify=$1; shift
138 local route=$1; shift
139 local expected_num_notifications=$1; shift
141 ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
142 $IP route add $route dev dummy1
145 local outfile=$(mktemp)
147 $IP monitor route &> $outfile &
149 $IP route del $route dev dummy1
151 kill %% && wait %% &> /dev/null
153 route_notify_check $outfile $expected_num_notifications
157 ipv4_route_deletion_test()
162 local route=192.0.2.0/24
163 local expected_num_notifications=1
165 # Make sure a single notification will be emitted for the deleted route,
166 # regardless of fib_notify_on_flag_change value.
168 # route_deletion_check will assign value to RET.
169 route_deletion_check $ip $notify $route $expected_num_notifications
172 route_deletion_check $ip $notify $route $expected_num_notifications
174 log_test "IPv4 route deletion"
177 route_replacement_check()
180 local notify=$1; shift
181 local route=$1; shift
182 local expected_num_notifications=$1; shift
184 ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
185 $IP route add $route dev dummy1
188 local outfile=$(mktemp)
190 $IP monitor route &> $outfile &
192 $IP route replace $route dev dummy2
194 kill %% && wait %% &> /dev/null
196 route_notify_check $outfile $expected_num_notifications
199 $IP route del $route dev dummy2
202 ipv4_route_replacement_test()
207 local route=192.0.2.0/24
209 $IP link add name dummy2 type dummy
210 $IP link set dev dummy2 up
212 # Make sure a single notification will be emitted for the new route.
214 local expected_num_notifications=1
215 # route_replacement_check will assign value to RET.
216 route_replacement_check $ip $notify $route $expected_num_notifications
218 # Make sure two notifications will be emitted for the new route.
220 expected_num_notifications=2
221 route_replacement_check $ip $notify $route $expected_num_notifications
223 # notify=2 means emit notifications only for failed route installation,
224 # make sure a single notification will be emitted for the new route.
226 expected_num_notifications=1
227 route_replacement_check $ip $notify $route $expected_num_notifications
229 $IP link del name dummy2
231 log_test "IPv4 route replacement"
234 ipv4_route_offload_failed_test()
240 local route=192.0.2.0/24
241 local offload_failed=1
243 echo "y"> $DEBUGFS_DIR/fib/fail_route_offload
244 check_err $? "Failed to setup route offload to fail"
246 # Make sure a single notification will be emitted for the programmed
249 local expected_num_notifications=1
250 route_addition_check $ip $notify $route $expected_num_notifications \
253 # Make sure two notifications will be emitted for the new route.
255 expected_num_notifications=2
256 route_addition_check $ip $notify $route $expected_num_notifications \
259 # notify=2 means emit notifications only for failed route installation,
260 # make sure two notifications will be emitted for the new route.
262 expected_num_notifications=2
263 route_addition_check $ip $notify $route $expected_num_notifications \
266 echo "n"> $DEBUGFS_DIR/fib/fail_route_offload
267 check_err $? "Failed to setup route offload not to fail"
269 log_test "IPv4 route offload failed"
272 ipv6_route_addition_test()
277 local route=2001:db8:1::/64
279 # Make sure a single notification will be emitted for the programmed
282 local expected_num_notifications=1
283 route_addition_check $ip $notify $route $expected_num_notifications
285 # Make sure two notifications will be emitted for the programmed route.
287 expected_num_notifications=2
288 route_addition_check $ip $notify $route $expected_num_notifications
290 # notify=2 means emit notifications only for failed route installation,
291 # make sure a single notification will be emitted for the programmed
294 expected_num_notifications=1
295 route_addition_check $ip $notify $route $expected_num_notifications
297 log_test "IPv6 route addition"
300 ipv6_route_deletion_test()
305 local route=2001:db8:1::/64
306 local expected_num_notifications=1
308 # Make sure a single notification will be emitted for the deleted route,
309 # regardless of fib_notify_on_flag_change value.
311 route_deletion_check $ip $notify $route $expected_num_notifications
314 route_deletion_check $ip $notify $route $expected_num_notifications
316 log_test "IPv6 route deletion"
319 ipv6_route_replacement_test()
324 local route=2001:db8:1::/64
326 $IP link add name dummy2 type dummy
327 $IP link set dev dummy2 up
329 # Make sure a single notification will be emitted for the new route.
331 local expected_num_notifications=1
332 route_replacement_check $ip $notify $route $expected_num_notifications
334 # Make sure two notifications will be emitted for the new route.
336 expected_num_notifications=2
337 route_replacement_check $ip $notify $route $expected_num_notifications
339 # notify=2 means emit notifications only for failed route installation,
340 # make sure a single notification will be emitted for the new route.
342 expected_num_notifications=1
343 route_replacement_check $ip $notify $route $expected_num_notifications
345 $IP link del name dummy2
347 log_test "IPv6 route replacement"
350 ipv6_route_offload_failed_test()
356 local route=2001:db8:1::/64
357 local offload_failed=1
359 echo "y"> $DEBUGFS_DIR/fib/fail_route_offload
360 check_err $? "Failed to setup route offload to fail"
362 # Make sure a single notification will be emitted for the programmed
365 local expected_num_notifications=1
366 route_addition_check $ip $notify $route $expected_num_notifications \
369 # Make sure two notifications will be emitted for the new route.
371 expected_num_notifications=2
372 route_addition_check $ip $notify $route $expected_num_notifications \
375 # notify=2 means emit notifications only for failed route installation,
376 # make sure two notifications will be emitted for the new route.
378 expected_num_notifications=2
379 route_addition_check $ip $notify $route $expected_num_notifications \
382 echo "n"> $DEBUGFS_DIR/fib/fail_route_offload
383 check_err $? "Failed to setup route offload not to fail"
385 log_test "IPv6 route offload failed"
390 modprobe netdevsim &> /dev/null
391 echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
392 while [ ! -d $SYSFS_NET_DIR ] ; do :; done
396 if [ $? -ne 0 ]; then
397 echo "Failed to add netns \"testns1\""
401 devlink dev reload $DEVLINK_DEV netns testns1
403 if [ $? -ne 0 ]; then
404 echo "Failed to reload into netns \"testns1\""
410 $IP link add name dummy1 type dummy
411 $IP link set dev dummy1 up
418 $IP link del name dummy1
420 echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
421 modprobe -r netdevsim &> /dev/null