From 94c83cea84b7431622bf986141a018b39e16f853 Mon Sep 17 00:00:00 2001 From: "alex.lyn" Date: Fri, 22 Dec 2023 10:37:40 +0800 Subject: [PATCH] runtime-rs: Refactor vfio driver implementation It's important to ensure that these tasks which setup vfio devices are completed before add_device. So Moving vfio device setup code to a dedicated method at device building time which does not affect the behavior of other code. And this change makes it easier to understand the difference between create and attach, and also makes the boundaries clearer. Fixes: #8665 Signed-off-by: alex.lyn --- .../hypervisor/src/device/device_manager.rs | 2 +- .../hypervisor/src/device/driver/vfio.rs | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) 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