Struct VirtQueue

Source
pub struct VirtQueue<'a> {
    pub desc: &'a mut [Descriptor],
    pub avail: AvailableRing<'a>,
    pub used: UsedRing<'a>,
    pub free_descriptors: Vec<usize>,
    pub last_used_idx: usize,
}
Expand description

VirtQueue structure

This structure represents the wrapper of the virtqueue. It contains the descriptor table, available ring, and used ring.

§Fields

  • index: The ID of the virtqueue.
  • desc: A mutable slice of descriptors.
  • avail: The available ring.
  • used: The used ring.
  • free_head: The index of the next free descriptor.
  • last_used_idx: The index of the last used descriptor.

Fields§

§desc: &'a mut [Descriptor]§avail: AvailableRing<'a>§used: UsedRing<'a>§free_descriptors: Vec<usize>§last_used_idx: usize

Implementations§

Source§

impl<'a> VirtQueue<'a>

Source

pub fn new(queue_size: usize) -> Self

Source

pub fn init(&mut self)

Initialize the virtqueue

This function initializes the descriptor table, available ring, and used ring. It sets the next pointer of each descriptor to point to the next descriptor in the table.

Source

pub fn get_raw_ptr(&self) -> *const u8

Get the raw pointer to the virtqueue

This function returns a raw pointer to the start of the virtqueue memory. It can be used to access the memory directly.

§Returns

*const u8: A raw pointer to the start of the virtqueue memory.

Source

pub fn get_raw_size(&self) -> usize

Get the size of the raw virtqueue

This function returns the size of the virtqueue in bytes. It is calculated as the sum of the sizes of the descriptor table, available ring, and used ring.

§Returns

usize: The size of the virtqueue in bytes.

Source

pub fn alloc_desc(&mut self) -> Option<usize>

Allocate a descriptor

This function allocates a descriptor from the free list.

§Returns

Option: The index of the allocated descriptor, or None if no descriptors are available.

Source

pub fn free_desc(&mut self, desc_idx: usize)

Free a descriptor

This function frees a descriptor and adds it back to the free list.

§Arguments
  • desc_idx - The index of the descriptor to free.
Source

pub fn alloc_desc_chain(&mut self, length: usize) -> Option<usize>

Allocate a chain of descriptors

This function allocates a chain of descriptors of the specified length.

§Arguments
  • length - The length of the chain to allocate.
§Returns

Option: The index of the first descriptor in the chain, or None if no descriptors are available.

Source

pub fn free_desc_chain(&mut self, desc_idx: usize)

Free a chain of descriptors

This function frees a chain of descriptors starting from the given index.

§Arguments
  • desc_idx - The index of the first descriptor in the chain.
Source

pub fn is_busy(&self) -> bool

Check if the virtqueue is busy

This function checks if the virtqueue is busy by comparing the last used index with the current index.

§Returns

bool: True if the virtqueue is busy, false otherwise.

Source

pub fn push(&mut self, desc_idx: usize) -> Result<(), &'static str>

Push a descriptor index to the available ring

This function pushes a descriptor index to the available ring.

§Arguments
  • desc_idx - The index of the descriptor to push. If you want to push a chain of descriptors, you should pass the first descriptor index.
§Returns

Result<(), &’static str>: Ok if the push was successful, or an error message if it failed.

Source

pub fn pop(&mut self) -> Option<usize>

Pop a buffer from the used ring

This function retrieves a buffer from the used ring when the device has finished processing it. The caller is responsible for freeing the descriptor when it’s done with the buffer.

§Returns

Option: The index of the descriptor that was used, or None if no descriptors are available.

Auto Trait Implementations§

§

impl<'a> Freeze for VirtQueue<'a>

§

impl<'a> RefUnwindSafe for VirtQueue<'a>

§

impl<'a> Send for VirtQueue<'a>

§

impl<'a> Sync for VirtQueue<'a>

§

impl<'a> Unpin for VirtQueue<'a>

§

impl<'a> !UnwindSafe for VirtQueue<'a>

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.