Skip to content

Commit

Permalink
vhost_user: Remove support for FS_* requests
Browse files Browse the repository at this point in the history
Since these non-standard backend request
message types are confirmed not to be used
anymore, let's just remove them.

```
pub enum BackendReq {
    ...
    /// Virtio-fs draft: map file content into the window.
    FS_MAP = 100,
    /// Virtio-fs draft: unmap file content from the window.
    FS_UNMAP = 1001,
    /// Virtio-fs draft: sync file content.
    FS_SYNC = 1002,
    /// Virtio-fs draft: perform a read/write from an fd directly to GPA.
    FS_IO = 1003,
    ...
}
```

Closes: rust-vmm#213
Signed-off-by: Albert Esteve <[email protected]>
  • Loading branch information
aesteve-rh committed Jun 27, 2024
1 parent 48081dd commit af987ba
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 222 deletions.
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 72.12,
"coverage_score": 73.04,
"exclude_path": "vhost/src/vhost_kern/",
"crate_features": "vhost/vhost-user-frontend,vhost/vhost-user-backend,vhost-user-backend/postcopy"
}
39 changes: 17 additions & 22 deletions vhost/src/vhost_user/backend_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,6 @@ impl VhostUserFrontendReqHandler for Backend {
Some(&[fd.as_raw_fd()]),
)
}

/// Forward vhost-user-fs map file requests to the backend.
fn fs_backend_map(&self, fs: &VhostUserFSBackendMsg, fd: &dyn AsRawFd) -> HandlerResult<u64> {
self.send_message(BackendReq::FS_MAP, fs, Some(&[fd.as_raw_fd()]))
}

/// Forward vhost-user-fs unmap file requests to the frontend.
fn fs_backend_unmap(&self, fs: &VhostUserFSBackendMsg) -> HandlerResult<u64> {
self.send_message(BackendReq::FS_UNMAP, fs, None)
}
}

#[cfg(test)]
Expand All @@ -211,15 +201,15 @@ mod tests {

#[test]
fn test_backend_req_send_failure() {
let (p1, p2) = UnixStream::pair().unwrap();
let (p1, _) = UnixStream::pair().unwrap();
let backend = Backend::from_stream(p1);

backend.set_failed(libc::ECONNRESET);
backend
.fs_backend_map(&VhostUserFSBackendMsg::default(), &p2)
.shared_object_add(&VhostUserSharedMsg::default())
.unwrap_err();
backend
.fs_backend_unmap(&VhostUserFSBackendMsg::default())
.shared_object_remove(&VhostUserSharedMsg::default())
.unwrap_err();
backend.node().error = None;
}
Expand All @@ -230,9 +220,9 @@ mod tests {
let backend = Backend::from_stream(p1);
let mut frontend = Endpoint::<BackendReq>::from_stream(p2);

let len = mem::size_of::<VhostUserFSBackendMsg>();
let len = mem::size_of::<VhostUserSharedMsg>();
let mut hdr = VhostUserMsgHeader::new(
BackendReq::FS_MAP,
BackendReq::SHARED_OBJECT_ADD,
VhostUserHeaderFlag::REPLY.bits(),
len as u32,
);
Expand All @@ -242,31 +232,36 @@ mod tests {
.send_message(&hdr, &body, Some(&[frontend.as_raw_fd()]))
.unwrap();
backend
.fs_backend_map(&VhostUserFSBackendMsg::default(), &frontend)
.shared_object_add(&VhostUserSharedMsg::default())
.unwrap_err();

backend.set_shared_object_flag(true);
backend
.shared_object_add(&VhostUserSharedMsg::default())
.unwrap();

backend.set_reply_ack_flag(true);
backend
.fs_backend_map(&VhostUserFSBackendMsg::default(), &frontend)
.shared_object_add(&VhostUserSharedMsg::default())
.unwrap_err();

hdr.set_code(BackendReq::FS_UNMAP);
hdr.set_code(BackendReq::SHARED_OBJECT_REMOVE);
frontend.send_message(&hdr, &body, None).unwrap();
backend
.fs_backend_map(&VhostUserFSBackendMsg::default(), &frontend)
.shared_object_add(&VhostUserSharedMsg::default())
.unwrap_err();
hdr.set_code(BackendReq::FS_MAP);
hdr.set_code(BackendReq::SHARED_OBJECT_ADD);

let body = VhostUserU64::new(1);
frontend.send_message(&hdr, &body, None).unwrap();
backend
.fs_backend_map(&VhostUserFSBackendMsg::default(), &frontend)
.shared_object_add(&VhostUserSharedMsg::default())
.unwrap_err();

let body = VhostUserU64::new(0);
frontend.send_message(&hdr, &body, None).unwrap();
backend
.fs_backend_map(&VhostUserFSBackendMsg::default(), &frontend)
.shared_object_add(&VhostUserSharedMsg::default())
.unwrap();
}
}
125 changes: 1 addition & 124 deletions vhost/src/vhost_user/frontend_req_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,6 @@ pub trait VhostUserFrontendReqHandler {
Err(std::io::Error::from_raw_os_error(libc::ENOSYS))
}

/// Handle virtio-fs map file requests.
fn fs_backend_map(&self, _fs: &VhostUserFSBackendMsg, _fd: &dyn AsRawFd) -> HandlerResult<u64> {
Err(std::io::Error::from_raw_os_error(libc::ENOSYS))
}

/// Handle virtio-fs unmap file requests.
fn fs_backend_unmap(&self, _fs: &VhostUserFSBackendMsg) -> HandlerResult<u64> {
Err(std::io::Error::from_raw_os_error(libc::ENOSYS))
}

/// Handle virtio-fs sync file requests.
fn fs_backend_sync(&self, _fs: &VhostUserFSBackendMsg) -> HandlerResult<u64> {
Err(std::io::Error::from_raw_os_error(libc::ENOSYS))
}

/// Handle virtio-fs file IO requests.
fn fs_backend_io(&self, _fs: &VhostUserFSBackendMsg, _fd: &dyn AsRawFd) -> HandlerResult<u64> {
Err(std::io::Error::from_raw_os_error(libc::ENOSYS))
}

// fn handle_iotlb_msg(&mut self, iotlb: VhostUserIotlb);
// fn handle_vring_host_notifier(&mut self, area: VhostUserVringArea, fd: &dyn AsRawFd);
}
Expand Down Expand Up @@ -104,34 +84,6 @@ pub trait VhostUserFrontendReqHandlerMut {
Err(std::io::Error::from_raw_os_error(libc::ENOSYS))
}

