Skip to content

Commit f62629a

Browse files
committed
vmm: simplify device errors
Merge the device-related errors that DeviceManager might return. This way, we can avoid adding yet another error type for PCI devices and reduce some the variants of StartMicrovmError. Suggested-by: Egor Lazarchuk <[email protected]> Signed-off-by: Babis Chalios <[email protected]>
1 parent 0f79f63 commit f62629a

File tree

2 files changed

+19
-37
lines changed

2 files changed

+19
-37
lines changed

src/vmm/src/builder.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ use crate::arch::{ConfigurationError, configure_system_for_boot, load_kernel};
2222
#[cfg(target_arch = "aarch64")]
2323
use crate::construct_kvm_mpidrs;
2424
use crate::cpu_config::templates::{GetCpuTemplate, GetCpuTemplateError, GuestConfigError};
25-
#[cfg(target_arch = "aarch64")]
26-
use crate::device_manager::AttachLegacyMmioDeviceError;
25+
#[cfg(target_arch = "x86_64")]
26+
use crate::device_manager;
2727
use crate::device_manager::pci_mngr::PciManagerError;
2828
use crate::device_manager::{
29-
AttachMmioDeviceError, AttachVmgenidError, DeviceManager, DeviceManagerCreateError,
30-
DevicePersistError, DeviceRestoreArgs,
29+
AttachDeviceError, DeviceManager, DeviceManagerCreateError, DevicePersistError,
30+
DeviceRestoreArgs,
3131
};
3232
use crate::devices::acpi::vmgenid::VmGenIdError;
3333
use crate::devices::virtio::balloon::Balloon;
@@ -48,18 +48,15 @@ use crate::vstate::kvm::{Kvm, KvmError};
4848
use crate::vstate::memory::GuestRegionMmap;
4949
use crate::vstate::vcpu::VcpuError;
5050
use crate::vstate::vm::{Vm, VmError};
51-
use crate::{EventManager, Vmm, VmmError, device_manager};
51+
use crate::{EventManager, Vmm, VmmError};
5252

