Skip to content
This repository was archived by the owner on Oct 24, 2022. It is now read-only.

Commit ed929c0

Browse files
committed
vring: introduce trait VringT
Introduce trait VringT, and provide three implementations of it: VringState, VringMutex, VringRwLock. Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
1 parent f363fb2 commit ed929c0

File tree

5 files changed

+366
-115
lines changed

5 files changed

+366
-115
lines changed

src/backend.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use vhost::vhost_user::SlaveFsCacheReq;
2828
use vm_memory::bitmap::Bitmap;
2929
use vmm_sys_util::eventfd::EventFd;
3030

31-
use super::{Vring, GM};
31+
use super::{VringRwLock, GM};
3232

3333
/// Trait with interior mutability for vhost user backend servers to implement concrete services.
3434
///
@@ -107,7 +107,7 @@ pub trait VhostUserBackend<B: Bitmap + 'static = ()>: Send + Sync + 'static {
107107
&self,
108108
device_event: u16,
109109
evset: epoll::Events,
110-
vrings: &[Vring<GM<B>>],
110+
vrings: &[VringRwLock<GM<B>>],
111111
thread_id: usize,
112112
) -> result::Result<bool, io::Error>;
113113
}
@@ -186,7 +186,7 @@ pub trait VhostUserBackendMut<B: Bitmap + 'static = ()>: Send + Sync + 'static {
186186
&mut self,
187187
device_event: u16,
188188
evset: epoll::Events,
189-
vrings: &[Vring<GM<B>>],
189+
vrings: &[VringRwLock<GM<B>>],
190190
thread_id: usize,
191191
) -> result::Result<bool, io::Error>;
192192
}
@@ -244,7 +244,7 @@ impl<T: VhostUserBackend<B>, B: Bitmap + 'static> VhostUserBackend<B> for Arc<T>
244244
&self,
245245
device_event: u16,
246246
evset: epoll::Events,
247-
vrings: &[Vring<GM<B>>],
247+
vrings: &[VringRwLock<GM<B>>],
248248
thread_id: usize,
249249
) -> Result<bool, io::Error> {
250250
self.deref()
@@ -305,7 +305,7 @@ impl<T: VhostUserBackendMut<B>, B: Bitmap + 'static> VhostUserBackend<B> for Mut
305305
&self,
306306
device_event: u16,
307307
evset: epoll::Events,
308-
vrings: &[Vring<GM<B>>],
308+
vrings: &[VringRwLock<GM<B>>],
309309
thread_id: usize,
310310
) -> Result<bool, io::Error> {
311311
self.lock()
@@ -367,7 +367,7 @@ impl<T: VhostUserBackendMut<B>, B: Bitmap + 'static> VhostUserBackend<B> for RwL
367367
&self,
368368
device_event: u16,
369369
evset: epoll::Events,
370-
vrings: &[Vring<GM<B>>],
370+
vrings: &[VringRwLock<GM<B>>],
371371
thread_id: usize,
372372
) -> Result<bool, io::Error> {
373373
self.write()
@@ -463,7 +463,7 @@ pub mod tests {
463463
&mut self,
464464
_device_event: u16,
465465
_evset: Events,
466-
_vrings: &[Vring],
466+
_vrings: &[VringRwLock],
467467
_thread_id: usize,
468468
) -> Result<bool, Error> {
469469
self.events += 1;

src/event_loop.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use std::result;
1212
use vm_memory::bitmap::Bitmap;
1313
use vmm_sys_util::eventfd::EventFd;
1414

15-
use super::{VhostUserBackend, Vring, GM};
15+
use super::vring::VringT;
16+
use super::{VhostUserBackend, VringRwLock, GM};
1617

1718
/// Errors related to vring epoll event handling.
1819
#[derive(Debug)]
@@ -59,7 +60,7 @@ pub type VringEpollResult<T> = std::result::Result<T, VringEpollError>;
5960
pub struct VringEpollHandler<S: VhostUserBackend<B>, B: Bitmap + 'static> {
6061
epoll_file: File,
6162
backend: S,
62-
vrings: Vec<Vring<GM<B>>>,
63+
vrings: Vec<VringRwLock<GM<B>>>,
6364
thread_id: usize,
6465
exit_event_fd: Option<EventFd>,
6566
exit_event_id: Option<u16>,
@@ -69,7 +70,7 @@ impl<S: VhostUserBackend<B>, B: Bitmap + 'static> VringEpollHandler<S, B> {
6970
/// Create a `VringEpollHandler` instance.
7071
pub(crate) fn new(
7172
backend: S,
72-
vrings: Vec<Vring<GM<B>>>,
73+
vrings: Vec<VringRwLock<GM<B>>>,
7374
thread_id: usize,
7475
) -> VringEpollResult<Self> {
7576
let epoll_fd = epoll::create(true).map_err(VringEpollError::EpollCreateFd)?;
@@ -234,7 +235,7 @@ mod tests {
234235
let mem = GuestMemoryAtomic::new(
235236
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
236237
);
237-
let vring = Vring::new(mem, 0x1000);
238+
let vring = VringRwLock::new(mem, 0x1000);
238239
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
239240

240241
let handler = VringEpollHandler::new(backend, vec![vring], 0x1).unwrap();

src/handler.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ use vhost::vhost_user::message::{
1515
VhostUserSingleMemoryRegion, VhostUserVirtioFeatures, VhostUserVringAddrFlags,
1616
VhostUserVringState,
1717
};
18-
use vhost::vhost_user::{Error as VhostUserError, Result as VhostUserResult, SlaveFsCacheReq};
18+
use vhost::vhost_user::{
19+
Error as VhostUserError, Result as VhostUserResult, SlaveFsCacheReq,
20+
VhostUserSlaveReqHandlerMut,
21+
};
1922
use virtio_bindings::bindings::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
2023
use vm_memory::bitmap::Bitmap;
2124
use vm_memory::mmap::NewBitmap;
22-
use vm_memory::{FileOffset, GuestAddress, GuestMemoryMmap, GuestRegionMmap};
25+
use vm_memory::{
26+
FileOffset, GuestAddress, GuestAddressSpace, GuestMemoryMmap, GuestRegionMmap, MmapRegion,
27+
};
2328

29+
use super::backend::VhostUserBackend;
30+
use super::event_loop::VringEpollHandler;
2431
use super::event_loop::{VringEpollError, VringEpollResult};
25-
use super::*;
32+
use super::vring::{VringRwLock, VringT};
33+
use super::GM;
2634

2735
const MAX_MEM_SLOTS: u64 = 32;
2836

@@ -74,7 +82,7 @@ pub struct VhostUserHandler<S: VhostUserBackend<B>, B: Bitmap + 'static> {
7482
queues_per_thread: Vec<u64>,
7583
mappings: Vec<AddrMapping>,
7684
atomic_mem: GM<B>,
77-
vrings: Vec<Vring<GM<B>>>,
85+
vrings: Vec<VringRwLock<GM<B>>>,
7886
worker_threads: Vec<thread::JoinHandle<VringEpollResult<()>>>,
7987
}
8088

@@ -86,7 +94,7 @@ impl<S: VhostUserBackend<B> + Clone, B: Bitmap + Clone + Send + Sync> VhostUserH
8694

8795
let mut vrings = Vec::new();
8896
for _ in 0..num_queues {
89-
let vring = Vring::new(atomic_mem.clone(), max_queue_size as u16);
97+
let vring = VringRwLock::new(atomic_mem.clone(), max_queue_size as u16);
9098
vrings.push(vring);
9199
}
92100

@@ -194,7 +202,7 @@ impl<S: VhostUserBackend<B>, B: NewBitmap + Clone> VhostUserSlaveReqHandlerMut
194202
// been disabled by VHOST_USER_SET_VRING_ENABLE with parameter 0.
195203
let vring_enabled =
196204
self.acked_features & VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits() == 0;
197-
for vring in self.vrings.iter() {
205+
for vring in self.vrings.iter_mut() {
198206
vring.set_enabled(vring_enabled);
199207
}
200208

src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ use std::result;
1414
use std::sync::{Arc, Mutex};
1515
use std::thread;
1616

17-
use vhost::vhost_user::{
18-
Error as VhostUserError, Listener, SlaveListener, VhostUserSlaveReqHandlerMut,
19-
};
17+
use vhost::vhost_user::{Error as VhostUserError, Listener, SlaveListener};
2018
use vm_memory::bitmap::Bitmap;
2119
use vm_memory::mmap::NewBitmap;
22-
use vm_memory::{GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap, MmapRegion};
20+
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};
2321

2422
use self::handler::VhostUserHandler;
2523

@@ -33,7 +31,7 @@ mod handler;
3331
pub use self::handler::VhostUserHandlerError;
3432

3533
mod vring;
36-
pub use self::vring::{Vring, VringState};
34+
pub use self::vring::{VringRwLock, VringState};
3735

3836
/// An alias for `GuestMemoryAtomic<GuestMemoryMmap<B>>` to simplify code.
3937
type GM<B> = GuestMemoryAtomic<GuestMemoryMmap<B>>;

0 commit comments

Comments
 (0)