Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

queue: check queue size at compile time #134

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// Each device can have zero or more virtqueues.
///
/// * `SIZE`: The size of the queue. This is both the number of descriptors, and the number of slots
/// in the available and used rings.
/// in the available and used rings. It must be a power of 2 and fit in a [`u16`].
#[derive(Debug)]
pub struct VirtQueue<H: Hal, const SIZE: usize> {
/// DMA guard
Expand Down Expand Up @@ -61,6 +61,8 @@
}

impl<H: Hal, const SIZE: usize> VirtQueue<H, SIZE> {
const SIZE_OK: () = assert!(SIZE.is_power_of_two() && SIZE <= u16::MAX as usize);

/// Creates a new VirtQueue.
///
/// * `indirect`: Whether to use indirect descriptors. This should be set if the
Expand All @@ -71,16 +73,16 @@
pub fn new<T: Transport>(
transport: &mut T,
idx: u16,
indirect: bool,

Check warning on line 76 in src/queue.rs

View workflow job for this annotation

GitHub Actions / build

unused variable: `indirect`

Check warning on line 76 in src/queue.rs

View workflow job for this annotation

GitHub Actions / build

unused variable: `indirect`
event_idx: bool,
) -> Result<Self> {
#[allow(clippy::let_unit_value)]
let _ = Self::SIZE_OK;

if transport.queue_used(idx) {
return Err(Error::AlreadyUsed);
}
if !SIZE.is_power_of_two()
|| SIZE > u16::MAX.into()
|| transport.max_queue_size(idx) < SIZE as u32
{
if transport.max_queue_size(idx) < SIZE as u32 {
return Err(Error::InvalidParam);
}
let size = SIZE as u16;
Expand Down Expand Up @@ -976,17 +978,6 @@
use core::ptr::NonNull;
use std::sync::{Arc, Mutex};

#[test]
fn invalid_queue_size() {
let mut header = VirtIOHeader::make_fake_header(MODERN_VERSION, 1, 0, 0, 4);
let mut transport = unsafe { MmioTransport::new(NonNull::from(&mut header)) }.unwrap();
// Size not a power of 2.
assert_eq!(
VirtQueue::<FakeHal, 3>::new(&mut transport, 0, false, false).unwrap_err(),
Error::InvalidParam
);
}

#[test]
fn queue_too_big() {
let mut header = VirtIOHeader::make_fake_header(MODERN_VERSION, 1, 0, 0, 4);
Expand Down
Loading