vmm_vhost: inline Endpoint::recv_body_into_buf

It isn't well differentiate from the other methods and inlining it is a
net win in lines-of-code.

Change-Id: I829a96dc4de5f4ee579b1a5beb9b09efddfdcd53
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5066128
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Frederick Mayle 2023-11-27 15:53:37 -08:00 committed by crosvm LUCI
parent bc9058e05f
commit 98e465673b
3 changed files with 13 additions and 43 deletions

View file

@ -360,34 +360,6 @@ impl<R: Req> Endpoint<R> {
Ok((hdr, body, files))
}
/// Receive a message with header and optional content. Callers need to
/// pre-allocate a big enough buffer to receive the message body and
/// optional payload. If there are attached file descriptor associated
/// with the message, the first MAX_ATTACHED_FD_ENTRIES file descriptors
/// will be accepted and all other file descriptor will be discard
/// silently.
///
/// # Return:
/// * - (message header, [received files]) on success.
/// * - PartialMessage: received a partial message.
/// * - InvalidMessage: received a invalid message.
/// * - backend specific errors
#[cfg(test)]
pub fn recv_body_into_buf(
&self,
buf: &mut [u8],
) -> Result<(VhostUserMsgHeader<R>, Option<Vec<File>>)> {
let mut hdr = VhostUserMsgHeader::default();
let mut slices = [hdr.as_bytes_mut(), buf];
let files = self.recv_into_bufs_all(&mut slices)?;
if !hdr.is_valid() {
return Err(Error::InvalidMessage);
}
Ok((hdr, files))
}
/// Receive a message with optional payload and attached file descriptors.
/// Note, only the first MAX_ATTACHED_FD_ENTRIES file descriptors will be
/// accepted and all other file descriptor will be discard silently.

View file

@ -242,11 +242,11 @@ mod tests {
use std::io::SeekFrom;
use std::io::Write;
use std::mem;
use std::slice;
use tempfile::tempfile;
use tempfile::Builder;
use tempfile::TempDir;
use zerocopy::AsBytes;
use super::*;
@ -492,14 +492,12 @@ mod tests {
let features1 = 0x1u64;
master.send_message(&hdr1, &features1, None).unwrap();
let mut hdr2 = VhostUserMsgHeader::default();
let mut features2 = 0u64;
let slice = unsafe {
slice::from_raw_parts_mut(
(&mut features2 as *mut u64) as *mut u8,
mem::size_of::<u64>(),
)
};
let (hdr2, files) = slave.recv_body_into_buf(slice).unwrap();
let files = slave
.recv_into_bufs_all(&mut [hdr2.as_bytes_mut(), features2.as_bytes_mut()])
.unwrap();
assert!(hdr2.is_valid());
assert_eq!(hdr1, hdr2);
assert_eq!(features1, features2);
assert!(files.is_none());

View file

@ -187,9 +187,11 @@ mod tests {
use base::AsRawDescriptor;
use base::Tube;
use tempfile::tempfile;
use zerocopy::AsBytes;
use crate::message::MasterReq;
use crate::message::VhostUserMsgHeader;
use crate::message::VhostUserMsgValidator;
use crate::Endpoint;
fn create_pair() -> (Endpoint<MasterReq>, Endpoint<MasterReq>) {
@ -307,14 +309,12 @@ mod tests {
let features1 = 0x1u64;
master.send_message(&hdr1, &features1, None).unwrap();
let mut hdr2 = VhostUserMsgHeader::default();
let mut features2 = 0u64;
let slice = unsafe {
std::slice::from_raw_parts_mut(
(&mut features2 as *mut u64) as *mut u8,
mem::size_of::<u64>(),
)
};
let (hdr2, files) = slave.recv_body_into_buf(slice).unwrap();
let files = slave
.recv_into_bufs_all(&mut [hdr2.as_bytes_mut(), features2.as_bytes_mut()])
.unwrap();
assert!(hdr2.is_valid());
assert_eq!(hdr1, hdr2);
assert_eq!(features1, features2);
assert!(files.is_none());