fix(net): state that a link's receive error means its device is gone, and test that the network setup then ends at once

This commit is contained in:
Tyler Smith committed 2026-11-12 04:07:33 -05:00
1 parent ce2f2b8102
commit 00adea6663
3 files changed
+37 -1

No files matched your search

+6
View File
@@ -130,6 +130,12 @@ pub trait Link {
/// Queues one frame (14-byte header and payload, at most 1514 bytes).
fn transmit(&mut self, frame: &[u8]) -> Result<(), LinkError>;
/// One frame the adapter received, if any, without blocking.
///
/// A damaged frame (CRC, length, alignment or framing error, a failed
/// bus transfer the adapter recovers from) is dropped and counted, as
/// Linux counts `rx_errors`, and never returned as an error: the stack
/// treats an `Err` as a device that is gone or wedged beyond its own
/// recovery and stops using the link.
fn receive(&mut self) -> Result<Option<Vec<u8>>, LinkError>;
/// Receive one frame into caller-owned reusable storage. The buffer is
/// unchanged if no frame is available or the adapter reports an error.
@@ -50,7 +50,9 @@ pub const DOWNLOAD_BUFFER: usize = 4 << 20;
const SEND_BUFFER: usize = 64 * 1024;
/// The link as smoltcp's device. A link refusal is kept, and the operation
/// polling the interface reports it.
/// polling the interface reports it. Under [`Link::receive`]'s contract a
/// receive error means the device is gone, so the operation ends there
/// rather than polling a dead link to its deadline.
struct Phy<L> {
link: L,
tx_frame: Vec<u8>,
@@ -443,6 +443,34 @@ pub(super) mod tests {
assert_eq!(result, Ok(()));
assert!((29_000_000..30_000_000).contains(&elapsed), "{elapsed}");
}
/// A link with carrier whose device is gone: every receive fails.
struct Gone(alloc::rc::Rc<core::cell::Cell<u32>>);
impl Link for Gone {
fn mac(&self) -> stela_drivers::net::Mac {
stela_drivers::net::Mac([2, 0, 0, 0, 0, 3])
}
fn transmit(&mut self, _: &[u8]) -> Result<(), LinkError> {
Ok(())
}
fn receive(&mut self) -> Result<Option<Vec<u8>>, LinkError> {
self.0.set(self.0.get() + 1);
Err(LinkError::Adapter("device removed".into()))
}
}
#[test]
fn a_receive_error_means_the_device_is_gone_and_ends_the_setup_at_once() {
let receives = alloc::rc::Rc::new(core::cell::Cell::new(0));
let now = alloc::rc::Rc::new(core::cell::Cell::new(0));
let mut stack = Stack::new(Gone(receives.clone()), SharedStep(now.clone()), 7).unwrap();
let result = stack.automatic();
assert_eq!(
result.map(|_| ()),
Err(NetError::Link("device removed".into()))
);
// One receive, not a dead link polled to the DHCP deadline.
assert_eq!(receives.get(), 1);
assert!(now.get() < 1_000_000, "{}", now.get());
}
#[test]
fn addresses_and_servers_set_by_hand_replace_dhcp_and_advertised_dns() {
let router = Router {