/// Handle virtio-fs map file requests.
fn fs_backend_map(
&mut self,
_fs: &VhostUserFSBackendMsg,
_fd: &dyn AsRawFd,
) -> HandlerResult<u64> {
Err(std::io::Error::from_raw_os_error(libc::ENOSYS))
}

/// Handle virtio-fs unmap file requests.
fn fs_backend_unmap(&mut self, _fs: &VhostUserFSBackendMsg) -> HandlerResult<u64> {
Err(std::io::Error::from_raw_os_error(libc::ENOSYS))
}

/// Handle virtio-fs sync file requests.
fn fs_backend_sync(&mut self, _fs: &VhostUserFSBackendMsg) -> HandlerResult<u64> {
Err(std::io::Error::from_raw_os_error(libc::ENOSYS))
}

/// Handle virtio-fs file IO requests.
fn fs_backend_io(
&mut self,
_fs: &VhostUserFSBackendMsg,
_fd: &dyn AsRawFd,
) -> HandlerResult<u64> {
Err(std::io::Error::from_raw_os_error(libc::ENOSYS))
}

// fn handle_iotlb_msg(&mut self, iotlb: VhostUserIotlb);
// fn handle_vring_host_notifier(&mut self, area: VhostUserVringArea, fd: RawFd);
}
Expand Down Expand Up @@ -159,22 +111,6 @@ impl<S: VhostUserFrontendReqHandlerMut> VhostUserFrontendReqHandler for Mutex<S>
) -> HandlerResult<u64> {
self.lock().unwrap().shared_object_lookup(uuid, fd)
}

fn fs_backend_map(&self, fs: &VhostUserFSBackendMsg, fd: &dyn AsRawFd) -> HandlerResult<u64> {
self.lock().unwrap().fs_backend_map(fs, fd)
}

fn fs_backend_unmap(&self, fs: &VhostUserFSBackendMsg) -> HandlerResult<u64> {
self.lock().unwrap().fs_backend_unmap(fs)
}

