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