diff --git a/src/device/net/dev.rs b/src/device/net/dev.rs index aa96573d..d3c3119d 100644 --- a/src/device/net/dev.rs +++ b/src/device/net/dev.rs @@ -4,7 +4,7 @@ use super::net_buf::{RxBuffer, TxBuffer}; use super::{EthernetAddress, VirtIONetRaw}; use crate::{hal::Hal, transport::Transport, Error, Result}; -/// Driver for a VirtIO block device. +/// Driver for a VirtIO network device. /// /// Unlike [`VirtIONetRaw`], it uses [`RxBuffer`]s for transmission and /// reception rather than the raw slices. On initialization, it pre-allocates diff --git a/src/device/net/dev_raw.rs b/src/device/net/dev_raw.rs index 37e720f7..39a84529 100644 --- a/src/device/net/dev_raw.rs +++ b/src/device/net/dev_raw.rs @@ -70,14 +70,14 @@ impl VirtIONetRaw VirtIONetRaw VirtIONetRaw VirtIONetRaw VirtIONetRaw Result { let header = VirtioNetHdr::default(); - if tx_buf.len() == 0 { + if tx_buf.is_empty() { // Special case sending an empty packet, to avoid adding an empty buffer to the // virtqueue. self.send_queue.add_notify_wait_pop( diff --git a/src/queue.rs b/src/queue.rs index e317e4be..9d1d88c2 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -316,27 +316,15 @@ impl VirtQueue { unsafe { self.pop_used(token, inputs, outputs) } } - /// Advise the device that notifications are needed. + /// Advise the device whether used buffer notifications are needed. /// /// See Virtio v1.1 2.6.7 Used Buffer Notification Suppression - pub fn enable_dev_notify(&mut self) { + pub fn set_dev_notify(&mut self, enable: bool) { + let avail_ring_flags = if enable { 0x0000 } else { 0x0001 }; if !self.event_idx { // Safe because self.avail points to a valid, aligned, initialised, dereferenceable, readable // instance of AvailRing. - unsafe { (*self.avail.as_ptr()).flags = 0x0000 } - } - // Write barrier so that device can see change to available index after this method returns. - fence(Ordering::SeqCst); - } - - /// Advise the device that notifications are not needed. - /// - /// See Virtio v1.1 2.6.7 Used Buffer Notification Suppression - pub fn disable_dev_notify(&mut self) { - if !self.event_idx { - // Safe because self.avail points to a valid, aligned, initialised, dereferenceable, readable - // instance of AvailRing. - unsafe { (*self.avail.as_ptr()).flags = 0x0001 } + unsafe { (*self.avail.as_ptr()).flags = avail_ring_flags } } // Write barrier so that device can see change to available index after this method returns. fence(Ordering::SeqCst); @@ -1163,12 +1151,12 @@ mod tests { // Check that the avail ring's flag is zero by default. assert_eq!(unsafe { (*queue.avail.as_ptr()).flags }, 0x0); - queue.disable_dev_notify(); + queue.set_dev_notify(false); // Check that the avail ring's flag is 1 after `disable_dev_notify`. assert_eq!(unsafe { (*queue.avail.as_ptr()).flags }, 0x1); - queue.enable_dev_notify(); + queue.set_dev_notify(true); // Check that the avail ring's flag is 0 after `enable_dev_notify`. assert_eq!(unsafe { (*queue.avail.as_ptr()).flags }, 0x0);