0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 lib_dir=$(dirname $0)/../../../net/forwarding
0033
0034 ALL_TESTS="
0035 tc_sample_rate_test
0036 tc_sample_max_rate_test
0037 tc_sample_conflict_test
0038 tc_sample_group_conflict_test
0039 tc_sample_md_iif_test
0040 tc_sample_md_lag_iif_test
0041 tc_sample_md_oif_test
0042 tc_sample_md_lag_oif_test
0043 tc_sample_md_out_tc_test
0044 tc_sample_md_out_tc_occ_test
0045 tc_sample_md_latency_test
0046 tc_sample_acl_group_conflict_test
0047 tc_sample_acl_rate_test
0048 tc_sample_acl_max_rate_test
0049 "
0050 NUM_NETIFS=8
0051 CAPTURE_FILE=$(mktemp)
0052 source $lib_dir/lib.sh
0053 source $lib_dir/devlink_lib.sh
0054 source mlxsw_lib.sh
0055
0056
0057 require_command psample
0058
0059 h1_create()
0060 {
0061 simple_if_init $h1 192.0.2.1/28
0062
0063 ip -4 route add default vrf v$h1 nexthop via 192.0.2.2
0064 }
0065
0066 h1_destroy()
0067 {
0068 ip -4 route del default vrf v$h1 nexthop via 192.0.2.2
0069
0070 simple_if_fini $h1 192.0.2.1/28
0071 }
0072
0073 h2_create()
0074 {
0075 simple_if_init $h2 198.51.100.1/28
0076
0077 ip -4 route add default vrf v$h2 nexthop via 198.51.100.2
0078 }
0079
0080 h2_destroy()
0081 {
0082 ip -4 route del default vrf v$h2 nexthop via 198.51.100.2
0083
0084 simple_if_fini $h2 198.51.100.1/28
0085 }
0086
0087 h3_create()
0088 {
0089 ip link set dev $h3 down
0090 ip link add name ${h3}_bond type bond mode 802.3ad
0091 ip link set dev $h3 master ${h3}_bond
0092
0093 simple_if_init ${h3}_bond 192.0.2.17/28
0094
0095 ip -4 route add default vrf v${h3}_bond nexthop via 192.0.2.18
0096 }
0097
0098 h3_destroy()
0099 {
0100 ip -4 route del default vrf v${h3}_bond nexthop via 192.0.2.18
0101
0102 simple_if_fini ${h3}_bond 192.0.2.17/28
0103
0104 ip link set dev $h3 nomaster
0105 ip link del dev ${h3}_bond
0106 }
0107
0108 h4_create()
0109 {
0110 ip link set dev $h4 down
0111 ip link add name ${h4}_bond type bond mode 802.3ad
0112 ip link set dev $h4 master ${h4}_bond
0113
0114 simple_if_init ${h4}_bond 198.51.100.17/28
0115
0116 ip -4 route add default vrf v${h4}_bond nexthop via 198.51.100.18
0117 }
0118
0119 h4_destroy()
0120 {
0121 ip -4 route del default vrf v${h4}_bond nexthop via 198.51.100.18
0122
0123 simple_if_fini ${h4}_bond 198.51.100.17/28
0124
0125 ip link set dev $h4 nomaster
0126 ip link del dev ${h4}_bond
0127 }
0128
0129 router_create()
0130 {
0131 ip link set dev $rp1 up
0132 __addr_add_del $rp1 add 192.0.2.2/28
0133 tc qdisc add dev $rp1 clsact
0134
0135 ip link set dev $rp2 up
0136 __addr_add_del $rp2 add 198.51.100.2/28
0137 tc qdisc add dev $rp2 clsact
0138
0139 ip link add name ${rp3}_bond type bond mode 802.3ad
0140 ip link set dev $rp3 master ${rp3}_bond
0141 __addr_add_del ${rp3}_bond add 192.0.2.18/28
0142 tc qdisc add dev $rp3 clsact
0143 ip link set dev ${rp3}_bond up
0144
0145 ip link add name ${rp4}_bond type bond mode 802.3ad
0146 ip link set dev $rp4 master ${rp4}_bond
0147 __addr_add_del ${rp4}_bond add 198.51.100.18/28
0148 tc qdisc add dev $rp4 clsact
0149 ip link set dev ${rp4}_bond up
0150 }
0151
0152 router_destroy()
0153 {
0154 ip link set dev ${rp4}_bond down
0155 tc qdisc del dev $rp4 clsact
0156 __addr_add_del ${rp4}_bond del 198.51.100.18/28
0157 ip link set dev $rp4 nomaster
0158 ip link del dev ${rp4}_bond
0159
0160 ip link set dev ${rp3}_bond down
0161 tc qdisc del dev $rp3 clsact
0162 __addr_add_del ${rp3}_bond del 192.0.2.18/28
0163 ip link set dev $rp3 nomaster
0164 ip link del dev ${rp3}_bond
0165
0166 tc qdisc del dev $rp2 clsact
0167 __addr_add_del $rp2 del 198.51.100.2/28
0168 ip link set dev $rp2 down
0169
0170 tc qdisc del dev $rp1 clsact
0171 __addr_add_del $rp1 del 192.0.2.2/28
0172 ip link set dev $rp1 down
0173 }
0174
0175 setup_prepare()
0176 {
0177 h1=${NETIFS[p1]}
0178 rp1=${NETIFS[p2]}
0179 rp2=${NETIFS[p3]}
0180 h2=${NETIFS[p4]}
0181 h3=${NETIFS[p5]}
0182 rp3=${NETIFS[p6]}
0183 h4=${NETIFS[p7]}
0184 rp4=${NETIFS[p8]}
0185
0186 vrf_prepare
0187
0188 h1_create
0189 h2_create
0190 h3_create
0191 h4_create
0192 router_create
0193 }
0194
0195 cleanup()
0196 {
0197 pre_cleanup
0198
0199 rm -f $CAPTURE_FILE
0200
0201 router_destroy
0202 h4_destroy
0203 h3_destroy
0204 h2_destroy
0205 h1_destroy
0206
0207 vrf_cleanup
0208 }
0209
0210 psample_capture_start()
0211 {
0212 rm -f $CAPTURE_FILE
0213
0214 psample &> $CAPTURE_FILE &
0215
0216 sleep 1
0217 }
0218
0219 psample_capture_stop()
0220 {
0221 { kill %% && wait %%; } 2>/dev/null
0222 }
0223
0224 __tc_sample_rate_test()
0225 {
0226 local desc=$1; shift
0227 local dip=$1; shift
0228 local pkts pct
0229
0230 RET=0
0231
0232 tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
0233 skip_sw action sample rate 32 group 1
0234 check_err $? "Failed to configure sampling rule"
0235
0236 psample_capture_start
0237
0238 ip vrf exec v$h1 $MZ $h1 -c 320000 -d 100usec -p 64 -A 192.0.2.1 \
0239 -B $dip -t udp dp=52768,sp=42768 -q
0240
0241 psample_capture_stop
0242
0243 pkts=$(grep -e "group 1 " $CAPTURE_FILE | wc -l)
0244 pct=$((100 * (pkts - 10000) / 10000))
0245 (( -25 <= pct && pct <= 25))
0246 check_err $? "Expected 10000 packets, got $pkts packets, which is $pct% off. Required accuracy is +-25%"
0247
0248 log_test "tc sample rate ($desc)"
0249
0250 tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
0251 }
0252
0253 tc_sample_rate_test()
0254 {
0255 __tc_sample_rate_test "forward" 198.51.100.1
0256 __tc_sample_rate_test "local receive" 192.0.2.2
0257 }
0258
0259 tc_sample_max_rate_test()
0260 {
0261 RET=0
0262
0263 tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
0264 skip_sw action sample rate $((35 * 10 ** 8)) group 1
0265 check_err $? "Failed to configure sampling rule with max rate"
0266
0267 tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
0268
0269 tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
0270 skip_sw action sample rate $((35 * 10 ** 8 + 1)) \
0271 group 1 &> /dev/null
0272 check_fail $? "Managed to configure sampling rate above maximum"
0273
0274 log_test "tc sample maximum rate"
0275 }
0276
0277 tc_sample_conflict_test()
0278 {
0279 RET=0
0280
0281
0282
0283
0284 tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
0285 skip_sw action sample rate 1024 group 1
0286 check_err $? "Failed to configure sampling rule"
0287
0288 tc filter add dev $rp1 ingress protocol all pref 2 handle 102 matchall \
0289 skip_sw action sample rate 1024 group 1 &> /dev/null
0290 check_fail $? "Managed to configure second sampling rule"
0291
0292
0293
0294
0295 tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
0296
0297 tc filter add dev $rp1 ingress protocol all pref 2 handle 102 matchall \
0298 skip_sw action sample rate 1024 group 1
0299 check_err $? "Failed to configure sampling rule after deletion"
0300
0301 log_test "tc sample conflict test"
0302
0303 tc filter del dev $rp1 ingress protocol all pref 2 handle 102 matchall
0304 }
0305
0306 tc_sample_group_conflict_test()
0307 {
0308 RET=0
0309
0310
0311
0312
0313 tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
0314 skip_sw action sample rate 1024 group 1
0315 check_err $? "Failed to configure sampling rule"
0316
0317 tc filter add dev $rp1 ingress protocol all pref 2 handle 102 matchall \
0318 skip_sw action sample rate 1024 group 2 &> /dev/null
0319 check_fail $? "Managed to configure sampling rule with conflicting group"
0320
0321 log_test "tc sample group conflict test"
0322
0323 tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
0324 }
0325
0326 tc_sample_md_iif_test()
0327 {
0328 local rp1_ifindex
0329
0330 RET=0
0331
0332 tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
0333 skip_sw action sample rate 5 group 1
0334 check_err $? "Failed to configure sampling rule"
0335
0336 psample_capture_start
0337
0338 ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
0339 -B 198.51.100.1 -t udp dp=52768,sp=42768 -q
0340
0341 psample_capture_stop
0342
0343 rp1_ifindex=$(ip -j -p link show dev $rp1 | jq '.[]["ifindex"]')
0344 grep -q -e "in-ifindex $rp1_ifindex " $CAPTURE_FILE
0345 check_err $? "Sampled packets do not have expected in-ifindex"
0346
0347 log_test "tc sample iif"
0348
0349 tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
0350 }
0351
0352 tc_sample_md_lag_iif_test()
0353 {
0354 local rp3_ifindex
0355
0356 RET=0
0357
0358 tc filter add dev $rp3 ingress protocol all pref 1 handle 101 matchall \
0359 skip_sw action sample rate 5 group 1
0360 check_err $? "Failed to configure sampling rule"
0361
0362 psample_capture_start
0363
0364 ip vrf exec v${h3}_bond $MZ ${h3}_bond -c 3200 -d 1msec -p 64 \
0365 -A 192.0.2.17 -B 198.51.100.17 -t udp dp=52768,sp=42768 -q
0366
0367 psample_capture_stop
0368
0369 rp3_ifindex=$(ip -j -p link show dev $rp3 | jq '.[]["ifindex"]')
0370 grep -q -e "in-ifindex $rp3_ifindex " $CAPTURE_FILE
0371 check_err $? "Sampled packets do not have expected in-ifindex"
0372
0373 log_test "tc sample lag iif"
0374
0375 tc filter del dev $rp3 ingress protocol all pref 1 handle 101 matchall
0376 }
0377
0378 tc_sample_md_oif_test()
0379 {
0380 local rp2_ifindex
0381
0382 RET=0
0383
0384 tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
0385 skip_sw action sample rate 5 group 1
0386 check_err $? "Failed to configure sampling rule"
0387
0388 psample_capture_start
0389
0390 ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
0391 -B 198.51.100.1 -t udp dp=52768,sp=42768 -q
0392
0393 psample_capture_stop
0394
0395 rp2_ifindex=$(ip -j -p link show dev $rp2 | jq '.[]["ifindex"]')
0396 grep -q -e "out-ifindex $rp2_ifindex " $CAPTURE_FILE
0397 check_err $? "Sampled packets do not have expected out-ifindex"
0398
0399 log_test "tc sample oif"
0400
0401 tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
0402 }
0403
0404 tc_sample_md_lag_oif_test()
0405 {
0406 local rp4_ifindex
0407
0408 RET=0
0409
0410 tc filter add dev $rp3 ingress protocol all pref 1 handle 101 matchall \
0411 skip_sw action sample rate 5 group 1
0412 check_err $? "Failed to configure sampling rule"
0413
0414 psample_capture_start
0415
0416 ip vrf exec v${h3}_bond $MZ ${h3}_bond -c 3200 -d 1msec -p 64 \
0417 -A 192.0.2.17 -B 198.51.100.17 -t udp dp=52768,sp=42768 -q
0418
0419 psample_capture_stop
0420
0421 rp4_ifindex=$(ip -j -p link show dev $rp4 | jq '.[]["ifindex"]')
0422 grep -q -e "out-ifindex $rp4_ifindex " $CAPTURE_FILE
0423 check_err $? "Sampled packets do not have expected out-ifindex"
0424
0425 log_test "tc sample lag oif"
0426
0427 tc filter del dev $rp3 ingress protocol all pref 1 handle 101 matchall
0428 }
0429
0430 tc_sample_md_out_tc_test()
0431 {
0432 RET=0
0433
0434
0435 mlxsw_only_on_spectrum 2+ || return
0436
0437 tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
0438 skip_sw action sample rate 5 group 1
0439 check_err $? "Failed to configure sampling rule"
0440
0441
0442
0443 psample_capture_start
0444
0445 ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
0446 -B 198.51.100.1 -t udp dp=52768,sp=42768 -q
0447
0448 psample_capture_stop
0449
0450 grep -q -e "out-tc 0 " $CAPTURE_FILE
0451 check_err $? "Sampled packets do not have expected out-tc (0)"
0452
0453
0454
0455 tc qdisc replace dev $rp2 root handle 1: \
0456 prio bands 3 priomap 0 0 0 0 0 0 0 0
0457
0458 psample_capture_start
0459
0460 ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
0461 -B 198.51.100.1 -t udp dp=52768,sp=42768 -q
0462
0463 psample_capture_stop
0464
0465 grep -q -e "out-tc 7 " $CAPTURE_FILE
0466 check_err $? "Sampled packets do not have expected out-tc (7)"
0467
0468 log_test "tc sample out-tc"
0469
0470 tc qdisc del dev $rp2 root handle 1:
0471 tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
0472 }
0473
0474 tc_sample_md_out_tc_occ_test()
0475 {
0476 local backlog pct occ
0477
0478 RET=0
0479
0480
0481 mlxsw_only_on_spectrum 2+ || return
0482
0483 tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
0484 skip_sw action sample rate 1024 group 1
0485 check_err $? "Failed to configure sampling rule"
0486
0487
0488 tc qdisc replace dev $rp2 root handle 1: \
0489 tbf rate 1Mbit burst 256k limit 1M
0490
0491 psample_capture_start
0492
0493 ip vrf exec v$h1 $MZ $h1 -c 0 -d 1usec -p 1400 -A 192.0.2.1 \
0494 -B 198.51.100.1 -t udp dp=52768,sp=42768 -q &
0495
0496
0497 sleep 10
0498
0499 backlog=$(tc -j -p -s qdisc show dev $rp2 | jq '.[0]["backlog"]')
0500
0501
0502 { kill %% && wait %%; } 2>/dev/null
0503
0504 psample_capture_stop
0505
0506
0507 occ=$(grep -e "out-tc-occ " $CAPTURE_FILE | tail -n 1 | \
0508 cut -d ' ' -f 16)
0509
0510 pct=$((100 * (occ - backlog) / backlog))
0511 (( -1 <= pct && pct <= 1))
0512 check_err $? "Recorded a congestion of $backlog bytes, but sampled congestion is $occ bytes, which is $pct% off. Required accuracy is +-5%"
0513
0514 log_test "tc sample out-tc-occ"
0515
0516 tc qdisc del dev $rp2 root handle 1:
0517 tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
0518 }
0519
0520 tc_sample_md_latency_test()
0521 {
0522 RET=0
0523
0524
0525 mlxsw_only_on_spectrum 2+ || return
0526
0527 tc filter add dev $rp2 egress protocol all pref 1 handle 101 matchall \
0528 skip_sw action sample rate 5 group 1
0529 check_err $? "Failed to configure sampling rule"
0530
0531 psample_capture_start
0532
0533 ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
0534 -B 198.51.100.1 -t udp dp=52768,sp=42768 -q
0535
0536 psample_capture_stop
0537
0538 grep -q -e "latency " $CAPTURE_FILE
0539 check_err $? "Sampled packets do not have latency attribute"
0540
0541 log_test "tc sample latency"
0542
0543 tc filter del dev $rp2 egress protocol all pref 1 handle 101 matchall
0544 }
0545
0546 tc_sample_acl_group_conflict_test()
0547 {
0548 RET=0
0549
0550
0551
0552
0553
0554 mlxsw_only_on_spectrum 2+ || return
0555
0556 tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
0557 skip_sw action sample rate 1024 group 1
0558 check_err $? "Failed to configure sampling rule"
0559
0560 tc filter add dev $rp1 ingress protocol ip pref 2 handle 102 flower \
0561 skip_sw action sample rate 1024 group 1
0562 check_err $? "Failed to configure sampling rule with same group"
0563
0564 tc filter add dev $rp1 ingress protocol ip pref 3 handle 103 flower \
0565 skip_sw action sample rate 1024 group 2 &> /dev/null
0566 check_fail $? "Managed to configure sampling rule with conflicting group"
0567
0568 log_test "tc sample (w/ flower) group conflict test"
0569
0570 tc filter del dev $rp1 ingress protocol ip pref 2 handle 102 flower
0571 tc filter del dev $rp1 ingress protocol ip pref 1 handle 101 flower
0572 }
0573
0574 __tc_sample_acl_rate_test()
0575 {
0576 local bind=$1; shift
0577 local port=$1; shift
0578 local pkts pct
0579
0580 RET=0
0581
0582
0583 mlxsw_only_on_spectrum 2+ || return
0584
0585 tc filter add dev $port $bind protocol ip pref 1 handle 101 flower \
0586 skip_sw dst_ip 198.51.100.1 action sample rate 32 group 1
0587 check_err $? "Failed to configure sampling rule"
0588
0589 psample_capture_start
0590
0591 ip vrf exec v$h1 $MZ $h1 -c 320000 -d 100usec -p 64 -A 192.0.2.1 \
0592 -B 198.51.100.1 -t udp dp=52768,sp=42768 -q
0593
0594 psample_capture_stop
0595
0596 pkts=$(grep -e "group 1 " $CAPTURE_FILE | wc -l)
0597 pct=$((100 * (pkts - 10000) / 10000))
0598 (( -25 <= pct && pct <= 25))
0599 check_err $? "Expected 10000 packets, got $pkts packets, which is $pct% off. Required accuracy is +-25%"
0600
0601
0602
0603 tc filter del dev $port $bind protocol ip pref 1 handle 101 flower
0604
0605 tc filter add dev $port $bind protocol ip pref 1 handle 101 flower \
0606 skip_sw dst_ip 198.51.100.10 action sample rate 32 group 1
0607 check_err $? "Failed to configure sampling rule"
0608
0609 psample_capture_start
0610
0611 ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
0612 -B 198.51.100.1 -t udp dp=52768,sp=42768 -q
0613
0614 psample_capture_stop
0615
0616 grep -q -e "group 1 " $CAPTURE_FILE
0617 check_fail $? "Sampled packets when should not"
0618
0619 log_test "tc sample (w/ flower) rate ($bind)"
0620
0621 tc filter del dev $port $bind protocol ip pref 1 handle 101 flower
0622 }
0623
0624 tc_sample_acl_rate_test()
0625 {
0626 __tc_sample_acl_rate_test ingress $rp1
0627 __tc_sample_acl_rate_test egress $rp2
0628 }
0629
0630 tc_sample_acl_max_rate_test()
0631 {
0632 RET=0
0633
0634
0635 mlxsw_only_on_spectrum 2+ || return
0636
0637 tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
0638 skip_sw action sample rate $((2 ** 24 - 1)) group 1
0639 check_err $? "Failed to configure sampling rule with max rate"
0640
0641 tc filter del dev $rp1 ingress protocol ip pref 1 handle 101 flower
0642
0643 tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
0644 skip_sw action sample rate $((2 ** 24)) \
0645 group 1 &> /dev/null
0646 check_fail $? "Managed to configure sampling rate above maximum"
0647
0648 log_test "tc sample (w/ flower) maximum rate"
0649 }
0650
0651 trap cleanup EXIT
0652
0653 setup_prepare
0654 setup_wait
0655
0656 tests_run
0657
0658 exit $EXIT_STATUS