dragonball: Remove virtio-net and vsock devices gracefully

This MR implements removing virtio-net and virtio-vsock devices gracefully when
shutting down VMM.

Fixes: #6684

Signed-off-by: Zizheng Bian <zizheng.bian@linux.alibaba.com>
Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
This commit is contained in:
Xuewei Niu 2023-05-18 10:24:47 +08:00
parent e762f70920
commit ee6deef09d
4 changed files with 44 additions and 1 deletions

View File

@ -278,6 +278,11 @@ where
self.info_list.iter_mut()
}
/// Remove the last device config info from the `info_list`.
pub fn pop(&mut self) -> Option<DeviceConfigInfo<T>> {
self.info_list.pop()
}
fn get_index_by_id(&self, config: &T) -> Option<usize> {
self.info_list
.iter()

View File

@ -714,6 +714,14 @@ impl DeviceManager {
#[cfg(feature = "virtio-blk")]
self.block_manager.remove_devices(&mut ctx)?;
// FIXME: To acquire the full abilities for gracefully removing
// virtio-net and virtio-vsock devices, updating dragonball-sandbox
// is required.
#[cfg(feature = "virtio-net")]
self.virtio_net_manager.remove_devices(&mut ctx)?;
#[cfg(feature = "virtio-vsock")]
self.vsock_manager.remove_devices(&mut ctx)?;
Ok(())
}
}

View File

@ -374,6 +374,21 @@ impl VirtioNetDeviceMgr {
Ok(Box::new(net_device))
}
/// Remove all virtio-net devices.
pub fn remove_devices(&mut self, ctx: &mut DeviceOpContext) -> Result<(), DeviceMgrError> {
while let Some(mut info) = self.info_list.pop() {
slog::info!(
ctx.logger(),
"remove virtio-net device: {}",
info.config.iface_id
);
if let Some(device) = info.device.take() {
DeviceManager::destroy_mmio_virtio_device(device, ctx)?;
}
}
Ok(())
}
}
impl Default for VirtioNetDeviceMgr {

View File

@ -17,7 +17,7 @@ use dbs_virtio_devices::vsock::Vsock;
use dbs_virtio_devices::Error as VirtioError;
use serde_derive::{Deserialize, Serialize};
use super::StartMicroVmError;
use super::{DeviceMgrError, StartMicroVmError};
use crate::config_manager::{ConfigItem, DeviceConfigInfo, DeviceConfigInfos};
use crate::device_manager::{DeviceManager, DeviceOpContext};
@ -284,6 +284,21 @@ impl VsockDeviceMgr {
// safe to unwrap, because we created the inner connector before
Ok(self.default_inner_connector.clone().unwrap())
}
/// Remove all virtio-vsock devices
pub fn remove_devices(&mut self, ctx: &mut DeviceOpContext) -> Result<(), DeviceMgrError> {
while let Some(mut info) = self.info_list.pop() {
slog::info!(
ctx.logger(),
"remove virtio-vsock device: {}",
info.config.id
);
if let Some(device) = info.device.take() {
DeviceManager::destroy_mmio_virtio_device(device, ctx)?;
}
}
Ok(())
}
}
impl Default for VsockDeviceMgr {