Skip to content

Commit 517222f

Browse files
committed
fix(iface): preserve pending replies when adapters refresh the same MAC
1 parent 10b880f commit 517222f

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ is required. These buffers are absent when the feature is disabled.
266266

267267
Pending responses retry neighbor discovery at the existing one-second rate limit,
268268
expire after five seconds, and are discarded when interface IP or MAC configuration
269-
changes. Resolved neighbors are serviced in arrival order; discovery attempts rotate
269+
changes. Refreshing an unchanged hardware address preserves the queue (as needed
270+
by embassy-net's polling adapter). Resolved neighbors are serviced in arrival order; discovery attempts rotate
270271
among unresolved entries so a silent peer does not monopolize the shared rate limit.
271272
`poll_at`/`poll_delay` include retry and expiry deadlines, including when no sockets
272273
are active. `poll_egress` attempts at most one retained response per call.

‎src/iface/interface/mod.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,13 @@ impl Interface {
341341
);
342342

343343
InterfaceInner::check_hardware_addr(&addr);
344-
self.inner.hardware_addr = addr;
345344
#[cfg(feature = "iface-pending-responses")]
346-
self.pending_responses.clear();
345+
if self.inner.hardware_addr != addr {
346+
// Adapters such as embassy-net refresh this value on every poll.
347+
// Only a change invalidates responses owned by this interface.
348+
self.pending_responses.clear();
349+
}
350+
self.inner.hardware_addr = addr;
347351
}
348352

349353
/// Get the IP addresses of the interface.

‎tests/pending_responses.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,34 @@ fn first_echo_is_owned_until_arp_completes_and_sent_once() {
174174
assert_eq!(iface.poll_at(Instant::from_millis(1002), &sockets), None);
175175
}
176176

177+
#[test]
178+
fn unchanged_hardware_address_preserves_response_across_embassy_style_polls() {
179+
let (mut device, mut iface, mut sockets) = setup();
180+
device.rx.push_back(echo(1));
181+
iface.set_hardware_addr(DEVICE_MAC.into());
182+
poll(&mut iface, &mut device, &mut sockets, 1);
183+
assert_arp(&device.tx.remove(0), HOST_IP);
184+
// embassy-net refreshes the same hardware address before every poll.
185+
for time in [2, 500, 1001] {
186+
iface.set_hardware_addr(DEVICE_MAC.into());
187+
poll(&mut iface, &mut device, &mut sockets, time);
188+
}
189+
assert_eq!(
190+
device.tx.len(),
191+
1,
192+
"ARP retry must survive unchanged MAC refreshes"
193+
);
194+
assert_arp(&device.tx.remove(0), HOST_IP);
195+
device.rx.push_back(arp_reply(HOST_IP));
196+
iface.set_hardware_addr(DEVICE_MAC.into());
197+
poll(&mut iface, &mut device, &mut sockets, 1002);
198+
assert_eq!(device.tx.len(), 1);
199+
assert_reply(&device.tx.remove(0), 1, &[1; 512], HOST_IP);
200+
iface.set_hardware_addr(DEVICE_MAC.into());
201+
poll(&mut iface, &mut device, &mut sockets, 1003);
202+
assert!(device.tx.is_empty());
203+
}
204+
177205
#[test]
178206
fn lost_arp_is_retried_without_a_second_echo_or_busy_poll() {
179207
let (mut device, mut iface, mut sockets) = setup();
@@ -310,6 +338,8 @@ fn reconfiguration_discards_old_interface_responses() {
310338
poll(&mut iface, &mut device, &mut sockets, 1);
311339
device.tx.clear();
312340
if hardware_change {
341+
iface.set_hardware_addr(EthernetAddress([2, 0, 0, 0, 0, 3]).into());
342+
// Restore addressing so an uncleared reply could actually dispatch.
313343
iface.set_hardware_addr(DEVICE_MAC.into());
314344
} else {
315345
iface.update_ip_addrs(|_| {});

0 commit comments

Comments
 (0)