diff options
author | Liu Jiang <gerry@linux.alibaba.com> | 2021-02-21 22:36:10 +0800 |
---|---|---|
committer | Sergio Lopez <slp@sinrega.org> | 2021-03-01 12:50:56 +0100 |
commit | 9e22e2fe2f0f22161eb292a538e5b712d1f7d9be (patch) | |
tree | 96603912d067fb8b74eac39e263b164bde7615e6 /src/vhost_user | |
parent | dc452e5aeb5d106c1f76210f8c6b788bc41cc571 (diff) | |
download | vmm_vhost-9e22e2fe2f0f22161eb292a538e5b712d1f7d9be.tar.gz |
vhost_user: refine connection implementation
Refine connection implementation by:
1) using "payload: &[u8]" for send_message_with_payload()
2) enabling all unit test cases
Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
Diffstat (limited to 'src/vhost_user')
-rw-r--r-- | src/vhost_user/connection.rs | 19 | ||||
-rw-r--r-- | src/vhost_user/master.rs | 9 | ||||
-rw-r--r-- | src/vhost_user/slave_req_handler.rs | 10 |
3 files changed, 20 insertions, 18 deletions
diff --git a/src/vhost_user/connection.rs b/src/vhost_user/connection.rs index 5aa580b..d89f9c7 100644 --- a/src/vhost_user/connection.rs +++ b/src/vhost_user/connection.rs @@ -216,6 +216,9 @@ impl<R: Req> Endpoint<R> { body: &T, fds: Option<&[RawFd]>, ) -> Result<()> { + if mem::size_of::<T>() > MAX_MSG_SIZE { + return Err(Error::OversizedMsg); + } // Safe because there can't be other mutable referance to hdr and body. let iovs = unsafe { [ @@ -244,14 +247,17 @@ impl<R: Req> Endpoint<R> { /// * - OversizedMsg: message size is too big. /// * - PartialMessage: received a partial message. /// * - IncorrectFds: wrong number of attached fds. - pub fn send_message_with_payload<T: Sized, P: Sized>( + pub fn send_message_with_payload<T: Sized>( &mut self, hdr: &VhostUserMsgHeader<R>, body: &T, - payload: &[P], + payload: &[u8], fds: Option<&[RawFd]>, ) -> Result<()> { - let len = payload.len() * mem::size_of::<P>(); + let len = payload.len(); + if mem::size_of::<T>() > MAX_MSG_SIZE { + return Err(Error::OversizedMsg); + } if len > MAX_MSG_SIZE - mem::size_of::<T>() { return Err(Error::OversizedMsg); } @@ -615,7 +621,9 @@ mod tests { #[test] fn create_listener() { - let _ = Listener::new(UNIX_SOCKET_LISTENER, true).unwrap(); + let listener = Listener::new(UNIX_SOCKET_LISTENER, true).unwrap(); + + assert!(listener.as_raw_fd() > 0); } #[test] @@ -629,7 +637,6 @@ mod tests { } #[test] - #[ignore] fn send_data() { let listener = Listener::new(UNIX_SOCKET_DATA, true).unwrap(); listener.set_nonblocking(true).unwrap(); @@ -655,7 +662,6 @@ mod tests { } #[test] - #[ignore] fn send_fd() { let listener = Listener::new(UNIX_SOCKET_FD, true).unwrap(); listener.set_nonblocking(true).unwrap(); @@ -809,7 +815,6 @@ mod tests { } #[test] - #[ignore] fn send_recv() { let listener = Listener::new(UNIX_SOCKET_SEND, true).unwrap(); listener.set_nonblocking(true).unwrap(); diff --git a/src/vhost_user/master.rs b/src/vhost_user/master.rs index 2651b84..906932c 100644 --- a/src/vhost_user/master.rs +++ b/src/vhost_user/master.rs @@ -176,10 +176,11 @@ impl VhostBackend for Master { let mut node = self.node.lock().unwrap(); let body = VhostUserMemory::new(ctx.regions.len() as u32); + let (_, payload, _) = unsafe { ctx.regions.align_to::<u8>() }; let hdr = node.send_request_with_payload( MasterReq::SET_MEM_TABLE, &body, - ctx.regions.as_slice(), + payload, Some(ctx.fds.as_slice()), )?; node.wait_for_ack(&hdr).map_err(|e| e.into()) @@ -503,14 +504,14 @@ impl MasterInternal { Ok(hdr) } - fn send_request_with_payload<T: Sized, P: Sized>( + fn send_request_with_payload<T: Sized>( &mut self, code: MasterReq, msg: &T, - payload: &[P], + payload: &[u8], fds: Option<&[RawFd]>, ) -> VhostUserResult<VhostUserMsgHeader<MasterReq>> { - let len = mem::size_of::<T>() + payload.len() * mem::size_of::<P>(); + let len = mem::size_of::<T>() + payload.len(); if len > MAX_MSG_SIZE { return Err(VhostUserError::InvalidParam); } diff --git a/src/vhost_user/slave_req_handler.rs b/src/vhost_user/slave_req_handler.rs index f3b0770..95e23d1 100644 --- a/src/vhost_user/slave_req_handler.rs +++ b/src/vhost_user/slave_req_handler.rs @@ -590,16 +590,12 @@ impl<S: VhostUserSlaveReqHandler> SlaveReqHandler<S> { Ok(()) } - fn send_reply_with_payload<T, P>( + fn send_reply_with_payload<T: Sized>( &mut self, req: &VhostUserMsgHeader<MasterReq>, msg: &T, - payload: &[P], - ) -> Result<()> - where - T: Sized, - P: Sized, - { + payload: &[u8], + ) -> Result<()> { let hdr = self.new_reply_header::<T>(req, payload.len())?; self.main_sock .send_message_with_payload(&hdr, msg, payload, None)?; |