2016-02-19 21:11:08 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
rootdir=$(readlink -f $(dirname $0))/..
|
|
|
|
|
2017-06-20 15:39:13 +00:00
|
|
|
function linux_iter_pci_class_code {
|
2016-02-19 21:11:08 +00:00
|
|
|
# Argument is the class code
|
2017-08-16 16:05:25 +00:00
|
|
|
lspci -mm -n -D | awk -v cc="\"$1\"" -F " " '{if (cc ~ $2) print $1}' | tr -d '"'
|
2017-06-20 15:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function linux_iter_pci_dev_id {
|
|
|
|
# Argument 1 is the vendor id
|
|
|
|
# Argument 2 is the device id
|
2017-08-16 16:05:25 +00:00
|
|
|
lspci -mm -n -D | awk -v ven="\"$1\"" -v dev="\"$2\"" -F " " '{if (ven ~ $3 && dev ~ $4) print $1}' | tr -d '"'
|
2016-02-19 21:11:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 19:21:32 +00:00
|
|
|
function linux_bind_driver() {
|
|
|
|
bdf="$1"
|
|
|
|
driver_name="$2"
|
|
|
|
old_driver_name="no driver"
|
|
|
|
ven_dev_id=$(lspci -n -s $bdf | cut -d' ' -f3 | sed 's/:/ /')
|
|
|
|
|
|
|
|
if [ -e "/sys/bus/pci/devices/$bdf/driver" ]; then
|
|
|
|
old_driver_name=$(basename $(readlink /sys/bus/pci/devices/$bdf/driver))
|
|
|
|
|
|
|
|
if [ "$driver_name" = "$old_driver_name" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$ven_dev_id" > "/sys/bus/pci/devices/$bdf/driver/remove_id" 2> /dev/null || true
|
|
|
|
echo "$bdf" > "/sys/bus/pci/devices/$bdf/driver/unbind"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$bdf ($ven_dev_id): $old_driver_name -> $driver_name"
|
|
|
|
|
|
|
|
echo "$ven_dev_id" > "/sys/bus/pci/drivers/$driver_name/new_id" 2> /dev/null || true
|
|
|
|
echo "$bdf" > "/sys/bus/pci/drivers/$driver_name/bind" 2> /dev/null || true
|
2016-04-06 03:03:28 +00:00
|
|
|
|
|
|
|
iommu_group=$(basename $(readlink -f /sys/bus/pci/devices/$bdf/iommu_group))
|
|
|
|
if [ -e "/dev/vfio/$iommu_group" ]; then
|
2016-11-29 21:26:42 +00:00
|
|
|
if [ "$username" != "" ]; then
|
|
|
|
chown "$username" "/dev/vfio/$iommu_group"
|
|
|
|
fi
|
2016-04-06 03:03:28 +00:00
|
|
|
fi
|
2016-04-14 19:21:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 15:32:39 +00:00
|
|
|
function linux_unbind_driver() {
|
|
|
|
bdf="$1"
|
|
|
|
ven_dev_id=$(lspci -n -s $bdf | cut -d' ' -f3 | sed 's/:/ /')
|
|
|
|
|
|
|
|
if ! [ -e "/sys/bus/pci/devices/$bdf/driver" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
old_driver_name=$(basename $(readlink /sys/bus/pci/devices/$bdf/driver))
|
|
|
|
|
|
|
|
echo "$ven_dev_id" > "/sys/bus/pci/devices/$bdf/driver/remove_id" 2> /dev/null || true
|
|
|
|
echo "$bdf" > "/sys/bus/pci/devices/$bdf/driver/unbind"
|
|
|
|
echo "$bdf ($ven_dev_id): $old_driver_name -> no driver"
|
|
|
|
}
|
|
|
|
|
2017-03-28 17:09:31 +00:00
|
|
|
function linux_hugetlbfs_mount() {
|
2017-07-05 23:39:28 +00:00
|
|
|
mount | grep ' type hugetlbfs ' | awk '{ print $3 }'
|
2017-03-28 17:09:31 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 17:19:43 +00:00
|
|
|
function get_nvme_name_from_bdf {
|
|
|
|
set +e
|
|
|
|
nvme_devs=`lsblk -d --output NAME | grep "^nvme"`
|
|
|
|
set -e
|
|
|
|
for dev in $nvme_devs; do
|
|
|
|
bdf=$(basename $(readlink /sys/block/$dev/device/device))
|
|
|
|
if [ "$bdf" = "$1" ]; then
|
|
|
|
eval "$2=$dev"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:05:47 +00:00
|
|
|
function configure_linux_pci {
|
2016-02-19 21:11:08 +00:00
|
|
|
driver_name=vfio-pci
|
|
|
|
if [ -z "$(ls /sys/kernel/iommu_groups)" ]; then
|
|
|
|
# No IOMMU. Use uio.
|
|
|
|
driver_name=uio_pci_generic
|
|
|
|
fi
|
|
|
|
|
|
|
|
# NVMe
|
|
|
|
modprobe $driver_name || true
|
2017-06-20 15:39:13 +00:00
|
|
|
for bdf in $(linux_iter_pci_class_code 0108); do
|
2017-10-13 17:19:43 +00:00
|
|
|
blkname=''
|
|
|
|
get_nvme_name_from_bdf "$bdf" blkname
|
|
|
|
if [ "$blkname" != "" ]; then
|
|
|
|
mountpoints=$(lsblk /dev/$blkname --output MOUNTPOINT -n | wc -w)
|
|
|
|
else
|
|
|
|
mountpoints="0"
|
|
|
|
fi
|
|
|
|
if [ "$mountpoints" = "0" ]; then
|
|
|
|
linux_bind_driver "$bdf" "$driver_name"
|
|
|
|
else
|
|
|
|
echo Active mountpoints on /dev/$blkname, so not binding PCI dev $bdf
|
|
|
|
fi
|
2016-02-19 21:11:08 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# IOAT
|
|
|
|
TMP=`mktemp`
|
|
|
|
#collect all the device_id info of ioat devices.
|
2016-08-08 22:57:49 +00:00
|
|
|
grep "PCI_DEVICE_ID_INTEL_IOAT" $rootdir/include/spdk/pci_ids.h \
|
2016-02-19 21:11:08 +00:00
|
|
|
| awk -F"x" '{print $2}' > $TMP
|
|
|
|
|
|
|
|
for dev_id in `cat $TMP`; do
|
2017-06-20 15:39:13 +00:00
|
|
|
for bdf in $(linux_iter_pci_dev_id 8086 $dev_id); do
|
2016-04-14 19:21:32 +00:00
|
|
|
linux_bind_driver "$bdf" "$driver_name"
|
2016-02-19 21:11:08 +00:00
|
|
|
done
|
|
|
|
done
|
|
|
|
rm $TMP
|
|
|
|
|
2017-05-30 21:13:50 +00:00
|
|
|
# virtio-scsi
|
|
|
|
TMP=`mktemp`
|
|
|
|
#collect all the device_id info of virtio-scsi devices.
|
2017-10-18 09:51:07 +00:00
|
|
|
grep "PCI_DEVICE_ID_VIRTIO_SCSI" $rootdir/include/spdk/pci_ids.h \
|
2017-05-30 21:13:50 +00:00
|
|
|
| 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
|
|
|
|
linux_bind_driver "$bdf" "$driver_name"
|
|
|
|
done
|
|
|
|
done
|
|
|
|
rm $TMP
|
|
|
|
|
2016-02-19 21:11:08 +00:00
|
|
|
echo "1" > "/sys/bus/pci/rescan"
|
2017-11-14 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function configure_linux {
|
|
|
|
configure_linux_pci
|
2016-04-14 20:22:11 +00:00
|
|
|
|
2017-03-28 17:09:31 +00:00
|
|
|
hugetlbfs_mount=$(linux_hugetlbfs_mount)
|
|
|
|
|
|
|
|
if [ -z "$hugetlbfs_mount" ]; then
|
|
|
|
hugetlbfs_mount=/mnt/huge
|
|
|
|
echo "Mounting hugetlbfs at $hugetlbfs_mount"
|
|
|
|
mkdir -p "$hugetlbfs_mount"
|
|
|
|
mount -t hugetlbfs nodev "$hugetlbfs_mount"
|
2016-04-14 20:22:11 +00:00
|
|
|
fi
|
2017-10-27 17:26:58 +00:00
|
|
|
|
|
|
|
if [ -z "$HUGENODE" ]; then
|
|
|
|
hugepages_target="/proc/sys/vm/nr_hugepages"
|
|
|
|
else
|
|
|
|
hugepages_target="/sys/devices/system/node/node${HUGENODE}/hugepages/hugepages-${HUGEPGSZ}kB/nr_hugepages"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$NRHUGE" > "$hugepages_target"
|
2017-10-27 17:56:36 +00:00
|
|
|
allocated_hugepages=`cat $hugepages_target`
|
|
|
|
if [ "$allocated_hugepages" -lt "$NRHUGE" ]; then
|
|
|
|
echo ""
|
|
|
|
echo "## ERROR: requested $NRHUGE hugepages but only $allocated_hugepages could be allocated."
|
|
|
|
echo "## Memory might be heavily fragmented. Please try flushing the system cache, or reboot the machine."
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-04-06 03:03:28 +00:00
|
|
|
|
|
|
|
if [ "$driver_name" = "vfio-pci" ]; then
|
2016-11-29 21:26:42 +00:00
|
|
|
if [ "$username" != "" ]; then
|
2017-03-28 17:09:31 +00:00
|
|
|
chown "$username" "$hugetlbfs_mount"
|
2017-05-18 18:25:38 +00:00
|
|
|
chmod g+w "$hugetlbfs_mount"
|
2016-11-29 21:26:42 +00:00
|
|
|
fi
|
2016-04-06 03:03:28 +00:00
|
|
|
|
|
|
|
MEMLOCK_AMNT=`ulimit -l`
|
|
|
|
if [ "$MEMLOCK_AMNT" != "unlimited" ] ; then
|
2016-11-08 14:21:15 +00:00
|
|
|
MEMLOCK_MB=$(( $MEMLOCK_AMNT / 1024 ))
|
2016-04-06 03:03:28 +00:00
|
|
|
echo ""
|
|
|
|
echo "Current user memlock limit: ${MEMLOCK_MB} MB"
|
|
|
|
echo ""
|
|
|
|
echo "This is the maximum amount of memory you will be"
|
|
|
|
echo "able to use with DPDK and VFIO if run as current user."
|
|
|
|
echo -n "To change this, please adjust limits.conf memlock "
|
|
|
|
echo "limit for current user."
|
|
|
|
|
|
|
|
if [ $MEMLOCK_AMNT -lt 65536 ] ; then
|
|
|
|
echo ""
|
|
|
|
echo "## WARNING: memlock limit is less than 64MB"
|
|
|
|
echo -n "## DPDK with VFIO may not be able to initialize "
|
|
|
|
echo "if run as current user."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2016-02-19 21:11:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:05:47 +00:00
|
|
|
function reset_linux_pci {
|
2016-02-19 21:11:08 +00:00
|
|
|
# NVMe
|
2017-10-13 15:32:39 +00:00
|
|
|
set +e
|
|
|
|
lsmod | grep nvme > /dev/null
|
|
|
|
driver_loaded=$?
|
|
|
|
set -e
|
2017-06-20 15:39:13 +00:00
|
|
|
for bdf in $(linux_iter_pci_class_code 0108); do
|
2017-10-13 15:32:39 +00:00
|
|
|
if [ $driver_loaded -eq 0 ]; then
|
|
|
|
linux_bind_driver "$bdf" nvme
|
|
|
|
else
|
|
|
|
linux_unbind_driver "$bdf"
|
|
|
|
fi
|
2016-02-19 21:11:08 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# IOAT
|
|
|
|
TMP=`mktemp`
|
|
|
|
#collect all the device_id info of ioat devices.
|
2016-08-08 22:57:49 +00:00
|
|
|
grep "PCI_DEVICE_ID_INTEL_IOAT" $rootdir/include/spdk/pci_ids.h \
|
2016-02-19 21:11:08 +00:00
|
|
|
| awk -F"x" '{print $2}' > $TMP
|
|
|
|
|
2017-10-13 15:32:39 +00:00
|
|
|
set +e
|
|
|
|
lsmod | grep ioatdma > /dev/null
|
|
|
|
driver_loaded=$?
|
|
|
|
set -e
|
2016-02-19 21:11:08 +00:00
|
|
|
for dev_id in `cat $TMP`; do
|
2017-06-20 15:39:13 +00:00
|
|
|
for bdf in $(linux_iter_pci_dev_id 8086 $dev_id); do
|
2017-10-13 15:32:39 +00:00
|
|
|
if [ $driver_loaded -eq 0 ]; then
|
|
|
|
linux_bind_driver "$bdf" ioatdma
|
|
|
|
else
|
|
|
|
linux_unbind_driver "$bdf"
|
|
|
|
fi
|
2016-02-19 21:11:08 +00:00
|
|
|
done
|
|
|
|
done
|
|
|
|
rm $TMP
|
|
|
|
|
2017-05-30 21:13:50 +00:00
|
|
|
# virtio-scsi
|
|
|
|
TMP=`mktemp`
|
|
|
|
#collect all the device_id info of virtio-scsi devices.
|
2017-10-18 09:51:07 +00:00
|
|
|
grep "PCI_DEVICE_ID_VIRTIO_SCSI" $rootdir/include/spdk/pci_ids.h \
|
2017-05-30 21:13:50 +00:00
|
|
|
| awk -F"x" '{print $2}' > $TMP
|
|
|
|
|
2017-10-13 15:32:39 +00:00
|
|
|
# TODO: check if virtio-pci is loaded first and just unbind if it is not loaded
|
|
|
|
# Requires some more investigation - for example, some kernels do not seem to have
|
|
|
|
# virtio-pci but just virtio_scsi instead. Also need to make sure we get the
|
|
|
|
# underscore vs. dash right in the virtio_scsi name.
|
2017-05-30 21:13:50 +00:00
|
|
|
modprobe virtio-pci || true
|
|
|
|
for dev_id in `cat $TMP`; do
|
|
|
|
for bdf in $(linux_iter_pci_dev_id 1af4 $dev_id); do
|
|
|
|
linux_bind_driver "$bdf" virtio-pci
|
|
|
|
done
|
|
|
|
done
|
|
|
|
rm $TMP
|
|
|
|
|
2016-02-19 21:11:08 +00:00
|
|
|
echo "1" > "/sys/bus/pci/rescan"
|
2017-11-14 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function reset_linux {
|
|
|
|
reset_linux_pci
|
2017-03-28 17:11:35 +00:00
|
|
|
|
|
|
|
hugetlbfs_mount=$(linux_hugetlbfs_mount)
|
2017-03-28 21:16:10 +00:00
|
|
|
rm -f "$hugetlbfs_mount"/spdk*map_*
|
2017-09-25 17:55:07 +00:00
|
|
|
rm -f /run/.spdk*
|
2016-02-19 21:11:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 13:38:44 +00:00
|
|
|
function status_linux {
|
|
|
|
echo "NVMe devices"
|
|
|
|
|
|
|
|
echo -e "BDF\t\tNuma Node\tDriver name\t\tDevice name"
|
2017-06-20 15:39:13 +00:00
|
|
|
for bdf in $(linux_iter_pci_class_code 0108); do
|
2017-01-30 13:38:44 +00:00
|
|
|
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
|
|
|
|
name="\t"`ls /sys/bus/pci/devices/$bdf/nvme`;
|
|
|
|
else
|
|
|
|
name="-";
|
|
|
|
fi
|
|
|
|
echo -e "$bdf\t$node\t\t$driver\t\t$name";
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "I/OAT DMA"
|
|
|
|
|
|
|
|
#collect all the device_id info of ioat devices.
|
|
|
|
TMP=`grep "PCI_DEVICE_ID_INTEL_IOAT" $rootdir/include/spdk/pci_ids.h \
|
|
|
|
| awk -F"x" '{print $2}'`
|
|
|
|
echo -e "BDF\t\tNuma Node\tDriver Name"
|
|
|
|
for dev_id in $TMP; do
|
2017-06-20 15:39:13 +00:00
|
|
|
for bdf in $(linux_iter_pci_dev_id 8086 $dev_id); do
|
2017-01-30 13:38:44 +00:00
|
|
|
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"
|
|
|
|
done
|
|
|
|
done
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
echo "virtio"
|
|
|
|
|
|
|
|
#collect all the device_id info of virtio-scsi devices.
|
2017-10-18 09:51:07 +00:00
|
|
|
TMP=`grep "PCI_DEVICE_ID_VIRTIO_SCSI" $rootdir/include/spdk/pci_ids.h \
|
2017-05-30 21:13:50 +00:00
|
|
|
| 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
|
|
|
|
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"
|
|
|
|
done
|
|
|
|
done
|
2017-01-30 13:38:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:05:47 +00:00
|
|
|
function configure_freebsd_pci {
|
2016-02-19 21:11:08 +00:00
|
|
|
TMP=`mktemp`
|
2016-08-08 22:57:49 +00:00
|
|
|
|
|
|
|
# NVMe
|
|
|
|
GREP_STR="class=0x010802"
|
|
|
|
|
|
|
|
# IOAT
|
|
|
|
grep "PCI_DEVICE_ID_INTEL_IOAT" $rootdir/include/spdk/pci_ids.h \
|
|
|
|
| awk -F"x" '{print $2}' > $TMP
|
|
|
|
for dev_id in `cat $TMP`; do
|
|
|
|
GREP_STR="${GREP_STR}\|chip=0x${dev_id}8086"
|
|
|
|
done
|
|
|
|
|
2016-02-19 21:11:08 +00:00
|
|
|
AWK_PROG="{if (count > 0) printf \",\"; printf \"%s:%s:%s\",\$2,\$3,\$4; count++}"
|
|
|
|
echo $AWK_PROG > $TMP
|
2016-08-08 22:57:49 +00:00
|
|
|
|
|
|
|
BDFS=`pciconf -l | grep "${GREP_STR}" | awk -F: -f $TMP`
|
|
|
|
|
2016-02-19 21:11:08 +00:00
|
|
|
kldunload nic_uio.ko || true
|
|
|
|
kenv hw.nic_uio.bdfs=$BDFS
|
|
|
|
kldload nic_uio.ko
|
|
|
|
rm $TMP
|
2017-11-14 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function configure_freebsd {
|
|
|
|
configure_freebsd_pci
|
2016-04-14 20:22:11 +00:00
|
|
|
|
|
|
|
kldunload contigmem.ko || true
|
2017-08-30 18:20:22 +00:00
|
|
|
kenv hw.contigmem.num_buffers=$((HUGEMEM / 256))
|
2016-07-21 16:55:32 +00:00
|
|
|
kenv hw.contigmem.buffer_size=$((256 * 1024 * 1024))
|
2016-04-14 20:22:11 +00:00
|
|
|
kldload contigmem.ko
|
2016-02-19 21:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function reset_freebsd {
|
|
|
|
kldunload contigmem.ko || true
|
|
|
|
kldunload nic_uio.ko || true
|
|
|
|
}
|
|
|
|
|
2016-04-06 03:03:28 +00:00
|
|
|
username=$1
|
|
|
|
mode=$2
|
|
|
|
|
2017-01-30 13:38:44 +00:00
|
|
|
if [ "$username" = "reset" -o "$username" = "config" -o "$username" = "status" ]; then
|
2016-04-06 03:03:28 +00:00
|
|
|
mode="$username"
|
|
|
|
username=""
|
|
|
|
fi
|
|
|
|
|
2016-02-19 21:11:08 +00:00
|
|
|
if [ "$mode" == "" ]; then
|
|
|
|
mode="config"
|
|
|
|
fi
|
|
|
|
|
2016-04-06 03:03:28 +00:00
|
|
|
if [ "$username" = "" ]; then
|
2016-11-29 21:26:42 +00:00
|
|
|
username="$SUDO_USER"
|
|
|
|
if [ "$username" = "" ]; then
|
|
|
|
username=`logname 2>/dev/null` || true
|
|
|
|
fi
|
2016-04-06 03:03:28 +00:00
|
|
|
fi
|
|
|
|
|
2017-08-30 18:20:22 +00:00
|
|
|
: ${HUGEMEM:=2048}
|
|
|
|
|
2016-02-19 21:11:08 +00:00
|
|
|
if [ `uname` = Linux ]; then
|
2017-10-27 17:26:58 +00:00
|
|
|
HUGEPGSZ=$(( `grep Hugepagesize /proc/meminfo | cut -d : -f 2 | tr -dc '0-9'` ))
|
|
|
|
HUGEPGSZ_MB=$(( $HUGEPGSZ / 1024 ))
|
|
|
|
: ${NRHUGE=$(( (HUGEMEM + HUGEPGSZ_MB - 1) / HUGEPGSZ_MB ))}
|
2017-08-30 18:20:22 +00:00
|
|
|
|
2016-02-19 21:11:08 +00:00
|
|
|
if [ "$mode" == "config" ]; then
|
|
|
|
configure_linux
|
|
|
|
elif [ "$mode" == "reset" ]; then
|
|
|
|
reset_linux
|
2017-01-30 13:38:44 +00:00
|
|
|
elif [ "$mode" == "status" ]; then
|
|
|
|
status_linux
|
2016-02-19 21:11:08 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [ "$mode" == "config" ]; then
|
|
|
|
configure_freebsd
|
|
|
|
elif [ "$mode" == "reset" ]; then
|
|
|
|
reset_freebsd
|
|
|
|
fi
|
|
|
|
fi
|