From 99422324a71086d0a521d0370a2186625d48a7eb Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 28 Apr 2020 16:02:46 +0100 Subject: [PATCH] vmm: vm: Add "add_vsock()" Add the vsock device to the device manager and patch the config to add the new vsock device. Signed-off-by: Rob Bradford --- vmm/src/vm.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 3d6a03b05..a41d2dad0 100755 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -27,7 +27,7 @@ extern crate vm_virtio; use crate::config::{ DeviceConfig, DiskConfig, FsConfig, HotplugMethod, NetConfig, PmemConfig, ValidationError, - VmConfig, + VmConfig, VsockConfig, }; use crate::cpu; use crate::device_manager::{get_win_size, Console, DeviceManager, DeviceManagerError}; @@ -197,6 +197,9 @@ pub enum Error { /// Failed to validate config ConfigValidation(ValidationError), + + /// No more that one virtio-vsock device + TooManyVsockDevices, } pub type Result = result::Result; @@ -884,6 +887,39 @@ impl Vm { } } + pub fn add_vsock(&mut self, mut _vsock_cfg: VsockConfig) -> Result<()> { + if cfg!(feature = "pci_support") { + #[cfg(feature = "pci_support")] + { + if self.config.lock().unwrap().vsock.is_some() { + return Err(Error::TooManyVsockDevices); + } + + self.device_manager + .lock() + .unwrap() + .add_vsock(&mut _vsock_cfg) + .map_err(Error::DeviceManager)?; + + // Update VmConfig by adding the new device. This is important to + // ensure the device would be created in case of a reboot. + { + let mut config = self.config.lock().unwrap(); + config.vsock = Some(_vsock_cfg); + } + + self.device_manager + .lock() + .unwrap() + .notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED) + .map_err(Error::DeviceManager)?; + } + Ok(()) + } else { + Err(Error::NoPciSupport) + } + } + fn os_signal_handler(signals: Signals, console_input_clone: Arc, on_tty: bool) { for signal in signals.forever() { match signal {