virtio-devices, vm-virtio: Refactor queue state saving/restoring

Use a separate QueueState structure which can be versioned.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2021-06-01 09:30:03 +00:00 committed by Sebastien Boeuf
parent 831dff8b4e
commit 57f532a760
5 changed files with 29 additions and 22 deletions

3
Cargo.lock generated
View File

@ -1255,9 +1255,6 @@ name = "vm-virtio"
version = "0.1.0"
dependencies = [
"log",
"serde",
"serde_derive",
"serde_json",
"virtio-bindings",
"vm-memory",
]

View File

@ -260,10 +260,21 @@ const NOTIFY_OFF_MULTIPLIER: u32 = 4; // A dword per notification address.
const VIRTIO_PCI_VENDOR_ID: u16 = 0x1af4;
const VIRTIO_PCI_DEVICE_ID_BASE: u16 = 0x1040; // Add to device type to get device ID.
#[derive(Serialize, Deserialize)]
struct QueueState {
max_size: u16,
size: u16,
ready: bool,
vector: u16,
desc_table: u64,
avail_ring: u64,
used_ring: u64,
}
#[derive(Serialize, Deserialize)]
struct VirtioPciDeviceState {
device_activated: bool,
queues: Vec<Queue>,
queues: Vec<QueueState>,
interrupt_status: usize,
}
@ -451,7 +462,19 @@ impl VirtioPciDevice {
VirtioPciDeviceState {
device_activated: self.device_activated.load(Ordering::Acquire),
interrupt_status: self.interrupt_status.load(Ordering::Acquire),
queues: self.queues.clone(),
queues: self
.queues
.iter()
.map(|q| QueueState {
max_size: q.max_size,
size: q.size,
ready: q.ready,
vector: q.vector,
desc_table: q.desc_table.0,
avail_ring: q.avail_ring.0,
used_ring: q.used_ring.0,
})
.collect(),
}
}
@ -469,9 +492,9 @@ impl VirtioPciDevice {
queue.size = state.queues[i].size;
queue.ready = state.queues[i].ready;
queue.vector = state.queues[i].vector;
queue.desc_table = state.queues[i].desc_table;
queue.avail_ring = state.queues[i].avail_ring;
queue.used_ring = state.queues[i].used_ring;
queue.desc_table = GuestAddress(state.queues[i].desc_table);
queue.avail_ring = GuestAddress(state.queues[i].avail_ring);
queue.used_ring = GuestAddress(state.queues[i].used_ring);
queue.next_avail = Wrapping(
queue
.used_index_from_memory(&mem)

View File

@ -9,8 +9,5 @@ default = []
[dependencies]
log = "0.4.14"
serde = ">=1.0.27"
serde_derive = ">=1.0.27"
serde_json = ">=1.0.9"
virtio-bindings = { version = "0.1", features = ["virtio-v5_0_0"]}
vm-memory = { version = "0.5.0", features = ["backend-mmap", "backend-atomic"] }

View File

@ -12,8 +12,6 @@
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
use std::fmt;

View File

@ -384,11 +384,7 @@ impl<'a, 'b> Iterator for AvailIter<'a, 'b> {
}
}
#[derive(Serialize, Deserialize)]
#[serde(remote = "GuestAddress")]
struct GuestAddressDef(pub u64);
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone)]
/// A virtio queue's parameters.
pub struct Queue {
/// The maximal size in elements offered by the device
@ -403,22 +399,18 @@ pub struct Queue {
/// Interrupt vector index of the queue
pub vector: u16,
#[serde(with = "GuestAddressDef")]
/// Guest physical address of the descriptor table
pub desc_table: GuestAddress,
#[serde(with = "GuestAddressDef")]
/// Guest physical address of the available ring
pub avail_ring: GuestAddress,
#[serde(with = "GuestAddressDef")]
/// Guest physical address of the used ring
pub used_ring: GuestAddress,
pub next_avail: Wrapping<u16>,
pub next_used: Wrapping<u16>,
#[serde(skip)]
pub iommu_mapping_cb: Option<Arc<VirtioIommuRemapping>>,
/// VIRTIO_F_RING_EVENT_IDX negotiated