Skip to content

Commit

Permalink
refactor(virtqueue): make next_wrap a bool
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed May 30, 2024
1 parent 1875714 commit 0789f25
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
16 changes: 7 additions & 9 deletions src/drivers/virtio/virtqueue/packed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl DescriptorRing {
}
}

fn push_batch(&mut self, tkn_lst: Vec<TransferToken>) -> (u16, u8) {
fn push_batch(&mut self, tkn_lst: Vec<TransferToken>) -> (u16, bool) {
// Catch empty push, in order to allow zero initialized first_ctrl_settings struct
// which will be overwritten in the first iteration of the for-loop
assert!(!tkn_lst.is_empty());
Expand Down Expand Up @@ -275,11 +275,10 @@ impl DescriptorRing {
fence(Ordering::SeqCst);
self.ring[usize::from(first_ctrl_settings.0)].flags |= first_ctrl_settings.2.as_flags_avail().into();

// Converting a boolean as u8 is fine
(first_ctrl_settings.0, first_ctrl_settings.2 .0 as u8)
(first_ctrl_settings.0, first_ctrl_settings.2 .0)
}

fn push(&mut self, tkn: TransferToken) -> (u16, u8) {
fn push(&mut self, tkn: TransferToken) -> (u16, bool) {
// Check length and if its fits. This should always be true due to the restriction of
// the memory pool, but to be sure.
assert!(tkn.buff_tkn.as_ref().unwrap().num_consuming_descr() <= self.capacity);
Expand Down Expand Up @@ -387,8 +386,7 @@ impl DescriptorRing {
ctrl.make_avail(Box::new(tkn));
fence(Ordering::SeqCst);

// Converting a boolean as u8 is fine
(ctrl.start, ctrl.wrap_at_init.0 as u8)
(ctrl.start, ctrl.wrap_at_init.0)
}

/// # Unsafe
Expand Down Expand Up @@ -872,7 +870,7 @@ impl DrvNotif {
}

/// Enables a notification by the device for a specific descriptor.
fn enable_specific(&mut self, at_offset: u16, at_wrap: u8) {
fn enable_specific(&mut self, at_offset: u16, at_wrap: bool) {
// Check if VIRTIO_F_RING_EVENT_IDX has been negotiated
if self.f_notif_idx {
self.raw.flags |= 1 << 1;
Expand All @@ -896,13 +894,13 @@ impl DevNotif {
self.raw.flags & (1 << 0) == 0
}

fn is_notif_specfic(&self, next_off: u16, next_wrap: u8) -> bool {
fn is_notif_specfic(&self, next_off: u16, next_wrap: bool) -> bool {
if self.f_notif_idx {
if self.raw.flags & 1 << 1 == 2 {
// as u16 is okay for usize, as size of queue is restricted to 2^15
// it is also okay to just loose the upper 8 bits, as we only check the LSB in second clause.
let desc_event_off = self.raw.event & !(1 << 15);
let desc_event_wrap = (self.raw.event >> 15) as u8;
let desc_event_wrap = (self.raw.event >> 15) as u8 != 0;

desc_event_off == next_off && desc_event_wrap == next_wrap
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/drivers/virtio/virtqueue/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl DescrRing {
unsafe { VolatileRef::new_read_only(NonNull::new(self.used_ring_cell.get()).unwrap()) }
}

fn push(&mut self, tkn: TransferToken) -> (u16, u16) {
fn push(&mut self, tkn: TransferToken) -> (u16, bool) {
let mut desc_lst = Vec::new();
let mut is_indirect = false;

Expand Down Expand Up @@ -272,7 +272,7 @@ impl DescrRing {
memory_barrier();
map_field!(avail_ring.index).update(|val| (val.to_ne().wrapping_add(1)).into());

(0, 0)
(0, false)
}

fn poll(&mut self) {
Expand Down Expand Up @@ -382,7 +382,7 @@ impl Virtq for SplitVq {
let mut index = index.iter();
// Even on 64bit systems this is fine, as we have a queue_size < 2^15!
let det_notif_data: u16 = next_off >> 1;
let flags = (det_notif_data | (next_wrap << 15)).to_le_bytes();
let flags = (det_notif_data | (u16::from(next_wrap) << 15)).to_le_bytes();
let mut flags = flags.iter();
let mut notif_data: [u8; 4] = [0, 0, 0, 0];

Expand Down

0 comments on commit 0789f25

Please sign in to comment.