diff --git a/scripts/common.sh b/scripts/common.sh new file mode 100644 index 000000000..d85fdb8b9 --- /dev/null +++ b/scripts/common.sh @@ -0,0 +1,44 @@ +# Common shell utility functions + +function iter_pci_class_code() { + local class="$(printf %02x $((0x$1)))" + local subclass="$(printf %02x $((0x$2)))" + local progif="$(printf %02x $((0x$3)))" + + if hash lspci &>/dev/null; then + if [ "$progif" != "00" ]; then + lspci -mm -n -D | \ + grep -i -- "-p${progif}" | \ + awk -v cc="\"${class}${subclass}\"" -F " " \ + '{if (cc ~ $2) print $1}' | tr -d '"' + else + lspci -mm -n -D | \ + awk -v cc="\"${class}${subclass}\"" -F " " \ + '{if (cc ~ $2) print $1}' | tr -d '"' + fi + elif hash pciconf &>/dev/null; then + addr=($(pciconf -l | grep -i "class=0x${class}${subclass}${progif}" | \ + cut -d$'\t' -f1 | sed -e 's/^[a-zA-Z0-9_]*@pci//g' | tr ':' ' ')) + printf "%04x:%02x:%02x:%x\n" ${addr[0]} ${addr[1]} ${addr[2]} ${addr[3]} + else + echo "Missing PCI enumeration utility" + exit 1 + fi +} + +function iter_pci_dev_id() { + local ven_id="$(printf %04x $((0x$1)))" + local dev_id="$(printf %04x $((0x$2)))" + + if hash lspci &>/dev/null; then + lspci -mm -n -D | awk -v ven="\"$ven_id\"" -v dev="\"${dev_id}\"" -F " " \ + '{if (ven ~ $3 && dev ~ $4) print $1}' | tr -d '"' + elif hash pciconf &>/dev/null; then + addr=($(pciconf -l | grep -i "chip=0x${dev_id}${ven_id}" | \ + cut -d$'\t' -f1 | sed -e 's/^[a-zA-Z0-9_]*@pci//g' | tr ':' ' ')) + printf "%04x:%02x:%02x:%x\n" ${addr[0]} ${addr[1]} ${addr[2]} ${addr[3]} + else + echo "Missing PCI enumeration utility" + exit 1 + fi +} diff --git a/scripts/gen_nvme.sh b/scripts/gen_nvme.sh index 94cdabada..ed450f68d 100755 --- a/scripts/gen_nvme.sh +++ b/scripts/gen_nvme.sh @@ -2,17 +2,10 @@ set -e -case `uname` in - FreeBSD) - bdfs=$(pciconf -l | grep "class=0x010802" | awk -F: ' {printf "0000:%02X:%02X.%X\n", $2, $3, $4}') - ;; - Linux) - bdfs=$(lspci -mm -n | grep 0108 | tr -d '"' | awk -F " " '{print "0000:"$1}') - ;; - *) - exit 1 - ;; -esac +rootdir=$(readlink -f $(dirname $0))/.. +source "$rootdir/scripts/common.sh" + +bdfs=$(iter_pci_class_code 01 08 02) echo "[Nvme]" i=0 diff --git a/scripts/setup.sh b/scripts/setup.sh index 0c58f8dd8..6ba75088e 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -3,17 +3,7 @@ set -e rootdir=$(readlink -f $(dirname $0))/.. - -function linux_iter_pci_class_code { - # Argument is the class code - lspci -mm -n -D | awk -v cc="\"$1\"" -F " " '{if (cc ~ $2) print $1}' | tr -d '"' -} - -function linux_iter_pci_dev_id { - # Argument 1 is the vendor id - # Argument 2 is the device id - lspci -mm -n -D | awk -v ven="\"$1\"" -v dev="\"$2\"" -F " " '{if (ven ~ $3 && dev ~ $4) print $1}' | tr -d '"' -} +source "$rootdir/scripts/common.sh" function linux_bind_driver() { bdf="$1" @@ -90,7 +80,7 @@ function configure_linux_pci { # NVMe modprobe $driver_name || true - for bdf in $(linux_iter_pci_class_code 0108); do + for bdf in $(iter_pci_class_code 01 08 02); do blkname='' get_nvme_name_from_bdf "$bdf" blkname if [ "$blkname" != "" ]; then @@ -112,7 +102,7 @@ function configure_linux_pci { | awk -F"x" '{print $2}' > $TMP for dev_id in `cat $TMP`; do - for bdf in $(linux_iter_pci_dev_id 8086 $dev_id); do + for bdf in $(iter_pci_dev_id 8086 $dev_id); do linux_bind_driver "$bdf" "$driver_name" done done @@ -125,7 +115,7 @@ function configure_linux_pci { | awk -F"x" '{print $2}' > $TMP for dev_id in `cat $TMP`; do - for bdf in $(linux_iter_pci_dev_id 1af4 $dev_id); do + for bdf in $(iter_pci_dev_id 1af4 $dev_id); do linux_bind_driver "$bdf" "$driver_name" done done @@ -196,7 +186,7 @@ function reset_linux_pci { lsmod | grep nvme > /dev/null driver_loaded=$? set -e - for bdf in $(linux_iter_pci_class_code 0108); do + for bdf in $(iter_pci_class_code 01 08 02); do if [ $driver_loaded -eq 0 ]; then linux_bind_driver "$bdf" nvme else @@ -215,7 +205,7 @@ function reset_linux_pci { driver_loaded=$? set -e for dev_id in `cat $TMP`; do - for bdf in $(linux_iter_pci_dev_id 8086 $dev_id); do + for bdf in $(iter_pci_dev_id 8086 $dev_id); do if [ $driver_loaded -eq 0 ]; then linux_bind_driver "$bdf" ioatdma else @@ -237,7 +227,7 @@ function reset_linux_pci { # underscore vs. dash right in the virtio_scsi name. modprobe virtio-pci || true for dev_id in `cat $TMP`; do - for bdf in $(linux_iter_pci_dev_id 1af4 $dev_id); do + for bdf in $(iter_pci_dev_id 1af4 $dev_id); do linux_bind_driver "$bdf" virtio-pci done done @@ -260,7 +250,7 @@ function status_linux { echo "NVMe devices" echo -e "BDF\t\tNuma Node\tDriver name\t\tDevice name" - for bdf in $(linux_iter_pci_class_code 0108); do + for bdf in $(iter_pci_class_code 01 08 02); do driver=`grep DRIVER /sys/bus/pci/devices/$bdf/uevent |awk -F"=" '{print $2}'` node=`cat /sys/bus/pci/devices/$bdf/numa_node`; if [ "$driver" = "nvme" ]; then @@ -278,7 +268,7 @@ function status_linux { | awk -F"x" '{print $2}'` echo -e "BDF\t\tNuma Node\tDriver Name" for dev_id in $TMP; do - for bdf in $(linux_iter_pci_dev_id 8086 $dev_id); do + for bdf in $(iter_pci_dev_id 8086 $dev_id); do driver=`grep DRIVER /sys/bus/pci/devices/$bdf/uevent |awk -F"=" '{print $2}'` node=`cat /sys/bus/pci/devices/$bdf/numa_node`; echo -e "$bdf\t$node\t\t$driver" @@ -292,7 +282,7 @@ function status_linux { | awk -F"x" '{print $2}'` echo -e "BDF\t\tNuma Node\tDriver Name" for dev_id in $TMP; do - for bdf in $(linux_iter_pci_dev_id 1af4 $dev_id); do + for bdf in $(iter_pci_dev_id 1af4 $dev_id); do driver=`grep DRIVER /sys/bus/pci/devices/$bdf/uevent |awk -F"=" '{print $2}'` node=`cat /sys/bus/pci/devices/$bdf/numa_node`; echo -e "$bdf\t$node\t\t$driver" diff --git a/test/lib/nvme/nvme.sh b/test/lib/nvme/nvme.sh index 6a9e16028..3c81dbb94 100755 --- a/test/lib/nvme/nvme.sh +++ b/test/lib/nvme/nvme.sh @@ -4,12 +4,9 @@ set -e testdir=$(readlink -f $(dirname $0)) rootdir=$(readlink -f $testdir/../../..) +source $rootdir/scripts/common.sh source $rootdir/scripts/autotest_common.sh -function linux_iter_pci { - lspci -mm -n -D | grep $1 | tr -d '"' | awk -F " " '{print $1}' -} - function get_nvme_name_from_bdf { lsblk -d --output NAME if ! [ $(lsblk -d --output NAME | grep "^nvme") ]; then @@ -44,7 +41,7 @@ if [ `uname` = Linux ]; then # # note: more work probably needs to be done to properly handle devices with multiple # namespaces - for bdf in $(linux_iter_pci 0108); do + for bdf in $(iter_pci_class_code 01 08 02); do get_nvme_name_from_bdf "$bdf" blkname if [ "$blkname" != "" ]; then mountpoints=$(lsblk /dev/$blkname --output MOUNTPOINT -n | wc -w) @@ -107,7 +104,7 @@ fi timing_enter identify $rootdir/examples/nvme/identify/identify -i 0 -for bdf in $(linux_iter_pci 0108); do +for bdf in $(iter_pci_class_code 01 08 02); do $rootdir/examples/nvme/identify/identify -r "trtype:PCIe traddr:${bdf}" -i 0 done timing_exit identify @@ -168,7 +165,7 @@ PLUGIN_DIR=$rootdir/examples/nvme/fio_plugin if [ -d /usr/src/fio ]; then timing_enter fio_plugin - for bdf in $(linux_iter_pci 0108); do + for bdf in $(iter_pci_class_code 01 08 02); do # Only test when ASAN is not enabled. If ASAN is enabled, we cannot test. if [ $SPDK_RUN_ASAN -eq 0 ]; then LD_PRELOAD=$PLUGIN_DIR/fio_plugin /usr/src/fio/fio $PLUGIN_DIR/example_config.fio --filename="trtype=PCIe traddr=${bdf//:/.} ns=1" diff --git a/test/vhost/lvol/lvol_test.sh b/test/vhost/lvol/lvol_test.sh index e9bc480e2..06b7ca863 100755 --- a/test/vhost/lvol/lvol_test.sh +++ b/test/vhost/lvol/lvol_test.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash set -e + +rootdir=$(readlink -f $(dirname $0))/../../.. +source "$rootdir/scripts/common.sh" + BASE_DIR=$(readlink -f $(dirname $0)) [[ -z "$TEST_DIR" ]] && TEST_DIR="$(cd $BASE_DIR/../../../../ && pwd)" [[ -z "$COMMON_DIR" ]] && COMMON_DIR="$(cd $BASE_DIR/../common && pwd)" @@ -91,7 +95,7 @@ while getopts 'xh-:' optchar; do done echo "INFO: Get NVMe disks:" -nvmes=($(lspci -mm -n | grep 0108 | tr -d '"' | awk -F " " '{print "0000:"$1}')) +nvmes=($(iter_pci_class_code 01 08 02)) if [[ -z $max_disks ]]; then max_disks=${#nvmes[@]}