virtio_drivers/device/net/
net_buf.rsuse super::{VirtioNetHdr, NET_HDR_SIZE};
use alloc::{vec, vec::Vec};
use core::{convert::TryInto, mem::size_of};
use zerocopy::AsBytes;
pub struct TxBuffer(pub(crate) Vec<u8>);
pub struct RxBuffer {
pub(crate) buf: Vec<usize>, pub(crate) packet_len: usize,
pub(crate) idx: u16,
}
impl TxBuffer {
pub fn from(buf: &[u8]) -> Self {
Self(Vec::from(buf))
}
pub fn packet_len(&self) -> usize {
self.0.len()
}
pub fn packet(&self) -> &[u8] {
self.0.as_slice()
}
pub fn packet_mut(&mut self) -> &mut [u8] {
self.0.as_mut_slice()
}
}
impl RxBuffer {
pub(crate) fn new(idx: usize, buf_len: usize) -> Self {
Self {
buf: vec![0; buf_len / size_of::<usize>()],
packet_len: 0,
idx: idx.try_into().unwrap(),
}
}
pub(crate) fn set_packet_len(&mut self, packet_len: usize) {
self.packet_len = packet_len
}
pub const fn packet_len(&self) -> usize {
self.packet_len
}
pub fn as_bytes(&self) -> &[u8] {
self.buf.as_bytes()
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
self.buf.as_bytes_mut()
}
pub fn header(&self) -> &VirtioNetHdr {
unsafe { &*(self.buf.as_ptr() as *const VirtioNetHdr) }
}
pub fn packet(&self) -> &[u8] {
&self.buf.as_bytes()[NET_HDR_SIZE..NET_HDR_SIZE + self.packet_len]
}
pub fn packet_mut(&mut self) -> &mut [u8] {
&mut self.buf.as_bytes_mut()[NET_HDR_SIZE..NET_HDR_SIZE + self.packet_len]
}
}