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 | #ifndef QUICHE_QUIC_CORE_QUIC_STREAM_ID_MANAGER_H_ |
| 5 | #define QUICHE_QUIC_CORE_QUIC_STREAM_ID_MANAGER_H_ |
| 6 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7 | #include "net/third_party/quiche/src/quic/core/frames/quic_frame.h" |
| 8 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 11 | #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] | 12 | |
| 13 | namespace quic { |
| 14 | |
| 15 | namespace test { |
| 16 | class QuicSessionPeer; |
| 17 | class QuicStreamIdManagerPeer; |
| 18 | } // namespace test |
| 19 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 20 | // Amount to increment a stream ID value to get the next stream ID in |
| 21 | // the stream ID space. |
| 22 | const QuicStreamId kV99StreamIdIncrement = 4; |
| 23 | |
| 24 | // This constant controls the size of the window when deciding whether |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 25 | // to generate a MAX_STREAMS frame or not. See the discussion of the |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 26 | // window, below, for more details. |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 27 | const int kMaxStreamsWindowDivisor = 2; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 28 | |
| 29 | // This class manages the stream ids for Version 99/IETF QUIC. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 30 | class QUIC_EXPORT_PRIVATE QuicStreamIdManager { |
| 31 | public: |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 32 | class QUIC_EXPORT_PRIVATE DelegateInterface { |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 33 | public: |
| 34 | virtual ~DelegateInterface() = default; |
| 35 | |
| 36 | // Called when new outgoing streams are available to be opened. This occurs |
| 37 | // when an extant, open, stream is moved to draining or closed. |
| 38 | // |unidirectional| indicates whether unidirectional or bidirectional |
| 39 | // streams are now available. If both become available at the same time then |
| 40 | // there will be two calls to this method, one with unidirectional==true, |
| 41 | // the other with it ==false. |
| 42 | virtual void OnCanCreateNewOutgoingStream(bool unidirectional) = 0; |
| 43 | |
| 44 | // Closes the connection when an error is encountered. |
renjietang | ff4b2b6 | 2020-02-12 16:52:32 -0800 | [diff] [blame] | 45 | virtual void OnStreamIdManagerError(QuicErrorCode error_code, |
| 46 | std::string error_details) = 0; |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 47 | |
| 48 | // Send a MAX_STREAMS frame. |
| 49 | virtual void SendMaxStreams(QuicStreamCount stream_count, |
| 50 | bool unidirectional) = 0; |
| 51 | |
| 52 | // Send a STREAMS_BLOCKED frame. |
| 53 | virtual void SendStreamsBlocked(QuicStreamCount stream_count, |
| 54 | bool unidirectional) = 0; |
| 55 | }; |
| 56 | |
| 57 | QuicStreamIdManager(DelegateInterface* delegate, |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 58 | bool unidirectional, |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 59 | Perspective perspective, |
| 60 | QuicTransportVersion transport_version, |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 61 | QuicStreamCount max_allowed_outgoing_streams, |
| 62 | QuicStreamCount max_allowed_incoming_streams); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 63 | |
| 64 | ~QuicStreamIdManager(); |
| 65 | |
| 66 | // Generate a string suitable for sending to the log/etc to show current state |
| 67 | // of the stream ID manager. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 68 | std::string DebugString() const { |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 69 | return quiche::QuicheStrCat( |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 70 | " { unidirectional_: ", unidirectional_, |
| 71 | ", perspective: ", perspective(), |
| 72 | ", outgoing_max_streams_: ", outgoing_max_streams_, |
| 73 | ", next_outgoing_stream_id_: ", next_outgoing_stream_id_, |
| 74 | ", outgoing_stream_count_: ", outgoing_stream_count_, |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 75 | ", incoming_actual_max_streams_: ", incoming_actual_max_streams_, |
| 76 | ", incoming_advertised_max_streams_: ", |
| 77 | incoming_advertised_max_streams_, |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 78 | ", incoming_stream_count_: ", incoming_stream_count_, |
| 79 | ", available_streams_.size(): ", available_streams_.size(), |
| 80 | ", largest_peer_created_stream_id_: ", largest_peer_created_stream_id_, |
| 81 | ", max_streams_window_: ", max_streams_window_, " }"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 82 | } |
| 83 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 84 | // Processes the MAX_STREAMS frame, invoked from |
| 85 | // QuicSession::OnMaxStreamsFrame. It has the same semantics as the |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 86 | // QuicFramerVisitorInterface, returning true if the framer should continue |
| 87 | // processing the packet, false if not. |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 88 | bool OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 89 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 90 | // Processes the STREAMS_BLOCKED frame, invoked from |
| 91 | // QuicSession::OnStreamsBlockedFrame. It has the same semantics as the |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 92 | // QuicFramerVisitorInterface, returning true if the framer should continue |
| 93 | // processing the packet, false if not. |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 94 | bool OnStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 95 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 96 | // Indicates whether the next outgoing stream ID can be allocated or not. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 97 | bool CanOpenNextOutgoingStream(); |
| 98 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 99 | // Generate and send a MAX_STREAMS frame. |
| 100 | void SendMaxStreamsFrame(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 101 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 102 | // Invoked to deal with releasing a stream. Does nothing if the stream is |
| 103 | // outgoing. If the stream is incoming, the number of streams that the peer |
| 104 | // can open will be updated and a MAX_STREAMS frame, informing the peer of |
| 105 | // the additional streams, may be sent. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 106 | void OnStreamClosed(QuicStreamId stream_id); |
| 107 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 108 | // Returns the next outgoing stream id. Applications must call |
| 109 | // CanOpenNextOutgoingStream() first. A QUIC_BUG is logged if this method |
| 110 | // allocates a stream ID past the peer specified limit. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 111 | QuicStreamId GetNextOutgoingStreamId(); |
| 112 | |
renjietang | 52e1338 | 2019-12-16 15:58:04 -0800 | [diff] [blame] | 113 | void SetMaxOpenIncomingStreams(QuicStreamCount max_open_streams); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 114 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 115 | // Sets the maximum number of outgoing streams to max_open_streams. |
| 116 | // Used when configuration has been done and we have an initial |
| 117 | // maximum stream count from the peer. Note that if the stream count is such |
| 118 | // that it would result in stream ID values that are greater than the |
| 119 | // implementation limit, it pegs the count at the implementation limit. |
renjietang | 52e1338 | 2019-12-16 15:58:04 -0800 | [diff] [blame] | 120 | bool SetMaxOpenOutgoingStreams(QuicStreamCount max_open_streams); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 121 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 122 | // Checks if the incoming stream ID exceeds the MAX_STREAMS limit. If the |
| 123 | // limit is exceeded, closes the connection and returns false. Uses the |
| 124 | // actual maximium, not the most recently advertised value, in order to |
| 125 | // enforce the Google-QUIC number of open streams behavior. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 126 | // This method should be called exactly once for each incoming stream |
| 127 | // creation. |
| 128 | bool MaybeIncreaseLargestPeerStreamId(const QuicStreamId stream_id); |
| 129 | |
| 130 | // Returns true if |id| is still available. |
| 131 | bool IsAvailableStream(QuicStreamId id) const; |
| 132 | |
| 133 | // Return true if given stream is peer initiated. |
| 134 | bool IsIncomingStream(QuicStreamId id) const; |
| 135 | |
renjietang | 52e1338 | 2019-12-16 15:58:04 -0800 | [diff] [blame] | 136 | QuicStreamCount incoming_initial_max_open_streams() const { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 137 | return incoming_initial_max_open_streams_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 138 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 139 | |
| 140 | QuicStreamCount max_streams_window() const { return max_streams_window_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 141 | |
| 142 | QuicStreamId next_outgoing_stream_id() const { |
| 143 | return next_outgoing_stream_id_; |
| 144 | } |
| 145 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 146 | // Number of streams that the peer believes that it can still create. |
renjietang | 52e1338 | 2019-12-16 15:58:04 -0800 | [diff] [blame] | 147 | QuicStreamCount available_incoming_streams(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 148 | |
| 149 | void set_largest_peer_created_stream_id( |
| 150 | QuicStreamId largest_peer_created_stream_id) { |
| 151 | largest_peer_created_stream_id_ = largest_peer_created_stream_id; |
| 152 | } |
| 153 | |
renjietang | 686ce58 | 2019-10-17 14:28:16 -0700 | [diff] [blame] | 154 | QuicStreamId largest_peer_created_stream_id() const { |
| 155 | return largest_peer_created_stream_id_; |
| 156 | } |
| 157 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 158 | // These are the limits for outgoing and incoming streams, |
| 159 | // respectively. For incoming there are two limits, what has |
| 160 | // been advertised to the peer and what is actually available. |
| 161 | // The advertised incoming amount should never be more than the actual |
| 162 | // incoming one. |
| 163 | QuicStreamCount outgoing_max_streams() const { return outgoing_max_streams_; } |
| 164 | QuicStreamCount incoming_actual_max_streams() const { |
| 165 | return incoming_actual_max_streams_; |
| 166 | } |
| 167 | QuicStreamCount incoming_advertised_max_streams() const { |
| 168 | return incoming_advertised_max_streams_; |
| 169 | } |
| 170 | // Number of streams that have been opened (including those that have been |
| 171 | // opened and then closed. Must never exceed outgoing_max_streams |
| 172 | QuicStreamCount outgoing_stream_count() { return outgoing_stream_count_; } |
| 173 | |
| 174 | // Perspective (CLIENT/SERVER) of this node and the peer, respectively. |
| 175 | Perspective perspective() const; |
| 176 | Perspective peer_perspective() const; |
| 177 | |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 178 | // Called when session has been configured. Causes the Stream ID manager to |
| 179 | // send out any pending MAX_STREAMS and STREAMS_BLOCKED frames. |
| 180 | void OnConfigNegotiated(); |
| 181 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 182 | private: |
| 183 | friend class test::QuicSessionPeer; |
| 184 | friend class test::QuicStreamIdManagerPeer; |
| 185 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 186 | // Check whether the MAX_STREAMS window has opened up enough and, if so, |
| 187 | // generate and send a MAX_STREAMS frame. |
| 188 | void MaybeSendMaxStreamsFrame(); |
| 189 | |
| 190 | // Get what should be the first incoming/outgoing stream ID that |
| 191 | // this stream id manager will manage, taking into account directionality and |
| 192 | // client/server perspective. |
| 193 | QuicStreamId GetFirstOutgoingStreamId() const; |
| 194 | QuicStreamId GetFirstIncomingStreamId() const; |
| 195 | |
| 196 | void CalculateIncomingMaxStreamsWindow(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 197 | |
| 198 | // Back reference to the session containing this Stream ID Manager. |
| 199 | // needed to access various session methods, such as perspective() |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 200 | DelegateInterface* delegate_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 201 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 202 | // Whether this stream id manager is for unidrectional (true) or bidirectional |
| 203 | // (false) streams. |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 204 | const bool unidirectional_; |
| 205 | |
| 206 | // Is this manager a client or a server. |
| 207 | const Perspective perspective_; |
| 208 | |
| 209 | // Transport version used for this manager. |
| 210 | const QuicTransportVersion transport_version_; |
| 211 | |
rch | a8b56e4 | 2019-09-20 10:41:48 -0700 | [diff] [blame] | 212 | // True if the config has been negotiated_; |
| 213 | bool is_config_negotiated_; |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 214 | |
| 215 | // This is the number of streams that this node can initiate. |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 216 | // This limit is: |
| 217 | // - Initiated to a value specified in the constructor |
| 218 | // - May be updated when the config is received. |
| 219 | // - Is updated whenever a MAX STREAMS frame is received. |
| 220 | QuicStreamCount outgoing_max_streams_; |
| 221 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 222 | // The ID to use for the next outgoing stream. |
| 223 | QuicStreamId next_outgoing_stream_id_; |
| 224 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 225 | // The number of outgoing streams that have ever been opened, including those |
| 226 | // that have been closed. This number must never be larger than |
| 227 | // outgoing_max_streams_. |
| 228 | QuicStreamCount outgoing_stream_count_; |
| 229 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 230 | // FOR INCOMING STREAMS |
| 231 | |
| 232 | // The maximum number of streams that can be opened by the peer. |
| 233 | QuicStreamCount incoming_actual_max_streams_; |
| 234 | QuicStreamCount incoming_advertised_max_streams_; |
| 235 | |
| 236 | // Initial maximum on the number of open streams allowed. |
| 237 | QuicStreamCount incoming_initial_max_open_streams_; |
| 238 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 239 | // This is the number of streams that have been created -- some are still |
| 240 | // open, the others have been closed. It is the number that is compared |
| 241 | // against MAX_STREAMS when deciding whether to accept a new stream or not. |
| 242 | QuicStreamCount incoming_stream_count_; |
| 243 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 244 | // Set of stream ids that are less than the largest stream id that has been |
| 245 | // received, but are nonetheless available to be created. |
| 246 | QuicUnorderedSet<QuicStreamId> available_streams_; |
| 247 | |
| 248 | QuicStreamId largest_peer_created_stream_id_; |
| 249 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 250 | // When incoming streams close the local node sends MAX_STREAMS frames. It |
| 251 | // does so only when the peer can open fewer than |max_stream_id_window_| |
| 252 | // streams. That is, when |incoming_actual_max_streams_| - |
| 253 | // |incoming_advertised_max_streams_| is less than the window. |
| 254 | // max_streams_window_ is set to 1/2 of the initial number of incoming streams |
| 255 | // that are allowed (as set in the constructor). |
| 256 | QuicStreamId max_streams_window_; |
fkastenholz | 56055be | 2019-09-17 11:17:37 -0700 | [diff] [blame] | 257 | |
| 258 | // MAX_STREAMS and STREAMS_BLOCKED frames are not sent before the session has |
| 259 | // been configured. Instead, the relevant information is stored in |
| 260 | // |pending_max_streams_| and |pending_streams_blocked_| and sent when |
| 261 | // OnConfigNegotiated() is invoked. |
| 262 | bool pending_max_streams_; |
| 263 | QuicStreamId pending_streams_blocked_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 264 | }; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 265 | } // namespace quic |
| 266 | |
| 267 | #endif // QUICHE_QUIC_CORE_QUIC_STREAM_ID_MANAGER_H_ |