diff --git a/vm-virtio/src/vsock/device.rs b/vm-virtio/src/vsock/device.rs index 6cd1ff9d6..b6a818db9 100644 --- a/vm-virtio/src/vsock/device.rs +++ b/vm-virtio/src/vsock/device.rs @@ -40,6 +40,7 @@ use libc::EFD_NONBLOCK; use std; use std::io; use std::os::unix::io::AsRawFd; +use std::path::PathBuf; use std::result; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, RwLock}; @@ -387,6 +388,7 @@ pub struct Vsock { interrupt_cb: Option>, epoll_threads: Option>>>, paused: Arc, + path: PathBuf, } impl Vsock @@ -395,7 +397,13 @@ where { /// Create a new virtio-vsock device with the given VM CID and vsock /// backend. - pub fn new(id: String, cid: u64, backend: B, iommu: bool) -> io::Result> { + pub fn new( + id: String, + cid: u64, + path: PathBuf, + backend: B, + iommu: bool, + ) -> io::Result> { let mut avail_features = 1u64 << VIRTIO_F_VERSION_1 | 1u64 << VIRTIO_F_IN_ORDER; if iommu { @@ -414,6 +422,7 @@ where interrupt_cb: None, epoll_threads: None, paused: Arc::new(AtomicBool::new(false)), + path, }) } } @@ -572,6 +581,10 @@ where self.queue_evts.take().unwrap(), )) } + + fn shutdown(&mut self) { + std::fs::remove_file(&self.path).ok(); + } } virtio_pausable!(Vsock, T: 'static + VsockBackend + Sync); diff --git a/vm-virtio/src/vsock/mod.rs b/vm-virtio/src/vsock/mod.rs index ab95cfddc..a3873b6ba 100644 --- a/vm-virtio/src/vsock/mod.rs +++ b/vm-virtio/src/vsock/mod.rs @@ -166,6 +166,7 @@ mod tests { use crate::{VIRTQ_DESC_F_NEXT, VIRTQ_DESC_F_WRITE}; use libc::EFD_NONBLOCK; use std::os::unix::io::AsRawFd; + use std::path::PathBuf; use std::sync::atomic::AtomicBool; use std::sync::{Arc, RwLock}; use vm_memory::{GuestAddress, GuestMemoryAtomic, GuestMemoryMmap}; @@ -265,7 +266,14 @@ mod tests { cid: CID, mem: GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE)]).unwrap(), mem_size: MEM_SIZE, - device: Vsock::new(String::from("vsock"), CID, TestBackend::new(), false).unwrap(), + device: Vsock::new( + String::from("vsock"), + CID, + PathBuf::from("/test/sock"), + TestBackend::new(), + false, + ) + .unwrap(), } } diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index ad0a19fac..8d5442b10 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -1714,8 +1714,14 @@ impl DeviceManager { .map_err(DeviceManagerError::CreateVsockBackend)?; let vsock_device = Arc::new(Mutex::new( - vm_virtio::Vsock::new(id.clone(), vsock_cfg.cid, backend, vsock_cfg.iommu) - .map_err(DeviceManagerError::CreateVirtioVsock)?, + vm_virtio::Vsock::new( + id.clone(), + vsock_cfg.cid, + vsock_cfg.sock.clone(), + backend, + vsock_cfg.iommu, + ) + .map_err(DeviceManagerError::CreateVirtioVsock)?, )); self.add_migratable_device(Arc::clone(&vsock_device) as Arc>);