A descriptor is storing four fields, with the first two, addr
and len
, pointing to the data in memory to which the descriptor refers, as shown in the diagram below. The flags
field is useful for indicating if, for example, the buffer is device readable or writable, or if we have another descriptor chained after this one (VIRTQ_DESC_F_NEXT flag set). next
field is storing the index of the next descriptor if VIRTQ_DESC_F_NEXT is set.
Requirements for device implementation
DescriptorChain
can be used to parse descriptors provided by the device, which represent input or output memory areas for device I/O. A descriptor is essentially an (address, length) pair, which is subsequently used by the device model operation. We do not check the validity of the descriptors, and instead expect any validations to happen when the device implementation is attempting to access the corresponding areas. Early checks can add non-negligible additional costs, and exclusively relying upon them may lead to time-of-check-to-time-of-use race conditions.QueueT
is a trait that allows different implementations for a Queue
object for single-threaded context and multi-threaded context. The implementations provided in virtio-queue
are:
Queue
→ it is used for the single-threaded context.QueueSync
→ it is used for the multi-threaded context, and is simply a wrapper over an Arc<Mutex<Queue>>
.Besides the above abstractions, the virtio-queue
crate provides also the following ones:
Descriptor
→ which mostly offers accessors for the members of the Descriptor
.DescriptorChain
→ provides accessors for the DescriptorChain
’s members and an Iterator
implementation for iterating over the DescriptorChain
, there is also an abstraction for iterators over just the device readable or just the device writable descriptors (DescriptorChainRwIter
).AvailIter
- is a consuming iterator over all available descriptor chain heads in the queue.The Queue
allows saving the state through the state
function which returns a QueueState
. Queue
objects can be created from a previously saved state by using QueueState::try_from
. The VMM should check for errors when restoring a Queue
from a previously saved state.
A big part of the virtio-queue
crate consists of the notification suppression support. As already mentioned, the driver can send an available buffer notification to the device when there are new entries in the available ring, and the device can send a used buffer notification to the driver when there are new entries in the used ring. There might be cases when sending a notification each time these scenarios happen is not efficient, for example when the driver is processing the used ring, it would not need to receive another used buffer notification. The mechanism for suppressing the notifications is detailed in the following sections from the specification:
The Queue
abstraction is proposing the following sequence of steps for processing new available ring entries:
Queue::disable_notification
. Notifications are disabled by the device either if VIRTIO_F_EVENT_IDX is not negotiated, and VIRTQ_USED_F_NO_NOTIFY is set in the flags
field of the used ring, or if VIRTIO_F_EVENT_IDX is negotiated, and avail_event
value is not updated, i.e. it remains set to the latest idx
value of the available ring that was already notified by the driver.AvailIter
iterator.Queue::enable_notification
. Notifications are enabled by the device either if VIRTIO_F_EVENT_IDX is not negotiated, and 0 is set in the flags
field of the used ring, or if VIRTIO_F_EVENT_IDX is negotiated, and avail_event
value is set to the smallest idx
value of the available ring that was not already notified by the driver. This way the device makes sure that it won’t miss any notification.The above steps should be done in a loop to also handle the less likely case where the driver added new entries just before we re-enabled notifications.
On the driver side, the Queue
provides the needs_notification
method which should be used each time the device adds a new entry to the used ring. Depending on the used_event
value and on the last used value (signalled_used
), needs_notification
returns true to let the device know it should send a notification to the guest.
We assume the users of the Queue
implementation won’t attempt to use the queue before checking that the ready
bit is set. This can be verified by calling Queue::is_valid
which, besides this, is also checking that the three queue parts are valid memory regions. We assume consumers will use AvailIter::go_to_previous_position
only in single-threaded contexts. We assume the users will consume the entries from the available ring in the recommended way from the documentation, i.e. device starts processing the available ring entries, disables the notifications, processes the entries, and then re-enables notifications.
This project is licensed under either of