pub struct QueueGuard<M, S> { /* private fields */ }
Expand description

A guard object to exclusively access an Queue object.

The guard object holds an exclusive lock to the underlying QueueState object, with an associated guest memory object. It helps to guarantee that the whole session is served with the same guest memory 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);
let mut queue_guard = queue.lock_with_memory();

// First, the driver sets up the queue; this set up is done via writes on the bus (PCI, MMIO).
queue_guard.set_size(8);
queue_guard.set_desc_table_address(Some(0x1000), None);
queue_guard.set_avail_ring_address(Some(0x2000), None);
queue_guard.set_used_ring_address(Some(0x3000), None);
queue_guard.set_event_idx(true);
queue_guard.set_ready(true);
// The user should check if the queue is valid before starting to use it.
assert!(queue_guard.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_guard.disable_notification().unwrap();

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

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

Implementations

Create a new instance of QueueGuard.

Check whether the queue configuration is valid.

Reset the queue to the initial state.

Get the maximum size of the virtio queue.

Configure the queue size for the virtio queue.

Check whether the queue is ready to be processed.

Configure the queue to ready for processing state.

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.

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.

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.

Enable/disable the VIRTIO_F_RING_EVENT_IDX feature for interrupt coalescing.

Read the idx field from the available ring.

Read the idx field from the used ring.

Put a used descriptor head into the used ring.

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.

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

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

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

Pop and return the next available descriptor chain, or None when there are no more descriptor chains available.

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

Decrement the value of the next available index by one position.

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 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.