5353
/// Errors associated with starting the instance.
5454
#[derive(Debug, thiserror::Error, displaydoc::Display)]
5555
pub enum StartMicrovmError {
5656
/// Unable to attach block device to Vmm: {0}
5757
AttachBlockDevice(io::Error),
58-
/// Unable to attach the VMGenID device: {0}
59-
AttachVmgenidDevice(#[from] AttachVmgenidError),
60-
#[cfg(target_arch = "aarch64")]
61-
/// Unable to attach legacy MMIO devices: {0}
62-
AttachLegacyDevices(#[from] AttachLegacyMmioDeviceError),
58+
/// Could not attach device: {0}
59+
AttachDevice(#[from] AttachDeviceError),
6360
/// System configuration error: {0}
6461
ConfigureSystem(#[from] ConfigurationError),
6562
/// Failed to create device manager: {0}
@@ -104,8 +101,6 @@ pub enum StartMicrovmError {
104101
NetDeviceNotConfigured,
105102
/// Cannot open the block device backing file: {0}
106103
OpenBlockDevice(io::Error),
107-
/// Cannot initialize a MMIO Device or add a device to the MMIO Bus or cmdline: {0}
108-
RegisterMmioDevice(#[from] device_manager::AttachMmioDeviceError),
109104
/// Cannot restore microvm state: {0}
110105
RestoreMicrovmState(MicrovmStateError),
111106
/// Cannot set vm resources: {0}
@@ -563,7 +558,7 @@ fn attach_entropy_device(
563558
cmdline: &mut LoaderKernelCmdline,
564559
entropy_device: &Arc<Mutex<Entropy>>,
565560
event_manager: &mut EventManager,
566-
) -> Result<(), AttachMmioDeviceError> {
561+
) -> Result<(), AttachDeviceError> {
567562
let id = entropy_device
568563
.lock()
569564
.expect("Poisoned lock")
@@ -625,7 +620,7 @@ fn attach_unixsock_vsock_device(
625620
cmdline: &mut LoaderKernelCmdline,
626621
unix_vsock: &Arc<Mutex<Vsock<VsockUnixBackend>>>,
627622
event_manager: &mut EventManager,
628-
) -> Result<(), AttachMmioDeviceError> {
623+
) -> Result<(), AttachDeviceError> {
629624
let id = String::from(unix_vsock.lock().expect("Poisoned lock").id());
630625
event_manager.add_subscriber(unix_vsock.clone());
631626
// The device mutex mustn't be locked here otherwise it will deadlock.
@@ -638,7 +633,7 @@ fn attach_balloon_device(
638633
cmdline: &mut LoaderKernelCmdline,
639634
balloon: &Arc<Mutex<Balloon>>,
640635
event_manager: &mut EventManager,
641-
) -> Result<(), AttachMmioDeviceError> {
636+
) -> Result<(), AttachDeviceError> {
642637
let id = String::from(balloon.lock().expect("Poisoned lock").id());
643638
event_manager.add_subscriber(balloon.clone());
644639
// The device mutex mustn't be locked here otherwise it will deadlock.

src/vmm/src/device_manager/mod.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,21 @@ pub enum DeviceManagerCreateError {
6464

6565
#[derive(Debug, thiserror::Error, displaydoc::Display)]
6666
/// Error while attaching a VirtIO device
67-
pub enum AttachMmioDeviceError {
67+
pub enum AttachDeviceError {
6868
/// MMIO transport error: {0}
6969
MmioTransport(#[from] MmioError),
7070
/// Error inserting device in bus: {0}
7171
Bus(#[from] vm_device::BusError),
72-
}
73-
74-
#[derive(Debug, thiserror::Error, displaydoc::Display)]
75-
/// Error while attaching the VMGenID device
76-
pub enum AttachVmgenidError {
7772
/// Error creating VMGenID device: {0}
7873
CreateVmGenID(#[from] VmGenIdError),
7974
/// Error while registering VMGenID with KVM: {0}
8075
AttachVmGenID(#[from] kvm_ioctls::Error),
81-
}
82-
83-
#[cfg(target_arch = "aarch64")]
84-
#[derive(Debug, thiserror::Error, displaydoc::Display)]
85-
/// Error while attaching the VMGenID device
86-
pub enum AttachLegacyMmioDeviceError {
76+
#[cfg(target_arch = "aarch64")]
8777
/// Cmdline error
8878
Cmdline,
79+
#[cfg(target_arch = "aarch64")]
8980
/// Error creating serial device: {0}
9081
CreateSerial(#[from] std::io::Error),
91-
/// Error registering device: {0}
92-
RegisterMMIODevice(#[from] MmioError),
93-
/// Error inserting device in the Bus: {0}
94-
Bus(#[from] vm_device::BusError),
9582
}
9683

9784
#[derive(Debug)]
@@ -181,7 +168,7 @@ impl DeviceManager {
181168
device: Arc<Mutex<T>>,
182169
cmdline: &mut Cmdline,
183170
is_vhost_user: bool,
184-
) -> Result<(), AttachMmioDeviceError> {
171+
) -> Result<(), AttachDeviceError> {
185172
let interrupt = Arc::new(IrqTrigger::new());
186173
// The device mutex mustn't be locked here otherwise it will deadlock.
187174
let device =
@@ -201,7 +188,7 @@ impl DeviceManager {
201188
pub(crate) fn attach_boot_timer_device(
202189
&mut self,
203190
request_ts: TimestampUs,
204-
) -> Result<(), AttachMmioDeviceError> {
191+
) -> Result<(), AttachDeviceError> {
205192
let boot_timer = Arc::new(Mutex::new(BootTimer::new(request_ts)));
206193

207194
self.mmio_devices
@@ -214,7 +201,7 @@ impl DeviceManager {
214201
&mut self,
215202
mem: &GuestMemoryMmap,
216203
vm: &Vm,
217-
) -> Result<(), AttachVmgenidError> {
204+
) -> Result<(), AttachDeviceError> {
218205
let vmgenid = VmGenId::new(mem, &self.resource_allocator)?;
219206
self.acpi_devices.attach_vmgenid(vmgenid, vm)?;
220207
Ok(())
@@ -226,13 +213,13 @@ impl DeviceManager {
226213
vm: &Vm,
227214
event_manager: &mut EventManager,
228215
cmdline: &mut Cmdline,
229-
) -> Result<(), AttachLegacyMmioDeviceError> {
216+
) -> Result<(), AttachDeviceError> {
230217
// Serial device setup.
231218
let cmdline_contains_console = cmdline
232219
.as_cstring()
233-
.map_err(|_| AttachLegacyMmioDeviceError::Cmdline)?
220+
.map_err(|_| AttachDeviceError::Cmdline)?
234221
.into_string()
235-
.map_err(|_| AttachLegacyMmioDeviceError::Cmdline)?
222+
.map_err(|_| AttachDeviceError::Cmdline)?
236223
.contains("console=");
237224

238225
if cmdline_contains_console {

0 commit comments

Comments
 (0)