diff --git a/pci/src/vfio.rs b/pci/src/vfio.rs index 0bae6dcd0..bfe54f444 100644 --- a/pci/src/vfio.rs +++ b/pci/src/vfio.rs @@ -465,7 +465,7 @@ impl VfioCommon { let state: Option = snapshot .as_ref() - .map(|s| s.to_versioned_state(VFIO_COMMON_ID)) + .map(|s| s.to_versioned_state()) .transpose() .map_err(|e| { VfioPciError::RetrieveVfioCommonState(anyhow!( diff --git a/virtio-devices/src/transport/pci_device.rs b/virtio-devices/src/transport/pci_device.rs index 2b13204e3..d908c7208 100644 --- a/virtio-devices/src/transport/pci_device.rs +++ b/virtio-devices/src/transport/pci_device.rs @@ -522,7 +522,7 @@ impl VirtioPciDevice { let state: Option = snapshot .as_ref() - .map(|s| s.to_versioned_state(&id)) + .map(|s| s.to_versioned_state()) .transpose() .map_err(|e| { VirtioPciDeviceError::CreateVirtioPciDevice(anyhow!( diff --git a/vm-migration/src/lib.rs b/vm-migration/src/lib.rs index dc5f6a45b..044ae0673 100644 --- a/vm-migration/src/lib.rs +++ b/vm-migration/src/lib.rs @@ -81,9 +81,6 @@ pub trait Pausable { /// allows for easier and forward compatible extensions. #[derive(Clone, Default, Deserialize, Serialize)] pub struct SnapshotDataSection { - /// The section id. - pub id: String, - /// The section serialized snapshot. pub snapshot: Vec, } @@ -94,9 +91,8 @@ impl SnapshotDataSection { where T: Deserialize<'a>, { - serde_json::from_slice(&self.snapshot).map_err(|e| { - MigratableError::Restore(anyhow!("Error deserialising: {} {}", self.id, e)) - }) + serde_json::from_slice(&self.snapshot) + .map_err(|e| MigratableError::Restore(anyhow!("Error deserialising: {}", e))) } /// Generate versioned state @@ -109,39 +105,33 @@ impl SnapshotDataSection { &T::version_map(), VMM_VERSION, ) - .map_err(|e| MigratableError::Restore(anyhow!("Error deserialising: {} {}", self.id, e))) + .map_err(|e| MigratableError::Restore(anyhow!("Error deserialising: {}", e))) } /// Create from state that can be serialized - pub fn new_from_state(id: &str, state: &T) -> Result + pub fn new_from_state(state: &T) -> Result where T: Serialize, { let snapshot = serde_json::to_vec(state) - .map_err(|e| MigratableError::Snapshot(anyhow!("Error serialising: {} {}", id, e)))?; + .map_err(|e| MigratableError::Snapshot(anyhow!("Error serialising: {}", e)))?; - let snapshot_data = SnapshotDataSection { - id: format!("{}-section", id), - snapshot, - }; + let snapshot_data = SnapshotDataSection { snapshot }; Ok(snapshot_data) } /// Create from versioned state - pub fn new_from_versioned_state(id: &str, state: &T) -> Result + pub fn new_from_versioned_state(state: &T) -> Result where T: Versionize + VersionMapped, { let mut snapshot = Vec::new(); state .serialize(&mut snapshot, &T::version_map(), VMM_VERSION) - .map_err(|e| MigratableError::Snapshot(anyhow!("Error serialising: {} {}", id, e)))?; + .map_err(|e| MigratableError::Snapshot(anyhow!("Error serialising: {}", e)))?; - let snapshot_data = SnapshotDataSection { - id: format!("{}-section", id), - snapshot, - }; + let snapshot_data = SnapshotDataSection { snapshot }; Ok(snapshot_data) } @@ -168,7 +158,7 @@ pub struct Snapshot { /// The Snapshottable component's snapshot data. /// A map of snapshot sections, indexed by the section ids. - pub snapshot_data: std::collections::HashMap, + pub snapshot_data: Option, } impl Snapshot { @@ -186,7 +176,7 @@ impl Snapshot { T: Serialize, { let mut snapshot_data = Snapshot::new(id); - snapshot_data.add_data_section(SnapshotDataSection::new_from_state(id, state)?); + snapshot_data.add_data_section(SnapshotDataSection::new_from_state(state)?); Ok(snapshot_data) } @@ -197,7 +187,7 @@ impl Snapshot { T: Versionize + VersionMapped, { let mut snapshot_data = Snapshot::new(id); - snapshot_data.add_data_section(SnapshotDataSection::new_from_versioned_state(id, state)?); + snapshot_data.add_data_section(SnapshotDataSection::new_from_versioned_state(state)?); Ok(snapshot_data) } @@ -210,28 +200,28 @@ impl Snapshot { /// Add a SnapshotDatasection to the component snapshot data. pub fn add_data_section(&mut self, section: SnapshotDataSection) { - self.snapshot_data.insert(section.id.clone(), section); + self.snapshot_data = Some(section); } /// Generate the state data from the snapshot - pub fn to_state<'a, T>(&'a self, id: &str) -> Result + pub fn to_state<'a, T>(&'a self) -> Result where T: Deserialize<'a>, { self.snapshot_data - .get(&format!("{}-section", id)) - .ok_or_else(|| MigratableError::Restore(anyhow!("Missing section for {}", id)))? + .as_ref() + .ok_or_else(|| MigratableError::Restore(anyhow!("Missing snapshot data")))? .to_state() } /// Generate versioned state - pub fn to_versioned_state(&self, id: &str) -> Result + pub fn to_versioned_state(&self) -> Result where T: Versionize + VersionMapped, { self.snapshot_data - .get(&format!("{}-section", id)) - .ok_or_else(|| MigratableError::Restore(anyhow!("Missing section for {}", id)))? + .as_ref() + .ok_or_else(|| MigratableError::Restore(anyhow!("Missing snapshot data")))? .to_versioned_state() } } @@ -249,7 +239,7 @@ where { snapshot .and_then(|s| s.snapshots.get(id).map(|s| *s.clone())) - .map(|s| s.to_versioned_state(id)) + .map(|s| s.to_versioned_state()) .transpose() } diff --git a/vmm/src/cpu.rs b/vmm/src/cpu.rs index e85645c71..d28b53296 100644 --- a/vmm/src/cpu.rs +++ b/vmm/src/cpu.rs @@ -403,10 +403,7 @@ impl Snapshottable for Vcpu { // TODO: The special format of the CPU id can be removed once ready to // break live upgrade. let mut vcpu_snapshot = Snapshot::new(&format!("{:03}", self.id)); - vcpu_snapshot.add_data_section(SnapshotDataSection::new_from_state( - VCPU_SNAPSHOT_ID, - &saved_state, - )?); + vcpu_snapshot.add_data_section(SnapshotDataSection::new_from_state(&saved_state)?); self.saved_state = Some(saved_state); @@ -717,7 +714,7 @@ impl CpuManager { #[cfg(target_arch = "aarch64")] vcpu.init(&self.vm)?; - let state: CpuState = snapshot.to_state(VCPU_SNAPSHOT_ID).map_err(|e| { + let state: CpuState = snapshot.to_state().map_err(|e| { Error::VcpuCreate(anyhow!("Could not get vCPU state from snapshot {:?}", e)) })?; vcpu.vcpu diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 0f0d927b8..1e5671e9f 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -966,7 +966,7 @@ impl DeviceManager { trace_scoped!("DeviceManager::new"); let (device_tree, device_id_cnt) = if let Some(snapshot) = snapshot.as_ref() { - let state: DeviceManagerState = snapshot.to_state(DEVICE_MANAGER_SNAPSHOT_ID).unwrap(); + let state: DeviceManagerState = snapshot.to_state().unwrap(); ( Arc::new(Mutex::new(state.device_tree.clone())), state.device_id_cnt, @@ -1389,7 +1389,7 @@ impl DeviceManager { } let vgic_state = vgic_snapshot - .to_state(&id) + .to_state() .map_err(DeviceManagerError::RestoreGetState)?; let saved_vcpu_states = self.cpu_manager.lock().unwrap().get_saved_states(); interrupt_controller @@ -2150,7 +2150,7 @@ impl DeviceManager { .map_err(DeviceManagerError::EventFd)?, self.force_iommu, snapshot - .map(|s| s.to_versioned_state(&id)) + .map(|s| s.to_versioned_state()) .transpose() .map_err(DeviceManagerError::RestoreGetState)?, ) { @@ -2249,7 +2249,7 @@ impl DeviceManager { .try_clone() .map_err(DeviceManagerError::EventFd)?, snapshot - .map(|s| s.to_versioned_state(&id)) + .map(|s| s.to_versioned_state()) .transpose() .map_err(DeviceManagerError::RestoreGetState)?, ) @@ -2332,7 +2332,7 @@ impl DeviceManager { .map_err(DeviceManagerError::EventFd)?, self.force_iommu, snapshot - .map(|s| s.to_versioned_state(&id)) + .map(|s| s.to_versioned_state()) .transpose() .map_err(DeviceManagerError::RestoreGetState)?, ) { @@ -2349,7 +2349,7 @@ impl DeviceManager { ) } else { let state = snapshot - .map(|s| s.to_versioned_state(&id)) + .map(|s| s.to_versioned_state()) .transpose() .map_err(DeviceManagerError::RestoreGetState)?; @@ -4456,10 +4456,7 @@ impl Snapshottable for DeviceManager { } // Then we store the DeviceManager state. - snapshot.add_data_section(SnapshotDataSection::new_from_state( - DEVICE_MANAGER_SNAPSHOT_ID, - &self.state(), - )?); + snapshot.add_data_section(SnapshotDataSection::new_from_state(&self.state())?); Ok(snapshot) } diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index 46c1f6cc3..dcf802fa2 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -1160,9 +1160,8 @@ impl MemoryManager { let mut memory_file_path = url_to_path(source_url).map_err(Error::Restore)?; memory_file_path.push(String::from(SNAPSHOT_FILENAME)); - let mem_snapshot: MemoryManagerSnapshotData = snapshot - .to_versioned_state(MEMORY_MANAGER_SNAPSHOT_ID) - .map_err(Error::Restore)?; + let mem_snapshot: MemoryManagerSnapshotData = + snapshot.to_versioned_state().map_err(Error::Restore)?; let mm = MemoryManager::new( vm, @@ -2444,7 +2443,6 @@ impl Snapshottable for MemoryManager { self.snapshot_memory_ranges = memory_ranges; memory_manager_snapshot.add_data_section(SnapshotDataSection::new_from_versioned_state( - MEMORY_MANAGER_SNAPSHOT_ID, &self.snapshot_data(), )?); diff --git a/vmm/src/migration.rs b/vmm/src/migration.rs index f9971763e..1906d69fd 100644 --- a/vmm/src/migration.rs +++ b/vmm/src/migration.rs @@ -4,10 +4,7 @@ #[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] use crate::coredump::GuestDebuggableError; -use crate::{ - config::VmConfig, - vm::{VmSnapshot, VM_SNAPSHOT_ID}, -}; +use crate::{config::VmConfig, vm::VmSnapshot}; use anyhow::anyhow; use std::fs::File; use std::io::BufReader; @@ -71,13 +68,8 @@ pub fn recv_vm_state(source_url: &str) -> std::result::Result std::result::Result { - if let Some(vm_section) = snapshot - .snapshot_data - .get(&format!("{}-section", VM_SNAPSHOT_ID)) - { - return serde_json::from_slice(&vm_section.snapshot).map_err(|e| { - MigratableError::Restore(anyhow!("Could not deserialize VM snapshot {}", e)) - }); + if let Some(snapshot_data) = snapshot.snapshot_data.as_ref() { + return snapshot_data.to_state(); } Err(MigratableError::Restore(anyhow!( diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index d3e00133c..a2a60c46e 100644 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -2522,7 +2522,6 @@ impl Snapshottable for Vm { vm_snapshot.add_snapshot(self.device_manager.lock().unwrap().snapshot()?); vm_snapshot.add_data_section(SnapshotDataSection { - id: format!("{}-section", VM_SNAPSHOT_ID), snapshot: vm_snapshot_data, });