diff --git a/crates/stela-driver-net/src/lib.rs b/crates/stela-driver-net/src/lib.rs index 28200ba395..97714d6fdd 100644 --- a/crates/stela-driver-net/src/lib.rs +++ b/crates/stela-driver-net/src/lib.rs @@ -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>, LinkError>; /// Receive one frame into caller-owned reusable storage. The buffer is /// unchanged if no frame is available or the adapter reports an error. diff --git a/crates/stela-runtime/src/platform/net/stack.rs b/crates/stela-runtime/src/platform/net/stack.rs index 014c0929af..6fa8588c80 100644 --- a/crates/stela-runtime/src/platform/net/stack.rs +++ b/crates/stela-runtime/src/platform/net/stack.rs @@ -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 { link: L, tx_frame: Vec, diff --git a/crates/stela-runtime/src/platform/net/stack/automatic.rs b/crates/stela-runtime/src/platform/net/stack/automatic.rs index 06245e94f9..1bf8e98298 100644 --- a/crates/stela-runtime/src/platform/net/stack/automatic.rs +++ b/crates/stela-runtime/src/platform/net/stack/automatic.rs @@ -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>); + 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>, 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 {