aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-06-21 01:49:32 +0200
committerGitHub <[email protected]>2021-06-21 01:49:32 +0200
commit9e5406f761a5a71e8a634796a60f1148b7d96ec1 (patch)
tree45dcecda6c8e6cd65502b37ddfccb4b8736362c6
parent5a4e3ceb884f0b2d9baaa57ada61bbf30cd4c208 (diff)
parentaca0fb10651359b5a3939f7424ac0a78ef20cc27 (diff)
Merge pull request #252 from thalesfragoso/net-resources
net: Make the user pass in the StackResources in init
-rw-r--r--embassy-net/src/lib.rs2
-rw-r--r--embassy-net/src/stack.rs54
-rw-r--r--examples/std/src/bin/net.rs5
-rw-r--r--examples/stm32h7/src/bin/eth.rs9
4 files changed, 41 insertions, 29 deletions
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs
index 51eb97a2e..47a3650b0 100644
--- a/embassy-net/src/lib.rs
+++ b/embassy-net/src/lib.rs
@@ -14,7 +14,7 @@ pub use config::{Config, Configurator, Event as ConfigEvent, StaticConfigurator}
14 14
15pub use device::{Device, LinkState}; 15pub use device::{Device, LinkState};
16pub use packet_pool::{Packet, PacketBox, PacketBoxExt, PacketBuf, MTU}; 16pub use packet_pool::{Packet, PacketBox, PacketBoxExt, PacketBuf, MTU};
17pub use stack::{init, is_config_up, is_init, is_link_up, run}; 17pub use stack::{init, is_config_up, is_init, is_link_up, run, StackResources};
18 18
19#[cfg(feature = "tcp")] 19#[cfg(feature = "tcp")]
20mod tcp_socket; 20mod tcp_socket;
diff --git a/embassy-net/src/stack.rs b/embassy-net/src/stack.rs
index a38f00958..5e795a4fd 100644
--- a/embassy-net/src/stack.rs
+++ b/embassy-net/src/stack.rs
@@ -4,7 +4,7 @@ use core::task::Context;
4use core::task::Poll; 4use core::task::Poll;
5use embassy::time::{Instant, Timer}; 5use embassy::time::{Instant, Timer};
6use embassy::util::ThreadModeMutex; 6use embassy::util::ThreadModeMutex;
7use embassy::util::{Forever, WakerRegistration}; 7use embassy::util::WakerRegistration;
8use futures::pin_mut; 8use futures::pin_mut;
9use smoltcp::iface::InterfaceBuilder; 9use smoltcp::iface::InterfaceBuilder;
10#[cfg(feature = "medium-ethernet")] 10#[cfg(feature = "medium-ethernet")]
@@ -22,23 +22,34 @@ use crate::config::Event;
22use crate::device::{Device, DeviceAdapter, LinkState}; 22use crate::device::{Device, DeviceAdapter, LinkState};
23use crate::{Interface, SocketSet}; 23use crate::{Interface, SocketSet};
24 24
25const ADDRESSES_LEN: usize = 1;
26const NEIGHBOR_CACHE_LEN: usize = 8;
27const SOCKETS_LEN: usize = 2;
28const LOCAL_PORT_MIN: u16 = 1025; 25const LOCAL_PORT_MIN: u16 = 1025;
29const LOCAL_PORT_MAX: u16 = 65535; 26const LOCAL_PORT_MAX: u16 = 65535;
30 27
31struct StackResources { 28pub struct StackResources<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> {
32 addresses: [IpCidr; ADDRESSES_LEN], 29 addresses: [IpCidr; ADDR],
33 sockets: [Option<SocketSetItem<'static>>; SOCKETS_LEN], 30 sockets: [Option<SocketSetItem<'static>>; SOCK],
34 31
35 #[cfg(feature = "medium-ethernet")] 32 #[cfg(feature = "medium-ethernet")]
36 routes: [Option<(IpCidr, Route)>; 1], 33 routes: [Option<(IpCidr, Route)>; 1],
37 #[cfg(feature = "medium-ethernet")] 34 #[cfg(feature = "medium-ethernet")]
38 neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR_CACHE_LEN], 35 neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR],
36}
37
38impl<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize>
39 StackResources<ADDR, SOCK, NEIGHBOR>
40{
41 pub fn new() -> Self {
42 const NONE_SOCKET: Option<SocketSetItem<'static>> = None;
43
44 Self {
45 addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32); ADDR],
46 sockets: [NONE_SOCKET; SOCK],
47 routes: [None; 1],
48 neighbor_cache: [None; NEIGHBOR],
49 }
50 }
39} 51}
40 52
41static STACK_RESOURCES: Forever<StackResources> = Forever::new();
42static STACK: ThreadModeMutex<RefCell<Option<Stack>>> = ThreadModeMutex::new(RefCell::new(None)); 53static STACK: ThreadModeMutex<RefCell<Option<Stack>>> = ThreadModeMutex::new(RefCell::new(None));
43 54
44pub(crate) struct Stack { 55pub(crate) struct Stack {
@@ -164,18 +175,11 @@ fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
164 175
165/// Initialize embassy_net. 176/// Initialize embassy_net.
166/// This function must be called from thread mode. 177/// This function must be called from thread mode.
167pub fn init(device: &'static mut dyn Device, configurator: &'static mut dyn Configurator) { 178pub fn init<const ADDR: usize, const SOCK: usize, const NEIGH: usize>(
168 const NONE_SOCKET: Option<SocketSetItem<'static>> = None; 179 device: &'static mut dyn Device,
169 let res = STACK_RESOURCES.put(StackResources { 180 configurator: &'static mut dyn Configurator,
170 addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32)], 181 resources: &'static mut StackResources<ADDR, SOCK, NEIGH>,
171 sockets: [NONE_SOCKET; SOCKETS_LEN], 182) {
172
173 #[cfg(feature = "medium-ethernet")]
174 routes: [None; 1],
175 #[cfg(feature = "medium-ethernet")]
176 neighbor_cache: [None; NEIGHBOR_CACHE_LEN],
177 });
178
179 let medium = device.capabilities().medium; 183 let medium = device.capabilities().medium;
180 184
181 #[cfg(feature = "medium-ethernet")] 185 #[cfg(feature = "medium-ethernet")]
@@ -186,18 +190,18 @@ pub fn init(device: &'static mut dyn Device, configurator: &'static mut dyn Conf
186 }; 190 };
187 191
188 let mut b = InterfaceBuilder::new(DeviceAdapter::new(device)); 192 let mut b = InterfaceBuilder::new(DeviceAdapter::new(device));
189 b = b.ip_addrs(&mut res.addresses[..]); 193 b = b.ip_addrs(&mut resources.addresses[..]);
190 194
191 #[cfg(feature = "medium-ethernet")] 195 #[cfg(feature = "medium-ethernet")]
192 if medium == Medium::Ethernet { 196 if medium == Medium::Ethernet {
193 b = b.ethernet_addr(EthernetAddress(ethernet_addr)); 197 b = b.ethernet_addr(EthernetAddress(ethernet_addr));
194 b = b.neighbor_cache(NeighborCache::new(&mut res.neighbor_cache[..])); 198 b = b.neighbor_cache(NeighborCache::new(&mut resources.neighbor_cache[..]));
195 b = b.routes(Routes::new(&mut res.routes[..])); 199 b = b.routes(Routes::new(&mut resources.routes[..]));
196 } 200 }
197 201
198 let iface = b.finalize(); 202 let iface = b.finalize();
199 203
200 let sockets = SocketSet::new(&mut res.sockets[..]); 204 let sockets = SocketSet::new(&mut resources.sockets[..]);
201 205
202 let local_port = loop { 206 let local_port = loop {
203 let mut res = [0u8; 2]; 207 let mut res = [0u8; 2];
diff --git a/examples/std/src/bin/net.rs b/examples/std/src/bin/net.rs
index 5a726e5d2..344a6e4d3 100644
--- a/examples/std/src/bin/net.rs
+++ b/examples/std/src/bin/net.rs
@@ -19,6 +19,7 @@ use crate::tuntap::TunTapDevice;
19 19
20static DEVICE: Forever<TunTapDevice> = Forever::new(); 20static DEVICE: Forever<TunTapDevice> = Forever::new();
21static CONFIG: Forever<DhcpConfigurator> = Forever::new(); 21static CONFIG: Forever<DhcpConfigurator> = Forever::new();
22static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new();
22 23
23#[derive(Clap)] 24#[derive(Clap)]
24#[clap(version = "1.0")] 25#[clap(version = "1.0")]
@@ -51,8 +52,10 @@ async fn main_task(spawner: Spawner) {
51 // DHCP configruation 52 // DHCP configruation
52 let config = DhcpConfigurator::new(); 53 let config = DhcpConfigurator::new();
53 54
55 let net_resources = StackResources::new();
56
54 // Init network stack 57 // Init network stack
55 embassy_net::init(DEVICE.put(device), CONFIG.put(config)); 58 embassy_net::init(DEVICE.put(device), CONFIG.put(config), NET_RESOURCES.put(net_resources));
56 59
57 // Launch network task 60 // Launch network task
58 spawner.spawn(net_task()).unwrap(); 61 spawner.spawn(net_task()).unwrap();
diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs
index 57997da0a..7d7ff941e 100644
--- a/examples/stm32h7/src/bin/eth.rs
+++ b/examples/stm32h7/src/bin/eth.rs
@@ -16,7 +16,9 @@ use embassy::io::AsyncWriteExt;
16use embassy::time::{Duration, Timer}; 16use embassy::time::{Duration, Timer};
17use embassy::util::Forever; 17use embassy::util::Forever;
18use embassy_macros::interrupt_take; 18use embassy_macros::interrupt_take;
19use embassy_net::{Config as NetConfig, Ipv4Address, Ipv4Cidr, StaticConfigurator, TcpSocket}; 19use embassy_net::{
20 Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator, TcpSocket,
21};
20use embassy_stm32::clock::{Alarm, Clock}; 22use embassy_stm32::clock::{Alarm, Clock};
21use embassy_stm32::eth::lan8742a::LAN8742A; 23use embassy_stm32::eth::lan8742a::LAN8742A;
22use embassy_stm32::eth::Ethernet; 24use embassy_stm32::eth::Ethernet;
@@ -43,8 +45,10 @@ async fn main_task(
43 config: &'static mut StaticConfigurator, 45 config: &'static mut StaticConfigurator,
44 spawner: Spawner, 46 spawner: Spawner,
45) { 47) {
48 let net_resources = NET_RESOURCES.put(StackResources::new());
49
46 // Init network stack 50 // Init network stack
47 embassy_net::init(device, config); 51 embassy_net::init(device, config, net_resources);
48 52
49 // Launch network task 53 // Launch network task
50 unwrap!(spawner.spawn(net_task())); 54 unwrap!(spawner.spawn(net_task()));
@@ -97,6 +101,7 @@ static ALARM: Forever<Alarm<TIM2>> = Forever::new();
97static ETH: Forever<Ethernet<'static, LAN8742A, 4, 4>> = Forever::new(); 101static ETH: Forever<Ethernet<'static, LAN8742A, 4, 4>> = Forever::new();
98static DEVICE: Forever<Pin<&'static mut Ethernet<'static, LAN8742A, 4, 4>>> = Forever::new(); 102static DEVICE: Forever<Pin<&'static mut Ethernet<'static, LAN8742A, 4, 4>>> = Forever::new();
99static CONFIG: Forever<StaticConfigurator> = Forever::new(); 103static CONFIG: Forever<StaticConfigurator> = Forever::new();
104static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new();
100 105
101#[entry] 106#[entry]
102fn main() -> ! { 107fn main() -> ! {