From 6539d4a87368541ae7f3f90d275348285997cba5 Mon Sep 17 00:00:00 2001 From: Jose Carlos Venegas Munoz Date: Wed, 4 Mar 2020 19:13:36 +0000 Subject: [PATCH] vfio: handle case for missing iommu_group For cases where IOMMU is not supported, iommu_group will be no present. This could happened because IOMMU is off at kernel level, bios level or is not supported at all. Instead of fail with a generic error handle it as different use case: Before: ReadLink(Os { code: 2, kind: NotFound, message: "No such file or directory"}) After: VfioNoIommuGroup("/sys/bus/pci/devices/0000:03:00.0/iommu_group") Signed-off-by: Jose Carlos Venegas Munoz --- vfio/src/vfio_device.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/vfio/src/vfio_device.rs b/vfio/src/vfio_device.rs index a77a610f1..3e95295ae 100644 --- a/vfio/src/vfio_device.rs +++ b/vfio/src/vfio_device.rs @@ -53,6 +53,7 @@ pub enum VfioError { IommuDmaUnmap, VfioDeviceGetIrqInfo, VfioDeviceSetIrq, + VfioNoIommuGroup(String), ReadLink(io::Error), ParseInt(num::ParseIntError), } @@ -104,6 +105,7 @@ impl fmt::Display for VfioError { VfioError::VfioDeviceGetIrqInfo => write!(f, "failed to get vfio device irq info"), VfioError::VfioDeviceSetIrq => write!(f, "failed to set vfio deviece irq"), VfioError::ReadLink(e) => write!(f, "failed to read link from path: {}", e), + VfioError::VfioNoIommuGroup(e) => write!(f, "Failed to find iommu_group for: {}", e), VfioError::ParseInt(e) => write!(f, "failed to parse integer: {}", e), } } @@ -602,6 +604,11 @@ impl VfioDevice { iommu_attached: bool, ) -> Result { let uuid_path: PathBuf = [sysfspath, Path::new("iommu_group")].iter().collect(); + if !uuid_path.exists() { + return Err(VfioError::VfioNoIommuGroup( + uuid_path.to_str().unwrap().to_string(), + )); + } let group_path = uuid_path.read_link().map_err(VfioError::ReadLink)?; let group_osstr = group_path.file_name().ok_or(VfioError::InvalidPath)?; let group_str = group_osstr.to_str().ok_or(VfioError::InvalidPath)?;