vfio: selftests: Split run.sh into separate scripts

Split run.sh into separate scripts (setup.sh, run.sh, cleanup.sh) to
enable multi-device testing, and prepare for VFIO selftests
automatically detecting which devices to use for testing by storing
device metadata on the filesystem.

 - setup.sh takes one or more BDFs as arguments and sets up each device.
   Metadata about each device is stored on the filesystem in the
   directory:

	   ${TMPDIR:-/tmp}/vfio-selftests-devices

   Within this directory is a directory for each BDF, and then files in
   those directories that cleanup.sh uses to cleanup the device.

 - run.sh runs a selftest by passing it the BDFs of all set up devices.

 - cleanup.sh takes zero or more BDFs as arguments and cleans up each
   device. If no BDFs are provided, it cleans up all devices.

This split enables multi-device testing by allowing multiple BDFs to be
set up and passed into tests:

For example:

  $ tools/testing/selftests/vfio/scripts/setup.sh <BDF1> <BDF2>
  $ tools/testing/selftests/vfio/scripts/setup.sh <BDF3>
  $ tools/testing/selftests/vfio/scripts/run.sh echo
  <BDF1> <BDF2> <BDF3>
  $ tools/testing/selftests/vfio/scripts/cleanup.sh

In the future, VFIO selftests can automatically detect set up devices by
inspecting ${TMPDIR:-/tmp}/vfio-selftests-devices. This will avoid the
need for the run.sh script.

Reviewed-by: Alex Mastro <amastro@fb.com>
Tested-by: Alex Mastro <amastro@fb.com>
Reviewed-by: Raghavendra Rao Ananta <rananta@google.com>
Signed-off-by: David Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20251126231733.3302983-3-dmatlack@google.com
Signed-off-by: Alex Williamson <alex@shazbot.org>
This commit is contained in:
David Matlack 2025-11-26 23:17:17 +00:00 committed by Alex Williamson
parent 2d5dbd3156
commit fa246a1d06
5 changed files with 140 additions and 99 deletions

View File

@ -4,7 +4,10 @@ TEST_GEN_PROGS += vfio_iommufd_setup_test
TEST_GEN_PROGS += vfio_pci_device_test
TEST_GEN_PROGS += vfio_pci_driver_test
TEST_FILES += scripts/cleanup.sh
TEST_FILES += scripts/lib.sh
TEST_FILES += scripts/run.sh
TEST_FILES += scripts/setup.sh
include ../lib.mk
include lib/libvfio.mk

View File

