From d4d62fc9dcf4eb96dbf43c7c0647a8155551193d Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Thu, 24 Jun 2021 09:47:49 +0200 Subject: [PATCH] deps: Update vhost crate from 1a03a2a to 9982541 This dependency bump needed some manual handling since the API changed quite a lot regarding some RawFd being changed into either File or AsRawFd traits. Signed-off-by: Sebastien Boeuf --- Cargo.lock | 2 +- fuzz/Cargo.lock | 2 +- vhost_user_backend/src/lib.rs | 54 ++++++------------- virtio-devices/src/vhost_user/fs.rs | 30 ++++++++--- .../src/vhost_user/vu_common_ctrl.rs | 2 +- 5 files changed, 42 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 72b66ded4..4f14b8b80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1118,7 +1118,7 @@ dependencies = [ [[package]] name = "vhost" version = "0.1.0" -source = "git+https://github.com/rust-vmm/vhost?branch=master#1a03a2aca700421f258b094b1a845f6420d8cfa9" +source = "git+https://github.com/rust-vmm/vhost?branch=master#9982541776a603d30556a06df55e8c0491072763" dependencies = [ "bitflags", "libc", diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index a552fc4ad..8e40e3018 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -652,7 +652,7 @@ dependencies = [ [[package]] name = "vhost" version = "0.1.0" -source = "git+https://github.com/rust-vmm/vhost?branch=master#a8ff939161d41fc2f449b80e461d013c1e19f666" +source = "git+https://github.com/rust-vmm/vhost?branch=master#9982541776a603d30556a06df55e8c0491072763" dependencies = [ "bitflags", "libc", diff --git a/vhost_user_backend/src/lib.rs b/vhost_user_backend/src/lib.rs index 767a75fb8..ec9e78ad5 100644 --- a/vhost_user_backend/src/lib.rs +++ b/vhost_user_backend/src/lib.rs @@ -11,12 +11,12 @@ use std::error; use std::fs::File; use std::io; use std::num::Wrapping; -use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; use std::result; use std::sync::{Arc, Mutex, RwLock}; use std::thread; use vhost::vhost_user::message::{ - VhostUserConfigFlags, VhostUserMemoryRegion, VhostUserProtocolFeatures, + VhostUserConfigFlags, VhostUserInflight, VhostUserMemoryRegion, VhostUserProtocolFeatures, VhostUserSingleMemoryRegion, VhostUserVirtioFeatures, VhostUserVringAddrFlags, VhostUserVringState, }; @@ -235,6 +235,7 @@ pub struct Vring { queue: Queue, kick: Option, call: Option, + #[allow(dead_code)] err: Option, enabled: bool, } @@ -625,18 +626,17 @@ impl VhostUserSlaveReqHandlerMut for VhostUserHandler { fn set_mem_table( &mut self, ctx: &[VhostUserMemoryRegion], - fds: &[RawFd], + mut files: Vec, ) -> VhostUserResult<()> { // We need to create tuple of ranges from the list of VhostUserMemoryRegion // that we get from the caller. let mut regions: Vec<(GuestAddress, usize, Option)> = Vec::new(); let mut mappings: Vec = Vec::new(); - for (idx, region) in ctx.iter().enumerate() { + for region in ctx.iter() { let g_addr = GuestAddress(region.guest_phys_addr); let len = region.memory_size as usize; - let file = unsafe { File::from_raw_fd(fds[idx]) }; - let f_off = FileOffset::new(file, region.mmap_offset); + let f_off = FileOffset::new(files.remove(0), region.mmap_offset); regions.push((g_addr, len, Some(f_off))); mappings.push(AddrMapping { @@ -770,17 +770,13 @@ impl VhostUserSlaveReqHandlerMut for VhostUserHandler { Ok(VhostUserVringState::new(index, u32::from(next_avail))) } - fn set_vring_kick(&mut self, index: u8, fd: Option) -> VhostUserResult<()> { + fn set_vring_kick(&mut self, index: u8, fd: Option) -> VhostUserResult<()> { if index as usize >= self.num_queues { return Err(VhostUserError::InvalidParam); } - if let Some(kick) = self.vrings[index as usize].write().unwrap().kick.take() { - // Close file descriptor set by previous operations. - let _ = unsafe { libc::close(kick.as_raw_fd()) }; - } self.vrings[index as usize].write().unwrap().kick = - fd.map(|x| unsafe { EventFd::from_raw_fd(x) }); + fd.map(|x| unsafe { EventFd::from_raw_fd(x.into_raw_fd()) }); // Quote from vhost-user specification: // Client must start ring upon receiving a kick (that is, detecting @@ -808,32 +804,24 @@ impl VhostUserSlaveReqHandlerMut for VhostUserHandler { Ok(()) } - fn set_vring_call(&mut self, index: u8, fd: Option) -> VhostUserResult<()> { + fn set_vring_call(&mut self, index: u8, fd: Option) -> VhostUserResult<()> { if index as usize >= self.num_queues { return Err(VhostUserError::InvalidParam); } - if let Some(call) = self.vrings[index as usize].write().unwrap().call.take() { - // Close file descriptor set by previous operations. - let _ = unsafe { libc::close(call.as_raw_fd()) }; - } self.vrings[index as usize].write().unwrap().call = - fd.map(|x| unsafe { EventFd::from_raw_fd(x) }); + fd.map(|x| unsafe { EventFd::from_raw_fd(x.into_raw_fd()) }); Ok(()) } - fn set_vring_err(&mut self, index: u8, fd: Option) -> VhostUserResult<()> { + fn set_vring_err(&mut self, index: u8, fd: Option) -> VhostUserResult<()> { if index as usize >= self.num_queues { return Err(VhostUserError::InvalidParam); } - if let Some(err) = self.vrings[index as usize].write().unwrap().err.take() { - // Close file descriptor set by previous operations. - let _ = unsafe { libc::close(err.as_raw_fd()) }; - } self.vrings[index as usize].write().unwrap().err = - fd.map(|x| unsafe { EventFd::from_raw_fd(x) }); + fd.map(|x| unsafe { EventFd::from_raw_fd(x.into_raw_fd()) }); Ok(()) } @@ -889,11 +877,10 @@ impl VhostUserSlaveReqHandlerMut for VhostUserHandler { fn add_mem_region( &mut self, region: &VhostUserSingleMemoryRegion, - fd: RawFd, + fd: File, ) -> VhostUserResult<()> { - let file = unsafe { File::from_raw_fd(fd) }; let mmap_region = MmapRegion::from_file( - FileOffset::new(file, region.mmap_offset), + FileOffset::new(fd, region.mmap_offset), region.memory_size as usize, ) .map_err(|e| VhostUserError::ReqHandlerError(io::Error::new(io::ErrorKind::Other, e)))?; @@ -962,19 +949,12 @@ impl VhostUserSlaveReqHandlerMut for VhostUserHandler { fn get_inflight_fd( &mut self, - _: &vhost::vhost_user::message::VhostUserInflight, - ) -> std::result::Result< - (vhost::vhost_user::message::VhostUserInflight, i32), - vhost::vhost_user::Error, - > { + _: &VhostUserInflight, + ) -> VhostUserResult<(VhostUserInflight, File)> { std::unimplemented!() } - fn set_inflight_fd( - &mut self, - _: &vhost::vhost_user::message::VhostUserInflight, - _: std::fs::File, - ) -> std::result::Result<(), vhost::vhost_user::Error> { + fn set_inflight_fd(&mut self, _: &VhostUserInflight, _: File) -> VhostUserResult<()> { std::unimplemented!() } } diff --git a/virtio-devices/src/vhost_user/fs.rs b/virtio-devices/src/vhost_user/fs.rs index ebf3269e0..47b8c1f8b 100644 --- a/virtio-devices/src/vhost_user/fs.rs +++ b/virtio-devices/src/vhost_user/fs.rs @@ -17,7 +17,7 @@ use libc::{self, c_void, off64_t, pread64, pwrite64}; use seccomp::{SeccompAction, SeccompFilter}; use std::io; use std::ops::Deref; -use std::os::unix::io::{AsRawFd, RawFd}; +use std::os::unix::io::AsRawFd; use std::result; use std::sync::{Arc, Barrier, Mutex}; use std::thread; @@ -62,7 +62,7 @@ impl VhostUserMasterReqHandler for SlaveReqHandler { Ok(0) } - fn fs_slave_map(&self, fs: &VhostUserFSSlaveMsg, fd: RawFd) -> HandlerResult { + fn fs_slave_map(&self, fs: &VhostUserFSSlaveMsg, fd: &dyn AsRawFd) -> HandlerResult { debug!("fs_slave_map"); for i in 0..VHOST_USER_FS_SLAVE_ENTRIES { @@ -86,7 +86,7 @@ impl VhostUserMasterReqHandler for SlaveReqHandler { len as usize, flags.bits() as i32, libc::MAP_SHARED | libc::MAP_FIXED, - fd, + fd.as_raw_fd(), fs.fd_offset[i] as libc::off_t, ) }; @@ -94,7 +94,7 @@ impl VhostUserMasterReqHandler for SlaveReqHandler { return Err(io::Error::last_os_error()); } - let ret = unsafe { libc::close(fd) }; + let ret = unsafe { libc::close(fd.as_raw_fd()) }; if ret == -1 { return Err(io::Error::last_os_error()); } @@ -171,7 +171,7 @@ impl VhostUserMasterReqHandler for SlaveReqHandler { Ok(0) } - fn fs_slave_io(&self, fs: &VhostUserFSSlaveMsg, fd: RawFd) -> HandlerResult { + fn fs_slave_io(&self, fs: &VhostUserFSSlaveMsg, fd: &dyn AsRawFd) -> HandlerResult { debug!("fs_slave_io"); let mut done: u64 = 0; @@ -218,10 +218,24 @@ impl VhostUserMasterReqHandler for SlaveReqHandler { == VhostUserFSSlaveMsgFlags::MAP_W { debug!("write: foffset={}, len={}", foffset, len); - unsafe { pwrite64(fd, ptr as *const c_void, len as usize, foffset as off64_t) } + unsafe { + pwrite64( + fd.as_raw_fd(), + ptr as *const c_void, + len as usize, + foffset as off64_t, + ) + } } else { debug!("read: foffset={}, len={}", foffset, len); - unsafe { pread64(fd, ptr as *mut c_void, len as usize, foffset as off64_t) } + unsafe { + pread64( + fd.as_raw_fd(), + ptr as *mut c_void, + len as usize, + foffset as off64_t, + ) + } }; if ret < 0 { @@ -242,7 +256,7 @@ impl VhostUserMasterReqHandler for SlaveReqHandler { } } - let ret = unsafe { libc::close(fd) }; + let ret = unsafe { libc::close(fd.as_raw_fd()) }; if ret == -1 { return Err(io::Error::last_os_error()); } diff --git a/virtio-devices/src/vhost_user/vu_common_ctrl.rs b/virtio-devices/src/vhost_user/vu_common_ctrl.rs index 2b1438119..b866dbe97 100644 --- a/virtio-devices/src/vhost_user/vu_common_ctrl.rs +++ b/virtio-devices/src/vhost_user/vu_common_ctrl.rs @@ -197,7 +197,7 @@ pub fn setup_vhost_user( } if let Some(slave_req_handler) = slave_req_handler { - vu.set_slave_request_fd(slave_req_handler.get_tx_raw_fd()) + vu.set_slave_request_fd(&slave_req_handler.get_tx_raw_fd()) .map_err(Error::VhostUserSetSlaveRequestFd) } else { Ok(())