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>
impl<'a> VirtQueue<'a>
pub fn new(queue_size: usize) -> Self
Sourcepub fn init(&mut self)
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.
Sourcepub fn get_raw_ptr(&self) -> *const u8
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.
Sourcepub fn get_raw_size(&self) -> usize
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.
Sourcepub fn alloc_desc(&mut self) -> Option<usize>
pub fn alloc_desc(&mut self) -> Option<usize>
Allocate a descriptor
This function allocates a descriptor from the free list.
§Returns
Option
Sourcepub fn free_desc(&mut self, desc_idx: usize)
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.
Sourcepub fn alloc_desc_chain(&mut self, length: usize) -> Option<usize>
pub fn alloc_desc_chain(&mut self, length: usize) -> Option<usize>
Sourcepub fn free_desc_chain(&mut self, desc_idx: usize)
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.
Sourcepub fn is_busy(&self) -> bool
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.
Sourcepub fn push(&mut self, desc_idx: usize) -> Result<(), &'static str>
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.
Sourcepub fn pop(&mut self) -> Option<usize>
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