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

virtio-net: Replace AsSliceU8 trait with zerocopy::AsBytes. #716

Merged
merged 1 commit into from
Apr 21, 2023
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
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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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