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 8d71ecbcc..31602a01f 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 c4d466404..f3ef545a9 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)