forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/tools/testing/selftests/net/forwarding/lib.sh
....@@ -15,6 +15,11 @@
1515 PAUSE_ON_CLEANUP=${PAUSE_ON_CLEANUP:=no}
1616 NETIF_TYPE=${NETIF_TYPE:=veth}
1717 NETIF_CREATE=${NETIF_CREATE:=yes}
18
+MCD=${MCD:=smcrouted}
19
+MC_CLI=${MC_CLI:=smcroutectl}
20
+PING_TIMEOUT=${PING_TIMEOUT:=5}
21
+WAIT_TIMEOUT=${WAIT_TIMEOUT:=20}
22
+INTERFACE_TIMEOUT=${INTERFACE_TIMEOUT:=600}
1823
1924 relative_path="${BASH_SOURCE%/*}"
2025 if [[ "$relative_path" == "${BASH_SOURCE}" ]]; then
....@@ -52,6 +57,26 @@
5257 if [[ $? -ne 0 ]]; then
5358 echo "SKIP: iproute2 too old; tc is missing chain support"
5459 exit 1
60
+ fi
61
+}
62
+
63
+check_tc_action_hw_stats_support()
64
+{
65
+ tc actions help 2>&1 | grep -q hw_stats
66
+ if [[ $? -ne 0 ]]; then
67
+ echo "SKIP: iproute2 too old; tc is missing action hw_stats support"
68
+ exit 1
69
+ fi
70
+}
71
+
72
+skip_on_veth()
73
+{
74
+ local kind=$(ip -j -d link show dev ${NETIFS[p1]} |
75
+ jq -r '.[].linkinfo.info_kind')
76
+
77
+ if [[ $kind == veth ]]; then
78
+ echo "SKIP: Test cannot be run with veth pairs"
79
+ exit $ksft_skip
5580 fi
5681 }
5782
....@@ -104,8 +129,13 @@
104129 {
105130 local i
106131
107
- for i in $(eval echo {1..$NUM_NETIFS}); do
132
+ for ((i = 1; i <= NUM_NETIFS; ++i)); do
108133 local j=$((i+1))
134
+
135
+ if [ -z ${NETIFS[p$i]} ]; then
136
+ echo "SKIP: Cannot create interface. Name not specified"
137
+ exit $ksft_skip
138
+ fi
109139
110140 ip link show dev ${NETIFS[p$i]} &> /dev/null
111141 if [[ $? -ne 0 ]]; then
....@@ -135,7 +165,7 @@
135165 create_netif
136166 fi
137167
138
-for i in $(eval echo {1..$NUM_NETIFS}); do
168
+for ((i = 1; i <= NUM_NETIFS; ++i)); do
139169 ip link show dev ${NETIFS[p$i]} &> /dev/null
140170 if [[ $? -ne 0 ]]; then
141171 echo "SKIP: could not find all required interfaces"
....@@ -209,7 +239,7 @@
209239 return 1
210240 fi
211241
212
- printf "TEST: %-60s [PASS]\n" "$test_name $opt_str"
242
+ printf "TEST: %-60s [ OK ]\n" "$test_name $opt_str"
213243 return 0
214244 }
215245
....@@ -220,31 +250,144 @@
220250 echo "INFO: $msg"
221251 }
222252
253
+busywait()
254
+{
255
+ local timeout=$1; shift
256
+
257
+ local start_time="$(date -u +%s%3N)"
258
+ while true
259
+ do
260
+ local out
261
+ out=$("$@")
262
+ local ret=$?
263
+ if ((!ret)); then
264
+ echo -n "$out"
265
+ return 0
266
+ fi
267
+
268
+ local current_time="$(date -u +%s%3N)"
269
+ if ((current_time - start_time > timeout)); then
270
+ echo -n "$out"
271
+ return 1
272
+ fi
273
+ done
274
+}
275
+
276
+not()
277
+{
278
+ "$@"
279
+ [[ $? != 0 ]]
280
+}
281
+
282
+grep_bridge_fdb()
283
+{
284
+ local addr=$1; shift
285
+ local word
286
+ local flag
287
+
288
+ if [ "$1" == "self" ] || [ "$1" == "master" ]; then
289
+ word=$1; shift
290
+ if [ "$1" == "-v" ]; then
291
+ flag=$1; shift
292
+ fi
293
+ fi
294
+
295
+ $@ | grep $addr | grep $flag "$word"
296
+}
297
+
298
+wait_for_offload()
299
+{
300
+ "$@" | grep -q offload
301
+}
302
+
303
+until_counter_is()
304
+{
305
+ local expr=$1; shift
306
+ local current=$("$@")
307
+
308
+ echo $((current))
309
+ ((current $expr))
310
+}
311
+
312
+busywait_for_counter()
313
+{
314
+ local timeout=$1; shift
315
+ local delta=$1; shift
316
+
317
+ local base=$("$@")
318
+ busywait "$timeout" until_counter_is ">= $((base + delta))" "$@"
319
+}
320
+
223321 setup_wait_dev()
224322 {
225323 local dev=$1; shift
324
+ local wait_time=${1:-$WAIT_TIME}; shift
226325
227
- while true; do
326
+ setup_wait_dev_with_timeout "$dev" $INTERFACE_TIMEOUT $wait_time
327
+
328
+ if (($?)); then
329
+ check_err 1
330
+ log_test setup_wait_dev ": Interface $dev does not come up."
331
+ exit 1
332
+ fi
333
+}
334
+
335
+setup_wait_dev_with_timeout()
336
+{
337
+ local dev=$1; shift
338
+ local max_iterations=${1:-$WAIT_TIMEOUT}; shift
339
+ local wait_time=${1:-$WAIT_TIME}; shift
340
+ local i
341
+
342
+ for ((i = 1; i <= $max_iterations; ++i)); do
228343 ip link show dev $dev up \
229344 | grep 'state UP' &> /dev/null
230345 if [[ $? -ne 0 ]]; then
231346 sleep 1
232347 else
233
- break
348
+ sleep $wait_time
349
+ return 0
234350 fi
235351 done
352
+
353
+ return 1
236354 }
237355
238356 setup_wait()
239357 {
240358 local num_netifs=${1:-$NUM_NETIFS}
359
+ local i
241360
242361 for ((i = 1; i <= num_netifs; ++i)); do
243
- setup_wait_dev ${NETIFS[p$i]}
362
+ setup_wait_dev ${NETIFS[p$i]} 0
244363 done
245364
246365 # Make sure links are ready.
247366 sleep $WAIT_TIME
367
+}
368
+
369
+cmd_jq()
370
+{
371
+ local cmd=$1
372
+ local jq_exp=$2
373
+ local jq_opts=$3
374
+ local ret
375
+ local output
376
+
377
+ output="$($cmd)"
378
+ # it the command fails, return error right away
379
+ ret=$?
380
+ if [[ $ret -ne 0 ]]; then
381
+ return $ret
382
+ fi
383
+ output=$(echo $output | jq -r $jq_opts "$jq_exp")
384
+ ret=$?
385
+ if [[ $ret -ne 0 ]]; then
386
+ return $ret
387
+ fi
388
+ echo $output
389
+ # return success only in case of non-empty output
390
+ [ ! -z "$output" ]
248391 }
249392
250393 lldpad_app_wait_set()
....@@ -477,11 +620,24 @@
477620 ip -j link show dev $if_name | jq -r '.[]["master"]'
478621 }
479622
623
+link_stats_get()
624
+{
625
+ local if_name=$1; shift
626
+ local dir=$1; shift
627
+ local stat=$1; shift
628
+
629
+ ip -j -s link show dev $if_name \
630
+ | jq '.[]["stats64"]["'$dir'"]["'$stat'"]'
631
+}
632
+
480633 link_stats_tx_packets_get()
481634 {
482
- local if_name=$1
635
+ link_stats_get $1 tx packets
636
+}
483637
484
- ip -j -s link show dev $if_name | jq '.[]["stats64"]["tx"]["packets"]'
638
+link_stats_rx_errors_get()
639
+{
640
+ link_stats_get $1 rx errors
485641 }
486642
487643 tc_rule_stats_get()
....@@ -489,9 +645,81 @@
489645 local dev=$1; shift
490646 local pref=$1; shift
491647 local dir=$1; shift
648
+ local selector=${1:-.packets}; shift
492649
493650 tc -j -s filter show dev $dev ${dir:-ingress} pref $pref \
494
- | jq '.[1].options.actions[].stats.packets'
651
+ | jq ".[1].options.actions[].stats$selector"
652
+}
653
+
654
+tc_rule_handle_stats_get()
655
+{
656
+ local id=$1; shift
657
+ local handle=$1; shift
658
+ local selector=${1:-.packets}; shift
659
+
660
+ tc -j -s filter show $id \
661
+ | jq ".[] | select(.options.handle == $handle) | \
662
+ .options.actions[0].stats$selector"
663
+}
664
+
665
+ethtool_stats_get()
666
+{
667
+ local dev=$1; shift
668
+ local stat=$1; shift
669
+
670
+ ethtool -S $dev | grep "^ *$stat:" | head -n 1 | cut -d: -f2
671
+}
672
+
673
+qdisc_stats_get()
674
+{
675
+ local dev=$1; shift
676
+ local handle=$1; shift
677
+ local selector=$1; shift
678
+
679
+ tc -j -s qdisc show dev "$dev" \
680
+ | jq '.[] | select(.handle == "'"$handle"'") | '"$selector"
681
+}
682
+
683
+qdisc_parent_stats_get()
684
+{
685
+ local dev=$1; shift
686
+ local parent=$1; shift
687
+ local selector=$1; shift
688
+
689
+ tc -j -s qdisc show dev "$dev" invisible \
690
+ | jq '.[] | select(.parent == "'"$parent"'") | '"$selector"
691
+}
692
+
693
+ipv6_stats_get()
694
+{
695
+ local dev=$1; shift
696
+ local stat=$1; shift
697
+
698
+ cat /proc/net/dev_snmp6/$dev | grep "^$stat" | cut -f2
699
+}
700
+
701
+humanize()
702
+{
703
+ local speed=$1; shift
704
+
705
+ for unit in bps Kbps Mbps Gbps; do
706
+ if (($(echo "$speed < 1024" | bc))); then
707
+ break
708
+ fi
709
+
710
+ speed=$(echo "scale=1; $speed / 1024" | bc)
711
+ done
712
+
713
+ echo "$speed${unit}"
714
+}
715
+
716
+rate()
717
+{
718
+ local t0=$1; shift
719
+ local t1=$1; shift
720
+ local interval=$1; shift
721
+
722
+ echo $((8 * (t1 - t0) / interval))
495723 }
496724
497725 mac_get()
....@@ -519,14 +747,14 @@
519747 local value=$1; shift
520748
521749 SYSCTL_ORIG[$key]=$(sysctl -n $key)
522
- sysctl -qw $key=$value
750
+ sysctl -qw $key="$value"
523751 }
524752
525753 sysctl_restore()
526754 {
527755 local key=$1; shift
528756
529
- sysctl -qw $key=${SYSCTL_ORIG["$key"]}
757
+ sysctl -qw $key="${SYSCTL_ORIG[$key]}"
530758 }
531759
532760 forwarding_enable()
....@@ -539,6 +767,23 @@
539767 {
540768 sysctl_restore net.ipv6.conf.all.forwarding
541769 sysctl_restore net.ipv4.conf.all.forwarding
770
+}
771
+
772
+declare -A MTU_ORIG
773
+mtu_set()
774
+{
775
+ local dev=$1; shift
776
+ local mtu=$1; shift
777
+
778
+ MTU_ORIG["$dev"]=$(ip -j link show dev $dev | jq -e '.[].mtu')
779
+ ip link set dev $dev mtu $mtu
780
+}
781
+
782
+mtu_restore()
783
+{
784
+ local dev=$1; shift
785
+
786
+ ip link set dev $dev mtu ${MTU_ORIG["$dev"]}
542787 }
543788
544789 tc_offload_check()
....@@ -758,6 +1003,17 @@
7581003 log_info "Expected ratio $weights_ratio Measured ratio $packets_ratio"
7591004 }
7601005
1006
+in_ns()
1007
+{
1008
+ local name=$1; shift
1009
+
1010
+ ip netns exec $name bash <<-EOF
1011
+ NUM_NETIFS=0
1012
+ source lib.sh
1013
+ $(for a in "$@"; do printf "%q${IFS:0:1}" "$a"; done)
1014
+ EOF
1015
+}
1016
+
7611017 ##############################################################################
7621018 # Tests
7631019
....@@ -765,10 +1021,12 @@
7651021 {
7661022 local if_name=$1
7671023 local dip=$2
1024
+ local args=$3
7681025 local vrf_name
7691026
7701027 vrf_name=$(master_name_get $if_name)
771
- ip vrf exec $vrf_name $PING $dip -c 10 -i 0.1 -w 2 &> /dev/null
1028
+ ip vrf exec $vrf_name \
1029
+ $PING $args $dip -c 10 -i 0.1 -w $PING_TIMEOUT &> /dev/null
7721030 }
7731031
7741032 ping_test()
....@@ -777,17 +1035,19 @@
7771035
7781036 ping_do $1 $2
7791037 check_err $?
780
- log_test "ping"
1038
+ log_test "ping$3"
7811039 }
7821040
7831041 ping6_do()
7841042 {
7851043 local if_name=$1
7861044 local dip=$2
1045
+ local args=$3
7871046 local vrf_name
7881047
7891048 vrf_name=$(master_name_get $if_name)
790
- ip vrf exec $vrf_name $PING6 $dip -c 10 -i 0.1 -w 2 &> /dev/null
1049
+ ip vrf exec $vrf_name \
1050
+ $PING6 $args $dip -c 10 -i 0.1 -w $PING_TIMEOUT &> /dev/null
7911051 }
7921052
7931053 ping6_test()
....@@ -796,7 +1056,7 @@
7961056
7971057 ping6_do $1 $2
7981058 check_err $?
799
- log_test "ping6"
1059
+ log_test "ping6$3"
8001060 }
8011061
8021062 learning_test()
....@@ -819,6 +1079,7 @@
8191079 # FDB entry was installed.
8201080 bridge link set dev $br_port1 flood off
8211081
1082
+ ip link set $host1_if promisc on
8221083 tc qdisc add dev $host1_if ingress
8231084 tc filter add dev $host1_if ingress protocol ip pref 1 handle 101 \
8241085 flower dst_mac $mac action drop
....@@ -829,7 +1090,7 @@
8291090 tc -j -s filter show dev $host1_if ingress \
8301091 | jq -e ".[] | select(.options.handle == 101) \
8311092 | select(.options.actions[0].stats.packets == 1)" &> /dev/null
832
- check_fail $? "Packet reached second host when should not"
1093
+ check_fail $? "Packet reached first host when should not"
8331094
8341095 $MZ $host1_if -c 1 -p 64 -a $mac -t ip -q
8351096 sleep 1
....@@ -868,6 +1129,7 @@
8681129
8691130 tc filter del dev $host1_if ingress protocol ip pref 1 handle 101 flower
8701131 tc qdisc del dev $host1_if ingress
1132
+ ip link set $host1_if promisc off
8711133
8721134 bridge link set dev $br_port1 flood on
8731135
....@@ -885,6 +1147,7 @@
8851147
8861148 # Add an ACL on `host2_if` which will tell us whether the packet
8871149 # was flooded to it or not.
1150
+ ip link set $host2_if promisc on
8881151 tc qdisc add dev $host2_if ingress
8891152 tc filter add dev $host2_if ingress protocol ip pref 1 handle 101 \
8901153 flower dst_mac $mac action drop
....@@ -902,6 +1165,7 @@
9021165
9031166 tc filter del dev $host2_if ingress protocol ip pref 1 handle 101 flower
9041167 tc qdisc del dev $host2_if ingress
1168
+ ip link set $host2_if promisc off
9051169
9061170 return $err
9071171 }
....@@ -962,3 +1226,75 @@
9621226 flood_unicast_test $br_port $host1_if $host2_if
9631227 flood_multicast_test $br_port $host1_if $host2_if
9641228 }
1229
+
1230
+__start_traffic()
1231
+{
1232
+ local proto=$1; shift
1233
+ local h_in=$1; shift # Where the traffic egresses the host
1234
+ local sip=$1; shift
1235
+ local dip=$1; shift
1236
+ local dmac=$1; shift
1237
+
1238
+ $MZ $h_in -p 8000 -A $sip -B $dip -c 0 \
1239
+ -a own -b $dmac -t "$proto" -q "$@" &
1240
+ sleep 1
1241
+}
1242
+
1243
+start_traffic()
1244
+{
1245
+ __start_traffic udp "$@"
1246
+}
1247
+
1248
+start_tcp_traffic()
1249
+{
1250
+ __start_traffic tcp "$@"
1251
+}
1252
+
1253
+stop_traffic()
1254
+{
1255
+ # Suppress noise from killing mausezahn.
1256
+ { kill %% && wait %%; } 2>/dev/null
1257
+}
1258
+
1259
+tcpdump_start()
1260
+{
1261
+ local if_name=$1; shift
1262
+ local ns=$1; shift
1263
+
1264
+ capfile=$(mktemp)
1265
+ capout=$(mktemp)
1266
+
1267
+ if [ -z $ns ]; then
1268
+ ns_cmd=""
1269
+ else
1270
+ ns_cmd="ip netns exec ${ns}"
1271
+ fi
1272
+
1273
+ if [ -z $SUDO_USER ] ; then
1274
+ capuser=""
1275
+ else
1276
+ capuser="-Z $SUDO_USER"
1277
+ fi
1278
+
1279
+ $ns_cmd tcpdump -e -n -Q in -i $if_name \
1280
+ -s 65535 -B 32768 $capuser -w $capfile > "$capout" 2>&1 &
1281
+ cappid=$!
1282
+
1283
+ sleep 1
1284
+}
1285
+
1286
+tcpdump_stop()
1287
+{
1288
+ $ns_cmd kill $cappid
1289
+ sleep 1
1290
+}
1291
+
1292
+tcpdump_cleanup()
1293
+{
1294
+ rm $capfile $capout
1295
+}
1296
+
1297
+tcpdump_show()
1298
+{
1299
+ tcpdump -e -n -r $capfile 2>&1
1300
+}