WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / drivers / net / mlxsw / devlink_trap_l3_exceptions.sh
blob1fedfc9da434fc0052f4beb69412a903d98bf898
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 # Test devlink-trap L3 exceptions functionality over mlxsw.
5 # Check all exception traps to make sure they are triggered under the right
6 # conditions.
8 # +---------------------------------+
9 # | H1 (vrf) |
10 # | + $h1 |
11 # | | 192.0.2.1/24 |
12 # | | 2001:db8:1::1/64 |
13 # | | |
14 # | | default via 192.0.2.2 |
15 # | | default via 2001:db8:1::2 |
16 # +----|----------------------------+
17 # |
18 # +----|----------------------------------------------------------------------+
19 # | SW | |
20 # | + $rp1 |
21 # | 192.0.2.2/24 |
22 # | 2001:db8:1::2/64 |
23 # | |
24 # | 2001:db8:2::2/64 |
25 # | 198.51.100.2/24 |
26 # | + $rp2 |
27 # | | |
28 # +----|----------------------------------------------------------------------+
29 # |
30 # +----|----------------------------+
31 # | | default via 198.51.100.2 |
32 # | | default via 2001:db8:2::2 |
33 # | | |
34 # | | 2001:db8:2::1/64 |
35 # | | 198.51.100.1/24 |
36 # | + $h2 |
37 # | H2 (vrf) |
38 # +---------------------------------+
40 lib_dir=$(dirname $0)/../../../net/forwarding
42 ALL_TESTS="
43 mtu_value_is_too_small_test
44 ttl_value_is_too_small_test
45 mc_reverse_path_forwarding_test
46 reject_route_test
47 unresolved_neigh_test
48 ipv4_lpm_miss_test
49 ipv6_lpm_miss_test
52 NUM_NETIFS=4
53 source $lib_dir/lib.sh
54 source $lib_dir/tc_common.sh
55 source $lib_dir/devlink_lib.sh
57 require_command $MCD
58 require_command $MC_CLI
59 table_name=selftests
61 h1_create()
63 simple_if_init $h1 192.0.2.1/24 2001:db8:1::1/64
65 ip -4 route add default vrf v$h1 nexthop via 192.0.2.2
66 ip -6 route add default vrf v$h1 nexthop via 2001:db8:1::2
68 tc qdisc add dev $h1 clsact
71 h1_destroy()
73 tc qdisc del dev $h1 clsact
75 ip -6 route del default vrf v$h1 nexthop via 2001:db8:1::2
76 ip -4 route del default vrf v$h1 nexthop via 192.0.2.2
78 simple_if_fini $h1 192.0.2.1/24 2001:db8:1::1/64
81 h2_create()
83 simple_if_init $h2 198.51.100.1/24 2001:db8:2::1/64
85 ip -4 route add default vrf v$h2 nexthop via 198.51.100.2
86 ip -6 route add default vrf v$h2 nexthop via 2001:db8:2::2
89 h2_destroy()
91 ip -6 route del default vrf v$h2 nexthop via 2001:db8:2::2
92 ip -4 route del default vrf v$h2 nexthop via 198.51.100.2
94 simple_if_fini $h2 198.51.100.1/24 2001:db8:2::1/64
97 router_create()
99 ip link set dev $rp1 up
100 ip link set dev $rp2 up
102 tc qdisc add dev $rp2 clsact
104 __addr_add_del $rp1 add 192.0.2.2/24 2001:db8:1::2/64
105 __addr_add_del $rp2 add 198.51.100.2/24 2001:db8:2::2/64
108 router_destroy()
110 __addr_add_del $rp2 del 198.51.100.2/24 2001:db8:2::2/64
111 __addr_add_del $rp1 del 192.0.2.2/24 2001:db8:1::2/64
113 tc qdisc del dev $rp2 clsact
116 setup_prepare()
118 h1=${NETIFS[p1]}
119 rp1=${NETIFS[p2]}
121 rp2=${NETIFS[p3]}
122 h2=${NETIFS[p4]}
124 rp1mac=$(mac_get $rp1)
126 start_mcd
128 vrf_prepare
129 forwarding_enable
131 h1_create
132 h2_create
134 router_create
137 cleanup()
139 pre_cleanup
141 router_destroy
143 h2_destroy
144 h1_destroy
146 forwarding_restore
147 vrf_cleanup
149 kill_mcd
152 ping_check()
154 ping_do $h1 198.51.100.1
155 check_err $? "Packets that should not be trapped were trapped"
158 trap_action_check()
160 local trap_name=$1; shift
161 local expected_action=$1; shift
163 action=$(devlink_trap_action_get $trap_name)
164 if [ "$action" != $expected_action ]; then
165 check_err 1 "Trap $trap_name has wrong action: $action"
169 mtu_value_is_too_small_test()
171 local trap_name="mtu_value_is_too_small"
172 local expected_action="trap"
173 local mz_pid
175 RET=0
177 ping_check $trap_name
178 trap_action_check $trap_name $expected_action
180 # type - Destination Unreachable
181 # code - Fragmentation Needed and Don't Fragment was Set
182 tc filter add dev $h1 ingress protocol ip pref 1 handle 101 \
183 flower skip_hw ip_proto icmp type 3 code 4 action pass
185 mtu_set $rp2 1300
187 # Generate IP packets bigger than router's MTU with don't fragment
188 # flag on.
189 $MZ $h1 -t udp "sp=54321,dp=12345,df" -p 1400 -c 0 -d 1msec -b $rp1mac \
190 -B 198.51.100.1 -q &
191 mz_pid=$!
193 devlink_trap_exception_test $trap_name
195 tc_check_packets_hitting "dev $h1 ingress" 101
196 check_err $? "Packets were not received to h1"
198 log_test "MTU value is too small"
200 mtu_restore $rp2
202 kill $mz_pid && wait $mz_pid &> /dev/null
203 tc filter del dev $h1 ingress protocol ip pref 1 handle 101 flower
206 __ttl_value_is_too_small_test()
208 local ttl_val=$1; shift
209 local trap_name="ttl_value_is_too_small"
210 local expected_action="trap"
211 local mz_pid
213 RET=0
215 ping_check $trap_name
216 trap_action_check $trap_name $expected_action
218 # type - Time Exceeded
219 # code - Time to Live exceeded in Transit
220 tc filter add dev $h1 ingress protocol ip pref 1 handle 101 \
221 flower skip_hw ip_proto icmp type 11 code 0 action pass
223 # Generate IP packets with small TTL
224 $MZ $h1 -t udp "ttl=$ttl_val,sp=54321,dp=12345" -c 0 -d 1msec \
225 -b $rp1mac -B 198.51.100.1 -q &
226 mz_pid=$!
228 devlink_trap_exception_test $trap_name
230 tc_check_packets_hitting "dev $h1 ingress" 101
231 check_err $? "Packets were not received to h1"
233 log_test "TTL value is too small: TTL=$ttl_val"
235 kill $mz_pid && wait $mz_pid &> /dev/null
236 tc filter del dev $h1 ingress protocol ip pref 1 handle 101 flower
239 ttl_value_is_too_small_test()
241 __ttl_value_is_too_small_test 0
242 __ttl_value_is_too_small_test 1
245 start_mcd()
247 SMCROUTEDIR="$(mktemp -d)"
248 for ((i = 1; i <= $NUM_NETIFS; ++i)); do
249 echo "phyint ${NETIFS[p$i]} enable" >> \
250 $SMCROUTEDIR/$table_name.conf
251 done
253 $MCD -N -I $table_name -f $SMCROUTEDIR/$table_name.conf \
254 -P $SMCROUTEDIR/$table_name.pid
257 kill_mcd()
259 pkill $MCD
260 rm -rf $SMCROUTEDIR
263 __mc_reverse_path_forwarding_test()
265 local desc=$1; shift
266 local src_ip=$1; shift
267 local dst_ip=$1; shift
268 local dst_mac=$1; shift
269 local proto=$1; shift
270 local flags=${1:-""}; shift
271 local trap_name="mc_reverse_path_forwarding"
272 local expected_action="trap"
273 local mz_pid
275 RET=0
277 ping_check $trap_name
278 trap_action_check $trap_name $expected_action
280 tc filter add dev $rp2 egress protocol $proto pref 1 handle 101 \
281 flower dst_ip $dst_ip ip_proto udp action drop
283 $MC_CLI -I $table_name add $rp1 $src_ip $dst_ip $rp2
285 # Generate packets to multicast address.
286 $MZ $h2 $flags -t udp "sp=54321,dp=12345" -c 0 -p 128 \
287 -a 00:11:22:33:44:55 -b $dst_mac \
288 -A $src_ip -B $dst_ip -q &
290 mz_pid=$!
292 devlink_trap_exception_test $trap_name
294 tc_check_packets "dev $rp2 egress" 101 0
295 check_err $? "Packets were not dropped"
297 log_test "Multicast reverse path forwarding: $desc"
299 kill $mz_pid && wait $mz_pid &> /dev/null
300 tc filter del dev $rp2 egress protocol $proto pref 1 handle 101 flower
303 mc_reverse_path_forwarding_test()
305 __mc_reverse_path_forwarding_test "IPv4" "192.0.2.1" "225.1.2.3" \
306 "01:00:5e:01:02:03" "ip"
307 __mc_reverse_path_forwarding_test "IPv6" "2001:db8:1::1" "ff0e::3" \
308 "33:33:00:00:00:03" "ipv6" "-6"
311 __reject_route_test()
313 local desc=$1; shift
314 local dst_ip=$1; shift
315 local proto=$1; shift
316 local ip_proto=$1; shift
317 local type=$1; shift
318 local code=$1; shift
319 local unreachable=$1; shift
320 local flags=${1:-""}; shift
321 local trap_name="reject_route"
322 local expected_action="trap"
323 local mz_pid
325 RET=0
327 ping_check $trap_name
328 trap_action_check $trap_name $expected_action
330 tc filter add dev $h1 ingress protocol $proto pref 1 handle 101 flower \
331 skip_hw ip_proto $ip_proto type $type code $code action pass
333 ip route add unreachable $unreachable
335 # Generate pacekts to h2. The destination IP is unreachable.
336 $MZ $flags $h1 -t udp "sp=54321,dp=12345" -c 0 -d 1msec -b $rp1mac \
337 -B $dst_ip -q &
338 mz_pid=$!
340 devlink_trap_exception_test $trap_name
342 tc_check_packets_hitting "dev $h1 ingress" 101
343 check_err $? "ICMP packet was not received to h1"
345 log_test "Reject route: $desc"
347 kill $mz_pid && wait $mz_pid &> /dev/null
348 ip route del unreachable $unreachable
349 tc filter del dev $h1 ingress protocol $proto pref 1 handle 101 flower
352 reject_route_test()
354 # type - Destination Unreachable
355 # code - Host Unreachable
356 __reject_route_test "IPv4" 198.51.100.1 "ip" "icmp" 3 1 \
357 "198.51.100.0/26"
358 # type - Destination Unreachable
359 # code - No Route
360 __reject_route_test "IPv6" 2001:db8:2::1 "ipv6" "icmpv6" 1 0 \
361 "2001:db8:2::0/66" "-6"
364 __host_miss_test()
366 local desc=$1; shift
367 local dip=$1; shift
368 local trap_name="unresolved_neigh"
369 local expected_action="trap"
370 local mz_pid
372 RET=0
374 ping_check $trap_name
375 trap_action_check $trap_name $expected_action
377 ip neigh flush dev $rp2
379 t0_packets=$(devlink_trap_rx_packets_get $trap_name)
381 # Generate packets to h2 (will incur a unresolved neighbor).
382 # The ping should pass and devlink counters should be increased.
383 ping_do $h1 $dip
384 check_err $? "ping failed: $desc"
386 t1_packets=$(devlink_trap_rx_packets_get $trap_name)
388 if [[ $t0_packets -eq $t1_packets ]]; then
389 check_err 1 "Trap counter did not increase"
392 log_test "Unresolved neigh: host miss: $desc"
395 __invalid_nexthop_test()
397 local desc=$1; shift
398 local dip=$1; shift
399 local extra_add=$1; shift
400 local subnet=$1; shift
401 local via_add=$1; shift
402 local trap_name="unresolved_neigh"
403 local expected_action="trap"
404 local mz_pid
406 RET=0
408 ping_check $trap_name
409 trap_action_check $trap_name $expected_action
411 ip address add $extra_add/$subnet dev $h2
413 # Check that correct route does not trigger unresolved_neigh
414 ip $flags route add $dip via $extra_add dev $rp2
416 # Generate packets in order to discover all neighbours.
417 # Without it, counters of unresolved_neigh will be increased
418 # during neighbours discovery and the check below will fail
419 # for a wrong reason
420 ping_do $h1 $dip
422 t0_packets=$(devlink_trap_rx_packets_get $trap_name)
423 ping_do $h1 $dip
424 t1_packets=$(devlink_trap_rx_packets_get $trap_name)
426 if [[ $t0_packets -ne $t1_packets ]]; then
427 check_err 1 "Trap counter increased when it should not"
430 ip $flags route del $dip via $extra_add dev $rp2
432 # Check that route to nexthop that does not exist trigger
433 # unresolved_neigh
434 ip $flags route add $dip via $via_add dev $h2
436 t0_packets=$(devlink_trap_rx_packets_get $trap_name)
437 ping_do $h1 $dip
438 t1_packets=$(devlink_trap_rx_packets_get $trap_name)
440 if [[ $t0_packets -eq $t1_packets ]]; then
441 check_err 1 "Trap counter did not increase"
444 ip $flags route del $dip via $via_add dev $h2
445 ip address del $extra_add/$subnet dev $h2
446 log_test "Unresolved neigh: nexthop does not exist: $desc"
449 unresolved_neigh_test()
451 __host_miss_test "IPv4" 198.51.100.1
452 __host_miss_test "IPv6" 2001:db8:2::1
453 __invalid_nexthop_test "IPv4" 198.51.100.1 198.51.100.3 24 198.51.100.4
454 __invalid_nexthop_test "IPv6" 2001:db8:2::1 2001:db8:2::3 64 \
455 2001:db8:2::4
458 vrf_without_routes_create()
460 # VRF creating makes the links to be down and then up again.
461 # By default, IPv6 address is not saved after link becomes down.
462 # Save IPv6 address using sysctl configuration.
463 sysctl_set net.ipv6.conf.$rp1.keep_addr_on_down 1
464 sysctl_set net.ipv6.conf.$rp2.keep_addr_on_down 1
466 ip link add dev vrf1 type vrf table 101
467 ip link set dev $rp1 master vrf1
468 ip link set dev $rp2 master vrf1
469 ip link set dev vrf1 up
471 # Wait for rp1 and rp2 to be up
472 setup_wait
475 vrf_without_routes_destroy()
477 ip link set dev $rp1 nomaster
478 ip link set dev $rp2 nomaster
479 ip link del dev vrf1
481 sysctl_restore net.ipv6.conf.$rp2.keep_addr_on_down
482 sysctl_restore net.ipv6.conf.$rp1.keep_addr_on_down
484 # Wait for interfaces to be up
485 setup_wait
488 ipv4_lpm_miss_test()
490 local trap_name="ipv4_lpm_miss"
491 local expected_action="trap"
492 local mz_pid
494 RET=0
496 ping_check $trap_name
497 trap_action_check $trap_name $expected_action
499 # Create a VRF without a default route
500 vrf_without_routes_create
502 # Generate packets through a VRF without a matching route.
503 $MZ $h1 -t udp "sp=54321,dp=12345" -c 0 -d 1msec -b $rp1mac \
504 -B 203.0.113.1 -q &
505 mz_pid=$!
507 devlink_trap_exception_test $trap_name
509 log_test "LPM miss: IPv4"
511 kill $mz_pid && wait $mz_pid &> /dev/null
512 vrf_without_routes_destroy
515 ipv6_lpm_miss_test()
517 local trap_name="ipv6_lpm_miss"
518 local expected_action="trap"
519 local mz_pid
521 RET=0
523 ping_check $trap_name
524 trap_action_check $trap_name $expected_action
526 # Create a VRF without a default route
527 vrf_without_routes_create
529 # Generate packets through a VRF without a matching route.
530 $MZ -6 $h1 -t udp "sp=54321,dp=12345" -c 0 -d 1msec -b $rp1mac \
531 -B 2001:db8::1 -q &
532 mz_pid=$!
534 devlink_trap_exception_test $trap_name
536 log_test "LPM miss: IPv6"
538 kill $mz_pid && wait $mz_pid &> /dev/null
539 vrf_without_routes_destroy
542 trap cleanup EXIT
544 setup_prepare
545 setup_wait
547 tests_run
549 exit $EXIT_STATUS