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 <alex.lyn@antgroup.com>
This commit is contained in:
alex.lyn 2023-11-27 15:21:56 +08:00
parent 1a6b45d3b7
commit 6af0592274
2 changed files with 42 additions and 4 deletions

View File

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

View File

@ -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<Option<u64>> {
// 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<bool> {
// vsock devices will not be attached multiple times, Just return Ok(false)
Ok(false)
}
async fn decrease_attach_count(&mut self) -> Result<bool> {
// 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)