diff --git a/vhost/src/vhost_user/connection.rs b/vhost/src/vhost_user/connection.rs index 82cc225..92995cf 100644 --- a/vhost/src/vhost_user/connection.rs +++ b/vhost/src/vhost_user/connection.rs @@ -226,7 +226,7 @@ impl Endpoint { body: &T, fds: Option<&[RawFd]>, ) -> Result<()> { - if mem::size_of::() > MAX_MSG_SIZE { + if mem::size_of::() > H::MAX_MSG_SIZE { return Err(Error::OversizedMsg); } let bytes = self.send_iovec_all(&[hdr.as_slice(), body.as_slice()], fds)?; @@ -255,10 +255,10 @@ impl Endpoint { fds: Option<&[RawFd]>, ) -> Result<()> { let len = payload.len(); - if mem::size_of::() > MAX_MSG_SIZE { + if mem::size_of::() > H::MAX_MSG_SIZE { return Err(Error::OversizedMsg); } - if len > MAX_MSG_SIZE - mem::size_of::() { + if len > H::MAX_MSG_SIZE - mem::size_of::() { return Err(Error::OversizedMsg); } if let Some(fd_arr) = fds { diff --git a/vhost/src/vhost_user/gpu_message.rs b/vhost/src/vhost_user/gpu_message.rs index a070ce3..38ad7ae 100644 --- a/vhost/src/vhost_user/gpu_message.rs +++ b/vhost/src/vhost_user/gpu_message.rs @@ -168,4 +168,5 @@ impl VhostUserMsgValidator for VhostUserGpuMsgHeader { impl MsgHeader for VhostUserGpuMsgHeader { type Request = R; + const MAX_MSG_SIZE: usize = u32::MAX as usize; } diff --git a/vhost/src/vhost_user/message.rs b/vhost/src/vhost_user/message.rs index 8eb06ee..68333af 100644 --- a/vhost/src/vhost_user/message.rs +++ b/vhost/src/vhost_user/message.rs @@ -25,6 +25,15 @@ use vm_memory::{GuestAddress, MmapRange, MmapXenFlags}; use super::{enum_value, Error, Result}; use crate::VringConfigData; +/* +TODO: Consider deprecating this. We don't actually have any preallocated buffers except in tests, +so we should be able to support u32::MAX normally. +Also this doesn't need to be public api, since Endpoint is private anyway, this doesn't seem +useful for consumers of this crate. + +There are GPU specific messages (GpuBackendReq::UPDATE and CURSOR_UPDATE) that are larger than 4K. +We can use MsgHeader::MAX_MSG_SIZE, if we want to support larger messages only for GPU headers. +*/ /// The vhost-user specification uses a field of u32 to store message length. /// On the other hand, preallocated buffers are needed to receive messages from the Unix domain /// socket. To preallocating a 4GB buffer for each vhost-user message is really just an overhead. @@ -58,6 +67,9 @@ pub(super) trait Req: pub(super) trait MsgHeader: ByteValued + Copy + Default + VhostUserMsgValidator { type Request: Req; + + /// The maximum size of a msg that can be encapsulated by this MsgHeader + const MAX_MSG_SIZE: usize; } enum_value! { @@ -228,6 +240,7 @@ pub(super) struct VhostUserMsgHeader { impl MsgHeader for VhostUserMsgHeader { type Request = R; + const MAX_MSG_SIZE: usize = MAX_MSG_SIZE; } impl Debug for VhostUserMsgHeader {