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 <techiepriyansh@gmail.com>
This commit is contained in:
Priyansh Rathi 2023-04-11 20:40:05 +05:30 committed by Viresh Kumar
parent 62268560c8
commit f52cc9cfff
3 changed files with 16 additions and 21 deletions

View File

@ -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<VsockArgs> 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());
}
}

View File

@ -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<VringRwLock, ()> for VhostUserVsockBackend {
impl VhostUserBackend<VringRwLock, ()> for VhostUserVsockBackend {
fn num_queues(&self) -> usize {
NUM_QUEUES
}
@ -246,13 +246,13 @@ impl VhostUserBackendMut<VringRwLock, ()> 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<GuestMemoryMmap>) -> IoResult<()> {
fn update_memory(&self, atomic_mem: GuestMemoryAtomic<GuestMemoryMmap>) -> IoResult<()> {
for thread in self.threads.iter() {
thread.lock().unwrap().mem = Some(atomic_mem.clone());
}
@ -260,7 +260,7 @@ impl VhostUserBackendMut<VringRwLock, ()> 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(),
);

View File

@ -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<RwLock<VhostUserVsockBackend>>;
type ArcVhostBknd = Arc<VhostUserVsockBackend>;
pub(crate) struct VhostUserVsockThread {
/// Guest memory map.