implement tcp.Policy and refactor rto to use it

This commit is contained in:
Patricio Whittingslow
2026-08-24 16:47:06 -03:00
parent 936790a5d0
commit 52a3926428
10 changed files with 961 additions and 427 deletions
+57 -35
View File
@@ -3,6 +3,7 @@ package rto
import (
"time"
"github.com/soypat/lneto"
"github.com/soypat/lneto/tcp"
)
@@ -35,15 +36,14 @@ const (
)
// Timer implements the RFC 6298 round-trip-time estimator and the single
// retransmission timer as a [tcp.Policy]. Construct it with new(Timer) and hand
// it to [tcp.ConnConfig.Policy]; the connection calls [Timer.Reset] on open, so
// the zero value is ready to use.
// retransmission timer as a [tcp.Policy]. Construct it with [NewTimer] and hand
// it to [tcp.ConnConfig.Policy].
//
// Timer is a pure, reactive state machine: it observes the segments a connection
// sends and receives (via the tcp.Policy hooks) and the monotonic time handed
// in at each hook, and from those alone derives RTT estimates and retransmission
// decisions. It holds no clock and allocates nothing, which keeps it
// deterministic for unit testing (see issue #140).
// sends and receives (via the tcp.Policy hooks) and from those alone derives RTT
// estimates and retransmission decisions. The tcp package holds no clock, so the
// Timer carries its own; injecting it keeps the estimator deterministic for unit
// testing (see issue #140).
//
// Timer tracks its own shadow of the send sequence space purely from the segments
// it observes: [Timer.PostTx] advances the highest sequence sent and [Timer.PreRx]
@@ -53,6 +53,9 @@ const (
// whose sequence space is not beyond the shadow snd.NXT is a retransmission and
// is never RTT-sampled.
type Timer struct {
// nanotime is the monotonic time source in nanoseconds. Preserved by Reset.
nanotime func() int64
srtt time.Duration // smoothed round-trip time (SRTT).
rttvar time.Duration // round-trip-time variation (RTTVAR).
rto time.Duration // current retransmission timeout.
@@ -76,17 +79,28 @@ type Timer struct {
// expirations counts timeouts since Reset. It exists so a policy sharing this
// timer can notice a timeout it did not itself drive: a congestion controller
// must collapse its window on one, and when the timer is a peer in a
// [tcp.Composite] the controller never sees the timer's directive.
// must collapse its window on one, and a policy that composes the timer as a
// peer never sees the timer's own directive.
expirations uint32
}
var _ tcp.Policy = (*Timer)(nil)
// Reset returns the estimator to its pre-connection state with the initial RTO.
// It implements [tcp.Policy] and is called when the connection opens or aborts
// so the estimator can be reused across connection reuse.
func (r *Timer) Reset() { *r = Timer{rto: rtoInitial} }
// Configure prepares the Timer for use with nanotime, the monotonic time source
// in nanoseconds (the func() int64 convention used across lneto). It must be
// called before the connection is opened.
func (r *Timer) Configure(nanotime func() int64) error {
if nanotime == nil {
return lneto.ErrMissingHALConfig // The estimator cannot run without a clock.
}
*r = Timer{rto: rtoInitial, nanotime: nanotime}
return nil
}
// Reset returns the estimator to its pre-connection state with the initial RTO,
// preserving the configured clock. It implements [tcp.Policy] and is called when
// the connection opens or aborts so the estimator survives connection reuse.
func (r *Timer) Reset() { *r = Timer{rto: rtoInitial, nanotime: r.nanotime} }
// SmoothedRTT returns the current smoothed round-trip time (SRTT), or zero
// before the first RTT measurement. It is concrete-type introspection and is
@@ -115,7 +129,9 @@ func (r *Timer) Running() bool { return r.running }
func (r *Timer) Expirations() uint32 { return r.expirations }
// NextDeadline returns the monotonic-nanosecond instant at which the timer
// expires, or 0 when it is not armed. It implements [tcp.Policy].
// expires, or 0 when it is not armed. It is concrete-type introspection, not
// part of [tcp.Policy]: an event loop that wants to schedule against the RTO
// holds the Timer it configured and reads this.
func (r *Timer) NextDeadline() int64 {
if !r.running {
return 0
@@ -126,19 +142,22 @@ func (r *Timer) NextDeadline() int64 {
// PreRx keeps every segment: the estimator never drops traffic and records
// nothing before the connection has decided whether the segment counts. It
// implements [tcp.Policy].
func (r *Timer) PreRx(rx tcp.RxMeta) tcp.RxDirective {
return tcp.RxDirective{Keep: true}
func (r *Timer) PreRx(h *tcp.Handler, incoming tcp.Frame) bool {
return true
}
// PostRx samples the RTT and manages the retransmission timer from a segment the
// connection accepted (RFC 6298 §5.2/§5.3). It implements [tcp.Policy].
//
// A refused segment is ignored. Acting on one would let an acknowledgement the
// state machine rejected, for data never sent, collapse the backoff and take a
// bogus RTT sample.
func (r *Timer) PostRx(event tcp.RxEvent) {
incoming, now := event.Segment, event.Now
if !event.Accepted || !r.haveSeq || !incoming.Flags.HasAny(tcp.FlagACK) {
// Only accepted segments reach here. Acting on a refused one would let an
// acknowledgement the state machine rejected, for data never sent, collapse the
// backoff and take a bogus RTT sample.
func (r *Timer) PostRx(h *tcp.Handler, prevState tcp.State, accepted tcp.Frame) {
r.postRx(accepted.Segment(len(accepted.Payload())), r.nanotime())
}
func (r *Timer) postRx(incoming tcp.Segment, now int64) {
if !r.haveSeq || !incoming.Flags.HasAny(tcp.FlagACK) {
return
}
ack := incoming.ACK
@@ -162,19 +181,18 @@ func (r *Timer) PostRx(event tcp.RxEvent) {
}
}
// WriteOptions adds no TCP options: retransmission timing needs none of its
// own. It implements [tcp.Policy].
func (r *Timer) WriteOptions(plan tcp.TxPlan, opts []byte) uint8 { return 0 }
// PreTx reports whether the retransmission timer has expired and, if so, applies
// the RFC 6298 §5.4–§5.6 timeout response — discard the outstanding RTT sample
// (Karn), back the RTO off exponentially and restart the timer — returning a
// directive that asks the connection to retransmit from snd.UNA (go-back-N). It
// implements [tcp.Policy].
func (r *Timer) PreTx(intent tcp.TxIntent) tcp.TxDirective {
now := intent.Now
// (Karn), back the RTO off exponentially and restart the timer — and asks the
// connection to retransmit from snd.UNA (go-back-N). It writes no TCP options:
// retransmission timing needs none of its own. It implements [tcp.Policy].
func (r *Timer) PreTx(h *tcp.Handler, outgoingOpts tcp.Frame) (rtxFrom tcp.Value, retransmit, holdNew bool) {
return r.preTx(r.nanotime(), h.ControlBlock().SendUNA())
}
func (r *Timer) preTx(now int64, una tcp.Value) (rtxFrom tcp.Value, retransmit, holdNew bool) {
if !r.running || now < r.deadline || r.sndUNA == r.sndNXT {
return tcp.TxDirective{}
return 0, false, false
}
r.expirations++
r.timing = false // §5.4: do not sample a retransmitted segment.
@@ -184,7 +202,7 @@ func (r *Timer) PreTx(intent tcp.TxIntent) tcp.TxDirective {
}
r.running = true
r.deadline = now + int64(r.CurrentRTO())
return tcp.TxDirective{Retransmit: true, RetransmitFrom: intent.UNA}
return una, true, false
}
// PostTx records an emitted segment: it advances the shadow send sequence,
@@ -192,7 +210,11 @@ func (r *Timer) PreTx(intent tcp.TxIntent) tcp.TxDirective {
// Segments that do not extend the send sequence are retransmissions and are
// never RTT-sampled (Karn's algorithm). Control-only segments (no data) are
// ignored. It implements [tcp.Policy].
func (r *Timer) PostTx(outgoing tcp.Segment, now int64) {
func (r *Timer) PostTx(h *tcp.Handler, outgoing tcp.Frame) {
r.postTx(outgoing.Segment(len(outgoing.Payload())), r.nanotime())
}
func (r *Timer) postTx(outgoing tcp.Segment, now int64) {
if outgoing.DATALEN == 0 {
return // only data segments are timed / arm the RTO.
}
@@ -204,7 +226,7 @@ func (r *Timer) PostTx(outgoing tcp.Segment, now int64) {
r.sndNXT = segStart
}
if !r.sndNXT.LessThan(segEnd) {
// tcp.Segment does not extend the send sequence: it is a retransmission.
// Segment does not extend the send sequence: it is a retransmission.
// Discard any outstanding RTT sample per Karn's algorithm. The timer was
// already (re)armed by PreTx on the timeout that triggered this resend.
r.timing = false
+98 -73
View File
@@ -21,25 +21,42 @@ func ackSeg(ack uint32) tcp.Segment {
func newRTO() *Timer {
var r Timer
r.Reset()
if err := r.Configure(func() int64 { return 0 }); err != nil {
panic(err)
}
return &r
}
// rxAt builds the minimal tcp.RxMeta for driving PreRx directly.
func rxAt(seg tcp.Segment, now int64) tcp.RxMeta { return tcp.RxMeta{Segment: seg, Now: now} }
// acceptedAt builds the event for a segment the connection accepted, which is what
// drives the estimator. Timing state is only allowed to move for those.
func acceptedAt(seg tcp.Segment, now int64) tcp.RxEvent {
return tcp.RxEvent{Segment: seg, Now: now, Accepted: true}
// frameOf renders a segment as the wire frame the [tcp.Policy] hooks receive.
func frameOf(t *testing.T, s tcp.Segment) tcp.Frame {
t.Helper()
frm, err := tcp.NewFrame(make([]byte, 20+int(s.DATALEN)))
if err != nil {
t.Fatal(err)
}
frm.SetSegment(s, 5)
return frm
}
// txAt builds the minimal tcp.TxIntent for driving Timer.PreTx directly: the timer
// tracks the send sequence itself via PostTx and only reads the clock.
func txAt(now int64) tcp.TxIntent { return tcp.TxIntent{Now: now} }
func TestRTO_Configure(t *testing.T) {
var r Timer
if err := r.Configure(nil); err == nil {
t.Error("Configure must reject a nil clock")
}
if err := r.Configure(func() int64 { return 0 }); err != nil {
t.Fatal(err)
}
if r.nanotime == nil {
t.Fatal("clock not stored")
}
r.Reset()
if r.nanotime == nil {
t.Error("Reset must preserve the configured clock")
}
}
func TestRTO_Reset(t *testing.T) {
var r Timer
r := newRTO()
r.Reset()
if r.rto != rtoInitial {
t.Errorf("initial rto=%v, want %v", r.rto, rtoInitial)
@@ -61,7 +78,7 @@ func TestRTO_ArmOnSendSampleOnAck(t *testing.T) {
r := newRTO()
const iss = uint32(1000)
r.PostTx(dataSeg(iss, 100), 0)
r.postTx(dataSeg(iss, 100), 0)
if !r.Running() {
t.Fatal("timer must arm after sending data")
}
@@ -70,10 +87,10 @@ func TestRTO_ArmOnSendSampleOnAck(t *testing.T) {
}
// ACK arrives one RTT (40ms) later covering all sent data.
if !r.PreRx(rxAt(ackSeg(iss+100), 40*rtoMs)).Keep {
if !r.PreRx(nil, frameOf(t, ackSeg(iss+100))) {
t.Error("PreRx must keep the segment")
}
r.PostRx(acceptedAt(ackSeg(iss+100), 40*rtoMs))
r.postRx(ackSeg(iss+100), 40*rtoMs)
if r.Running() {
t.Error("timer must stop once all data is acknowledged")
}
@@ -87,23 +104,26 @@ func TestRTO_ArmOnSendSampleOnAck(t *testing.T) {
func TestRTO_RetransmitOnTimeout(t *testing.T) {
r := newRTO()
const iss = uint32(1000)
r.PostTx(dataSeg(iss, 100), 0)
r.postTx(dataSeg(iss, 100), 0)
if r.PreTx(txAt(int64(rtoInitial) - 1)).Retransmit {
if _, rtx, _ := r.preTx(int64(rtoInitial)-1, tcp.Value(iss)); rtx {
t.Fatal("must not retransmit before the deadline")
}
dir := r.PreTx(tcp.TxIntent{Now: int64(rtoInitial), UNA: tcp.Value(iss), NXT: tcp.Value(iss + 100)})
if !dir.Retransmit {
from, rtx, hold := r.preTx(int64(rtoInitial), tcp.Value(iss))
if !rtx {
t.Fatal("RTO must fire at the deadline with data outstanding")
}
if dir.RetransmitFrom != tcp.Value(iss) {
t.Errorf("retransmit from %d, want snd.UNA=%d", dir.RetransmitFrom, iss)
if hold {
t.Error("the estimator never holds new data back")
}
if from != tcp.Value(iss) {
t.Errorf("retransmit from %d, want snd.UNA=%d", from, iss)
}
if r.CurrentRTO() != 2*rtoInitial {
t.Errorf("rto=%v after one backoff, want %v", r.CurrentRTO(), 2*rtoInitial)
}
// The connection resends from snd.UNA; PostTx sees a retransmission.
r.PostTx(dataSeg(iss, 100), int64(rtoInitial))
// The connection resends from snd.UNA; postTx sees a retransmission.
r.postTx(dataSeg(iss, 100), int64(rtoInitial))
if r.timing {
t.Error("retransmitted segment must not be RTT-sampled (Karn)")
}
@@ -114,12 +134,12 @@ func TestRTO_RetransmitOnTimeout(t *testing.T) {
func TestRTO_KarnNoSampleOnRetransmittedAck(t *testing.T) {
r := newRTO()
const iss = uint32(1000)
r.PostTx(dataSeg(iss, 100), 0)
r.postTx(dataSeg(iss, 100), 0)
// Timeout and retransmit.
r.PreTx(txAt(int64(rtoInitial)))
r.PostTx(dataSeg(iss, 100), int64(rtoInitial))
r.preTx(int64(rtoInitial), tcp.Value(iss))
r.postTx(dataSeg(iss, 100), int64(rtoInitial))
// ACK now arrives; no sample should be taken since timing was discarded.
r.PostRx(acceptedAt(ackSeg(iss+100), int64(rtoInitial)+10*rtoMs))
r.postRx(ackSeg(iss+100), int64(rtoInitial)+10*rtoMs)
if r.haveRTT {
t.Error("no RTT sample should exist after a retransmission (Karn)")
}
@@ -130,10 +150,10 @@ func TestRTO_KarnNoSampleOnRetransmittedAck(t *testing.T) {
func TestRTO_TimerRestartsWhilePartiallyAcked(t *testing.T) {
r := newRTO()
const iss = uint32(1000)
r.PostTx(dataSeg(iss, 100), 0)
r.PostTx(dataSeg(iss+100, 100), 0) // 200 octets outstanding, iss..iss+200.
r.postTx(dataSeg(iss, 100), 0)
r.postTx(dataSeg(iss+100, 100), 0) // 200 octets outstanding, iss..iss+200.
r.PostRx(acceptedAt(ackSeg(iss+100), 40*rtoMs)) // acks first 100 only.
r.postRx(ackSeg(iss+100), 40*rtoMs) // acks first 100 only.
if !r.Running() {
t.Fatal("timer must remain armed while data is still in flight")
}
@@ -146,7 +166,7 @@ func TestRTO_TimerRestartsWhilePartiallyAcked(t *testing.T) {
// nor start an RTT sample.
func TestRTO_NoArmWithoutData(t *testing.T) {
r := newRTO()
r.PostTx(tcp.Segment{SEQ: 1000, Flags: tcp.FlagACK}, 0) // pure ACK, DATALEN==0.
r.postTx(tcp.Segment{SEQ: 1000, Flags: tcp.FlagACK}, 0) // pure ACK, DATALEN==0.
if r.Running() || r.timing {
t.Error("pure control segment must not arm the timer or start a sample")
}
@@ -157,15 +177,15 @@ func TestRTO_NoArmWithoutData(t *testing.T) {
func TestRTO_BackoffCollapsesOnValidSample(t *testing.T) {
r := newRTO()
const iss = uint32(1000)
r.PostTx(dataSeg(iss, 100), 0)
r.PreTx(txAt(int64(rtoInitial))) // one timeout: backoff=1.
r.PostTx(dataSeg(iss, 100), int64(rtoInitial)) // retransmit (no sample).
r.postTx(dataSeg(iss, 100), 0)
r.preTx(int64(rtoInitial), tcp.Value(iss)) // one timeout: backoff=1.
r.postTx(dataSeg(iss, 100), int64(rtoInitial)) // retransmit (no sample).
if r.backoff != 1 {
t.Fatalf("backoff=%d, want 1 after a timeout", r.backoff)
}
// New data sent and freshly sampled, then acked.
r.PostTx(dataSeg(iss+100, 100), int64(rtoInitial)+rtoMs)
r.PostRx(acceptedAt(ackSeg(iss+200), int64(rtoInitial)+30*rtoMs))
r.postTx(dataSeg(iss+100, 100), int64(rtoInitial)+rtoMs)
r.postRx(ackSeg(iss+200), int64(rtoInitial)+30*rtoMs)
if r.backoff != 0 {
t.Errorf("backoff=%d, want 0 after a valid RTT sample", r.backoff)
}
@@ -173,8 +193,7 @@ func TestRTO_BackoffCollapsesOnValidSample(t *testing.T) {
// TestRTO_Clamped verifies CurrentRTO is clamped to [rtoMin, rtoMax].
func TestRTO_Clamped(t *testing.T) {
var r Timer
r.Reset()
r := newRTO()
r.rto = time.Nanosecond
if got := r.CurrentRTO(); got != rtoMin {
t.Errorf("CurrentRTO=%v, want floor %v", got, rtoMin)
@@ -188,8 +207,7 @@ func TestRTO_Clamped(t *testing.T) {
// TestRTO_UpdateRTTFirstSample verifies the first-measurement initialization of
// SRTT/RTTVAR (RFC 6298 §2.2).
func TestRTO_UpdateRTTFirstSample(t *testing.T) {
var r Timer
r.Reset()
r := newRTO()
r.updateRTT(100 * time.Millisecond)
if r.srtt != 100*time.Millisecond {
t.Errorf("srtt=%v, want 100ms", r.srtt)
@@ -203,53 +221,61 @@ func TestRTO_UpdateRTTFirstSample(t *testing.T) {
}
}
// TestRTO_ImplementsPolicy exercises Timer through the [tcp.Policy]
// interface: sending data arms a deadline and a full ACK disarms it.
func TestRTO_ImplementsPolicy(t *testing.T) {
var lr tcp.Policy = newRTO()
lr.Reset()
lr.PostTx(dataSeg(1000, 100), 0)
if lr.NextDeadline() == 0 {
t.Error("expected an armed deadline after sending data")
// TestRTO_PolicyHooksDeriveFromFrame exercises Timer through the [tcp.Policy]
// hooks, verifying it reads the segment out of the frame it is handed: sending
// data arms a deadline and a full ACK disarms it and yields the RTT sample.
func TestRTO_PolicyHooksDeriveFromFrame(t *testing.T) {
var clock int64
var r Timer
if err := r.Configure(func() int64 { return clock }); err != nil {
t.Fatal(err)
}
if !lr.PreRx(rxAt(ackSeg(1100), 10*rtoMs)).Keep {
var pol tcp.Policy = &r
pol.Reset()
pol.PostTx(nil, frameOf(t, dataSeg(1000, 100)))
if r.NextDeadline() == 0 {
t.Fatal("expected an armed deadline after sending data")
}
clock = 10 * rtoMs
if !pol.PreRx(nil, frameOf(t, ackSeg(1100))) {
t.Error("PreRx must keep")
}
lr.PostRx(acceptedAt(ackSeg(1100), 10*rtoMs))
if lr.NextDeadline() != 0 {
pol.PostRx(nil, tcp.StateEstablished, frameOf(t, ackSeg(1100)))
if r.NextDeadline() != 0 {
t.Error("expected disarmed timer after full ack")
}
if r.SmoothedRTT() != 10*time.Millisecond {
t.Errorf("srtt=%v, want 10ms sampled through the hooks", r.SmoothedRTT())
}
}
// TestRTO_IgnoresRejectedSegment verifies the estimator does not act on a segment
// the connection refused. PreRx runs before the state machine has judged the
// segment, so an acknowledgement for data never sent would otherwise collapse the
// backoff and take a bogus round-trip sample.
func TestRTO_IgnoresRejectedSegment(t *testing.T) {
// TestRTO_PreRxNeverDrops verifies the estimator keeps every segment and records
// nothing at PreRx time. Dropping is not its business, and the connection has not
// yet judged the segment: an acknowledgement for data never sent would otherwise
// collapse the backoff and take a bogus round-trip sample. Only accepted segments
// reach PostRx, which the Handler guarantees.
func TestRTO_PreRxNeverDrops(t *testing.T) {
r := newRTO()
const iss = uint32(1000)
r.PostTx(dataSeg(iss, 100), 0)
r.postTx(dataSeg(iss, 100), 0)
armed := r.NextDeadline()
if armed == 0 {
t.Fatal("timer must be armed after sending data")
}
// An acknowledgement far beyond anything sent, refused by the connection.
bogus := ackSeg(iss + 100000)
if !r.PreRx(rxAt(bogus, 40*rtoMs)).Keep {
// An acknowledgement far beyond anything sent, which the connection refuses.
if !r.PreRx(nil, frameOf(t, ackSeg(iss+100000))) {
t.Error("PreRx must keep: dropping is not the estimator's business")
}
r.PostRx(tcp.RxEvent{Segment: bogus, Now: 40 * rtoMs, Accepted: false})
if r.NextDeadline() != armed {
t.Errorf("deadline moved to %d on a refused segment, want it left at %d",
r.NextDeadline(), armed)
t.Errorf("deadline moved to %d at PreRx, want it left at %d", r.NextDeadline(), armed)
}
if r.SmoothedRTT() != 0 {
t.Errorf("took an RTT sample of %v from a refused segment", r.SmoothedRTT())
t.Errorf("took an RTT sample of %v at PreRx", r.SmoothedRTT())
}
if !r.Running() {
t.Error("timer disarmed by a refused acknowledgement")
t.Error("timer disarmed at PreRx")
}
}
@@ -262,25 +288,24 @@ func TestRTO_RetransmitsZeroWindowProbe(t *testing.T) {
r := newRTO()
const iss = uint32(5000)
probe := dataSeg(iss, 1) // The one-octet probe.
r.PostTx(probe, 0)
r.postTx(probe, 0)
now := int64(rtoInitial)
prevRTO := r.CurrentRTO()
for attempt := 1; attempt <= 4; attempt++ {
dir := r.PreTx(tcp.TxIntent{Now: now, UNA: tcp.Value(iss), NXT: tcp.Value(iss + 1)})
if !dir.Retransmit {
from, rtx, _ := r.preTx(now, tcp.Value(iss))
if !rtx {
t.Fatalf("attempt %d: timer did not fire; the probe would never be resent", attempt)
}
if dir.RetransmitFrom != tcp.Value(iss) {
t.Errorf("attempt %d: retransmit from %d, want the probe octet at %d",
attempt, dir.RetransmitFrom, iss)
if from != tcp.Value(iss) {
t.Errorf("attempt %d: retransmit from %d, want the probe octet at %d", attempt, from, iss)
}
if got := r.CurrentRTO(); got <= prevRTO {
t.Errorf("attempt %d: rto %v did not back off past %v", attempt, got, prevRTO)
}
prevRTO = r.CurrentRTO()
// The peer still cannot accept the octet, so it stays unacknowledged.
r.PostTx(probe, now)
r.postTx(probe, now)
now += int64(prevRTO)
}
}