diff --git a/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs b/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs index 2b5ef6df54..04dddead35 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs @@ -291,7 +291,7 @@ impl DeviceManager { Arc::new(Mutex::new(VfioDevice::new( device_id.clone(), &vfio_dev_config, - ))) + )?)) } DeviceConfig::VhostUserBlkCfg(config) => { // try to find the device, found and just return id. diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs index 3e4534be3a..aac879a594 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs @@ -252,7 +252,7 @@ pub struct VfioDevice { impl VfioDevice { // new with VfioConfig - pub fn new(device_id: String, dev_info: &VfioConfig) -> Self { + pub fn new(device_id: String, dev_info: &VfioConfig) -> Result { // devices and device_options are in a 1-1 mapping, used in // vfio-pci handler for kata-agent. let devices: Vec = Vec::with_capacity(MAX_DEV_ID_SIZE); @@ -262,7 +262,7 @@ impl VfioDevice { let dev_type = dev_info.dev_type.as_str(); let driver_type = VfioBusMode::driver_type(dev_type).to_owned(); - Self { + let mut vfio_device = Self { device_id, attach_count: 0, bus_mode: VfioBusMode::PCI, @@ -270,7 +270,13 @@ impl VfioDevice { config: dev_info.clone(), devices, device_options, - } + }; + + vfio_device + .initialize_vfio_device() + .context("initialize vfio device failed.")?; + + Ok(vfio_device) } fn get_host_path(&self) -> String { @@ -370,7 +376,7 @@ impl VfioDevice { Ok(DeviceVendor(device, vendor)) } - async fn set_vfio_config( + fn set_vfio_config( &mut self, iommu_devs_path: PathBuf, device_name: &str, @@ -422,11 +428,8 @@ impl VfioDevice { _ => None, } } -} -#[async_trait] -impl Device for VfioDevice { - async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> { + fn initialize_vfio_device(&mut self) -> Result<()> { // host path: /dev/vfio/X let host_path = self.get_host_path(); // vfio group: X @@ -460,7 +463,6 @@ impl Device for VfioDevice { let mut hostdev: HostDevice = self .set_vfio_config(iommu_devs_path.clone(), device) - .await .context("set vfio config failed")?; let dev_prefix = self.get_vfio_prefix(); hostdev.hostdev_id = make_device_nameid(&dev_prefix, index, MAX_DEV_ID_SIZE); @@ -468,6 +470,13 @@ impl Device for VfioDevice { self.devices.push(hostdev); } + Ok(()) + } +} + +#[async_trait] +impl Device for VfioDevice { + async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> { if self .increase_attach_count() .await