-
Notifications
You must be signed in to change notification settings - Fork 82
vhost_user: Add support for SHMEM_MAP/UNMAP backend requests #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,16 @@ pub trait VhostUserFrontendReqHandler { | |
Err(std::io::Error::from_raw_os_error(libc::ENOSYS)) | ||
} | ||
|
||
/// Handle shared memory region mapping requests. | ||
fn shmem_map(&self, _req: &VhostUserMMap, _fd: &dyn AsRawFd) -> HandlerResult<u64> { | ||
Err(std::io::Error::from_raw_os_error(libc::ENOSYS)) | ||
} | ||
|
||
/// Handle shared memory region unmapping requests. | ||
fn shmem_unmap(&self, _req: &VhostUserMMap) -> HandlerResult<u64> { | ||
Err(std::io::Error::from_raw_os_error(libc::ENOSYS)) | ||
} | ||
mtjhrc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// fn handle_iotlb_msg(&mut self, iotlb: VhostUserIotlb); | ||
// fn handle_vring_host_notifier(&mut self, area: VhostUserVringArea, fd: &dyn AsRawFd); | ||
} | ||
|
@@ -84,6 +94,16 @@ pub trait VhostUserFrontendReqHandlerMut { | |
Err(std::io::Error::from_raw_os_error(libc::ENOSYS)) | ||
} | ||
|
||
/// Handle shared memory region mapping requests. | ||
fn shmem_map(&mut self, _req: &VhostUserMMap, _fd: &dyn AsRawFd) -> HandlerResult<u64> { | ||
Err(std::io::Error::from_raw_os_error(libc::ENOSYS)) | ||
} | ||
|
||
/// Handle shared memory region unmapping requests. | ||
fn shmem_unmap(&mut self, _req: &VhostUserMMap) -> 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); | ||
} | ||
|
@@ -111,6 +131,14 @@ impl<S: VhostUserFrontendReqHandlerMut> VhostUserFrontendReqHandler for Mutex<S> | |
) -> HandlerResult<u64> { | ||
self.lock().unwrap().shared_object_lookup(uuid, fd) | ||
} | ||
|
||
fn shmem_map(&self, req: &VhostUserMMap, fd: &dyn AsRawFd) -> HandlerResult<u64> { | ||
self.lock().unwrap().shmem_map(req, fd) | ||
} | ||
|
||
fn shmem_unmap(&self, req: &VhostUserMMap) -> HandlerResult<u64> { | ||
self.lock().unwrap().shmem_unmap(req) | ||
} | ||
} | ||
|
||
/// Server to handle service requests from backends from the backend communication channel. | ||
|
@@ -241,6 +269,18 @@ impl<S: VhostUserFrontendReqHandler> FrontendReqHandler<S> { | |
.shared_object_lookup(&msg, &files.unwrap()[0]) | ||
.map_err(Error::ReqHandlerError) | ||
} | ||
Ok(BackendReq::SHMEM_MAP) => { | ||
let msg = self.extract_msg_body::<VhostUserMMap>(&hdr, size, &buf)?; | ||
self.backend | ||
.shmem_map(&msg, &files.unwrap()[0]) | ||
.map_err(Error::ReqHandlerError) | ||
} | ||
Ok(BackendReq::SHMEM_UNMAP) => { | ||
let msg = self.extract_msg_body::<VhostUserMMap>(&hdr, size, &buf)?; | ||
self.backend | ||
.shmem_unmap(&msg) | ||
.map_err(Error::ReqHandlerError) | ||
} | ||
_ => Err(Error::InvalidMessage), | ||
}; | ||
|
||
|
@@ -278,7 +318,7 @@ impl<S: VhostUserFrontendReqHandler> FrontendReqHandler<S> { | |
files: &Option<Vec<File>>, | ||
) -> Result<()> { | ||
match hdr.get_code() { | ||
Ok(BackendReq::SHARED_OBJECT_LOOKUP) => { | ||
Ok(BackendReq::SHARED_OBJECT_LOOKUP | BackendReq::SHMEM_MAP) => { | ||
// Expect a single file is passed. | ||
match files { | ||
Some(files) if files.len() == 1 => Ok(()), | ||
|
@@ -356,6 +396,7 @@ mod tests { | |
use super::*; | ||
|
||
use std::collections::HashSet; | ||
use std::io::ErrorKind; | ||
|
||
use uuid::Uuid; | ||
|
||
|
@@ -366,12 +407,14 @@ mod tests { | |
|
||
struct MockFrontendReqHandler { | ||
shared_objects: HashSet<Uuid>, | ||
shmem_mappings: HashSet<(u64, u64)>, | ||
} | ||
|
||
impl MockFrontendReqHandler { | ||
fn new() -> Self { | ||
Self { | ||
shared_objects: HashSet::new(), | ||
shmem_mappings: HashSet::new(), | ||
} | ||
} | ||
} | ||
|
@@ -395,6 +438,82 @@ mod tests { | |
} | ||
Ok(1) | ||
} | ||
|
||
fn shmem_map(&mut self, req: &VhostUserMMap, _fd: &dyn AsRawFd) -> HandlerResult<u64> { | ||
assert_eq!({ req.shmid }, 0); | ||
Ok(!self.shmem_mappings.insert((req.shm_offset, req.len)) as u64) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think it's better to remove the negation (!) and use a direct check to return meaningful values. e.g There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By braces do you mean In regards to the negation with cast instead of explicit fn shared_object_add(&mut self, uuid: &VhostUserSharedMsg) -> HandlerResult<u64> {
Ok(!self.shared_objects.insert(uuid.uuid) as u64)
}
fn shared_object_remove(&mut self, uuid: &VhostUserSharedMsg) -> HandlerResult<u64> {
Ok(!self.shared_objects.remove(&uuid.uuid) as u64)
} I am not sure if I should change it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ah right!
doesn't necessarily mean it's the correct approach. |
||
} | ||
|
||
fn shmem_unmap(&mut self, req: &VhostUserMMap) -> HandlerResult<u64> { | ||
assert_eq!({ req.shmid }, 0); | ||
Ok(!self.shmem_mappings.remove(&(req.shm_offset, req.len)) as u64) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
} | ||
} | ||
|
||
#[test] | ||
fn test_default_frontend_impl() { | ||
struct Frontend; | ||
impl VhostUserFrontendReqHandler for Frontend {} | ||
|
||
let f = Frontend; | ||
assert_eq!( | ||
f.shared_object_add(&Default::default()).unwrap_err().kind(), | ||
ErrorKind::Unsupported | ||
); | ||
assert_eq!( | ||
f.shared_object_remove(&Default::default()) | ||
.unwrap_err() | ||
.kind(), | ||
ErrorKind::Unsupported | ||
); | ||
assert_eq!( | ||
f.shared_object_lookup(&Default::default(), &0) | ||
.unwrap_err() | ||
.kind(), | ||
ErrorKind::Unsupported | ||
); | ||
|
||
assert_eq!( | ||
f.shmem_map(&Default::default(), &0).unwrap_err().kind(), | ||
ErrorKind::Unsupported | ||
); | ||
assert_eq!( | ||
f.shmem_unmap(&Default::default()).unwrap_err().kind(), | ||
ErrorKind::Unsupported | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_default_frontend_impl_mut() { | ||
struct FrontendMut; | ||
impl VhostUserFrontendReqHandlerMut for FrontendMut {} | ||
|
||
let mut f = FrontendMut; | ||
assert_eq!( | ||
f.shared_object_add(&Default::default()).unwrap_err().kind(), | ||
ErrorKind::Unsupported | ||
); | ||
assert_eq!( | ||
f.shared_object_remove(&Default::default()) | ||
.unwrap_err() | ||
.kind(), | ||
ErrorKind::Unsupported | ||
); | ||
assert_eq!( | ||
f.shared_object_lookup(&Default::default(), &0) | ||
.unwrap_err() | ||
.kind(), | ||
ErrorKind::Unsupported | ||
); | ||
|
||
assert_eq!( | ||
f.shmem_map(&Default::default(), &0).unwrap_err().kind(), | ||
ErrorKind::Unsupported | ||
); | ||
assert_eq!( | ||
f.shmem_unmap(&Default::default()).unwrap_err().kind(), | ||
ErrorKind::Unsupported | ||
); | ||
} | ||
|
||
#[test] | ||
|
@@ -436,6 +555,13 @@ mod tests { | |
assert_eq!(handler.handle_request().unwrap(), 1); | ||
assert_eq!(handler.handle_request().unwrap(), 0); | ||
assert_eq!(handler.handle_request().unwrap(), 1); | ||
|
||
// Testing shmem map/unmap messages. | ||
assert_eq!(handler.handle_request().unwrap(), 0); | ||
assert_eq!(handler.handle_request().unwrap(), 1); | ||
assert_eq!(handler.handle_request().unwrap(), 0); | ||
assert_eq!(handler.handle_request().unwrap(), 0); | ||
assert_eq!(handler.handle_request().unwrap(), 0); | ||
}); | ||
|
||
backend.set_shared_object_flag(true); | ||
|
@@ -456,6 +582,24 @@ mod tests { | |
.is_ok()); | ||
assert!(backend.shared_object_remove(&shobj_msg).is_ok()); | ||
assert!(backend.shared_object_remove(&shobj_msg).is_ok()); | ||
|
||
let (_, some_fd_to_map) = UnixStream::pair().unwrap(); | ||
let map_request1 = VhostUserMMap { | ||
shm_offset: 0, | ||
len: 4096, | ||
..Default::default() | ||
}; | ||
let map_request2 = VhostUserMMap { | ||
shm_offset: 4096, | ||
len: 8192, | ||
..Default::default() | ||
}; | ||
backend.shmem_map(&map_request1, &some_fd_to_map).unwrap(); | ||
backend.shmem_unmap(&map_request2).unwrap(); | ||
backend.shmem_map(&map_request2, &some_fd_to_map).unwrap(); | ||
backend.shmem_unmap(&map_request2).unwrap(); | ||
backend.shmem_unmap(&map_request1).unwrap(); | ||
|
||
// Ensure that the handler thread did not panic. | ||
assert!(frontend_handler.join().is_ok()); | ||
} | ||
|
@@ -485,6 +629,13 @@ mod tests { | |
assert_eq!(handler.handle_request().unwrap(), 1); | ||
assert_eq!(handler.handle_request().unwrap(), 0); | ||
assert_eq!(handler.handle_request().unwrap(), 1); | ||
|
||
// Testing shmem map/unmap messages. | ||
assert_eq!(handler.handle_request().unwrap(), 0); | ||
assert_eq!(handler.handle_request().unwrap(), 1); | ||
assert_eq!(handler.handle_request().unwrap(), 0); | ||
assert_eq!(handler.handle_request().unwrap(), 0); | ||
assert_eq!(handler.handle_request().unwrap(), 0); | ||
}); | ||
|
||
backend.set_reply_ack_flag(true); | ||
|
@@ -506,6 +657,24 @@ mod tests { | |
.is_err()); | ||
assert!(backend.shared_object_remove(&shobj_msg).is_ok()); | ||
assert!(backend.shared_object_remove(&shobj_msg).is_err()); | ||
|
||
let (_, some_fd_to_map) = UnixStream::pair().unwrap(); | ||
let map_request1 = VhostUserMMap { | ||
shm_offset: 0, | ||
len: 4096, | ||
..Default::default() | ||
}; | ||
let map_request2 = VhostUserMMap { | ||
shm_offset: 4096, | ||
len: 8192, | ||
..Default::default() | ||
}; | ||
backend.shmem_map(&map_request1, &some_fd_to_map).unwrap(); | ||
backend.shmem_unmap(&map_request2).unwrap_err(); | ||
backend.shmem_map(&map_request2, &some_fd_to_map).unwrap(); | ||
backend.shmem_unmap(&map_request2).unwrap(); | ||
backend.shmem_unmap(&map_request1).unwrap(); | ||
|
||
// Ensure that the handler thread did not panic. | ||
assert!(frontend_handler.join().is_ok()); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.