Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit 933bbc1

Browse files
committed
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 <[email protected]>
1 parent e45aea4 commit 933bbc1

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

functional/vfio/run.sh

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,23 @@ HYPERVISOR=
2525
MACHINE_TYPE=
2626
IMAGE_TYPE=
2727

28+
# Option to choose an alternative PCI device for the VFIO test
29+
VFIO_PCI_CLASS=${VFIO_PCI_CLASS:-"Ethernet controller"}
30+
VFIO_PCI_NAME=${VFIO_PCI_NAME:-"Virtio.*network device"}
31+
VFIO_CHECK_GUEST_KERNEL=${VFIO_CHECK_GUEST_KERNEL:-"ip a | grep \"eth\" || die \"Missing VFIO network interface\""}
32+
VFIO_HOTPLUG=${VFIO_HOTPLUG:-"bridge-port"}
33+
VFIO_COLDPLUG=${VFIO_COLDPLUG:-"bridge-port"}
34+
VFIO_CHECK_NUM_DEVICES=${VFIO_CHECK_NUM_DEVICES:-"2"}
35+
2836
cleanup() {
2937
clean_env_ctr
3038
sudo rm -rf "${tmp_data_dir}"
31-
32-
[ -n "${host_pci}" ] && sudo driverctl unset-override "${host_pci}"
39+
# some devices fail if no previous driver being bound
40+
[ -n "${host_pci}" ] && sudo driverctl --noprobe unset-override "${host_pci}"
3341
}
3442

3543
host_pci_addr() {
36-
lspci -D | grep "Ethernet controller" | grep "Virtio.*network device" | tail -1 | cut -d' ' -f1
44+
lspci -D | grep "${VFIO_PCI_CLASS}" | grep "${VFIO_PCI_NAME}" | tail -1 | cut -d' ' -f1
3745
}
3846

3947
get_vfio_path() {
@@ -87,7 +95,7 @@ check_guest_kernel() {
8795
# For vfio_mode=guest-kernel, the device should be bound to
8896
# the guest kernel's native driver. To check this has worked,
8997
# we look for an ethernet device named 'eth*'
90-
get_ctr_cmd_output "${container_id}" ip a | grep "eth" || die "Missing VFIO network interface"
98+
get_ctr_cmd_output "${container_id}" ash -c "${VFIO_CHECK_GUEST_KERNEL}"
9199
}
92100

93101
check_vfio() {
@@ -113,8 +121,8 @@ check_vfio() {
113121
# There should be two devices in the IOMMU group: the ethernet
114122
# device we care about, plus the PCIe to PCI bridge device
115123
devs="$(get_ctr_cmd_output "${cid}" ls /sys/kernel/iommu_groups/"${group}"/devices)"
116-
if [ $(echo "${devs}" | wc -w) != "2" ] ; then
117-
die "Expected exactly two devices got: ${devs}"
124+
if [ $(echo "${devs}" | wc -w) != ${VFIO_CHECK_NUM_DEVICES} ] ; then
125+
die "Expected exactly ${VFIO_CHECK_NUM_DEVICES} device(s) got: ${devs}"
118126
fi
119127

120128
# The bridge device will always sort first, because it is on
@@ -188,11 +196,25 @@ setup_configuration_file() {
188196
if [ -n "$MACHINE_TYPE" ]; then
189197
if [ "$HYPERVISOR" = "qemu" ]; then
190198
sed -i 's|^machine_type.*|machine_type = "'${MACHINE_TYPE}'"|g' "${kata_config_file}"
199+
# Make sure we have set hot_plug_vfio to a reasonable value
200+
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}"
191201
else
192202
warn "Variable machine_type only applies to qemu. It will be ignored"
193203
fi
194204
fi
195205

206+
if [ "${VFIO_HOTPLUG}" != "bridge-port" ]; then
207+
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}"
208+
cat "${kata_config_file}" | grep -v '#' | grep -v '^$'
209+
fi
210+
211+
if [ "${VFIO_COLDPLUG}" != "bridge-port" ]; then
212+
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}"
213+
cat "${kata_config_file}" | grep -v '#' | grep -v '^$'
214+
fi
215+
216+
217+
196218
if [ -n "${SANDBOX_CGROUP_ONLY}" ]; then
197219
sed -i 's|^sandbox_cgroup_only.*|sandbox_cgroup_only='${SANDBOX_CGROUP_ONLY}'|g' "${kata_config_file}"
198220
fi
@@ -286,7 +308,7 @@ main() {
286308
#
287309
# Get the device ready on the host
288310
#
289-
setup_configuration_file
311+
#setup_configuration_file
290312

291313
restart_containerd_service
292314
sudo modprobe vfio
@@ -316,6 +338,37 @@ main() {
316338
# Run the tests
317339
#
318340

341+
# First test hot_plug_vfio="bridge-port"
342+
export VFIO_HOTPLUG="bridge-port"
343+
344+
setup_configuration_file
345+
346+
# test for guest-kernel mode
347+
guest_kernel_cid="vfio-guest-kernel-${RANDOM}"
348+
run_test_container "${guest_kernel_cid}" \
349+
"${tmp_data_dir}/vfio-guest-kernel" \
350+
"${script_path}/guest-kernel.json.in" \
351+
"${host_pci}"
352+
check_guest_kernel "${guest_kernel_cid}"
353+
354+
# Remove the container so we can re-use the device for the next test
355+
clean_env_ctr
356+
357+
# test for vfio mode
358+
vfio_cid="vfio-vfio-${RANDOM}"
359+
run_test_container "${vfio_cid}" \
360+
"${tmp_data_dir}/vfio-vfio" \
361+
"${script_path}/vfio.json.in" \
362+
"${host_pci}"
363+
check_vfio "${vfio_cid}"
364+
365+
366+
367+
# Now test cold_plug_vfio="bridge-port"
368+
export VFIO_COLDPLUG="bridge-port"
369+
370+
setup_configuration_file
371+
319372
# test for guest-kernel mode
320373
guest_kernel_cid="vfio-guest-kernel-${RANDOM}"
321374
run_test_container "${guest_kernel_cid}" \
@@ -336,4 +389,4 @@ main() {
336389
check_vfio "${vfio_cid}"
337390
}
338391

339-
main $@
392+
main "$@"

0 commit comments

Comments
 (0)