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

Commit 68b3475

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 5102a2d commit 68b3475

File tree

1 file changed

+87
-16
lines changed

1 file changed

+87
-16
lines changed

functional/vfio/run.sh

+87-16
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,33 @@ 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_PORT=${VFIO_PORT:-"bridge-port"}
33+
VFIO_CHECK_NUM_DEVICES=${VFIO_CHECK_NUM_DEVICES:-"2"}
34+
2835
cleanup() {
2936
clean_env_ctr
3037
sudo rm -rf "${tmp_data_dir}"
31-
32-
[ -n "${host_pci}" ] && sudo driverctl unset-override "${host_pci}"
38+
# some devices fail if no previous driver being bound
39+
[ -n "${host_pci}" ] && sudo driverctl --noprobe unset-override "${host_pci}"
3340
}
3441

3542
host_pci_addr() {
36-
lspci -D | grep "Ethernet controller" | grep "Virtio.*network device" | tail -1 | cut -d' ' -f1
43+
lspci -D | grep "${VFIO_PCI_CLASS}" | grep "${VFIO_PCI_NAME}" | tail -1 | cut -d' ' -f1
3744
}
3845

3946
get_vfio_path() {
4047
local addr="$1"
41-
echo "/dev/vfio/$(basename $(realpath /sys/bus/pci/drivers/vfio-pci/${host_pci}/iommu_group))"
48+
local iommu_group_path
49+
local iommu_group
50+
51+
iommu_group_path=$(realpath /sys/bus/pci/drivers/vfio-pci/"${addr}"/iommu_group)
52+
iommu_group=$(basename "${iommu_group_path}")
53+
54+
echo "/dev/vfio/${iommu_group}"
4255
}
4356

