From 1fe59d27dc1fa4c55ba2e89ca5a52c2523079141 Mon Sep 17 00:00:00 2001 From: Michael Birtwell Date: Tue, 12 Apr 2022 11:39:47 +0100 Subject: [PATCH] Use an Ipv4AddrConfig enum instead of the USE_DHCP constant --- artiq/firmware/bootloader/main.rs | 7 +++- artiq/firmware/libboard_misoc/net_settings.rs | 40 ++++++++++++++----- artiq/firmware/runtime/main.rs | 8 ++-- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/artiq/firmware/bootloader/main.rs b/artiq/firmware/bootloader/main.rs index 535c6234d..c496a79b7 100644 --- a/artiq/firmware/bootloader/main.rs +++ b/artiq/firmware/bootloader/main.rs @@ -19,7 +19,7 @@ use board_misoc::{clock, ethmac, net_settings}; use board_misoc::uart_console::Console; use riscv::register::{mcause, mepc, mtval}; use smoltcp::iface::SocketStorage; -use smoltcp::wire::HardwareAddress; +use smoltcp::wire::{HardwareAddress, IpAddress, Ipv4Address}; fn check_integrity() -> bool { extern { @@ -410,10 +410,13 @@ fn network_boot() { let net_addresses = net_settings::get_adresses(); println!("Network addresses: {}", net_addresses); 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) ]; + 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 { Some(addr) => { ip_addrs[2] = IpCidr::new(addr, 0); diff --git a/artiq/firmware/libboard_misoc/net_settings.rs b/artiq/firmware/libboard_misoc/net_settings.rs index 4c70dece6..466e1a893 100644 --- a/artiq/firmware/libboard_misoc/net_settings.rs +++ b/artiq/firmware/libboard_misoc/net_settings.rs @@ -1,4 +1,6 @@ use core::fmt; +use core::fmt::{Display, Formatter}; +use core::str::FromStr; use smoltcp::wire::{EthernetAddress, IpAddress, Ipv4Address}; @@ -7,12 +9,36 @@ use config; 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 { + 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 hardware_addr: EthernetAddress, - pub ipv4_addr: IpAddress, + pub ipv4_addr: Ipv4AddrConfig, pub ipv6_ll_addr: IpAddress, pub ipv6_addr: Option } @@ -52,15 +78,9 @@ pub fn get_adresses() -> NetAddresses { } let ipv4_addr; - match config::read_str("ip", |r| r.map(|s| { - if s == "use_dhcp" { - Ok(USE_DHCP) - } else { - s.parse() - } - })) { + match config::read_str("ip", |r| r.map(|s| s.parse())) { Ok(Ok(addr)) => ipv4_addr = addr, - _ => ipv4_addr = USE_DHCP, + _ => ipv4_addr = Ipv4AddrConfig::UseDhcp, } let ipv6_ll_addr = IpAddress::v6( diff --git a/artiq/firmware/runtime/main.rs b/artiq/firmware/runtime/main.rs index ab731a6b9..01cf89f5c 100644 --- a/artiq/firmware/runtime/main.rs +++ b/artiq/firmware/runtime/main.rs @@ -32,7 +32,7 @@ use smoltcp::wire::{HardwareAddress, IpAddress, IpCidr, Ipv4Address}; use board_misoc::{csr, ident, clock, spiflash, config, net_settings, pmp, boot}; #[cfg(has_ethmac)] use board_misoc::ethmac; -use board_misoc::net_settings::{NetAddresses, USE_DHCP}; +use board_misoc::net_settings::{NetAddresses, Ipv4AddrConfig}; #[cfg(has_drtio)] use board_artiq::drtioaux; use board_artiq::drtio_routing; @@ -100,7 +100,9 @@ pub fn get_ip_addrs(net_addresses: &NetAddresses) -> [IpCidr; IP_ADDRESS_STORAGE let mut storage = [ 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); if let Some(ipv6) = net_addresses.ipv6_addr { storage[IPV6_INDEX] = IpCidr::new(ipv6, 0); @@ -162,7 +164,7 @@ fn startup() { smoltcp::iface::NeighborCache::new(alloc::collections::btree_map::BTreeMap::new()); let net_addresses = net_settings::get_adresses(); 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"); true } else {