Skip to content

Commit 55649df

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 <[email protected]>
1 parent c37849c commit 55649df

File tree

4 files changed

+78
-85
lines changed

4 files changed

+78
-85
lines changed

src/vmm/src/builder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +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,
30-
DeviceRestoreArgs,
29+
AttachDeviceError, DeviceManager, DeviceManagerCreateError, DeviceManagerPersistError, DeviceRestoreArgs
3130
};
3231
use crate::devices::acpi::vmgenid::VmGenIdError;
3332
use crate::devices::virtio::balloon::Balloon;
@@ -423,7 +422,7 @@ pub enum BuildMicrovmFromSnapshotError {
423422
/// Failed to apply VMM secccomp filter: {0}
424423
SeccompFiltersInternal(#[from] crate::seccomp::InstallationError),
425424
/// Failed to restore devices: {0}
426-
RestoreDevices(#[from] DevicePersistError),
425+
RestoreDevices(#[from] DeviceManagerPersistError),
427426
}
428427

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

src/vmm/src/device_manager/mod.rs

Lines changed: 51 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;
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_write_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};
@@ -389,14 +398,51 @@ pub struct DevicesState {
389398
pub pci_state: pci_mngr::PciDevicesState,
390399
}
391400

401+
/// Errors for (de)serialization of the devices.
392402
#[derive(Debug, thiserror::Error, displaydoc::Display)]
393403
pub enum DevicePersistError {
404+
/// Balloon: {0}
405+
Balloon(#[from] BalloonError),
406+
/// Block: {0}
407+
Block(#[from] BlockError),
408+
/// MMIO Device manager: {0}
409+
MmioDeviceManager(#[from] mmio::MmioError),
410+
/// Mmio transport
411+
MmioTransport,
412+
/// PCI Device manager: {0}
413+
PciDeviceManager(#[from] PciManagerError),
414+
/// Bus error: {0}
415+
Bus(#[from] BusError),
416+
#[cfg(target_arch = "aarch64")]
417+
/// Legacy: {0}
418+
Legacy(#[from] std::io::Error),
419+
/// Net: {0}
420+
Net(#[from] NetPersistError),
421+
/// Vsock: {0}
422+
Vsock(#[from] VsockError),
423+
/// VsockUnixBackend: {0}
424+
VsockUnixBackend(#[from] VsockUnixBackendError),
425+
/// MmdsConfig: {0}
426+
MmdsConfig(#[from] MmdsConfigError),
427+
/// Entropy: {0}
428+
Entropy(#[from] EntropyPersistError),
429+
/// Pmem: {0}
430+
Pmem(#[from] PmemPersistError),
431+
/// virtio-mem: {0}
432+
VirtioMem(#[from] VirtioMemPersistError),
433+
/// Could not activate device: {0}
434+
DeviceActivation(#[from] ActivateError),
435+
}
436+
437+
438+
#[derive(Debug, thiserror::Error, displaydoc::Display)]
439+
pub enum DeviceManagerPersistError {
394440
/// Error restoring MMIO devices: {0}
395-
MmioRestore(#[from] persist::DevicePersistError),
441+
MmioRestore(DevicePersistError),
396442
/// Error restoring ACPI devices: {0}
397443
AcpiRestore(#[from] persist::ACPIDeviceManagerRestoreError),
398444
/// Error restoring PCI devices: {0}
399-
PciRestore(#[from] PciManagerError),
445+
PciRestore(DevicePersistError),
400446
/// Error notifying VMGenID device: {0}
401447
VmGenidUpdate(#[from] std::io::Error),
402448
/// Error resetting serial console: {0}
@@ -430,7 +476,7 @@ impl std::fmt::Debug for DeviceRestoreArgs<'_> {
430476
impl<'a> Persist<'a> for DeviceManager {
431477
type State = DevicesState;
432478
type ConstructorArgs = DeviceRestoreArgs<'a>;
433-
type Error = DevicePersistError;
479+
type Error = DeviceManagerPersistError;
434480

435481
fn save(&self) -> Self::State {
436482
DevicesState {
@@ -461,7 +507,7 @@ impl<'a> Persist<'a> for DeviceManager {
461507
vm_resources: constructor_args.vm_resources,
462508
instance_id: constructor_args.instance_id,
463509
};
464-
let mmio_devices = MMIODeviceManager::restore(mmio_ctor_args, &state.mmio_state)?;
510+
let mmio_devices = MMIODeviceManager::restore(mmio_ctor_args, &state.mmio_state).map_err(DeviceManagerPersistError::MmioRestore)?;
465511

466512
// Restore ACPI devices
467513
let acpi_ctor_args = ACPIDeviceManagerConstructorArgs {
@@ -479,7 +525,7 @@ impl<'a> Persist<'a> for DeviceManager {
479525
instance_id: constructor_args.instance_id,
480526
event_manager: constructor_args.event_manager,
481527
};
482-
let pci_devices = PciDevices::restore(pci_ctor_args, &state.pci_state)?;
528+
let pci_devices = PciDevices::restore(pci_ctor_args, &state.pci_state).map_err(DeviceManagerPersistError::PciRestore)?;
483529

484530
let device_manager = DeviceManager {
485531
mmio_devices,

src/vmm/src/device_manager/pci_mngr.rs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use log::{debug, error, warn};
1111
use serde::{Deserialize, Serialize};
1212

1313
use super::persist::MmdsState;
14+
use crate::device_manager::DevicePersistError;
1415
use crate::devices::pci::PciSegment;
1516
use crate::devices::virtio::balloon::Balloon;
1617
use crate::devices::virtio::balloon::persist::{BalloonConstructorArgs, BalloonState};
@@ -37,7 +38,6 @@ use crate::pci::bus::PciRootError;
3738
use crate::resources::VmResources;
3839
use crate::snapshot::Persist;
3940
use crate::vmm_config::memory_hotplug::MemoryHotplugConfig;
40-
use crate::vmm_config::mmds::MmdsConfigError;
4141
use crate::vstate::bus::BusError;
4242
use crate::vstate::interrupts::InterruptError;
4343
use crate::vstate::memory::GuestMemoryMmap;
@@ -65,8 +65,6 @@ pub enum PciManagerError {
6565
VirtioPciDevice(#[from] VirtioPciDeviceError),
6666
/// KVM error: {0}
6767
Kvm(#[from] vmm_sys_util::errno::Error),
68-
/// MMDS error: {0}
69-
Mmds(#[from] MmdsConfigError),
7068
}
7169

7270
impl PciDevices {
@@ -271,7 +269,7 @@ impl<'a> Debug for PciDevicesConstructorArgs<'a> {
271269
impl<'a> Persist<'a> for PciDevices {
272270
type State = PciDevicesState;
273271
type ConstructorArgs = PciDevicesConstructorArgs<'a>;
274-
type Error = PciManagerError;
272+
type Error = DevicePersistError;
275273

276274
fn save(&self) -> Self::State {
277275
let mut state = PciDevicesState::default();
@@ -444,8 +442,7 @@ impl<'a> Persist<'a> for PciDevices {
444442
Balloon::restore(
445443
BalloonConstructorArgs { mem: mem.clone() },
446444
&balloon_state.device_state,
447-
)
448-
.unwrap(),
445+
)?,
449446
));
450447

451448
constructor_args
@@ -460,17 +457,15 @@ impl<'a> Persist<'a> for PciDevices {
460457
&balloon_state.device_id,
461458
&balloon_state.transport_state,
462459
constructor_args.event_manager,
463-
)
464-
.unwrap()
460+
)?
465461
}
466462

467463
for block_state in &state.block_devices {
468464
let device = Arc::new(Mutex::new(
469465
Block::restore(
470466
BlockConstructorArgs { mem: mem.clone() },
471467
&block_state.device_state,
472-
)
473-
.unwrap(),
468+
)?,
474469
));
475470

476471
constructor_args
@@ -485,16 +480,14 @@ impl<'a> Persist<'a> for PciDevices {
485480
&block_state.device_id,
486481
&block_state.transport_state,
487482
constructor_args.event_manager,
488-
)
489-
.unwrap()
483+
)?
490484
}
491485

492486
// Initialize MMDS if MMDS state is included.
493487
if let Some(mmds) = &state.mmds {
494488
constructor_args
495489
.vm_resources
496-
.set_mmds_basic_config(mmds.version, mmds.imds_compat, constructor_args.instance_id)
497-
.unwrap();
490+
.set_mmds_basic_config(mmds.version, mmds.imds_compat, constructor_args.instance_id)?;
498491
} else if state
499492
.net_devices
500493
.iter()
@@ -519,8 +512,7 @@ impl<'a> Persist<'a> for PciDevices {
519512
.cloned(),
520513
},
521514
&net_state.device_state,
522-
)
523-
.unwrap(),
515+
)?,
524516
));
525517

526518
constructor_args
@@ -535,25 +527,23 @@ impl<'a> Persist<'a> for PciDevices {
535527
&net_state.device_id,
536528
&net_state.transport_state,
537529
constructor_args.event_manager,
538-
)
539-
.unwrap()
530+
)?
540531
}
541532

542533
if let Some(vsock_state) = &state.vsock_device {
543534
let ctor_args = VsockUdsConstructorArgs {
544535
cid: vsock_state.device_state.frontend.cid,
545536
};
546537
let backend =
547-
VsockUnixBackend::restore(ctor_args, &vsock_state.device_state.backend).unwrap();
538+
VsockUnixBackend::restore(ctor_args, &vsock_state.device_state.backend)?;
548539
let device = Arc::new(Mutex::new(
549540
Vsock::restore(
550541
VsockConstructorArgs {
551542
mem: mem.clone(),
552543
backend,
553544
},
554545
&vsock_state.device_state.frontend,
555-
)
556-
.unwrap(),
546+
)?,
557547
));
558548

559549
constructor_args
@@ -568,15 +558,14 @@ impl<'a> Persist<'a> for PciDevices {
568558
&vsock_state.device_id,
569559
&vsock_state.transport_state,
570560
constructor_args.event_manager,
571-
)
572-
.unwrap()
561+
)?
573562
}
574563

575564
if let Some(entropy_state) = &state.entropy_device {
576565
let ctor_args = EntropyConstructorArgs { mem: mem.clone() };
577566

578567
let device = Arc::new(Mutex::new(
579-
Entropy::restore(ctor_args, &entropy_state.device_state).unwrap(),
568+
Entropy::restore(ctor_args, &entropy_state.device_state)?,
580569
));
581570

582571
constructor_args
@@ -591,8 +580,7 @@ impl<'a> Persist<'a> for PciDevices {
591580
&entropy_state.device_id,
592581
&entropy_state.transport_state,
593582
constructor_args.event_manager,
594-
)
595-
.unwrap()
583+
)?
596584
}
597585

598586
for pmem_state in &state.pmem_devices {
@@ -603,8 +591,7 @@ impl<'a> Persist<'a> for PciDevices {
603591
vm: constructor_args.vm.as_ref(),
604592
},
605593
&pmem_state.device_state,
606-
)
607-
.unwrap(),
594+
)?,
608595
));
609596

610597
constructor_args
@@ -619,13 +606,12 @@ impl<'a> Persist<'a> for PciDevices {
619606
&pmem_state.device_id,
620607
&pmem_state.transport_state,
621608
constructor_args.event_manager,
622-
)
623-
.unwrap()
609+
)?
624610
}
625611

626612
if let Some(memory_device) = &state.memory_device {
627613
let ctor_args = VirtioMemConstructorArgs::new(Arc::clone(constructor_args.vm));
628-
let device = VirtioMem::restore(ctor_args, &memory_device.device_state).unwrap();
614+
let device = VirtioMem::restore(ctor_args, &memory_device.device_state)?;
629615

630616
constructor_args.vm_resources.memory_hotplug = Some(MemoryHotplugConfig {
631617
total_size_mib: device.total_size_mib(),
@@ -641,8 +627,7 @@ impl<'a> Persist<'a> for PciDevices {
641627
&memory_device.device_id,
642628
&memory_device.transport_state,
643629
constructor_args.event_manager,
644-
)
645-
.unwrap()
630+
)?
646631
}
647632

648633
Ok(pci_devices)

src/vmm/src/device_manager/persist.rs

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,82 +14,45 @@ use super::acpi::ACPIDeviceManager;
1414
use super::mmio::*;
1515
#[cfg(target_arch = "aarch64")]
1616
use crate::arch::DeviceType;
17+
use crate::device_manager::DevicePersistError;
1718
use crate::devices::acpi::vmgenid::{VMGenIDState, VMGenIdConstructorArgs, VmGenId, VmGenIdError};
1819
#[cfg(target_arch = "aarch64")]
1920
use crate::devices::legacy::RTCDevice;
20-
use crate::devices::virtio::ActivateError;
2121
use crate::devices::virtio::balloon::persist::{BalloonConstructorArgs, BalloonState};
22-
use crate::devices::virtio::balloon::{Balloon, BalloonError};
23-
use crate::devices::virtio::block::BlockError;
22+
use crate::devices::virtio::balloon::Balloon;
2423
use crate::devices::virtio::block::device::Block;
2524
use crate::devices::virtio::block::persist::{BlockConstructorArgs, BlockState};
2625
use crate::devices::virtio::device::VirtioDevice;
2726
use crate::devices::virtio::generated::virtio_ids;
2827
use crate::devices::virtio::mem::VirtioMem;
2928
use crate::devices::virtio::mem::persist::{
30-
VirtioMemConstructorArgs, VirtioMemPersistError, VirtioMemState,
29+
VirtioMemConstructorArgs, VirtioMemState,
3130
};
3231
use crate::devices::virtio::net::Net;
3332
use crate::devices::virtio::net::persist::{
34-
NetConstructorArgs, NetPersistError as NetError, NetState,
33+
NetConstructorArgs, NetState,
3534
};
3635
use crate::devices::virtio::persist::{MmioTransportConstructorArgs, MmioTransportState};
3736
use crate::devices::virtio::pmem::device::Pmem;
3837
use crate::devices::virtio::pmem::persist::{
39-
PmemConstructorArgs, PmemPersistError as PmemError, PmemState,
38+
PmemConstructorArgs, PmemState,
4039
};
4140
use crate::devices::virtio::rng::Entropy;
4241
use crate::devices::virtio::rng::persist::{
43-
EntropyConstructorArgs, EntropyPersistError as EntropyError, EntropyState,
42+
EntropyConstructorArgs, EntropyState,
4443
};
4544
use crate::devices::virtio::transport::mmio::{IrqTrigger, MmioTransport};
4645
use crate::devices::virtio::vsock::persist::{
4746
VsockConstructorArgs, VsockState, VsockUdsConstructorArgs,
4847
};
49-
use crate::devices::virtio::vsock::{Vsock, VsockError, VsockUnixBackend, VsockUnixBackendError};
48+
use crate::devices::virtio::vsock::{Vsock, VsockUnixBackend,};
5049
use crate::mmds::data_store::MmdsVersion;
5150
use crate::resources::VmResources;
5251
use crate::snapshot::Persist;
5352
use crate::vmm_config::memory_hotplug::MemoryHotplugConfig;
54-
use crate::vmm_config::mmds::MmdsConfigError;
55-
use crate::vstate::bus::BusError;
5653
use crate::vstate::memory::GuestMemoryMmap;
5754
use crate::{EventManager, Vm};
5855

59-
/// Errors for (de)serialization of the MMIO device manager.
60-
#[derive(Debug, thiserror::Error, displaydoc::Display)]
61-
pub enum DevicePersistError {
62-
/// Balloon: {0}
63-
Balloon(#[from] BalloonError),
64-
/// Block: {0}
65-
Block(#[from] BlockError),
66-
/// Device manager: {0}
67-
DeviceManager(#[from] super::mmio::MmioError),
68-
/// Mmio transport
69-
MmioTransport,
70-
/// Bus error: {0}
71-
Bus(#[from] BusError),
72-
#[cfg(target_arch = "aarch64")]
73-
/// Legacy: {0}
74-
Legacy(#[from] std::io::Error),
75-
/// Net: {0}
76-
Net(#[from] NetError),
77-
/// Vsock: {0}
78-
Vsock(#[from] VsockError),
79-
/// VsockUnixBackend: {0}
80-
VsockUnixBackend(#[from] VsockUnixBackendError),
81-
/// MmdsConfig: {0}
82-
MmdsConfig(#[from] MmdsConfigError),
83-
/// Entropy: {0}
84-
Entropy(#[from] EntropyError),
85-
/// Pmem: {0}
86-
Pmem(#[from] PmemError),
87-
/// virtio-mem: {0}
88-
VirtioMem(#[from] VirtioMemPersistError),
89-
/// Could not activate device: {0}
90-
DeviceActivation(#[from] ActivateError),
91-
}
92-
9356
/// Holds the state of a MMIO VirtIO device
9457
#[derive(Debug, Clone, Serialize, Deserialize)]
9558
pub struct VirtioDeviceState<T> {

0 commit comments

Comments
 (0)