Skip to content

Commit 7f09f38

Browse files
author
Joanne Chen
committed
Replace AsSliceU8 trait with zerocopy::AsBytes.
Changed VirtioNetHdr to derive from AsBytes instead of AsSliceU8 and changed related functions as necessary. Fixes hermit-os#692.
1 parent 7f7ef30 commit 7f09f38

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
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

+3-10
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,8 @@ acpi = []
5656
smp = ["include-transformed"]
5757
fsgsbase = []
5858
trace = []
59-
tcp = [
60-
"async-task",
61-
"futures-lite",
62-
"smoltcp",
63-
]
64-
dhcpv4 = [
65-
"tcp",
66-
"smoltcp/proto-dhcpv4",
67-
"smoltcp/socket-dhcpv4",
68-
]
59+
tcp = ["async-task", "futures-lite", "smoltcp"]
60+
dhcpv4 = ["tcp", "smoltcp/proto-dhcpv4", "smoltcp/socket-dhcpv4"]
6961

7062
[dependencies]
7163
ahash = { version = "0.8", default-features = false }
@@ -90,6 +82,7 @@ lock_api = "0.4"
9082
num = { version = "0.4", default-features = false }
9183
num-traits = { version = "0.2", default-features = false }
9284
num-derive = "0.3"
85+
zerocopy = "0.6.1"
9386

9487
[dependencies.smoltcp]
9588
version = "0.9"

src/drivers/net/virtio_net.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use core::cell::RefCell;
1010
use core::cmp::Ordering;
1111
use core::mem;
1212
use core::result::Result;
13+
use zerocopy::AsBytes;
1314

1415
use self::constants::{FeatureSet, Features, NetHdrGSO, Status, MAX_NUM_VQ};
1516
use self::error::VirtioNetError;
@@ -25,7 +26,7 @@ use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg};
2526
#[cfg(feature = "pci")]
2627
use crate::drivers::virtio::transport::pci::{ComCfg, IsrStatus, NotifCfg};
2728
use crate::drivers::virtio::virtqueue::{
28-
AsSliceU8, BuffSpec, BufferToken, Bytes, Transfer, Virtq, VqIndex, VqSize, VqType,
29+
BuffSpec, BufferToken, Bytes, Transfer, Virtq, VqIndex, VqSize, VqType,
2930
};
3031

3132
pub const ETH_HDR: usize = 14usize;
@@ -41,6 +42,7 @@ pub struct NetDevCfg {
4142
pub features: FeatureSet,
4243
}
4344

45+
#[derive(AsBytes)]
4446
#[derive(Debug)]
4547
#[repr(C)]
4648
pub struct VirtioNetHdr {
@@ -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
@@ -19,6 +19,7 @@ use alloc::rc::Rc;
1919
use alloc::vec::Vec;
2020
use core::cell::RefCell;
2121
use core::ops::{BitAnd, Deref, DerefMut};
22+
use zerocopy::AsBytes;
2223

2324
use align_address::Align;
2425

@@ -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

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ extern crate log;
3636
extern crate std;
3737
#[macro_use]
3838
extern crate num_derive;
39+
#[macro_use]
40+
extern crate zerocopy;
3941

4042
use alloc::alloc::Layout;
4143
use core::alloc::GlobalAlloc;

0 commit comments

Comments
 (0)