Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCP exponential backoff fix #1020

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions src/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,10 @@ impl Timer {
}
}
Timer::Retransmit { expires_at, delay } if timestamp >= expires_at => {
let delay = delay * 2;
*self = Timer::Retransmit {
expires_at: timestamp + delay,
delay: delay * 2,
delay,
}
}
Timer::Retransmit { .. } => (),
Expand Down Expand Up @@ -2285,11 +2286,9 @@ impl<'a> Socket<'a> {
// to be sent again.
self.remote_last_seq = self.local_seq_no;

// Clear the `should_retransmit` state. If we can't retransmit right
// now for whatever reason (like zero window), this avoids an
// infinite polling loop where `poll_at` returns `Now` but `dispatch`
// can't actually do anything.
self.timer.set_for_idle(cx.now(), self.keep_alive);
// Set next time of retransmission. `set_for_retransmit` will exponentially increase it.
self.timer
.set_for_retransmit(cx.now(), self.rtte.retransmission_timeout());
Copy link
Member

@Dirbaio Dirbaio Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure if restarting the retransmit timer here is 100% correct, because it's not yet guaranteed we're going to send a packet. Actually transmitting is done further below, which might fail because there's not enough buffers in the device for example.

i've opened #1023 which does this a slightly different way: it still does set_for_idle here so we only restart the retransmit timer if we did actually send a retransmission packet (because it's done after sending), and keeps the delay variable outside the timer (in the RTTE struct) so it doesn't get cleared.

i've included your test from this PR in it.


// Inform RTTE, so that it can avoid bogus measurements.
self.rtte.on_retransmit();
Expand Down Expand Up @@ -6355,6 +6354,39 @@ mod test {
recv_nothing!(s);
}

#[test]
fn test_retransmit_exponential_backoff() {
let mut s = socket_established();
s.send_slice(b"abcdef").unwrap();
recv!(s, time 0, Ok(TcpRepr {
seq_number: LOCAL_SEQ + 1,
ack_number: Some(REMOTE_SEQ + 1),
payload: &b"abcdef"[..],
..RECV_TEMPL
}));

let expected_retransmission_instant = s.rtte.retransmission_timeout().millis() as i64;

recv_nothing!(s, time expected_retransmission_instant - 1);
recv!(s, time expected_retransmission_instant, Ok(TcpRepr {
seq_number: LOCAL_SEQ + 1,
ack_number: Some(REMOTE_SEQ + 1),
payload: &b"abcdef"[..],
..RECV_TEMPL
}));

// "current time" is expected_retransmission_instant, and we want to wait 2 * retransmission timeout
let expected_retransmission_instant = 3 * expected_retransmission_instant;

recv_nothing!(s, time expected_retransmission_instant - 1);
recv!(s, time expected_retransmission_instant, Ok(TcpRepr {
seq_number: LOCAL_SEQ + 1,
ack_number: Some(REMOTE_SEQ + 1),
payload: &b"abcdef"[..],
..RECV_TEMPL
}));
}

// =========================================================================================//
// Tests for window management.
// =========================================================================================//
Expand Down Expand Up @@ -7794,7 +7826,7 @@ mod test {
assert_eq!(r.should_retransmit(Instant::from_millis(1200)), None);
assert_eq!(
r.should_retransmit(Instant::from_millis(1301)),
Some(Duration::from_millis(300))
Some(Duration::from_millis(200))
);
r.set_for_idle(Instant::from_millis(1301), None);
assert_eq!(r.should_retransmit(Instant::from_millis(1350)), None);
Expand Down
Loading