diff --git a/vhost-user-backend/src/backend.rs b/vhost-user-backend/src/backend.rs index 07a3de9..9121b2e 100644 --- a/vhost-user-backend/src/backend.rs +++ b/vhost-user-backend/src/backend.rs @@ -89,9 +89,14 @@ pub trait VhostUserBackend: Send + Sync { /// Set handler for communicating with the frontend by the gpu specific backend communication /// channel. /// - /// This method only exits when the crate feature gpu-socket is enabled, because this is only - /// useful for a gpu device. - fn set_gpu_socket(&self, _gpu_backend: GpuBackend); + /// This function returns a `Result`, returning an error if the backend does not implement this + /// function. + fn set_gpu_socket(&self, _gpu_backend: GpuBackend) -> Result<()> { + Err(std::io::Error::new( + std::io::ErrorKind::Unsupported, + "backend does not support set_gpu_socket() / VHOST_USER_GPU_SET_SOCKET", + )) + } /// Get the map to map queue index to worker thread index. /// @@ -206,9 +211,14 @@ pub trait VhostUserBackendMut: Send + Sync { /// Set handler for communicating with the frontend by the gpu specific backend communication /// channel. /// - /// This method only exits when the crate feature gpu-socket is enabled, because this is only - /// useful for a gpu device. - fn set_gpu_socket(&mut self, gpu_backend: GpuBackend); + /// This function returns a `Result`, returning an error if the backend does not implement this + /// function. + fn set_gpu_socket(&mut self, _gpu_backend: GpuBackend) -> Result<()> { + Err(std::io::Error::new( + std::io::ErrorKind::Unsupported, + "backend does not support set_gpu_socket() / VHOST_USER_GPU_SET_SOCKET", + )) + } /// Get the map to map queue index to worker thread index. /// @@ -315,7 +325,7 @@ impl VhostUserBackend for Arc { self.deref().set_backend_req_fd(backend) } - fn set_gpu_socket(&self, gpu_backend: GpuBackend) { + fn set_gpu_socket(&self, gpu_backend: GpuBackend) -> Result<()> { self.deref().set_gpu_socket(gpu_backend) } @@ -396,7 +406,7 @@ impl VhostUserBackend for Mutex { self.lock().unwrap().set_backend_req_fd(backend) } - fn set_gpu_socket(&self, gpu_backend: GpuBackend) { + fn set_gpu_socket(&self, gpu_backend: GpuBackend) -> Result<()> { self.lock().unwrap().set_gpu_socket(gpu_backend) } @@ -480,7 +490,7 @@ impl VhostUserBackend for RwLock { self.write().unwrap().set_backend_req_fd(backend) } - fn set_gpu_socket(&self, gpu_backend: GpuBackend) { + fn set_gpu_socket(&self, gpu_backend: GpuBackend) -> Result<()> { self.write().unwrap().set_gpu_socket(gpu_backend) } @@ -604,8 +614,6 @@ pub mod tests { fn set_backend_req_fd(&mut self, _backend: Backend) {} - fn set_gpu_socket(&mut self, _gpu_backend: GpuBackend) {} - fn queues_per_thread(&self) -> Vec { vec![1, 1] } diff --git a/vhost-user-backend/src/handler.rs b/vhost-user-backend/src/handler.rs index 54fbc47..3986731 100644 --- a/vhost-user-backend/src/handler.rs +++ b/vhost-user-backend/src/handler.rs @@ -555,8 +555,10 @@ where self.backend.set_backend_req_fd(backend); } - fn set_gpu_socket(&mut self, gpu_backend: GpuBackend) { - self.backend.set_gpu_socket(gpu_backend); + fn set_gpu_socket(&mut self, gpu_backend: GpuBackend) -> VhostUserResult<()> { + self.backend + .set_gpu_socket(gpu_backend) + .map_err(VhostUserError::ReqHandlerError) } fn get_inflight_fd( diff --git a/vhost-user-backend/tests/vhost-user-server.rs b/vhost-user-backend/tests/vhost-user-server.rs index 213748b..7f78e2a 100644 --- a/vhost-user-backend/tests/vhost-user-server.rs +++ b/vhost-user-backend/tests/vhost-user-server.rs @@ -10,7 +10,6 @@ use std::thread; use vhost::vhost_user::message::{ VhostUserConfigFlags, VhostUserHeaderFlag, VhostUserInflight, VhostUserProtocolFeatures, }; -use vhost::vhost_user::GpuBackend; use vhost::vhost_user::{Backend, Frontend, Listener, VhostUserFrontend}; use vhost::{VhostBackend, VhostUserMemoryRegionInfo, VringConfigData}; use vhost_user_backend::{VhostUserBackendMut, VhostUserDaemon, VringRwLock}; @@ -78,8 +77,6 @@ impl VhostUserBackendMut for MockVhostBackend { Ok(()) } - fn set_gpu_socket(&mut self, _gpu_backend: GpuBackend) {} - fn update_memory(&mut self, atomic_mem: GuestMemoryAtomic) -> Result<()> { let mem = atomic_mem.memory(); let region = mem.find_region(GuestAddress(0x100000)).unwrap(); diff --git a/vhost/src/vhost_user/backend_req_handler.rs b/vhost/src/vhost_user/backend_req_handler.rs index ec0c9ac..22e893b 100644 --- a/vhost/src/vhost_user/backend_req_handler.rs +++ b/vhost/src/vhost_user/backend_req_handler.rs @@ -66,7 +66,7 @@ pub trait VhostUserBackendReqHandler { fn get_config(&self, offset: u32, size: u32, flags: VhostUserConfigFlags) -> Result>; fn set_config(&self, offset: u32, buf: &[u8], flags: VhostUserConfigFlags) -> Result<()>; fn set_backend_req_fd(&self, _backend: Backend) {} - fn set_gpu_socket(&self, gpu_backend: GpuBackend); + fn set_gpu_socket(&self, _gpu_backend: GpuBackend) -> Result<()>; fn get_inflight_fd(&self, inflight: &VhostUserInflight) -> Result<(VhostUserInflight, File)>; fn set_inflight_fd(&self, inflight: &VhostUserInflight, file: File) -> Result<()>; fn get_max_mem_slots(&self) -> Result; @@ -126,7 +126,7 @@ pub trait VhostUserBackendReqHandlerMut { ) -> Result>; fn set_config(&mut self, offset: u32, buf: &[u8], flags: VhostUserConfigFlags) -> Result<()>; fn set_backend_req_fd(&mut self, _backend: Backend) {} - fn set_gpu_socket(&mut self, _gpu_backend: GpuBackend); + fn set_gpu_socket(&mut self, _gpu_backend: GpuBackend) -> Result<()>; fn get_inflight_fd( &mut self, inflight: &VhostUserInflight, @@ -238,8 +238,8 @@ impl VhostUserBackendReqHandler for Mutex { self.lock().unwrap().set_backend_req_fd(backend) } - fn set_gpu_socket(&self, gpu_backend: GpuBackend) { - self.lock().unwrap().set_gpu_socket(gpu_backend); + fn set_gpu_socket(&self, gpu_backend: GpuBackend) -> Result<()> { + self.lock().unwrap().set_gpu_socket(gpu_backend) } fn get_inflight_fd(&self, inflight: &VhostUserInflight) -> Result<(VhostUserInflight, File)> { @@ -814,8 +814,7 @@ impl BackendReqHandler { // since we have no way to check this. If not, it will fail later. let sock = unsafe { UnixStream::from_raw_fd(file.into_raw_fd()) }; let gpu_backend = GpuBackend::from_stream(sock); - self.backend.set_gpu_socket(gpu_backend); - Ok(()) + self.backend.set_gpu_socket(gpu_backend) } fn handle_vring_fd_request( diff --git a/vhost/src/vhost_user/dummy_backend.rs b/vhost/src/vhost_user/dummy_backend.rs index e671f19..b83279b 100644 --- a/vhost/src/vhost_user/dummy_backend.rs +++ b/vhost/src/vhost_user/dummy_backend.rs @@ -259,7 +259,9 @@ impl VhostUserBackendReqHandlerMut for DummyBackendReqHandler { Ok(()) } - fn set_gpu_socket(&mut self, _gpu_backend: GpuBackend) {} + fn set_gpu_socket(&mut self, _gpu_backend: GpuBackend) -> Result<()> { + Ok(()) + } fn get_inflight_fd( &mut self,