Skip to content

Commit

Permalink
Removes Queue::actual_size()
Browse files Browse the repository at this point in the history
Remove actual_size() since Queue::set_size() checks that the
size cannot be greater than max_size.

Signed-off-by: German Maglione <[email protected]>
  • Loading branch information
germag committed Nov 1, 2021
1 parent d32f97a commit 2053baf
Showing 1 changed file with 6 additions and 27 deletions.
33 changes: 6 additions & 27 deletions crates/virtio-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub mod defs;
#[cfg(any(test, feature = "test-utils"))]
pub mod mock;

use std::cmp::min;
use std::convert::TryFrom;
use std::fmt::{self, Debug, Display};
use std::marker::PhantomData;
Expand Down Expand Up @@ -476,11 +475,6 @@ pub trait QueueStateT<M: GuestAddressSpace> {
/// Get the maximum size of the virtio queue.
fn max_size(&self) -> u16;

/// Return the actual size of the queue.
///
/// The virtio driver may configure queue size smaller than the value reported by `max_size()`.
fn actual_size(&self) -> u16;

/// Configure the queue size for the virtio queue.
///
/// The `size` should power of two and less than or equal to value reported by `max_size()`,
Expand Down Expand Up @@ -590,15 +584,15 @@ impl<M: GuestAddressSpace> QueueState<M> {
desc_table: self.desc_table,
avail_ring: self.avail_ring,
last_index: idx,
queue_size: self.actual_size(),
queue_size: self.size,
next_avail: &mut self.next_avail,
})
}

// Helper method that writes `val` to the `avail_event` field of the used ring, using
// the provided ordering.
fn set_avail_event(&self, mem: &M::T, val: u16, order: Ordering) -> Result<(), Error> {
let elem_sz = VIRTQ_USED_ELEMENT_SIZE * u64::from(self.actual_size());
let elem_sz = VIRTQ_USED_ELEMENT_SIZE * u64::from(self.size);
let offset = VIRTQ_USED_RING_HEADER_SIZE + elem_sz;
let addr = self.used_ring.unchecked_add(offset);

Expand Down Expand Up @@ -647,7 +641,7 @@ impl<M: GuestAddressSpace> QueueState<M> {
fn used_event(&self, mem: &M::T, order: Ordering) -> Result<Wrapping<u16>, Error> {
// Safe because we have validated the queue and access guest
// memory through GuestMemory interfaces.
let elem_sz = u64::from(self.actual_size()) * VIRTQ_AVAIL_ELEMENT_SIZE;
let elem_sz = u64::from(self.size) * VIRTQ_AVAIL_ELEMENT_SIZE;
let offset = VIRTQ_AVAIL_RING_HEADER_SIZE + elem_sz;
let used_event_addr = self.avail_ring.unchecked_add(offset);

Expand Down Expand Up @@ -675,7 +669,7 @@ impl<M: GuestAddressSpace> QueueStateT<M> for QueueState<M> {
}

fn is_valid(&self, mem: &M::T) -> bool {
let queue_size = self.actual_size() as u64;
let queue_size = self.size as u64;
let desc_table = self.desc_table;
let desc_table_size = size_of::<Descriptor>() as u64 * queue_size;
let avail_ring = self.avail_ring;
Expand Down Expand Up @@ -740,10 +734,6 @@ impl<M: GuestAddressSpace> QueueStateT<M> for QueueState<M> {
self.max_size
}

fn actual_size(&self) -> u16 {
min(self.size, self.max_size)
}

fn set_size(&mut self, size: u16) -> Result<(), Error> {
if size > self.max_size() || size == 0 || (size & (size - 1)) != 0 {
error!("virtio queue with invalid size: {}", size);
Expand Down Expand Up @@ -818,15 +808,15 @@ impl<M: GuestAddressSpace> QueueStateT<M> for QueueState<M> {
}

fn add_used(&mut self, mem: &M::T, head_index: u16, len: u32) -> Result<(), Error> {
if head_index >= self.actual_size() {
if head_index >= self.size {
error!(
"attempted to add out of bounds descriptor to used ring: {}",
head_index
);
return Err(Error::InvalidDescriptorIndex);
}

let next_used_index = u64::from(self.next_used.0 % self.actual_size());
let next_used_index = u64::from(self.next_used.0 % self.size);
let elem_sz = next_used_index * VIRTQ_USED_ELEMENT_SIZE;
let offset = VIRTQ_USED_RING_HEADER_SIZE + elem_sz;
let addr = self.used_ring.unchecked_add(offset);
Expand Down Expand Up @@ -945,10 +935,6 @@ impl<M: GuestAddressSpace> QueueStateT<M> for QueueStateSync<M> {
self.state.lock().unwrap().max_size()
}

fn actual_size(&self) -> u16 {
self.state.lock().unwrap().actual_size()
}

fn set_size(&mut self, size: u16) -> Result<(), Error> {
self.state.lock().unwrap().set_size(size)
}
Expand Down Expand Up @@ -1047,13 +1033,6 @@ impl<M: GuestAddressSpace, S: QueueStateT<M>> Queue<M, S> {
self.state.max_size()
}

/// Return the actual size of the queue.
///
/// The virtio driver may configure queue size smaller than the value reported by `max_size()`.
pub fn actual_size(&self) -> u16 {
self.state.actual_size()
}

/// Configure the queue size for the virtio queue.
///
/// The `size` should power of two and less than or equal to value reported by `max_size()`,
Expand Down

0 comments on commit 2053baf

Please sign in to comment.