Browse Source

Use byteorder for explicit endian types

Alice Wang 2 years ago
parent
commit
e6eedb81fc
3 changed files with 30 additions and 91 deletions
  1. 30 28
      src/device/socket/protocol.rs
  2. 0 62
      src/endian.rs
  3. 0 1
      src/lib.rs

+ 30 - 28
src/device/socket/protocol.rs

@@ -1,11 +1,13 @@
 //! This module defines the socket device protocol according to the virtio spec 5.10 Socket Device
 
 use super::error::{self, SocketError};
-use crate::endian::{Le16, Le32, Le64};
 use crate::volatile::ReadOnly;
 use core::convert::TryInto;
 use core::{convert::TryFrom, mem::size_of};
-use zerocopy::{AsBytes, FromBytes};
+use zerocopy::{
+    byteorder::{LittleEndian, U16, U32, U64},
+    AsBytes, FromBytes,
+};
 
 pub const TYPE_STREAM_SOCKET: u16 = 1;
 
@@ -24,31 +26,31 @@ pub struct VirtioVsockConfig {
 #[repr(packed)]
 #[derive(AsBytes, Clone, Copy, Debug, FromBytes)]
 pub struct VirtioVsockHdr {
-    pub src_cid: Le64,
-    pub dst_cid: Le64,
-    pub src_port: Le32,
-    pub dst_port: Le32,
-    pub len: Le32,
-    pub r#type: Le16,
-    pub op: Le16,
-    pub flags: Le32,
-    pub buf_alloc: Le32,
-    pub fwd_cnt: Le32,
+    pub src_cid: U64<LittleEndian>,
+    pub dst_cid: U64<LittleEndian>,
+    pub src_port: U32<LittleEndian>,
+    pub dst_port: U32<LittleEndian>,
+    pub len: U32<LittleEndian>,
+    pub r#type: U16<LittleEndian>,
+    pub op: U16<LittleEndian>,
+    pub flags: U32<LittleEndian>,
+    pub buf_alloc: U32<LittleEndian>,
+    pub fwd_cnt: U32<LittleEndian>,
 }
 
 impl Default for VirtioVsockHdr {
     fn default() -> Self {
         Self {
-            src_cid: Le64::default(),
-            dst_cid: Le64::default(),
-            src_port: Le32::default(),
-            dst_port: Le32::default(),
-            len: Le32::default(),
+            src_cid: 0.into(),
+            dst_cid: 0.into(),
+            src_port: 0.into(),
+            dst_port: 0.into(),
+            len: 0.into(),
             r#type: TYPE_STREAM_SOCKET.into(),
-            op: Le16::default(),
-            flags: Le32::default(),
-            buf_alloc: Le32::default(),
-            fwd_cnt: Le32::default(),
+            op: 0.into(),
+            flags: 0.into(),
+            buf_alloc: 0.into(),
+            fwd_cnt: 0.into(),
         }
     }
 }
@@ -65,7 +67,7 @@ impl<'a> VirtioVsockPacket<'a> {
             .get(0..size_of::<VirtioVsockHdr>())
             .ok_or(SocketError::BufferTooShort)?;
         let hdr = VirtioVsockHdr::read_from(hdr).ok_or(SocketError::PacketParsingFailed)?;
-        let data_end = size_of::<VirtioVsockHdr>() + (hdr.len.to_native() as usize);
+        let data_end = size_of::<VirtioVsockHdr>() + (hdr.len.get() as usize);
         let data = buffer
             .get(size_of::<VirtioVsockHdr>()..data_end)
             .ok_or(SocketError::BufferTooShort)?;
@@ -82,7 +84,7 @@ impl<'a> VirtioVsockPacket<'a> {
 #[repr(C)]
 pub struct VirtioVsockEvent {
     // ID from the virtio_vsock_event_id struct in the virtio spec
-    pub id: Le32,
+    pub id: U32<LittleEndian>,
 }
 
 #[allow(non_camel_case_types)]
@@ -106,16 +108,16 @@ pub enum Op {
     VIRTIO_VSOCK_OP_CREDIT_REQUEST = 7,
 }
 
-impl Into<Le16> for Op {
-    fn into(self) -> Le16 {
-        Le16::from(self as u16)
+impl Into<U16<LittleEndian>> for Op {
+    fn into(self) -> U16<LittleEndian> {
+        (self as u16).into()
     }
 }
 
-impl TryFrom<Le16> for Op {
+impl TryFrom<U16<LittleEndian>> for Op {
     type Error = SocketError;
 
-    fn try_from(v: Le16) -> Result<Self, Self::Error> {
+    fn try_from(v: U16<LittleEndian>) -> Result<Self, Self::Error> {
         let op = match u16::from(v) {
             0 => Self::VIRTIO_VSOCK_OP_INVALID,
             1 => Self::VIRTIO_VSOCK_OP_REQUEST,

+ 0 - 62
src/endian.rs

@@ -1,62 +0,0 @@
-//! Endian types used in this library.
-//! Each endian type is guarnteed to have the same size and alignment as a regular unsigned primiive
-//! of the equal size.
-//!
-//! # Examples
-//!
-//! ```
-//! # use endian::*;
-//!   let b = Be32::from(3);
-//!   let l = Le32::from(3);
-//!
-//!   assert_eq!(b.to_native(), 3);
-//!   assert_eq!(l.to_native(), 3);
-//!   assert!(b == 3);
-//!   assert!(l == 3);
-//! ```
-
-use zerocopy::{AsBytes, FromBytes};
-
-macro_rules! endian_type {
-    ($old_type:ident, $new_type:ident, $to_new:ident, $from_new:ident) => {
-        /// An integer type of with an explicit endianness.
-        #[repr(transparent)]
-        #[derive(Copy, Clone, Eq, PartialEq, Debug, Default, FromBytes, AsBytes)]
-        pub struct $new_type($old_type);
-
-        impl $new_type {
-            /// Converts `self` to the native endianness.
-            pub fn to_native(self) -> $old_type {
-                $old_type::$from_new(self.0)
-            }
-        }
-
-        impl PartialEq<$old_type> for $new_type {
-            fn eq(&self, other: &$old_type) -> bool {
-                self.0 == $old_type::$to_new(*other)
-            }
-        }
-
-        impl PartialEq<$new_type> for $old_type {
-            fn eq(&self, other: &$new_type) -> bool {
-                $old_type::$to_new(other.0) == *self
-            }
-        }
-
-        impl From<$new_type> for $old_type {
-            fn from(v: $new_type) -> $old_type {
-                $old_type::$from_new(v.0)
-            }
-        }
-
-        impl From<$old_type> for $new_type {
-            fn from(v: $old_type) -> $new_type {
-                $new_type($old_type::$to_new(v))
-            }
-        }
-    };
-}
-
-endian_type!(u16, Le16, to_le, from_le);
-endian_type!(u32, Le32, to_le, from_le);
-endian_type!(u64, Le64, to_le, from_le);

+ 0 - 1
src/lib.rs

@@ -48,7 +48,6 @@
 extern crate alloc;
 
 pub mod device;
-mod endian;
 mod hal;
 mod queue;
 pub mod transport;