Skip to content

Commit 0093165

Browse files
committed
fix: Refactor vmm builder code to simplify logic
remove cfg_attr and extract create_vcpus from create_vmm_and_vcpus Signed-off-by: tommady <[email protected]>
1 parent 2b5ad8b commit 0093165

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

src/vmm/src/builder.rs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ impl std::convert::From<linux_loader::cmdline::Error> for StartMicrovmError {
148148
}
149149
}
150150

151-
#[cfg_attr(target_arch = "aarch64", allow(unused))]
152151
fn create_vmm_and_vcpus(
153152
instance_info: &InstanceInfo,
154153
event_manager: &mut EventManager,
@@ -179,17 +178,8 @@ fn create_vmm_and_vcpus(
179178

180179
// Instantiate ACPI device manager.
181180
let acpi_device_manager = ACPIDeviceManager::new();
182-
183-
// For x86_64 we need to create the interrupt controller before calling `KVM_CREATE_VCPUS`
184-
// while on aarch64 we need to do it the other way around.
185181
#[cfg(target_arch = "x86_64")]
186-
let (vcpus, pio_device_manager) = {
187-
setup_interrupt_controller(&mut vm)?;
188-
let vcpus = create_vcpus(&vm, vm_config.vcpu_count, &vcpus_exit_evt).map_err(Internal)?;
189-
190-
// Make stdout non blocking.
191-
set_stdout_nonblocking();
192-
182+
let pio_device_manager = {
193183
// Serial device setup.
194184
let serial_device =
195185
setup_serial_device(event_manager, std::io::stdin(), io::stdout()).map_err(Internal)?;
@@ -208,19 +198,10 @@ fn create_vmm_and_vcpus(
208198
pio_dev_mgr
209199
};
210200

211-
(vcpus, pio_device_manager)
201+
pio_device_manager
212202
};
213203

214-
// On aarch64, the vCPUs need to be created (i.e call KVM_CREATE_VCPU) before setting up the
215-
// IRQ chip because the `KVM_CREATE_VCPU` ioctl will return error if the IRQCHIP
216-
// was already initialized.
217-
// Search for `kvm_arch_vcpu_create` in arch/arm/kvm/arm.c.
218-
#[cfg(target_arch = "aarch64")]
219-
let vcpus = {
220-
let vcpus = create_vcpus(&vm, vcpu_count, &vcpus_exit_evt).map_err(Internal)?;
221-
setup_interrupt_controller(&mut vm, vcpu_count)?;
222-
vcpus
223-
};
204+
let vcpus = create_vcpus(&mut vm, vm_config.vcpu_count, &vcpus_exit_evt).map_err(Internal)?;
224205

225206
let vmm = Vmm {
226207
events_observer: Some(std::io::stdin()),
@@ -659,18 +640,14 @@ where
659640

660641
/// Sets up the irqchip for a x86_64 microVM.
661642
#[cfg(target_arch = "x86_64")]
662-
pub fn setup_interrupt_controller(vm: &mut Vm) -> Result<(), StartMicrovmError> {
663-
vm.setup_irqchip()
664-
.map_err(VmmError::Vm)
665-
.map_err(StartMicrovmError::Internal)
643+
pub fn setup_interrupt_controller(vm: &mut Vm) -> Result<(), VmmError> {
644+
vm.setup_irqchip().map_err(VmmError::Vm)
666645
}
667646

668647
/// Sets up the irqchip for a aarch64 microVM.
669648
#[cfg(target_arch = "aarch64")]
670-
pub fn setup_interrupt_controller(vm: &mut Vm, vcpu_count: u8) -> Result<(), StartMicrovmError> {
671-
vm.setup_irqchip(vcpu_count)
672-
.map_err(VmmError::Vm)
673-
.map_err(StartMicrovmError::Internal)
649+
pub fn setup_interrupt_controller(vm: &mut Vm, vcpu_count: u8) -> Result<(), VmmError> {
650+
vm.setup_irqchip(vcpu_count).map_err(VmmError::Vm)
674651
}
675652

676653
/// Sets up the serial device.
@@ -730,13 +707,39 @@ fn attach_legacy_devices_aarch64(
730707
.map_err(VmmError::RegisterMMIODevice)
731708
}
732709

733-
fn create_vcpus(vm: &Vm, vcpu_count: u8, exit_evt: &EventFd) -> Result<Vec<Vcpu>, VmmError> {
710+
// For x86_64 we need to create the interrupt controller before calling `KVM_CREATE_VCPUS`
711+
// while on aarch64 we need to do it the other way around.
712+
#[cfg(target_arch = "x86_64")]
713+
fn create_vcpus(vm: &mut Vm, vcpu_count: u8, exit_evt: &EventFd) -> Result<Vec<Vcpu>, VmmError> {
714+
setup_interrupt_controller(vm)?;
715+
716+
let mut vcpus = Vec::with_capacity(vcpu_count as usize);
717+
for cpu_idx in 0..vcpu_count {
718+
let exit_evt = exit_evt.try_clone().map_err(VmmError::EventFd)?;
719+
let vcpu = Vcpu::new(cpu_idx, vm, exit_evt).map_err(VmmError::VcpuCreate)?;
720+
vcpus.push(vcpu);
721+
}
722+
// Make stdout non blocking.
723+
set_stdout_nonblocking();
724+
725+
Ok(vcpus)
726+
}
727+
728+
// On aarch64, the vCPUs need to be created (i.e call KVM_CREATE_VCPU) before setting up the
729+
// IRQ chip because the `KVM_CREATE_VCPU` ioctl will return error if the IRQCHIP
730+
// was already initialized.
731+
// Search for `kvm_arch_vcpu_create` in arch/arm/kvm/arm.c.
732+
#[cfg(target_arch = "aarch64")]
733+
fn create_vcpus(vm: &mut Vm, vcpu_count: u8, exit_evt: &EventFd) -> Result<Vec<Vcpu>, VmmError> {
734734
let mut vcpus = Vec::with_capacity(vcpu_count as usize);
735735
for cpu_idx in 0..vcpu_count {
736736
let exit_evt = exit_evt.try_clone().map_err(VmmError::EventFd)?;
737737
let vcpu = Vcpu::new(cpu_idx, vm, exit_evt).map_err(VmmError::VcpuCreate)?;
738738
vcpus.push(vcpu);
739739
}
740+
741+
setup_interrupt_controller(vm, vcpu_count)?;
742+
740743
Ok(vcpus)
741744
}
742745

@@ -1362,7 +1365,7 @@ pub mod tests {
13621365
#[cfg(target_arch = "x86_64")]
13631366
setup_interrupt_controller(&mut vm).unwrap();
13641367

1365-
let vcpu_vec = create_vcpus(&vm, vcpu_count, &evfd).unwrap();
1368+
let vcpu_vec = create_vcpus(&mut vm, vcpu_count, &evfd).unwrap();
13661369
assert_eq!(vcpu_vec.len(), vcpu_count as usize);
13671370
}
13681371

0 commit comments

Comments
 (0)