iface: use owned frag buffer, remove frag from builder.

This commit is contained in:
Dario Nieuwenhuis 2023-01-16 02:04:41 +01:00
parent 0af56fac58
commit c015cc358e
8 changed files with 30 additions and 153 deletions

View File

@ -46,16 +46,6 @@ fn main() {
let medium = device.capabilities().medium;
let builder = InterfaceBuilder::new().ip_addrs(ip_addrs).routes(routes);
#[cfg(feature = "proto-ipv4-fragmentation")]
let mut ipv4_out_packet_cache = [0u8; 1280];
#[cfg(feature = "proto-ipv4-fragmentation")]
let builder = builder.ipv4_fragmentation_buffer(&mut ipv4_out_packet_cache[..]);
#[cfg(feature = "proto-sixlowpan-fragmentation")]
let mut sixlowpan_out_packet_cache = [0u8; 1280];
#[cfg(feature = "proto-sixlowpan-fragmentation")]
let builder = builder.sixlowpan_fragmentation_buffer(&mut sixlowpan_out_packet_cache[..]);
let builder = if medium == Medium::Ethernet {
builder
.hardware_addr(ethernet_addr.into())

View File

@ -73,16 +73,6 @@ fn main() {
.as_secs(),
);
#[cfg(feature = "proto-ipv4-fragmentation")]
let mut ipv4_out_packet_cache = [0u8; 10_000];
#[cfg(feature = "proto-ipv4-fragmentation")]
let builder = builder.ipv4_fragmentation_buffer(&mut ipv4_out_packet_cache[..]);
#[cfg(feature = "proto-sixlowpan-fragmentation")]
let mut sixlowpan_out_packet_cache = [0u8; 1280];
#[cfg(feature = "proto-sixlowpan-fragmentation")]
let mut builder = builder.sixlowpan_fragmentation_buffer(&mut sixlowpan_out_packet_cache[..]);
if medium == Medium::Ethernet {
builder = builder
.hardware_addr(ethernet_addr.into())

View File

@ -95,13 +95,6 @@ fn main() {
.hardware_addr(ieee802154_addr.into())
.neighbor_cache(neighbor_cache);
#[cfg(feature = "proto-sixlowpan-fragmentation")]
let mut out_packet_buffer = [0u8; 1280];
#[cfg(feature = "proto-sixlowpan-fragmentation")]
{
builder = builder.sixlowpan_fragmentation_buffer(&mut out_packet_buffer[..]);
}
let mut iface = builder.finalize(&mut device);
let mut sockets = SocketSet::new(vec![]);

View File

@ -172,8 +172,7 @@ fn main() {
.pan_id(Ieee802154Pan(0xbeef));
builder = builder
.hardware_addr(ieee802154_addr.into())
.neighbor_cache(neighbor_cache)
.sixlowpan_fragmentation_buffer(vec![]);
.neighbor_cache(neighbor_cache);
let mut iface = builder.finalize(&mut device);
let mut sockets = SocketSet::new(vec![]);

View File

@ -111,16 +111,8 @@ impl<K> PacketAssembler<K> {
offset
);
match self.assembler.add(offset, len) {
Ok(()) => {
net_debug!("assembler: {}", self.assembler);
Ok(())
}
Err(_) => {
net_debug!("packet assembler: too many holes, dropping.");
Err(AssemblerError)
}
}
self.assembler.add(offset, len);
Ok(())
}
/// Add a fragment into the packet that is being reassembled.
@ -149,16 +141,8 @@ impl<K> PacketAssembler<K> {
offset
);
match self.assembler.add(offset, data.len()) {
Ok(()) => {
net_debug!("assembler: {}", self.assembler);
Ok(())
}
Err(_) => {
net_debug!("packet assembler: too many holes, dropping.");
Err(AssemblerError)
}
}
self.assembler.add(offset, data.len());
Ok(())
}
/// Get an immutable slice of the underlying packet data, if reassembly complete.

View File

@ -22,7 +22,6 @@ use core::cmp;
use core::marker::PhantomData;
use core::result::Result;
use heapless::{LinearMap, Vec};
use managed::ManagedSlice;
#[cfg(any(feature = "proto-ipv4", feature = "proto-sixlowpan"))]
use super::fragmentation::PacketAssemblerSet;
@ -41,6 +40,7 @@ use crate::wire::*;
const MAX_IP_ADDR_COUNT: usize = 5;
#[cfg(feature = "proto-igmp")]
const MAX_IPV4_MULTICAST_GROUPS: usize = 4;
const FRAGMENTATION_BUFFER_SIZE: usize = 1500;
pub(crate) struct FragmentsBuffer {
#[cfg(feature = "proto-sixlowpan")]
@ -53,17 +53,14 @@ pub(crate) struct FragmentsBuffer {
sixlowpan_fragments_cache_timeout: Duration,
}
pub(crate) struct OutPackets<'a> {
pub(crate) struct OutPackets {
#[cfg(feature = "proto-ipv4-fragmentation")]
ipv4_out_packet: Ipv4OutPacket<'a>,
ipv4_out_packet: Ipv4OutPacket,
#[cfg(feature = "proto-sixlowpan-fragmentation")]
sixlowpan_out_packet: SixlowpanOutPacket<'a>,
#[cfg(not(feature = "proto-sixlowpan-fragmentation"))]
_lifetime: core::marker::PhantomData<&'a ()>,
sixlowpan_out_packet: SixlowpanOutPacket,
}
impl<'a> OutPackets<'a> {
impl OutPackets {
#[cfg(any(
feature = "proto-ipv4-fragmentation",
feature = "proto-sixlowpan-fragmentation"
@ -86,9 +83,9 @@ impl<'a> OutPackets<'a> {
#[allow(unused)]
#[cfg(feature = "proto-ipv4-fragmentation")]
pub(crate) struct Ipv4OutPacket<'a> {
pub(crate) struct Ipv4OutPacket {
/// The buffer that holds the unfragmented 6LoWPAN packet.
buffer: ManagedSlice<'a, u8>,
buffer: [u8; FRAGMENTATION_BUFFER_SIZE],
/// The size of the packet without the IEEE802.15.4 header and the fragmentation headers.
packet_len: usize,
/// The amount of bytes that already have been transmitted.
@ -106,10 +103,10 @@ pub(crate) struct Ipv4OutPacket<'a> {
}
#[cfg(feature = "proto-ipv4-fragmentation")]
impl<'a> Ipv4OutPacket<'a> {
pub(crate) fn new(buffer: ManagedSlice<'a, u8>) -> Self {
impl Ipv4OutPacket {
pub(crate) fn new() -> Self {
Self {
buffer,
buffer: [0u8; FRAGMENTATION_BUFFER_SIZE],
packet_len: 0,
sent_bytes: 0,
repr: Ipv4Repr {
@ -158,9 +155,9 @@ impl<'a> Ipv4OutPacket<'a> {
#[allow(unused)]
#[cfg(feature = "proto-sixlowpan")]
pub(crate) struct SixlowpanOutPacket<'a> {
pub(crate) struct SixlowpanOutPacket {
/// The buffer that holds the unfragmented 6LoWPAN packet.
buffer: ManagedSlice<'a, u8>,
buffer: [u8; FRAGMENTATION_BUFFER_SIZE],
/// The size of the packet without the IEEE802.15.4 header and the fragmentation headers.
packet_len: usize,
/// The amount of bytes that already have been transmitted.
@ -182,10 +179,10 @@ pub(crate) struct SixlowpanOutPacket<'a> {
}
#[cfg(feature = "proto-sixlowpan-fragmentation")]
impl<'a> SixlowpanOutPacket<'a> {
pub(crate) fn new(buffer: ManagedSlice<'a, u8>) -> Self {
impl SixlowpanOutPacket {
pub(crate) fn new() -> Self {
Self {
buffer,
buffer: [0u8; FRAGMENTATION_BUFFER_SIZE],
packet_len: 0,
datagram_size: 0,
datagram_tag: 0,
@ -246,7 +243,7 @@ use check;
pub struct Interface<'a> {
inner: InterfaceInner<'a>,
fragments: FragmentsBuffer,
out_packets: OutPackets<'a>,
out_packets: OutPackets,
}
/// The device independent part of an Ethernet network interface.
@ -307,17 +304,8 @@ pub struct InterfaceBuilder<'a> {
ipv4_multicast_groups: LinearMap<Ipv4Address, (), MAX_IPV4_MULTICAST_GROUPS>,
random_seed: u64,
#[cfg(feature = "proto-ipv4-fragmentation")]
ipv4_fragments: PacketAssemblerSet<Ipv4FragKey>,
#[cfg(feature = "proto-ipv4-fragmentation")]
ipv4_out_buffer: ManagedSlice<'a, u8>,
#[cfg(feature = "proto-sixlowpan-fragmentation")]
sixlowpan_fragments: PacketAssemblerSet<SixlowpanFragKey>,
#[cfg(feature = "proto-sixlowpan-fragmentation")]
sixlowpan_reassembly_buffer_timeout: Duration,
#[cfg(feature = "proto-sixlowpan-fragmentation")]
sixlowpan_out_buffer: ManagedSlice<'a, u8>,
#[cfg(feature = "proto-sixlowpan")]
sixlowpan_address_context: &'a [SixlowpanAddressContext<'a>],
@ -377,17 +365,8 @@ let iface = builder.finalize(&mut device);
ipv4_multicast_groups: LinearMap::new(),
random_seed: 0,
#[cfg(feature = "proto-ipv4-fragmentation")]
ipv4_fragments: PacketAssemblerSet::new(),
#[cfg(feature = "proto-ipv4-fragmentation")]
ipv4_out_buffer: ManagedSlice::Borrowed(&mut [][..]),
#[cfg(feature = "proto-sixlowpan-fragmentation")]
sixlowpan_fragments: PacketAssemblerSet::new(),
#[cfg(feature = "proto-sixlowpan-fragmentation")]
sixlowpan_reassembly_buffer_timeout: Duration::from_secs(60),
#[cfg(feature = "proto-sixlowpan-fragmentation")]
sixlowpan_out_buffer: ManagedSlice::Borrowed(&mut [][..]),
#[cfg(feature = "proto-sixlowpan")]
sixlowpan_address_context: &[],
@ -502,23 +481,6 @@ let iface = builder.finalize(&mut device);
self
}
/// Set the IPv4 reassembly buffer the interface will use.
#[cfg(feature = "proto-ipv4-fragmentation")]
pub fn ipv4_reassembly_buffer(mut self, storage: PacketAssemblerSet<Ipv4FragKey>) -> Self {
self.ipv4_fragments = storage;
self
}
/// Set the IPv4 fragments buffer the interface will use.
#[cfg(feature = "proto-ipv4-fragmentation")]
pub fn ipv4_fragmentation_buffer<T>(mut self, storage: T) -> Self
where
T: Into<ManagedSlice<'a, u8>>,
{
self.ipv4_out_buffer = storage.into();
self
}
/// Set the address contexts the interface will use.
#[cfg(feature = "proto-sixlowpan")]
pub fn sixlowpan_address_context(
@ -529,16 +491,6 @@ let iface = builder.finalize(&mut device);
self
}
/// Set the 6LoWPAN reassembly buffer the interface will use.
#[cfg(feature = "proto-sixlowpan-fragmentation")]
pub fn sixlowpan_reassembly_buffer(
mut self,
storage: PacketAssemblerSet<SixlowpanFragKey>,
) -> Self {
self.sixlowpan_fragments = storage;
self
}
/// Set the timeout value the 6LoWPAN reassembly buffer will use.
#[cfg(feature = "proto-sixlowpan-fragmentation")]
pub fn sixlowpan_reassembly_buffer_timeout(mut self, timeout: Duration) -> Self {
@ -549,16 +501,6 @@ let iface = builder.finalize(&mut device);
self
}
/// Set the 6LoWPAN fragments buffer the interface will use.
#[cfg(feature = "proto-sixlowpan-fragmentation")]
pub fn sixlowpan_fragmentation_buffer<T>(mut self, storage: T) -> Self
where
T: Into<ManagedSlice<'a, u8>>,
{
self.sixlowpan_out_buffer = storage.into();
self
}
/// Create a network interface using the previously provided configuration.
///
/// # Panics
@ -654,20 +596,17 @@ let iface = builder.finalize(&mut device);
decompress_buf: [0u8; sixlowpan::MAX_DECOMPRESSED_LEN],
#[cfg(feature = "proto-ipv4-fragmentation")]
ipv4_fragments: self.ipv4_fragments,
ipv4_fragments: PacketAssemblerSet::new(),
#[cfg(feature = "proto-sixlowpan-fragmentation")]
sixlowpan_fragments: self.sixlowpan_fragments,
sixlowpan_fragments: PacketAssemblerSet::new(),
#[cfg(feature = "proto-sixlowpan-fragmentation")]
sixlowpan_fragments_cache_timeout: self.sixlowpan_reassembly_buffer_timeout,
},
out_packets: OutPackets {
#[cfg(feature = "proto-ipv4-fragmentation")]
ipv4_out_packet: Ipv4OutPacket::new(self.ipv4_out_buffer),
ipv4_out_packet: Ipv4OutPacket::new(),
#[cfg(feature = "proto-sixlowpan-fragmentation")]
sixlowpan_out_packet: SixlowpanOutPacket::new(self.sixlowpan_out_buffer),
#[cfg(not(feature = "proto-sixlowpan-fragmentation"))]
_lifetime: core::marker::PhantomData,
sixlowpan_out_packet: SixlowpanOutPacket::new(),
},
inner: InterfaceInner {
phantom: PhantomData,
@ -1706,7 +1645,7 @@ impl<'a> InterfaceInner<'a> {
&mut self,
tx_token: Tx,
packet: EthernetPacket,
_out_packet: Option<&mut OutPackets<'_>>,
_out_packet: Option<&mut OutPackets>,
) -> Result<(), DispatchError>
where
Tx: TxToken,
@ -1921,7 +1860,7 @@ impl<'a> InterfaceInner<'a> {
&mut self,
tx_token: Tx,
packet: IpPacket,
_out_packet: Option<&mut OutPackets<'_>>,
_out_packet: Option<&mut OutPackets>,
) -> Result<(), DispatchError> {
let mut ip_repr = packet.ip_repr();
assert!(!ip_repr.dst_addr().is_unspecified());

View File

@ -384,18 +384,12 @@ impl<'a> InterfaceInner<'a> {
..
} = &mut _out_packet.unwrap().sixlowpan_out_packet;
match buffer {
managed::ManagedSlice::Borrowed(buffer) => {
if buffer.len() < total_size {
net_debug!(
if buffer.len() < total_size {
net_debug!(
"dispatch_ieee802154: dropping, fragmentation buffer is too small, at least {} needed",
total_size
);
return;
}
}
#[cfg(feature = "alloc")]
managed::ManagedSlice::Owned(buffer) => buffer.resize(total_size, 0),
return;
}
*ll_dst_addr = ll_dst_a;

View File

@ -56,9 +56,6 @@ fn create_ip<'a>() -> (Interface<'a>, SocketSet<'a>, Loopback) {
let iface_builder = InterfaceBuilder::new().ip_addrs(ip_addrs);
#[cfg(feature = "proto-ipv4-fragmentation")]
let iface_builder = iface_builder.ipv4_fragmentation_buffer(vec![]);
let iface = iface_builder.finalize(&mut device);
(iface, SocketSet::new(vec![]), device)
@ -87,12 +84,6 @@ fn create_ethernet<'a>() -> (Interface<'a>, SocketSet<'a>, Loopback) {
.neighbor_cache(NeighborCache::new())
.ip_addrs(ip_addrs);
#[cfg(feature = "proto-sixlowpan-fragmentation")]
let iface_builder = iface_builder.sixlowpan_fragmentation_buffer(vec![]);
#[cfg(feature = "proto-ipv4-fragmentation")]
let iface_builder = iface_builder.ipv4_fragmentation_buffer(vec![]);
let iface = iface_builder.finalize(&mut device);
(iface, SocketSet::new(vec![]), device)
@ -117,9 +108,6 @@ fn create_ieee802154<'a>() -> (Interface<'a>, SocketSet<'a>, Loopback) {
.neighbor_cache(NeighborCache::new())
.ip_addrs(ip_addrs);
#[cfg(feature = "proto-sixlowpan-fragmentation")]
let iface_builder = iface_builder.sixlowpan_fragmentation_buffer(vec![]);
let iface = iface_builder.finalize(&mut device);
(iface, SocketSet::new(vec![]), device)