QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | #include "net/third_party/quiche/src/quic/core/quic_stream_id_manager.h" |
| 5 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 6 | #include <string> |
| 7 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8 | #include "net/third_party/quiche/src/quic/core/quic_connection.h" |
| 9 | #include "net/third_party/quiche/src/quic/core/quic_constants.h" |
| 10 | #include "net/third_party/quiche/src/quic/core/quic_session.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/common/platform/api/quiche_str_cat.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | |
| 17 | namespace quic { |
| 18 | |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 19 | #define ENDPOINT \ |
| 20 | (perspective_ == Perspective::IS_SERVER ? " Server: " : " Client: ") |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 21 | |
| 22 | QuicStreamIdManager::QuicStreamIdManager( |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 23 | DelegateInterface* delegate, |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 24 | bool unidirectional, |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 25 | Perspective perspective, |
| 26 | QuicTransportVersion transport_version, |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 27 | QuicStreamCount max_allowed_outgoing_streams, |
| 28 | QuicStreamCount max_allowed_incoming_streams) |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 29 | : delegate_(delegate), |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 30 | unidirectional_(unidirectional), |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 31 | perspective_(perspective), |
| 32 | transport_version_(transport_version), |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 33 | is_config_negotiated_(false), |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 34 | outgoing_max_streams_(max_allowed_outgoing_streams), |
| 35 | next_outgoing_stream_id_(GetFirstOutgoingStreamId()), |
| 36 | outgoing_stream_count_(0), |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 37 | incoming_actual_max_streams_(max_allowed_incoming_streams), |
| 38 | // Advertised max starts at actual because it's communicated in the |
| 39 | // handshake. |
| 40 | incoming_advertised_max_streams_(max_allowed_incoming_streams), |
| 41 | incoming_initial_max_open_streams_(max_allowed_incoming_streams), |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 42 | incoming_stream_count_(0), |
| 43 | largest_peer_created_stream_id_( |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 44 | QuicUtils::GetInvalidStreamId(transport_version)), |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 45 | max_streams_window_(0), |
| 46 | pending_max_streams_(false), |
| 47 | pending_streams_blocked_( |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 48 | QuicUtils::GetInvalidStreamId(transport_version)) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 49 | CalculateIncomingMaxStreamsWindow(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 50 | } |
| 51 | |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 52 | QuicStreamIdManager::~QuicStreamIdManager() {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 53 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 54 | bool QuicStreamIdManager::OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame) { |
| 55 | // Ensure that the frame has the correct directionality. |
| 56 | DCHECK_EQ(frame.unidirectional, unidirectional_); |
| 57 | QUIC_CODE_COUNT_N(quic_max_streams_received, 2, 2); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 58 | |
| 59 | // Set the limit to be exactly the stream count in the frame. |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 60 | // Also informs the higher layers that they can create more |
| 61 | // streams if the limit is increased. |
| 62 | return SetMaxOpenOutgoingStreams(frame.stream_count); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 63 | } |
| 64 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 65 | // The peer sends a streams blocked frame when it can not open any more |
| 66 | // streams because it has runs into the limit. |
| 67 | bool QuicStreamIdManager::OnStreamsBlockedFrame( |
| 68 | const QuicStreamsBlockedFrame& frame) { |
| 69 | // Ensure that the frame has the correct directionality. |
| 70 | DCHECK_EQ(frame.unidirectional, unidirectional_); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 71 | if (frame.stream_count > incoming_advertised_max_streams_) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 72 | // Peer thinks it can send more streams that we've told it. |
| 73 | // This is a protocol error. |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 74 | QUIC_CODE_COUNT(quic_streams_blocked_too_big); |
renjietang | ff4b2b6 | 2020-02-12 16:52:32 -0800 | [diff] [blame] | 75 | delegate_->OnStreamIdManagerError(QUIC_STREAMS_BLOCKED_ERROR, |
| 76 | "Invalid stream count specified"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 77 | return false; |
| 78 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 79 | if (frame.stream_count < incoming_actual_max_streams_) { |
| 80 | // Peer thinks it's blocked on a stream count that is less than our current |
| 81 | // max. Inform the peer of the correct stream count. Sending a MAX_STREAMS |
| 82 | // frame in this case is not controlled by the window. |
| 83 | SendMaxStreamsFrame(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 84 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 85 | QUIC_CODE_COUNT(quic_streams_blocked_id_correct); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 89 | // Used when configuration has been done and we have an initial |
| 90 | // maximum stream count from the peer. |
renjietang | 52e1338 | 2019-12-16 15:58:04 -0800 | [diff] [blame] | 91 | bool QuicStreamIdManager::SetMaxOpenOutgoingStreams( |
| 92 | QuicStreamCount max_open_streams) { |
renjietang | 315428e | 2020-01-27 10:18:12 -0800 | [diff] [blame] | 93 | if (max_open_streams <= outgoing_max_streams_) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 94 | // Only update the stream count if it would increase the limit. |
| 95 | // If it decreases the limit, or doesn't change it, then do not update. |
| 96 | // Note that this handles the case of receiving a count of 0 in the frame |
| 97 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 98 | } |
| 99 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 100 | // This implementation only supports 32 bit Stream IDs, so limit max streams |
| 101 | // if it would exceed the max 32 bits can express. |
renjietang | 52e1338 | 2019-12-16 15:58:04 -0800 | [diff] [blame] | 102 | outgoing_max_streams_ = |
| 103 | std::min(max_open_streams, |
| 104 | QuicUtils::GetMaxStreamCount(unidirectional_, perspective_)); |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 105 | |
| 106 | // Inform the higher layers that the stream limit has increased and that |
| 107 | // new streams may be created. |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 108 | delegate_->OnCanCreateNewOutgoingStream(unidirectional_); |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 109 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 110 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 111 | } |
| 112 | |
renjietang | 52e1338 | 2019-12-16 15:58:04 -0800 | [diff] [blame] | 113 | void QuicStreamIdManager::SetMaxOpenIncomingStreams( |
| 114 | QuicStreamCount max_open_streams) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 115 | QuicStreamCount implementation_max = |
| 116 | QuicUtils::GetMaxStreamCount(unidirectional_, perspective()); |
renjietang | 52e1338 | 2019-12-16 15:58:04 -0800 | [diff] [blame] | 117 | QuicStreamCount new_max = std::min(implementation_max, max_open_streams); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 118 | if (new_max < incoming_stream_count_) { |
renjietang | ff4b2b6 | 2020-02-12 16:52:32 -0800 | [diff] [blame] | 119 | delegate_->OnStreamIdManagerError( |
| 120 | QUIC_MAX_STREAMS_ERROR, "Stream limit less than existing stream count"); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | incoming_actual_max_streams_ = new_max; |
| 124 | incoming_advertised_max_streams_ = new_max; |
| 125 | incoming_initial_max_open_streams_ = |
renjietang | 52e1338 | 2019-12-16 15:58:04 -0800 | [diff] [blame] | 126 | std::min(max_open_streams, implementation_max); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 127 | CalculateIncomingMaxStreamsWindow(); |
| 128 | } |
| 129 | |
| 130 | void QuicStreamIdManager::MaybeSendMaxStreamsFrame() { |
| 131 | if ((incoming_advertised_max_streams_ - incoming_stream_count_) > |
| 132 | max_streams_window_) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 133 | // window too large, no advertisement |
| 134 | return; |
| 135 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 136 | SendMaxStreamsFrame(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 137 | } |
| 138 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 139 | void QuicStreamIdManager::SendMaxStreamsFrame() { |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 140 | if (!is_config_negotiated_) { |
| 141 | // The config has not yet been negotiated, so we can not send the |
| 142 | // MAX STREAMS frame yet. Record that we would have sent one and then |
| 143 | // return. A new frame will be generated once the configuration is |
| 144 | // received. |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 145 | pending_max_streams_ = true; |
| 146 | return; |
| 147 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 148 | incoming_advertised_max_streams_ = incoming_actual_max_streams_; |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 149 | delegate_->SendMaxStreams(incoming_advertised_max_streams_, unidirectional_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void QuicStreamIdManager::OnStreamClosed(QuicStreamId stream_id) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 153 | DCHECK_NE(QuicUtils::IsBidirectionalStreamId(stream_id), unidirectional_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 154 | if (!IsIncomingStream(stream_id)) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 155 | // Nothing to do for outgoing streams. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 156 | return; |
| 157 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 158 | // If the stream is inbound, we can increase the actual stream limit and maybe |
| 159 | // advertise the new limit to the peer. Have to check to make sure that we do |
| 160 | // not exceed the maximum. |
| 161 | if (incoming_actual_max_streams_ == |
| 162 | QuicUtils::GetMaxStreamCount(unidirectional_, perspective())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 163 | // Reached the maximum stream id value that the implementation |
| 164 | // supports. Nothing can be done here. |
| 165 | return; |
| 166 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 167 | // One stream closed ... another can be opened. |
| 168 | incoming_actual_max_streams_++; |
| 169 | MaybeSendMaxStreamsFrame(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | QuicStreamId QuicStreamIdManager::GetNextOutgoingStreamId() { |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 173 | // Applications should always consult CanOpenNextOutgoingStream() first. |
| 174 | // If they ask for stream ids that violate the limit, it's an implementation |
| 175 | // bug. |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 176 | QUIC_BUG_IF(outgoing_stream_count_ >= outgoing_max_streams_) |
| 177 | << "Attempt to allocate a new outgoing stream that would exceed the " |
fkastenholz | 9b4b0ad | 2019-08-20 05:10:40 -0700 | [diff] [blame] | 178 | "limit (" |
| 179 | << outgoing_max_streams_ << ")"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 180 | QuicStreamId id = next_outgoing_stream_id_; |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 181 | next_outgoing_stream_id_ += QuicUtils::StreamIdDelta(transport_version_); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 182 | outgoing_stream_count_++; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 183 | return id; |
| 184 | } |
| 185 | |
| 186 | bool QuicStreamIdManager::CanOpenNextOutgoingStream() { |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 187 | DCHECK(VersionHasIetfQuicFrames(transport_version_)); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 188 | if (outgoing_stream_count_ < outgoing_max_streams_) { |
| 189 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 190 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 191 | // Next stream ID would exceed the limit, need to inform the peer. |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 192 | |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 193 | if (!is_config_negotiated_) { |
| 194 | // The config is not negotiated, so we can not send the STREAMS_BLOCKED |
| 195 | // frame yet. Record that we would have sent one, and what the limit was |
| 196 | // when we were blocked, and return. |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 197 | pending_streams_blocked_ = outgoing_max_streams_; |
| 198 | return false; |
| 199 | } |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 200 | delegate_->SendStreamsBlocked(outgoing_max_streams_, unidirectional_); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 201 | QUIC_CODE_COUNT(quic_reached_outgoing_stream_id_limit); |
| 202 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 203 | } |
| 204 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 205 | // Stream_id is the id of a new incoming stream. Check if it can be |
| 206 | // created (doesn't violate limits, etc). |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 207 | bool QuicStreamIdManager::MaybeIncreaseLargestPeerStreamId( |
| 208 | const QuicStreamId stream_id) { |
rch | 9301d3c | 2019-09-20 14:30:48 -0700 | [diff] [blame] | 209 | // |stream_id| must be an incoming stream of the right directionality. |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 210 | DCHECK_NE(QuicUtils::IsBidirectionalStreamId(stream_id), unidirectional_); |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 211 | DCHECK_NE(QuicUtils::IsServerInitiatedStreamId(transport_version_, stream_id), |
| 212 | perspective() == Perspective::IS_SERVER); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 213 | available_streams_.erase(stream_id); |
| 214 | |
| 215 | if (largest_peer_created_stream_id_ != |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 216 | QuicUtils::GetInvalidStreamId(transport_version_) && |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 217 | stream_id <= largest_peer_created_stream_id_) { |
| 218 | return true; |
| 219 | } |
| 220 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 221 | QuicStreamCount stream_count_increment; |
| 222 | if (largest_peer_created_stream_id_ != |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 223 | QuicUtils::GetInvalidStreamId(transport_version_)) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 224 | stream_count_increment = (stream_id - largest_peer_created_stream_id_) / |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 225 | QuicUtils::StreamIdDelta(transport_version_); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 226 | } else { |
| 227 | // Largest_peer_created_stream_id is the invalid ID, |
| 228 | // which means that the peer has not created any stream IDs. |
| 229 | // The "+1" is because the first stream ID has not yet |
| 230 | // been used. For example, if the FirstIncoming ID is 1 |
| 231 | // and stream_id is 1, then we want the increment to be 1. |
| 232 | stream_count_increment = ((stream_id - GetFirstIncomingStreamId()) / |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 233 | QuicUtils::StreamIdDelta(transport_version_)) + |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 234 | 1; |
| 235 | } |
| 236 | |
| 237 | // If already at, or over, the limit, close the connection/etc. |
| 238 | if (((incoming_stream_count_ + stream_count_increment) > |
| 239 | incoming_advertised_max_streams_) || |
| 240 | ((incoming_stream_count_ + stream_count_increment) < |
| 241 | incoming_stream_count_)) { |
| 242 | // This stream would exceed the limit. do not increase. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 243 | QUIC_DLOG(INFO) << ENDPOINT |
| 244 | << "Failed to create a new incoming stream with id:" |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 245 | << stream_id << ", reaching MAX_STREAMS limit: " |
| 246 | << incoming_advertised_max_streams_ << "."; |
renjietang | ff4b2b6 | 2020-02-12 16:52:32 -0800 | [diff] [blame] | 247 | delegate_->OnStreamIdManagerError( |
| 248 | QUIC_INVALID_STREAM_ID, |
| 249 | quiche::QuicheStrCat("Stream id ", stream_id, |
| 250 | " would exceed stream count limit ", |
| 251 | incoming_advertised_max_streams_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 252 | return false; |
| 253 | } |
| 254 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 255 | QuicStreamId id = GetFirstIncomingStreamId(); |
| 256 | if (largest_peer_created_stream_id_ != |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 257 | QuicUtils::GetInvalidStreamId(transport_version_)) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 258 | id = largest_peer_created_stream_id_ + |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 259 | QuicUtils::StreamIdDelta(transport_version_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 260 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 261 | |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 262 | for (; id < stream_id; id += QuicUtils::StreamIdDelta(transport_version_)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 263 | available_streams_.insert(id); |
| 264 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 265 | incoming_stream_count_ += stream_count_increment; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 266 | largest_peer_created_stream_id_ = stream_id; |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | bool QuicStreamIdManager::IsAvailableStream(QuicStreamId id) const { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 271 | DCHECK_NE(QuicUtils::IsBidirectionalStreamId(id), unidirectional_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 272 | if (!IsIncomingStream(id)) { |
| 273 | // Stream IDs under next_ougoing_stream_id_ are either open or previously |
| 274 | // open but now closed. |
| 275 | return id >= next_outgoing_stream_id_; |
| 276 | } |
| 277 | // For peer created streams, we also need to consider available streams. |
| 278 | return largest_peer_created_stream_id_ == |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 279 | QuicUtils::GetInvalidStreamId(transport_version_) || |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 280 | id > largest_peer_created_stream_id_ || |
| 281 | QuicContainsKey(available_streams_, id); |
| 282 | } |
| 283 | |
| 284 | bool QuicStreamIdManager::IsIncomingStream(QuicStreamId id) const { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 285 | DCHECK_NE(QuicUtils::IsBidirectionalStreamId(id), unidirectional_); |
| 286 | // The 0x1 bit in the stream id indicates whether the stream id is |
| 287 | // server- or client- initiated. Next_OUTGOING_stream_id_ has that bit |
| 288 | // set based on whether this node is a server or client. Thus, if the stream |
| 289 | // id in question has the 0x1 bit set opposite of next_OUTGOING_stream_id_, |
| 290 | // then that stream id is incoming -- it is for streams initiated by the peer. |
| 291 | return (id & 0x1) != (next_outgoing_stream_id_ & 0x1); |
| 292 | } |
| 293 | |
| 294 | QuicStreamId QuicStreamIdManager::GetFirstOutgoingStreamId() const { |
| 295 | return (unidirectional_) ? QuicUtils::GetFirstUnidirectionalStreamId( |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 296 | transport_version_, perspective()) |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 297 | : QuicUtils::GetFirstBidirectionalStreamId( |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 298 | transport_version_, perspective()); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | QuicStreamId QuicStreamIdManager::GetFirstIncomingStreamId() const { |
| 302 | return (unidirectional_) ? QuicUtils::GetFirstUnidirectionalStreamId( |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 303 | transport_version_, peer_perspective()) |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 304 | : QuicUtils::GetFirstBidirectionalStreamId( |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 305 | transport_version_, peer_perspective()); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | Perspective QuicStreamIdManager::perspective() const { |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 309 | return perspective_; |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | Perspective QuicStreamIdManager::peer_perspective() const { |
nharper | 4eba09b | 2019-06-26 20:17:25 -0700 | [diff] [blame] | 313 | return QuicUtils::InvertPerspective(perspective()); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 314 | } |
| 315 | |
renjietang | 52e1338 | 2019-12-16 15:58:04 -0800 | [diff] [blame] | 316 | QuicStreamCount QuicStreamIdManager::available_incoming_streams() { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 317 | return incoming_advertised_max_streams_ - incoming_stream_count_; |
| 318 | } |
| 319 | |
| 320 | void QuicStreamIdManager::CalculateIncomingMaxStreamsWindow() { |
| 321 | max_streams_window_ = incoming_actual_max_streams_ / kMaxStreamsWindowDivisor; |
| 322 | if (max_streams_window_ == 0) { |
| 323 | max_streams_window_ = 1; |
| 324 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 325 | } |
| 326 | |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 327 | void QuicStreamIdManager::OnConfigNegotiated() { |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 328 | is_config_negotiated_ = true; |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 329 | // If a STREAMS_BLOCKED or MAX_STREAMS is pending, send it and clear |
| 330 | // the pending state. |
| 331 | if (pending_streams_blocked_ != |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 332 | QuicUtils::GetInvalidStreamId(transport_version_)) { |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 333 | if (pending_streams_blocked_ >= outgoing_max_streams_) { |
| 334 | // There is a pending STREAMS_BLOCKED frame and the current limit does not |
| 335 | // let new streams be formed. Regenerate and send the frame. |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 336 | delegate_->SendStreamsBlocked(outgoing_max_streams_, unidirectional_); |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 337 | } |
| 338 | pending_streams_blocked_ = |
renjietang | ab93f58 | 2019-12-13 10:29:31 -0800 | [diff] [blame] | 339 | QuicUtils::GetInvalidStreamId(transport_version_); |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 340 | } |
| 341 | if (pending_max_streams_) { |
| 342 | // Generate a MAX_STREAMS using the current stream limits. |
| 343 | SendMaxStreamsFrame(); |
| 344 | pending_max_streams_ = false; |
| 345 | } |
| 346 | } |
| 347 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 348 | } // namespace quic |