1
2
3
4
5 package quic
6
7 import (
8 "context"
9 "log/slog"
10 "math"
11 "time"
12 )
13
14
15
16 type ccReno struct {
17 maxDatagramSize int
18
19
20 congestionWindow int
21
22
23
24
25 bytesInFlight int
26
27
28
29 slowStartThreshold int
30
31
32
33 recoveryStartTime time.Time
34
35
36 congestionPendingAcks int
37
38
39
40
41 sendOnePacketInRecovery bool
42
43
44 inRecovery bool
45
46
47
48
49 underutilized bool
50
51
52
53 ackLastLoss time.Time
54
55
56
57
58
59
60 persistentCongestion [numberSpaceCount]struct {
61 start time.Time
62 end time.Time
63 next packetNumber
64 }
65 }
66
67 func newReno(maxDatagramSize int) *ccReno {
68 c := &ccReno{
69 maxDatagramSize: maxDatagramSize,
70 }
71
72
73 c.congestionWindow = min(10*maxDatagramSize, max(14720, c.minimumCongestionWindow()))
74
75
76 c.slowStartThreshold = math.MaxInt
77
78 for space := range c.persistentCongestion {
79 c.persistentCongestion[space].next = -1
80 }
81 return c
82 }
83
84
85
86
87
88
89
90
91
92 func (c *ccReno) canSend() bool {
93 if c.sendOnePacketInRecovery {
94 return true
95 }
96 return c.bytesInFlight+c.maxDatagramSize <= c.congestionWindow
97 }
98
99
100
101
102
103
104
105
106 func (c *ccReno) setUnderutilized(log *slog.Logger, v bool) {
107 if c.underutilized == v {
108 return
109 }
110 oldState := c.state()
111 c.underutilized = v
112 if logEnabled(log, QLogLevelPacket) {
113 logCongestionStateUpdated(log, oldState, c.state())
114 }
115 }
116
117
118 func (c *ccReno) packetSent(now time.Time, log *slog.Logger, space numberSpace, sent *sentPacket) {
119 if !sent.inFlight {
120 return
121 }
122 c.bytesInFlight += sent.size
123 if c.sendOnePacketInRecovery {
124 c.sendOnePacketInRecovery = false
125 }
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 func (c *ccReno) packetAcked(now time.Time, sent *sentPacket) {
140 if !sent.inFlight {
141 return
142 }
143 c.bytesInFlight -= sent.size
144
145 if c.underutilized {
146
147 return
148 }
149 if sent.time.Before(c.recoveryStartTime) {
150
151
152
153
154 return
155 }
156 c.congestionPendingAcks += sent.size
157 }
158
159
160
161 func (c *ccReno) packetLost(now time.Time, space numberSpace, sent *sentPacket, rtt *rttState) {
162
163
164
165
166
167
168 isValidPersistentCongestionSample := (sent.ackEliciting &&
169 !rtt.firstSampleTime.IsZero() &&
170 !sent.time.Before(rtt.firstSampleTime))
171 if isValidPersistentCongestionSample {
172
173
174 if sent.num != c.persistentCongestion[space].next {
175 c.persistentCongestion[space].start = sent.time
176 }
177 c.persistentCongestion[space].end = sent.time
178 c.persistentCongestion[space].next = sent.num + 1
179 } else {
180
181
182
183 if sent.num == c.persistentCongestion[space].next {
184 c.persistentCongestion[space].next = sent.num + 1
185 }
186 }
187
188 if !sent.inFlight {
189 return
190 }
191 c.bytesInFlight -= sent.size
192 if sent.time.After(c.ackLastLoss) {
193 c.ackLastLoss = sent.time
194 }
195 }
196
197
198 func (c *ccReno) packetBatchEnd(now time.Time, log *slog.Logger, space numberSpace, rtt *rttState, maxAckDelay time.Duration) {
199 if logEnabled(log, QLogLevelPacket) {
200 oldState := c.state()
201 defer func() { logCongestionStateUpdated(log, oldState, c.state()) }()
202 }
203 if !c.ackLastLoss.IsZero() && !c.ackLastLoss.Before(c.recoveryStartTime) {
204
205
206 c.recoveryStartTime = now
207 c.slowStartThreshold = c.congestionWindow / 2
208 c.congestionWindow = max(c.slowStartThreshold, c.minimumCongestionWindow())
209 c.sendOnePacketInRecovery = true
210
211
212 c.congestionPendingAcks = 0
213 c.inRecovery = true
214 } else if c.congestionPendingAcks > 0 {
215
216 c.inRecovery = false
217 if c.congestionWindow < c.slowStartThreshold {
218
219
220
221 d := min(c.slowStartThreshold-c.congestionWindow, c.congestionPendingAcks)
222 c.congestionWindow += d
223 c.congestionPendingAcks -= d
224 }
225
226
227
228
229
230
231
232 for c.congestionPendingAcks > c.congestionWindow {
233 c.congestionPendingAcks -= c.congestionWindow
234 c.congestionWindow += c.maxDatagramSize
235 }
236 }
237 if !c.ackLastLoss.IsZero() {
238
239
240
241
242
243
244
245
246 const persistentCongestionThreshold = 3
247 d := (rtt.smoothedRTT + max(4*rtt.rttvar, timerGranularity) + maxAckDelay) *
248 persistentCongestionThreshold
249 start := c.persistentCongestion[space].start
250 end := c.persistentCongestion[space].end
251 if end.Sub(start) >= d {
252 c.congestionWindow = c.minimumCongestionWindow()
253 c.recoveryStartTime = time.Time{}
254 rtt.establishPersistentCongestion()
255 }
256 }
257 c.ackLastLoss = time.Time{}
258 }
259
260
261 func (c *ccReno) packetDiscarded(sent *sentPacket) {
262
263 if sent.inFlight {
264 c.bytesInFlight -= sent.size
265 }
266 }
267
268 func (c *ccReno) minimumCongestionWindow() int {
269
270 return 2 * c.maxDatagramSize
271 }
272
273 func logCongestionStateUpdated(log *slog.Logger, oldState, newState congestionState) {
274 if oldState == newState {
275 return
276 }
277 log.LogAttrs(context.Background(), QLogLevelPacket,
278 "recovery:congestion_state_updated",
279 slog.String("old", oldState.String()),
280 slog.String("new", newState.String()),
281 )
282 }
283
284 type congestionState string
285
286 func (s congestionState) String() string { return string(s) }
287
288 const (
289 congestionSlowStart = congestionState("slow_start")
290 congestionCongestionAvoidance = congestionState("congestion_avoidance")
291 congestionApplicationLimited = congestionState("application_limited")
292 congestionRecovery = congestionState("recovery")
293 )
294
295 func (c *ccReno) state() congestionState {
296 switch {
297 case c.inRecovery:
298 return congestionRecovery
299 case c.underutilized:
300 return congestionApplicationLimited
301 case c.congestionWindow < c.slowStartThreshold:
302 return congestionSlowStart
303 default:
304 return congestionCongestionAvoidance
305 }
306 }
307
View as plain text