pci: Versionize PCI state

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2021-05-26 15:28:15 +00:00 committed by Sebastien Boeuf
parent cacec04df6
commit 3ffd2cb9be
5 changed files with 23 additions and 19 deletions

5
Cargo.lock generated
View File

@ -642,9 +642,8 @@ dependencies = [
"hypervisor", "hypervisor",
"libc", "libc",
"log", "log",
"serde", "versionize",
"serde_derive", "versionize_derive",
"serde_json",
"vfio-bindings", "vfio-bindings",
"vfio-ioctls", "vfio-ioctls",
"vm-allocator", "vm-allocator",

View File

@ -12,9 +12,8 @@ vfio-ioctls = { git = "https://github.com/rust-vmm/vfio-ioctls", branch = "maste
vmm-sys-util = ">=0.3.1" vmm-sys-util = ">=0.3.1"
libc = "0.2.95" libc = "0.2.95"
log = "0.4.14" log = "0.4.14"
serde = {version = ">=1.0.27", features = ["rc"] } versionize = "0.1.6"
serde_derive = ">=1.0.27" versionize_derive = "0.1.4"
serde_json = ">=1.0.9"
vm-allocator = { path = "../vm-allocator" } vm-allocator = { path = "../vm-allocator" }
vm-device = { path = "../vm-device" } vm-device = { path = "../vm-device" }
vm-memory = "0.5.0" vm-memory = "0.5.0"

View File

@ -7,7 +7,9 @@ use crate::{MsixConfig, PciInterruptPin};
use byteorder::{ByteOrder, LittleEndian}; use byteorder::{ByteOrder, LittleEndian};
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use vm_migration::{MigratableError, Pausable, Snapshot, Snapshottable}; use versionize::{VersionMap, Versionize, VersionizeError, VersionizeResult};
use versionize_derive::Versionize;
use vm_migration::{MigratableError, Pausable, Snapshot, Snapshottable, VersionMapped};
// The number of 32bit registers in the config space, 4096 bytes. // The number of 32bit registers in the config space, 4096 bytes.
const NUM_CONFIGURATION_REGISTERS: usize = 1024; const NUM_CONFIGURATION_REGISTERS: usize = 1024;
@ -276,7 +278,7 @@ fn decode_64_bits_bar_size(bar_size_hi: u32, bar_size_lo: u32) -> Option<u64> {
None None
} }
#[derive(Serialize, Deserialize, Default, Clone, Copy)] #[derive(Default, Clone, Copy, Versionize)]
struct PciBar { struct PciBar {
addr: u32, addr: u32,
size: u32, size: u32,
@ -284,7 +286,7 @@ struct PciBar {
r#type: Option<PciBarRegionType>, r#type: Option<PciBarRegionType>,
} }
#[derive(Serialize, Deserialize)] #[derive(Versionize)]
struct PciConfigurationState { struct PciConfigurationState {
registers: Vec<u32>, registers: Vec<u32>,
writable_bits: Vec<u32>, writable_bits: Vec<u32>,
@ -296,6 +298,8 @@ struct PciConfigurationState {
msix_cap_reg_idx: Option<usize>, msix_cap_reg_idx: Option<usize>,
} }
impl VersionMapped for PciConfigurationState {}
/// Contains the configuration space of a PCI node. /// Contains the configuration space of a PCI node.
/// See the [specification](https://en.wikipedia.org/wiki/PCI_configuration_space). /// See the [specification](https://en.wikipedia.org/wiki/PCI_configuration_space).
/// The configuration space is accessed with DWORD reads and writes from the guest. /// The configuration space is accessed with DWORD reads and writes from the guest.
@ -313,7 +317,7 @@ pub struct PciConfiguration {
} }
/// See pci_regs.h in kernel /// See pci_regs.h in kernel
#[derive(Copy, Clone, PartialEq, Serialize, Deserialize)] #[derive(Copy, Clone, PartialEq, Versionize)]
pub enum PciBarRegionType { pub enum PciBarRegionType {
Memory32BitRegion = 0, Memory32BitRegion = 0,
IoRegion = 0x01, IoRegion = 0x01,
@ -881,11 +885,11 @@ impl Snapshottable for PciConfiguration {
} }
fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> { fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> {
Snapshot::new_from_state(&self.id(), &self.state()) Snapshot::new_from_versioned_state(&self.id(), &self.state())
} }
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
self.set_state(&snapshot.to_state(&self.id())?); self.set_state(&snapshot.to_versioned_state(&self.id())?);
Ok(()) Ok(())
} }
} }

View File

@ -5,8 +5,6 @@
//! Implements pci devices and busses. //! Implements pci devices and busses.
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[macro_use]
extern crate serde_derive;
mod bus; mod bus;
mod configuration; mod configuration;

View File

@ -9,11 +9,13 @@ use byteorder::{ByteOrder, LittleEndian};
use std::io; use std::io;
use std::result; use std::result;
use std::sync::Arc; use std::sync::Arc;
use versionize::{VersionMap, Versionize, VersionizeResult};
use versionize_derive::Versionize;
use vm_device::interrupt::{ use vm_device::interrupt::{
InterruptIndex, InterruptSourceConfig, InterruptSourceGroup, MsiIrqSourceConfig, InterruptIndex, InterruptSourceConfig, InterruptSourceGroup, MsiIrqSourceConfig,
}; };
use vm_memory::ByteValued; use vm_memory::ByteValued;
use vm_migration::{MigratableError, Pausable, Snapshot, Snapshottable}; use vm_migration::{MigratableError, Pausable, Snapshot, Snapshottable, VersionMapped};
const MAX_MSIX_VECTORS_PER_DEVICE: u16 = 2048; const MAX_MSIX_VECTORS_PER_DEVICE: u16 = 2048;
const MSIX_TABLE_ENTRIES_MODULO: u64 = 16; const MSIX_TABLE_ENTRIES_MODULO: u64 = 16;
@ -33,7 +35,7 @@ enum Error {
UpdateInterruptRoute(io::Error), UpdateInterruptRoute(io::Error),
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Versionize)]
pub struct MsixTableEntry { pub struct MsixTableEntry {
pub msg_addr_lo: u32, pub msg_addr_lo: u32,
pub msg_addr_hi: u32, pub msg_addr_hi: u32,
@ -58,7 +60,7 @@ impl Default for MsixTableEntry {
} }
} }
#[derive(Serialize, Deserialize)] #[derive(Versionize)]
struct MsixConfigState { struct MsixConfigState {
table_entries: Vec<MsixTableEntry>, table_entries: Vec<MsixTableEntry>,
pba_entries: Vec<u64>, pba_entries: Vec<u64>,
@ -66,6 +68,8 @@ struct MsixConfigState {
enabled: bool, enabled: bool,
} }
impl VersionMapped for MsixConfigState {}
pub struct MsixConfig { pub struct MsixConfig {
pub table_entries: Vec<MsixTableEntry>, pub table_entries: Vec<MsixTableEntry>,
pub pba_entries: Vec<u64>, pub pba_entries: Vec<u64>,
@ -429,11 +433,11 @@ impl Snapshottable for MsixConfig {
} }
fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> { fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> {
Snapshot::new_from_state(&self.id(), &self.state()) Snapshot::new_from_versioned_state(&self.id(), &self.state())
} }
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
self.set_state(&snapshot.to_state(&self.id())?) self.set_state(&snapshot.to_versioned_state(&self.id())?)
.map_err(|e| { .map_err(|e| {
MigratableError::Restore(anyhow!( MigratableError::Restore(anyhow!(
"Could not restore state for {}: {:?}", "Could not restore state for {}: {:?}",