hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-only
 
NSIM_ID=$((RANDOM % 1024))
NSIM_DEV_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_ID
NSIM_DEV_DFS=/sys/kernel/debug/netdevsim/netdevsim$NSIM_ID/ports/0
NSIM_NETDEV=
num_passes=0
num_errors=0
 
function cleanup_nsim {
    if [ -e $NSIM_DEV_SYS ]; then
   echo $NSIM_ID > /sys/bus/netdevsim/del_device
    fi
}
 
function cleanup {
    cleanup_nsim
}
 
trap cleanup EXIT
 
function get_netdev_name {
    local -n old=$1
 
    new=$(ls /sys/class/net)
 
    for netdev in $new; do
   for check in $old; do
            [ $netdev == $check ] && break
   done
 
   if [ $netdev != $check ]; then
       echo $netdev
       break
   fi
    done
}
 
function check {
    local code=$1
    local str=$2
    local exp_str=$3
 
    if [ $code -ne 0 ]; then
   ((num_errors++))
   return
    fi
 
    if [ "$str" != "$exp_str"  ]; then
   echo -e "Expected: '$exp_str', got '$str'"
   ((num_errors++))
   return
    fi
 
    ((num_passes++))
}
 
# Bail if ethtool is too old
if ! ethtool -h | grep include-stat 2>&1 >/dev/null; then
    echo "SKIP: No --include-statistics support in ethtool"
    exit 4
fi
 
# Make a netdevsim
old_netdevs=$(ls /sys/class/net)
 
modprobe netdevsim
echo $NSIM_ID > /sys/bus/netdevsim/new_device
 
NSIM_NETDEV=`get_netdev_name old_netdevs`
 
set -o pipefail
 
echo n > $NSIM_DEV_DFS/ethtool/pause/report_stats_tx
echo n > $NSIM_DEV_DFS/ethtool/pause/report_stats_rx
 
s=$(ethtool --json -a $NSIM_NETDEV | jq '.[].statistics')
check $? "$s" "null"
 
s=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics')
check $? "$s" "{}"
 
echo y > $NSIM_DEV_DFS/ethtool/pause/report_stats_tx
 
s=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics | length')
check $? "$s" "1"
 
s=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics.tx_pause_frames')
check $? "$s" "2"
 
echo y > $NSIM_DEV_DFS/ethtool/pause/report_stats_rx
 
s=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics | length')
check $? "$s" "2"
 
s=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics.rx_pause_frames')
check $? "$s" "1"
s=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics.tx_pause_frames')
check $? "$s" "2"
 
if [ $num_errors -eq 0 ]; then
    echo "PASSED all $((num_passes)) checks"
    exit 0
else
    echo "FAILED $num_errors/$((num_errors+num_passes)) checks"
    exit 1
fi