fix(boot): drive a Wi-Fi adapter's wait for an address past its connection keeper's failures until its carrier wait and then the network stack's DHCP wait, now exported, have passed, as the host check drives its station, where the keeper's first failure, reaching the stack as the link's, ended the wait and gave the adapter up while the keeper's next attempt could still join
This commit is contained in:
1 parent
5cd5570a3e
commit
999c0d361a
4 files changed
+367
-18
No files matched your search
@@ -7,7 +7,19 @@
|
||||
//! `NETWORK adapter … failed` line and the screen's account, a later
|
||||
//! download's and the guest network's. [`hide_ssid`] takes the configured
|
||||
//! name out of them where the boot receives them.
|
||||
use alloc::string::String;
|
||||
//!
|
||||
//! The keeper also fails and tries again on its own: a scan that missed
|
||||
//! the network, a refused handshake, an AP that went. Its failure reaches
|
||||
//! the network stack as the link's, which ends the stack's wait for an
|
||||
//! address, often while the keeper's next attempt is joining. So a Wi-Fi
|
||||
//! link's wait is driven again past each failure another attempt may mend
|
||||
//! ([`address`]), as the host check's station check drives it (M3 plan
|
||||
//! Task 20), until the link's carrier wait and then the stack's DHCP wait
|
||||
//! have passed since it began ([`Cut`]). A wired link's wait ends at its
|
||||
//! first failure, as it always has.
|
||||
use alloc::string::{String, ToString};
|
||||
use core::{cell::Cell, fmt};
|
||||
use stela_runtime::net::{NetError, stack::DHCP_WAIT_US};
|
||||
|
||||
/// What a hidden network name reads as.
|
||||
const HIDDEN: &str = "<ssid>";
|
||||
@@ -62,5 +74,157 @@ pub fn hide_ssid(text: &str, ssid: &[u8]) -> String {
|
||||
hidden
|
||||
}
|
||||
|
||||
/// What a Wi-Fi link's wait gives when it was cut before any failure: its
|
||||
/// one attempt, with carrier, did not get a lease in the time left.
|
||||
pub const NO_LEASE_IN_TIME: &str =
|
||||
"no DHCPv4 lease or usable IPv6 router advertisement within the Wi-Fi link's wait";
|
||||
|
||||
/// When a host adapter's wait for an address ends, read by the network
|
||||
/// stack's clock: a Wi-Fi adapter's [`Self::wait_us`] after the stack's
|
||||
/// first read of it; a wired adapter's never (its wait ends at its first
|
||||
/// failure, [`address`]). A read at or past the end fails the clock, which
|
||||
/// ends the stack's wait (the stack then refuses every operation, so the
|
||||
/// adapter is given up as on any other failure).
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Cut {
|
||||
/// How long the wait lasts; `None` for a wired adapter, and once the
|
||||
/// wait is over ([`address`]), so the stack's later reads are never cut.
|
||||
wait_us: Cell<Option<u64>>,
|
||||
/// The end, from the first read on.
|
||||
at_us: Cell<Option<u64>>,
|
||||
reached: Cell<bool>,
|
||||
}
|
||||
|
||||
impl Cut {
|
||||
/// The cut of an adapter whose link may take `carrier_wait_us` to come
|
||||
/// up (`Link::carrier_wait_us`): for a Wi-Fi adapter that and then the
|
||||
/// stack's DHCP wait ([`DHCP_WAIT_US`]), the longest one wait of the
|
||||
/// stack's takes; none for a wired adapter.
|
||||
pub fn new(wireless: bool, carrier_wait_us: u64) -> Self {
|
||||
Self {
|
||||
wait_us: Cell::new(wireless.then(|| carrier_wait_us.saturating_add(DHCP_WAIT_US))),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// How long a Wi-Fi adapter's wait lasts, while it goes on.
|
||||
pub fn wait_us(&self) -> Option<u64> {
|
||||
self.wait_us.get()
|
||||
}
|
||||
|
||||
/// Whether the stack's clock, reading `now_us`, is past the end: the
|
||||
/// first read starts the wait, and once past, every read is.
|
||||
pub fn passed(&self, now_us: u64) -> bool {
|
||||
let Some(wait_us) = self.wait_us.get() else {
|
||||
return false;
|
||||
};
|
||||
let at_us = self
|
||||
.at_us
|
||||
.get()
|
||||
.unwrap_or_else(|| now_us.saturating_add(wait_us));
|
||||
self.at_us.set(Some(at_us));
|
||||
if now_us >= at_us {
|
||||
self.reached.set(true);
|
||||
}
|
||||
self.reached.get()
|
||||
}
|
||||
|
||||
/// Whether a read was past the end.
|
||||
pub fn reached(&self) -> bool {
|
||||
self.reached.get()
|
||||
}
|
||||
}
|
||||
|
||||
/// The failures a Wi-Fi link's wait was driven past: how many, the first
|
||||
/// and the last, and how long the wait was when it was cut.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Retried {
|
||||
count: u32,
|
||||
first: Option<String>,
|
||||
last: Option<String>,
|
||||
cut_us: Option<u64>,
|
||||
}
|
||||
|
||||
impl Retried {
|
||||
fn note(&mut self, failure: String) {
|
||||
self.count = self.count.saturating_add(1);
|
||||
match self.first {
|
||||
None => self.first = Some(failure),
|
||||
Some(_) => self.last = Some(failure),
|
||||
}
|
||||
}
|
||||
|
||||
/// No failure was gone past and the wait was not cut: a wired adapter's
|
||||
/// always.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.count == 0 && self.cut_us.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
/// Nothing when [`Retried::is_empty`]; else `retried past 1 failure:
|
||||
/// <first>`, `retried past <n> failures: <first>` when the last was the
|
||||
/// first's words, or `retried past <n> failures, first: <first>, last:
|
||||
/// <last>`, then `; no address within <s> s` when the wait was cut.
|
||||
impl fmt::Display for Retried {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if let Some(first) = &self.first {
|
||||
let count = self.count;
|
||||
match &self.last {
|
||||
None => write!(f, "retried past 1 failure: {first}")?,
|
||||
Some(last) if last == first => {
|
||||
write!(f, "retried past {count} failures: {first}")?;
|
||||
}
|
||||
Some(last) => {
|
||||
write!(
|
||||
f,
|
||||
"retried past {count} failures, first: {first}, last: {last}"
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(cut_us) = self.cut_us {
|
||||
if self.first.is_some() {
|
||||
f.write_str("; ")?;
|
||||
}
|
||||
write!(f, "no address within {} s", cut_us / 1_000_000)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// A host adapter's wait for an address, `attempt` being the network
|
||||
/// stack's (`Stack::automatic_waiting`) over a clock that [`Cut::passed`]
|
||||
/// cuts. A wired adapter's first failure is its result, as it always was.
|
||||
/// A Wi-Fi adapter's failures that another attempt may mend
|
||||
/// (`NetError::retryable`: the keeper's failures, which reach the stack as
|
||||
/// the link's, a carrier that did not come or went, no lease) are noted
|
||||
/// and the attempt made again; any other failure is the result at once.
|
||||
/// Once the cut ends an attempt, the result is the last failure gone past,
|
||||
/// else [`NO_LEASE_IN_TIME`]. The wait is over on every return, so the
|
||||
/// stack's later reads of the clock are never cut.
|
||||
pub fn address<T>(
|
||||
cut: &Cut,
|
||||
mut attempt: impl FnMut() -> Result<T, NetError>,
|
||||
) -> (Result<T, NetError>, Retried) {
|
||||
let mut retried = Retried::default();
|
||||
let mut last = None;
|
||||
let result = loop {
|
||||
match attempt() {
|
||||
Ok(done) => break Ok(done),
|
||||
Err(NetError::Clock(_)) if cut.reached() => {
|
||||
retried.cut_us = cut.wait_us();
|
||||
break Err(last.unwrap_or(NetError::Dhcp(NO_LEASE_IN_TIME)));
|
||||
}
|
||||
Err(error) if cut.wait_us().is_some() && error.retryable() => {
|
||||
retried.note(error.to_string());
|
||||
last = Some(error);
|
||||
}
|
||||
Err(error) => break Err(error),
|
||||
}
|
||||
};
|
||||
cut.wait_us.set(None);
|
||||
(result, retried)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -1,6 +1,162 @@
|
||||
use super::*;
|
||||
use alloc::string::ToString;
|
||||
use stela_runtime::net::NetError;
|
||||
use alloc::{string::ToString, vec, vec::Vec};
|
||||
use core::cell::{Cell, RefCell};
|
||||
use stela_drivers::net::wifi::realtek::ax::WIFI_CARRIER_WAIT_US;
|
||||
use stela_runtime::net::{ClockError, NetError, stack::DHCP_WAIT_US};
|
||||
|
||||
/// A keeper's failure as the network stack reports it.
|
||||
fn not_found() -> NetError {
|
||||
NetError::Link(String::from("rtw89: <ssid> not found"))
|
||||
}
|
||||
|
||||
/// `address` over `results`, one per attempt, in order: what it gave, what
|
||||
/// it said of the failures it went past, and how many attempts it made.
|
||||
fn drive(cut: &Cut, results: Vec<Result<u32, NetError>>) -> (Result<u32, NetError>, String, usize) {
|
||||
let results = RefCell::new(results.into_iter());
|
||||
let attempts = Cell::new(0);
|
||||
let (result, retried) = address(cut, || {
|
||||
attempts.set(attempts.get() + 1);
|
||||
results
|
||||
.borrow_mut()
|
||||
.next()
|
||||
.expect("an attempt past the last result")
|
||||
});
|
||||
(result, retried.to_string(), attempts.get())
|
||||
}
|
||||
|
||||
/// A wired adapter's wait ends at its first failure, whatever it is, as it
|
||||
/// did before a Wi-Fi link's went on, and its clock is never cut.
|
||||
#[test]
|
||||
fn a_wired_link_stops_at_its_first_failure() {
|
||||
for failure in [
|
||||
not_found(),
|
||||
NetError::NoCarrier,
|
||||
NetError::Dhcp("no DHCPv4 lease"),
|
||||
NetError::DuplicateAddress(core::net::Ipv6Addr::LOCALHOST),
|
||||
] {
|
||||
let cut = Cut::new(false, WIFI_CARRIER_WAIT_US);
|
||||
let (result, retried, attempts) = drive(&cut, vec![Err(failure.clone()), Ok(1)]);
|
||||
assert_eq!(result, Err(failure));
|
||||
assert_eq!((retried.as_str(), attempts), ("", 1));
|
||||
assert!(!cut.passed(0) && !cut.passed(u64::MAX) && !cut.reached());
|
||||
}
|
||||
let cut = Cut::new(false, WIFI_CARRIER_WAIT_US);
|
||||
assert_eq!(drive(&cut, vec![Ok(7)]), (Ok(7), String::new(), 1));
|
||||
}
|
||||
|
||||
/// A Wi-Fi link's keeper fails and tries again on its own, and its failure
|
||||
/// reaches the stack as the link's: the wait goes on past it.
|
||||
#[test]
|
||||
fn a_wifi_link_is_driven_past_its_keepers_failures() {
|
||||
let cut = Cut::new(true, WIFI_CARRIER_WAIT_US);
|
||||
let (result, retried, attempts) = drive(&cut, vec![Err(not_found()), Ok(7)]);
|
||||
assert_eq!((result, attempts), (Ok(7), 2));
|
||||
assert_eq!(
|
||||
retried,
|
||||
"retried past 1 failure: network link: rtw89: <ssid> not found"
|
||||
);
|
||||
// The wait is over: the stack's later reads, a download's, are never
|
||||
// cut.
|
||||
assert!(!cut.passed(u64::MAX) && !cut.reached());
|
||||
|
||||
let cut = Cut::new(true, WIFI_CARRIER_WAIT_US);
|
||||
let (result, retried, attempts) = drive(
|
||||
&cut,
|
||||
vec![
|
||||
Err(not_found()),
|
||||
Err(NetError::NoCarrier),
|
||||
Err(NetError::Dhcp("no lease")),
|
||||
Ok(7),
|
||||
],
|
||||
);
|
||||
assert_eq!((result, attempts), (Ok(7), 4));
|
||||
assert_eq!(
|
||||
retried,
|
||||
"retried past 3 failures, first: network link: rtw89: <ssid> not found, last: DHCP: \
|
||||
no lease"
|
||||
);
|
||||
let cut = Cut::new(true, WIFI_CARRIER_WAIT_US);
|
||||
let (_, retried, _) = drive(&cut, vec![Err(not_found()), Err(not_found()), Ok(7)]);
|
||||
assert_eq!(
|
||||
retried,
|
||||
"retried past 2 failures: network link: rtw89: <ssid> not found"
|
||||
);
|
||||
}
|
||||
|
||||
/// A failure no other attempt mends ends a Wi-Fi link's wait at once, as a
|
||||
/// wired one's, and so does a clock that failed before the cut.
|
||||
#[test]
|
||||
fn a_wifi_link_stops_at_a_failure_no_attempt_mends() {
|
||||
for failure in [
|
||||
NetError::DuplicateAddress(core::net::Ipv6Addr::LOCALHOST),
|
||||
NetError::Invalid("DHCP after a configuration by hand"),
|
||||
NetError::Clock(ClockError::Unavailable),
|
||||
] {
|
||||
let cut = Cut::new(true, WIFI_CARRIER_WAIT_US);
|
||||
let (result, retried, attempts) = drive(&cut, vec![Err(not_found()), Err(failure.clone())]);
|
||||
assert_eq!((result, attempts), (Err(failure), 2));
|
||||
assert_eq!(
|
||||
retried,
|
||||
"retried past 1 failure: network link: rtw89: <ssid> not found"
|
||||
);
|
||||
assert!(!cut.passed(u64::MAX));
|
||||
}
|
||||
}
|
||||
|
||||
/// A Wi-Fi link's wait lasts its carrier wait and then the DHCP wait, the
|
||||
/// stack's, from the stack's first read of the clock; a read at or past
|
||||
/// that fails from then on.
|
||||
#[test]
|
||||
fn a_wifi_links_wait_is_its_carrier_wait_and_the_dhcp_wait() {
|
||||
assert_eq!(DHCP_WAIT_US, 60_000_000);
|
||||
let cut = Cut::new(true, WIFI_CARRIER_WAIT_US);
|
||||
let start = 5_000_000;
|
||||
let end = start + WIFI_CARRIER_WAIT_US + DHCP_WAIT_US;
|
||||
assert!(!cut.passed(start));
|
||||
assert!(!cut.passed(end - 1));
|
||||
assert!(!cut.reached());
|
||||
assert!(cut.passed(end));
|
||||
assert!(cut.reached());
|
||||
// Once cut, every read is: the stack refuses from then on.
|
||||
assert!(cut.passed(start));
|
||||
}
|
||||
|
||||
/// The stack's clock cut drives a Wi-Fi link's wait to its end: the
|
||||
/// failure is the last one gone past, so a keeper that never joined still
|
||||
/// reads as no carrier.
|
||||
#[test]
|
||||
fn a_wifi_links_wait_ends_at_its_cut() {
|
||||
let cut = Cut::new(true, WIFI_CARRIER_WAIT_US);
|
||||
let now = Cell::new(1_000);
|
||||
let attempts = Cell::new(0);
|
||||
// Each attempt reads the clock as it starts, then waits out the carrier
|
||||
// wait without carrier.
|
||||
let (result, retried) = address(&cut, || {
|
||||
attempts.set(attempts.get() + 1);
|
||||
if cut.passed(now.get()) {
|
||||
return Err::<u32, _>(NetError::Clock(ClockError::Unavailable));
|
||||
}
|
||||
now.set(now.get() + WIFI_CARRIER_WAIT_US);
|
||||
Err(NetError::NoCarrier)
|
||||
});
|
||||
assert_eq!((result, attempts.get()), (Err(NetError::NoCarrier), 4));
|
||||
assert_eq!(
|
||||
retried.to_string(),
|
||||
"retried past 3 failures: network link has no carrier; no address within 90 s"
|
||||
);
|
||||
assert!(!cut.passed(u64::MAX));
|
||||
|
||||
// Cut inside its first attempt, with no failure gone past: a lease that
|
||||
// did not come.
|
||||
let cut = Cut::new(true, WIFI_CARRIER_WAIT_US);
|
||||
assert!(!cut.passed(0));
|
||||
assert!(cut.passed(WIFI_CARRIER_WAIT_US + DHCP_WAIT_US));
|
||||
let (result, retried, attempts) =
|
||||
drive(&cut, vec![Err(NetError::Clock(ClockError::Unavailable))]);
|
||||
assert_eq!(attempts, 1);
|
||||
assert_eq!(result, Err(NetError::Dhcp(NO_LEASE_IN_TIME)));
|
||||
assert_eq!(retried, "no address within 90 s");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hide_ssid_hides_every_occurrence_of_the_name() {
|
||||
|
||||
@@ -23,6 +23,7 @@ use core::{
|
||||
cell::{Cell, RefCell},
|
||||
mem::ManuallyDrop,
|
||||
};
|
||||
use stela_boot_core::network_link;
|
||||
use stela_contracts::refuse;
|
||||
use stela_drivers::{
|
||||
Clock,
|
||||
@@ -163,7 +164,7 @@ impl RootLink {
|
||||
fn hidden(&self, error: LinkError) -> LinkError {
|
||||
match error {
|
||||
LinkError::Adapter(why) => {
|
||||
LinkError::Adapter(stela_boot_core::network_link::hide_ssid(&why, &self.ssid))
|
||||
LinkError::Adapter(network_link::hide_ssid(&why, &self.ssid))
|
||||
}
|
||||
error => error,
|
||||
}
|
||||
@@ -172,7 +173,7 @@ impl RootLink {
|
||||
/// The adapter's state line (`Link::diagnostic`) with the network's
|
||||
/// name hidden, as in [`Self::hidden`].
|
||||
fn state(&self, state: Option<String>) -> Option<String> {
|
||||
state.map(|state| stela_boot_core::network_link::hide_ssid(&state, &self.ssid))
|
||||
state.map(|state| network_link::hide_ssid(&state, &self.ssid))
|
||||
}
|
||||
|
||||
/// Send `frame` inside the host drivers.
|
||||
@@ -293,21 +294,31 @@ impl Link for RootLink {
|
||||
/// The PC's clock, moving the screen's running step as the stack waits. It
|
||||
/// reads the device clock (`host::device_counter`) on whichever processor
|
||||
/// serves the host, so the stack's time never goes back when its calls move
|
||||
/// between processors.
|
||||
struct Ticking(Clock);
|
||||
/// between processors. A read past `cut` fails, which ends a Wi-Fi
|
||||
/// adapter's wait for an address (`network_link::Cut`); a wired adapter's
|
||||
/// is never cut.
|
||||
struct Ticking {
|
||||
clock: Clock,
|
||||
cut: Rc<network_link::Cut>,
|
||||
}
|
||||
|
||||
impl stela_runtime::net::Clock for Ticking {
|
||||
fn now_us(&self) -> Result<u64, stela_runtime::net::ClockError> {
|
||||
crate::status::tick();
|
||||
self.0
|
||||
let now = self
|
||||
.clock
|
||||
.try_now_us()
|
||||
.map_err(stela_runtime::net::ClockError::Calibration)
|
||||
.map_err(stela_runtime::net::ClockError::Calibration)?;
|
||||
if self.cut.passed(now) {
|
||||
return Err(stela_runtime::net::ClockError::Unavailable);
|
||||
}
|
||||
Ok(now)
|
||||
}
|
||||
/// The adapter's completion signal wakes the boot processor, which runs
|
||||
/// the stack.
|
||||
fn sleep(&self, until_us: u64) -> Result<(), stela_runtime::net::ClockError> {
|
||||
let now = self
|
||||
.0
|
||||
.clock
|
||||
.try_now_us()
|
||||
.map_err(stela_runtime::net::ClockError::Calibration)?;
|
||||
if until_us > now {
|
||||
@@ -326,9 +337,12 @@ pub struct HostNetwork {
|
||||
|
||||
impl HostNetwork {
|
||||
/// Try each native adapter, those with carrier first, until DHCPv4 or
|
||||
/// IPv6 SLAAC provides a usable address. A failed adapter keeps its
|
||||
/// diagnostic while the next is tried, and stays in `links` with every
|
||||
/// adapter not tried, for the guest network ([`GuestNetwork::open`]).
|
||||
/// IPv6 SLAAC provides a usable address. A Wi-Fi adapter's wait goes on
|
||||
/// past its connection keeper's failures until its carrier wait and the
|
||||
/// DHCP wait have passed (`network_link::address`). A failed adapter
|
||||
/// keeps its diagnostic while the next is tried, and stays in `links`
|
||||
/// with every adapter not tried, for the guest network
|
||||
/// ([`GuestNetwork::open`]).
|
||||
pub fn open(
|
||||
links: &mut Links,
|
||||
clock: Clock,
|
||||
@@ -367,7 +381,12 @@ impl HostNetwork {
|
||||
let mut seed = [0u8; 8];
|
||||
crate::random::fill(&mut seed)
|
||||
.map_err(|_| refuse!("secure host entropy is unavailable for the network stack"))?;
|
||||
let mut stack = Stack::new(link, Ticking(clock), u64::from_le_bytes(seed))
|
||||
let cut = Rc::new(network_link::Cut::new(wireless, link.carrier_wait_us()));
|
||||
let ticking = Ticking {
|
||||
clock,
|
||||
cut: Rc::clone(&cut),
|
||||
};
|
||||
let mut stack = Stack::new(link, ticking, u64::from_le_bytes(seed))
|
||||
.map_err(|error| refuse!("host network stack: {error}", %error))?;
|
||||
if let Some(ipv4) = options.ipv4 {
|
||||
stack
|
||||
@@ -379,12 +398,21 @@ impl HostNetwork {
|
||||
.set_dns(&options.dns)
|
||||
.map_err(|error| refuse!("host network --dns: {error}", %error))?;
|
||||
}
|
||||
let waiting = |seconds: u64| {
|
||||
let mut waiting = |seconds: u64| {
|
||||
crate::status::progress(&format!(
|
||||
"adapter {location}: waiting for the link, {seconds} s"
|
||||
));
|
||||
};
|
||||
match stack.automatic_waiting(waiting) {
|
||||
// A Wi-Fi adapter's keeper fails and tries again on its own,
|
||||
// and its failure reaches the stack as the link's: the wait is
|
||||
// driven past it until its cut. A wired adapter's ends at its
|
||||
// first failure.
|
||||
let (addressed, retried) =
|
||||
network_link::address(&cut, || stack.automatic_waiting(&mut waiting));
|
||||
if !retried.is_empty() {
|
||||
output::arguments(format_args!("NETWORK adapter {location} {retried}\n"));
|
||||
}
|
||||
match addressed {
|
||||
Ok(config) => {
|
||||
output::arguments(format_args!("NETWORK driver={driver}\n"));
|
||||
output::arguments(format_args!(
|
||||
|
||||
@@ -36,8 +36,9 @@ pub struct Config {
|
||||
/// the link comes up (IEEE 802.1D: 15 s listening, 15 s learning), and the
|
||||
/// adapter's start has just dropped and raised the link, so a wait of 30
|
||||
/// seconds lost every request there. Other systems' clients ask for a
|
||||
/// minute or longer.
|
||||
const DHCP_WAIT_US: u64 = 60_000_000;
|
||||
/// minute or longer. [`Stack::dhcp`] and [`Stack::automatic`] wait this
|
||||
/// long once the link has carrier.
|
||||
pub const DHCP_WAIT_US: u64 = 60_000_000;
|
||||
/// Seconds between DHCPDISCOVERs while no server answered: RFC 2131 4.1's
|
||||
/// first retransmission delay, where smoltcp's default waits 10 s. The
|
||||
/// first DISCOVER goes out as the link comes up, when a switch or the
|
||||
|
||||
Reference in new issue
Block a user