-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathQuicStreamManager.cpp
563 lines (519 loc) · 19.7 KB
/
QuicStreamManager.cpp
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
#include "quic/state/QuicStreamManager.h"
#include <quic/state/QuicStreamUtilities.h>
namespace quic {
/**
* Updates the head of line blocked time for the stream. This should be called
* on new data received or even data being read from the stream.
* There are 2 cases when you can become head of line blocked:
* 1. You're not previously holb. You receive new data which cannot be read.
* 2. You are not head of line blocked. You read data from the stream, but you
* discover a hole.
*
* You can become not head of line blocked if the following conditions happen:
* 1. You were head of line blocked, and you receive something that allows you
* to read from the stream.
* 2. You were head of line blocked, but you receive a reset from the peer.
*/
static void updateHolBlockedTime(QuicStreamState& stream) {
// No data has arrived, or the current stream offset matches
// the stream offset that has been read so far. Stream is not HOL-blocked
// (although may be blocked on missing data).
// If there is no more data to read, or if the current read offset
// matches the read offset in the front queue, a potential HOL block
// becomes unblocked.
if (stream.readBuffer.empty() ||
(stream.currentReadOffset == stream.readBuffer.front().offset)) {
// If we were previously HOL blocked, we're not any more.
// Update the total HOLB time and reset the latch.
if (stream.lastHolbTime) {
stream.totalHolbTime +=
std::chrono::duration_cast<std::chrono::microseconds>(
Clock::now() - *stream.lastHolbTime);
stream.lastHolbTime = folly::none;
}
return;
}
// No HOL unblocking event has occured. If we are already HOL bloked,
// we remain HOL blocked.
if (stream.lastHolbTime) {
return;
}
// If we were previously not HOL blocked, we are now.
stream.lastHolbTime = Clock::now();
stream.holbCount++;
}
static bool isStreamUnopened(
StreamId streamId,
StreamId nextAcceptableStreamId) {
return streamId >= nextAcceptableStreamId;
}
// If a stream is un-opened, these automatically creates all lower streams.
// Returns false if the stream is closed or already opened.
static LocalErrorCode openPeerStreamIfNotClosed(
StreamId streamId,
folly::F14FastSet<StreamId>& openStreams,
StreamId& nextAcceptableStreamId,
StreamId maxStreamId,
std::vector<StreamId>& newStreams) {
if (streamId < nextAcceptableStreamId) {
return LocalErrorCode::CREATING_EXISTING_STREAM;
}
if (streamId >= maxStreamId) {
return LocalErrorCode::STREAM_LIMIT_EXCEEDED;
}
StreamId start = nextAcceptableStreamId;
auto numNewStreams = (streamId - start) / detail::kStreamIncrement;
openStreams.reserve(openStreams.size() + numNewStreams);
newStreams.reserve(newStreams.size() + numNewStreams);
while (start <= streamId) {
openStreams.emplace(start);
newStreams.push_back(start);
start += detail::kStreamIncrement;
}
if (streamId >= nextAcceptableStreamId) {
nextAcceptableStreamId = streamId + detail::kStreamIncrement;
}
return LocalErrorCode::NO_ERROR;
}
static LocalErrorCode openLocalStreamIfNotClosed(
StreamId streamId,
folly::F14FastSet<StreamId>& openStreams,
StreamId& nextAcceptableStreamId,
StreamId maxStreamId) {
if (streamId < nextAcceptableStreamId) {
return LocalErrorCode::CREATING_EXISTING_STREAM;
}
if (streamId >= maxStreamId) {
return LocalErrorCode::STREAM_LIMIT_EXCEEDED;
}
StreamId start = nextAcceptableStreamId;
auto numNewStreams = (streamId - start) / detail::kStreamIncrement;
openStreams.reserve(openStreams.size() + numNewStreams);
while (start <= streamId) {
openStreams.emplace(start);
start += detail::kStreamIncrement;
}
if (streamId >= nextAcceptableStreamId) {
nextAcceptableStreamId = streamId + detail::kStreamIncrement;
}
return LocalErrorCode::NO_ERROR;
}
bool QuicStreamManager::streamExists(StreamId streamId) {
if (isLocalStream(nodeType_, streamId)) {
if (isUnidirectionalStream(streamId)) {
return openUnidirectionalLocalStreams_.count(streamId) > 0;
} else {
return openBidirectionalLocalStreams_.count(streamId) > 0;
}
} else {
if (isUnidirectionalStream(streamId)) {
return openUnidirectionalPeerStreams_.count(streamId) > 0;
} else {
return openBidirectionalPeerStreams_.count(streamId) > 0;
}
}
}
QuicStreamState* QuicStreamManager::findStream(StreamId streamId) {
auto lookup = streams_.find(streamId);
if (lookup == streams_.end()) {
return nullptr;
} else {
return &lookup->second;
}
}
void QuicStreamManager::setMaxLocalBidirectionalStreams(
uint64_t maxStreams,
bool force) {
if (maxStreams > kMaxMaxStreams) {
throw QuicTransportException(
"Attempt to set maxStreams beyond the max allowed.",
TransportErrorCode::STREAM_LIMIT_ERROR);
}
StreamId maxStreamId = maxStreams * detail::kStreamIncrement +
initialLocalBidirectionalStreamId_;
if (force || maxStreamId > maxLocalBidirectionalStreamId_) {
maxLocalBidirectionalStreamId_ = maxStreamId;
maxLocalBidirectionalStreamIdIncreased_ = true;
}
}
void QuicStreamManager::setMaxLocalUnidirectionalStreams(
uint64_t maxStreams,
bool force) {
if (maxStreams > kMaxMaxStreams) {
throw QuicTransportException(
"Attempt to set maxStreams beyond the max allowed.",
TransportErrorCode::STREAM_LIMIT_ERROR);
}
StreamId maxStreamId = maxStreams * detail::kStreamIncrement +
initialLocalUnidirectionalStreamId_;
if (force || maxStreamId > maxLocalUnidirectionalStreamId_) {
maxLocalUnidirectionalStreamId_ = maxStreamId;
maxLocalUnidirectionalStreamIdIncreased_ = true;
}
}
void QuicStreamManager::setMaxRemoteBidirectionalStreams(uint64_t maxStreams) {
setMaxRemoteBidirectionalStreamsInternal(maxStreams, false);
}
void QuicStreamManager::setMaxRemoteUnidirectionalStreams(uint64_t maxStreams) {
setMaxRemoteUnidirectionalStreamsInternal(maxStreams, false);
}
void QuicStreamManager::setMaxRemoteBidirectionalStreamsInternal(
uint64_t maxStreams,
bool force) {
if (maxStreams > kMaxMaxStreams) {
throw QuicTransportException(
"Attempt to set maxStreams beyond the max allowed.",
TransportErrorCode::STREAM_LIMIT_ERROR);
}
StreamId maxStreamId = maxStreams * detail::kStreamIncrement +
initialRemoteBidirectionalStreamId_;
if (force || maxStreamId > maxRemoteBidirectionalStreamId_) {
maxRemoteBidirectionalStreamId_ = maxStreamId;
}
}
void QuicStreamManager::setMaxRemoteUnidirectionalStreamsInternal(
uint64_t maxStreams,
bool force) {
if (maxStreams > kMaxMaxStreams) {
throw QuicTransportException(
"Attempt to set maxStreams beyond the max allowed.",
TransportErrorCode::STREAM_LIMIT_ERROR);
}
StreamId maxStreamId = maxStreams * detail::kStreamIncrement +
initialRemoteUnidirectionalStreamId_;
if (force || maxStreamId > maxRemoteUnidirectionalStreamId_) {
maxRemoteUnidirectionalStreamId_ = maxStreamId;
}
}
bool QuicStreamManager::consumeMaxLocalBidirectionalStreamIdIncreased() {
bool res = maxLocalBidirectionalStreamIdIncreased_;
maxLocalBidirectionalStreamIdIncreased_ = false;
return res;
}
bool QuicStreamManager::consumeMaxLocalUnidirectionalStreamIdIncreased() {
bool res = maxLocalUnidirectionalStreamIdIncreased_;
maxLocalUnidirectionalStreamIdIncreased_ = false;
return res;
}
bool QuicStreamManager::setStreamPriority(
StreamId id,
PriorityLevel level,
bool incremental) {
auto stream = findStream(id);
if (stream) {
Priority newPriority(level, incremental);
if (stream->priority == newPriority) {
return false;
}
stream->priority = newPriority;
// If this stream is already in the writable or loss queus, update the
// priority there.
writableStreams_.updateIfExist(id, stream->priority);
lossStreams_.updateIfExist(id, stream->priority);
return true;
}
return false;
}
void QuicStreamManager::refreshTransportSettings(
const TransportSettings& settings) {
transportSettings_ = &settings;
setMaxRemoteBidirectionalStreamsInternal(
transportSettings_->advertisedInitialMaxStreamsBidi, true);
setMaxRemoteUnidirectionalStreamsInternal(
transportSettings_->advertisedInitialMaxStreamsUni, true);
}
// We create local streams lazily. If a local stream was created
// but not allocated yet, this will allocate a stream.
// This will return nullptr if a stream is closed or un-opened.
QuicStreamState* FOLLY_NULLABLE
QuicStreamManager::getOrCreateOpenedLocalStream(StreamId streamId) {
auto& openLocalStreams = isUnidirectionalStream(streamId)
? openUnidirectionalLocalStreams_
: openBidirectionalLocalStreams_;
if (openLocalStreams.count(streamId)) {
// Open a lazily created stream.
auto it = streams_.emplace(
std::piecewise_construct,
std::forward_as_tuple(streamId),
std::forward_as_tuple(streamId, conn_));
QUIC_STATS(conn_.statsCallback, onNewQuicStream);
if (!it.second) {
throw QuicTransportException(
"Creating an active stream", TransportErrorCode::STREAM_STATE_ERROR);
}
return &it.first->second;
}
return nullptr;
}
QuicStreamState* QuicStreamManager::getStream(StreamId streamId) {
if (isRemoteStream(nodeType_, streamId)) {
auto stream = getOrCreatePeerStream(streamId);
updateAppIdleState();
return stream;
}
auto it = streams_.find(streamId);
if (it != streams_.end()) {
return &it->second;
}
auto stream = getOrCreateOpenedLocalStream(streamId);
auto nextAcceptableStreamId = isUnidirectionalStream(streamId)
? nextAcceptableLocalUnidirectionalStreamId_
: nextAcceptableLocalBidirectionalStreamId_;
if (!stream && isStreamUnopened(streamId, nextAcceptableStreamId)) {
throw QuicTransportException(
"Trying to get unopened local stream",
TransportErrorCode::STREAM_STATE_ERROR);
}
updateAppIdleState();
return stream;
}
folly::Expected<QuicStreamState*, LocalErrorCode>
QuicStreamManager::createNextBidirectionalStream() {
auto stream = createStream(nextBidirectionalStreamId_);
if (stream.hasValue()) {
nextBidirectionalStreamId_ += detail::kStreamIncrement;
}
return stream;
}
folly::Expected<QuicStreamState*, LocalErrorCode>
QuicStreamManager::createNextUnidirectionalStream() {
auto stream = createStream(nextUnidirectionalStreamId_);
if (stream.hasValue()) {
nextUnidirectionalStreamId_ += detail::kStreamIncrement;
}
return stream;
}
QuicStreamState* FOLLY_NULLABLE
QuicStreamManager::getOrCreatePeerStream(StreamId streamId) {
// This function maintains 3 invariants:
// 1. Streams below nextAcceptableStreamId are streams that have been
// seen before. Everything above can be opened.
// 2. Streams that have been seen before, always have an entry in
// openPeerStreams. If a stream below nextAcceptableStreamId does not
// have an entry in openPeerStreams, then it is closed.
// 3. If streamId n is open all streams < n will be seen.
// It also tries to create the entire state for a stream in a lazy manner.
// Validate the stream id is correct
if (nodeType_ == QuicNodeType::Client && isClientStream(streamId)) {
throw QuicTransportException(
"Attempted getting client peer stream on client",
TransportErrorCode::STREAM_STATE_ERROR);
} else if (nodeType_ == QuicNodeType::Server && isServerStream(streamId)) {
throw QuicTransportException(
"Attempted getting server peer stream on server",
TransportErrorCode::STREAM_STATE_ERROR);
} else if (!isClientStream(streamId) && !isServerStream(streamId)) {
throw QuicTransportException(
"Invalid stream", TransportErrorCode::STREAM_STATE_ERROR);
}
// TODO when we can rely on C++17, this is a good candidate for try_emplace.
auto peerStream = streams_.find(streamId);
if (peerStream != streams_.end()) {
return &peerStream->second;
}
auto& openPeerStreams = isUnidirectionalStream(streamId)
? openUnidirectionalPeerStreams_
: openBidirectionalPeerStreams_;
if (openPeerStreams.count(streamId)) {
// Stream was already open, create the state for it lazily.
auto it = streams_.emplace(
std::piecewise_construct,
std::forward_as_tuple(streamId),
std::forward_as_tuple(streamId, conn_));
QUIC_STATS(conn_.statsCallback, onNewQuicStream);
return &it.first->second;
}
auto& nextAcceptableStreamId = isUnidirectionalStream(streamId)
? nextAcceptablePeerUnidirectionalStreamId_
: nextAcceptablePeerBidirectionalStreamId_;
auto maxStreamId = isUnidirectionalStream(streamId)
? maxRemoteUnidirectionalStreamId_
: maxRemoteBidirectionalStreamId_;
auto openedResult = openPeerStreamIfNotClosed(
streamId,
openPeerStreams,
nextAcceptableStreamId,
maxStreamId,
newPeerStreams_);
if (openedResult == LocalErrorCode::CREATING_EXISTING_STREAM) {
// Stream could be closed here.
return nullptr;
} else if (openedResult == LocalErrorCode::STREAM_LIMIT_EXCEEDED) {
throw QuicTransportException(
"Exceeded stream limit.", TransportErrorCode::STREAM_LIMIT_ERROR);
}
auto it = streams_.emplace(
std::piecewise_construct,
std::forward_as_tuple(streamId),
std::forward_as_tuple(streamId, conn_));
QUIC_STATS(conn_.statsCallback, onNewQuicStream);
return &it.first->second;
}
folly::Expected<QuicStreamState*, LocalErrorCode>
QuicStreamManager::createStream(StreamId streamId) {
if (nodeType_ == QuicNodeType::Client && !isClientStream(streamId)) {
throw QuicTransportException(
"Attempted creating non-client stream on client",
TransportErrorCode::STREAM_STATE_ERROR);
} else if (nodeType_ == QuicNodeType::Server && !isServerStream(streamId)) {
throw QuicTransportException(
"Attempted creating non-server stream on server",
TransportErrorCode::STREAM_STATE_ERROR);
}
auto existingStream = getOrCreateOpenedLocalStream(streamId);
if (existingStream) {
return existingStream;
}
bool isUni = isUnidirectionalStream(streamId);
auto& nextAcceptableStreamId = isUni
? nextAcceptableLocalUnidirectionalStreamId_
: nextAcceptableLocalBidirectionalStreamId_;
auto maxStreamId =
isUni ? maxLocalUnidirectionalStreamId_ : maxLocalBidirectionalStreamId_;
auto& openLocalStreams =
isUni ? openUnidirectionalLocalStreams_ : openBidirectionalLocalStreams_;
auto openedResult = openLocalStreamIfNotClosed(
streamId, openLocalStreams, nextAcceptableStreamId, maxStreamId);
if (openedResult != LocalErrorCode::NO_ERROR) {
return folly::makeUnexpected(openedResult);
}
auto it = streams_.emplace(
std::piecewise_construct,
std::forward_as_tuple(streamId),
std::forward_as_tuple(streamId, conn_));
QUIC_STATS(conn_.statsCallback, onNewQuicStream);
updateAppIdleState();
return &it.first->second;
}
void QuicStreamManager::removeClosedStream(StreamId streamId) {
auto it = streams_.find(streamId);
if (it == streams_.end()) {
VLOG(10) << "Trying to remove already closed stream=" << streamId;
return;
}
VLOG(10) << "Removing closed stream=" << streamId;
DCHECK(it->second.inTerminalStates());
readableStreams_.erase(streamId);
peekableStreams_.erase(streamId);
writableStreams_.erase(streamId);
writableControlStreams_.erase(streamId);
blockedStreams_.erase(streamId);
deliverableStreams_.erase(streamId);
txStreams_.erase(streamId);
windowUpdates_.erase(streamId);
lossStreams_.erase(streamId);
stopSendingStreams_.erase(streamId);
flowControlUpdated_.erase(streamId);
dataRejectedStreams_.erase(streamId);
dataExpiredStreams_.erase(streamId);
if (it->second.isControl) {
DCHECK_GT(numControlStreams_, 0);
numControlStreams_--;
}
streams_.erase(it);
QUIC_STATS(conn_.statsCallback, onQuicStreamClosed);
if (isRemoteStream(nodeType_, streamId)) {
auto& openPeerStreams = isUnidirectionalStream(streamId)
? openUnidirectionalPeerStreams_
: openBidirectionalPeerStreams_;
openPeerStreams.erase(streamId);
// Check if we should send a stream limit update. We need to send an
// update every time we've closed a number of streams >= the set windowing
// fraction.
uint64_t initialStreamLimit = isUnidirectionalStream(streamId)
? transportSettings_->advertisedInitialMaxStreamsUni
: transportSettings_->advertisedInitialMaxStreamsBidi;
uint64_t streamWindow = initialStreamLimit / streamLimitWindowingFraction_;
uint64_t openableRemoteStreams = isUnidirectionalStream(streamId)
? openableRemoteUnidirectionalStreams()
: openableRemoteBidirectionalStreams();
// The "credit" here is how much available stream space we have based on
// what the initial stream limit was set to.
uint64_t streamCredit =
initialStreamLimit - openableRemoteStreams - openPeerStreams.size();
if (streamCredit >= streamWindow) {
if (isUnidirectionalStream(streamId)) {
uint64_t maxStreams = (maxRemoteUnidirectionalStreamId_ -
initialRemoteUnidirectionalStreamId_) /
detail::kStreamIncrement;
setMaxRemoteUnidirectionalStreams(maxStreams + streamCredit);
remoteUnidirectionalStreamLimitUpdate_ = maxStreams + streamCredit;
} else {
uint64_t maxStreams = (maxRemoteBidirectionalStreamId_ -
initialRemoteBidirectionalStreamId_) /
detail::kStreamIncrement;
setMaxRemoteBidirectionalStreams(maxStreams + streamCredit);
remoteBidirectionalStreamLimitUpdate_ = maxStreams + streamCredit;
}
}
} else {
auto& openLocalStreams = isUnidirectionalStream(streamId)
? openUnidirectionalLocalStreams_
: openBidirectionalLocalStreams_;
openLocalStreams.erase(streamId);
}
updateAppIdleState();
}
void QuicStreamManager::updateLossStreams(QuicStreamState& stream) {
if (stream.lossBuffer.empty()) {
// No-op if not present
lossStreams_.erase(stream.id);
} else {
// No-op if already inserted
lossStreams_.insertOrUpdate(stream.id, stream.priority);
}
}
void QuicStreamManager::updateReadableStreams(QuicStreamState& stream) {
updateHolBlockedTime(stream);
if (stream.hasReadableData() || stream.streamReadError.has_value()) {
readableStreams_.emplace(stream.id);
} else {
readableStreams_.erase(stream.id);
}
}
void QuicStreamManager::updateWritableStreams(QuicStreamState& stream) {
if (stream.hasWritableData() && !stream.streamWriteError.has_value()) {
stream.conn.streamManager->addWritable(stream);
} else {
stream.conn.streamManager->removeWritable(stream);
}
}
void QuicStreamManager::updatePeekableStreams(QuicStreamState& stream) {
if (stream.hasPeekableData() && !stream.streamReadError.has_value()) {
peekableStreams_.emplace(stream.id);
} else {
peekableStreams_.erase(stream.id);
}
}
void QuicStreamManager::updateAppIdleState() {
bool currentNonCtrlStreams = hasNonCtrlStreams();
if (isAppIdle_ && !currentNonCtrlStreams) {
// We were app limited, and we continue to be app limited.
return;
} else if (!isAppIdle_ && currentNonCtrlStreams) {
// We were not app limited, and we continue to be not app limited.
return;
}
isAppIdle_ = !currentNonCtrlStreams;
if (conn_.congestionController) {
conn_.congestionController->setAppIdle(isAppIdle_, Clock::now());
}
}
void QuicStreamManager::setStreamAsControl(QuicStreamState& stream) {
if (!stream.isControl) {
stream.isControl = true;
numControlStreams_++;
}
updateAppIdleState();
}
bool QuicStreamManager::isAppIdle() const {
return isAppIdle_;
}
} // namespace quic