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 76eaba4e1d..376ed671d1 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs @@ -18,6 +18,7 @@ use crate::{ }; use super::{ + topology::PCIeTopology, util::{get_host_path, get_virt_drive_name, DEVICE_TYPE_BLOCK}, Device, DeviceConfig, DeviceType, }; @@ -121,7 +122,9 @@ impl DeviceManager { .context("failed to find device")?; let mut device_guard = device.lock().await; // attach device - let result = device_guard.attach(self.hypervisor.as_ref()).await; + let result = device_guard + .attach(&mut None::<&mut PCIeTopology>, self.hypervisor.as_ref()) + .await; // handle attach error if let Err(e) = result { match device_guard.get_device_info().await { @@ -161,7 +164,10 @@ impl DeviceManager { pub async fn try_remove_device(&mut self, device_id: &str) -> Result<()> { if let Some(dev) = self.devices.get(device_id) { let mut device_guard = dev.lock().await; - let result = match device_guard.detach(self.hypervisor.as_ref()).await { + let result = match device_guard + .detach(&mut None::<&mut PCIeTopology>, self.hypervisor.as_ref()) + .await + { Ok(index) => { if let Some(i) = index { // release the declared device index 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 958081d8eb..4c1f89e450 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs @@ -477,7 +477,11 @@ impl VfioDevice { #[async_trait] impl Device for VfioDevice { - async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> { + async fn attach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result<()> { if self .increase_attach_count() .await @@ -525,7 +529,11 @@ impl Device for VfioDevice { } } - async fn detach(&mut self, h: &dyn hypervisor) -> Result> { + async fn detach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result> { if self .decrease_attach_count() .await diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_blk.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_blk.rs index 5150f19563..b2a1d90f92 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_blk.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_blk.rs @@ -9,7 +9,7 @@ use async_trait::async_trait; use super::VhostUserConfig; use crate::{ - device::{Device, DeviceType}, + device::{topology::PCIeTopology, Device, DeviceType}, Hypervisor as hypervisor, }; @@ -45,7 +45,11 @@ impl VhostUserBlkDevice { #[async_trait] impl Device for VhostUserBlkDevice { - async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> { + async fn attach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result<()> { // increase attach count, skip attach the device if the device is already attached if self .increase_attach_count() @@ -64,7 +68,11 @@ impl Device for VhostUserBlkDevice { return Ok(()); } - async fn detach(&mut self, h: &dyn hypervisor) -> Result> { + async fn detach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result> { // get the count of device detached, and detach once it reaches 0 if self .decrease_attach_count() diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_net.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_net.rs index dd31da8697..a28f969852 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_net.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_net.rs @@ -4,6 +4,7 @@ use anyhow::{Context, Result}; use async_trait::async_trait; +use crate::device::topology::PCIeTopology; use crate::device::{Device, DeviceType}; use crate::{Hypervisor, VhostUserConfig}; @@ -22,14 +23,22 @@ impl VhostUserNetDevice { #[async_trait] impl Device for VhostUserNetDevice { - async fn attach(&mut self, h: &dyn Hypervisor) -> Result<()> { + async fn attach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn Hypervisor, + ) -> Result<()> { h.add_device(DeviceType::VhostUserNetwork(self.clone())) .await .context("add vhost-user-net device to hypervisor")?; Ok(()) } - async fn detach(&mut self, h: &dyn Hypervisor) -> Result> { + async fn detach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn Hypervisor, + ) -> Result> { h.remove_device(DeviceType::VhostUserNetwork(self.clone())) .await .context("remove vhost-user-net device from hypervisor")?; diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs index f16fe8eb04..50d8179200 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs @@ -5,6 +5,7 @@ // use crate::device::pci_path::PciPath; +use crate::device::topology::PCIeTopology; use crate::device::Device; use crate::device::DeviceType; use crate::Hypervisor as hypervisor; @@ -73,7 +74,11 @@ impl BlockDevice { #[async_trait] impl Device for BlockDevice { - async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> { + async fn attach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result<()> { // increase attach count, skip attach the device if the device is already attached if self .increase_attach_count() @@ -98,7 +103,11 @@ impl Device for BlockDevice { } } - async fn detach(&mut self, h: &dyn hypervisor) -> Result> { + async fn detach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result> { // get the count of device detached, skip detach once it reaches the 0 if self .decrease_attach_count() diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_fs.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_fs.rs index e968606de3..c06498b24c 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_fs.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_fs.rs @@ -7,7 +7,7 @@ use anyhow::{Context, Result}; use async_trait::async_trait; -use crate::device::{hypervisor, Device, DeviceType}; +use crate::device::{hypervisor, topology::PCIeTopology, Device, DeviceType}; #[derive(Copy, Clone, Debug, Default)] pub enum ShareFsMountOperation { @@ -99,7 +99,11 @@ impl ShareFsDevice { #[async_trait] impl Device for ShareFsDevice { - async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> { + async fn attach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result<()> { h.add_device(DeviceType::ShareFs(self.clone())) .await .context("add share-fs device.")?; @@ -107,7 +111,11 @@ impl Device for ShareFsDevice { Ok(()) } - async fn detach(&mut self, _h: &dyn hypervisor) -> Result> { + async fn detach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + _h: &dyn hypervisor, + ) -> Result> { // no need to detach share-fs device Ok(None) diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_net.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_net.rs index bc17e3f21a..4462761bed 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_net.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_net.rs @@ -9,6 +9,7 @@ use std::fmt; use anyhow::{Context, Result}; use async_trait::async_trait; +use crate::device::topology::PCIeTopology; use crate::device::{Device, DeviceType}; use crate::Hypervisor as hypervisor; @@ -70,7 +71,11 @@ impl NetworkDevice { #[async_trait] impl Device for NetworkDevice { - async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> { + async fn attach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result<()> { h.add_device(DeviceType::Network(self.clone())) .await .context("add network device.")?; @@ -78,7 +83,11 @@ impl Device for NetworkDevice { return Ok(()); } - async fn detach(&mut self, h: &dyn hypervisor) -> Result> { + async fn detach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result> { h.remove_device(DeviceType::Network(self.clone())) .await .context("remove network device.")?; diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_vsock.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_vsock.rs index 0c37fb18dd..efafd191fd 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_vsock.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_vsock.rs @@ -12,7 +12,7 @@ use tokio::fs::{File, OpenOptions}; use async_trait::async_trait; use crate::{ - device::{Device, DeviceType}, + device::{topology::PCIeTopology, Device, DeviceType}, Hypervisor as hypervisor, }; @@ -49,7 +49,11 @@ impl HybridVsockDevice { #[async_trait] impl Device for HybridVsockDevice { - async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> { + async fn attach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result<()> { h.add_device(DeviceType::HybridVsock(self.clone())) .await .context("add hybrid vsock device.")?; @@ -57,7 +61,11 @@ impl Device for HybridVsockDevice { return Ok(()); } - async fn detach(&mut self, _h: &dyn hypervisor) -> Result> { + async fn detach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + _h: &dyn hypervisor, + ) -> Result> { // no need to do detach, just return Ok(None) Ok(None) } @@ -135,7 +143,11 @@ impl VsockDevice { #[async_trait] impl Device for VsockDevice { - async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> { + async fn attach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result<()> { h.add_device(DeviceType::Vsock(self.clone())) .await .context("add vsock device.")?; @@ -143,7 +155,11 @@ impl Device for VsockDevice { return Ok(()); } - async fn detach(&mut self, _h: &dyn hypervisor) -> Result> { + async fn detach( + &mut self, + _pcie_topo: &mut Option<&mut PCIeTopology>, + _h: &dyn hypervisor, + ) -> Result> { // no need to do detach, just return Ok(None) Ok(None) } diff --git a/src/runtime-rs/crates/hypervisor/src/device/mod.rs b/src/runtime-rs/crates/hypervisor/src/device/mod.rs index 7b18d2f867..49b1ff844a 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/mod.rs @@ -56,9 +56,17 @@ impl fmt::Display for DeviceType { #[async_trait] pub trait Device: std::fmt::Debug + Send + Sync { // attach is to plug device into VM - async fn attach(&mut self, h: &dyn hypervisor) -> Result<()>; + async fn attach( + &mut self, + pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result<()>; // detach is to unplug device from VM - async fn detach(&mut self, h: &dyn hypervisor) -> Result>; + async fn detach( + &mut self, + pcie_topo: &mut Option<&mut PCIeTopology>, + h: &dyn hypervisor, + ) -> Result>; // update is to do update for some device async fn update(&mut self, h: &dyn hypervisor) -> Result<()>; // get_device_info returns device config