vm-virtio: Add helpers to update queue indexes

In anticipation for adding snapshot/restore support to virtio devices,
this commit introduces two new helpers updating the available and used
indexes of a queue, relying on the guest memory.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-04-16 14:11:37 +02:00
parent fd45e94510
commit 12fec55064

View File

@ -648,6 +648,46 @@ impl Queue {
pub fn go_to_previous_position(&mut self) {
self.next_avail -= Wrapping(1);
}
/// Get latest index from available ring.
pub fn update_avail_index_from_memory(&mut self, mem: &GuestMemoryMmap) {
let index_addr = match mem.checked_offset(self.avail_ring, 2) {
Some(ret) => ret,
None => {
error!("Invalid offset 0x{:x}", self.avail_ring.raw_value() + 2);
return;
}
};
let next_avail: u16 = match mem.read_obj::<u16>(index_addr) {
Ok(ret) => ret,
Err(e) => {
error!("Couldn't read `idx` field from memory: {}", e);
return;
}
};
self.next_avail = Wrapping(next_avail);
}
/// Get latest index from used ring.
pub fn update_used_index_from_memory(&mut self, mem: &GuestMemoryMmap) {
let index_addr = match mem.checked_offset(self.used_ring, 2) {
Some(ret) => ret,
None => {
error!("Invalid offset 0x{:x}", self.used_ring.raw_value() + 2);
return;
}
};
let next_used: u16 = match mem.read_obj::<u16>(index_addr) {
Ok(ret) => ret,
Err(e) => {
error!("Couldn't read `idx` field from memory: {}", e);
return;
}
};
self.next_used = Wrapping(next_used);
}
}
#[cfg(test)]