selftests: lib: Add a generic helper for obtaining HW stats
authorPetr Machata <petrm@nvidia.com>
Sun, 8 May 2022 08:08:22 +0000 (11:08 +0300)
committerDavid S. Miller <davem@davemloft.net>
Sun, 8 May 2022 10:46:20 +0000 (11:46 +0100)
The function get_l3_stats() from the test hw_stats_l3.sh will be useful for
any test that wishes to work with L3 stats. Furthermore, it is easy to
generalize to other HW stats suites (for when such are added). Therefore,
move the code to lib.sh, rewrite it to have the same interface as the other
stats-collecting functions, and generalize to take the name of the HW stats
suite to collect as an argument.

Signed-off-by: Petr Machata <petrm@nvidia.com>
Signed-off-by: Ido Schimmel <idosch@nvidia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
tools/testing/selftests/net/forwarding/hw_stats_l3.sh
tools/testing/selftests/net/forwarding/lib.sh

index 1c11c42..9c1f76e 100755 (executable)
@@ -162,14 +162,6 @@ ping_ipv6()
        ping_test $h1.200 2001:db8:2::1 " IPv6"
 }
 
-get_l3_stat()
-{
-       local selector=$1; shift
-
-       ip -j stats show dev $rp1.200 group offload subgroup l3_stats |
-                 jq '.[0].stats64.'$selector
-}
-
 send_packets_rx_ipv4()
 {
        # Send 21 packets instead of 20, because the first one might trap and go
@@ -208,11 +200,11 @@ ___test_stats()
        local a
        local b
 
-       a=$(get_l3_stat ${dir}.packets)
+       a=$(hw_stats_get l3_stats $rp1.200 ${dir} packets)
        send_packets_${dir}_${prot}
        "$@"
        b=$(busywait "$TC_HIT_TIMEOUT" until_counter_is ">= $a + 20" \
-                      get_l3_stat ${dir}.packets)
+                      hw_stats_get l3_stats $rp1.200 ${dir} packets)
        check_err $? "Traffic not reflected in the counter: $a -> $b"
 }
 
@@ -281,11 +273,11 @@ __test_stats_report()
 
        RET=0
 
-       a=$(get_l3_stat ${dir}.packets)
+       a=$(hw_stats_get l3_stats $rp1.200 ${dir} packets)
        send_packets_${dir}_${prot}
        ip address flush dev $rp1.200
        b=$(busywait "$TC_HIT_TIMEOUT" until_counter_is ">= $a + 20" \
-                      get_l3_stat ${dir}.packets)
+                      hw_stats_get l3_stats $rp1.200 ${dir} packets)
        check_err $? "Traffic not reflected in the counter: $a -> $b"
        log_test "Test ${dir} packets: stats pushed on loss of L3"
 
index 66681a2..37ae49d 100755 (executable)
@@ -828,6 +828,17 @@ ipv6_stats_get()
        cat /proc/net/dev_snmp6/$dev | grep "^$stat" | cut -f2
 }
 
+hw_stats_get()
+{
+       local suite=$1; shift
+       local if_name=$1; shift
+       local dir=$1; shift
+       local stat=$1; shift
+
+       ip -j stats show dev $if_name group offload subgroup $suite |
+               jq ".[0].stats64.$dir.$stat"
+}
+
 humanize()
 {
        local speed=$1; shift