From f52cc9cfff98ecce4b9718e0f860ca5902c31ba0 Mon Sep 17 00:00:00 2001 From: Priyansh Rathi Date: Tue, 11 Apr 2023 20:40:05 +0530 Subject: [PATCH] vsock: Implement VhostUserBackend for VhostUserVsockBackend Implement VhostUserBackend instead of VhostUserBackendMut for VhostUserVsockBackend. VhostUserBackendMut trait is supposed to be used for structures without interior mutability. But VhostUserVsockBackend already uses Mutex to protect its threads, so it can implement the trait with interior mutability (i.e. VhostUserBackend). Signed-off-by: Priyansh Rathi --- crates/vsock/src/main.rs | 19 +++++++------------ crates/vsock/src/vhu_vsock.rs | 14 +++++++------- crates/vsock/src/vhu_vsock_thread.rs | 4 ++-- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/crates/vsock/src/main.rs b/crates/vsock/src/main.rs index 3d36c53..0f192f9 100644 --- a/crates/vsock/src/main.rs +++ b/crates/vsock/src/main.rs @@ -8,10 +8,7 @@ mod vhu_vsock; mod vhu_vsock_thread; mod vsock_conn; -use std::{ - convert::TryFrom, - sync::{Arc, RwLock}, -}; +use std::{convert::TryFrom, sync::Arc}; use clap::Parser; use log::{info, warn}; @@ -52,9 +49,7 @@ impl TryFrom for VsockConfig { /// vhost-user-vsock backend server. pub(crate) fn start_backend_server(config: VsockConfig) { loop { - let backend = Arc::new(RwLock::new( - VhostUserVsockBackend::new(config.clone()).unwrap(), - )); + let backend = Arc::new(VhostUserVsockBackend::new(config.clone()).unwrap()); let listener = Listener::new(config.get_socket_path(), true).unwrap(); @@ -67,7 +62,7 @@ pub(crate) fn start_backend_server(config: VsockConfig) { let mut vring_workers = daemon.get_epoll_handlers(); - for thread in backend.read().unwrap().threads.iter() { + for thread in backend.threads.iter() { thread .lock() .unwrap() @@ -89,7 +84,7 @@ pub(crate) fn start_backend_server(config: VsockConfig) { } // No matter the result, we need to shut down the worker thread. - backend.read().unwrap().exit_event.write(1).unwrap(); + backend.exit_event.write(1).unwrap(); } } @@ -142,7 +137,7 @@ mod tests { VSOCK_SOCKET_PATH.to_string(), ); - let backend = Arc::new(RwLock::new(VhostUserVsockBackend::new(config).unwrap())); + let backend = Arc::new(VhostUserVsockBackend::new(config).unwrap()); let daemon = VhostUserDaemon::new( String::from("vhost-user-vsock"), @@ -154,8 +149,8 @@ mod tests { let vring_workers = daemon.get_epoll_handlers(); // VhostUserVsockBackend support a single thread that handles the TX and RX queues - assert_eq!(backend.read().unwrap().threads.len(), 1); + assert_eq!(backend.threads.len(), 1); - assert_eq!(vring_workers.len(), backend.read().unwrap().threads.len()); + assert_eq!(vring_workers.len(), backend.threads.len()); } } diff --git a/crates/vsock/src/vhu_vsock.rs b/crates/vsock/src/vhu_vsock.rs index 102b3d1..41eea0c 100644 --- a/crates/vsock/src/vhu_vsock.rs +++ b/crates/vsock/src/vhu_vsock.rs @@ -8,7 +8,7 @@ use std::{ use thiserror::Error as ThisError; use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures}; -use vhost_user_backend::{VhostUserBackendMut, VringRwLock}; +use vhost_user_backend::{VhostUserBackend, VringRwLock}; use virtio_bindings::bindings::{ virtio_config::VIRTIO_F_NOTIFY_ON_EMPTY, virtio_config::VIRTIO_F_VERSION_1, virtio_ring::VIRTIO_RING_F_EVENT_IDX, @@ -226,7 +226,7 @@ impl VhostUserVsockBackend { } } -impl VhostUserBackendMut for VhostUserVsockBackend { +impl VhostUserBackend for VhostUserVsockBackend { fn num_queues(&self) -> usize { NUM_QUEUES } @@ -246,13 +246,13 @@ impl VhostUserBackendMut for VhostUserVsockBackend { VhostUserProtocolFeatures::CONFIG } - fn set_event_idx(&mut self, enabled: bool) { + fn set_event_idx(&self, enabled: bool) { for thread in self.threads.iter() { thread.lock().unwrap().event_idx = enabled; } } - fn update_memory(&mut self, atomic_mem: GuestMemoryAtomic) -> IoResult<()> { + fn update_memory(&self, atomic_mem: GuestMemoryAtomic) -> IoResult<()> { for thread in self.threads.iter() { thread.lock().unwrap().mem = Some(atomic_mem.clone()); } @@ -260,7 +260,7 @@ impl VhostUserBackendMut for VhostUserVsockBackend { } fn handle_event( - &mut self, + &self, device_event: u16, evset: EventSet, vrings: &[VringRwLock], @@ -344,7 +344,7 @@ mod tests { let backend = VhostUserVsockBackend::new(config); assert!(backend.is_ok()); - let mut backend = backend.unwrap(); + let backend = backend.unwrap(); assert_eq!(backend.num_queues(), NUM_QUEUES); assert_eq!(backend.max_queue_size(), QUEUE_SIZE); @@ -422,7 +422,7 @@ mod tests { VSOCK_SOCKET_PATH.to_string(), ); - let mut backend = VhostUserVsockBackend::new(config).unwrap(); + let backend = VhostUserVsockBackend::new(config).unwrap(); let mem = GuestMemoryAtomic::new( GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0), 0x10000)]).unwrap(), ); diff --git a/crates/vsock/src/vhu_vsock_thread.rs b/crates/vsock/src/vhu_vsock_thread.rs index 93d898f..e9a95b4 100644 --- a/crates/vsock/src/vhu_vsock_thread.rs +++ b/crates/vsock/src/vhu_vsock_thread.rs @@ -10,7 +10,7 @@ use std::{ net::{UnixListener, UnixStream}, prelude::{AsRawFd, FromRawFd, RawFd}, }, - sync::{Arc, RwLock}, + sync::Arc, }; use futures::executor::{ThreadPool, ThreadPoolBuilder}; @@ -31,7 +31,7 @@ use crate::{ vsock_conn::*, }; -type ArcVhostBknd = Arc>; +type ArcVhostBknd = Arc; pub(crate) struct VhostUserVsockThread { /// Guest memory map.