Skip to main content

VirtqConsumer

Struct VirtqConsumer 

Source
pub struct VirtqConsumer<M, N> { /* private fields */ }
Expand description

A high-level virtqueue consumer (device side).

The consumer receives chains from the producer (driver), processes them, and sends back replies. This is typically used on the device/host side.

§Example

let mut consumer = VirtqConsumer::new(layout, mem, notifier);

// Poll and process
while let Some((chain, reply)) = consumer.poll(MAX_RECV_LEN)? {
    let data = chain.to_bytes();
    match reply {
        ReplyChain::Writable(mut wc) => {
            let response = handle_request(data);
            wc.write_all(&response)?;
            consumer.complete(wc)?;
        }
        ReplyChain::Ack(ack) => {
            consumer.complete(ack)?;
        }
    }
}

// Or defer completions
let mut pending = Vec::new();
while let Some((chain, reply)) = consumer.poll(MAX_RECV_LEN)? {
    pending.push((process(chain), reply));
}

for (result, reply) in pending {
    // ... complete later ...
    consumer.complete(reply)?;
}

Implementations§

Source§

impl<M: MemOps + Clone, N: Notifier> VirtqConsumer<M, N>

Source

pub fn new(layout: Layout, mem: M, notifier: N) -> Self

Create a new virtqueue consumer.

§Arguments
  • layout - Ring memory layout
  • mem - Memory ops implementation for reading/writing to shared memory
  • notifier - Callback for notifying the driver about replies
Source

pub fn poll( &mut self, max_recv_len: usize, ) -> Result<Option<(RecvChain, ReplyChain<M>)>, VirtqError>

Poll for a single incoming chain from the driver.

Returns a RecvChain (copied data) and a ReplyChain (writable reply capacity or ack token). Both are independent owned values with no borrow on the consumer.

On VirtqError::BadChain, VirtqError::PayloadTooLarge, and VirtqError::MemoryReadError the descriptor is returned to the driver (completed with zero length) before the error is propagated, so a rejected chain does not leak.

§Arguments
§Errors
Source

pub fn complete( &mut self, reply: impl Into<ReplyChain<M>>, ) -> Result<(), VirtqError>

Submit a reply/ack for a received chain back to the ring.

Accepts both WritableChain (with written byte count) and AckChain (zero-length) via the ReplyChain enum. Clears the inflight slot and notifies the producer if event suppression allows.

Source

pub fn avail_cursor(&self) -> RingCursor

Get the current available cursor position.

Returns the position where the next available descriptor will be consumed. Useful for setting up descriptor-based event suppression.

Source

pub fn used_cursor(&self) -> RingCursor

Get the current used cursor position.

Returns the position where the next used descriptor will be written. Useful for setting up descriptor-based event suppression.

Source

pub fn set_avail_suppression( &mut self, kind: SuppressionKind, ) -> Result<(), VirtqError>

Configure event suppression for available buffer notifications.

This controls when the driver (producer) signals us about new buffers:

§Example: Polling Mode
consumer.set_avail_suppression(SuppressionKind::Disable)?;
loop {
    while let Some((chain, reply)) = consumer.poll(1024)? {
        process(chain, reply);
    }
    // ... do other work ...
}
Source

pub fn reset(&mut self)

Reset ring and inflight state to initial values.

Auto Trait Implementations§

§

impl<M, N> Freeze for VirtqConsumer<M, N>
where N: Freeze, M: Freeze,

§

impl<M, N> RefUnwindSafe for VirtqConsumer<M, N>

§

impl<M, N> Send for VirtqConsumer<M, N>
where N: Send, M: Send,

§

impl<M, N> Sync for VirtqConsumer<M, N>
where N: Sync, M: Sync,

§

impl<M, N> Unpin for VirtqConsumer<M, N>
where N: Unpin, M: Unpin,

§

impl<M, N> UnsafeUnpin for VirtqConsumer<M, N>
where N: UnsafeUnpin, M: UnsafeUnpin,

§

impl<M, N> UnwindSafe for VirtqConsumer<M, N>
where N: UnwindSafe, M: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more