From 6af059227468763ac5725a1dc59c0774e2a9b81c Mon Sep 17 00:00:00 2001 From: "alex.lyn" Date: Mon, 27 Nov 2023 15:21:56 +0800 Subject: [PATCH] runtime-rs: Add vsock device in device manager. (1) Implement Device Trait for vsock device. (2) add vsock device in device manager. Fixes: #8474 Signed-off-by: alex.lyn --- .../hypervisor/src/device/device_manager.rs | 9 +++-- .../src/device/driver/virtio_vsock.rs | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 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 8d71ecbccd..31602a01f0 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs @@ -12,7 +12,7 @@ use tokio::sync::{Mutex, RwLock}; use crate::{ vhost_user_blk::VhostUserBlkDevice, BlockConfig, BlockDevice, HybridVsockDevice, Hypervisor, - NetworkDevice, ShareFsDevice, VfioDevice, VhostUserConfig, KATA_BLK_DEV_TYPE, + NetworkDevice, ShareFsDevice, VfioDevice, VhostUserConfig, VsockDevice, KATA_BLK_DEV_TYPE, KATA_MMIO_BLK_DEV_TYPE, KATA_NVDIMM_DEV_TYPE, VIRTIO_BLOCK_MMIO, VIRTIO_BLOCK_PCI, VIRTIO_PMEM, }; @@ -330,6 +330,10 @@ impl DeviceManager { // No need to do find device for hybrid vsock device. Arc::new(Mutex::new(HybridVsockDevice::new(&device_id, hvconfig))) } + DeviceConfig::VsockCfg(_vconfig) => { + // No need to do find device for vsock device. + Arc::new(Mutex::new(VsockDevice::new(device_id.clone()).await?)) + } DeviceConfig::ShareFsCfg(config) => { // Try to find the sharefs device. If found, just return matched device id. if let Some(device_id_matched) = @@ -346,9 +350,6 @@ impl DeviceManager { Arc::new(Mutex::new(ShareFsDevice::new(&device_id, config))) } - _ => { - return Err(anyhow!("invliad device type")); - } }; // register device to devices 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 c4d4664041..f3ef545a96 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 @@ -129,6 +129,43 @@ impl VsockDevice { } } +#[async_trait] +impl Device for VsockDevice { + async fn attach(&mut self, h: &dyn hypervisor) -> Result<()> { + h.add_device(DeviceType::Vsock(self.clone())) + .await + .context("add vsock device.")?; + + return Ok(()); + } + + async fn detach(&mut self, _h: &dyn hypervisor) -> Result> { + // no need to do detach, just return Ok(None) + Ok(None) + } + + async fn update(&mut self, _h: &dyn hypervisor) -> Result<()> { + // There's no need to do update for vsock device + Ok(()) + } + + async fn get_device_info(&self) -> DeviceType { + DeviceType::Vsock(self.clone()) + } + + async fn increase_attach_count(&mut self) -> Result { + // vsock devices will not be attached multiple times, Just return Ok(false) + + Ok(false) + } + + async fn decrease_attach_count(&mut self) -> Result { + // vsock devices will not be detached multiple times, Just return Ok(false) + + Ok(false) + } +} + pub async fn generate_vhost_vsock_cid() -> Result<(u32, File)> { let vhost_fd = OpenOptions::new() .read(true)