Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": 85.41,
"coverage_score": 86.22,
"exclude_path": "vhost/src/vhost_kern/",
"crate_features": "vhost/vhost-user-frontend,vhost/vhost-user-backend,vhost-user-backend/postcopy"
}
3 changes: 3 additions & 0 deletions vhost-user-backend/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,9 @@ where
if self.acked_protocol_features & VhostUserProtocolFeatures::SHARED_OBJECT.bits() != 0 {
backend.set_shared_object_flag(true);
}
if self.acked_protocol_features & VhostUserProtocolFeatures::SHMEM.bits() != 0 {
backend.set_shmem_flag(true);
}
self.backend.set_backend_req_fd(backend);
}

Expand Down
2 changes: 2 additions & 0 deletions vhost/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## [Unreleased]

### Added
- [[#251]](https://github.com/rust-vmm/vhost/pull/251) Add `SHMEM_MAP` and `SHMEM_UNMAP` support

### Changed
### Deprecated
### Fixed
Expand Down
101 changes: 94 additions & 7 deletions vhost/src/vhost_user/backend_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ struct BackendInternal {
// Protocol feature VHOST_USER_PROTOCOL_F_SHARED_OBJECT has been negotiated.
shared_object_negotiated: bool,

// Protocol feature VHOST_USER_PROTOCOL_F_SHMEM has been negotiated.
shmem_negotiated: bool,

// whether the endpoint has encountered any failure
error: Option<i32>,
}
Expand Down Expand Up @@ -92,6 +95,7 @@ impl Backend {
sock: ep,
reply_ack_negotiated: false,
shared_object_negotiated: false,
shmem_negotiated: false,
error: None,
})),
}
Expand Down Expand Up @@ -136,6 +140,14 @@ impl Backend {
self.node().shared_object_negotiated = enable;
}

/// Set the negotiation state of the `VHOST_USER_PROTOCOL_F_SHMEM` protocol feature.
///
/// When the `VHOST_USER_PROTOCOL_F_SHMEM` protocol feature has been negotiated,
/// the backend is allowed to send "SHMEM_{MAP, UNMAP}" messages to the frontend.
pub fn set_shmem_flag(&self, enable: bool) {
self.node().shmem_negotiated = enable;
}

/// Mark endpoint as failed with specified error code.
pub fn set_failed(&self, error: i32) {
self.node().error = Some(error);
Expand Down Expand Up @@ -174,6 +186,22 @@ impl VhostUserFrontendReqHandler for Backend {
Some(&[fd.as_raw_fd()]),
)
}

/// Forward vhost-user memory map file request to the frontend.
fn shmem_map(&self, req: &VhostUserMMap, fd: &dyn AsRawFd) -> HandlerResult<u64> {
if !self.node().shmem_negotiated {
return Err(io::Error::other("SHMEM feature not negotiated"));
}
self.send_message(BackendReq::SHMEM_MAP, req, Some(&[fd.as_raw_fd()]))
}

/// Forward vhost-user memory unmap file request to the frontend.
fn shmem_unmap(&self, req: &VhostUserMMap) -> HandlerResult<u64> {
if !self.node().shmem_negotiated {
return Err(io::Error::other("SHMEM feature not negotiated"));
}
self.send_message(BackendReq::SHMEM_UNMAP, req, None)
}
}

#[cfg(test)]
Expand All @@ -182,10 +210,16 @@ mod tests {

use super::*;

fn frontend_backend_pair() -> (Endpoint<VhostUserMsgHeader<BackendReq>>, Backend) {
let (p1, p2) = UnixStream::pair().unwrap();
let backend = Backend::from_stream(p1);
let frontend = Endpoint::<VhostUserMsgHeader<BackendReq>>::from_stream(p2);
(frontend, backend)
}

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

assert!(backend.node().error.is_none());
backend.set_failed(libc::EAGAIN);
Expand All @@ -194,8 +228,7 @@ mod tests {

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

backend.set_failed(libc::ECONNRESET);
backend
Expand All @@ -209,9 +242,7 @@ mod tests {

#[test]
fn test_backend_req_recv_negative() {
let (p1, p2) = UnixStream::pair().unwrap();
let backend = Backend::from_stream(p1);
let mut frontend = Endpoint::<VhostUserMsgHeader<BackendReq>>::from_stream(p2);
let (mut frontend, backend) = frontend_backend_pair();

let len = mem::size_of::<VhostUserSharedMsg>();
let mut hdr = VhostUserMsgHeader::new(
Expand Down Expand Up @@ -257,4 +288,60 @@ mod tests {
.shared_object_add(&VhostUserSharedMsg::default())
.unwrap();
}

#[test]
fn test_shmem_map() {
let (mut frontend, backend) = frontend_backend_pair();

let (_, some_fd_to_send) = UnixStream::pair().unwrap();
let map_request = VhostUserMMap {
shmid: 0,
padding: Default::default(),
fd_offset: 0,
shm_offset: 1028,
len: 4096,
flags: VhostUserMMapFlags::WRITABLE.bits(),
};

// Feature not negotiated -> fails
backend
.shmem_map(&map_request, &some_fd_to_send)
.unwrap_err();

backend.set_shmem_flag(true);
backend.shmem_map(&map_request, &some_fd_to_send).unwrap();

let (hdr, request, fd) = frontend.recv_body::<VhostUserMMap>().unwrap();
assert_eq!(hdr.get_code().unwrap(), BackendReq::SHMEM_MAP);
assert!(fd.is_some());
assert_eq!({ request.shm_offset }, { map_request.shm_offset });
assert_eq!({ request.len }, { map_request.len });
assert_eq!({ request.flags }, { map_request.flags });
}

#[test]
fn test_shmem_unmap() {
let (mut frontend, backend) = frontend_backend_pair();

let unmap_request = VhostUserMMap {
shmid: 0,
padding: Default::default(),
fd_offset: 0,
shm_offset: 1028,
len: 4096,
flags: 0,
};

// Feature not negotiated -> fails
backend.shmem_unmap(&unmap_request).unwrap_err();

backend.set_shmem_flag(true);
backend.shmem_unmap(&unmap_request).unwrap();

let (hdr, request, fd) = frontend.recv_body::<VhostUserMMap>().unwrap();
assert_eq!(hdr.get_code().unwrap(), BackendReq::SHMEM_UNMAP);
assert!(fd.is_none());
assert_eq!({ request.shm_offset }, { unmap_request.shm_offset });
assert_eq!({ request.len }, { unmap_request.len });
}
}
Loading