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

feat(virtio-spec): migrate to endian-num crate #1225

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/drivers/net/virtio_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ impl NetworkDriver for VirtioNetDriver {
num_buffers
};

for _ in 1..num_buffers.get() {
for _ in 1..num_buffers.to_ne() {
let transfer =
match RxQueues::post_processing(self.recv_vqs.get_next().unwrap()) {
Ok(trf) => trf,
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/virtio/transport/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ impl MmioRegisterLayout {

/// Write selected features into driver_select field.
pub fn set_drv_features(&mut self, features: virtio_spec::F) {
let features = features.bits().get() as u64;
let features = features.bits().to_ne() as u64;
let high: u32 = (features >> 32) as u32;
let low: u32 = features as u32;

Expand Down
14 changes: 7 additions & 7 deletions src/drivers/virtio/transport/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,11 @@ impl<'a> VqCfgHandler<'a> {
self.select_queue();
let queue_size = self.raw.as_mut_ptr().queue_size();

if queue_size.read().get() >= size {
if queue_size.read().to_ne() >= size {
queue_size.write(size.into());
}

queue_size.read().get()
queue_size.read().to_ne()
}

pub fn set_ring_addr(&mut self, addr: PhysAddr) {
Expand Down Expand Up @@ -485,7 +485,7 @@ impl<'a> VqCfgHandler<'a> {

pub fn notif_off(&mut self) -> u16 {
self.select_queue();
self.raw.as_mut_ptr().queue_notify_off().read().get()
self.raw.as_mut_ptr().queue_notify_off().read().to_ne()
}

pub fn enable_queue(&mut self) {
Expand All @@ -503,7 +503,7 @@ impl ComCfg {
pub fn select_vq(&mut self, index: u16) -> Option<VqCfgHandler<'_>> {
self.com_cfg.as_mut_ptr().queue_select().write(index.into());

if self.com_cfg.as_mut_ptr().queue_size().read().get() == 0 {
if self.com_cfg.as_mut_ptr().queue_size().read().to_ne() == 0 {
None
} else {
Some(VqCfgHandler {
Expand Down Expand Up @@ -608,22 +608,22 @@ impl ComCfg {
memory_barrier();

// read high 32 bits of device features
let mut device_features = u64::from(device_feature.read().get()) << 32;
let mut device_features = u64::from(device_feature.read().to_ne()) << 32;

// Indicate device to show low 32 bits in device_feature field.
// See Virtio specification v1.1. - 4.1.4.3
device_feature_select.write(0.into());
memory_barrier();

// read low 32 bits of device features
device_features |= u64::from(device_feature.read().get());
device_features |= u64::from(device_feature.read().to_ne());

virtio_spec::F::from_bits_retain(u128::from(device_features).into())
}

/// Write selected features into driver_select field.
pub fn set_drv_features(&mut self, features: virtio_spec::F) {
let features = features.bits().get() as u64;
let features = features.bits().to_ne() as u64;
let com_cfg = self.com_cfg.as_mut_ptr();
let driver_feature_select = com_cfg.driver_feature_select();
let driver_feature = com_cfg.driver_feature();
Expand Down
20 changes: 11 additions & 9 deletions src/drivers/virtio/virtqueue/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::cell::{RefCell, UnsafeCell};
use core::mem::{size_of, MaybeUninit};
use core::ptr::{self, NonNull};

use virtio_spec::num::{le16, le32, le64};
use virtio_spec::{le16, le32, le64};
use volatile::access::ReadOnly;
use volatile::{map_field, VolatilePtr, VolatileRef};

Expand Down Expand Up @@ -264,13 +264,13 @@ impl DescrRing {
let len = self.token_ring.len();
let mut avail_ring_ref = self.avail_ring_ref();
let avail_ring = avail_ring_ref.as_mut_ptr();
let idx = map_field!(avail_ring.index).read().get();
let idx = map_field!(avail_ring.index).read().to_ne();
AvailRing::ring_ptr(avail_ring)
.index(idx as usize % len)
.write(MaybeUninit::new((index as u16).into()));

memory_barrier();
map_field!(avail_ring.index).update(|val| (val.get().wrapping_add(1)).into());
map_field!(avail_ring.index).update(|val| (val.to_ne().wrapping_add(1)).into());

(0, 0)
}
Expand All @@ -284,23 +284,25 @@ impl DescrRing {
{
let used_ring_ref = self.used_ring_ref();
let used_ring = used_ring_ref.as_ptr();
if self.read_idx == map_field!(used_ring.index).read().get() {
if self.read_idx == map_field!(used_ring.index).read().to_ne() {
break;
} else {
let cur_ring_index = self.read_idx as usize % self.token_ring.len();
used_elem = UsedRing::ring_ptr(used_ring).index(cur_ring_index).read();
}
}

let mut tkn = self.token_ring[used_elem.id.get() as usize].take().expect(
"The buff_id is incorrect or the reference to the TransferToken was misplaced.",
);
let mut tkn = self.token_ring[used_elem.id.to_ne() as usize]
.take()
.expect(
"The buff_id is incorrect or the reference to the TransferToken was misplaced.",
);

if tkn.buff_tkn.as_ref().unwrap().recv_buff.as_ref().is_some() {
tkn.buff_tkn
.as_mut()
.unwrap()
.restr_size(None, Some(used_elem.len.get() as usize))
.restr_size(None, Some(used_elem.len.to_ne() as usize))
.unwrap();
}
if let Some(queue) = tkn.await_queue.take() {
Expand All @@ -326,7 +328,7 @@ impl DescrRing {
fn dev_is_notif(&self) -> bool {
let used_ring_ref = self.used_ring_ref();
let used_ring = used_ring_ref.as_ptr();
map_field!(used_ring.flags).read().get() & 1 == 0
map_field!(used_ring.flags).read().to_ne() & 1 == 0
}
}

Expand Down
3 changes: 2 additions & 1 deletion virtio-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ categories = ["no-std", "no-std::no-alloc"]

[dependencies]
bitflags = "2"
endian-num = { version = "0.1", features = ["bitflags", "linux-types"] }
volatile = { version = "0.5.3", features = ["derive"] }
zerocopy = { version = "0.7", optional = true, default-features = false }
zerocopy-derive = { version = "0.7", optional = true }

[features]
zerocopy = ["dep:zerocopy", "dep:zerocopy-derive"]
zerocopy = ["dep:zerocopy", "dep:zerocopy-derive", "endian-num/zerocopy"]
20 changes: 10 additions & 10 deletions virtio-spec/src/bitflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ macro_rules! endian_bitflags {
impl $BitFlags {
$(
$(#[$inner $($args)*])*
pub const $Flag: Self = Self(<$T>::new($value));
pub const $Flag: Self = Self(<$T>::from_ne($value));
)*
}

Expand Down Expand Up @@ -186,25 +186,25 @@ macro_rules! endian_bitflags {
/// Whether all bits in this flags value are unset.
#[inline]
pub const fn is_empty(&self) -> bool {
self.bits().get() == <$T as ::bitflags::Bits>::EMPTY.get()
self.bits().to_ne() == <$T as ::bitflags::Bits>::EMPTY.to_ne()
}

/// Whether all known bits in this flags value are set.
#[inline]
pub const fn is_all(&self) -> bool {
Self::all().bits().get() | self.bits().get() == self.bits().get()
Self::all().bits().to_ne() | self.bits().to_ne() == self.bits().to_ne()
}

/// Whether any set bits in a source flags value are also set in a target flags value.
#[inline]
pub const fn intersects(&self, other: Self) -> bool {
self.bits().get() & other.bits().get() != <$T as ::bitflags::Bits>::EMPTY.get()
self.bits().to_ne() & other.bits().to_ne() != <$T as ::bitflags::Bits>::EMPTY.to_ne()
}

/// Whether all set bits in a source flags value are also set in a target flags value.
#[inline]
pub const fn contains(&self, other: Self) -> bool {
self.bits().get() & other.bits().get() == other.bits().get()
self.bits().to_ne() & other.bits().to_ne() == other.bits().to_ne()
}

/// The bitwise or (`|`) of the bits in two flags values.
Expand Down Expand Up @@ -242,14 +242,14 @@ macro_rules! endian_bitflags {
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Self::from_bits_retain(<$T>::new(self.bits().get() & other.bits().get()))
Self::from_bits_retain(<$T>::from_ne(self.bits().to_ne() & other.bits().to_ne()))
}

/// The bitwise or (`|`) of the bits in two flags values.
#[inline]
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self::from_bits_retain(<$T>::new(self.bits().get() | other.bits().get()))
Self::from_bits_retain(<$T>::from_ne(self.bits().to_ne() | other.bits().to_ne()))
}

/// The intersection of a source flags value with the complement of a target flags value (`&!`).
Expand All @@ -259,21 +259,21 @@ macro_rules! endian_bitflags {
#[inline]
#[must_use]
pub const fn difference(self, other: Self) -> Self {
Self::from_bits_retain(<$T>::new(self.bits().get() & !other.bits().get()))
Self::from_bits_retain(<$T>::from_ne(self.bits().to_ne() & !other.bits().to_ne()))
}

/// The bitwise exclusive-or (`^`) of the bits in two flags values.
#[inline]
#[must_use]
pub const fn symmetric_difference(self, other: Self) -> Self {
Self::from_bits_retain(<$T>::new(self.bits().get() ^ other.bits().get()))
Self::from_bits_retain(<$T>::from_ne(self.bits().to_ne() ^ other.bits().to_ne()))
}

/// The bitwise negation (`!`) of the bits in a flags value, truncating the result.
#[inline]
#[must_use]
pub const fn complement(self) -> Self {
Self::from_bits_truncate(<$T>::new(!self.bits().get()))
Self::from_bits_truncate(<$T>::from_ne(!self.bits().to_ne()))
}
}

Expand Down
28 changes: 14 additions & 14 deletions virtio-spec/src/features.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Feature Bits

use crate::num::le128;
use crate::le128;

/// Feature Bits
#[doc(alias = "VIRTIO_F")]
Expand Down Expand Up @@ -209,37 +209,37 @@ macro_rules! feature_bits {
)*

/// Device-independent Bit. See [`virtio_spec::F::INDIRECT_DESC`](crate::F::INDIRECT_DESC).
const INDIRECT_DESC = $crate::F::INDIRECT_DESC.bits().get();
const INDIRECT_DESC = $crate::F::INDIRECT_DESC.bits().to_ne();

/// Device-independent Bit. See [`virtio_spec::F::EVENT_IDX`](crate::F::EVENT_IDX).
const EVENT_IDX = $crate::F::EVENT_IDX.bits().get();
const EVENT_IDX = $crate::F::EVENT_IDX.bits().to_ne();

/// Device-independent Bit. See [`virtio_spec::F::VERSION_1`](crate::F::VERSION_1).
const VERSION_1 = $crate::F::VERSION_1.bits().get();
const VERSION_1 = $crate::F::VERSION_1.bits().to_ne();

/// Device-independent Bit. See [`virtio_spec::F::ACCESS_PLATFORM`](crate::F::ACCESS_PLATFORM).
const ACCESS_PLATFORM = $crate::F::ACCESS_PLATFORM.bits().get();
const ACCESS_PLATFORM = $crate::F::ACCESS_PLATFORM.bits().to_ne();

/// Device-independent Bit. See [`virtio_spec::F::RING_PACKED`](crate::F::RING_PACKED).
const RING_PACKED = $crate::F::RING_PACKED.bits().get();
const RING_PACKED = $crate::F::RING_PACKED.bits().to_ne();

/// Device-independent Bit. See [`virtio_spec::F::IN_ORDER`](crate::F::IN_ORDER).
const IN_ORDER = $crate::F::IN_ORDER.bits().get();
const IN_ORDER = $crate::F::IN_ORDER.bits().to_ne();

/// Device-independent Bit. See [`virtio_spec::F::ORDER_PLATFORM`](crate::F::ORDER_PLATFORM).
const ORDER_PLATFORM = $crate::F::ORDER_PLATFORM.bits().get();
const ORDER_PLATFORM = $crate::F::ORDER_PLATFORM.bits().to_ne();

/// Device-independent Bit. See [`virtio_spec::F::SR_IOV`](crate::F::SR_IOV).
const SR_IOV = $crate::F::SR_IOV.bits().get();
const SR_IOV = $crate::F::SR_IOV.bits().to_ne();

/// Device-independent Bit. See [`virtio_spec::F::NOTIFICATION_DATA`](crate::F::NOTIFICATION_DATA).
const NOTIFICATION_DATA = $crate::F::NOTIFICATION_DATA.bits().get();
const NOTIFICATION_DATA = $crate::F::NOTIFICATION_DATA.bits().to_ne();

/// Device-independent Bit. See [`virtio_spec::F::NOTIF_CONFIG_DATA`](crate::F::NOTIF_CONFIG_DATA).
const NOTIF_CONFIG_DATA = $crate::F::NOTIF_CONFIG_DATA.bits().get();
const NOTIF_CONFIG_DATA = $crate::F::NOTIF_CONFIG_DATA.bits().to_ne();

/// Device-independent Bit. See [`virtio_spec::F::RING_RESET`](crate::F::RING_RESET).
const RING_RESET = $crate::F::RING_RESET.bits().get();
const RING_RESET = $crate::F::RING_RESET.bits().to_ne();
}
}

Expand Down Expand Up @@ -290,7 +290,7 @@ macro_rules! feature_bits {
}

pub mod net {
use crate::num::le128;
use crate::le128;

feature_bits! {
/// Network Device Feature Bits
Expand Down Expand Up @@ -460,7 +460,7 @@ pub mod net {
}

pub mod fs {
use crate::num::le128;
use crate::le128;

feature_bits! {
/// File System Device Feature Bits
Expand Down
7 changes: 4 additions & 3 deletions virtio-spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ mod bitflags;
mod volatile;
mod features;
pub mod net;
pub mod num;
pub mod pci;

pub use features::{FeatureBits, F};
pub use volatile::WideVolatilePtr;
pub use endian_num::{be128, be16, be32, be64, le128, le16, le32, le64};

pub use self::features::{FeatureBits, F};
pub use self::volatile::WideVolatilePtr;

pub mod fs {
//! File System Device
Expand Down
2 changes: 1 addition & 1 deletion virtio-spec/src/net.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Network Device

pub use super::features::net::F;
use crate::num::{le16, le32};
use crate::{le16, le32};

virtio_bitflags! {
/// Network Device Header Flags
Expand Down
Loading