Use an Ipv4AddrConfig enum instead of the USE_DHCP constant

This commit is contained in:
Michael Birtwell 2022-04-12 11:39:47 +01:00 committed by Sebastien Bourdeauducq
parent 73082d116f
commit 1fe59d27dc
3 changed files with 40 additions and 15 deletions

View File

@ -19,7 +19,7 @@ use board_misoc::{clock, ethmac, net_settings};
use board_misoc::uart_console::Console; use board_misoc::uart_console::Console;
use riscv::register::{mcause, mepc, mtval}; use riscv::register::{mcause, mepc, mtval};
use smoltcp::iface::SocketStorage; use smoltcp::iface::SocketStorage;
use smoltcp::wire::HardwareAddress; use smoltcp::wire::{HardwareAddress, IpAddress, Ipv4Address};
fn check_integrity() -> bool { fn check_integrity() -> bool {
extern { extern {
@ -410,10 +410,13 @@ fn network_boot() {
let net_addresses = net_settings::get_adresses(); let net_addresses = net_settings::get_adresses();
println!("Network addresses: {}", net_addresses); println!("Network addresses: {}", net_addresses);
let mut ip_addrs = [ let mut ip_addrs = [
IpCidr::new(net_addresses.ipv4_addr, 0), IpCidr::new(IpAddress::Ipv4(Ipv4Address::UNSPECIFIED), 0),
IpCidr::new(net_addresses.ipv6_ll_addr, 0), IpCidr::new(net_addresses.ipv6_ll_addr, 0),
IpCidr::new(net_addresses.ipv6_ll_addr, 0) IpCidr::new(net_addresses.ipv6_ll_addr, 0)
]; ];
if let net_settings::Ipv4AddrConfig::Static(ipv4) = net_addresses.ipv4_addr {
ip_addrs[0] = IpCidr::new(IpAddress::Ipv4(ipv4), 0);
}
let mut interface = match net_addresses.ipv6_addr { let mut interface = match net_addresses.ipv6_addr {
Some(addr) => { Some(addr) => {
ip_addrs[2] = IpCidr::new(addr, 0); ip_addrs[2] = IpCidr::new(addr, 0);

View File

@ -1,4 +1,6 @@
use core::fmt; use core::fmt;
use core::fmt::{Display, Formatter};
use core::str::FromStr;
use smoltcp::wire::{EthernetAddress, IpAddress, Ipv4Address}; use smoltcp::wire::{EthernetAddress, IpAddress, Ipv4Address};
@ -7,12 +9,36 @@ use config;
use i2c_eeprom; use i2c_eeprom;
pub const USE_DHCP: IpAddress = IpAddress::Ipv4(Ipv4Address::UNSPECIFIED); pub enum Ipv4AddrConfig {
UseDhcp,
Static(Ipv4Address),
}
impl FromStr for Ipv4AddrConfig {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(if s == "use_dhcp" {
Ipv4AddrConfig::UseDhcp
} else {
Ipv4AddrConfig::Static(Ipv4Address::from_str(s)?)
})
}
}
impl Display for Ipv4AddrConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Ipv4AddrConfig::UseDhcp => write!(f, "use_dhcp"),
Ipv4AddrConfig::Static(ipv4) => write!(f, "{}", ipv4)
}
}
}
pub struct NetAddresses { pub struct NetAddresses {
pub hardware_addr: EthernetAddress, pub hardware_addr: EthernetAddress,
pub ipv4_addr: IpAddress, pub ipv4_addr: Ipv4AddrConfig,
pub ipv6_ll_addr: IpAddress, pub ipv6_ll_addr: IpAddress,
pub ipv6_addr: Option<IpAddress> pub ipv6_addr: Option<IpAddress>
} }
@ -52,15 +78,9 @@ pub fn get_adresses() -> NetAddresses {
} }
let ipv4_addr; let ipv4_addr;
match config::read_str("ip", |r| r.map(|s| { match config::read_str("ip", |r| r.map(|s| s.parse())) {
if s == "use_dhcp" {
Ok(USE_DHCP)
} else {
s.parse()
}
})) {
Ok(Ok(addr)) => ipv4_addr = addr, Ok(Ok(addr)) => ipv4_addr = addr,
_ => ipv4_addr = USE_DHCP, _ => ipv4_addr = Ipv4AddrConfig::UseDhcp,
} }
let ipv6_ll_addr = IpAddress::v6( let ipv6_ll_addr = IpAddress::v6(

View File

@ -32,7 +32,7 @@ use smoltcp::wire::{HardwareAddress, IpAddress, IpCidr, Ipv4Address};
use board_misoc::{csr, ident, clock, spiflash, config, net_settings, pmp, boot}; use board_misoc::{csr, ident, clock, spiflash, config, net_settings, pmp, boot};
#[cfg(has_ethmac)] #[cfg(has_ethmac)]
use board_misoc::ethmac; use board_misoc::ethmac;
use board_misoc::net_settings::{NetAddresses, USE_DHCP}; use board_misoc::net_settings::{NetAddresses, Ipv4AddrConfig};
#[cfg(has_drtio)] #[cfg(has_drtio)]
use board_artiq::drtioaux; use board_artiq::drtioaux;
use board_artiq::drtio_routing; use board_artiq::drtio_routing;
@ -100,7 +100,9 @@ pub fn get_ip_addrs(net_addresses: &NetAddresses) -> [IpCidr; IP_ADDRESS_STORAGE
let mut storage = [ let mut storage = [
IpCidr::new(IpAddress::Ipv4(Ipv4Address::UNSPECIFIED), 0); IP_ADDRESS_STORAGE_SIZE IpCidr::new(IpAddress::Ipv4(Ipv4Address::UNSPECIFIED), 0); IP_ADDRESS_STORAGE_SIZE
]; ];
storage[IPV4_INDEX] = IpCidr::new(net_addresses.ipv4_addr, 0); if let Ipv4AddrConfig::Static(ipv4) = net_addresses.ipv4_addr {
storage[IPV4_INDEX] = IpCidr::new(IpAddress::Ipv4(ipv4), 0);
}
storage[IPV6_LL_INDEX] = IpCidr::new(net_addresses.ipv6_ll_addr, 0); storage[IPV6_LL_INDEX] = IpCidr::new(net_addresses.ipv6_ll_addr, 0);
if let Some(ipv6) = net_addresses.ipv6_addr { if let Some(ipv6) = net_addresses.ipv6_addr {
storage[IPV6_INDEX] = IpCidr::new(ipv6, 0); storage[IPV6_INDEX] = IpCidr::new(ipv6, 0);
@ -162,7 +164,7 @@ fn startup() {
smoltcp::iface::NeighborCache::new(alloc::collections::btree_map::BTreeMap::new()); smoltcp::iface::NeighborCache::new(alloc::collections::btree_map::BTreeMap::new());
let net_addresses = net_settings::get_adresses(); let net_addresses = net_settings::get_adresses();
info!("network addresses: {}", net_addresses); info!("network addresses: {}", net_addresses);
let use_dhcp = if net_addresses.ipv4_addr == USE_DHCP { let use_dhcp = if matches!(net_addresses.ipv4_addr, Ipv4AddrConfig::UseDhcp) {
info!("Will try to acquire an IPv4 address with DHCP"); info!("Will try to acquire an IPv4 address with DHCP");
true true
} else { } else {