Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # Test tc-police action.
0005 #
0006 # +---------------------------------+
0007 # | H1 (vrf)                        |
0008 # |    + $h1                        |
0009 # |    | 192.0.2.1/24               |
0010 # |    |                            |
0011 # |    |  default via 192.0.2.2     |
0012 # +----|----------------------------+
0013 #      |
0014 # +----|----------------------------------------------------------------------+
0015 # | SW |                                                                      |
0016 # |    + $rp1                                                                 |
0017 # |        192.0.2.2/24                                                       |
0018 # |                                                                           |
0019 # |        198.51.100.2/24                           203.0.113.2/24           |
0020 # |    + $rp2                                    + $rp3                       |
0021 # |    |                                         |                            |
0022 # +----|-----------------------------------------|----------------------------+
0023 #      |                                         |
0024 # +----|----------------------------+       +----|----------------------------+
0025 # |    |  default via 198.51.100.2  |       |    |  default via 203.0.113.2   |
0026 # |    |                            |       |    |                            |
0027 # |    | 198.51.100.1/24            |       |    | 203.0.113.1/24             |
0028 # |    + $h2                        |       |    + $h3                        |
0029 # | H2 (vrf)                        |       | H3 (vrf)                        |
0030 # +---------------------------------+       +---------------------------------+
0031 
0032 ALL_TESTS="
0033         police_rx_test
0034         police_tx_test
0035         police_shared_test
0036         police_rx_mirror_test
0037         police_tx_mirror_test
0038         police_pps_rx_test
0039         police_pps_tx_test
0040         police_mtu_rx_test
0041         police_mtu_tx_test
0042 "
0043 NUM_NETIFS=6
0044 source tc_common.sh
0045 source lib.sh
0046 
0047 h1_create()
0048 {
0049         simple_if_init $h1 192.0.2.1/24
0050 
0051         ip -4 route add default vrf v$h1 nexthop via 192.0.2.2
0052 }
0053 
0054 h1_destroy()
0055 {
0056         ip -4 route del default vrf v$h1 nexthop via 192.0.2.2
0057 
0058         simple_if_fini $h1 192.0.2.1/24
0059 }
0060 
0061 h2_create()
0062 {
0063         simple_if_init $h2 198.51.100.1/24
0064 
0065         ip -4 route add default vrf v$h2 nexthop via 198.51.100.2
0066 
0067         tc qdisc add dev $h2 clsact
0068 }
0069 
0070 h2_destroy()
0071 {
0072         tc qdisc del dev $h2 clsact
0073 
0074         ip -4 route del default vrf v$h2 nexthop via 198.51.100.2
0075 
0076         simple_if_fini $h2 198.51.100.1/24
0077 }
0078 
0079 h3_create()
0080 {
0081         simple_if_init $h3 203.0.113.1/24
0082 
0083         ip -4 route add default vrf v$h3 nexthop via 203.0.113.2
0084 
0085         tc qdisc add dev $h3 clsact
0086 }
0087 
0088 h3_destroy()
0089 {
0090         tc qdisc del dev $h3 clsact
0091 
0092         ip -4 route del default vrf v$h3 nexthop via 203.0.113.2
0093 
0094         simple_if_fini $h3 203.0.113.1/24
0095 }
0096 
0097 router_create()
0098 {
0099         ip link set dev $rp1 up
0100         ip link set dev $rp2 up
0101         ip link set dev $rp3 up
0102 
0103         __addr_add_del $rp1 add 192.0.2.2/24
0104         __addr_add_del $rp2 add 198.51.100.2/24
0105         __addr_add_del $rp3 add 203.0.113.2/24
0106 
0107         tc qdisc add dev $rp1 clsact
0108         tc qdisc add dev $rp2 clsact
0109 }
0110 
0111 router_destroy()
0112 {
0113         tc qdisc del dev $rp2 clsact
0114         tc qdisc del dev $rp1 clsact
0115 
0116         __addr_add_del $rp3 del 203.0.113.2/24
0117         __addr_add_del $rp2 del 198.51.100.2/24
0118         __addr_add_del $rp1 del 192.0.2.2/24
0119 
0120         ip link set dev $rp3 down
0121         ip link set dev $rp2 down
0122         ip link set dev $rp1 down
0123 }
0124 
0125 police_common_test()
0126 {
0127         local test_name=$1; shift
0128 
0129         RET=0
0130 
0131         # Rule to measure bandwidth on ingress of $h2
0132         tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
0133                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0134                 action drop
0135 
0136         mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
0137                 -t udp sp=12345,dp=54321 -p 1000 -c 0 -q &
0138 
0139         local t0=$(tc_rule_stats_get $h2 1 ingress .bytes)
0140         sleep 10
0141         local t1=$(tc_rule_stats_get $h2 1 ingress .bytes)
0142 
0143         local er=$((80 * 1000 * 1000))
0144         local nr=$(rate $t0 $t1 10)
0145         local nr_pct=$((100 * (nr - er) / er))
0146         ((-10 <= nr_pct && nr_pct <= 10))
0147         check_err $? "Expected rate $(humanize $er), got $(humanize $nr), which is $nr_pct% off. Required accuracy is +-10%."
0148 
0149         log_test "$test_name"
0150 
0151         { kill %% && wait %%; } 2>/dev/null
0152         tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
0153 }
0154 
0155 police_rx_test()
0156 {
0157         # Rule to police traffic destined to $h2 on ingress of $rp1
0158         tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
0159                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0160                 action police rate 80mbit burst 16k conform-exceed drop/ok
0161 
0162         police_common_test "police on rx"
0163 
0164         tc filter del dev $rp1 ingress protocol ip pref 1 handle 101 flower
0165 }
0166 
0167 police_tx_test()
0168 {
0169         # Rule to police traffic destined to $h2 on egress of $rp2
0170         tc filter add dev $rp2 egress protocol ip pref 1 handle 101 flower \
0171                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0172                 action police rate 80mbit burst 16k conform-exceed drop/ok
0173 
0174         police_common_test "police on tx"
0175 
0176         tc filter del dev $rp2 egress protocol ip pref 1 handle 101 flower
0177 }
0178 
0179 police_shared_common_test()
0180 {
0181         local dport=$1; shift
0182         local test_name=$1; shift
0183 
0184         RET=0
0185 
0186         mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
0187                 -t udp sp=12345,dp=$dport -p 1000 -c 0 -q &
0188 
0189         local t0=$(tc_rule_stats_get $h2 1 ingress .bytes)
0190         sleep 10
0191         local t1=$(tc_rule_stats_get $h2 1 ingress .bytes)
0192 
0193         local er=$((80 * 1000 * 1000))
0194         local nr=$(rate $t0 $t1 10)
0195         local nr_pct=$((100 * (nr - er) / er))
0196         ((-10 <= nr_pct && nr_pct <= 10))
0197         check_err $? "Expected rate $(humanize $er), got $(humanize $nr), which is $nr_pct% off. Required accuracy is +-10%."
0198 
0199         log_test "$test_name"
0200 
0201         { kill %% && wait %%; } 2>/dev/null
0202 }
0203 
0204 police_shared_test()
0205 {
0206         # Rule to measure bandwidth on ingress of $h2
0207         tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
0208                 dst_ip 198.51.100.1 ip_proto udp src_port 12345 \
0209                 action drop
0210 
0211         # Rule to police traffic destined to $h2 on ingress of $rp1
0212         tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
0213                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0214                 action police rate 80mbit burst 16k conform-exceed drop/ok \
0215                 index 10
0216 
0217         # Rule to police a different flow destined to $h2 on egress of $rp2
0218         # using same policer
0219         tc filter add dev $rp2 egress protocol ip pref 1 handle 101 flower \
0220                 dst_ip 198.51.100.1 ip_proto udp dst_port 22222 \
0221                 action police index 10
0222 
0223         police_shared_common_test 54321 "police with shared policer - rx"
0224 
0225         police_shared_common_test 22222 "police with shared policer - tx"
0226 
0227         tc filter del dev $rp2 egress protocol ip pref 1 handle 101 flower
0228         tc filter del dev $rp1 ingress protocol ip pref 1 handle 101 flower
0229         tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
0230 }
0231 
0232 police_mirror_common_test()
0233 {
0234         local pol_if=$1; shift
0235         local dir=$1; shift
0236         local test_name=$1; shift
0237 
0238         RET=0
0239 
0240         # Rule to measure bandwidth on ingress of $h2
0241         tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
0242                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0243                 action drop
0244 
0245         # Rule to measure bandwidth of mirrored traffic on ingress of $h3
0246         tc filter add dev $h3 ingress protocol ip pref 1 handle 101 flower \
0247                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0248                 action drop
0249 
0250         # Rule to police traffic destined to $h2 and mirror to $h3
0251         tc filter add dev $pol_if $dir protocol ip pref 1 handle 101 flower \
0252                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0253                 action police rate 80mbit burst 16k conform-exceed drop/pipe \
0254                 action mirred egress mirror dev $rp3
0255 
0256         mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
0257                 -t udp sp=12345,dp=54321 -p 1000 -c 0 -q &
0258 
0259         local t0=$(tc_rule_stats_get $h2 1 ingress .bytes)
0260         sleep 10
0261         local t1=$(tc_rule_stats_get $h2 1 ingress .bytes)
0262 
0263         local er=$((80 * 1000 * 1000))
0264         local nr=$(rate $t0 $t1 10)
0265         local nr_pct=$((100 * (nr - er) / er))
0266         ((-10 <= nr_pct && nr_pct <= 10))
0267         check_err $? "Expected rate $(humanize $er), got $(humanize $nr), which is $nr_pct% off. Required accuracy is +-10%."
0268 
0269         local t0=$(tc_rule_stats_get $h3 1 ingress .bytes)
0270         sleep 10
0271         local t1=$(tc_rule_stats_get $h3 1 ingress .bytes)
0272 
0273         local er=$((80 * 1000 * 1000))
0274         local nr=$(rate $t0 $t1 10)
0275         local nr_pct=$((100 * (nr - er) / er))
0276         ((-10 <= nr_pct && nr_pct <= 10))
0277         check_err $? "Expected rate $(humanize $er), got $(humanize $nr), which is $nr_pct% off. Required accuracy is +-10%."
0278 
0279         log_test "$test_name"
0280 
0281         { kill %% && wait %%; } 2>/dev/null
0282         tc filter del dev $pol_if $dir protocol ip pref 1 handle 101 flower
0283         tc filter del dev $h3 ingress protocol ip pref 1 handle 101 flower
0284         tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
0285 }
0286 
0287 police_rx_mirror_test()
0288 {
0289         police_mirror_common_test $rp1 ingress "police rx and mirror"
0290 }
0291 
0292 police_tx_mirror_test()
0293 {
0294         police_mirror_common_test $rp2 egress "police tx and mirror"
0295 }
0296 
0297 police_pps_common_test()
0298 {
0299         local test_name=$1; shift
0300 
0301         RET=0
0302 
0303         # Rule to measure bandwidth on ingress of $h2
0304         tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
0305                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0306                 action drop
0307 
0308         mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
0309                 -t udp sp=12345,dp=54321 -p 1000 -c 0 -q &
0310 
0311         local t0=$(tc_rule_stats_get $h2 1 ingress .packets)
0312         sleep 10
0313         local t1=$(tc_rule_stats_get $h2 1 ingress .packets)
0314 
0315         local er=$((2000))
0316         local nr=$(packets_rate $t0 $t1 10)
0317         local nr_pct=$((100 * (nr - er) / er))
0318         ((-10 <= nr_pct && nr_pct <= 10))
0319         check_err $? "Expected rate $(humanize $er), got $(humanize $nr), which is $nr_pct% off. Required accuracy is +-10%."
0320 
0321         log_test "$test_name"
0322 
0323         { kill %% && wait %%; } 2>/dev/null
0324         tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
0325 }
0326 
0327 police_pps_rx_test()
0328 {
0329         # Rule to police traffic destined to $h2 on ingress of $rp1
0330         tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
0331                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0332                 action police pkts_rate 2000 pkts_burst 400 conform-exceed drop/ok
0333 
0334         police_pps_common_test "police pps on rx"
0335 
0336         tc filter del dev $rp1 ingress protocol ip pref 1 handle 101 flower
0337 }
0338 
0339 police_pps_tx_test()
0340 {
0341         # Rule to police traffic destined to $h2 on egress of $rp2
0342         tc filter add dev $rp2 egress protocol ip pref 1 handle 101 flower \
0343                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0344                 action police pkts_rate 2000 pkts_burst 400 conform-exceed drop/ok
0345 
0346         police_pps_common_test "police pps on tx"
0347 
0348         tc filter del dev $rp2 egress protocol ip pref 1 handle 101 flower
0349 }
0350 
0351 police_mtu_common_test() {
0352         RET=0
0353 
0354         local test_name=$1; shift
0355         local dev=$1; shift
0356         local direction=$1; shift
0357 
0358         tc filter add dev $dev $direction protocol ip pref 1 handle 101 flower \
0359                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0360                 action police mtu 1042 conform-exceed drop/ok
0361 
0362         # to count "conform" packets
0363         tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
0364                 dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
0365                 action drop
0366 
0367         mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
0368                 -t udp sp=12345,dp=54321 -p 1001 -c 10 -q
0369 
0370         mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
0371                 -t udp sp=12345,dp=54321 -p 1000 -c 3 -q
0372 
0373         tc_check_packets "dev $dev $direction" 101 13
0374         check_err $? "wrong packet counter"
0375 
0376         # "exceed" packets
0377         local overlimits_t0=$(tc_rule_stats_get ${dev} 1 ${direction} .overlimits)
0378         test ${overlimits_t0} = 10
0379         check_err $? "wrong overlimits, expected 10 got ${overlimits_t0}"
0380 
0381         # "conform" packets
0382         tc_check_packets "dev $h2 ingress" 101 3
0383         check_err $? "forwarding error"
0384 
0385         tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
0386         tc filter del dev $dev $direction protocol ip pref 1 handle 101 flower
0387 
0388         log_test "$test_name"
0389 }
0390 
0391 police_mtu_rx_test()
0392 {
0393         police_mtu_common_test "police mtu (rx)" $rp1 ingress
0394 }
0395 
0396 police_mtu_tx_test()
0397 {
0398         police_mtu_common_test "police mtu (tx)" $rp2 egress
0399 }
0400 
0401 setup_prepare()
0402 {
0403         h1=${NETIFS[p1]}
0404         rp1=${NETIFS[p2]}
0405 
0406         rp2=${NETIFS[p3]}
0407         h2=${NETIFS[p4]}
0408 
0409         rp3=${NETIFS[p5]}
0410         h3=${NETIFS[p6]}
0411 
0412         vrf_prepare
0413         forwarding_enable
0414 
0415         h1_create
0416         h2_create
0417         h3_create
0418         router_create
0419 }
0420 
0421 cleanup()
0422 {
0423         pre_cleanup
0424 
0425         router_destroy
0426         h3_destroy
0427         h2_destroy
0428         h1_destroy
0429 
0430         forwarding_restore
0431         vrf_cleanup
0432 }
0433 
0434 trap cleanup EXIT
0435 
0436 setup_prepare
0437 setup_wait
0438 
0439 tests_run
0440 
0441 exit $EXIT_STATUS