From 73871b147aa83d68885bafaeaf095d951fa863cf Mon Sep 17 00:00:00 2001 From: Zvonko Kaiser Date: Fri, 21 Jul 2023 08:45:18 +0000 Subject: [PATCH] vfio: Fix default port assignment Fix default port assignment with an explicit value in the TOML It was implicit before and now the runtime will bail out if we did not set it explicitly. Update the configuration and set it. This will make the intent more clear. Fixes: #5726 Signed-off-by: Zvonko Kaiser --- functional/vfio/run.sh | 71 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/functional/vfio/run.sh b/functional/vfio/run.sh index ed4111e6c..090d44367 100755 --- a/functional/vfio/run.sh +++ b/functional/vfio/run.sh @@ -25,15 +25,23 @@ HYPERVISOR= MACHINE_TYPE= IMAGE_TYPE= +# Option to choose an alternative PCI device for the VFIO test +VFIO_PCI_CLASS=${VFIO_PCI_CLASS:-"Ethernet controller"} +VFIO_PCI_NAME=${VFIO_PCI_NAME:-"Virtio.*network device"} +VFIO_CHECK_GUEST_KERNEL=${VFIO_CHECK_GUEST_KERNEL:-"ip a | grep \"eth\" || die \"Missing VFIO network interface\""} +VFIO_HOTPLUG=${VFIO_HOTPLUG:-"bridge-port"} +VFIO_COLDPLUG=${VFIO_COLDPLUG:-"bridge-port"} +VFIO_CHECK_NUM_DEVICES=${VFIO_CHECK_NUM_DEVICES:-"2"} + cleanup() { clean_env_ctr sudo rm -rf "${tmp_data_dir}" - - [ -n "${host_pci}" ] && sudo driverctl unset-override "${host_pci}" + # some devices fail if no previous driver being bound + [ -n "${host_pci}" ] && sudo driverctl --noprobe unset-override "${host_pci}" } host_pci_addr() { - lspci -D | grep "Ethernet controller" | grep "Virtio.*network device" | tail -1 | cut -d' ' -f1 + lspci -D | grep "${VFIO_PCI_CLASS}" | grep "${VFIO_PCI_NAME}" | tail -1 | cut -d' ' -f1 } get_vfio_path() { @@ -87,7 +95,7 @@ check_guest_kernel() { # For vfio_mode=guest-kernel, the device should be bound to # the guest kernel's native driver. To check this has worked, # we look for an ethernet device named 'eth*' - get_ctr_cmd_output "${container_id}" ip a | grep "eth" || die "Missing VFIO network interface" + get_ctr_cmd_output "${container_id}" ash -c "${VFIO_CHECK_GUEST_KERNEL}" } check_vfio() { @@ -113,8 +121,8 @@ check_vfio() { # There should be two devices in the IOMMU group: the ethernet # device we care about, plus the PCIe to PCI bridge device devs="$(get_ctr_cmd_output "${cid}" ls /sys/kernel/iommu_groups/"${group}"/devices)" - if [ $(echo "${devs}" | wc -w) != "2" ] ; then - die "Expected exactly two devices got: ${devs}" + if [ $(echo "${devs}" | wc -w) != ${VFIO_CHECK_NUM_DEVICES} ] ; then + die "Expected exactly ${VFIO_CHECK_NUM_DEVICES} device(s) got: ${devs}" fi # The bridge device will always sort first, because it is on @@ -189,12 +197,28 @@ setup_configuration_file() { if [ "$HYPERVISOR" = "qemu" ]; then sed -i 's|^machine_type.*|machine_type = "'${MACHINE_TYPE}'"|g' "${kata_config_file}" # Make sure we have set hot_plug_vfio to a reasonable value +<<<<<<< HEAD sudo sed -i -e 's|^#hot_plug_vfio =.*$|hot_plug_vfio = "bridge-port"|' -e 's|^hot_plug_vfio = .*$|hot_plug_vfio = "bridge-port"|' "${kata_config_file}" +======= + sed -i -e 's|^#hot_plug_vfio =.*$|hot_plug_vfio = "bridge-port"|' -e 's|^hot_plug_vfio =.*$|hot_plug_vfio = "bridge-port"|' "${kata_config_file}" +>>>>>>> 933bbc1f (vfio: Fix default port assignment) else warn "Variable machine_type only applies to qemu. It will be ignored" fi fi + if [ "${VFIO_HOTPLUG}" != "bridge-port" ]; then + sed -i -e "s|^#hot_plug_vfio =.*$|hot_plug_vfio = \"${VFIO_HOTPLUG}\"|" -e "s|^hot_plug_vfio =.*$|hot_plug_vfio = \"${VFIO_HOTPLUG}\"|" "${kata_config_file}" + cat "${kata_config_file}" | grep -v '#' | grep -v '^$' + fi + + if [ "${VFIO_COLDPLUG}" != "bridge-port" ]; then + sed -i -e "s|^#cold_plug_vfio =.*$|cold_plug_vfio = \"${VFIO_HOTPLUG}\"|" -e "s|^cold_plug_vfio =.*$|cold_plug_vfio = \"${VFIO_HOTPLUG}\"|" "${kata_config_file}" + cat "${kata_config_file}" | grep -v '#' | grep -v '^$' + fi + + + if [ -n "${SANDBOX_CGROUP_ONLY}" ]; then sed -i 's|^sandbox_cgroup_only.*|sandbox_cgroup_only='${SANDBOX_CGROUP_ONLY}'|g' "${kata_config_file}" fi @@ -288,7 +312,7 @@ main() { # # Get the device ready on the host # - setup_configuration_file + #setup_configuration_file restart_containerd_service sudo modprobe vfio @@ -318,6 +342,37 @@ main() { # Run the tests # + # First test hot_plug_vfio="bridge-port" + export VFIO_HOTPLUG="bridge-port" + + setup_configuration_file + + # test for guest-kernel mode + guest_kernel_cid="vfio-guest-kernel-${RANDOM}" + run_test_container "${guest_kernel_cid}" \ + "${tmp_data_dir}/vfio-guest-kernel" \ + "${script_path}/guest-kernel.json.in" \ + "${host_pci}" + check_guest_kernel "${guest_kernel_cid}" + + # Remove the container so we can re-use the device for the next test + clean_env_ctr + + # test for vfio mode + vfio_cid="vfio-vfio-${RANDOM}" + run_test_container "${vfio_cid}" \ + "${tmp_data_dir}/vfio-vfio" \ + "${script_path}/vfio.json.in" \ + "${host_pci}" + check_vfio "${vfio_cid}" + + + + # Now test cold_plug_vfio="bridge-port" + export VFIO_COLDPLUG="bridge-port" + + setup_configuration_file + # test for guest-kernel mode guest_kernel_cid="vfio-guest-kernel-${RANDOM}" run_test_container "${guest_kernel_cid}" \ @@ -338,4 +393,4 @@ main() { check_vfio "${vfio_cid}" } -main $@ +main "$@"