allow more flexible access to virtqueue chain

This commit is contained in:
Bruce Leidl 2019-09-11 15:55:38 -04:00
parent f893d5a316
commit 9cca10a2c0
2 changed files with 49 additions and 7 deletions

View File

@ -4,6 +4,7 @@ use std::io::{self,Read,Write};
use crate::memory::GuestRam; use crate::memory::GuestRam;
use super::VirtQueue; use super::VirtQueue;
use super::vring::Descriptor; use super::vring::Descriptor;
use byteorder::{WriteBytesExt, LittleEndian, ReadBytesExt};
pub struct Chain { pub struct Chain {
@ -127,7 +128,7 @@ impl Chain {
/// load next descriptor if `current` descriptor /// load next descriptor if `current` descriptor
/// has been fully consumed. /// has been fully consumed.
/// ///
fn inc_offset(&mut self, sz: usize) { pub fn inc_offset(&mut self, sz: usize) {
self.offset += sz; self.offset += sz;
if self.offset >= self.current_size() { if self.offset >= self.current_size() {
self.load_next_descriptor(); self.load_next_descriptor();
@ -225,13 +226,54 @@ impl Chain {
res res
} }
/* pub fn current_write_slice(&self) -> &mut [u8] {
pub fn copy_to_writer<W: Write+Sized>(&mut self, w: W, size: usize) -> io::Result<usize> { match self.current {
unimplemented!() Some(d) if d.is_write() && d.remaining(self.offset) > 0 => {
let size = d.remaining(self.offset);
self.memory.mut_slice(d.addr + self.offset as u64, size).unwrap_or(&mut [])
},
_ => &mut [],
}
}
pub fn current_read_slice(&self) -> &[u8] {
match self.current {
Some(d) if !d.is_write() && d.remaining(self.offset) > 0 => {
let size = d.remaining(self.offset);
self.memory.slice(d.addr + self.offset as u64, size).unwrap_or(&[])
},
_ => &[],
}
} }
*/
pub fn w8(&mut self, n: u8) -> io::Result<()> {
self.write_u8(n)
}
#[allow(unused)]
pub fn w16(&mut self, n: u16) -> io::Result<()> {
self.write_u16::<LittleEndian>(n)
}
pub fn w32(&mut self, n: u32) -> io::Result<()> {
self.write_u32::<LittleEndian>(n)
}
pub fn w64(&mut self, n: u64) -> io::Result<()> {
self.write_u64::<LittleEndian>(n)
}
#[allow(unused)]
pub fn r16(&mut self) -> io::Result<u16> {
self.read_u16::<LittleEndian>()
}
pub fn r32(&mut self) -> io::Result<u32> {
self.read_u32::<LittleEndian>()
}
pub fn r64(&mut self) -> io::Result<u64> {
self.read_u64::<LittleEndian>()
}
} }
impl Drop for Chain { impl Drop for Chain {

View File

@ -345,7 +345,7 @@ impl Descriptor {
self.has_flag(VRING_DESC_F_INDIRECT) self.has_flag(VRING_DESC_F_INDIRECT)
} }
fn remaining(&self, offset: usize) -> usize { pub fn remaining(&self, offset: usize) -> usize {
if offset >= self.len as usize { if offset >= self.len as usize {
0 0
} else { } else {