From 7349ec5fbe5077533805e793c1632d0bb6776d56 Mon Sep 17 00:00:00 2001 From: Oleg Strizhechenko Date: Wed, 17 Oct 2018 20:33:35 +0500 Subject: [PATCH] Fixed(netdev-pci-speed): removed hardcode, fixed dualport-NIC's support, added basic error handling + shell strict mode --- setup.py | 2 +- utils/netdev-pci-speed | 71 ++++++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/setup.py b/setup.py index 73ad2dd..578435c 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ def read(*paths): setuptools.setup( name='netutils-linux', - version='2.7.6', + version='2.7.7', author='Oleg Strizhechenko', author_email='oleg.strizhechenko@gmail.com', license='MIT', diff --git a/utils/netdev-pci-speed b/utils/netdev-pci-speed index 8446e00..0b880bb 100755 --- a/utils/netdev-pci-speed +++ b/utils/netdev-pci-speed @@ -1,33 +1,44 @@ #!/bin/bash -device=$1 -bus_addr="$(ethtool -i $device | grep bus | awk '{print $2}')" -dev_speed="$(ethtool eth1 | grep Speed | egrep -o [0-9]+)" -pci_type="$(dmidecode --type slot | grep -B6 0000:af:00.0 | egrep -m1 -o 'PCI Express [0-9] x[0-9]+')" -read _ _ version x <<< "$pci_type" -if [ "$version" = '1' ]; then - [ "$x" = 'x1' ] && slot_speed=250 - [ "$x" = 'x2' ] && slot_speed=500 - [ "$x" = 'x4' ] && slot_speed=1000 - [ "$x" = 'x8' ] && slot_speed=2000 - [ "$x" = 'x16' ] && slot_speed=4000 -elif [ "$version" = '2' ]; then - [ "$x" = 'x1' ] && slot_speed=500 - [ "$x" = 'x2' ] && slot_speed=1000 - [ "$x" = 'x4' ] && slot_speed=2000 - [ "$x" = 'x8' ] && slot_speed=4000 - [ "$x" = 'x16' ] && slot_speed=8000 -elif [ "$version" = '3' ]; then - [ "$x" = 'x1' ] && slot_speed=984 - [ "$x" = 'x2' ] && slot_speed=1970 - [ "$x" = 'x4' ] && slot_speed=3940 - [ "$x" = 'x8' ] && slot_speed=7880 - [ "$x" = 'x16' ] && slot_speed=15800 -elif [ "$version" = '4' ]; then - [ "$x" = 'x1' ] && slot_speed=1969 - [ "$x" = 'x2' ] && slot_speed=3940 - [ "$x" = 'x4' ] && slot_speed=7880 - [ "$x" = 'x8' ] && slot_speed=15750 - [ "$x" = 'x16' ] && slot_speed=31500 +set -eu + +DEVICE="$1" +if [ ! -d /sys/class/net/$DEVICE ]; then + echo "No such device $DEVICE" + exit 2 +fi +BUS_ADDR="$(ethtool -i "$DEVICE" | grep bus | awk '{print $2}')" +DEV_SPEED="$(ethtool "$DEVICE" | grep Speed | egrep -o [0-9]+)" +if ! PCI_TYPE="$(dmidecode --type slot | grep -B6 "${BUS_ADDR%.*}" | egrep -m1 -o 'PCI Express [0-9] x[0-9]+')"; then + echo "$DEVICE is probably built-in" + exit 1 +fi +read _ _ VERSION X <<< "$PCI_TYPE" + +if [ "$VERSION" = '1' ]; then + [ "$X" = 'x1' ] && slot_speed=250 + [ "$X" = 'x2' ] && slot_speed=500 + [ "$X" = 'x4' ] && slot_speed=1000 + [ "$X" = 'x8' ] && slot_speed=2000 + [ "$X" = 'x16' ] && slot_speed=4000 +elif [ "$VERSION" = '2' ]; then + [ "$X" = 'x1' ] && slot_speed=500 + [ "$X" = 'x2' ] && slot_speed=1000 + [ "$X" = 'x4' ] && slot_speed=2000 + [ "$X" = 'x8' ] && slot_speed=4000 + [ "$X" = 'x16' ] && slot_speed=8000 +elif [ "$VERSION" = '3' ]; then + [ "$X" = 'x1' ] && slot_speed=984 + [ "$X" = 'x2' ] && slot_speed=1970 + [ "$X" = 'x4' ] && slot_speed=3940 + [ "$X" = 'x8' ] && slot_speed=7880 + [ "$X" = 'x16' ] && slot_speed=15800 +elif [ "$VERSION" = '4' ]; then + [ "$X" = 'x1' ] && slot_speed=1969 + [ "$X" = 'x2' ] && slot_speed=3940 + [ "$X" = 'x4' ] && slot_speed=7880 + [ "$X" = 'x8' ] && slot_speed=15750 + [ "$X" = 'x16' ] && slot_speed=31500 fi -echo $device $dev_speed/$((slot_speed*8)) +echo "$DEVICE $DEV_SPEED/$((slot_speed*8))" +exit 0