4457
pull_rootfs() {
@@ -55,12 +68,19 @@ create_bundle() {
5568
mkdir -p "${bundle_dir}"
5669

5770
# extract busybox rootfs
58-
local rootfs_dir="${bundle_dir}/rootfs"
71+
local rootfs_dir
72+
local layers_dir
73+
74+
rootfs_dir="${bundle_dir}/rootfs"
5975
mkdir -p "${rootfs_dir}"
60-
local layers_dir="$(mktemp -d)"
76+
77+
layers_dir="$(mktemp -d)"
6178
tar -C "${layers_dir}" -pxf "${rootfs_tar}"
79+
6280
for ((i=0;i<$(cat ${layers_dir}/manifest.json | jq -r ".[].Layers | length");i++)); do
63-
tar -C ${rootfs_dir} -xf ${layers_dir}/$(cat ${layers_dir}/manifest.json | jq -r ".[].Layers[${i}]")
81+
local layer
82+
layer=$(cat ${layers_dir}/manifest.json | jq -r ".[].Layers[${i}]")
83+
tar -C "${rootfs_dir}" -xf "${layers_dir}/${layer}"
6484
done
6585
sync
6686

@@ -87,7 +107,7 @@ check_guest_kernel() {
87107
# For vfio_mode=guest-kernel, the device should be bound to
88108
# the guest kernel's native driver. To check this has worked,
89109
# we look for an ethernet device named 'eth*'
90-
get_ctr_cmd_output "${container_id}" ip a | grep "eth" || die "Missing VFIO network interface"
110+
get_ctr_cmd_output "${container_id}" ash -c "${VFIO_CHECK_GUEST_KERNEL}"
91111
}
92112

93113
check_vfio() {
@@ -113,8 +133,8 @@ check_vfio() {
113133
# There should be two devices in the IOMMU group: the ethernet
114134
# device we care about, plus the PCIe to PCI bridge device
115135
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}"
136+
if [ $(echo "${devs}" | wc -w) != ${VFIO_CHECK_NUM_DEVICES} ] ; then
137+
die "Expected exactly ${VFIO_CHECK_NUM_DEVICES} device(s) got: ${devs}"
118138
fi
119139

120140
# The bridge device will always sort first, because it is on
@@ -155,6 +175,8 @@ EOF
155175
}
156176

157177
setup_configuration_file() {
178+
local hot_or_cold_plug_vfio=${$1:-"hot_plug_vfio"}
179+
local vfio_port=${$2:-"bridge-port"}
158180
local qemu_config_file="configuration-qemu.toml"
159181
local clh_config_file="configuration-clh.toml"
160182
local image_file="/opt/kata/share/kata-containers/kata-containers.img"
@@ -188,13 +210,28 @@ setup_configuration_file() {
188210
if [ -n "$MACHINE_TYPE" ]; then
189211
if [ "$HYPERVISOR" = "qemu" ]; then
190212
sed -i 's|^machine_type.*|machine_type = "'${MACHINE_TYPE}'"|g' "${kata_config_file}"
191-
# 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}"
193213
else
194214
warn "Variable machine_type only applies to qemu. It will be ignored"
195215
fi
196216
fi
197217

218+
# Make sure the configuration file has the values available
219+
# otherwise sed will fail to update it. sed will not throw an error.
220+
grep -q -e '^#*.*hot_plug_vfio' "${kata_config_file}" || echo 'hot_plug_vfio="bridge-port"' >> "${kata_config_file}"
221+
grep -q -e '^#*.*cold_plug_vfio' "${kata_config_file}" || echo 'cold_plug_vfio="bridge-port"' >> "${kata_config_file}"
222+
223+
224+
if [ $hot_or_cold_plug_vfio == "hot_plug_vfio" ]; then
225+
sed -i -e "s|^#*.*hot_plug_vfio =.*|hot_plug_vfio = \"${VFIO_PORT}\"|" "${kata_config_file}"
226+
sed -i -e "s|^#*.*cold_plug_vfio =.*|cold_plug_vfio = \"no-port\"|" "${kata_config_file}"
227+
fi
228+
229+
if [ $hot_or_cold_plug_vfio == "cold_plug_vfio" ]; then
230+
sed -i -e "s|^#*.*cold_plug_vfio =.*|cold_plug_vfio = \"${VFIO_PORT}\"|" "${kata_config_file}"
231+
sed -i -e "s|^#*.*hot_plug_vfio =.*|hot_plug_vfio = \"no-port\"|" "${kata_config_file}"
232+
fi
233+
234+
198235
if [ -n "${SANDBOX_CGROUP_ONLY}" ]; then
199236
sed -i 's|^sandbox_cgroup_only.*|sandbox_cgroup_only='${SANDBOX_CGROUP_ONLY}'|g' "${kata_config_file}"
200237
fi
@@ -228,6 +265,8 @@ setup_configuration_file() {
228265
# enable VFIO relevant hypervisor annotations
229266
sed -i -e 's/^\(enable_annotations\).*=.*$/\1 = ["enable_iommu"]/' \
230267
"${kata_config_file}"
268+
269+
cat ${kata_config_file} | grep -v '#' | grep -v '^$'
231270
}
232271

233272
run_test_container() {
@@ -288,8 +327,6 @@ main() {
288327
#
289328
# Get the device ready on the host
290329
#
291-
setup_configuration_file
292-
293330
restart_containerd_service
294331
sudo modprobe vfio
295332
sudo modprobe vfio-pci
@@ -307,7 +344,11 @@ main() {
307344
vfio_major="$(printf '%d' $(stat -c '0x%t' ${vfio_device}))"
308345
vfio_minor="$(printf '%d' $(stat -c '0x%T' ${vfio_device}))"
309346

310-
[ -n "/dev/vfio/vfio" ] || die "vfio control device not found"
347+
# check if /dev/vfio/vfio exists
348+
local vfio_control_device
349+
vfio_control_device=$(ls /dev/vfio/vfio)
350+
351+
[ -n "${vfio_control_device}" ] || die "vfio control device not found"
311352
vfio_ctl_major="$(printf '%d' $(stat -c '0x%t' /dev/vfio/vfio))"
312353
vfio_ctl_minor="$(printf '%d' $(stat -c '0x%T' /dev/vfio/vfio))"
313354

@@ -318,6 +359,36 @@ main() {
318359
# Run the tests
319360
#
320361

362+
# First test hot_plug_vfio="bridge-port"
363+
# the default is VFIO_PORT="bridge-port" if not overriden
364+
setup_configuration_file "hot_plug_vfio" "${VFIO_PORT}"
365+
366+
# test for guest-kernel mode
367+
guest_kernel_cid="vfio-guest-kernel-${RANDOM}"
368+
run_test_container "${guest_kernel_cid}" \
369+
"${tmp_data_dir}/vfio-guest-kernel" \
370+
"${script_path}/guest-kernel.json.in" \
371+
"${host_pci}"
372+
check_guest_kernel "${guest_kernel_cid}"
373+
374+
# Remove the container so we can re-use the device for the next test
375+
clean_env_ctr
376+
377+
# test for vfio mode
378+
vfio_cid="vfio-vfio-${RANDOM}"
379+
run_test_container "${vfio_cid}" \
380+
"${tmp_data_dir}/vfio-vfio" \
381+
"${script_path}/vfio.json.in" \
382+
"${host_pci}"
383+
check_vfio "${vfio_cid}"
384+
385+
# Remove the container so we can re-use the device for the next test
386+
clean_env_ctr
387+
388+
# Mpw test cold_plug_vfio="bridge-port"
389+
# the default is VFIO_PORT="bridge-port" if not overriden
390+
setup_configuration_file "cold_plug_vfio" "${VFIO_PORT}"
391+
321392
# test for guest-kernel mode
322393
guest_kernel_cid="vfio-guest-kernel-${RANDOM}"
323394
run_test_container "${guest_kernel_cid}" \
@@ -338,4 +409,4 @@ main() {
338409
check_vfio "${vfio_cid}"
339410
}
340411

341-
main $@
412+
main "$@"

0 commit comments

Comments
 (0)