Struct virtio_queue::Queue
source · pub struct Queue { /* private fields */ }
Expand description
Struct to maintain information and manipulate a virtio queue.
§Example
use virtio_queue::{Queue, QueueOwnedT, QueueT};
use vm_memory::{Bytes, GuestAddress, GuestAddressSpace, GuestMemoryMmap};
let m = GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0), 0x10000)]).unwrap();
let mut queue = Queue::new(1024).unwrap();
// First, the driver sets up the queue; this set up is done via writes on the bus (PCI, MMIO).
queue.set_size(8);
queue.set_desc_table_address(Some(0x1000), None);
queue.set_avail_ring_address(Some(0x2000), None);
queue.set_used_ring_address(Some(0x3000), None);
queue.set_event_idx(true);
queue.set_ready(true);
// The user should check if the queue is valid before starting to use it.
assert!(queue.is_valid(&m));
// Here the driver would add entries in the available ring and then update the `idx` field of
// the available ring (address = 0x2000 + 2).
m.write_obj(3, GuestAddress(0x2002));
loop {
queue.disable_notification(&m).unwrap();
// Consume entries from the available ring.
while let Some(chain) = queue.iter(&m).unwrap().next() {
// Process the descriptor chain, and then add an entry in the used ring and optionally
// notify the driver.
queue.add_used(&m, chain.head_index(), 0x100).unwrap();
if queue.needs_notification(&m).unwrap() {
// Here we would notify the driver it has new entries in the used ring to consume.
}
}
if !queue.enable_notification(&m).unwrap() {
break;
}
}
// We can reset the queue at some point.
queue.reset();
// The queue should not be ready after reset.
assert!(!queue.ready());
Implementations§
source§impl Queue
impl Queue
sourcepub fn try_set_size(&mut self, size: u16) -> Result<(), Error>
pub fn try_set_size(&mut self, size: u16) -> Result<(), Error>
Equivalent of QueueT::set_size
returning an error in case of invalid size.
This should not be directly used, as the preferred method is part of the QueueT
interface. This is a convenience function for implementing save/restore capabilities.
sourcepub fn try_set_desc_table_address(
&mut self,
desc_table: GuestAddress
) -> Result<(), Error>
pub fn try_set_desc_table_address( &mut self, desc_table: GuestAddress ) -> Result<(), Error>
Tries to set the descriptor table address. In case of an invalid value, the address is not updated.
This should not be directly used, as the preferred method is
QueueT::set_desc_table_address
. This is a convenience function for implementing
save/restore capabilities.
sourcepub fn try_set_avail_ring_address(
&mut self,
avail_ring: GuestAddress
) -> Result<(), Error>
pub fn try_set_avail_ring_address( &mut self, avail_ring: GuestAddress ) -> Result<(), Error>
Tries to update the available ring address. In case of an invalid value, the address is not updated.
This should not be directly used, as the preferred method is
QueueT::set_avail_ring_address
. This is a convenience function for implementing
save/restore capabilities.
sourcepub fn try_set_used_ring_address(
&mut self,
used_ring: GuestAddress
) -> Result<(), Error>
pub fn try_set_used_ring_address( &mut self, used_ring: GuestAddress ) -> Result<(), Error>
Tries to update the used ring address. In cae of an invalid value, the address is not updated.
This should not be directly used, as the preferred method is
QueueT::set_used_ring_address
. This is a convenience function for implementing
save/restore capabilities.
sourcepub fn state(&self) -> QueueState
pub fn state(&self) -> QueueState
Returns the state of the Queue
.
This is useful for implementing save/restore capabilities. The state does not have support for serialization, but this can be added by VMMs locally through the use of a remote type.
Alternatively, a version aware and serializable/deserializable QueueState
is available in the virtio-queue-ser
crate.
Trait Implementations§
source§impl PartialEq for Queue
impl PartialEq for Queue
source§impl<'a> QueueGuard<'a> for Queue
impl<'a> QueueGuard<'a> for Queue
source§impl QueueOwnedT for Queue
impl QueueOwnedT for Queue
source§fn iter<M>(&mut self, mem: M) -> Result<AvailIter<'_, M>, Error>
fn iter<M>(&mut self, mem: M) -> Result<AvailIter<'_, M>, Error>
source§fn go_to_previous_position(&mut self)
fn go_to_previous_position(&mut self)
source§impl QueueT for Queue
impl QueueT for Queue
source§fn new(max_size: u16) -> Result<Self, Error>
fn new(max_size: u16) -> Result<Self, Error>
max_size
. Read moresource§fn is_valid<M: GuestMemory>(&self, mem: &M) -> bool
fn is_valid<M: GuestMemory>(&self, mem: &M) -> bool
source§fn lock(&mut self) -> <Self as QueueGuard<'_>>::G
fn lock(&mut self) -> <Self as QueueGuard<'_>>::G
Queue
object. Read moresource§fn set_desc_table_address(&mut self, low: Option<u32>, high: Option<u32>)
fn set_desc_table_address(&mut self, low: Option<u32>, high: Option<u32>)
source§fn set_avail_ring_address(&mut self, low: Option<u32>, high: Option<u32>)
fn set_avail_ring_address(&mut self, low: Option<u32>, high: Option<u32>)
source§fn set_used_ring_address(&mut self, low: Option<u32>, high: Option<u32>)
fn set_used_ring_address(&mut self, low: Option<u32>, high: Option<u32>)
source§fn set_event_idx(&mut self, enabled: bool)
fn set_event_idx(&mut self, enabled: bool)
source§fn avail_idx<M>(&self, mem: &M, order: Ordering) -> Result<Wrapping<u16>, Error>where
M: GuestMemory + ?Sized,
fn avail_idx<M>(&self, mem: &M, order: Ordering) -> Result<Wrapping<u16>, Error>where
M: GuestMemory + ?Sized,
idx
field from the available ring. Read moresource§fn used_idx<M: GuestMemory>(
&self,
mem: &M,
order: Ordering
) -> Result<Wrapping<u16>, Error>
fn used_idx<M: GuestMemory>( &self, mem: &M, order: Ordering ) -> Result<Wrapping<u16>, Error>
idx
field from the used ring. Read moresource§fn add_used<M: GuestMemory>(
&mut self,
mem: &M,
head_index: u16,
len: u32
) -> Result<(), Error>
fn add_used<M: GuestMemory>( &mut self, mem: &M, head_index: u16, len: u32 ) -> Result<(), Error>
source§fn enable_notification<M: GuestMemory>(
&mut self,
mem: &M
) -> Result<bool, Error>
fn enable_notification<M: GuestMemory>( &mut self, mem: &M ) -> Result<bool, Error>
source§fn disable_notification<M: GuestMemory>(&mut self, mem: &M) -> Result<(), Error>
fn disable_notification<M: GuestMemory>(&mut self, mem: &M) -> Result<(), Error>
source§fn needs_notification<M: GuestMemory>(&mut self, mem: &M) -> Result<bool, Error>
fn needs_notification<M: GuestMemory>(&mut self, mem: &M) -> Result<bool, Error>
source§fn next_avail(&self) -> u16
fn next_avail(&self) -> u16
source§fn set_next_avail(&mut self, next_avail: u16)
fn set_next_avail(&mut self, next_avail: u16)
source§fn set_next_used(&mut self, next_used: u16)
fn set_next_used(&mut self, next_used: u16)
source§fn desc_table(&self) -> u64
fn desc_table(&self) -> u64
source§fn avail_ring(&self) -> u64
fn avail_ring(&self) -> u64
source§fn event_idx_enabled(&self) -> bool
fn event_idx_enabled(&self) -> bool
VIRTIO_F_RING_EVENT_IDX
is negotiated. Read moresource§fn pop_descriptor_chain<M>(&mut self, mem: M) -> Option<DescriptorChain<M>>
fn pop_descriptor_chain<M>(&mut self, mem: M) -> Option<DescriptorChain<M>>
None
when there are no more
descriptor chains available. Read more