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 <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-04-27 13:49:54 +02:00 committed by Rob Bradford
parent 9b53044aae
commit ec5ff395cf
3 changed files with 22 additions and 11 deletions

View File

@ -376,6 +376,7 @@ where
/// Virtio device exposing virtual socket to the guest.
pub struct Vsock<B: VsockBackend> {
id: String,
cid: u64,
backend: Arc<RwLock<B>>,
kill_evt: Option<EventFd>,
@ -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<Vsock<B>> {
pub fn new(id: String, cid: u64, backend: B, iommu: bool) -> io::Result<Vsock<B>> {
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<B> Snapshottable for Vsock<B> where B: VsockBackend + Sync + 'static {}
impl<B> Snapshottable for Vsock<B>
where
B: VsockBackend + Sync + 'static,
{
fn id(&self) -> String {
self.id.clone()
}
}
impl<B> Transportable for Vsock<B> where B: VsockBackend + Sync + 'static {}
impl<B> Migratable for Vsock<B> where B: VsockBackend + Sync + 'static {}

View File

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

View File

@ -1690,9 +1690,13 @@ impl DeviceManager {
&mut self,
vsock_cfg: &mut VsockConfig,
) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option<String>)> {
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<Mutex<dyn Migratable>>;
let id = migratable.lock().unwrap().id();
self.migratable_devices.push((id, migratable));
self.add_migratable_device(Arc::clone(&vsock_device) as Arc<Mutex<dyn Migratable>>);
Ok((
Arc::clone(&vsock_device) as VirtioDeviceArc,
vsock_cfg.iommu,
vsock_cfg.id.clone(),
Some(id),
))
}