From a2ee6816655f3dec4a106f5c1b55a134e40bd567 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Mon, 18 Nov 2019 12:35:05 +0100 Subject: [PATCH] vmm: device_manager: Add an MMIO devices creation routine In order to reduce the DeviceManager's new() complexity, we can move the MMIO devices creation code into its own routine. Fixes: #441 Signed-off-by: Samuel Ortiz --- vmm/src/device_manager.rs | 65 +++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 29f798c54..458cf60f6 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -472,29 +472,13 @@ impl DeviceManager { &interrupt_info, )?; } else if cfg!(feature = "mmio_support") { - #[cfg(feature = "mmio_support")] - { - for (device, _) in virtio_devices { - let mmio_addr = address_manager - .allocator - .lock() - .unwrap() - .allocate_mmio_addresses(None, MMIO_LEN, Some(MMIO_LEN)); - if let Some(addr) = mmio_addr { - DeviceManager::add_virtio_mmio_device( - device, - vm_info.memory, - &address_manager, - vm_info.vm_fd, - &interrupt_info, - addr, - &mut cmdline_additions, - )?; - } else { - error!("Unable to allocate MMIO address!"); - } - } - } + DeviceManager::add_mmio_devices( + vm_info, + &address_manager, + virtio_devices, + &interrupt_info, + &mut cmdline_additions, + )?; } Ok(DeviceManager { @@ -610,6 +594,41 @@ impl DeviceManager { Ok(()) } + #[allow(unused_variables, unused_mut)] + fn add_mmio_devices( + vm_info: &VmInfo, + address_manager: &Arc, + virtio_devices: Vec<(Box, bool)>, + interrupt_info: &InterruptInfo, + mut cmdline_additions: &mut Vec, + ) -> DeviceManagerResult<()> { + #[cfg(feature = "mmio_support")] + { + for (device, _) in virtio_devices { + let mmio_addr = address_manager + .allocator + .lock() + .unwrap() + .allocate_mmio_addresses(None, MMIO_LEN, Some(MMIO_LEN)); + if let Some(addr) = mmio_addr { + DeviceManager::add_virtio_mmio_device( + device, + vm_info.memory, + &address_manager, + vm_info.vm_fd, + &interrupt_info, + addr, + &mut cmdline_additions, + )?; + } else { + error!("Unable to allocate MMIO address!"); + } + } + } + + Ok(()) + } + fn make_ioapic( vm_info: &VmInfo, address_manager: &Arc,