virtio_drivers/device/net/
net_buf.rs

1use super::{VirtioNetHdr, VirtioNetHdrLegacy};
2use alloc::{vec, vec::Vec};
3use core::{convert::TryInto, mem::size_of};
4use zerocopy::{FromBytes, IntoBytes};
5
6/// A buffer used for transmitting.
7pub struct TxBuffer(pub(crate) Vec<u8>);
8
9/// A buffer used for receiving.
10pub struct RxBuffer {
11    pub(crate) buf: Vec<usize>, // for alignment
12    pub(crate) packet_len: usize,
13    pub(crate) idx: u16,
14    /// Whether `num_buffers` is missing in the `virtio_net_hdr` struct.
15    legacy_header: bool,
16}
17
18impl TxBuffer {
19    /// Constructs the buffer from the given slice.
20    pub fn from(buf: &[u8]) -> Self {
21        Self(Vec::from(buf))
22    }
23
24    /// Returns the network packet length.
25    pub fn packet_len(&self) -> usize {
26        self.0.len()
27    }
28
29    /// Returns the network packet as a slice.
30    pub fn packet(&self) -> &[u8] {
31        self.0.as_slice()
32    }
33
34    /// Returns the network packet as a mutable slice.
35    pub fn packet_mut(&mut self) -> &mut [u8] {
36        self.0.as_mut_slice()
37    }
38}
39
40impl RxBuffer {
41    /// Allocates a new buffer with length `buf_len`.
42    pub(crate) fn new(idx: usize, buf_len: usize, legacy_header: bool) -> Self {
43        Self {
44            buf: vec![0; buf_len / size_of::<usize>()],
45            packet_len: 0,
46            idx: idx.try_into().unwrap(),
47            legacy_header,
48        }
49    }
50
51    /// Set the network packet length.
52    pub(crate) fn set_packet_len(&mut self, packet_len: usize) {
53        self.packet_len = packet_len
54    }
55
56    /// Returns the network packet length (without header).
57    pub const fn packet_len(&self) -> usize {
58        self.packet_len
59    }
60
61    /// Returns all data in the buffer, including both the header and the packet.
62    pub fn as_bytes(&self) -> &[u8] {
63        self.buf.as_bytes()
64    }
65
66    /// Returns all data in the buffer with the mutable reference,
67    /// including both the header and the packet.
68    pub fn as_bytes_mut(&mut self) -> &mut [u8] {
69        self.buf.as_mut_bytes()
70    }
71
72    /// Returns a copy of the header.
73    pub fn header(&self) -> VirtioNetHdr {
74        if self.legacy_header {
75            VirtioNetHdrLegacy::ref_from_prefix(self.as_bytes())
76                .unwrap()
77                .0
78                .into()
79        } else {
80            *VirtioNetHdr::ref_from_prefix(self.as_bytes()).unwrap().0
81        }
82    }
83
84    /// Returns the network packet as a slice.
85    pub fn packet(&self) -> &[u8] {
86        let hdr_size = if self.legacy_header {
87            size_of::<VirtioNetHdrLegacy>()
88        } else {
89            size_of::<VirtioNetHdr>()
90        };
91
92        &self.buf.as_bytes()[hdr_size..hdr_size + self.packet_len]
93    }
94
95    /// Returns the network packet as a mutable slice.
96    pub fn packet_mut(&mut self) -> &mut [u8] {
97        let hdr_size = if self.legacy_header {
98            size_of::<VirtioNetHdrLegacy>()
99        } else {
100            size_of::<VirtioNetHdr>()
101        };
102        &mut self.buf.as_mut_bytes()[hdr_size..hdr_size + self.packet_len]
103    }
104}