net: Support dual stack IP

This commit is contained in:
Ruben De Smet 2023-06-07 12:04:15 +02:00
parent ca47af6978
commit 352f0b6c38
No known key found for this signature in database
GPG Key ID: 1AE26A210C14115B
19 changed files with 94 additions and 45 deletions

View File

@ -127,16 +127,61 @@ impl Default for DhcpConfig {
}
/// Network stack configuration.
pub enum Config {
/// Use a static IPv4 address configuration.
pub struct Config {
#[cfg(feature = "proto-ipv4")]
StaticV4(StaticConfigV4),
/// Use a static IPv6 address configuration.
pub ipv4: ConfigV4,
#[cfg(feature = "proto-ipv6")]
StaticV6(StaticConfigV6),
pub ipv6: ConfigV6,
}
impl Config {
#[cfg(feature = "proto-ipv4")]
pub fn ipv4_static(config: StaticConfigV4) -> Self {
Self {
ipv4: ConfigV4::Static(config),
#[cfg(feature = "proto-ipv6")]
ipv6: ConfigV6::None,
}
}
#[cfg(feature = "proto-ipv6")]
pub fn ipv6_static(config: StaticConfigV6) -> Self {
Self {
#[cfg(feature = "proto-ipv4")]
ipv4: ConfigV4::None,
ipv6: ConfigV6::Static(config),
}
}
#[cfg(feature = "dhcpv4")]
pub fn dhcpv4(config: DhcpConfig) -> Self {
Self {
ipv4: ConfigV4::Dhcp(config),
#[cfg(feature = "proto-ipv6")]
ipv6: ConfigV6::None,
}
}
}
/// Network stack IPv4 configuration.
#[cfg(feature = "proto-ipv4")]
pub enum ConfigV4 {
/// Use a static IPv4 address configuration.
Static(StaticConfigV4),
/// Use DHCP to obtain an IP address configuration.
#[cfg(feature = "dhcpv4")]
Dhcp(DhcpConfig),
/// Do not configure IPv6.
None,
}
/// Network stack IPv6 configuration.
#[cfg(feature = "proto-ipv6")]
pub enum ConfigV6 {
/// Use a static IPv6 address configuration.
Static(StaticConfigV6),
/// Do not configure IPv6.
None,
}
/// A network stack.
@ -224,22 +269,26 @@ impl<D: Driver + 'static> Stack<D> {
dns_waker: WakerRegistration::new(),
};
match config {
#[cfg(feature = "proto-ipv4")]
Config::StaticV4(config) => {
#[cfg(feature = "proto-ipv4")]
match config.ipv4 {
ConfigV4::Static(config) => {
inner.apply_config_v4(&mut socket, config);
}
#[cfg(feature = "proto-ipv6")]
Config::StaticV6(config) => {
inner.apply_config_v6(&mut socket, config);
}
#[cfg(feature = "dhcpv4")]
Config::Dhcp(config) => {
ConfigV4::Dhcp(config) => {
let mut dhcp_socket = smoltcp::socket::dhcpv4::Socket::new();
inner.apply_dhcp_config(&mut dhcp_socket, config);
let handle = socket.sockets.add(dhcp_socket);
inner.dhcp_socket = Some(handle);
}
ConfigV4::None => {}
}
#[cfg(feature = "proto-ipv6")]
match config.ipv6 {
ConfigV6::Static(config) => {
inner.apply_config_v6(&mut socket, config);
}
ConfigV6::None => {}
}
Self {

View File

@ -97,12 +97,12 @@ async fn main(spawner: Spawner) {
let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(make_static!(NetState::new()), our_mac_addr);
unwrap!(spawner.spawn(usb_ncm_task(runner)));
let config = embassy_net::Config::Dhcp(Default::default());
//let config = embassy_net::Config::StaticV4(embassy_net::StaticConfigV4 {
let config = embassy_net::Config::dhcpv4(Default::default());
// let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
// dns_servers: Vec::new(),
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),
//});
// });
// Generate random seed
let mut rng = Rng::new(p.RNG, Irqs);

View File

@ -64,7 +64,7 @@ async fn main(spawner: Spawner) {
// Init network stack
let stack = &*make_static!(Stack::new(
device,
embassy_net::Config::Dhcp(Default::default()),
embassy_net::Config::dhcpv4(Default::default()),
make_static!(StackResources::<3>::new()),
seed
));

View File

@ -67,7 +67,7 @@ async fn main(spawner: Spawner) {
// Init network stack
let stack = &*make_static!(Stack::new(
device,
embassy_net::Config::Dhcp(Default::default()),
embassy_net::Config::dhcpv4(Default::default()),
make_static!(StackResources::<2>::new()),
seed
));

View File

@ -65,7 +65,7 @@ async fn main(spawner: Spawner) {
// Init network stack
let stack = &*make_static!(Stack::new(
device,
embassy_net::Config::Dhcp(Default::default()),
embassy_net::Config::dhcpv4(Default::default()),
make_static!(StackResources::<2>::new()),
seed
));

View File

@ -62,7 +62,7 @@ async fn main(spawner: Spawner) {
// Init network stack
let stack = &*make_static!(Stack::new(
device,
embassy_net::Config::Dhcp(Default::default()),
embassy_net::Config::dhcpv4(Default::default()),
make_static!(StackResources::<2>::new()),
seed
));

View File

@ -86,8 +86,8 @@ async fn main(spawner: Spawner) {
let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(make_static!(NetState::new()), our_mac_addr);
unwrap!(spawner.spawn(usb_ncm_task(runner)));
let config = embassy_net::Config::Dhcp(Default::default());
//let config = embassy_net::Config::StaticV4(embassy_net::StaticConfigV4 {
let config = embassy_net::Config::dhcpv4(Default::default());
//let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
// dns_servers: Vec::new(),
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),

View File

@ -62,7 +62,7 @@ async fn main(spawner: Spawner) {
.await;
// Use a link-local address for communication without DHCP server
let config = Config::StaticV4(embassy_net::StaticConfigV4 {
let config = Config::ipv4_static(embassy_net::StaticConfigV4 {
address: embassy_net::Ipv4Cidr::new(embassy_net::Ipv4Address::new(169, 254, 1, 1), 16),
dns_servers: heapless::Vec::new(),
gateway: None,

View File

@ -61,8 +61,8 @@ async fn main(spawner: Spawner) {
.set_power_management(cyw43::PowerManagementMode::PowerSave)
.await;
let config = Config::Dhcp(Default::default());
//let config = embassy_net::Config::StaticV4(embassy_net::Config {
let config = Config::dhcpv4(Default::default());
//let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
// address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24),
// dns_servers: Vec::new(),
// gateway: Some(Ipv4Address::new(192, 168, 69, 1)),

View File

@ -42,13 +42,13 @@ async fn main_task(spawner: Spawner) {
// Choose between dhcp or static ip
let config = if opts.static_ip {
Config::StaticV4(embassy_net::StaticConfigV4 {
Config::ipv4_static(embassy_net::StaticConfigV4 {
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24),
dns_servers: Vec::new(),
gateway: Some(Ipv4Address::new(192, 168, 69, 1)),
})
} else {
Config::Dhcp(Default::default())
Config::dhcpv4(Default::default())
};
// Generate random seed

View File

@ -40,14 +40,14 @@ async fn main_task(spawner: Spawner) {
// Choose between dhcp or static ip
let config = if opts.static_ip {
Config::StaticV4(embassy_net::StaticConfigV4 {
Config::ipv4_static(embassy_net::StaticConfigV4 {
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 1), 24),
dns_servers: Vec::from_slice(&[Ipv4Address::new(8, 8, 4, 4).into(), Ipv4Address::new(8, 8, 8, 8).into()])
.unwrap(),
gateway: Some(Ipv4Address::new(192, 168, 69, 100)),
})
} else {
Config::Dhcp(Default::default())
Config::dhcpv4(Default::default())
};
// Generate random seed

View File

@ -38,13 +38,13 @@ async fn main_task(spawner: Spawner) {
// Choose between dhcp or static ip
let config = if opts.static_ip {
Config::StaticV4(embassy_net::StaticConfigV4 {
Config::ipv4_static(embassy_net::StaticConfigV4 {
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24),
dns_servers: Vec::new(),
gateway: Some(Ipv4Address::new(192, 168, 69, 1)),
})
} else {
Config::Dhcp(Default::default())
Config::dhcpv4(Default::default())
};
// Generate random seed

View File

@ -53,13 +53,13 @@ async fn main_task(spawner: Spawner) {
// Choose between dhcp or static ip
let config = if opts.static_ip {
Config::StaticV4(embassy_net::StaticConfigV4 {
Config::ipv4_static(embassy_net::StaticConfigV4 {
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24),
dns_servers: Vec::new(),
gateway: Some(Ipv4Address::new(192, 168, 69, 1)),
})
} else {
Config::Dhcp(Default::default())
Config::dhcpv4(Default::default())
};
// Generate random seed

View File

@ -94,8 +94,8 @@ async fn main(spawner: Spawner) {
let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(make_static!(NetState::new()), our_mac_addr);
unwrap!(spawner.spawn(usb_ncm_task(runner)));
let config = embassy_net::Config::Dhcp(Default::default());
//let config = embassy_net::Config::StaticV4(embassy_net::StaticConfigV4 {
let config = embassy_net::Config::dhcpv4(Default::default());
//let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
// dns_servers: Vec::new(),
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),

View File

@ -62,8 +62,8 @@ async fn main(spawner: Spawner) -> ! {
0,
);
let config = embassy_net::Config::Dhcp(Default::default());
//let config = embassy_net::Config::StaticV4(embassy_net::StaticConfigV4 {
let config = embassy_net::Config::dhcpv4(Default::default());
//let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
// dns_servers: Vec::new(),
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),

View File

@ -81,8 +81,8 @@ async fn main(spawner: Spawner) -> ! {
0,
);
let config = embassy_net::Config::Dhcp(Default::default());
//let config = embassy_net::Config::StaticV4(embassy_net::StaticConfigV4 {
let config = embassy_net::Config::dhcpv4(Default::default());
//let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
// dns_servers: Vec::new(),
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),

View File

@ -63,8 +63,8 @@ async fn main(spawner: Spawner) -> ! {
0,
);
let config = embassy_net::Config::Dhcp(Default::default());
//let config = embassy_net::Config::StaticV4(embassy_net::StaticConfigV4 {
let config = embassy_net::Config::dhcpv4(Default::default());
//let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
// dns_servers: Vec::new(),
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),

View File

@ -64,8 +64,8 @@ async fn main(spawner: Spawner) -> ! {
0,
);
let config = embassy_net::Config::Dhcp(Default::default());
//let config = embassy_net::Config::StaticV4(embassy_net::StaticConfigV4 {
let config = embassy_net::Config::dhcpv4(Default::default());
//let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
// dns_servers: Vec::new(),
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),

View File

@ -91,8 +91,8 @@ async fn main(spawner: Spawner) {
let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(make_static!(NetState::new()), our_mac_addr);
unwrap!(spawner.spawn(usb_ncm_task(runner)));
let config = embassy_net::Config::Dhcp(Default::default());
//let config = embassy_net::Config::StaticV4(embassy_net::StaticConfigV4 {
let config = embassy_net::Config::dhcpv4(Default::default());
//let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
// dns_servers: Vec::new(),
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),