fix(net): a lookup waits up to 60 s for the DHCP lease being got back after one was lost and then uses the name's cached address, and each failed lookup is logged with its cached address's family, age and usability, where a lease lost at its renewal failed the lookup at once and ended an online install 25 GB in
This commit is contained in:
1 parent
ed81449da9
commit
e53cd3f6c7
2 files changed
+109
-4
No files matched your search
@@ -398,6 +398,17 @@ impl<L: Link, C: Clock> Stack<L, C> {
|
||||
))
|
||||
}
|
||||
|
||||
/// Whether DHCP is getting a new lease for one lost less than
|
||||
/// [`DHCP_WAIT_US`] ago: IPv4 is expected back, so a lookup waits for
|
||||
/// it rather than failing.
|
||||
fn ipv4_pending(&self, now: u64) -> bool {
|
||||
self.dhcp.is_some()
|
||||
&& self.config.is_none()
|
||||
&& self
|
||||
.lost_at
|
||||
.is_some_and(|at| now.saturating_sub(at) < DHCP_WAIT_US)
|
||||
}
|
||||
|
||||
/// Gives the interface `address`/`prefix` and the default router
|
||||
/// `router` by hand instead of by DHCP. IPv6 autoconfiguration goes on.
|
||||
pub fn set_ipv4(
|
||||
@@ -628,7 +639,10 @@ impl<L: Link, C: Clock> Stack<L, C> {
|
||||
/// An address found in the last [`RESOLVED_US`] is used again; IPv6 is
|
||||
/// asked first when it is ready, then IPv4; and a lookup that fails falls
|
||||
/// back to the last address the name had, so one lost reply does not end
|
||||
/// a long download.
|
||||
/// a long download. While DHCP gets back a lease lost less than
|
||||
/// [`DHCP_WAIT_US`] ago, a failed lookup waits for it and asks again,
|
||||
/// so a lease that lapses at its renewal does not end one either. Each
|
||||
/// failure is noted with what the name's cached address was.
|
||||
fn resolve(&mut self, lookup: &mut Lookup) -> Result<Option<IpAddress>, NetError> {
|
||||
if let Ok(address) = lookup.name.parse() {
|
||||
return Ok(Some(address));
|
||||
@@ -657,14 +671,50 @@ impl<L: Link, C: Clock> Stack<L, C> {
|
||||
Err(error @ NetError::Clock(_)) => return Err(error),
|
||||
Err(error) => error,
|
||||
};
|
||||
match known {
|
||||
Some(index) if self.address_usable(self.resolved[index].1) => {
|
||||
Ok(Some(self.resolved[index].1))
|
||||
let cached = known.map(|index| (self.resolved[index].1, self.resolved[index].2));
|
||||
let usable = cached.is_some_and(|(address, _)| self.address_usable(address));
|
||||
let pending = self.ipv4_pending(now);
|
||||
let line = alloc::format!(
|
||||
"DNS {} failed: {failure}; cached {}, usable={usable} (ipv4 config present={}, \
|
||||
ipv6 global ready={}, lease lost {})",
|
||||
lookup.name,
|
||||
match cached {
|
||||
Some((IpAddress::Ipv4(_), at)) =>
|
||||
alloc::format!("IPv4 address {} s old", now.wrapping_sub(at) / 1_000_000),
|
||||
Some((IpAddress::Ipv6(_), at)) =>
|
||||
alloc::format!("IPv6 address {} s old", now.wrapping_sub(at) / 1_000_000),
|
||||
None => String::from("none"),
|
||||
},
|
||||
self.config.is_some(),
|
||||
self.ipv6_global_ready(),
|
||||
match (self.lost_at, pending) {
|
||||
(Some(at), true) => alloc::format!(
|
||||
"{} s ago, waiting for the next",
|
||||
now.saturating_sub(at) / 1_000_000
|
||||
),
|
||||
(Some(at), false) => alloc::format!("{} s ago", now.saturating_sub(at) / 1_000_000),
|
||||
(None, _) => String::from("never"),
|
||||
},
|
||||
);
|
||||
self.note(line);
|
||||
match cached {
|
||||
Some((address, _)) if usable => Ok(Some(address)),
|
||||
_ if pending => {
|
||||
self.await_lease(lookup);
|
||||
Ok(None)
|
||||
}
|
||||
_ => Err(failure),
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep `lookup` waiting until the lease DHCP is getting back is due:
|
||||
/// [`DHCP_WAIT_US`] after the last was lost.
|
||||
fn await_lease(&self, lookup: &mut Lookup) {
|
||||
if let Some(at) = self.lost_at {
|
||||
lookup.deadline = lookup.deadline.max(at.saturating_add(DHCP_WAIT_US));
|
||||
}
|
||||
}
|
||||
|
||||
fn lookup(&mut self, lookup: &mut Lookup, now: u64) -> Result<Option<IpAddress>, NetError> {
|
||||
let Some((query, kind)) = lookup.query else {
|
||||
// IPv6 first where it reaches the internet; from a unique local
|
||||
@@ -675,6 +725,9 @@ impl<L: Link, C: Clock> Stack<L, C> {
|
||||
DnsQueryType::A
|
||||
} else if !lookup.ipv4_only && self.ipv6_ready() {
|
||||
DnsQueryType::Aaaa
|
||||
} else if self.ipv4_pending(now) {
|
||||
self.await_lease(lookup);
|
||||
return Ok(None);
|
||||
} else if now >= lookup.deadline {
|
||||
return Err(NetError::Dns(alloc::format!(
|
||||
"{}: no reachable address family",
|
||||
|
||||
@@ -911,3 +911,55 @@ fn a_dhcp_lease_and_its_loss_are_noted() {
|
||||
stack.set_ipv4(CLIENT_IP, 24, None).unwrap();
|
||||
assert_eq!(stack.dhcp_summary(), None);
|
||||
}
|
||||
|
||||
/// LOG-3: a lease lost at its renewal no longer fails the lookup of a name
|
||||
/// with a cached IPv4 address: it waits for the next lease and then uses
|
||||
/// the cached address, where it failed at once and ended a 25 GB download.
|
||||
/// A lease that does not come back within the DHCP wait still fails it.
|
||||
#[test]
|
||||
fn a_lookup_waits_for_a_lost_lease_and_then_uses_the_cached_address() {
|
||||
let (mut stack, answering, clock) = dhcpd(true);
|
||||
stack.dhcp().unwrap();
|
||||
let address = IpAddress::Ipv4(SERVER_IP);
|
||||
let now = stack.now().unwrap();
|
||||
stack
|
||||
.resolved
|
||||
.push((String::from("cdn.example"), address, now));
|
||||
answering.set(false);
|
||||
let dhcp = stack.dhcp.unwrap();
|
||||
stack.sockets.get_mut::<dhcpv4::Socket>(dhcp).reset();
|
||||
stack.poll().unwrap();
|
||||
assert!(stack.config().is_none());
|
||||
let mut lookup = Lookup {
|
||||
name: String::from("cdn.example"),
|
||||
port: 80,
|
||||
buffer: 0,
|
||||
raw: false,
|
||||
query: None,
|
||||
asked: 0,
|
||||
ipv4_only: false,
|
||||
deadline: 0,
|
||||
};
|
||||
assert_eq!(stack.resolve(&mut lookup), Ok(None));
|
||||
assert!(lookup.deadline >= clock.0.get() + DHCP_WAIT_US - 1_000_000);
|
||||
answering.set(true);
|
||||
while stack.config().is_none() {
|
||||
stack.poll().unwrap();
|
||||
}
|
||||
assert_eq!(stack.resolve(&mut lookup), Ok(Some(address)));
|
||||
// A lease that stays away past the wait: the lookup fails, and says
|
||||
// what the cached address was.
|
||||
answering.set(false);
|
||||
stack.sockets.get_mut::<dhcpv4::Socket>(dhcp).reset();
|
||||
stack.poll().unwrap();
|
||||
clock.0.set(clock.0.get() + DHCP_WAIT_US);
|
||||
stack.take_notes();
|
||||
assert!(matches!(stack.resolve(&mut lookup), Err(NetError::Dns(_))));
|
||||
let notes = stack.take_notes();
|
||||
assert!(
|
||||
notes.iter().any(|line| line.starts_with("DNS cdn.example failed: ")
|
||||
&& line.contains("cached IPv4 address ")
|
||||
&& line.contains("usable=false (ipv4 config present=false, ipv6 global ready=false, lease lost 60 s ago)")),
|
||||
"{notes:?}"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user