-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathframer.go
More file actions
484 lines (433 loc) · 14.8 KB
/
Copy pathframer.go
File metadata and controls
484 lines (433 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package quic
import (
"slices"
"sync"
"github.com/quic-go/quic-go/internal/ackhandler"
"github.com/quic-go/quic-go/internal/monotime"
"github.com/quic-go/quic-go/internal/protocol"
"github.com/quic-go/quic-go/internal/utils/minheap"
"github.com/quic-go/quic-go/internal/utils/ringbuffer"
"github.com/quic-go/quic-go/internal/wire"
"github.com/quic-go/quic-go/quicvarint"
)
const (
maxPathResponses = 256
maxControlFrames = 16 << 10
)
// This is the largest possible size of a stream-related control frame
// (which is the RESET_STREAM frame).
const maxStreamControlFrameSize = 25
type streamFrameGetter interface {
priority() (urgency int8, incremental bool, generation uint32)
popStreamFrame(protocol.ByteCount, protocol.Version) (_ ackhandler.StreamFrame, _ *wire.StreamDataBlockedFrame, hasMoreData bool)
popRetransmissionFrame(protocol.ByteCount, protocol.Version) (ackhandler.StreamFrame, bool)
}
type streamControlFrameGetter interface {
getControlFrame(monotime.Time) (_ ackhandler.Frame, ok, hasMore bool)
}
// streamQueueEntry captures the stream's generation when it is queued.
// A mismatch with the current generation identifies an entry left behind by a priority change.
type streamQueueEntry struct {
id protocol.StreamID
generation uint32
}
// queuedStream tracks the latest generation added to a scheduling queue,
// preventing duplicate and out-of-order notifications from queueing it again.
type queuedStream struct {
streamFrameGetter
generation uint32
}
type framer struct {
mutex sync.Mutex
activeStreams map[protocol.StreamID]queuedStream
retransmissionStreams map[protocol.StreamID]streamFrameGetter
incrementalStreams [8]ringbuffer.RingBuffer[streamQueueEntry]
nonIncrementalStreams [8]minheap.Heap[protocol.StreamID, uint32 /* generation */]
// If an urgency level contains both incremental and non-incremental streams,
// we round-robin between incremental and non-incremental streams.
lastSendWasIncremental [8]bool
streamsWithControlFrames map[protocol.StreamID]streamControlFrameGetter
// Retransmissions are not incremental: repair all lost data for the first queued stream.
// New losses extend its batch, so A, B, A is repaired as A, A, B, delaying B.
// The ring buffer provides FIFO scheduling while reusing its storage.
retransmissionQueue [8]ringbuffer.RingBuffer[protocol.StreamID]
controlFrameMutex sync.Mutex
controlFrames []wire.Frame
pathResponses []*wire.PathResponseFrame
connFlowController *connectionFlowController
queuedTooManyControlFrames bool
}
func newFramer(connFlowController *connectionFlowController) *framer {
return &framer{
activeStreams: make(map[protocol.StreamID]queuedStream),
retransmissionStreams: make(map[protocol.StreamID]streamFrameGetter),
streamsWithControlFrames: make(map[protocol.StreamID]streamControlFrameGetter),
connFlowController: connFlowController,
}
}
func (f *framer) HasData() bool {
f.controlFrameMutex.Lock()
hasControlFrames := len(f.streamsWithControlFrames) > 0 || len(f.controlFrames) > 0 || len(f.pathResponses) > 0
f.controlFrameMutex.Unlock()
if hasControlFrames {
return true
}
f.mutex.Lock()
defer f.mutex.Unlock()
for urgency := range f.incrementalStreams {
if !f.incrementalStreams[urgency].Empty() || !f.nonIncrementalStreams[urgency].Empty() ||
!f.retransmissionQueue[urgency].Empty() {
return true
}
}
return false
}
func (f *framer) QueueControlFrame(frame wire.Frame) {
f.controlFrameMutex.Lock()
defer f.controlFrameMutex.Unlock()
if pr, ok := frame.(*wire.PathResponseFrame); ok {
// Only queue up to maxPathResponses PATH_RESPONSE frames.
// This limit should be high enough to never be hit in practice,
// unless the peer is doing something malicious.
if len(f.pathResponses) >= maxPathResponses {
return
}
f.pathResponses = append(f.pathResponses, pr)
return
}
// This is a hack.
if len(f.controlFrames) >= maxControlFrames {
f.queuedTooManyControlFrames = true
return
}
f.controlFrames = append(f.controlFrames, frame)
}
func (f *framer) Append(
frames []ackhandler.Frame,
streamFrames []ackhandler.StreamFrame,
maxLen protocol.ByteCount,
now monotime.Time,
v protocol.Version,
) ([]ackhandler.Frame, []ackhandler.StreamFrame, protocol.ByteCount) {
f.controlFrameMutex.Lock()
frames, controlFrameLen := f.appendControlFrames(frames, maxLen, now, v)
maxLen -= controlFrameLen
var lastFrame ackhandler.StreamFrame
var streamFrameLen protocol.ByteCount
f.mutex.Lock()
// retransmit all lost STREAM data before sending new STREAM data
retransmissions:
for urgency := range f.retransmissionQueue {
bucket := &f.retransmissionQueue[urgency]
for !bucket.Empty() && protocol.MinStreamFrameSize <= maxLen {
id := bucket.PeekFront()
str, ok := f.retransmissionStreams[id]
if !ok {
bucket.PopFront()
continue
}
currentUrgency, _, _ := str.priority()
if currentUrgency != int8(urgency) {
bucket.PopFront()
f.retransmissionQueue[currentUrgency].PushBack(id)
continue
}
// For the last STREAM frame, we'll remove the DataLen field later.
frameMaxLen := maxLen + protocol.ByteCount(quicvarint.Len(uint64(maxLen)))
sf, hasMoreRetransmissions := str.popRetransmissionFrame(frameMaxLen, v)
if !hasMoreRetransmissions {
bucket.PopFront()
delete(f.retransmissionStreams, id)
}
if sf.Frame == nil {
// If the retransmission didn't fit, retry it in the next packet.
if hasMoreRetransmissions {
break retransmissions
}
continue
}
streamFrames = append(streamFrames, sf)
maxLen -= sf.Frame.Length(v)
lastFrame = sf
streamFrameLen += sf.Frame.Length(v)
}
}
// pop STREAM frames, until less than 128 bytes are left in the packet
for urgency := range f.incrementalStreams {
numActiveStreams := f.incrementalStreams[urgency].Len() + f.nonIncrementalStreams[urgency].Len()
for range numActiveStreams {
if protocol.MinStreamFrameSize > maxLen {
break
}
sf, blocked := f.getNextStreamFrame(maxLen, int8(urgency), v)
if sf.Frame != nil {
streamFrames = append(streamFrames, sf)
maxLen -= sf.Frame.Length(v)
lastFrame = sf
streamFrameLen += sf.Frame.Length(v)
}
// If the stream just became blocked on stream flow control, attempt to pack the
// STREAM_DATA_BLOCKED into the same packet.
if blocked != nil {
l := blocked.Length(v)
// In case it doesn't fit, queue it for the next packet.
if maxLen < l {
f.controlFrames = append(f.controlFrames, blocked)
break
}
frames = append(frames, ackhandler.Frame{Frame: blocked})
maxLen -= l
controlFrameLen += l
}
}
}
// The only way to become blocked on connection-level flow control is by sending STREAM frames.
if isBlocked, offset := f.connFlowController.IsNewlyBlocked(); isBlocked {
blocked := &wire.DataBlockedFrame{MaximumData: offset}
l := blocked.Length(v)
// In case it doesn't fit, queue it for the next packet.
if maxLen >= l {
frames = append(frames, ackhandler.Frame{Frame: blocked})
controlFrameLen += l
} else {
f.controlFrames = append(f.controlFrames, blocked)
}
}
f.mutex.Unlock()
f.controlFrameMutex.Unlock()
if lastFrame.Frame != nil {
// account for the smaller size of the last STREAM frame
streamFrameLen -= lastFrame.Frame.Length(v)
lastFrame.Frame.DataLenPresent = false
streamFrameLen += lastFrame.Frame.Length(v)
}
return frames, streamFrames, controlFrameLen + streamFrameLen
}
func (f *framer) appendControlFrames(
frames []ackhandler.Frame,
maxLen protocol.ByteCount,
now monotime.Time,
v protocol.Version,
) ([]ackhandler.Frame, protocol.ByteCount) {
var length protocol.ByteCount
// add a PATH_RESPONSE first, but only pack a single PATH_RESPONSE per packet
if len(f.pathResponses) > 0 {
frame := f.pathResponses[0]
frameLen := frame.Length(v)
if frameLen <= maxLen {
frames = append(frames, ackhandler.Frame{Frame: frame})
length += frameLen
f.pathResponses = f.pathResponses[1:]
}
}
// add stream-related control frames
for id, str := range f.streamsWithControlFrames {
start:
remainingLen := maxLen - length
if remainingLen <= maxStreamControlFrameSize {
break
}
fr, ok, hasMore := str.getControlFrame(now)
if !hasMore {
delete(f.streamsWithControlFrames, id)
}
if !ok {
continue
}
frames = append(frames, fr)
length += fr.Frame.Length(v)
if hasMore {
// It is rare that a stream has more than one control frame to queue.
// We don't want to spawn another loop for just to cover that case.
goto start
}
}
for len(f.controlFrames) > 0 {
frame := f.controlFrames[len(f.controlFrames)-1]
frameLen := frame.Length(v)
if length+frameLen > maxLen {
break
}
frames = append(frames, ackhandler.Frame{Frame: frame})
length += frameLen
f.controlFrames = f.controlFrames[:len(f.controlFrames)-1]
}
return frames, length
}
// QueuedTooManyControlFrames says if the control frame queue exceeded its maximum queue length.
// This is a hack.
// It is easier to implement than propagating an error return value in QueueControlFrame.
// The correct solution would be to queue frames with their respective structs.
// See https://github.com/quic-go/quic-go/issues/4271 for the queueing of stream-related control frames.
func (f *framer) QueuedTooManyControlFrames() bool {
return f.queuedTooManyControlFrames
}
func (f *framer) AddActiveStream(id protocol.StreamID, str streamFrameGetter) {
f.mutex.Lock()
defer f.mutex.Unlock()
urgency, incremental, generation := str.priority()
if activeStr, ok := f.activeStreams[id]; ok && activeStr.generation == generation {
return
}
if incremental {
f.incrementalStreams[urgency].PushBack(streamQueueEntry{id: id, generation: generation})
} else {
f.nonIncrementalStreams[urgency].Push(id, generation)
}
f.activeStreams[id] = queuedStream{streamFrameGetter: str, generation: generation}
}
func (f *framer) AddStreamWithRetransmission(id protocol.StreamID, str streamFrameGetter) {
f.mutex.Lock()
defer f.mutex.Unlock()
urgency, _, _ := str.priority()
if _, ok := f.retransmissionStreams[id]; ok {
return
}
f.retransmissionQueue[urgency].PushBack(id)
f.retransmissionStreams[id] = str
}
func (f *framer) AddStreamWithControlFrames(id protocol.StreamID, str streamControlFrameGetter) {
f.controlFrameMutex.Lock()
defer f.controlFrameMutex.Unlock()
if _, ok := f.streamsWithControlFrames[id]; !ok {
f.streamsWithControlFrames[id] = str
}
}
// RemoveActiveStream is called when a stream completes.
func (f *framer) RemoveActiveStream(id protocol.StreamID) {
f.mutex.Lock()
defer f.mutex.Unlock()
// We don't delete the stream from the ring buffers and heaps,
// since we'd have to find it there first.
// Instead, we check if the stream is still in active when appending STREAM frames.
delete(f.activeStreams, id)
delete(f.retransmissionStreams, id)
}
func (f *framer) UpdateStreamPriority(id protocol.StreamID) {
f.mutex.Lock()
defer f.mutex.Unlock()
str, ok := f.activeStreams[id]
if !ok {
return
}
urgency, incremental, generation := str.priority()
if str.generation != generation {
// Leave the old queue entry in place. It will be discarded when it reaches
// the front of that queue and no longer matches the stream's generation.
if incremental {
f.incrementalStreams[urgency].PushBack(streamQueueEntry{id: id, generation: generation})
} else {
f.nonIncrementalStreams[urgency].Push(id, generation)
}
str.generation = generation
f.activeStreams[id] = str
}
}
func (f *framer) getNextStreamFrame(
maxLen protocol.ByteCount,
urgency int8,
v protocol.Version,
) (ackhandler.StreamFrame, *wire.StreamDataBlockedFrame) {
if f.nonIncrementalStreams[urgency].Empty() || (!f.lastSendWasIncremental[urgency] && !f.incrementalStreams[urgency].Empty()) {
return f.getNextIncrementalStreamFrame(maxLen, urgency, v)
}
return f.getNextNonIncrementalStreamFrame(maxLen, urgency, v)
}
func (f *framer) getNextIncrementalStreamFrame(
maxLen protocol.ByteCount,
urgency int8,
v protocol.Version,
) (ackhandler.StreamFrame, *wire.StreamDataBlockedFrame) {
queue := &f.incrementalStreams[urgency]
if queue.Empty() {
return ackhandler.StreamFrame{}, nil
}
entry := queue.PopFront()
str, ok := f.activeStreams[entry.id]
if !ok {
return ackhandler.StreamFrame{}, nil
}
_, _, generation := str.priority()
if generation != entry.generation {
return ackhandler.StreamFrame{}, nil
}
frame, blocked, hasMoreData := f.popStreamFrame(entry.id, str, maxLen, v)
if hasMoreData {
queue.PushBack(entry)
}
f.lastSendWasIncremental[urgency] = true
return frame, blocked
}
func (f *framer) getNextNonIncrementalStreamFrame(
maxLen protocol.ByteCount,
urgency int8,
v protocol.Version,
) (ackhandler.StreamFrame, *wire.StreamDataBlockedFrame) {
queue := &f.nonIncrementalStreams[urgency]
if queue.Empty() {
return ackhandler.StreamFrame{}, nil
}
id, queuedGeneration := queue.Peek()
str, ok := f.activeStreams[id]
if !ok {
queue.Pop()
return ackhandler.StreamFrame{}, nil
}
_, _, generation := str.priority()
if generation != queuedGeneration {
queue.Pop()
return ackhandler.StreamFrame{}, nil
}
frame, blocked, hasMoreData := f.popStreamFrame(id, str, maxLen, v)
if !hasMoreData {
queue.Pop()
}
f.lastSendWasIncremental[urgency] = false
return frame, blocked
}
func (f *framer) popStreamFrame(
id protocol.StreamID,
str queuedStream,
maxLen protocol.ByteCount,
v protocol.Version,
) (ackhandler.StreamFrame, *wire.StreamDataBlockedFrame, bool) {
// For the last STREAM frame, we'll remove the DataLen field later.
// Therefore, we can pretend to have more bytes available when popping
// the STREAM frame (which will always have the DataLen set).
maxLen += protocol.ByteCount(quicvarint.Len(uint64(maxLen)))
frame, blocked, hasMoreData := str.popStreamFrame(maxLen, v)
if !hasMoreData {
delete(f.activeStreams, id)
}
// Note that the frame.Frame can be nil:
// * if the stream was canceled after it said it had data
// * the remaining size doesn't allow us to add another STREAM frame
return frame, blocked, hasMoreData
}
func (f *framer) Handle0RTTRejection() {
f.mutex.Lock()
defer f.mutex.Unlock()
f.controlFrameMutex.Lock()
defer f.controlFrameMutex.Unlock()
for urgency := range f.incrementalStreams {
f.incrementalStreams[urgency].Clear()
f.nonIncrementalStreams[urgency].Clear()
f.lastSendWasIncremental[urgency] = false
f.retransmissionQueue[urgency].Clear()
}
clear(f.activeStreams)
clear(f.retransmissionStreams)
clear(f.streamsWithControlFrames)
var j int
for i, frame := range f.controlFrames {
switch frame.(type) {
case *wire.MaxDataFrame, *wire.MaxStreamDataFrame, *wire.MaxStreamsFrame,
*wire.DataBlockedFrame, *wire.StreamDataBlockedFrame, *wire.StreamsBlockedFrame:
continue
default:
f.controlFrames[j] = f.controlFrames[i]
j++
}
}
f.controlFrames = slices.Delete(f.controlFrames, j, len(f.controlFrames))
}