aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src/packet_pool.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-12-07 15:55:46 +0100
committerDario Nieuwenhuis <[email protected]>2022-12-13 16:18:39 +0100
commitac74613b5a7be72acd8d5259055963f8b4aba7fd (patch)
treebcc287e4edf65e0bb15cb5ff880aa0319ebf254f /embassy-net/src/packet_pool.rs
parent47747d3b73f392e53ead8ff49cd09fd017df3215 (diff)
net: remove packet pool.
The pool was prone to deadlocks, especially due to having a single pool for rx+tx. If the pool got full with rx'd packets it would deadlock because processing a rx packet requires doing another allocation on the pool, for the possibly tx'd response, before deallocating the rx'd packet. This also allows Device impls to allocate the packet memory in a particular RAM kind, if needed for example to do DMA. The `Device` trait is now token-based, like smoltcp's. In the end, this is better because it allows callers to manage memory however they want (including with a pool if they want to).
Diffstat (limited to 'embassy-net/src/packet_pool.rs')
-rw-r--r--embassy-net/src/packet_pool.rs107
1 files changed, 0 insertions, 107 deletions
diff --git a/embassy-net/src/packet_pool.rs b/embassy-net/src/packet_pool.rs
deleted file mode 100644
index cb8a1316c..000000000
--- a/embassy-net/src/packet_pool.rs
+++ /dev/null
@@ -1,107 +0,0 @@
1use core::ops::{Deref, DerefMut, Range};
2
3use as_slice::{AsMutSlice, AsSlice};
4use atomic_pool::{pool, Box};
5
6pub const MTU: usize = 1516;
7
8#[cfg(feature = "pool-4")]
9pub const PACKET_POOL_SIZE: usize = 4;
10
11#[cfg(feature = "pool-8")]
12pub const PACKET_POOL_SIZE: usize = 8;
13
14#[cfg(feature = "pool-16")]
15pub const PACKET_POOL_SIZE: usize = 16;
16
17#[cfg(feature = "pool-32")]
18pub const PACKET_POOL_SIZE: usize = 32;
19
20#[cfg(feature = "pool-64")]
21pub const PACKET_POOL_SIZE: usize = 64;
22
23#[cfg(feature = "pool-128")]
24pub const PACKET_POOL_SIZE: usize = 128;
25
26pool!(pub PacketPool: [Packet; PACKET_POOL_SIZE]);
27pub type PacketBox = Box<PacketPool>;
28
29#[repr(align(4))]
30pub struct Packet(pub [u8; MTU]);
31
32impl Packet {
33 pub const fn new() -> Self {
34 Self([0; MTU])
35 }
36}
37
38pub trait PacketBoxExt {
39 fn slice(self, range: Range<usize>) -> PacketBuf;
40}
41
42impl PacketBoxExt for PacketBox {
43 fn slice(self, range: Range<usize>) -> PacketBuf {
44 PacketBuf { packet: self, range }
45 }
46}
47
48impl AsSlice for Packet {
49 type Element = u8;
50
51 fn as_slice(&self) -> &[Self::Element] {
52 &self.deref()[..]
53 }
54}
55
56impl AsMutSlice for Packet {
57 fn as_mut_slice(&mut self) -> &mut [Self::Element] {
58 &mut self.deref_mut()[..]
59 }
60}
61
62impl Deref for Packet {
63 type Target = [u8; MTU];
64
65 fn deref(&self) -> &[u8; MTU] {
66 &self.0
67 }
68}
69
70impl DerefMut for Packet {
71 fn deref_mut(&mut self) -> &mut [u8; MTU] {
72 &mut self.0
73 }
74}
75
76pub struct PacketBuf {
77 packet: PacketBox,
78 range: Range<usize>,
79}
80
81impl AsSlice for PacketBuf {
82 type Element = u8;
83
84 fn as_slice(&self) -> &[Self::Element] {
85 &self.packet[self.range.clone()]
86 }
87}
88
89impl AsMutSlice for PacketBuf {
90 fn as_mut_slice(&mut self) -> &mut [Self::Element] {
91 &mut self.packet[self.range.clone()]
92 }
93}
94
95impl Deref for PacketBuf {
96 type Target = [u8];
97
98 fn deref(&self) -> &[u8] {
99 &self.packet[self.range.clone()]
100 }
101}
102
103impl DerefMut for PacketBuf {
104 fn deref_mut(&mut self) -> &mut [u8] {
105 &mut self.packet[self.range.clone()]
106 }
107}