vmm_vhost: vhost_user: Use IoSliceMut in recv_into_bufs()

Replace `&mut [u8]` in recv_into_bufs() with IoSliceMut.
Note that recv_into_bufs_all()'s interface is unchanged as it changes
internal cursor with advance().

BUG=none
TEST=cargo test --features=vhost-user-master,vhost-user-slave

Change-Id: I74666cd2472f879a473c07dbe617add79d0ca903
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3302693
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
This commit is contained in:
Keiichi Watanabe 2021-11-26 18:11:52 +09:00 committed by Commit Bot
parent dc069ba021
commit a6da8a5406
2 changed files with 7 additions and 5 deletions

View file

@ -7,6 +7,7 @@ mod socket;
pub use self::socket::{SocketEndpoint, SocketListener};
use std::fs::File;
use std::io::IoSliceMut;
use std::mem;
use std::os::unix::io::RawFd;
use std::path::Path;
@ -55,7 +56,7 @@ pub trait Endpoint<R: Req>: Sized {
///
/// # Return:
/// * - (number of bytes received, [received files]) on success
fn recv_into_bufs(&mut self, bufs: &mut [&mut [u8]]) -> Result<(usize, Option<Vec<File>>)>;
fn recv_into_bufs(&mut self, bufs: &mut [IoSliceMut]) -> Result<(usize, Option<Vec<File>>)>;
}
// Advance the internal cursor of the slices.
@ -236,7 +237,8 @@ pub(super) trait EndpointExt<R: Req>: Endpoint<R> {
let mut rfds = None;
while (data_total - data_read) > 0 {
let res = self.recv_into_bufs(bufs);
let mut slices: Vec<IoSliceMut> = bufs.iter_mut().map(|b| IoSliceMut::new(b)).collect();
let res = self.recv_into_bufs(&mut slices);
match res {
Ok((0, _)) => return Ok((data_read, rfds)),
Ok((n, fds)) => {
@ -264,7 +266,7 @@ pub(super) trait EndpointExt<R: Req>: Endpoint<R> {
#[cfg(test)]
fn recv_into_buf(&mut self, buf_size: usize) -> Result<(usize, Vec<u8>, Option<Vec<File>>)> {
let mut buf = vec![0u8; buf_size];
let mut slices = vec![buf.as_mut_slice()];
let mut slices = [IoSliceMut::new(buf.as_mut_slice())];
let (bytes, files) = self.recv_into_bufs(&mut slices)?;
Ok((bytes, buf, files))
}
@ -280,7 +282,7 @@ pub(super) trait EndpointExt<R: Req>: Endpoint<R> {
/// * - backend specific errors
fn recv_header(&mut self) -> Result<(VhostUserMsgHeader<R>, Option<Vec<File>>)> {
let mut hdr = VhostUserMsgHeader::default();
let (bytes, files) = self.recv_into_bufs(&mut [hdr.as_mut_slice()])?;
let (bytes, files) = self.recv_into_bufs(&mut [IoSliceMut::new(hdr.as_mut_slice())])?;
if bytes != mem::size_of::<VhostUserMsgHeader<R>>() {
return Err(Error::PartialMessage);

View file

@ -176,7 +176,7 @@ impl<R: Req> Endpoint<R> for SocketEndpoint<R> {
/// * - SocketRetry: temporary error caused by signals or short of resources.
/// * - SocketBroken: the underline socket is broken.
/// * - SocketError: other socket related errors.
fn recv_into_bufs(&mut self, bufs: &mut [&mut [u8]]) -> Result<(usize, Option<Vec<File>>)> {
fn recv_into_bufs(&mut self, bufs: &mut [IoSliceMut]) -> Result<(usize, Option<Vec<File>>)> {
let mut fd_array = vec![0; MAX_ATTACHED_FD_ENTRIES];
let mut iovs: Vec<_> = bufs.iter_mut().map(|s| IoSliceMut::new(s)).collect();
let (bytes, fds) = self.sock.recv_iovecs_with_fds(&mut iovs, &mut fd_array)?;