#![deny(missing_docs)]
#[macro_use]
extern crate vmm_sys_util;
use std::io;
use thiserror::Error;
use vmm_sys_util::errno::Error as SysError;
mod fam;
mod vfio_device;
mod vfio_ioctls;
pub use vfio_device::{
VfioContainer, VfioDevice, VfioGroup, VfioIrq, VfioRegion, VfioRegionInfoCap,
VfioRegionInfoCapNvlink2Lnkspd, VfioRegionInfoCapNvlink2Ssatgt, VfioRegionInfoCapSparseMmap,
VfioRegionInfoCapType, VfioRegionSparseMmapArea,
};
#[derive(Debug, Error)]
#[allow(missing_docs)]
pub enum VfioError {
#[error("failed to open /dev/vfio/vfio container: {0}")]
OpenContainer(#[source] io::Error),
#[error("failed to open /dev/vfio/{1} group: {0}")]
OpenGroup(#[source] io::Error, String),
#[error("failed to get Group Status")]
GetGroupStatus,
#[error("group is not viable")]
GroupViable,
#[error("vfio API version doesn't match with VFIO_API_VERSION defined in vfio-bindings")]
VfioApiVersion,
#[error("failed to check VFIO extension")]
VfioExtension,
#[error("invalid VFIO type")]
VfioInvalidType,
#[error("container doesn't support VfioType1V2 IOMMU driver type")]
VfioType1V2,
#[error("failed to add vfio group into vfio container")]
GroupSetContainer,
#[error("failed to unset vfio container")]
UnsetContainer,
#[error("failed to set container's IOMMU driver type as VfioType1V2")]
ContainerSetIOMMU,
#[error("failed to get vfio device fd")]
GroupGetDeviceFD,
#[error("failed to set vfio device's attribute: {0}")]
SetDeviceAttr(#[source] SysError),
#[error("failed to get vfio device's info or info doesn't match")]
VfioDeviceGetInfo,
#[error("failed to get vfio device's region info: {0}")]
VfioDeviceGetRegionInfo(#[source] SysError),
#[error("invalid file path")]
InvalidPath,
#[error("failed to add guest memory map into iommu table")]
IommuDmaMap,
#[error("failed to remove guest memory map from iommu table")]
IommuDmaUnmap,
#[error("failed to get vfio device irq info")]
VfioDeviceGetIrqInfo,
#[error("failed to set vfio device irq")]
VfioDeviceSetIrq,
#[error("failed to enable vfio device irq")]
VfioDeviceEnableIrq,
#[error("failed to disable vfio device irq")]
VfioDeviceDisableIrq,
#[error("failed to unmask vfio device irq")]
VfioDeviceUnmaskIrq,
#[error("failed to trigger vfio device irq")]
VfioDeviceTriggerIrq,
}
pub type Result<T> = std::result::Result<T, VfioError>;
#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;
#[test]
fn test_vfio_error_fmt() {
let e = VfioError::GetGroupStatus;
let e2 = VfioError::OpenContainer(std::io::Error::from(std::io::ErrorKind::Other));
let str = format!("{}", e);
assert_eq!(&str, "failed to get Group Status");
assert!(e2.source().is_some());
assert!(e.source().is_none());
}
}