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

Commit b3c780d

Browse files
committed
# SQUASH
1 parent 5102a2d commit b3c780d

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

functional/vfio/run.sh

Lines changed: 60 additions & 9 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
@@ -189,12 +197,24 @@ setup_configuration_file() {
189197
if [ "$HYPERVISOR" = "qemu" ]; then
190198
sed -i 's|^machine_type.*|machine_type = "'${MACHINE_TYPE}'"|g' "${kata_config_file}"
191199
# Make sure we have set hot_plug_vfio to a reasonable value
192-
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}"
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}"
193201
else
194202
warn "Variable machine_type only applies to qemu. It will be ignored"
195203
fi
196204
fi
197205

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+
198218
if [ -n "${SANDBOX_CGROUP_ONLY}" ]; then
199219
sed -i 's|^sandbox_cgroup_only.*|sandbox_cgroup_only='${SANDBOX_CGROUP_ONLY}'|g' "${kata_config_file}"
200220
fi
@@ -288,7 +308,7 @@ main() {
288308
#
289309
# Get the device ready on the host
290310
#
291-
setup_configuration_file
311+
#setup_configuration_file
292312

293313
restart_containerd_service
294314
sudo modprobe vfio
@@ -318,6 +338,37 @@ main() {
318338
# Run the tests
319339
#
320340

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+
321372
# test for guest-kernel mode
322373
guest_kernel_cid="vfio-guest-kernel-${RANDOM}"
323374
run_test_container "${guest_kernel_cid}" \
@@ -338,4 +389,4 @@ main() {
338389
check_vfio "${vfio_cid}"
339390
}
340391

341-
main $@
392+
main "$@"

0 commit comments

Comments
 (0)