mirror of https://gitee.com/openkylin/linux.git
selftests: extract common functions in ethtool-common.sh
Factor out some useful functions so that they can be reused by other ethtool-netdevsim scripts. Signed-off-by: Antonio Cardace <acardace@redhat.com> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
parent
a7fc6db099
commit
fbb8531e58
|
@ -0,0 +1,69 @@
|
|||
#!/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++))
|
||||
}
|
||||
|
||||
function make_netdev {
|
||||
# Make a netdevsim
|
||||
old_netdevs=$(ls /sys/class/net)
|
||||
|
||||
if ! $(lsmod | grep -q netdevsim); then
|
||||
modprobe netdevsim
|
||||
fi
|
||||
|
||||
echo $NSIM_ID > /sys/bus/netdevsim/new_device
|
||||
echo `get_netdev_name old_netdevs`
|
||||
}
|
|
@ -1,60 +1,7 @@
|
|||
#!/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++))
|
||||
}
|
||||
source ethtool-common.sh
|
||||
|
||||
# Bail if ethtool is too old
|
||||
if ! ethtool -h | grep include-stat 2>&1 >/dev/null; then
|
||||
|
@ -62,13 +9,7 @@ if ! ethtool -h | grep include-stat 2>&1 >/dev/null; then
|
|||
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`
|
||||
NSIM_NETDEV=$(make_netdev)
|
||||
|
||||
set -o pipefail
|
||||
|
||||
|
|
Loading…
Reference in New Issue