mirror of
https://github.com/soypat/lneto.git
synced 2026-09-08 07:49:05 +00:00
tcp: simplified Policy implementation based on @MDr164 (#190)
* begin prepping policy refactor manually * implement tcp.Policy and refactor rto to use it * fix CI * chatting with claude gave me idea to reformulate Policy * tcp.Policy: add newTransmitLimit output * merge with main and fix failing tests * remove fix.patch * answer my own comments
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
package rto
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/soypat/lneto/ethernet"
|
||||
"github.com/soypat/lneto/tcp"
|
||||
)
|
||||
|
||||
// sizeHeaderTCP is the fixed TCP header length. The tcp package's own constant
|
||||
// is unexported and these tests live outside it.
|
||||
const sizeHeaderTCP = 20
|
||||
|
||||
// TestRTO_HandlerRetransmitsAfterTimeout covers the seam between a Handler and
|
||||
// its Policy, which the Timer unit tests do not: a lost data segment must be
|
||||
// resent once the timer expires, with nothing arriving to prompt it.
|
||||
func TestRTO_HandlerRetransmitsAfterTimeout(t *testing.T) {
|
||||
const mtu = ethernet.MaxMTU
|
||||
const maxpackets = 4
|
||||
rng := rand.New(rand.NewSource(5))
|
||||
client, server := newHandler(t, mtu, maxpackets), newHandler(t, mtu, maxpackets)
|
||||
|
||||
var now int64 // injected monotonic clock, in nanoseconds
|
||||
client.SetPolicy(newTimer(t, func() int64 { return now }))
|
||||
|
||||
setupClientServer(t, rng, client, server)
|
||||
var rawbuf [mtu]byte
|
||||
establish(t, client, server, rawbuf[:])
|
||||
|
||||
data := []byte("hello")
|
||||
if n, err := client.Write(data); err != nil || n != len(data) {
|
||||
t.Fatal("client write:", n, err)
|
||||
}
|
||||
clear(rawbuf[:])
|
||||
n, err := client.Send(rawbuf[:])
|
||||
if err != nil || n == 0 {
|
||||
t.Fatal("client send:", n, err)
|
||||
}
|
||||
// That frame is lost: it is never handed to the server.
|
||||
|
||||
// Nothing may come back before the timer expires.
|
||||
var probe [mtu]byte
|
||||
if n, err := client.Send(probe[:]); err != nil || n != 0 {
|
||||
t.Fatalf("client sent %d bytes before the RTO expired (err %v)", n, err)
|
||||
}
|
||||
|
||||
now += int64(3 * time.Second) // past the initial RTO and one backoff
|
||||
|
||||
clear(probe[:])
|
||||
n, err = client.Send(probe[:])
|
||||
if err != nil {
|
||||
t.Fatal("client send after RTO:", err)
|
||||
}
|
||||
if n == 0 {
|
||||
t.Fatal("no retransmission after the RTO expired: the Policy directive is never applied")
|
||||
}
|
||||
if err := server.Recv(probe[:n]); err != nil {
|
||||
t.Fatal("server refused the retransmission:", err)
|
||||
}
|
||||
got := make([]byte, 16)
|
||||
nr, err := server.Read(got)
|
||||
if err != nil || string(got[:nr]) != string(data) {
|
||||
t.Fatalf("server read %q (%v), want %q", got[:nr], err, data)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRTO_HandlerRetransmitsAfterCloseWithUnackedData is the write-then-close
|
||||
// case every server performs. With the last data segment lost, the FIN behind it
|
||||
// sits above a gap the peer cannot cross, so FIN-WAIT-1 must still retransmit
|
||||
// that data or both sides wait forever.
|
||||
func TestRTO_HandlerRetransmitsAfterCloseWithUnackedData(t *testing.T) {
|
||||
const mtu = ethernet.MaxMTU
|
||||
const maxpackets = 4
|
||||
rng := rand.New(rand.NewSource(9))
|
||||
client, server := newHandler(t, mtu, maxpackets), newHandler(t, mtu, maxpackets)
|
||||
|
||||
var now int64
|
||||
client.SetPolicy(newTimer(t, func() int64 { return now }))
|
||||
|
||||
setupClientServer(t, rng, client, server)
|
||||
var rawbuf [mtu]byte
|
||||
establish(t, client, server, rawbuf[:])
|
||||
|
||||
data := []byte("last response bytes")
|
||||
if n, err := client.Write(data); err != nil || n != len(data) {
|
||||
t.Fatal("client write:", n, err)
|
||||
}
|
||||
clear(rawbuf[:])
|
||||
n, err := client.Send(rawbuf[:]) // this frame is lost in transit
|
||||
if err != nil || n == 0 {
|
||||
t.Fatal("client send:", n, err)
|
||||
}
|
||||
|
||||
// The application closes right after writing.
|
||||
if err := client.Close(); err != nil {
|
||||
t.Fatal("client close:", err)
|
||||
}
|
||||
var finbuf [mtu]byte
|
||||
nfin, err := client.Send(finbuf[:]) // FIN (also lost, or simply unacked)
|
||||
if err != nil {
|
||||
t.Fatal("client send FIN:", err)
|
||||
}
|
||||
t.Logf("state after close: %s (FIN frame %d bytes)", client.State(), nfin)
|
||||
|
||||
now += int64(3 * time.Second) // past the RTO
|
||||
|
||||
var probe [mtu]byte
|
||||
n, err = client.Send(probe[:])
|
||||
if err != nil {
|
||||
t.Fatal("client send after RTO:", err)
|
||||
}
|
||||
if n == 0 {
|
||||
t.Fatalf("no retransmission in %s: unacknowledged data is stranded by the close", client.State())
|
||||
}
|
||||
if err := server.Recv(probe[:n]); err != nil {
|
||||
t.Fatal("server refused the retransmission:", err)
|
||||
}
|
||||
got := make([]byte, 32)
|
||||
nr, err := server.Read(got)
|
||||
if err != nil || string(got[:nr]) != string(data) {
|
||||
t.Fatalf("server read %q (%v), want %q", got[:nr], err, data)
|
||||
}
|
||||
}
|
||||
|
||||
// newTimer returns a Timer driven by nanotime, ready to install as a [tcp.Policy].
|
||||
func newTimer(t *testing.T, nanotime func() int64) *Timer {
|
||||
t.Helper()
|
||||
r := new(Timer)
|
||||
err := r.Configure(nanotime)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// The handshake helpers below mirror those in the tcp package's own tests, which
|
||||
// are unexported and so unavailable here. They drive two Handlers against each
|
||||
// other over a single packet buffer, with no network in between.
|
||||
|
||||
func newHandler(t *testing.T, mtu, minpackets int) *tcp.Handler {
|
||||
t.Helper()
|
||||
h := new(tcp.Handler)
|
||||
err := h.SetBuffers(make([]byte, mtu), make([]byte, mtu), minpackets)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func setupClientServer(t *testing.T, rng *rand.Rand, client, server *tcp.Handler) {
|
||||
t.Helper()
|
||||
err := server.OpenListen(uint16(rng.Uint32()), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = client.OpenActive(uint16(rng.Uint32()), server.LocalPort(), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !client.AwaitingSynSend() {
|
||||
t.Fatal("client in wrong state")
|
||||
}
|
||||
if !server.AwaitingSynAck() {
|
||||
t.Fatal("server in wrong state")
|
||||
}
|
||||
}
|
||||
|
||||
func establish(t *testing.T, client, server *tcp.Handler, packetBuf []byte) {
|
||||
t.Helper()
|
||||
if client.State() != tcp.StateClosed {
|
||||
t.Fatal("client in wrong state")
|
||||
} else if server.State() != tcp.StateListen {
|
||||
t.Fatal("server in wrong state")
|
||||
}
|
||||
clear(packetBuf)
|
||||
|
||||
// Commence 3-way handshake: client sends SYN, server sends SYN-ACK, client sends ACK.
|
||||
n, err := client.Send(packetBuf)
|
||||
if err != nil {
|
||||
t.Fatal("client sending:", err)
|
||||
} else if n < sizeHeaderTCP {
|
||||
t.Fatal("expected client to send SYN packet")
|
||||
} else if client.State() != tcp.StateSynSent {
|
||||
t.Fatal("client did not transition to SynSent state:", client.State().String())
|
||||
}
|
||||
err = server.Recv(packetBuf[:n]) // Server receives SYN.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if server.State() != tcp.StateSynRcvd {
|
||||
t.Fatal("server did not transition to SynReceived state:", server.State().String())
|
||||
}
|
||||
|
||||
clear(packetBuf)
|
||||
n, err = server.Send(packetBuf) // Server sends SYNACK.
|
||||
if err != nil {
|
||||
t.Fatal("server sending:", err)
|
||||
} else if n < sizeHeaderTCP {
|
||||
t.Fatal("expected server to send SYNACK packet")
|
||||
}
|
||||
err = client.Recv(packetBuf[:n]) // Client receives SYNACK, is established but must send ACK.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if client.State() != tcp.StateEstablished {
|
||||
t.Fatal("client did not transition to Established state:", client.State().String())
|
||||
}
|
||||
|
||||
clear(packetBuf)
|
||||
n, err = client.Send(packetBuf) // Client sends ACK.
|
||||
if err != nil {
|
||||
t.Fatal("client sending ACK:", err)
|
||||
} else if n < sizeHeaderTCP {
|
||||
t.Fatal("expected client to send ACK packet")
|
||||
}
|
||||
err = server.Recv(packetBuf[:n]) // Server receives ACK.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if server.State() != tcp.StateEstablished {
|
||||
t.Fatal("server did not transition to Established state on ACK receive:", server.State().String())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
package rto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/soypat/lneto"
|
||||
"github.com/soypat/lneto/tcp"
|
||||
)
|
||||
|
||||
// RFC 6298 retransmission-timeout (RTO) parameters. The algorithm keeps a
|
||||
// single retransmission timer per connection (RFC 6298 §5): the timer is
|
||||
// (re)started whenever new data is acknowledged while data remains in flight,
|
||||
// stopped when all data is acknowledged, and on expiry the oldest unacknowledged
|
||||
// segment is retransmitted and the RTO is doubled (exponential backoff, §5.5).
|
||||
const (
|
||||
// rtoInitial is the RTO used before the first RTT measurement (RFC 6298 §2.1).
|
||||
rtoInitial = time.Second
|
||||
// rtoMin clamps the lower bound of the RTO. RFC 6298 §2.4 recommends a
|
||||
// minimum of 1s, but that is punishing on the low-latency links lneto
|
||||
// targets; like Linux we use a smaller floor so recovery on LAN/embedded
|
||||
// links is timely.
|
||||
rtoMin = 200 * time.Millisecond
|
||||
// rtoMax clamps the upper bound across exponential backoff (RFC 6298 §5.5
|
||||
// permits a maximum of at least 60s).
|
||||
rtoMax = 60 * time.Second
|
||||
|
||||
// rttGainShift (alpha = 1/8) and rttvarGainShift (beta = 1/4) are the
|
||||
// smoothing gains of RFC 6298 §2.3, applied as integer shifts.
|
||||
rttGainShift = 3 // alpha = 1/8
|
||||
rttvarGainShift = 2 // beta = 1/4
|
||||
// rttvarK is the RTTVAR multiplier in RTO = SRTT + K*RTTVAR (RFC 6298 §2.3).
|
||||
rttvarK = 4
|
||||
// backoffMax caps the exponential-backoff doublings so RTO arithmetic cannot
|
||||
// overflow and a wedged connection keeps probing at rtoMax.
|
||||
backoffMax = 12
|
||||
)
|
||||
|
||||
// Timer implements the RFC 6298 round-trip-time estimator and the single
|
||||
// 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 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]
|
||||
// advances the highest sequence acknowledged. This is what lets it manage the
|
||||
// timer (RFC 6298 §5.2/§5.3) without reaching into the tcp state machine, and it
|
||||
// is also how retransmissions are distinguished for Karn's algorithm — a segment
|
||||
// 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.
|
||||
haveRTT bool // false until the first RTT sample is taken.
|
||||
|
||||
// Shadow of the send sequence space, derived from observed segments.
|
||||
haveSeq bool // false until the first data segment is observed.
|
||||
sndUNA tcp.Value // highest acknowledged sequence number seen on the wire.
|
||||
sndNXT tcp.Value // one past the highest sequence number sent.
|
||||
|
||||
// RTT sampling state (Karn's algorithm, RFC 6298 §3): at most one segment is
|
||||
// timed at a time and retransmitted segments are never sampled.
|
||||
timing bool
|
||||
timedSeq tcp.Value // ACK at or beyond this value completes the sample.
|
||||
timedAt int64 // send time (monotonic ns) of the timed segment.
|
||||
|
||||
// Retransmission timer state.
|
||||
running bool
|
||||
deadline int64 // time (monotonic ns) at which the timer expires.
|
||||
backoff uint8 // consecutive timeouts, for exponential backoff.
|
||||
|
||||
// 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 a policy that composes the timer as a
|
||||
// peer never sees the timer's own directive.
|
||||
expirations uint32
|
||||
}
|
||||
|
||||
var _ tcp.Policy = (*Timer)(nil)
|
||||
|
||||
// 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
|
||||
// intentionally not part of [tcp.Policy].
|
||||
func (r *Timer) SmoothedRTT() time.Duration { return r.srtt }
|
||||
|
||||
// CurrentRTO returns the timeout currently in effect, clamped to [rtoMin, rtoMax].
|
||||
func (r *Timer) CurrentRTO() time.Duration {
|
||||
rto := r.rto
|
||||
if rto < rtoMin {
|
||||
rto = rtoMin
|
||||
} else if rto > rtoMax {
|
||||
rto = rtoMax
|
||||
}
|
||||
return rto
|
||||
}
|
||||
|
||||
// Running reports whether the retransmission timer is currently armed.
|
||||
func (r *Timer) Running() bool { return r.running }
|
||||
|
||||
// Expirations returns how many times the retransmission timer has expired since
|
||||
// [Timer.Reset]. A policy that shares this timer rather than driving it watches
|
||||
// this for a change to learn that a timeout happened, since it never sees the
|
||||
// timer's own directive. It is concrete-type introspection and is intentionally
|
||||
// not part of [tcp.Policy].
|
||||
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 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
|
||||
}
|
||||
return r.deadline
|
||||
}
|
||||
|
||||
// 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(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].
|
||||
//
|
||||
// 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
|
||||
if r.timing && !ack.LessThan(r.timedSeq) {
|
||||
// ACK covers the timed segment: take the RTT sample (§4). A valid
|
||||
// measurement collapses the backoff (§5.7).
|
||||
r.updateRTT(time.Duration(now - r.timedAt))
|
||||
r.timing = false
|
||||
r.backoff = 0
|
||||
}
|
||||
if r.sndUNA.LessThan(ack) && !r.sndNXT.LessThan(ack) {
|
||||
// ACK advances snd.UNA and does not exceed what we have sent.
|
||||
r.sndUNA = ack
|
||||
}
|
||||
if r.sndUNA == r.sndNXT {
|
||||
r.running = false // §5.3: all outstanding data acknowledged.
|
||||
} else {
|
||||
// §5.3: new (but not all) data acknowledged — restart the timer.
|
||||
r.running = true
|
||||
r.deadline = now + int64(r.CurrentRTO())
|
||||
}
|
||||
}
|
||||
|
||||
// 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 — and asks the
|
||||
// connection to retransmit from snd.UNA (go-back-N). It writes no TCP options
|
||||
// and imposes no transmit limit: retransmission timing needs neither, and
|
||||
// congestion control belongs to a Policy composing this timer. It implements
|
||||
// [tcp.Policy].
|
||||
func (r *Timer) PreTx(h *tcp.Handler, outgoingOpts tcp.Frame) (newTransmitLimit tcp.Size, rtxFrom tcp.Value, retransmit bool) {
|
||||
return r.preTx(r.nanotime(), h.ControlBlock().SendUNA())
|
||||
}
|
||||
|
||||
func (r *Timer) preTx(now int64, una tcp.Value) (newTransmitLimit tcp.Size, rtxFrom tcp.Value, retransmit bool) {
|
||||
if !r.running || now < r.deadline || r.sndUNA == r.sndNXT {
|
||||
return tcp.TransmitUnlimited, 0, false
|
||||
}
|
||||
r.expirations++
|
||||
r.timing = false // §5.4: do not sample a retransmitted segment.
|
||||
if r.backoff < backoffMax {
|
||||
r.backoff++
|
||||
r.rto = min(r.CurrentRTO()*2, rtoMax) // §5.5: RTO = RTO * 2.
|
||||
}
|
||||
r.running = true
|
||||
r.deadline = now + int64(r.CurrentRTO())
|
||||
return tcp.TransmitUnlimited, una, true
|
||||
}
|
||||
|
||||
// PostTx records an emitted segment: it advances the shadow send sequence,
|
||||
// begins timing newly transmitted data (RFC 6298 §3) and arms the timer (§5.1).
|
||||
// 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(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.
|
||||
}
|
||||
segStart := outgoing.SEQ
|
||||
segEnd := segStart + tcp.Value(outgoing.LEN())
|
||||
if !r.haveSeq {
|
||||
r.haveSeq = true
|
||||
r.sndUNA = segStart
|
||||
r.sndNXT = segStart
|
||||
}
|
||||
if !r.sndNXT.LessThan(segEnd) {
|
||||
// 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
|
||||
return
|
||||
}
|
||||
r.sndNXT = segEnd
|
||||
if !r.timing {
|
||||
r.timing = true
|
||||
r.timedSeq = segEnd
|
||||
r.timedAt = now
|
||||
}
|
||||
if !r.running {
|
||||
r.running = true
|
||||
r.deadline = now + int64(r.CurrentRTO())
|
||||
}
|
||||
}
|
||||
|
||||
// ObserveRTT folds a round-trip measurement taken by other means into the
|
||||
// estimator, for a policy that composes this timer and can measure the round trip
|
||||
// more accurately than acknowledgement timing allows. The RFC 7323 timestamp echo
|
||||
// is the case this exists for.
|
||||
//
|
||||
// Unlike the timer's own sampling this does not apply Karn's algorithm, because a
|
||||
// sample derived from an echoed timestamp is unambiguous even when the segment
|
||||
// carrying it was a retransmission (RFC 7323 §4.1). Non-positive samples are
|
||||
// ignored.
|
||||
func (r *Timer) ObserveRTT(rtt time.Duration) { r.updateRTT(rtt) }
|
||||
|
||||
// updateRTT folds a round-trip measurement into SRTT/RTTVAR/RTO using the
|
||||
// integer-shift form of RFC 6298 §2.2/§2.3.
|
||||
func (r *Timer) updateRTT(sample time.Duration) {
|
||||
if sample <= 0 {
|
||||
return
|
||||
}
|
||||
if !r.haveRTT {
|
||||
// First measurement (RFC 6298 §2.2).
|
||||
r.srtt = sample
|
||||
r.rttvar = sample / 2
|
||||
r.haveRTT = true
|
||||
} else {
|
||||
// Subsequent measurements (RFC 6298 §2.3):
|
||||
// RTTVAR = (1-beta)*RTTVAR + beta*|SRTT-R|
|
||||
// SRTT = (1-alpha)*SRTT + alpha*R
|
||||
diff := r.srtt - sample
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
}
|
||||
r.rttvar += (diff - r.rttvar) >> rttvarGainShift
|
||||
r.srtt += (sample - r.srtt) >> rttGainShift
|
||||
}
|
||||
r.rto = r.srtt + rttvarK*r.rttvar
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
package rto
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/soypat/lneto/tcp"
|
||||
)
|
||||
|
||||
const rtoMs = int64(time.Millisecond)
|
||||
|
||||
// dataSeg builds a data segment of datalen octets starting at seq.
|
||||
func dataSeg(seq uint32, datalen int) tcp.Segment {
|
||||
return tcp.Segment{SEQ: tcp.Value(seq), DATALEN: tcp.Size(datalen), Flags: tcp.FlagPSH | tcp.FlagACK}
|
||||
}
|
||||
|
||||
// ackSeg builds a bare ACK acknowledging up to ack.
|
||||
func ackSeg(ack uint32) tcp.Segment {
|
||||
return tcp.Segment{ACK: tcp.Value(ack), Flags: tcp.FlagACK}
|
||||
}
|
||||
|
||||
func newRTO() *Timer {
|
||||
var r Timer
|
||||
if err := r.Configure(func() int64 { return 0 }); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &r
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
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) {
|
||||
r := newRTO()
|
||||
r.Reset()
|
||||
if r.rto != rtoInitial {
|
||||
t.Errorf("initial rto=%v, want %v", r.rto, rtoInitial)
|
||||
}
|
||||
if r.CurrentRTO() != rtoInitial {
|
||||
t.Errorf("CurrentRTO=%v, want %v", r.CurrentRTO(), rtoInitial)
|
||||
}
|
||||
if r.haveRTT {
|
||||
t.Error("haveRTT should be false before first sample")
|
||||
}
|
||||
if r.Running() || r.NextDeadline() != 0 {
|
||||
t.Error("timer must be disarmed after Reset")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRTO_ArmOnSendSampleOnAck sends data, verifies the timer arms, then acks it
|
||||
// and verifies an RTT sample is taken and the timer stops once all data is acked.
|
||||
func TestRTO_ArmOnSendSampleOnAck(t *testing.T) {
|
||||
r := newRTO()
|
||||
const iss = uint32(1000)
|
||||
|
||||
r.postTx(dataSeg(iss, 100), 0)
|
||||
if !r.Running() {
|
||||
t.Fatal("timer must arm after sending data")
|
||||
}
|
||||
if r.NextDeadline() != int64(rtoInitial) {
|
||||
t.Errorf("deadline=%d, want %d", r.NextDeadline(), int64(rtoInitial))
|
||||
}
|
||||
|
||||
// ACK arrives one RTT (40ms) later covering all sent data.
|
||||
if !r.PreRx(nil, frameOf(t, ackSeg(iss+100))) {
|
||||
t.Error("PreRx must keep the segment")
|
||||
}
|
||||
r.postRx(ackSeg(iss+100), 40*rtoMs)
|
||||
if r.Running() {
|
||||
t.Error("timer must stop once all data is acknowledged")
|
||||
}
|
||||
if r.SmoothedRTT() != 40*time.Millisecond {
|
||||
t.Errorf("srtt=%v, want 40ms", r.SmoothedRTT())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRTO_RetransmitOnTimeout verifies PreTx directs a go-back-N retransmit once
|
||||
// the deadline passes with data outstanding, and backs the RTO off.
|
||||
func TestRTO_RetransmitOnTimeout(t *testing.T) {
|
||||
r := newRTO()
|
||||
const iss = uint32(1000)
|
||||
r.postTx(dataSeg(iss, 100), 0)
|
||||
|
||||
if _, _, rtx := r.preTx(int64(rtoInitial)-1, tcp.Value(iss)); rtx {
|
||||
t.Fatal("must not retransmit before the deadline")
|
||||
}
|
||||
limit, from, rtx := r.preTx(int64(rtoInitial), tcp.Value(iss))
|
||||
if !rtx {
|
||||
t.Fatal("RTO must fire at the deadline with data outstanding")
|
||||
}
|
||||
if limit != tcp.TransmitUnlimited {
|
||||
t.Error("the estimator never limits new data")
|
||||
}
|
||||
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))
|
||||
if r.timing {
|
||||
t.Error("retransmitted segment must not be RTT-sampled (Karn)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRTO_KarnNoSampleOnRetransmittedAck verifies that after a retransmission the
|
||||
// ACK does not produce an RTT sample (Karn's algorithm).
|
||||
func TestRTO_KarnNoSampleOnRetransmittedAck(t *testing.T) {
|
||||
r := newRTO()
|
||||
const iss = uint32(1000)
|
||||
r.postTx(dataSeg(iss, 100), 0)
|
||||
// Timeout and retransmit.
|
||||
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(ackSeg(iss+100), int64(rtoInitial)+10*rtoMs)
|
||||
if r.haveRTT {
|
||||
t.Error("no RTT sample should exist after a retransmission (Karn)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRTO_TimerRestartsWhilePartiallyAcked verifies the timer restarts (not
|
||||
// stops) when an ACK advances UNA but data remains in flight (RFC 6298 §5.3).
|
||||
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.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")
|
||||
}
|
||||
if r.NextDeadline() != 40*rtoMs+int64(r.CurrentRTO()) {
|
||||
t.Errorf("deadline=%d, want %d", r.NextDeadline(), 40*rtoMs+int64(r.CurrentRTO()))
|
||||
}
|
||||
}
|
||||
|
||||
// TestRTO_NoArmWithoutData verifies control-only segments neither arm the timer
|
||||
// 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.
|
||||
if r.Running() || r.timing {
|
||||
t.Error("pure control segment must not arm the timer or start a sample")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRTO_BackoffCollapsesOnValidSample verifies a valid RTT measurement
|
||||
// collapses the exponential backoff counter (RFC 6298 §5.7).
|
||||
func TestRTO_BackoffCollapsesOnValidSample(t *testing.T) {
|
||||
r := newRTO()
|
||||
const iss = uint32(1000)
|
||||
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(ackSeg(iss+200), int64(rtoInitial)+30*rtoMs)
|
||||
if r.backoff != 0 {
|
||||
t.Errorf("backoff=%d, want 0 after a valid RTT sample", r.backoff)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRTO_Clamped verifies CurrentRTO is clamped to [rtoMin, rtoMax].
|
||||
func TestRTO_Clamped(t *testing.T) {
|
||||
r := newRTO()
|
||||
r.rto = time.Nanosecond
|
||||
if got := r.CurrentRTO(); got != rtoMin {
|
||||
t.Errorf("CurrentRTO=%v, want floor %v", got, rtoMin)
|
||||
}
|
||||
r.rto = time.Hour
|
||||
if got := r.CurrentRTO(); got != rtoMax {
|
||||
t.Errorf("CurrentRTO=%v, want ceiling %v", got, rtoMax)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRTO_UpdateRTTFirstSample verifies the first-measurement initialization of
|
||||
// SRTT/RTTVAR (RFC 6298 §2.2).
|
||||
func TestRTO_UpdateRTTFirstSample(t *testing.T) {
|
||||
r := newRTO()
|
||||
r.updateRTT(100 * time.Millisecond)
|
||||
if r.srtt != 100*time.Millisecond {
|
||||
t.Errorf("srtt=%v, want 100ms", r.srtt)
|
||||
}
|
||||
if r.rttvar != 50*time.Millisecond {
|
||||
t.Errorf("rttvar=%v, want 50ms", r.rttvar)
|
||||
}
|
||||
// RTO = SRTT + K*RTTVAR = 100 + 4*50 = 300ms.
|
||||
if r.rto != 300*time.Millisecond {
|
||||
t.Errorf("rto=%v, want 300ms", r.rto)
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
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")
|
||||
}
|
||||
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_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)
|
||||
armed := r.NextDeadline()
|
||||
if armed == 0 {
|
||||
t.Fatal("timer must be armed after sending data")
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
if 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 at PreRx", r.SmoothedRTT())
|
||||
}
|
||||
if !r.Running() {
|
||||
t.Error("timer disarmed at PreRx")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRTO_RetransmitsZeroWindowProbe verifies the timer takes over the periodic
|
||||
// probing of a closed send window. A zero-window probe is a single octet the peer
|
||||
// cannot accept, so it goes unacknowledged; the timer must keep resending it, with
|
||||
// exponential backoff, which is the persist-timer behaviour of RFC 9293 §3.8.6.1.
|
||||
// The tcp package relies on this and refuses to probe without a policy installed.
|
||||
func TestRTO_RetransmitsZeroWindowProbe(t *testing.T) {
|
||||
r := newRTO()
|
||||
const iss = uint32(5000)
|
||||
probe := dataSeg(iss, 1) // The one-octet probe.
|
||||
r.postTx(probe, 0)
|
||||
|
||||
now := int64(rtoInitial)
|
||||
prevRTO := r.CurrentRTO()
|
||||
for attempt := 1; attempt <= 4; attempt++ {
|
||||
_, 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 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)
|
||||
now += int64(prevRTO)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user