runtime-rs: add one more argument in Device attach/detach

Add one more argument with type &mut Option<&mut PCIeTopology>
in attach and detach to inroduce methods within PCIe Topology.

Fixes: #7218

Signed-off-by: alex.lyn <alex.lyn@antgroup.com>
This commit is contained in:
alex.lyn
2023-12-27 15:40:01 +08:00
parent b425de6105
commit 0d4992b24d
9 changed files with 104 additions and 23 deletions

View File

@@ -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

View File

@@ -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<Option<u64>> {
async fn detach(
&mut self,
_pcie_topo: &mut Option<&mut PCIeTopology>,
h: &dyn hypervisor,
) -> Result<Option<u64>> {
if self
.decrease_attach_count()
.await

View File

@@ -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<Option<u64>> {
async fn detach(
&mut self,
_pcie_topo: &mut Option<&mut PCIeTopology>,
h: &dyn hypervisor,
) -> Result<Option<u64>> {
// get the count of device detached, and detach once it reaches 0
if self
.decrease_attach_count()

View File

@@ -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<Option<u64>> {
async fn detach(
&mut self,
_pcie_topo: &mut Option<&mut PCIeTopology>,
h: &dyn Hypervisor,
) -> Result<Option<u64>> {
h.remove_device(DeviceType::VhostUserNetwork(self.clone()))
.await
.context("remove vhost-user-net device from hypervisor")?;

View File

@@ -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<Option<u64>> {
async fn detach(
&mut self,
_pcie_topo: &mut Option<&mut PCIeTopology>,
h: &dyn hypervisor,
) -> Result<Option<u64>> {
// get the count of device detached, skip detach once it reaches the 0
if self
.decrease_attach_count()

View File

@@ -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<Option<u64>> {
async fn detach(
&mut self,
_pcie_topo: &mut Option<&mut PCIeTopology>,
_h: &dyn hypervisor,
) -> Result<Option<u64>> {
// no need to detach share-fs device
Ok(None)

View File

@@ -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<Option<u64>> {
async fn detach(
&mut self,
_pcie_topo: &mut Option<&mut PCIeTopology>,
h: &dyn hypervisor,
) -> Result<Option<u64>> {
h.remove_device(DeviceType::Network(self.clone()))
.await
.context("remove network device.")?;

View File

@@ -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<Option<u64>> {
async fn detach(
&mut self,
_pcie_topo: &mut Option<&mut PCIeTopology>,
_h: &dyn hypervisor,
) -> Result<Option<u64>> {
// 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<Option<u64>> {
async fn detach(
&mut self,
_pcie_topo: &mut Option<&mut PCIeTopology>,
_h: &dyn hypervisor,
) -> Result<Option<u64>> {
// no need to do detach, just return Ok(None)
Ok(None)
}

View File

@@ -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<Option<u64>>;
async fn detach(
&mut self,
pcie_topo: &mut Option<&mut PCIeTopology>,
h: &dyn hypervisor,
) -> Result<Option<u64>>;
// update is to do update for some device
async fn update(&mut self, h: &dyn hypervisor) -> Result<()>;
// get_device_info returns device config