fn fs_backend_sync(&self, fs: &VhostUserFSBackendMsg) -> HandlerResult<u64> {
self.lock().unwrap().fs_backend_sync(fs)
}

fn fs_backend_io(&self, fs: &VhostUserFSBackendMsg, fd: &dyn AsRawFd) -> HandlerResult<u64> {
self.lock().unwrap().fs_backend_io(fs, fd)
}
}

/// Server to handle service requests from backends from the backend communication channel.
Expand Down Expand Up @@ -305,32 +241,6 @@ impl<S: VhostUserFrontendReqHandler> FrontendReqHandler<S> {
.shared_object_lookup(&msg, &files.unwrap()[0])
.map_err(Error::ReqHandlerError)
}
Ok(BackendReq::FS_MAP) => {
let msg = self.extract_msg_body::<VhostUserFSBackendMsg>(&hdr, size, &buf)?;
// check_attached_files() has validated files
self.backend
.fs_backend_map(&msg, &files.unwrap()[0])
.map_err(Error::ReqHandlerError)
}
Ok(BackendReq::FS_UNMAP) => {
let msg = self.extract_msg_body::<VhostUserFSBackendMsg>(&hdr, size, &buf)?;
self.backend
.fs_backend_unmap(&msg)
.map_err(Error::ReqHandlerError)
}
Ok(BackendReq::FS_SYNC) => {
let msg = self.extract_msg_body::<VhostUserFSBackendMsg>(&hdr, size, &buf)?;
self.backend
.fs_backend_sync(&msg)
.map_err(Error::ReqHandlerError)
}
Ok(BackendReq::FS_IO) => {
let msg = self.extract_msg_body::<VhostUserFSBackendMsg>(&hdr, size, &buf)?;
// check_attached_files() has validated files
self.backend
.fs_backend_io(&msg, &files.unwrap()[0])
.map_err(Error::ReqHandlerError)
}
_ => Err(Error::InvalidMessage),
};

