Skip to content

Commit cfb57da

Browse files
committed
chore(pci): gracefully handle errors in the PCI restore code
All errors were previously unwrapped, differently from MMIO where they were handled. Refactor the code to handle the errors in the same way as MMIO. Signed-off-by: Riccardo Mancini <mancio@amazon.com>
1 parent cc1a614 commit cfb57da

4 files changed

Lines changed: 158 additions & 185 deletions

File tree

src/vmm/src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::cpu_config::templates::{GetCpuTemplate, GetCpuTemplateError, GuestCon
2626
use crate::device_manager;
2727
use crate::device_manager::pci_mngr::PciManagerError;
2828
use crate::device_manager::{
29-
AttachDeviceError, DeviceManager, DeviceManagerCreateError, DevicePersistError,
29+
AttachDeviceError, DeviceManager, DeviceManagerCreateError, DeviceManagerPersistError,
3030
DeviceRestoreArgs,
3131
};
3232
use crate::devices::virtio::balloon::Balloon;
@@ -424,7 +424,7 @@ pub enum BuildMicrovmFromSnapshotError {
424424
/// Failed to apply VMM secccomp filter: {0}
425425
SeccompFiltersInternal(#[from] crate::seccomp::InstallationError),
426426
/// Failed to restore devices: {0}
427-
RestoreDevices(#[from] DevicePersistError),
427+
RestoreDevices(#[from] DeviceManagerPersistError),
428428
}
429429

430430
/// Builds and starts a microVM based on the provided MicrovmState.

src/vmm/src/device_manager/mod.rs

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,20 @@ use crate::devices::legacy::RTCDevice;
3131
use crate::devices::legacy::serial::SerialOut;
3232
use crate::devices::legacy::{IER_RDA_BIT, IER_RDA_OFFSET, SerialDevice};
3333
use crate::devices::pseudo::BootTimer;
34+
use crate::devices::virtio::ActivateError;
35+
use crate::devices::virtio::balloon::BalloonError;
36+
use crate::devices::virtio::block::BlockError;
3437
use crate::devices::virtio::device::{VirtioDevice, VirtioDeviceType};
38+
use crate::devices::virtio::mem::persist::VirtioMemPersistError;
39+
use crate::devices::virtio::net::persist::NetPersistError;
40+
use crate::devices::virtio::pmem::persist::PmemPersistError;
41+
use crate::devices::virtio::rng::persist::EntropyPersistError;
3542
use crate::devices::virtio::transport::mmio::{IrqTrigger, MmioTransport};
43+
use crate::devices::virtio::vsock::{VsockError, VsockUnixBackendError};
3644
use crate::resources::VmResources;
3745
use crate::snapshot::Persist;
3846
use crate::utils::open_file_nonblock;
47+
use crate::vmm_config::mmds::MmdsConfigError;
3948
use crate::vstate::bus::BusError;
4049
use crate::vstate::memory::GuestMemoryMmap;
4150
use crate::{EmulateSerialInitError, EventManager, Vm};
@@ -406,14 +415,51 @@ pub struct DevicesState {
406415
pub pci_state: pci_mngr::PciDevicesState,
407416
}
408417

418+
/// Errors for (de)serialization of the devices.
409419
#[derive(Debug, thiserror::Error, displaydoc::Display)]
410420
pub enum DevicePersistError {
421+
/// Balloon: {0}
422+
Balloon(#[from] BalloonError),
423+
/// Block: {0}
424+
Block(#[from] BlockError),
425+
/// MMIO Device manager: {0}
426+
MmioDeviceManager(#[from] mmio::MmioError),
427+
/// Mmio transport
428+
MmioTransport,
429+
/// PCI Device manager: {0}
430+
PciDeviceManager(#[from] PciManagerError),
431+
/// Bus error: {0}
432+
Bus(#[from] BusError),
433+
#[cfg(target_arch = "aarch64")]
434+
/// Legacy: {0}
435+
Legacy(#[from] std::io::Error),
436+
/// Net: {0}
437+
Net(#[from] NetPersistError),
438+
/// Vsock: {0}
439+
Vsock(#[from] VsockError),
440+
/// VsockUnixBackend: {0}
441+
VsockUnixBackend(#[from] VsockUnixBackendError),
442+
/// MmdsConfig: {0}
443+
MmdsConfig(#[from] MmdsConfigError),
444+
/// Entropy: {0}
445+
Entropy(#[from] EntropyPersistError),
446+
/// Pmem: {0}
447+
Pmem(#[from] PmemPersistError),
448+
/// virtio-mem: {0}
449+
VirtioMem(#[from] VirtioMemPersistError),
450+
/// Could not activate device: {0}
451+
DeviceActivation(#[from] ActivateError),
452+
}
453+
454+
/// Errors for (de)serialization of the device manager.
455+
#[derive(Debug, thiserror::Error, displaydoc::Display)]
456+
pub enum DeviceManagerPersistError {
411457
/// Error restoring MMIO devices: {0}
412-
MmioRestore(#[from] persist::DevicePersistError),
458+
MmioRestore(DevicePersistError),
413459
/// Error restoring ACPI devices: {0}
414460
AcpiRestore(#[from] ACPIDeviceError),
415461
/// Error restoring PCI devices: {0}
416-
PciRestore(#[from] PciManagerError),
462+
PciRestore(DevicePersistError),
417463
/// Error resetting serial console: {0}
418464
SerialRestore(#[from] EmulateSerialInitError),
419465
/// Error inserting device in bus: {0}
@@ -445,7 +491,7 @@ impl std::fmt::Debug for DeviceRestoreArgs<'_> {
445491
impl<'a> Persist<'a> for DeviceManager {
446492
type State = DevicesState;
447493
type ConstructorArgs = DeviceRestoreArgs<'a>;
448-
type Error = DevicePersistError;
494+
type Error = DeviceManagerPersistError;
449495

450496
fn save(&self) -> Self::State {
451497
DevicesState {
@@ -476,7 +522,8 @@ impl<'a> Persist<'a> for DeviceManager {
476522
vm_resources: constructor_args.vm_resources,
477523
instance_id: constructor_args.instance_id,
478524
};
479-
let mmio_devices = MMIODeviceManager::restore(mmio_ctor_args, &state.mmio_state)?;
525+
let mmio_devices = MMIODeviceManager::restore(mmio_ctor_args, &state.mmio_state)
526+
.map_err(DeviceManagerPersistError::MmioRestore)?;
480527

481528
// Restore ACPI devices
482529
let acpi_devices = ACPIDeviceManager::restore(constructor_args.vm, &state.acpi_state)?;
@@ -489,7 +536,8 @@ impl<'a> Persist<'a> for DeviceManager {
489536
instance_id: constructor_args.instance_id,
490537
event_manager: constructor_args.event_manager,
491538
};
492-
let pci_devices = PciDevices::restore(pci_ctor_args, &state.pci_state)?;
539+
let pci_devices = PciDevices::restore(pci_ctor_args, &state.pci_state)
540+
.map_err(DeviceManagerPersistError::PciRestore)?;
493541

494542
let device_manager = DeviceManager {
495543
mmio_devices,

0 commit comments

Comments
 (0)