Skip to content

Commit

Permalink
Replace AsSliceU8 trait with zerocopy::AsBytes for VirtioNetHdr struct.
Browse files Browse the repository at this point in the history
Changed VirtioNetHdr to derive from AsBytes instead of AsSliceU8 and
changed related functions as necessary. AsSliceU8 cannot be completely
removed yet because of lack of support for deriving AsBytes for
generics.
  • Loading branch information
Joanne Chen committed Apr 21, 2023
1 parent df7483d commit 8644a76
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

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

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ smp = ["include-transformed"]
fsgsbase = []
trace = []
tcp = [
"async-task",
"futures-lite",
"smoltcp",
"async-task",
"futures-lite",
"smoltcp"
]
dhcpv4 = [
"tcp",
"smoltcp/proto-dhcpv4",
"smoltcp/socket-dhcpv4",
"tcp",
"smoltcp/proto-dhcpv4",
"smoltcp/socket-dhcpv4"
]

[dependencies]
Expand All @@ -90,6 +90,7 @@ lock_api = "0.4"
num = { version = "0.4", default-features = false }
num-traits = { version = "0.2", default-features = false }
num-derive = "0.3"
zerocopy = "0.6"

[dependencies.smoltcp]
version = "0.9"
Expand Down
9 changes: 4 additions & 5 deletions src/drivers/net/virtio_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use core::cmp::Ordering;
use core::mem;
use core::result::Result;

use zerocopy::AsBytes;

use self::constants::{FeatureSet, Features, NetHdrGSO, Status, MAX_NUM_VQ};
use self::error::VirtioNetError;
use crate::arch::kernel::core_local::increment_irq_counter;
Expand All @@ -25,7 +27,7 @@ use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg};
#[cfg(feature = "pci")]
use crate::drivers::virtio::transport::pci::{ComCfg, IsrStatus, NotifCfg};
use crate::drivers::virtio::virtqueue::{
AsSliceU8, BuffSpec, BufferToken, Bytes, Transfer, Virtq, VqIndex, VqSize, VqType,
BuffSpec, BufferToken, Bytes, Transfer, Virtq, VqIndex, VqSize, VqType,
};

pub const ETH_HDR: usize = 14usize;
Expand All @@ -41,7 +43,7 @@ pub struct NetDevCfg {
pub features: FeatureSet,
}

#[derive(Debug)]
#[derive(AsBytes, Debug)]
#[repr(C)]
pub struct VirtioNetHdr {
flags: u8,
Expand All @@ -58,9 +60,6 @@ pub struct VirtioNetHdr {
num_buffers: u16,
}

// Using the default implementation of the trait for VirtioNetHdr
impl AsSliceU8 for VirtioNetHdr {}

impl VirtioNetHdr {
pub fn get_tx_hdr() -> VirtioNetHdr {
VirtioNetHdr {
Expand Down
7 changes: 4 additions & 3 deletions src/drivers/virtio/virtqueue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use core::cell::RefCell;
use core::ops::{BitAnd, Deref, DerefMut};

use align_address::Align;
use zerocopy::AsBytes;

use self::error::{BufferError, VirtqError};
use self::packed::PackedVq;
Expand Down Expand Up @@ -1683,15 +1684,15 @@ impl BufferToken {
/// * Will result in 4 bytes written to the second buffer descriptor of the recv buffer. Nothing is written into the second buffer descriptor.
/// * Third Write: `write_seq(Some(10 bytes, Some(4 bytes))`:
/// * Will result in 10 bytes written to the second buffer descriptor of the send buffer and 4 bytes written to the third buffer descriptor of the recv buffer.
pub fn write_seq<K: AsSliceU8, H: AsSliceU8 + ?Sized>(
pub fn write_seq<K: AsBytes, H: AsBytes + ?Sized>(
mut self,
send_seq: Option<&K>,
recv_seq: Option<&H>,
) -> Result<Self, VirtqError> {
if let Some(data) = send_seq {
match self.send_buff.as_mut() {
Some(buff) => {
match buff.next_write(data.as_slice_u8()) {
match buff.next_write(data.as_bytes()) {
Ok(_) => (), // Do nothing, write fitted inside descriptor and not to many writes to buffer happened
Err(_) => {
// Need no match here, as result is the same, but for the future one could
Expand All @@ -1707,7 +1708,7 @@ impl BufferToken {
if let Some(data) = recv_seq {
match self.recv_buff.as_mut() {
Some(buff) => {
match buff.next_write(data.as_slice_u8()) {
match buff.next_write(data.as_bytes()) {
Ok(_) => (), // Do nothing, write fitted inside descriptor and not to many writes to buffer happened
Err(_) => {
// Need no match here, as result is the same, but for the future one could
Expand Down

0 comments on commit 8644a76

Please sign in to comment.