vmm_vhost: inline Endpoint::recv_data

Convenience methods like this aren't worth the mental overhead IMO.

Change-Id: I2c08cf64b781328f515b88373170c4506058814c
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5066129
Commit-Queue: Frederick Mayle <fmayle@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Frederick Mayle 2023-11-27 16:25:37 -08:00 committed by crosvm LUCI
parent 98e465673b
commit 50cbe0cade
4 changed files with 11 additions and 37 deletions

View file

@ -240,19 +240,6 @@ impl<R: Req> Endpoint<R> {
Ok(())
}
/// Reads `len` bytes at most.
///
/// # Return:
/// * - (number of bytes received, buf) on success
pub fn recv_data(&self, len: usize) -> Result<Vec<u8>> {
let mut buf = vec![0u8; len];
let (data_len, _) = self
.0
.recv_into_bufs(&mut [IoSliceMut::new(&mut buf)], false /* allow_fd */)?;
buf.truncate(data_len);
Ok(buf)
}
/// Reads all bytes into the given scatter/gather vectors with optional attached files. Will
/// loop until all data has been transfered and errors if EOF is reached before then.
///

View file

@ -386,8 +386,8 @@ mod tests {
.unwrap();
assert_eq!(len, 4);
let buf4 = slave.recv_data(2).unwrap();
assert_eq!(buf4.len(), 2);
let mut buf4 = vec![0u8; 2];
slave.recv_into_bufs_all(&mut [&mut buf4[..]]).unwrap();
assert_eq!(&buf1[..2], &buf4[..]);
let (bytes, buf2, files) = slave.recv_into_buf(0x2).unwrap();
assert_eq!(bytes, 2);
@ -451,8 +451,9 @@ mod tests {
.unwrap();
assert_eq!(len, 4);
let v = slave.recv_data(5).unwrap();
assert_eq!(v.len(), 5);
let mut v = vec![0u8; 5];
slave.recv_into_bufs_all(&mut [&mut v[..]]).unwrap();
assert_eq!(&v[..], &[1, 2, 3, 4, 1]);
let (bytes, _, files) = slave.recv_into_buf(0x4).unwrap();
assert_eq!(bytes, 3);

View file

@ -303,16 +303,8 @@ impl<S: VhostUserMasterReqHandler> MasterReqHandler<S> {
// . validate message body and optional payload
let (hdr, files) = self.sub_sock.recv_header()?;
self.check_attached_files(&hdr, &files)?;
let buf = match hdr.get_size() {
0 => vec![0u8; 0],
len => {
let rbuf = self.sub_sock.recv_data(len as usize)?;
if rbuf.len() != len as usize {
return Err(Error::InvalidMessage);
}
rbuf
}
};
let mut buf = vec![0u8; hdr.get_size().try_into().unwrap()];
self.sub_sock.recv_into_bufs_all(&mut [&mut buf[..]])?;
let size = buf.len();
let res = match hdr.get_code() {

View file

@ -667,16 +667,10 @@ impl<S: VhostUserSlaveReqHandler> SlaveReqHandler<S> {
hdr: VhostUserMsgHeader<MasterReq>,
files: Option<Vec<File>>,
) -> Result<()> {
let buf = match hdr.get_size() {
0 => vec![0u8; 0],
len => {
let rbuf = self.slave_req_helper.endpoint.recv_data(len as usize)?;
if rbuf.len() != len as usize {
return Err(Error::InvalidMessage);
}
rbuf
}
};
let mut buf = vec![0u8; hdr.get_size().try_into().unwrap()];
self.slave_req_helper
.endpoint
.recv_into_bufs_all(&mut [&mut buf[..]])?;
let size = buf.len();
match hdr.get_code() {