pub struct Queue<M: GuestAddressSpace, S: QueueStateT = QueueState> {
    pub mem: M,
    pub state: S,
}
Expand description

A convenient wrapper struct for a virtio queue, with associated GuestMemory object.

Example

use virtio_queue::{Queue, QueueState};
use vm_memory::{Bytes, GuestAddress, GuestAddressSpace, GuestMemoryMmap};

let m = GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0), 0x10000)]).unwrap();
let mut queue = Queue::<&GuestMemoryMmap, QueueState>::new(&m, 1024);

// 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());

// 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().unwrap();

    // Consume entries from the available ring.
    while let Some(chain) = queue.iter().unwrap().next() {
        // Process the descriptor chain, and then add an entry in the used ring and optionally
        // notify the driver.
        queue.add_used(chain.head_index(), 0x100).unwrap();

        if queue.needs_notification().unwrap() {
            // Here we would notify the driver it has new entries in the used ring to consume.
        }
    }
    if !queue.enable_notification().unwrap() {
        break;
    }
}

// We can reset the queue at some point.
queue.reset();
// The queue should not be ready after reset.
assert!(!queue.ready());

Fields

mem: M

Guest memory object associated with the queue.

state: S

Virtio queue state.

Implementations

Construct an empty virtio queue with the given max_size.

Arguments
  • mem - the guest memory object that can be used to access the queue buffers.
  • max_size - the maximum size (and the default one) of the queue.

Check whether the queue configuration is valid.

Reset the queue to the initial state.

Get an exclusive reference to the underlying QueueState object.

Logically this method will acquire the underlying lock protecting the QueueState Object. The lock will be released when the returned object gets dropped.

Get an exclusive reference to the underlying QueueState object with an associated GuestMemory object.

Logically this method will acquire the underlying lock protecting the QueueState Object. The lock will be released when the returned object gets dropped.

Get the maximum size of the virtio queue.

Configure the queue size for the virtio queue.

Arguments
  • size - the queue size; it should be a power of two, different than 0 and less than or equal to the value reported by max_size(), otherwise the queue size remains the default one (which is the maximum one).

Check whether the queue is ready to be processed.

Configure the queue to the ready for processing state.

Arguments
  • ready - a boolean to indicate whether the queue is ready to be used or not.

Set the descriptor table address for the queue.

The descriptor table address is 64-bit, the corresponding part will be updated if ‘low’ and/or high is Some and valid.

Arguments
  • low - an optional value for the lowest 32 bits of the address.
  • high - an optional value for the highest 32 bits of the address.

Set the available ring address for the queue.

The available ring address is 64-bit, the corresponding part will be updated if ‘low’ and/or high is Some and valid.

Arguments
  • low - an optional value for the lowest 32 bits of the address.
  • high - an optional value for the highest 32 bits of the address.

Set the used ring address for the queue.

The used ring address is 64-bit, the corresponding part will be updated if ‘low’ and/or high is Some and valid.

Arguments
  • low - an optional value for the lowest 32 bits of the address.
  • high - an optional value for the highest 32 bits of the address.

Enable/disable the VIRTIO_F_RING_EVENT_IDX feature for interrupt coalescing.

Arguments
  • enabled - a boolean to indicate whether the VIRTIO_F_RING_EVENT_IDX feature was successfully negotiated or not.

Read the idx field from the available ring.

Arguments
  • order - the memory ordering used to access the idx field from memory.

Reads the idx field from the used ring.

Arguments
  • order - the memory ordering used to access the idx field from memory.

Put a used descriptor head into the used ring.

Arguments
  • head_index - the index of the used descriptor chain.
  • len - the total length of the descriptor chain which was used (written to).

Enable notification events from the guest driver.

Return true if one or more descriptors can be consumed from the available ring after notifications were enabled (and thus it’s possible there will be no corresponding notification).

Disable notification events from the guest driver.

Check whether a notification to the guest is needed.

Please note this method has side effects: once it returns true, it considers the driver will actually be notified, remember the associated index in the used ring, and won’t return true again until the driver updates used_event and/or the notification conditions hold once more.

Return the index of the next entry in the available ring.

Returns the index for the next descriptor in the used ring.

Set the index of the next entry in the available ring.

Arguments
  • next_avail - the index of the next available ring entry.

Sets the index for the next descriptor in the used ring.

Arguments
  • next_used - the index of the next used ring entry.

A consuming iterator over all available descriptor chain heads offered by the driver.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.