Expand Down Expand Up @@ -368,7 +278,7 @@ impl<S: VhostUserFrontendReqHandler> FrontendReqHandler<S> {
files: &Option<Vec<File>>,
) -> Result<()> {
match hdr.get_code() {
Ok(BackendReq::SHARED_OBJECT_LOOKUP | BackendReq::FS_MAP | BackendReq::FS_IO) => {
Ok(BackendReq::SHARED_OBJECT_LOOKUP) => {
// Expect a single file is passed.
match files {
Some(files) if files.len() == 1 => Ok(()),
Expand Down Expand Up @@ -485,19 +395,6 @@ mod tests {
}
Ok(1)
}
/// Handle virtio-fs map file requests from the backend.
fn fs_backend_map(
&mut self,
_fs: &VhostUserFSBackendMsg,
_fd: &dyn AsRawFd,
) -> HandlerResult<u64> {
Ok(0)
}

/// Handle virtio-fs unmap file requests from the backend.
fn fs_backend_unmap(&mut self, _fs: &VhostUserFSBackendMsg) -> HandlerResult<u64> {
Err(std::io::Error::from_raw_os_error(libc::ENOSYS))
}
}

#[test]
Expand Down Expand Up @@ -532,9 +429,6 @@ mod tests {
let backend = Backend::from_stream(stream);

let frontend_handler = std::thread::spawn(move || {
let res = handler.handle_request().unwrap();
assert_eq!(res, 0);
handler.handle_request().unwrap_err();
// Testing shared object messages.
assert_eq!(handler.handle_request().unwrap(), 0);
assert_eq!(handler.handle_request().unwrap(), 1);
Expand All @@ -545,14 +439,6 @@ mod tests {
});

backend.set_shared_object_flag(true);
backend
.fs_backend_map(&VhostUserFSBackendMsg::default(), &fd)
.unwrap();
// When REPLY_ACK has not been negotiated, the frontend has no way to detect failure from
// backend side.
backend
.fs_backend_unmap(&VhostUserFSBackendMsg::default())
.unwrap();

let shobj_msg = VhostUserSharedMsg {
uuid: Uuid::new_v4(),
Expand Down Expand Up @@ -592,9 +478,6 @@ mod tests {
let backend = Backend::from_stream(stream);

let frontend_handler = std::thread::spawn(move || {
let res = handler.handle_request().unwrap();
assert_eq!(res, 0);
handler.handle_request().unwrap_err();
// Testing shared object messages.
assert_eq!(handler.handle_request().unwrap(), 0);
assert_eq!(handler.handle_request().unwrap(), 1);
Expand All @@ -606,12 +489,6 @@ mod tests {

backend.set_reply_ack_flag(true);
backend.set_shared_object_flag(true);
backend
.fs_backend_map(&VhostUserFSBackendMsg::default(), &fd)
.unwrap();
backend
.fs_backend_unmap(&VhostUserFSBackendMsg::default())
.unwrap_err();

let shobj_msg = VhostUserSharedMsg {
uuid: Uuid::new_v4(),
Expand Down
75 changes: 0 additions & 75 deletions vhost/src/vhost_user/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,6 @@ enum_value! {
SHARED_OBJECT_REMOVE = 7,
/// Lookup for a virtio shared object.
SHARED_OBJECT_LOOKUP = 8,

// Non-standard message types.
/// Virtio-fs draft: map file content into the window.
FS_MAP = 1000,
/// Virtio-fs draft: unmap file content from the window.
FS_UNMAP = 1001,
/// Virtio-fs draft: sync file content.
FS_SYNC = 1002,
/// Virtio-fs draft: perform a read/write from an fd directly to GPA.
FS_IO = 1003,
}
}

Expand Down Expand Up @@ -1014,54 +1004,6 @@ impl VhostUserMsgValidator for VhostUserTransferDeviceState {
}
}

// Bit mask for flags in virtio-fs backend messages
bitflags! {
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
/// Flags for virtio-fs backend messages.
pub struct VhostUserFSBackendMsgFlags: u64 {
/// Empty permission.
const EMPTY = 0x0;
/// Read permission.
const MAP_R = 0x1;
/// Write permission.
const MAP_W = 0x2;
}
}

/// Max entries in one virtio-fs backend request.
pub const VHOST_USER_FS_BACKEND_ENTRIES: usize = 8;

/// Backend request message to update the MMIO window.
#[repr(C, packed)]
#[derive(Copy, Clone, Default)]
pub struct VhostUserFSBackendMsg {
/// File offset.
pub fd_offset: [u64; VHOST_USER_FS_BACKEND_ENTRIES],
/// Offset into the DAX window.
pub cache_offset: [u64; VHOST_USER_FS_BACKEND_ENTRIES],
/// Size of region to map.
pub len: [u64; VHOST_USER_FS_BACKEND_ENTRIES],
/// Flags for the mmap operation
pub flags: [VhostUserFSBackendMsgFlags; VHOST_USER_FS_BACKEND_ENTRIES],
}

// SAFETY: Safe because all fields of VhostUserFSBackendMsg are POD.
unsafe impl ByteValued for VhostUserFSBackendMsg {}

impl VhostUserMsgValidator for VhostUserFSBackendMsg {
fn is_valid(&self) -> bool {
for i in 0..VHOST_USER_FS_BACKEND_ENTRIES {
if ({ self.flags[i] }.bits() & !VhostUserFSBackendMsgFlags::all().bits()) != 0
|| self.fd_offset[i].checked_add(self.len[i]).is_none()
|| self.cache_offset[i].checked_add(self.len[i]).is_none()
{
return false;
}
}
true
}
}

/// Inflight I/O descriptor state for split virtqueues
#[repr(C, packed)]
#[derive(Clone, Copy, Default)]
Expand Down Expand Up @@ -1489,21 +1431,4 @@ mod tests {
msg.flags |= 0x4;
assert!(!msg.is_valid());
}

#[test]
fn test_vhost_user_fs_backend() {
let mut fs_backend = VhostUserFSBackendMsg::default();

assert!(fs_backend.is_valid());

fs_backend.fd_offset[0] = 0xffff_ffff_ffff_ffff;
fs_backend.len[0] = 0x1;
assert!(!fs_backend.is_valid());

assert_ne!(
VhostUserFSBackendMsgFlags::MAP_R,
VhostUserFSBackendMsgFlags::MAP_W
);
assert_eq!(VhostUserFSBackendMsgFlags::EMPTY.bits(), 0);
}
}

0 comments on commit af987ba

Please sign in to comment.