From ec5ff395cfe318b787d590db11029eb17d08d25b Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 27 Apr 2020 13:49:54 +0200 Subject: [PATCH] vm-virtio: vsock: Expect an identifier upon device creation This identifier is chosen from the DeviceManager so that it will manage all identifiers across the VM, which will ensure uniqueness. Signed-off-by: Sebastien Boeuf --- vm-virtio/src/vsock/device.rs | 13 +++++++++++-- vm-virtio/src/vsock/mod.rs | 2 +- vmm/src/device_manager.rs | 18 ++++++++++-------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/vm-virtio/src/vsock/device.rs b/vm-virtio/src/vsock/device.rs index ed60a70b8..6cd1ff9d6 100644 --- a/vm-virtio/src/vsock/device.rs +++ b/vm-virtio/src/vsock/device.rs @@ -376,6 +376,7 @@ where /// Virtio device exposing virtual socket to the guest. pub struct Vsock { + id: String, cid: u64, backend: Arc>, kill_evt: Option, @@ -394,7 +395,7 @@ where { /// Create a new virtio-vsock device with the given VM CID and vsock /// backend. - pub fn new(cid: u64, backend: B, iommu: bool) -> io::Result> { + pub fn new(id: String, cid: u64, backend: B, iommu: bool) -> io::Result> { let mut avail_features = 1u64 << VIRTIO_F_VERSION_1 | 1u64 << VIRTIO_F_IN_ORDER; if iommu { @@ -402,6 +403,7 @@ where } Ok(Vsock { + id, cid, backend: Arc::new(RwLock::new(backend)), kill_evt: None, @@ -574,7 +576,14 @@ where virtio_pausable!(Vsock, T: 'static + VsockBackend + Sync); -impl Snapshottable for Vsock where B: VsockBackend + Sync + 'static {} +impl Snapshottable for Vsock +where + B: VsockBackend + Sync + 'static, +{ + fn id(&self) -> String { + self.id.clone() + } +} impl Transportable for Vsock where B: VsockBackend + Sync + 'static {} impl Migratable for Vsock where B: VsockBackend + Sync + 'static {} diff --git a/vm-virtio/src/vsock/mod.rs b/vm-virtio/src/vsock/mod.rs index 34280b20d..ab95cfddc 100644 --- a/vm-virtio/src/vsock/mod.rs +++ b/vm-virtio/src/vsock/mod.rs @@ -265,7 +265,7 @@ mod tests { cid: CID, mem: GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE)]).unwrap(), mem_size: MEM_SIZE, - device: Vsock::new(CID, TestBackend::new(), false).unwrap(), + device: Vsock::new(String::from("vsock"), CID, TestBackend::new(), false).unwrap(), } } diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 2ee2118be..f62784d01 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -1690,9 +1690,13 @@ impl DeviceManager { &mut self, vsock_cfg: &mut VsockConfig, ) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option)> { - if vsock_cfg.id.is_none() { - vsock_cfg.id = Some(self.next_device_name(VSOCK_DEVICE_NAME_PREFIX)?); - } + let id = if let Some(id) = &vsock_cfg.id { + id.clone() + } else { + let id = self.next_device_name(VSOCK_DEVICE_NAME_PREFIX)?; + vsock_cfg.id = Some(id.clone()); + id + }; let socket_path = vsock_cfg .sock @@ -1703,18 +1707,16 @@ impl DeviceManager { .map_err(DeviceManagerError::CreateVsockBackend)?; let vsock_device = Arc::new(Mutex::new( - vm_virtio::Vsock::new(vsock_cfg.cid, backend, vsock_cfg.iommu) + vm_virtio::Vsock::new(id.clone(), vsock_cfg.cid, backend, vsock_cfg.iommu) .map_err(DeviceManagerError::CreateVirtioVsock)?, )); - let migratable = Arc::clone(&vsock_device) as Arc>; - let id = migratable.lock().unwrap().id(); - self.migratable_devices.push((id, migratable)); + self.add_migratable_device(Arc::clone(&vsock_device) as Arc>); Ok(( Arc::clone(&vsock_device) as VirtioDeviceArc, vsock_cfg.iommu, - vsock_cfg.id.clone(), + Some(id), )) }