vm-virtio, vmm: Delete unix socket on shutdown

It's not possible to call UnixListener::Bind() on an existing file so
unlink the created socket when shutting down the Vsock device.

This will allow the VM to be rebooted with a vsock device.

Fixes: #1083

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-05-05 10:54:38 +01:00 committed by Sebastien Boeuf
parent 5109f914eb
commit fec97e0586
3 changed files with 31 additions and 4 deletions

View File

@ -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<B: VsockBackend> {
interrupt_cb: Option<Arc<dyn VirtioInterrupt>>,
epoll_threads: Option<Vec<thread::JoinHandle<result::Result<(), DeviceError>>>>,
paused: Arc<AtomicBool>,
path: PathBuf,
}
impl<B> Vsock<B>
@ -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<Vsock<B>> {
pub fn new(
id: String,
cid: u64,
path: PathBuf,
backend: B,
iommu: bool,
) -> io::Result<Vsock<B>> {
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);

View File

@ -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(),
}
}

View File

@ -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<Mutex<dyn Migratable>>);