Debian Codegraph @

tcpip/transport/tcp/rack.go

raw symbols SCIP
1 // Copyright 2020 The gVisor Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package tcp
16
17 import (
18 "time"
19
20 "inet.af/netstack/tcpip"
21 "inet.af/netstack/tcpip/seqnum"
22 "inet.af/netstack/tcpip/stack"
23 )
24
25 const (
26 // wcDelayedACKTimeout is the recommended maximum delayed ACK timer
27 // value as defined in the RFC. It stands for worst case delayed ACK
28 // timer (WCDelAckT). When FlightSize is 1, PTO is inflated by
29 // WCDelAckT time to compensate for a potential long delayed ACK timer
30 // at the receiver.
31 // See: https://tools.ietf.org/html/draft-ietf-tcpm-rack-08#section-7.5.
32 wcDelayedACKTimeout = 200 * time.Millisecond
33
34 // tcpRACKRecoveryThreshold is the number of loss recoveries for which
35 // the reorder window is inflated and after that the reorder window is
36 // reset to its initial value of minRTT/4.
37 // See: https://tools.ietf.org/html/draft-ietf-tcpm-rack-08#section-7.2.
38 tcpRACKRecoveryThreshold = 16
39 )
40
41 // RACK is a loss detection algorithm used in TCP to detect packet loss and
42 // reordering using transmission timestamp of the packets instead of packet or
43 // sequence counts. To use RACK, SACK should be enabled on the connection.
44
45 // rackControl stores the rack related fields.
46 // See: https://tools.ietf.org/html/draft-ietf-tcpm-rack-08#section-6.1
47 //
48 // +stateify savable
49 type rackControl struct {
50 stack.TCPRACKState
51
52 // exitedRecovery indicates if the connection is exiting loss recovery.
53 // This flag is set if the sender is leaving the recovery after
54 // receiving an ACK and is reset during updating of reorder window.
55 exitedRecovery bool
56
57 // minRTT is the estimated minimum RTT of the connection.
58 minRTT time.Duration
59
60 // tlpRxtOut indicates whether there is an unacknowledged
61 // TLP retransmission.
62 tlpRxtOut bool
63
64 // tlpHighRxt the value of sender.sndNxt at the time of sending
65 // a TLP retransmission.
66 tlpHighRxt seqnum.Value
67
68 // snd is a reference to the sender.
69 snd *sender
70 }
71
72 // init initializes RACK specific fields.
73 func (rc *rackControl) init(snd *sender, iss seqnum.Value) {
74 rc.FACK = iss
75 rc.ReoWndIncr = 1
76 rc.snd = snd
77 }
78
79 // update will update the RACK related fields when an ACK has been received.
80 // See: https://tools.ietf.org/html/draft-ietf-tcpm-rack-09#section-6.2
81 func (rc *rackControl) update(seg *segment, ackSeg *segment) {
82 rtt := rc.snd.ep.stack.Clock().NowMonotonic().Sub(seg.xmitTime)
83
84 // If the ACK is for a retransmitted packet, do not update if it is a
85 // spurious inference which is determined by below checks:
86 // 1. When Timestamping option is available, if the TSVal is less than
87 // the transmit time of the most recent retransmitted packet.
88 // 2. When RTT calculated for the packet is less than the smoothed RTT
89 // for the connection.
90 // See: https://tools.ietf.org/html/draft-ietf-tcpm-rack-08#section-7.2
91 // step 2
92 if seg.xmitCount > 1 {
93 if ackSeg.parsedOptions.TS && ackSeg.parsedOptions.TSEcr != 0 {
94 if ackSeg.parsedOptions.TSEcr < rc.snd.ep.tsVal(seg.xmitTime) {
95 return
96 }
97 }
98 if rtt < rc.minRTT {
99 return
100 }
101 }
102
103 rc.RTT = rtt
104
105 // The sender can either track a simple global minimum of all RTT
106 // measurements from the connection, or a windowed min-filtered value
107 // of recent RTT measurements. This implementation keeps track of the
108 // simple global minimum of all RTTs for the connection.
109 if rtt < rc.minRTT || rc.minRTT == 0 {
110 rc.minRTT = rtt
111 }
112
113 // Update rc.xmitTime and rc.endSequence to the transmit time and
114 // ending sequence number of the packet which has been acknowledged
115 // most recently.
116 endSeq := seg.sequenceNumber.Add(seqnum.Size(seg.data.Size()))
117 if rc.XmitTime.Before(seg.xmitTime) || (seg.xmitTime == rc.XmitTime && rc.EndSequence.LessThan(endSeq)) {
118 rc.XmitTime = seg.xmitTime
119 rc.EndSequence = endSeq
120 }
121 }
122
123 // detectReorder detects if packet reordering has been observed.
124 // See: https://tools.ietf.org/html/draft-ietf-tcpm-rack-08#section-7.2
125 // * Step 3: Detect data segment reordering.
126 // To detect reordering, the sender looks for original data segments being
127 // delivered out of order. To detect such cases, the sender tracks the
128 // highest sequence selectively or cumulatively acknowledged in the RACK.fack
129 // variable. The name "fack" stands for the most "Forward ACK" (this term is
130 // adopted from [FACK]). If a never retransmitted segment that's below
131 // RACK.fack is (selectively or cumulatively) acknowledged, it has been
132 // delivered out of order. The sender sets RACK.reord to TRUE if such segment
133 // is identified.
134 func (rc *rackControl) detectReorder(seg *segment) {
135 endSeq := seg.sequenceNumber.Add(seqnum.Size(seg.data.Size()))
136 if rc.FACK.LessThan(endSeq) {
137 rc.FACK = endSeq
138 return
139 }
140
141 if endSeq.LessThan(rc.FACK) && seg.xmitCount == 1 {
142 rc.Reord = true
143 }
144 }
145
146 func (rc *rackControl) setDSACKSeen(dsackSeen bool) {
147 rc.DSACKSeen = dsackSeen
148 }
149
150 // shouldSchedulePTO dictates whether we should schedule a PTO or not.
151 // See https://tools.ietf.org/html/draft-ietf-tcpm-rack-08#section-7.5.1.
152 func (s *sender) shouldSchedulePTO() bool {
153 // Schedule PTO only if RACK loss detection is enabled.
154 return s.ep.tcpRecovery&tcpip.TCPRACKLossDetection != 0 &&
155 // The connection supports SACK.
156 s.ep.SACKPermitted &&
157 // The connection is not in loss recovery.
158 (s.state != tcpip.RTORecovery && s.state != tcpip.SACKRecovery) &&
159 // The connection has no SACKed sequences in the SACK scoreboard.
160 s.ep.scoreboard.Sacked() == 0
161 }
162
163 // schedulePTO schedules the probe timeout as defined in
164 // https://tools.ietf.org/html/draft-ietf-tcpm-rack-08#section-7.5.1.
165 func (s *sender) schedulePTO() {
166 pto := time.Second
167 s.rtt.Lock()
168 if s.rtt.TCPRTTState.SRTTInited && s.rtt.TCPRTTState.SRTT > 0 {
169 pto = s.rtt.TCPRTTState.SRTT * 2
170 if s.Outstanding == 1 {
171 pto += wcDelayedACKTimeout
172 }
173 }
174 s.rtt.Unlock()
175
176 now := s.ep.stack.Clock().NowMonotonic()
177 if s.resendTimer.enabled() {
178 if now.Add(pto).After(s.resendTimer.target) {
179 pto = s.resendTimer.target.Sub(now)
180 }
181 s.resendTimer.disable()
182 }
183
184 s.probeTimer.enable(pto)
185 }
186
187 // probeTimerExpired is the same as TLP_send_probe() as defined in
188 // https://tools.ietf.org/html/draft-ietf-tcpm-rack-08#section-7.5.2.
189 func (s *sender) probeTimerExpired() tcpip.Error {
190 if !s.probeTimer.checkExpiration() {
191 return nil
192 }
193
194 var dataSent bool
195 if s.writeNext != nil && s.writeNext.xmitCount == 0 && s.Outstanding < s.SndCwnd {
196 dataSent = s.maybeSendSegment(s.writeNext, int(s.ep.scoreboard.SMSS()), s.SndUna.Add(s.SndWnd))
197 if dataSent {
198 s.Outstanding += s.pCount(s.writeNext, s.MaxPayloadSize)
199 s.writeNext = s.writeNext.Next()
200 }
201 }
202
203 if !dataSent && !s.rc.tlpRxtOut {
204 var highestSeqXmit *segment
205 for highestSeqXmit = s.writeList.Front(); highestSeqXmit != nil; highestSeqXmit = highestSeqXmit.Next() {
206 if highestSeqXmit.xmitCount == 0 {
207 // Nothing in writeList is transmitted, no need to send a probe.
208 highestSeqXmit = nil
209 break
210 }
211 if highestSeqXmit.Next() == nil || highestSeqXmit.Next().xmitCount == 0 {
212 // Either everything in writeList has been transmitted or the next
213 // sequence has not been transmitted. Either way this is the highest
214 // sequence segment that was transmitted.
215 break
216 }
217 }
218
219 if highestSeqXmit != nil {
220 dataSent = s.maybeSendSegment(highestSeqXmit, int(s.ep.scoreboard.SMSS()), s.SndUna.Add(s.SndWnd))
221 if dataSent {
222 s.rc.tlpRxtOut = true
223 s.rc.tlpHighRxt = s.SndNxt
224 }
225 }
226 }
227
228 // Whether or not the probe was sent, the sender must arm the resend timer,
229 // not the probe timer. This ensures that the sender does not send repeated,
230 // back-to-back tail loss probes.
231 s.postXmit(dataSent, false /* shouldScheduleProbe */)
232 return nil
233 }
234
235 // detectTLPRecovery detects if recovery was accomplished by the loss probes
236 // and updates TLP state accordingly.
237 // See https://tools.ietf.org/html/draft-ietf-tcpm-rack-08#section-7.6.3.
238 func (s *sender) detectTLPRecovery(ack seqnum.Value, rcvdSeg *segment) {
239 if !(s.ep.SACKPermitted && s.rc.tlpRxtOut) {
240 return
241 }
242
243 // Step 1.
244 if s.isDupAck(rcvdSeg) && ack == s.rc.tlpHighRxt {
245 var sbAboveTLPHighRxt bool
246 for _, sb := range rcvdSeg.parsedOptions.SACKBlocks {
247 if s.rc.tlpHighRxt.LessThan(sb.End) {
248 sbAboveTLPHighRxt = true
249 break
250 }
251 }
252 if !sbAboveTLPHighRxt {
253 // TLP episode is complete.
254 s.rc.tlpRxtOut = false
255 }
256 }
257
258 if s.rc.tlpRxtOut && s.rc.tlpHighRxt.LessThanEq(ack) {
259 // TLP episode is complete.
260 s.rc.tlpRxtOut = false
261 if !checkDSACK(rcvdSeg) {
262 // Step 2. Either the original packet or the retransmission (in the
263 // form of a probe) was lost. Invoke a congestion control response
264 // equivalent to fast recovery.
265 s.cc.HandleLossDetected()
266 s.enterRecovery()
267 s.leaveRecovery()
268 }
269 }
270 }
271
272 // updateRACKReorderWindow updates the reorder window.
273 // See: https://tools.ietf.org/html/draft-ietf-tcpm-rack-08#section-7.2
274 // * Step 4: Update RACK reordering window
275 // To handle the prevalent small degree of reordering, RACK.reo_wnd serves as
276 // an allowance for settling time before marking a packet lost. RACK starts
277 // initially with a conservative window of min_RTT/4. If no reordering has
278 // been observed RACK uses reo_wnd of zero during loss recovery, in order to
279 // retransmit quickly, or when the number of DUPACKs exceeds the classic
280 // DUPACKthreshold.
281 func (rc *rackControl) updateRACKReorderWindow() {
282 dsackSeen := rc.DSACKSeen
283 snd := rc.snd
284
285 // React to DSACK once per round trip.
286 // If SND.UNA < RACK.rtt_seq:
287 // RACK.dsack = false
288 if snd.SndUna.LessThan(rc.RTTSeq) {
289 dsackSeen = false
290 }
291
292 // If RACK.dsack:
293 // RACK.reo_wnd_incr += 1
294 // RACK.dsack = false
295 // RACK.rtt_seq = SND.NXT
296 // RACK.reo_wnd_persist = 16
297 if dsackSeen {
298 rc.ReoWndIncr++
299 dsackSeen = false
300 rc.RTTSeq = snd.SndNxt
301 rc.ReoWndPersist = tcpRACKRecoveryThreshold
302 } else if rc.exitedRecovery {
303 // Else if exiting loss recovery:
304 // RACK.reo_wnd_persist -= 1
305 // If RACK.reo_wnd_persist <= 0:
306 // RACK.reo_wnd_incr = 1
307 rc.ReoWndPersist--
308 if rc.ReoWndPersist <= 0 {
309 rc.ReoWndIncr = 1
310 }
311 rc.exitedRecovery = false
312 }
313
314 // Reorder window is zero during loss recovery, or when the number of
315 // DUPACKs exceeds the classic DUPACKthreshold.
316 // If RACK.reord is FALSE:
317 // If in loss recovery: (If in fast or timeout recovery)
318 // RACK.reo_wnd = 0
319 // Return
320 // Else if RACK.pkts_sacked >= RACK.dupthresh:
321 // RACK.reo_wnd = 0
322 // return
323 if !rc.Reord {
324 if snd.state == tcpip.RTORecovery || snd.state == tcpip.SACKRecovery {
325 rc.ReoWnd = 0
326 return
327 }
328
329 if snd.SackedOut >= nDupAckThreshold {
330 rc.ReoWnd = 0
331 return
332 }
333 }
334
335 // Calculate reorder window.
336 // RACK.reo_wnd = RACK.min_RTT / 4 * RACK.reo_wnd_incr
337 // RACK.reo_wnd = min(RACK.reo_wnd, SRTT)
338 snd.rtt.Lock()
339 srtt := snd.rtt.TCPRTTState.SRTT
340 snd.rtt.Unlock()
341 rc.ReoWnd = time.Duration((int64(rc.minRTT) / 4) * int64(rc.ReoWndIncr))
342 if srtt < rc.ReoWnd {
343 rc.ReoWnd = srtt
344 }
345 }
346
347 func (rc *rackControl) exitRecovery() {
348 rc.exitedRecovery = true
349 }
350
351 // detectLoss marks the segment as lost if the reordering window has elapsed
352 // and the ACK is not received. It will also arm the reorder timer.
353 // See: https://tools.ietf.org/html/draft-ietf-tcpm-rack-08#section-7.2 Step 5.
354 func (rc *rackControl) detectLoss(rcvTime tcpip.MonotonicTime) int {
355 var timeout time.Duration
356 numLost := 0
357 for seg := rc.snd.writeList.Front(); seg != nil && seg.xmitCount != 0; seg = seg.Next() {
358 if rc.snd.ep.scoreboard.IsSACKED(seg.sackBlock()) {
359 continue
360 }
361
362 if seg.lost && seg.xmitCount == 1 {
363 numLost++
364 continue
365 }
366
367 endSeq := seg.sequenceNumber.Add(seqnum.Size(seg.data.Size()))
368 if seg.xmitTime.Before(rc.XmitTime) || (seg.xmitTime == rc.XmitTime && rc.EndSequence.LessThan(endSeq)) {
369 timeRemaining := seg.xmitTime.Sub(rcvTime) + rc.RTT + rc.ReoWnd
370 if timeRemaining <= 0 {
371 seg.lost = true
372 numLost++
373 } else if timeRemaining > timeout {
374 timeout = timeRemaining
375 }
376 }
377 }
378
379 if timeout != 0 && !rc.snd.reorderTimer.enabled() {
380 rc.snd.reorderTimer.enable(timeout)
381 }
382 return numLost
383 }
384
385 // reorderTimerExpired will retransmit the segments which have not been acked
386 // before the reorder timer expired.
387 func (rc *rackControl) reorderTimerExpired() tcpip.Error {
388 // Check if the timer actually expired or if it's a spurious wake due
389 // to a previously orphaned runtime timer.
390 if !rc.snd.reorderTimer.checkExpiration() {
391 return nil
392 }
393
394 numLost := rc.detectLoss(rc.snd.ep.stack.Clock().NowMonotonic())
395 if numLost == 0 {
396 return nil
397 }
398
399 fastRetransmit := false
400 if !rc.snd.FastRecovery.Active {
401 rc.snd.cc.HandleLossDetected()
402 rc.snd.enterRecovery()
403 fastRetransmit = true
404 }
405
406 rc.DoRecovery(nil, fastRetransmit)
407 return nil
408 }
409
410 // DoRecovery implements lossRecovery.DoRecovery.
411 func (rc *rackControl) DoRecovery(_ *segment, fastRetransmit bool) {
412 snd := rc.snd
413 if fastRetransmit {
414 snd.resendSegment()
415 }
416
417 var dataSent bool
418 // Iterate the writeList and retransmit the segments which are marked
419 // as lost by RACK.
420 for seg := snd.writeList.Front(); seg != nil && seg.xmitCount > 0; seg = seg.Next() {
421 if seg == snd.writeNext {
422 break
423 }
424
425 if !seg.lost {
426 continue
427 }
428
429 // Reset seg.lost as it is already SACKed.
430 if snd.ep.scoreboard.IsSACKED(seg.sackBlock()) {
431 seg.lost = false
432 continue
433 }
434
435 // Check the congestion window after entering recovery.
436 if snd.Outstanding >= snd.SndCwnd {
437 break
438 }
439
440 if sent := snd.maybeSendSegment(seg, int(snd.ep.scoreboard.SMSS()), snd.SndUna.Add(snd.SndWnd)); !sent {
441 break
442 }
443 dataSent = true
444 snd.Outstanding += snd.pCount(seg, snd.MaxPayloadSize)
445 }
446
447 snd.postXmit(dataSent, true /* shouldScheduleProbe */)
448 }