Skip to content

Commit 1f43684

Browse files
committed
net: refactor vnet_hdr methods
We're going to use it from different modules, so move them upper in the crate. Signed-off-by: Sergio Lopez <[email protected]>
1 parent 9b8e9eb commit 1f43684

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/devices/src/virtio/net/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::{io, result};
4+
use std::{io, mem, result};
5+
use virtio_bindings::virtio_net::virtio_net_hdr_v1;
6+
57
pub const MAX_BUFFER_SIZE: usize = 65562;
68
pub const QUEUE_SIZE: u16 = 1024;
79
pub const NUM_QUEUES: usize = 2;
@@ -17,6 +19,18 @@ mod unixgram;
1719
mod unixstream;
1820
mod worker;
1921

22+
fn vnet_hdr_len() -> usize {
23+
mem::size_of::<virtio_net_hdr_v1>()
24+
}
25+
26+
// This initializes to all 0 the virtio_net_hdr part of a buf and return the length of the header
27+
// https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.html#x1-2050006
28+
fn write_virtio_net_hdr(buf: &mut [u8]) -> usize {
29+
let len = vnet_hdr_len();
30+
buf[0..len].fill(0);
31+
len
32+
}
33+
2034
pub use self::device::Net;
2135
#[derive(Debug)]
2236
pub enum Error {

src/devices/src/virtio/net/worker.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,15 @@ use crate::virtio::{InterruptTransport, Queue};
55

66
use super::backend::{NetBackend, ReadError, WriteError};
77
use super::device::{FrontendError, RxError, TxError, VirtioNetBackend};
8+
use super::{vnet_hdr_len, write_virtio_net_hdr};
89

910
use std::os::fd::AsRawFd;
1011
use std::thread;
11-
use std::{cmp, mem, result};
12+
use std::{cmp, result};
1213
use utils::epoll::{ControlOperation, Epoll, EpollEvent, EventSet};
1314
use utils::eventfd::EventFd;
14-
use virtio_bindings::virtio_net::virtio_net_hdr_v1;
1515
use vm_memory::{Bytes, GuestAddress, GuestMemoryMmap};
1616

17-
fn vnet_hdr_len() -> usize {
18-
mem::size_of::<virtio_net_hdr_v1>()
19-
}
20-
21-
// This initializes to all 0 the virtio_net_hdr part of a buf and return the length of the header
22-
// https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.html#x1-2050006
23-
fn write_virtio_net_hdr(buf: &mut [u8]) -> usize {
24-
let len = vnet_hdr_len();
25-
buf[0..len].fill(0);
26-
len
27-
}
28-
2917
pub struct NetWorker {
3018
queues: Vec<Queue>,
3119
queue_evts: Vec<EventFd>,

0 commit comments

Comments
 (0)