Skip to content

Commit

Permalink
add QueueStateOwnedT
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Agache <[email protected]>
  • Loading branch information
alexandruag committed Mar 4, 2022
1 parent 0d1e4c5 commit 16f4894
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
17 changes: 17 additions & 0 deletions crates/virtio-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,20 @@ pub trait QueueStateT: for<'a> QueueStateGuard<'a> {
M: Clone + Deref,
M::Target: GuestMemory;
}

/// Trait to access and manipulate a Virtio queue that's known to be exclusively accessed
/// by a single execution thread.
pub trait QueueStateOwnedT: QueueStateT {
/// Get a consuming iterator over all available descriptor chain heads offered by the driver.
///
/// # Arguments
/// * `mem` - the `GuestMemory` object that can be used to access the queue buffers.
fn iter<M>(&mut self, mem: M) -> Result<AvailIter<'_, M>, Error>
where
M: Deref,
M::Target: GuestMemory;

/// Undo the last advancement of the next available index field by decrementing its
/// value by one.
fn go_to_previous_position(&mut self);
}
4 changes: 3 additions & 1 deletion crates/virtio-queue/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use std::sync::atomic::Ordering;

use vm_memory::GuestAddressSpace;

use crate::{AvailIter, Error, QueueGuard, QueueState, QueueStateGuard, QueueStateT};
use crate::{
AvailIter, Error, QueueGuard, QueueState, QueueStateGuard, QueueStateOwnedT, QueueStateT,
};

/// A convenient wrapper struct for a virtio queue, with associated `GuestMemory` object.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/virtio-queue/src/queue_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::atomic::Ordering;

use vm_memory::GuestMemory;

use crate::{AvailIter, Error, QueueState, QueueStateT};
use crate::{AvailIter, Error, QueueState, QueueStateOwnedT, QueueStateT};

/// A guard object to exclusively access an `Queue` object.
///
Expand Down
40 changes: 21 additions & 19 deletions crates/virtio-queue/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::defs::{
VIRTQ_USED_RING_META_SIZE,
};
use crate::{
error, AvailIter, Descriptor, DescriptorChain, Error, QueueStateGuard, QueueStateT,
VirtqUsedElem,
error, AvailIter, Descriptor, DescriptorChain, Error, QueueStateGuard, QueueStateOwnedT,
QueueStateT, VirtqUsedElem,
};

/// Struct to maintain information and manipulate state of a virtio queue.
Expand Down Expand Up @@ -59,19 +59,6 @@ pub struct QueueState {
}

impl QueueState {
/// Get a consuming iterator over all available descriptor chain heads offered by the driver.
///
/// # Arguments
/// * `mem` - the `GuestMemory` object that can be used to access the queue buffers.
pub fn iter<M>(&mut self, mem: M) -> Result<AvailIter<'_, M>, Error>
where
M: Deref,
M::Target: GuestMemory,
{
self.avail_idx(mem.deref(), Ordering::Acquire)
.map(move |idx| AvailIter::new(mem, idx, self))
}

// Helper method that writes `val` to the `avail_event` field of the used ring, using
// the provided ordering.
fn set_avail_event<M: GuestMemory>(
Expand Down Expand Up @@ -432,14 +419,14 @@ impl QueueStateT for QueueState {
self.next_avail.0
}

fn next_used(&self) -> u16 {
self.next_used.0
}

fn set_next_avail(&mut self, next_avail: u16) {
self.next_avail = Wrapping(next_avail);
}

fn next_used(&self) -> u16 {
self.next_used.0
}

fn set_next_used(&mut self, next_used: u16) {
self.next_used = Wrapping(next_used);
}
Expand All @@ -453,3 +440,18 @@ impl QueueStateT for QueueState {
self.iter(mem).ok()?.next()
}
}

impl QueueStateOwnedT for QueueState {
fn iter<M>(&mut self, mem: M) -> Result<AvailIter<'_, M>, Error>
where
M: Deref,
M::Target: GuestMemory,
{
self.avail_idx(mem.deref(), Ordering::Acquire)
.map(move |idx| AvailIter::new(mem, idx, self))
}

fn go_to_previous_position(&mut self) {
self.next_avail -= Wrapping(1);
}
}

0 comments on commit 16f4894

Please sign in to comment.