@ -0,0 +1,41 @@
# SPDX-License-Identifier: GPL-2.0-or-later
source $(dirname -- "${BASH_SOURCE[0]}")/lib.sh
function cleanup_devices() {
local device_bdf
local device_dir
for device_bdf in "$@"; do
device_dir=${DEVICES_DIR}/${device_bdf}
if [ -f ${device_dir}/vfio-pci ]; then
unbind ${device_bdf} vfio-pci
fi
if [ -f ${device_dir}/driver_override ]; then
clear_driver_override ${device_bdf}
fi
if [ -f ${device_dir}/driver ]; then
bind ${device_bdf} $(cat ${device_dir}/driver)
fi
if [ -f ${device_dir}/sriov_numvfs ]; then
set_sriov_numvfs ${device_bdf} $(cat ${device_dir}/sriov_numvfs)
fi
rm -rf ${device_dir}
done
}
function main() {
if [ $# = 0 ]; then
cleanup_devices $(ls ${DEVICES_DIR})
rmdir ${DEVICES_DIR}
else
cleanup_devices "$@"
fi
}
main "$@"

View File

@ -0,0 +1,42 @@
# SPDX-License-Identifier: GPL-2.0-or-later
readonly DEVICES_DIR="${TMPDIR:-/tmp}/vfio-selftests-devices"
function write_to() {
# Unfortunately set -x does not show redirects so use echo to manually
# tell the user what commands are being run.
echo "+ echo \"${2}\" > ${1}"
echo "${2}" > ${1}
}
function get_driver() {
if [ -L /sys/bus/pci/devices/${1}/driver ]; then
basename $(readlink -m /sys/bus/pci/devices/${1}/driver)
fi
}
function bind() {
write_to /sys/bus/pci/drivers/${2}/bind ${1}
}
function unbind() {
write_to /sys/bus/pci/drivers/${2}/unbind ${1}
}
function set_sriov_numvfs() {
write_to /sys/bus/pci/devices/${1}/sriov_numvfs ${2}
}
function get_sriov_numvfs() {
if [ -f /sys/bus/pci/devices/${1}/sriov_numvfs ]; then
cat /sys/bus/pci/devices/${1}/sriov_numvfs
fi
}
function set_driver_override() {
write_to /sys/bus/pci/devices/${1}/driver_override ${2}
}
function clear_driver_override() {
set_driver_override ${1} ""
}

View File

@ -1,109 +1,16 @@
# SPDX-License-Identifier: GPL-2.0-or-later
# Global variables initialized in main() and then used during cleanup() when
# the script exits.
declare DEVICE_BDF
declare NEW_DRIVER
declare OLD_DRIVER
declare OLD_NUMVFS
declare DRIVER_OVERRIDE
function write_to() {
# Unfortunately set -x does not show redirects so use echo to manually
# tell the user what commands are being run.
echo "+ echo \"${2}\" > ${1}"
echo "${2}" > ${1}
}
function bind() {
write_to /sys/bus/pci/drivers/${2}/bind ${1}
}
function unbind() {
write_to /sys/bus/pci/drivers/${2}/unbind ${1}
}
function set_sriov_numvfs() {
write_to /sys/bus/pci/devices/${1}/sriov_numvfs ${2}
}
function set_driver_override() {
write_to /sys/bus/pci/devices/${1}/driver_override ${2}
}
function clear_driver_override() {
set_driver_override ${1} ""
}
function cleanup() {
if [ "${NEW_DRIVER}" ]; then unbind ${DEVICE_BDF} ${NEW_DRIVER} ; fi
if [ "${DRIVER_OVERRIDE}" ]; then clear_driver_override ${DEVICE_BDF} ; fi
if [ "${OLD_DRIVER}" ]; then bind ${DEVICE_BDF} ${OLD_DRIVER} ; fi
if [ "${OLD_NUMVFS}" ]; then set_sriov_numvfs ${DEVICE_BDF} ${OLD_NUMVFS} ; fi
}
function usage() {
echo "usage: $0 [-d segment:bus:device.function] [-s] [-h] [cmd ...]" >&2
echo >&2
echo " -d: The BDF of the device to use for the test (required)" >&2
echo " -h: Show this help message" >&2
echo " -s: Drop into a shell rather than running a command" >&2
echo >&2
echo " cmd: The command to run and arguments to pass to it." >&2
echo " Required when not using -s. The SBDF will be " >&2
echo " appended to the argument list." >&2
exit 1
}
source $(dirname -- "${BASH_SOURCE[0]}")/lib.sh
function main() {
local shell
local device_bdfs=$(ls ${DEVICES_DIR})
while getopts "d:hs" opt; do
case $opt in
d) DEVICE_BDF="$OPTARG" ;;
s) shell=true ;;
*) usage ;;
esac
done
# Shift past all optional arguments.
shift $((OPTIND - 1))
# Check that the user passed in the command to run.
[ ! "${shell}" ] && [ $# = 0 ] && usage
# Check that the user passed in a BDF.
[ "${DEVICE_BDF}" ] || usage
trap cleanup EXIT
set -e
test -d /sys/bus/pci/devices/${DEVICE_BDF}
if [ -f /sys/bus/pci/devices/${DEVICE_BDF}/sriov_numvfs ]; then
OLD_NUMVFS=$(cat /sys/bus/pci/devices/${DEVICE_BDF}/sriov_numvfs)
set_sriov_numvfs ${DEVICE_BDF} 0
if [ -z "${device_bdfs}" ]; then
echo "No devices found, skipping."
exit 4
fi
if [ -L /sys/bus/pci/devices/${DEVICE_BDF}/driver ]; then
OLD_DRIVER=$(basename $(readlink -m /sys/bus/pci/devices/${DEVICE_BDF}/driver))
unbind ${DEVICE_BDF} ${OLD_DRIVER}
fi
set_driver_override ${DEVICE_BDF} vfio-pci
DRIVER_OVERRIDE=true
bind ${DEVICE_BDF} vfio-pci
NEW_DRIVER=vfio-pci
echo
if [ "${shell}" ]; then
echo "Dropping into ${SHELL} with VFIO_SELFTESTS_BDF=${DEVICE_BDF}"
VFIO_SELFTESTS_BDF=${DEVICE_BDF} ${SHELL}
else
"$@" ${DEVICE_BDF}
fi
echo
"$@" ${device_bdfs}
}
main "$@"

View File

@ -0,0 +1,48 @@
# SPDX-License-Identifier: GPL-2.0-or-later
set -e
source $(dirname -- "${BASH_SOURCE[0]}")/lib.sh
function main() {
local device_bdf
local device_dir
local numvfs
local driver
if [ $# = 0 ]; then
echo "usage: $0 segment:bus:device.function ..." >&2
exit 1
fi
for device_bdf in "$@"; do
test -d /sys/bus/pci/devices/${device_bdf}
device_dir=${DEVICES_DIR}/${device_bdf}
if [ -d "${device_dir}" ]; then
echo "${device_bdf} has already been set up, exiting."
exit 0
fi
mkdir -p ${device_dir}
numvfs=$(get_sriov_numvfs ${device_bdf})
if [ "${numvfs}" ]; then
set_sriov_numvfs ${device_bdf} 0
echo ${numvfs} > ${device_dir}/sriov_numvfs
fi
driver=$(get_driver ${device_bdf})
if [ "${driver}" ]; then
unbind ${device_bdf} ${driver}
echo ${driver} > ${device_dir}/driver
fi
set_driver_override ${device_bdf} vfio-pci
touch ${device_dir}/driver_override
bind ${device_bdf} vfio-pci
touch ${device_dir}/vfio-pci
done
}
main "$@"