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 <alex.lyn@antgroup.com>
This commit is contained in:
alex.lyn 2023-12-22 10:37:40 +08:00
parent 82d3cfdeda
commit 94c83cea84
2 changed files with 19 additions and 10 deletions

View File

@ -291,7 +291,7 @@ impl DeviceManager {
Arc::new(Mutex::new(VfioDevice::new( Arc::new(Mutex::new(VfioDevice::new(
device_id.clone(), device_id.clone(),
&vfio_dev_config, &vfio_dev_config,
))) )?))
} }
DeviceConfig::VhostUserBlkCfg(config) => { DeviceConfig::VhostUserBlkCfg(config) => {
// try to find the device, found and just return id. // try to find the device, found and just return id.

View File

@ -252,7 +252,7 @@ pub struct VfioDevice {
impl VfioDevice { impl VfioDevice {
// new with VfioConfig // new with VfioConfig
pub fn new(device_id: String, dev_info: &VfioConfig) -> Self { pub fn new(device_id: String, dev_info: &VfioConfig) -> Result<Self> {
// devices and device_options are in a 1-1 mapping, used in // devices and device_options are in a 1-1 mapping, used in
// vfio-pci handler for kata-agent. // vfio-pci handler for kata-agent.
let devices: Vec<HostDevice> = Vec::with_capacity(MAX_DEV_ID_SIZE); let devices: Vec<HostDevice> = Vec::with_capacity(MAX_DEV_ID_SIZE);
@ -262,7 +262,7 @@ impl VfioDevice {
let dev_type = dev_info.dev_type.as_str(); let dev_type = dev_info.dev_type.as_str();
let driver_type = VfioBusMode::driver_type(dev_type).to_owned(); let driver_type = VfioBusMode::driver_type(dev_type).to_owned();
Self { let mut vfio_device = Self {
device_id, device_id,
attach_count: 0, attach_count: 0,
bus_mode: VfioBusMode::PCI, bus_mode: VfioBusMode::PCI,
@ -270,7 +270,13 @@ impl VfioDevice {
config: dev_info.clone(), config: dev_info.clone(),
devices, devices,
device_options, device_options,
} };
vfio_device
.initialize_vfio_device()
.context("initialize vfio device failed.")?;
Ok(vfio_device)
} }
fn get_host_path(&self) -> String { fn get_host_path(&self) -> String {
@ -370,7 +376,7 @@ impl VfioDevice {
Ok(DeviceVendor(device, vendor)) Ok(DeviceVendor(device, vendor))
} }
async fn set_vfio_config( fn set_vfio_config(
&mut self, &mut self,
iommu_devs_path: PathBuf, iommu_devs_path: PathBuf,
device_name: &str, device_name: &str,
@ -422,11 +428,8 @@ impl VfioDevice {
_ => None, _ => None,
} }
} }
}
#[async_trait] fn initialize_vfio_device(&mut self) -> Result<()> {
impl Device for VfioDevice {
async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> {
// host path: /dev/vfio/X // host path: /dev/vfio/X
let host_path = self.get_host_path(); let host_path = self.get_host_path();
// vfio group: X // vfio group: X
@ -460,7 +463,6 @@ impl Device for VfioDevice {
let mut hostdev: HostDevice = self let mut hostdev: HostDevice = self
.set_vfio_config(iommu_devs_path.clone(), device) .set_vfio_config(iommu_devs_path.clone(), device)
.await
.context("set vfio config failed")?; .context("set vfio config failed")?;
let dev_prefix = self.get_vfio_prefix(); let dev_prefix = self.get_vfio_prefix();
hostdev.hostdev_id = make_device_nameid(&dev_prefix, index, MAX_DEV_ID_SIZE); 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); self.devices.push(hostdev);
} }
Ok(())
}
}
#[async_trait]
impl Device for VfioDevice {
async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> {
if self if self
.increase_attach_count() .increase_attach_count()
.await .await