Skip to content

Commit 8644a76

Browse files
author
Joanne Chen
committed
Replace AsSliceU8 trait with zerocopy::AsBytes for VirtioNetHdr struct.
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.
1 parent df7483d commit 8644a76

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

Cargo.lock

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+7-6
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ smp = ["include-transformed"]
5757
fsgsbase = []
5858
trace = []
5959
tcp = [
60-
"async-task",
61-
"futures-lite",
62-
"smoltcp",
60+
"async-task",
61+
"futures-lite",
62+
"smoltcp"
6363
]
6464
dhcpv4 = [
65-
"tcp",
66-
"smoltcp/proto-dhcpv4",
67-
"smoltcp/socket-dhcpv4",
65+
"tcp",
66+
"smoltcp/proto-dhcpv4",
67+
"smoltcp/socket-dhcpv4"
6868
]
6969

7070
[dependencies]
@@ -90,6 +90,7 @@ lock_api = "0.4"
9090
num = { version = "0.4", default-features = false }
9191
num-traits = { version = "0.2", default-features = false }
9292
num-derive = "0.3"
93+
zerocopy = "0.6"
9394

9495
[dependencies.smoltcp]
9596
version = "0.9"

src/drivers/net/virtio_net.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use core::cmp::Ordering;
1111
use core::mem;
1212
use core::result::Result;
1313

14+
use zerocopy::AsBytes;
15+
1416
use self::constants::{FeatureSet, Features, NetHdrGSO, Status, MAX_NUM_VQ};
1517
use self::error::VirtioNetError;
1618
use crate::arch::kernel::core_local::increment_irq_counter;
@@ -25,7 +27,7 @@ use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg};
2527
#[cfg(feature = "pci")]
2628
use crate::drivers::virtio::transport::pci::{ComCfg, IsrStatus, NotifCfg};
2729
use crate::drivers::virtio::virtqueue::{
28-
AsSliceU8, BuffSpec, BufferToken, Bytes, Transfer, Virtq, VqIndex, VqSize, VqType,
30+
BuffSpec, BufferToken, Bytes, Transfer, Virtq, VqIndex, VqSize, VqType,
2931
};
3032

3133
pub const ETH_HDR: usize = 14usize;
@@ -41,7 +43,7 @@ pub struct NetDevCfg {
4143
pub features: FeatureSet,
4244
}
4345

44-
#[derive(Debug)]
46+
#[derive(AsBytes, Debug)]
4547
#[repr(C)]
4648
pub struct VirtioNetHdr {
4749
flags: u8,
@@ -58,9 +60,6 @@ pub struct VirtioNetHdr {
5860
num_buffers: u16,
5961
}
6062

61-
// Using the default implementation of the trait for VirtioNetHdr
62-
impl AsSliceU8 for VirtioNetHdr {}
63-
6463
impl VirtioNetHdr {
6564
pub fn get_tx_hdr() -> VirtioNetHdr {
6665
VirtioNetHdr {

src/drivers/virtio/virtqueue/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use core::cell::RefCell;
2121
use core::ops::{BitAnd, Deref, DerefMut};
2222

2323
use align_address::Align;
24+
use zerocopy::AsBytes;
2425

2526
use self::error::{BufferError, VirtqError};
2627
use self::packed::PackedVq;
@@ -1683,15 +1684,15 @@ impl BufferToken {
16831684
/// * Will result in 4 bytes written to the second buffer descriptor of the recv buffer. Nothing is written into the second buffer descriptor.
16841685
/// * Third Write: `write_seq(Some(10 bytes, Some(4 bytes))`:
16851686
/// * 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.
1686-
pub fn write_seq<K: AsSliceU8, H: AsSliceU8 + ?Sized>(
1687+
pub fn write_seq<K: AsBytes, H: AsBytes + ?Sized>(
16871688
mut self,
16881689
send_seq: Option<&K>,
16891690
recv_seq: Option<&H>,
16901691
) -> Result<Self, VirtqError> {
16911692
if let Some(data) = send_seq {
16921693
match self.send_buff.as_mut() {
16931694
Some(buff) => {
1694-
match buff.next_write(data.as_slice_u8()) {
1695+
match buff.next_write(data.as_bytes()) {
16951696
Ok(_) => (), // Do nothing, write fitted inside descriptor and not to many writes to buffer happened
16961697
Err(_) => {
16971698
// Need no match here, as result is the same, but for the future one could
@@ -1707,7 +1708,7 @@ impl BufferToken {
17071708
if let Some(data) = recv_seq {
17081709
match self.recv_buff.as_mut() {
17091710
Some(buff) => {
1710-
match buff.next_write(data.as_slice_u8()) {
1711+
match buff.next_write(data.as_bytes()) {
17111712
Ok(_) => (), // Do nothing, write fitted inside descriptor and not to many writes to buffer happened
17121713
Err(_) => {
17131714
// Need no match here, as result is the same, but for the future one could

0 commit comments

Comments
 (0)