diff options
author | Chirantan Ekbote <chirantan@chromium.org> | 2020-05-19 19:40:20 +0900 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-05-28 07:14:58 +0000 |
commit | 1a9f2a5454481a511257625f985d503c45fd8246 (patch) | |
tree | 0b03744ca2137adb7fac308939fd068d4fd6ea63 /devices | |
parent | 247134fe68f32f45f7a92a7f3181c0a74f713dec (diff) | |
download | crosvm-1a9f2a5454481a511257625f985d503c45fd8246.tar crosvm-1a9f2a5454481a511257625f985d503c45fd8246.tar.gz crosvm-1a9f2a5454481a511257625f985d503c45fd8246.tar.bz2 crosvm-1a9f2a5454481a511257625f985d503c45fd8246.tar.lz crosvm-1a9f2a5454481a511257625f985d503c45fd8246.tar.xz crosvm-1a9f2a5454481a511257625f985d503c45fd8246.tar.zst crosvm-1a9f2a5454481a511257625f985d503c45fd8246.zip |
sys_util: Refactor IntoIovec
The original stated purpose of this trait was to reduce memory allocations but having the `into_iovec` method return a Vec kind of defeats that purpose. Refactor the trait so that it can either convert a T into an iovec or convert a &[T] into a &[iovec]. Implement the trait for VolatileSlice, IoSlice, and IoSliceMut and update all the callers. BUG=none TEST=unit tests Cq-Depend: chromium:2210272 Change-Id: I9d0d617a23030d241d50411f4a5a16e7cba4bcee Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2208527 Reviewed-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org> Commit-Queue: Chirantan Ekbote <chirantan@chromium.org> Tested-by: Chirantan Ekbote <chirantan@chromium.org>
Diffstat (limited to 'devices')
-rw-r--r-- | devices/src/virtio/descriptor_utils.rs | 68 | ||||
-rw-r--r-- | devices/src/virtio/wl.rs | 8 |
2 files changed, 17 insertions, 59 deletions
diff --git a/devices/src/virtio/descriptor_utils.rs b/devices/src/virtio/descriptor_utils.rs index fcf5793..c878a45 100644 --- a/devices/src/virtio/descriptor_utils.rs +++ b/devices/src/virtio/descriptor_utils.rs @@ -14,9 +14,7 @@ use std::ptr::copy_nonoverlapping; use std::result; use data_model::{DataInit, Le16, Le32, Le64, VolatileMemoryError, VolatileSlice}; -use sys_util::{ - FileReadWriteAtVolatile, FileReadWriteVolatile, GuestAddress, GuestMemory, IntoIovec, -}; +use sys_util::{FileReadWriteAtVolatile, FileReadWriteVolatile, GuestAddress, GuestMemory}; use super::DescriptorChain; @@ -166,33 +164,6 @@ impl<'a> DescriptorChainConsumer<'a> { other } - - fn get_iovec(&mut self, len: usize) -> io::Result<DescriptorIovec<'a>> { - let mut iovec = Vec::with_capacity(self.get_remaining().len()); - - let mut rem = len; - for buf in self.get_remaining() { - let iov = if rem < buf.size() { - // Safe because we know that `rem` is in-bounds. - buf.sub_slice(0, rem).unwrap().as_iovec() - } else { - buf.as_iovec() - }; - - rem -= iov.iov_len; - iovec.push(iov); - - if rem == 0 { - break; - } - } - self.consume(len); - - Ok(DescriptorIovec { - iovec, - mem: PhantomData, - }) - } } /// Provides high-level interface over the sequence of memory regions @@ -381,6 +352,19 @@ impl<'a> Reader<'a> { self.buffer.bytes_consumed() } + /// Returns a `&[VolatileSlice]` that represents all the remaining data in this `Reader`. + /// Calling this method does not actually consume any data from the `Reader` and callers should + /// call `consume` to advance the `Reader`. + pub fn get_remaining(&self) -> &[VolatileSlice] { + self.buffer.get_remaining() + } + + /// Consumes `amt` bytes from the underlying descriptor chain. If `amt` is larger than the + /// remaining data left in this `Reader`, then all remaining data will be consumed. + pub fn consume(&mut self, amt: usize) { + self.buffer.consume(amt) + } + /// Splits this `Reader` into two at the given offset in the `DescriptorChain` buffer. After the /// split, `self` will be able to read up to `offset` bytes while the returned `Reader` can read /// up to `available_bytes() - offset` bytes. If `offset > self.available_bytes()`, then the @@ -390,12 +374,6 @@ impl<'a> Reader<'a> { buffer: self.buffer.split_at(offset), } } - - /// Returns a DescriptorIovec for the next `len` bytes of the descriptor chain - /// buffer, which can be used as an IntoIovec. - pub fn get_iovec(&mut self, len: usize) -> io::Result<DescriptorIovec<'a>> { - self.buffer.get_iovec(len) - } } impl<'a> io::Read for Reader<'a> { @@ -581,12 +559,6 @@ impl<'a> Writer<'a> { buffer: self.buffer.split_at(offset), } } - - /// Returns a DescriptorIovec for the next `len` bytes of the descriptor chain - /// buffer, which can be used as an IntoIovec. - pub fn get_iovec(&mut self, len: usize) -> io::Result<DescriptorIovec<'a>> { - self.buffer.get_iovec(len) - } } impl<'a> io::Write for Writer<'a> { @@ -617,18 +589,6 @@ impl<'a> io::Write for Writer<'a> { } } -pub struct DescriptorIovec<'a> { - iovec: Vec<libc::iovec>, - mem: PhantomData<&'a GuestMemory>, -} - -// Safe because the lifetime of DescriptorIovec is tied to the underlying GuestMemory. -unsafe impl<'a> IntoIovec for DescriptorIovec<'a> { - fn into_iovec(&self) -> Vec<libc::iovec> { - self.iovec.clone() - } -} - const VIRTQ_DESC_F_NEXT: u16 = 0x1; const VIRTQ_DESC_F_WRITE: u16 = 0x2; diff --git a/devices/src/virtio/wl.rs b/devices/src/virtio/wl.rs index 8e94783..3a5ecee 100644 --- a/devices/src/virtio/wl.rs +++ b/devices/src/virtio/wl.rs @@ -730,12 +730,10 @@ impl WlVfd { fn send(&mut self, fds: &[RawFd], data: &mut Reader) -> WlResult<WlResp> { if let Some(socket) = &self.socket { socket - .send_with_fds( - data.get_iovec(usize::max_value()) - .map_err(WlError::ParseDesc)?, - fds, - ) + .send_with_fds(data.get_remaining(), fds) .map_err(WlError::SendVfd)?; + // All remaining data in `data` is now considered consumed. + data.consume(::std::usize::MAX); Ok(WlResp::Ok) } else if let Some((_, local_pipe)) = &mut self.local_pipe { // Impossible to send fds over a simple pipe. |