@@ -25,15 +25,23 @@ HYPERVISOR=
25
25
MACHINE_TYPE=
26
26
IMAGE_TYPE=
27
27
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
+
28
36
cleanup () {
29
37
clean_env_ctr
30
38
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} "
33
41
}
34
42
35
43
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
37
45
}
38
46
39
47
get_vfio_path () {
@@ -87,7 +95,7 @@ check_guest_kernel() {
87
95
# For vfio_mode=guest-kernel, the device should be bound to
88
96
# the guest kernel's native driver. To check this has worked,
89
97
# 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} "
91
99
}
92
100
93
101
check_vfio () {
@@ -113,8 +121,8 @@ check_vfio() {
113
121
# There should be two devices in the IOMMU group: the ethernet
114
122
# device we care about, plus the PCIe to PCI bridge device
115
123
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} "
118
126
fi
119
127
120
128
# The bridge device will always sort first, because it is on
@@ -188,11 +196,25 @@ setup_configuration_file() {
188
196
if [ -n " $MACHINE_TYPE " ]; then
189
197
if [ " $HYPERVISOR " = " qemu" ]; then
190
198
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} "
191
201
else
192
202
warn " Variable machine_type only applies to qemu. It will be ignored"
193
203
fi
194
204
fi
195
205
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
+
196
218
if [ -n " ${SANDBOX_CGROUP_ONLY} " ]; then
197
219
sed -i ' s|^sandbox_cgroup_only.*|sandbox_cgroup_only=' ${SANDBOX_CGROUP_ONLY} ' |g' " ${kata_config_file} "
198
220
fi
@@ -286,7 +308,7 @@ main() {
286
308
#
287
309
# Get the device ready on the host
288
310
#
289
- setup_configuration_file
311
+ # setup_configuration_file
290
312
291
313
restart_containerd_service
292
314
sudo modprobe vfio
@@ -316,6 +338,37 @@ main() {
316
338
# Run the tests
317
339
#
318
340
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
+
319
372
# test for guest-kernel mode
320
373
guest_kernel_cid=" vfio-guest-kernel-${RANDOM} "
321
374
run_test_container " ${guest_kernel_cid} " \
@@ -336,4 +389,4 @@ main() {
336
389
check_vfio " ${vfio_cid} "
337
390
}
338
391
339
- main $@
392
+ main " $@ "
0 commit comments