18c2ecf20Sopenharmony_ci#!/bin/bash
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ciNSIM_ID=$((RANDOM % 1024))
58c2ecf20Sopenharmony_ciNSIM_DEV_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_ID
68c2ecf20Sopenharmony_ciNSIM_DEV_DFS=/sys/kernel/debug/netdevsim/netdevsim$NSIM_ID/ports/0
78c2ecf20Sopenharmony_ciNSIM_NETDEV=
88c2ecf20Sopenharmony_cinum_passes=0
98c2ecf20Sopenharmony_cinum_errors=0
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cifunction cleanup_nsim {
128c2ecf20Sopenharmony_ci    if [ -e $NSIM_DEV_SYS ]; then
138c2ecf20Sopenharmony_ci	echo $NSIM_ID > /sys/bus/netdevsim/del_device
148c2ecf20Sopenharmony_ci    fi
158c2ecf20Sopenharmony_ci}
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cifunction cleanup {
188c2ecf20Sopenharmony_ci    cleanup_nsim
198c2ecf20Sopenharmony_ci}
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_citrap cleanup EXIT
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cifunction get_netdev_name {
248c2ecf20Sopenharmony_ci    local -n old=$1
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci    new=$(ls /sys/class/net)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci    for netdev in $new; do
298c2ecf20Sopenharmony_ci	for check in $old; do
308c2ecf20Sopenharmony_ci            [ $netdev == $check ] && break
318c2ecf20Sopenharmony_ci	done
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	if [ $netdev != $check ]; then
348c2ecf20Sopenharmony_ci	    echo $netdev
358c2ecf20Sopenharmony_ci	    break
368c2ecf20Sopenharmony_ci	fi
378c2ecf20Sopenharmony_ci    done
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cifunction check {
418c2ecf20Sopenharmony_ci    local code=$1
428c2ecf20Sopenharmony_ci    local str=$2
438c2ecf20Sopenharmony_ci    local exp_str=$3
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci    if [ $code -ne 0 ]; then
468c2ecf20Sopenharmony_ci	((num_errors++))
478c2ecf20Sopenharmony_ci	return
488c2ecf20Sopenharmony_ci    fi
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci    if [ "$str" != "$exp_str"  ]; then
518c2ecf20Sopenharmony_ci	echo -e "Expected: '$exp_str', got '$str'"
528c2ecf20Sopenharmony_ci	((num_errors++))
538c2ecf20Sopenharmony_ci	return
548c2ecf20Sopenharmony_ci    fi
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci    ((num_passes++))
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci# Bail if ethtool is too old
608c2ecf20Sopenharmony_ciif ! ethtool -h | grep include-stat 2>&1 >/dev/null; then
618c2ecf20Sopenharmony_ci    echo "SKIP: No --include-statistics support in ethtool"
628c2ecf20Sopenharmony_ci    exit 4
638c2ecf20Sopenharmony_cifi
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci# Make a netdevsim
668c2ecf20Sopenharmony_ciold_netdevs=$(ls /sys/class/net)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cimodprobe netdevsim
698c2ecf20Sopenharmony_ciecho $NSIM_ID > /sys/bus/netdevsim/new_device
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ciNSIM_NETDEV=`get_netdev_name old_netdevs`
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciset -o pipefail
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ciecho n > $NSIM_DEV_DFS/ethtool/pause/report_stats_tx
768c2ecf20Sopenharmony_ciecho n > $NSIM_DEV_DFS/ethtool/pause/report_stats_rx
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cis=$(ethtool --json -a $NSIM_NETDEV | jq '.[].statistics')
798c2ecf20Sopenharmony_cicheck $? "$s" "null"
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cis=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics')
828c2ecf20Sopenharmony_cicheck $? "$s" "{}"
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciecho y > $NSIM_DEV_DFS/ethtool/pause/report_stats_tx
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cis=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics | length')
878c2ecf20Sopenharmony_cicheck $? "$s" "1"
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cis=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics.tx_pause_frames')
908c2ecf20Sopenharmony_cicheck $? "$s" "2"
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ciecho y > $NSIM_DEV_DFS/ethtool/pause/report_stats_rx
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cis=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics | length')
958c2ecf20Sopenharmony_cicheck $? "$s" "2"
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cis=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics.rx_pause_frames')
988c2ecf20Sopenharmony_cicheck $? "$s" "1"
998c2ecf20Sopenharmony_cis=$(ethtool -I --json -a $NSIM_NETDEV | jq '.[].statistics.tx_pause_frames')
1008c2ecf20Sopenharmony_cicheck $? "$s" "2"
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciif [ $num_errors -eq 0 ]; then
1038c2ecf20Sopenharmony_ci    echo "PASSED all $((num_passes)) checks"
1048c2ecf20Sopenharmony_ci    exit 0
1058c2ecf20Sopenharmony_cielse
1068c2ecf20Sopenharmony_ci    echo "FAILED $num_errors/$((num_errors+num_passes)) checks"
1078c2ecf20Sopenharmony_ci    exit 1
1088c2ecf20Sopenharmony_cifi
109