Files
linux/tools/testing/selftests/vfio/run.sh
David Matlack fd134b0f2f vfio: selftests: Add a script to help with running VFIO selftests
Introduce run.sh, a script to help with running VFIO selftests. The
script is intended to be used for both humans manually running VFIO
selftests, and to incorporate into test automation where VFIO selftests
may run alongside other tests. As such the script aims to be hermetic,
returning the system to the state it was before the test started.

The script takes as input the BDF of a device to use and a command to
run (typically the command would be a VFIO selftest). e.g.

  $ ./run.sh -d 0000:6a:01.0 ./vfio_pci_device_test

 or

  $ ./run.sh -d 0000:6a:01.0 -- ./vfio_pci_device_test

The script then handles unbinding device 0000:6a:01.0 from its current
driver, binding it to vfio-pci, running the test, unbinding from
vfio-pci, and binding back to the original driver.

When run.sh runs the provided test, it does so by appending the BDF as
the last parameter. For example:

  $ ./run.sh -d 0000:6a:01.0 -- echo hello

Results in the following being printed to stdout:

  hello 0000:6a:01.0

The script also supports a mode where it can break out into a shell so
that multiple tests can be run manually.

  $ ./run.sh -d 0000:6a:01.0 -s
  $ echo $VFIO_SELFTESTS_BDF
  $ ./vfio_pci_device_test
  $ exit

Choosing which device to use is up to the user.

In the future this script should be extensible to tests that want to use
multiple devices. The script can support accepting -d BDF multiple times
and parse them into an array, setup all the devices, pass the list of
BDFs to the test, and then cleanup all the devices.

Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: David Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20250822212518.4156428-31-dmatlack@google.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
2025-08-27 12:14:11 -06:00

110 lines
2.7 KiB
Bash
Executable File

# 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
}
function main() {
local shell
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
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
}
main "$@"