QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | // The base class for client/server QUIC streams. |
| 6 | |
| 7 | // It does not contain the entire interface needed by an application to interact |
| 8 | // with a QUIC stream. Some parts of the interface must be obtained by |
| 9 | // accessing the owning session object. A subclass of QuicStream |
| 10 | // connects the object and the application that generates and consumes the data |
| 11 | // of the stream. |
| 12 | |
| 13 | // The QuicStream object has a dependent QuicStreamSequencer object, |
| 14 | // which is given the stream frames as they arrive, and provides stream data in |
| 15 | // order by invoking ProcessRawData(). |
| 16 | |
| 17 | #ifndef QUICHE_QUIC_CORE_QUIC_STREAM_H_ |
| 18 | #define QUICHE_QUIC_CORE_QUIC_STREAM_H_ |
| 19 | |
| 20 | #include <cstddef> |
| 21 | #include <cstdint> |
| 22 | #include <list> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 23 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 24 | |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 25 | #include "absl/strings/string_view.h" |
vasilvv | 7df418b | 2020-10-13 13:47:09 -0700 | [diff] [blame] | 26 | #include "absl/types/optional.h" |
vasilvv | 53f59f6 | 2021-06-09 10:48:48 -0700 | [diff] [blame] | 27 | #include "absl/types/span.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 28 | #include "quic/core/quic_flow_controller.h" |
| 29 | #include "quic/core/quic_packets.h" |
| 30 | #include "quic/core/quic_stream_send_buffer.h" |
| 31 | #include "quic/core/quic_stream_sequencer.h" |
| 32 | #include "quic/core/quic_types.h" |
| 33 | #include "quic/core/session_notifier_interface.h" |
| 34 | #include "quic/core/stream_delegate_interface.h" |
| 35 | #include "quic/platform/api/quic_export.h" |
vasilvv | 53f59f6 | 2021-06-09 10:48:48 -0700 | [diff] [blame] | 36 | #include "quic/platform/api/quic_mem_slice.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 37 | #include "quic/platform/api/quic_mem_slice_span.h" |
| 38 | #include "quic/platform/api/quic_reference_counted.h" |
| 39 | #include "spdy/core/spdy_protocol.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | |
| 41 | namespace quic { |
| 42 | |
| 43 | namespace test { |
| 44 | class QuicStreamPeer; |
| 45 | } // namespace test |
| 46 | |
| 47 | class QuicSession; |
| 48 | class QuicStream; |
| 49 | |
| 50 | // Buffers frames for a stream until the first byte of that frame arrives. |
| 51 | class QUIC_EXPORT_PRIVATE PendingStream |
| 52 | : public QuicStreamSequencer::StreamInterface { |
| 53 | public: |
| 54 | PendingStream(QuicStreamId id, QuicSession* session); |
| 55 | PendingStream(const PendingStream&) = delete; |
| 56 | PendingStream(PendingStream&&) = default; |
| 57 | ~PendingStream() override = default; |
| 58 | |
| 59 | // QuicStreamSequencer::StreamInterface |
| 60 | void OnDataAvailable() override; |
| 61 | void OnFinRead() override; |
| 62 | void AddBytesConsumed(QuicByteCount bytes) override; |
| 63 | void Reset(QuicRstStreamErrorCode error) override; |
renjietang | 87df0d0 | 2020-02-13 11:53:52 -0800 | [diff] [blame] | 64 | void OnUnrecoverableError(QuicErrorCode error, |
| 65 | const std::string& details) override; |
mattm | 55006b0 | 2021-01-14 15:09:54 -0800 | [diff] [blame] | 66 | void OnUnrecoverableError(QuicErrorCode error, |
| 67 | QuicIetfTransportErrorCodes ietf_error, |
| 68 | const std::string& details) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 69 | QuicStreamId id() const override; |
fayang | 528c36b | 2020-12-23 07:02:09 -0800 | [diff] [blame] | 70 | ParsedQuicVersion version() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 71 | |
| 72 | // Buffers the contents of |frame|. Frame must have a non-zero offset. |
| 73 | // If the data violates flow control, the connection will be closed. |
| 74 | void OnStreamFrame(const QuicStreamFrame& frame); |
| 75 | |
| 76 | // Stores the final byte offset from |frame|. |
| 77 | // If the final offset violates flow control, the connection will be closed. |
| 78 | void OnRstStreamFrame(const QuicRstStreamFrame& frame); |
| 79 | |
| 80 | // Returns the number of bytes read on this stream. |
| 81 | uint64_t stream_bytes_read() { return stream_bytes_read_; } |
| 82 | |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 83 | const QuicStreamSequencer* sequencer() const { return &sequencer_; } |
| 84 | |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 85 | void MarkConsumed(QuicByteCount num_bytes); |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 86 | |
bnc | 4ff6062 | 2019-08-09 18:55:45 -0700 | [diff] [blame] | 87 | // Tells the sequencer to ignore all incoming data itself and not call |
| 88 | // OnDataAvailable(). |
| 89 | void StopReading(); |
| 90 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 91 | private: |
| 92 | friend class QuicStream; |
| 93 | |
| 94 | bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset); |
| 95 | |
| 96 | // ID of this stream. |
| 97 | QuicStreamId id_; |
| 98 | |
fayang | 528c36b | 2020-12-23 07:02:09 -0800 | [diff] [blame] | 99 | // QUIC version being used by this stream. |
| 100 | ParsedQuicVersion version_; |
| 101 | |
renjietang | 49aec63 | 2020-08-31 10:56:28 -0700 | [diff] [blame] | 102 | // |stream_delegate_| must outlive this stream. |
renjietang | f196f6a | 2020-02-12 12:34:23 -0800 | [diff] [blame] | 103 | StreamDelegateInterface* stream_delegate_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 104 | |
| 105 | // Bytes read refers to payload bytes only: they do not include framing, |
| 106 | // encryption overhead etc. |
| 107 | uint64_t stream_bytes_read_; |
| 108 | |
| 109 | // True if a frame containing a fin has been received. |
| 110 | bool fin_received_; |
| 111 | |
| 112 | // Connection-level flow controller. Owned by the session. |
| 113 | QuicFlowController* connection_flow_controller_; |
| 114 | // Stream-level flow controller. |
| 115 | QuicFlowController flow_controller_; |
| 116 | // Stores the buffered frames. |
| 117 | QuicStreamSequencer sequencer_; |
| 118 | }; |
| 119 | |
| 120 | class QUIC_EXPORT_PRIVATE QuicStream |
| 121 | : public QuicStreamSequencer::StreamInterface { |
| 122 | public: |
bnc | 3e004e9 | 2020-08-31 07:03:13 -0700 | [diff] [blame] | 123 | // Default priority for Google QUIC. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 124 | // This is somewhat arbitrary. It's possible, but unlikely, we will either |
| 125 | // fail to set a priority client-side, or cancel a stream before stripping the |
| 126 | // priority from the wire server-side. In either case, start out with a |
bnc | 5f20251 | 2020-02-01 18:43:02 -0800 | [diff] [blame] | 127 | // priority in the middle in case of Google QUIC. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 128 | static const spdy::SpdyPriority kDefaultPriority = 3; |
| 129 | static_assert(kDefaultPriority == |
| 130 | (spdy::kV3LowestPriority + spdy::kV3HighestPriority) / 2, |
| 131 | "Unexpected value of kDefaultPriority"); |
| 132 | |
| 133 | // Creates a new stream with stream_id |id| associated with |session|. If |
| 134 | // |is_static| is true, then the stream will be given precedence |
| 135 | // over other streams when determing what streams should write next. |
| 136 | // |type| indicates whether the stream is bidirectional, read unidirectional |
| 137 | // or write unidirectional. |
| 138 | // TODO(fayang): Remove |type| when IETF stream ID numbering fully kicks in. |
| 139 | QuicStream(QuicStreamId id, |
| 140 | QuicSession* session, |
| 141 | bool is_static, |
| 142 | StreamType type); |
renjietang | 49aec63 | 2020-08-31 10:56:28 -0700 | [diff] [blame] | 143 | QuicStream(PendingStream* pending, |
| 144 | QuicSession* session, |
| 145 | StreamType type, |
| 146 | bool is_static); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 147 | QuicStream(const QuicStream&) = delete; |
| 148 | QuicStream& operator=(const QuicStream&) = delete; |
| 149 | |
| 150 | virtual ~QuicStream(); |
| 151 | |
bnc | 3e004e9 | 2020-08-31 07:03:13 -0700 | [diff] [blame] | 152 | // Default priority for IETF QUIC, defined by the priority extension at |
| 153 | // https://httpwg.org/http-extensions/draft-ietf-httpbis-priority.html#urgency. |
bnc | 9e7c08c | 2020-11-09 11:24:49 -0800 | [diff] [blame] | 154 | static const int kDefaultUrgency = 3; |
bnc | 3e004e9 | 2020-08-31 07:03:13 -0700 | [diff] [blame] | 155 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 156 | // QuicStreamSequencer::StreamInterface implementation. |
| 157 | QuicStreamId id() const override { return id_; } |
fayang | 528c36b | 2020-12-23 07:02:09 -0800 | [diff] [blame] | 158 | ParsedQuicVersion version() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 159 | // Called by the stream subclass after it has consumed the final incoming |
| 160 | // data. |
| 161 | void OnFinRead() override; |
| 162 | |
| 163 | // Called by the subclass or the sequencer to reset the stream from this |
| 164 | // end. |
| 165 | void Reset(QuicRstStreamErrorCode error) override; |
| 166 | |
| 167 | // Called by the subclass or the sequencer to close the entire connection from |
| 168 | // this end. |
renjietang | 87df0d0 | 2020-02-13 11:53:52 -0800 | [diff] [blame] | 169 | void OnUnrecoverableError(QuicErrorCode error, |
| 170 | const std::string& details) override; |
mattm | 55006b0 | 2021-01-14 15:09:54 -0800 | [diff] [blame] | 171 | void OnUnrecoverableError(QuicErrorCode error, |
| 172 | QuicIetfTransportErrorCodes ietf_error, |
| 173 | const std::string& details) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 174 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 175 | // Called by the session when a (potentially duplicate) stream frame has been |
| 176 | // received for this stream. |
| 177 | virtual void OnStreamFrame(const QuicStreamFrame& frame); |
| 178 | |
| 179 | // Called by the session when the connection becomes writeable to allow the |
| 180 | // stream to write any pending data. |
| 181 | virtual void OnCanWrite(); |
| 182 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 183 | // Called by the session when the endpoint receives a RST_STREAM from the |
| 184 | // peer. |
| 185 | virtual void OnStreamReset(const QuicRstStreamFrame& frame); |
| 186 | |
| 187 | // Called by the session when the endpoint receives or sends a connection |
| 188 | // close, and should immediately close the stream. |
| 189 | virtual void OnConnectionClosed(QuicErrorCode error, |
| 190 | ConnectionCloseSource source); |
| 191 | |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 192 | const spdy::SpdyStreamPrecedence& precedence() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 193 | |
bnc | cf09f95 | 2020-01-30 17:35:59 -0800 | [diff] [blame] | 194 | // Send PRIORITY_UPDATE frame if application protocol supports it. |
| 195 | virtual void MaybeSendPriorityUpdateFrame() {} |
| 196 | |
| 197 | // Sets |priority_| to priority. This should only be called before bytes are |
| 198 | // written to the server. For a server stream, this is called when a |
| 199 | // PRIORITY_UPDATE frame is received. This calls |
| 200 | // MaybeSendPriorityUpdateFrame(), which for a client stream might send a |
| 201 | // PRIORITY_UPDATE frame. |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 202 | void SetPriority(const spdy::SpdyStreamPrecedence& precedence); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 203 | |
| 204 | // Returns true if this stream is still waiting for acks of sent data. |
| 205 | // This will return false if all data has been acked, or if the stream |
| 206 | // is no longer interested in data being acked (which happens when |
| 207 | // a stream is reset because of an error). |
| 208 | bool IsWaitingForAcks() const; |
| 209 | |
| 210 | // Number of bytes available to read. |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 211 | QuicByteCount ReadableBytes() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 212 | |
| 213 | QuicRstStreamErrorCode stream_error() const { return stream_error_; } |
| 214 | QuicErrorCode connection_error() const { return connection_error_; } |
| 215 | |
| 216 | bool reading_stopped() const { |
| 217 | return sequencer_.ignore_read_data() || read_side_closed_; |
| 218 | } |
| 219 | bool write_side_closed() const { return write_side_closed_; } |
QUICHE team | 9fade46 | 2020-10-12 12:54:41 -0700 | [diff] [blame] | 220 | bool read_side_closed() const { return read_side_closed_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 221 | |
renjietang | 647b3cf | 2020-08-04 13:23:12 -0700 | [diff] [blame] | 222 | bool IsZombie() const { |
| 223 | return read_side_closed_ && write_side_closed_ && IsWaitingForAcks(); |
| 224 | } |
| 225 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 226 | bool rst_received() const { return rst_received_; } |
| 227 | bool rst_sent() const { return rst_sent_; } |
| 228 | bool fin_received() const { return fin_received_; } |
| 229 | bool fin_sent() const { return fin_sent_; } |
| 230 | bool fin_outstanding() const { return fin_outstanding_; } |
| 231 | bool fin_lost() const { return fin_lost_; } |
| 232 | |
| 233 | uint64_t BufferedDataBytes() const; |
| 234 | |
| 235 | uint64_t stream_bytes_read() const { return stream_bytes_read_; } |
| 236 | uint64_t stream_bytes_written() const; |
| 237 | |
| 238 | size_t busy_counter() const { return busy_counter_; } |
| 239 | void set_busy_counter(size_t busy_counter) { busy_counter_ = busy_counter; } |
| 240 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 241 | // Adjust the flow control window according to new offset in |frame|. |
| 242 | virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame); |
| 243 | |
| 244 | int num_frames_received() const; |
| 245 | int num_duplicate_frames_received() const; |
| 246 | |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 247 | // Flow controller related methods. |
| 248 | bool IsFlowControlBlocked() const; |
| 249 | QuicStreamOffset highest_received_byte_offset() const; |
| 250 | void UpdateReceiveWindowSize(QuicStreamOffset size); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 251 | |
| 252 | // Called when endpoint receives a frame which could increase the highest |
| 253 | // offset. |
| 254 | // Returns true if the highest offset did increase. |
| 255 | bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 256 | |
renjietang | e0f96d7 | 2020-04-28 11:25:29 -0700 | [diff] [blame] | 257 | // Set the flow controller's send window offset from session config. |
renjietang | 3f68bdf | 2020-08-10 11:52:30 -0700 | [diff] [blame] | 258 | // |was_zero_rtt_rejected| is true if this config is from a rejected IETF QUIC |
| 259 | // 0-RTT attempt. Closes the connection and returns false if |new_offset| is |
| 260 | // not valid. |
| 261 | bool MaybeConfigSendWindowOffset(QuicStreamOffset new_offset, |
| 262 | bool was_zero_rtt_rejected); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 263 | |
| 264 | // Returns true if the stream has received either a RST_STREAM or a FIN - |
| 265 | // either of which gives a definitive number of bytes which the peer has |
| 266 | // sent. If this is not true on deletion of the stream object, the session |
| 267 | // must keep track of the stream's byte offset until a definitive final value |
| 268 | // arrives. |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 269 | bool HasReceivedFinalOffset() const { return fin_received_ || rst_received_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 270 | |
| 271 | // Returns true if the stream has queued data waiting to write. |
| 272 | bool HasBufferedData() const; |
| 273 | |
| 274 | // Returns the version of QUIC being used for this stream. |
| 275 | QuicTransportVersion transport_version() const; |
| 276 | |
| 277 | // Returns the crypto handshake protocol that was used on this stream's |
| 278 | // connection. |
| 279 | HandshakeProtocol handshake_protocol() const; |
| 280 | |
| 281 | // Sets the sequencer to consume all incoming data itself and not call |
| 282 | // OnDataAvailable(). |
| 283 | // When the FIN is received, the stream will be notified automatically (via |
| 284 | // OnFinRead()) (which may happen during the call of StopReading()). |
| 285 | // TODO(dworley): There should be machinery to send a RST_STREAM/NO_ERROR and |
| 286 | // stop sending stream-level flow-control updates when this end sends FIN. |
| 287 | virtual void StopReading(); |
| 288 | |
fayang | 9c41f8b | 2020-10-30 13:13:06 -0700 | [diff] [blame] | 289 | // Sends as much of |data| to the connection on the application encryption |
| 290 | // level as the connection will consume, and then buffers any remaining data |
| 291 | // in the send buffer. If fin is true: if it is immediately passed on to the |
| 292 | // session, write_side_closed() becomes true, otherwise fin_buffered_ becomes |
| 293 | // true. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 294 | void WriteOrBufferData( |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 295 | absl::string_view data, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 296 | bool fin, |
| 297 | QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener); |
| 298 | |
fayang | 9c41f8b | 2020-10-30 13:13:06 -0700 | [diff] [blame] | 299 | // Sends |data| to connection with specified |level|. |
| 300 | void WriteOrBufferDataAtLevel( |
| 301 | absl::string_view data, |
| 302 | bool fin, |
| 303 | EncryptionLevel level, |
| 304 | QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener); |
| 305 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 306 | // Adds random padding after the fin is consumed for this stream. |
| 307 | void AddRandomPaddingAfterFin(); |
| 308 | |
| 309 | // Write |data_length| of data starts at |offset| from send buffer. |
| 310 | bool WriteStreamData(QuicStreamOffset offset, |
| 311 | QuicByteCount data_length, |
| 312 | QuicDataWriter* writer); |
| 313 | |
| 314 | // Called when data [offset, offset + data_length) is acked. |fin_acked| |
| 315 | // indicates whether the fin is acked. Returns true and updates |
| 316 | // |newly_acked_length| if any new stream data (including fin) gets acked. |
| 317 | virtual bool OnStreamFrameAcked(QuicStreamOffset offset, |
| 318 | QuicByteCount data_length, |
| 319 | bool fin_acked, |
| 320 | QuicTime::Delta ack_delay_time, |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 321 | QuicTime receive_timestamp, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 322 | QuicByteCount* newly_acked_length); |
| 323 | |
| 324 | // Called when data [offset, offset + data_length) was retransmitted. |
| 325 | // |fin_retransmitted| indicates whether fin was retransmitted. |
| 326 | virtual void OnStreamFrameRetransmitted(QuicStreamOffset offset, |
| 327 | QuicByteCount data_length, |
| 328 | bool fin_retransmitted); |
| 329 | |
| 330 | // Called when data [offset, offset + data_length) is considered as lost. |
| 331 | // |fin_lost| indicates whether the fin is considered as lost. |
| 332 | virtual void OnStreamFrameLost(QuicStreamOffset offset, |
| 333 | QuicByteCount data_length, |
| 334 | bool fin_lost); |
| 335 | |
| 336 | // Called to retransmit outstanding portion in data [offset, offset + |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 337 | // data_length) and |fin| with Transmission |type|. |
| 338 | // Returns true if all data gets retransmitted. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 339 | virtual bool RetransmitStreamData(QuicStreamOffset offset, |
| 340 | QuicByteCount data_length, |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 341 | bool fin, |
| 342 | TransmissionType type); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 343 | |
| 344 | // Sets deadline of this stream to be now + |ttl|, returns true if the setting |
| 345 | // succeeds. |
| 346 | bool MaybeSetTtl(QuicTime::Delta ttl); |
| 347 | |
vasilvv | 53f59f6 | 2021-06-09 10:48:48 -0700 | [diff] [blame] | 348 | // Commits data into the stream write buffer, and potentially sends it over |
| 349 | // the wire. This method has all-or-nothing semantics: if the write buffer is |
| 350 | // not full, all of the memslices in |span| are moved into it; otherwise, |
| 351 | // nothing happens. |
| 352 | // TODO(vasilvv): deprecate and remove QuicMemSliceSpan version. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 353 | QuicConsumedData WriteMemSlices(QuicMemSliceSpan span, bool fin); |
vasilvv | 53f59f6 | 2021-06-09 10:48:48 -0700 | [diff] [blame] | 354 | QuicConsumedData WriteMemSlices(absl::Span<QuicMemSlice> span, bool fin); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 355 | |
| 356 | // Returns true if any stream data is lost (including fin) and needs to be |
| 357 | // retransmitted. |
| 358 | virtual bool HasPendingRetransmission() const; |
| 359 | |
| 360 | // Returns true if any portion of data [offset, offset + data_length) is |
| 361 | // outstanding or fin is outstanding (if |fin| is true). Returns false |
| 362 | // otherwise. |
| 363 | bool IsStreamFrameOutstanding(QuicStreamOffset offset, |
| 364 | QuicByteCount data_length, |
| 365 | bool fin) const; |
| 366 | |
| 367 | StreamType type() const { return type_; } |
| 368 | |
renjietang | ed49cb9 | 2020-03-03 14:30:53 -0800 | [diff] [blame] | 369 | // Handle received StopSending frame. Returns true if the processing finishes |
| 370 | // gracefully. |
bnc | 187eea3 | 2020-09-02 12:16:15 -0700 | [diff] [blame] | 371 | virtual bool OnStopSending(QuicRstStreamErrorCode code); |
renjietang | d976228 | 2019-11-11 17:11:18 -0800 | [diff] [blame] | 372 | |
renjietang | fbeb5bf | 2019-04-19 15:06:20 -0700 | [diff] [blame] | 373 | // Returns true if the stream is static. |
| 374 | bool is_static() const { return is_static_; } |
| 375 | |
fayang | be6d664 | 2020-04-16 14:15:34 -0700 | [diff] [blame] | 376 | bool was_draining() const { return was_draining_; } |
| 377 | |
bnc | 5f20251 | 2020-02-01 18:43:02 -0800 | [diff] [blame] | 378 | static spdy::SpdyStreamPrecedence CalculateDefaultPriority( |
| 379 | const QuicSession* session); |
| 380 | |
fayang | a330b7c | 2020-09-10 08:15:39 -0700 | [diff] [blame] | 381 | QuicTime creation_time() const { return creation_time_; } |
| 382 | |
| 383 | bool fin_buffered() const { return fin_buffered_; } |
| 384 | |
vasilvv | 547041f | 2021-03-17 17:29:58 -0700 | [diff] [blame] | 385 | // True if buffered data in send buffer is below buffered_data_threshold_. |
| 386 | bool CanWriteNewData() const; |
| 387 | |
vasilvv | 847b014 | 2021-03-24 10:32:35 -0700 | [diff] [blame] | 388 | // Called immediately after the stream is created from a pending stream, |
| 389 | // indicating it can start processing data. |
| 390 | void OnStreamCreatedFromPendingStream(); |
| 391 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 392 | protected: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 393 | // Called when data of [offset, offset + data_length] is buffered in send |
| 394 | // buffer. |
| 395 | virtual void OnDataBuffered( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 396 | QuicStreamOffset /*offset*/, |
| 397 | QuicByteCount /*data_length*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 398 | const QuicReferenceCountedPointer<QuicAckListenerInterface>& |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 399 | /*ack_listener*/) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 400 | |
fayang | b8f8344 | 2020-06-01 12:09:17 -0700 | [diff] [blame] | 401 | // Called just before the object is destroyed. |
| 402 | // The object should not be accessed after OnClose is called. |
| 403 | // Sends a RST_STREAM with code QUIC_RST_ACKNOWLEDGEMENT if neither a FIN nor |
| 404 | // a RST_STREAM has been sent. |
| 405 | virtual void OnClose(); |
| 406 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 407 | // True if buffered data in send buffer is still below |
| 408 | // buffered_data_threshold_ even after writing |length| bytes. |
| 409 | bool CanWriteNewDataAfterData(QuicByteCount length) const; |
| 410 | |
| 411 | // Called when upper layer can write new data. |
| 412 | virtual void OnCanWriteNewData() {} |
| 413 | |
| 414 | // Called when |bytes_consumed| bytes has been consumed. |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 415 | virtual void OnStreamDataConsumed(QuicByteCount bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 416 | |
ianswett | 9363720 | 2019-04-03 08:05:29 -0700 | [diff] [blame] | 417 | // Called by the stream sequencer as bytes are consumed from the buffer. |
| 418 | // If the receive window has dropped below the threshold, then send a |
| 419 | // WINDOW_UPDATE frame. |
| 420 | void AddBytesConsumed(QuicByteCount bytes) override; |
| 421 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 422 | // Writes pending retransmissions if any. |
| 423 | virtual void WritePendingRetransmission(); |
| 424 | |
| 425 | // This is called when stream tries to retransmit data after deadline_. Make |
| 426 | // this virtual so that subclasses can implement their own logics. |
| 427 | virtual void OnDeadlinePassed(); |
| 428 | |
fayang | 3a51d1a | 2020-04-16 13:42:08 -0700 | [diff] [blame] | 429 | // Called to set fin_sent_. This is only used by Google QUIC while body is |
| 430 | // empty. |
| 431 | void SetFinSent(); |
| 432 | |
renjietang | 052df7c | 2020-10-13 14:46:09 -0700 | [diff] [blame] | 433 | // Send STOP_SENDING if it hasn't been sent yet. |
| 434 | void MaybeSendStopSending(QuicRstStreamErrorCode error); |
| 435 | |
| 436 | // Send RESET_STREAM if it hasn't been sent yet. |
| 437 | void MaybeSendRstStream(QuicRstStreamErrorCode error); |
| 438 | |
renjietang | f00e1e1 | 2020-08-26 10:51:15 -0700 | [diff] [blame] | 439 | // Close the write side of the socket. Further writes will fail. |
| 440 | // Can be called by the subclass or internally. |
| 441 | // Does not send a FIN. May cause the stream to be closed. |
| 442 | virtual void CloseWriteSide(); |
| 443 | |
renjietang | df2240d | 2020-07-17 12:17:00 -0700 | [diff] [blame] | 444 | void set_rst_received(bool rst_received) { rst_received_ = rst_received; } |
| 445 | void set_stream_error(QuicRstStreamErrorCode error) { stream_error_ = error; } |
| 446 | |
renjietang | f196f6a | 2020-02-12 12:34:23 -0800 | [diff] [blame] | 447 | StreamDelegateInterface* stream_delegate() { return stream_delegate_; } |
| 448 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 449 | const QuicSession* session() const { return session_; } |
| 450 | QuicSession* session() { return session_; } |
| 451 | |
| 452 | const QuicStreamSequencer* sequencer() const { return &sequencer_; } |
| 453 | QuicStreamSequencer* sequencer() { return &sequencer_; } |
| 454 | |
| 455 | void DisableConnectionFlowControlForThisStream() { |
| 456 | stream_contributes_to_connection_flow_control_ = false; |
| 457 | } |
| 458 | |
| 459 | const QuicIntervalSet<QuicStreamOffset>& bytes_acked() const; |
| 460 | |
| 461 | const QuicStreamSendBuffer& send_buffer() const { return send_buffer_; } |
| 462 | |
| 463 | QuicStreamSendBuffer& send_buffer() { return send_buffer_; } |
| 464 | |
danzh | 2475cc8 | 2021-02-19 14:54:15 -0800 | [diff] [blame] | 465 | // Return the current flow control send window in bytes. |
| 466 | absl::optional<QuicByteCount> GetSendWindow() const; |
danzh | 6fec8e9 | 2021-02-24 07:53:56 -0800 | [diff] [blame] | 467 | absl::optional<QuicByteCount> GetReceiveWindow() const; |
danzh | 2475cc8 | 2021-02-19 14:54:15 -0800 | [diff] [blame] | 468 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 469 | private: |
| 470 | friend class test::QuicStreamPeer; |
| 471 | friend class QuicStreamUtils; |
| 472 | |
vasilvv | 53f59f6 | 2021-06-09 10:48:48 -0700 | [diff] [blame] | 473 | // Wraps around either QuicMemSliceSpan or absl::Span<QuicMemSlice>. |
| 474 | // TODO(vasilvv): delete this after QuicMemSliceSpan is gone. |
| 475 | class QUIC_EXPORT_PRIVATE MemSliceSpanWrapper { |
| 476 | public: |
| 477 | explicit MemSliceSpanWrapper(QuicMemSliceSpan span) : old_(span) {} |
| 478 | explicit MemSliceSpanWrapper(absl::Span<QuicMemSlice> span) : new_(span) {} |
| 479 | |
| 480 | bool empty() { return old_.has_value() ? old_->empty() : new_.empty(); } |
| 481 | size_t SaveTo(QuicStreamSendBuffer& send_buffer) { |
| 482 | if (old_.has_value()) { |
| 483 | return send_buffer.SaveMemSliceSpan(*old_); |
| 484 | } |
| 485 | return send_buffer.SaveMemSliceSpan(new_); |
| 486 | } |
| 487 | |
| 488 | private: |
| 489 | absl::optional<QuicMemSliceSpan> old_; |
| 490 | absl::Span<QuicMemSlice> new_; |
| 491 | }; |
| 492 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 493 | QuicStream(QuicStreamId id, |
| 494 | QuicSession* session, |
| 495 | QuicStreamSequencer sequencer, |
| 496 | bool is_static, |
| 497 | StreamType type, |
| 498 | uint64_t stream_bytes_read, |
| 499 | bool fin_received, |
vasilvv | 7df418b | 2020-10-13 13:47:09 -0700 | [diff] [blame] | 500 | absl::optional<QuicFlowController> flow_controller, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 501 | QuicFlowController* connection_flow_controller); |
| 502 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 503 | // Calls MaybeSendBlocked on the stream's flow controller and the connection |
| 504 | // level flow controller. If the stream is flow control blocked by the |
| 505 | // connection-level flow controller but not by the stream-level flow |
| 506 | // controller, marks this stream as connection-level write blocked. |
| 507 | void MaybeSendBlocked(); |
| 508 | |
fayang | 9c41f8b | 2020-10-30 13:13:06 -0700 | [diff] [blame] | 509 | // Write buffered data in send buffer. |
| 510 | // TODO(fayang): Change absl::optional<EncryptionLevel> to EncryptionLevel |
| 511 | // when deprecating quic_use_write_or_buffer_data_at_level. |
| 512 | void WriteBufferedData(absl::optional<EncryptionLevel> level); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 513 | |
renjietang | f00e1e1 | 2020-08-26 10:51:15 -0700 | [diff] [blame] | 514 | // Close the read side of the stream. May cause the stream to be closed. |
| 515 | void CloseReadSide(); |
| 516 | |
ianswett | 9363720 | 2019-04-03 08:05:29 -0700 | [diff] [blame] | 517 | // Called when bytes are sent to the peer. |
| 518 | void AddBytesSent(QuicByteCount bytes); |
| 519 | |
fayang | 9c41f8b | 2020-10-30 13:13:06 -0700 | [diff] [blame] | 520 | // TODO(fayang): Inline this function when deprecating |
| 521 | // quic_use_write_or_buffer_data_at_level. |
| 522 | void WriteOrBufferDataInner( |
| 523 | absl::string_view data, |
| 524 | bool fin, |
| 525 | absl::optional<EncryptionLevel> level, |
| 526 | QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener); |
| 527 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 528 | // Returns true if deadline_ has passed. |
| 529 | bool HasDeadlinePassed() const; |
| 530 | |
vasilvv | 53f59f6 | 2021-06-09 10:48:48 -0700 | [diff] [blame] | 531 | QuicConsumedData WriteMemSlicesInner(MemSliceSpanWrapper span, bool fin); |
| 532 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 533 | QuicStreamSequencer sequencer_; |
| 534 | QuicStreamId id_; |
| 535 | // Pointer to the owning QuicSession object. |
renjietang | f196f6a | 2020-02-12 12:34:23 -0800 | [diff] [blame] | 536 | // TODO(b/136274541): Remove session pointer from streams. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 537 | QuicSession* session_; |
renjietang | f196f6a | 2020-02-12 12:34:23 -0800 | [diff] [blame] | 538 | StreamDelegateInterface* stream_delegate_; |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 539 | // The precedence of the stream, once parsed. |
| 540 | spdy::SpdyStreamPrecedence precedence_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 541 | // Bytes read refers to payload bytes only: they do not include framing, |
| 542 | // encryption overhead etc. |
| 543 | uint64_t stream_bytes_read_; |
| 544 | |
| 545 | // Stream error code received from a RstStreamFrame or error code sent by the |
| 546 | // visitor or sequencer in the RstStreamFrame. |
| 547 | QuicRstStreamErrorCode stream_error_; |
| 548 | // Connection error code due to which the stream was closed. |stream_error_| |
| 549 | // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers |
| 550 | // should check |connection_error_|. |
| 551 | QuicErrorCode connection_error_; |
| 552 | |
| 553 | // True if the read side is closed and further frames should be rejected. |
| 554 | bool read_side_closed_; |
| 555 | // True if the write side is closed, and further writes should fail. |
| 556 | bool write_side_closed_; |
| 557 | |
| 558 | // True if the subclass has written a FIN with WriteOrBufferData, but it was |
| 559 | // buffered in queued_data_ rather than being sent to the session. |
| 560 | bool fin_buffered_; |
| 561 | // True if a FIN has been sent to the session. |
| 562 | bool fin_sent_; |
| 563 | // True if a FIN is waiting to be acked. |
| 564 | bool fin_outstanding_; |
| 565 | // True if a FIN is lost. |
| 566 | bool fin_lost_; |
| 567 | |
| 568 | // True if this stream has received (and the sequencer has accepted) a |
| 569 | // StreamFrame with the FIN set. |
| 570 | bool fin_received_; |
| 571 | |
| 572 | // True if an RST_STREAM has been sent to the session. |
| 573 | // In combination with fin_sent_, used to ensure that a FIN and/or a |
| 574 | // RST_STREAM is always sent to terminate the stream. |
| 575 | bool rst_sent_; |
| 576 | |
| 577 | // True if this stream has received a RST_STREAM frame. |
| 578 | bool rst_received_; |
| 579 | |
renjietang | 052df7c | 2020-10-13 14:46:09 -0700 | [diff] [blame] | 580 | // True if the stream has sent STOP_SENDING to the session. |
| 581 | bool stop_sending_sent_; |
| 582 | |
vasilvv | 7df418b | 2020-10-13 13:47:09 -0700 | [diff] [blame] | 583 | absl::optional<QuicFlowController> flow_controller_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 584 | |
| 585 | // The connection level flow controller. Not owned. |
| 586 | QuicFlowController* connection_flow_controller_; |
| 587 | |
| 588 | // Special streams, such as the crypto and headers streams, do not respect |
| 589 | // connection level flow control limits (but are stream level flow control |
| 590 | // limited). |
| 591 | bool stream_contributes_to_connection_flow_control_; |
| 592 | |
| 593 | // A counter incremented when OnCanWrite() is called and no progress is made. |
| 594 | // For debugging only. |
| 595 | size_t busy_counter_; |
| 596 | |
| 597 | // Indicates whether paddings will be added after the fin is consumed for this |
| 598 | // stream. |
| 599 | bool add_random_padding_after_fin_; |
| 600 | |
| 601 | // Send buffer of this stream. Send buffer is cleaned up when data gets acked |
| 602 | // or discarded. |
| 603 | QuicStreamSendBuffer send_buffer_; |
| 604 | |
bnc | 5a40142 | 2019-06-18 04:32:32 -0700 | [diff] [blame] | 605 | // Latched value of quic_buffered_data_threshold. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 606 | const QuicByteCount buffered_data_threshold_; |
| 607 | |
| 608 | // If true, then this stream has precedence over other streams for write |
| 609 | // scheduling. |
| 610 | const bool is_static_; |
| 611 | |
| 612 | // If initialized, reset this stream at this deadline. |
| 613 | QuicTime deadline_; |
| 614 | |
fayang | 6dfe58d | 2020-06-01 08:02:47 -0700 | [diff] [blame] | 615 | // True if this stream has entered draining state. |
fayang | be6d664 | 2020-04-16 14:15:34 -0700 | [diff] [blame] | 616 | bool was_draining_; |
| 617 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 618 | // Indicates whether this stream is bidirectional, read unidirectional or |
| 619 | // write unidirectional. |
| 620 | const StreamType type_; |
renjietang | ff3d3a3 | 2020-02-13 15:13:51 -0800 | [diff] [blame] | 621 | |
fayang | a330b7c | 2020-09-10 08:15:39 -0700 | [diff] [blame] | 622 | // Creation time of this stream, as reported by the QuicClock. |
| 623 | const QuicTime creation_time_; |
| 624 | |
renjietang | ff3d3a3 | 2020-02-13 15:13:51 -0800 | [diff] [blame] | 625 | Perspective perspective_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | } // namespace quic |
| 629 | |
| 630 | #endif // QUICHE_QUIC_CORE_QUIC_STREAM_H_ |