From f664cddec94a90db315ac12ddc208579bea2e256 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Mon, 23 Mar 2020 16:20:57 +0000 Subject: [PATCH] vmm: Add support for adding network devices to the VM The persistent memory will be hotplugged via DeviceManager and saved in the config for later use. Signed-off-by: Rob Bradford --- vmm/src/vm.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 95731f0aa..8e9d268c4 100755 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -26,7 +26,7 @@ extern crate vm_allocator; extern crate vm_memory; extern crate vm_virtio; -use crate::config::{DeviceConfig, DiskConfig, HotplugMethod, PmemConfig, VmConfig}; +use crate::config::{DeviceConfig, DiskConfig, HotplugMethod, NetConfig, PmemConfig, VmConfig}; use crate::cpu; use crate::device_manager::{get_win_size, Console, DeviceManager, DeviceManagerError}; use crate::memory_manager::{get_host_cpu_phys_bits, Error as MemoryManagerError, MemoryManager}; @@ -720,6 +720,39 @@ impl Vm { } } + pub fn add_net(&mut self, mut _net_cfg: NetConfig) -> Result<()> { + if cfg!(feature = "pci_support") { + #[cfg(feature = "pci_support")] + { + self.device_manager + .lock() + .unwrap() + .add_net(&mut _net_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(); + if let Some(net) = config.net.as_mut() { + net.push(_net_cfg); + } else { + config.net = Some(vec![_net_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 {