Skip to content

Commit a7255b3

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 cfa427d commit a7255b3

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
@@ -7,30 +7,18 @@ use crate::Error as DeviceError;
77

88
use super::backend::{NetBackend, ReadError, WriteError};
99
use super::device::{FrontendError, RxError, TxError, VirtioNetBackend};
10+
use super::{vnet_hdr_len, write_virtio_net_hdr};
1011

1112
use std::os::fd::AsRawFd;
1213
use std::sync::atomic::AtomicUsize;
1314
use std::sync::atomic::Ordering;
1415
use std::sync::Arc;
1516
use std::thread;
16-
use std::{cmp, mem, result};
17+
use std::{cmp, result};
1718
use utils::epoll::{ControlOperation, Epoll, EpollEvent, EventSet};
1819
use utils::eventfd::EventFd;
19-
use virtio_bindings::virtio_net::virtio_net_hdr_v1;
2020
use vm_memory::{Bytes, GuestAddress, GuestMemoryMmap};
2121

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-
3422
pub struct NetWorker {
3523
queues: Vec<Queue>,
3624
queue_evts: Vec<EventFd>,

0 commit comments

Comments
 (0)