blob: 40bed1c19c9b0a84d7aa9cac3be82e97912576a1 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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>
vasilvv872e7a32019-03-12 16:42:44 -070023#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050024
QUICHE teama6ef0a62019-03-07 20:34:33 -050025#include "net/third_party/quiche/src/quic/core/quic_flow_controller.h"
26#include "net/third_party/quiche/src/quic/core/quic_packets.h"
27#include "net/third_party/quiche/src/quic/core/quic_stream_send_buffer.h"
28#include "net/third_party/quiche/src/quic/core/quic_stream_sequencer.h"
29#include "net/third_party/quiche/src/quic/core/quic_types.h"
30#include "net/third_party/quiche/src/quic/core/session_notifier_interface.h"
31#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
32#include "net/third_party/quiche/src/quic/platform/api/quic_mem_slice_span.h"
33#include "net/third_party/quiche/src/quic/platform/api/quic_reference_counted.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050034#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
35#include "net/third_party/quiche/src/spdy/core/spdy_protocol.h"
36
37namespace quic {
38
39namespace test {
40class QuicStreamPeer;
41} // namespace test
42
43class QuicSession;
44class QuicStream;
45
46// Buffers frames for a stream until the first byte of that frame arrives.
47class QUIC_EXPORT_PRIVATE PendingStream
48 : public QuicStreamSequencer::StreamInterface {
49 public:
50 PendingStream(QuicStreamId id, QuicSession* session);
51 PendingStream(const PendingStream&) = delete;
52 PendingStream(PendingStream&&) = default;
53 ~PendingStream() override = default;
54
55 // QuicStreamSequencer::StreamInterface
56 void OnDataAvailable() override;
57 void OnFinRead() override;
58 void AddBytesConsumed(QuicByteCount bytes) override;
59 void Reset(QuicRstStreamErrorCode error) override;
60 void CloseConnectionWithDetails(QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -070061 const std::string& details) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050062 QuicStreamId id() const override;
63 const QuicSocketAddress& PeerAddressOfLatestPacket() const override;
64
65 // Buffers the contents of |frame|. Frame must have a non-zero offset.
66 // If the data violates flow control, the connection will be closed.
67 void OnStreamFrame(const QuicStreamFrame& frame);
68
69 // Stores the final byte offset from |frame|.
70 // If the final offset violates flow control, the connection will be closed.
71 void OnRstStreamFrame(const QuicRstStreamFrame& frame);
72
73 // Returns the number of bytes read on this stream.
74 uint64_t stream_bytes_read() { return stream_bytes_read_; }
75
76 private:
77 friend class QuicStream;
78
79 bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset);
80
81 // ID of this stream.
82 QuicStreamId id_;
83
84 // Session which owns this.
85 QuicSession* session_;
86
87 // Bytes read refers to payload bytes only: they do not include framing,
88 // encryption overhead etc.
89 uint64_t stream_bytes_read_;
90
91 // True if a frame containing a fin has been received.
92 bool fin_received_;
93
94 // Connection-level flow controller. Owned by the session.
95 QuicFlowController* connection_flow_controller_;
96 // Stream-level flow controller.
97 QuicFlowController flow_controller_;
98 // Stores the buffered frames.
99 QuicStreamSequencer sequencer_;
100};
101
102class QUIC_EXPORT_PRIVATE QuicStream
103 : public QuicStreamSequencer::StreamInterface {
104 public:
105 // This is somewhat arbitrary. It's possible, but unlikely, we will either
106 // fail to set a priority client-side, or cancel a stream before stripping the
107 // priority from the wire server-side. In either case, start out with a
108 // priority in the middle.
109 static const spdy::SpdyPriority kDefaultPriority = 3;
110 static_assert(kDefaultPriority ==
111 (spdy::kV3LowestPriority + spdy::kV3HighestPriority) / 2,
112 "Unexpected value of kDefaultPriority");
113
114 // Creates a new stream with stream_id |id| associated with |session|. If
115 // |is_static| is true, then the stream will be given precedence
116 // over other streams when determing what streams should write next.
117 // |type| indicates whether the stream is bidirectional, read unidirectional
118 // or write unidirectional.
119 // TODO(fayang): Remove |type| when IETF stream ID numbering fully kicks in.
120 QuicStream(QuicStreamId id,
121 QuicSession* session,
122 bool is_static,
123 StreamType type);
124 QuicStream(PendingStream pending, StreamType type);
125 QuicStream(const QuicStream&) = delete;
126 QuicStream& operator=(const QuicStream&) = delete;
127
128 virtual ~QuicStream();
129
130 // Not in use currently.
131 void SetFromConfig();
132
133 // QuicStreamSequencer::StreamInterface implementation.
134 QuicStreamId id() const override { return id_; }
135 // Called by the stream subclass after it has consumed the final incoming
136 // data.
137 void OnFinRead() override;
138
139 // Called by the subclass or the sequencer to reset the stream from this
140 // end.
141 void Reset(QuicRstStreamErrorCode error) override;
142
143 // Called by the subclass or the sequencer to close the entire connection from
144 // this end.
145 void CloseConnectionWithDetails(QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -0700146 const std::string& details) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500147
QUICHE teama6ef0a62019-03-07 20:34:33 -0500148 // Get peer IP of the lastest packet which connection is dealing/delt with.
149 const QuicSocketAddress& PeerAddressOfLatestPacket() const override;
150
151 // Called by the session when a (potentially duplicate) stream frame has been
152 // received for this stream.
153 virtual void OnStreamFrame(const QuicStreamFrame& frame);
154
155 // Called by the session when the connection becomes writeable to allow the
156 // stream to write any pending data.
157 virtual void OnCanWrite();
158
159 // Called by the session just before the object is destroyed.
160 // The object should not be accessed after OnClose is called.
161 // Sends a RST_STREAM with code QUIC_RST_ACKNOWLEDGEMENT if neither a FIN nor
162 // a RST_STREAM has been sent.
163 virtual void OnClose();
164
165 // Called by the session when the endpoint receives a RST_STREAM from the
166 // peer.
167 virtual void OnStreamReset(const QuicRstStreamFrame& frame);
168
169 // Called by the session when the endpoint receives or sends a connection
170 // close, and should immediately close the stream.
171 virtual void OnConnectionClosed(QuicErrorCode error,
172 ConnectionCloseSource source);
173
174 spdy::SpdyPriority priority() const;
175
176 // Sets priority_ to priority. This should only be called before bytes are
177 // written to the server.
178 void SetPriority(spdy::SpdyPriority priority);
179
180 // Returns true if this stream is still waiting for acks of sent data.
181 // This will return false if all data has been acked, or if the stream
182 // is no longer interested in data being acked (which happens when
183 // a stream is reset because of an error).
184 bool IsWaitingForAcks() const;
185
186 // Number of bytes available to read.
187 size_t ReadableBytes() const;
188
189 QuicRstStreamErrorCode stream_error() const { return stream_error_; }
190 QuicErrorCode connection_error() const { return connection_error_; }
191
192 bool reading_stopped() const {
193 return sequencer_.ignore_read_data() || read_side_closed_;
194 }
195 bool write_side_closed() const { return write_side_closed_; }
196
197 bool rst_received() const { return rst_received_; }
198 bool rst_sent() const { return rst_sent_; }
199 bool fin_received() const { return fin_received_; }
200 bool fin_sent() const { return fin_sent_; }
201 bool fin_outstanding() const { return fin_outstanding_; }
202 bool fin_lost() const { return fin_lost_; }
203
204 uint64_t BufferedDataBytes() const;
205
206 uint64_t stream_bytes_read() const { return stream_bytes_read_; }
207 uint64_t stream_bytes_written() const;
208
209 size_t busy_counter() const { return busy_counter_; }
210 void set_busy_counter(size_t busy_counter) { busy_counter_ = busy_counter; }
211
212 void set_fin_sent(bool fin_sent) { fin_sent_ = fin_sent; }
213 void set_fin_received(bool fin_received) { fin_received_ = fin_received; }
214 void set_rst_sent(bool rst_sent) { rst_sent_ = rst_sent; }
215
216 void set_rst_received(bool rst_received) { rst_received_ = rst_received; }
217 void set_stream_error(QuicRstStreamErrorCode error) { stream_error_ = error; }
218
219 // Adjust the flow control window according to new offset in |frame|.
220 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame);
221
222 int num_frames_received() const;
223 int num_duplicate_frames_received() const;
224
225 QuicFlowController* flow_controller() { return &flow_controller_; }
226
227 // Called when endpoint receives a frame which could increase the highest
228 // offset.
229 // Returns true if the highest offset did increase.
230 bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500231
232 // Updates the flow controller's send window offset and calls OnCanWrite if
233 // it was blocked before.
234 void UpdateSendWindowOffset(QuicStreamOffset new_offset);
235
236 // Returns true if the stream has received either a RST_STREAM or a FIN -
237 // either of which gives a definitive number of bytes which the peer has
238 // sent. If this is not true on deletion of the stream object, the session
239 // must keep track of the stream's byte offset until a definitive final value
240 // arrives.
241 bool HasFinalReceivedByteOffset() const {
242 return fin_received_ || rst_received_;
243 }
244
245 // Returns true if the stream has queued data waiting to write.
246 bool HasBufferedData() const;
247
248 // Returns the version of QUIC being used for this stream.
249 QuicTransportVersion transport_version() const;
250
251 // Returns the crypto handshake protocol that was used on this stream's
252 // connection.
253 HandshakeProtocol handshake_protocol() const;
254
255 // Sets the sequencer to consume all incoming data itself and not call
256 // OnDataAvailable().
257 // When the FIN is received, the stream will be notified automatically (via
258 // OnFinRead()) (which may happen during the call of StopReading()).
259 // TODO(dworley): There should be machinery to send a RST_STREAM/NO_ERROR and
260 // stop sending stream-level flow-control updates when this end sends FIN.
261 virtual void StopReading();
262
263 // Sends as much of 'data' to the connection as the connection will consume,
264 // and then buffers any remaining data in queued_data_.
265 // If fin is true: if it is immediately passed on to the session,
266 // write_side_closed() becomes true, otherwise fin_buffered_ becomes true.
267 void WriteOrBufferData(
268 QuicStringPiece data,
269 bool fin,
270 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener);
271
272 // Adds random padding after the fin is consumed for this stream.
273 void AddRandomPaddingAfterFin();
274
275 // Write |data_length| of data starts at |offset| from send buffer.
276 bool WriteStreamData(QuicStreamOffset offset,
277 QuicByteCount data_length,
278 QuicDataWriter* writer);
279
280 // Called when data [offset, offset + data_length) is acked. |fin_acked|
281 // indicates whether the fin is acked. Returns true and updates
282 // |newly_acked_length| if any new stream data (including fin) gets acked.
283 virtual bool OnStreamFrameAcked(QuicStreamOffset offset,
284 QuicByteCount data_length,
285 bool fin_acked,
286 QuicTime::Delta ack_delay_time,
287 QuicByteCount* newly_acked_length);
288
289 // Called when data [offset, offset + data_length) was retransmitted.
290 // |fin_retransmitted| indicates whether fin was retransmitted.
291 virtual void OnStreamFrameRetransmitted(QuicStreamOffset offset,
292 QuicByteCount data_length,
293 bool fin_retransmitted);
294
295 // Called when data [offset, offset + data_length) is considered as lost.
296 // |fin_lost| indicates whether the fin is considered as lost.
297 virtual void OnStreamFrameLost(QuicStreamOffset offset,
298 QuicByteCount data_length,
299 bool fin_lost);
300
301 // Called to retransmit outstanding portion in data [offset, offset +
302 // data_length) and |fin|. Returns true if all data gets retransmitted.
303 virtual bool RetransmitStreamData(QuicStreamOffset offset,
304 QuicByteCount data_length,
305 bool fin);
306
307 // Sets deadline of this stream to be now + |ttl|, returns true if the setting
308 // succeeds.
309 bool MaybeSetTtl(QuicTime::Delta ttl);
310
311 // Same as WritevData except data is provided in reference counted memory so
312 // that data copy is avoided.
313 QuicConsumedData WriteMemSlices(QuicMemSliceSpan span, bool fin);
314
315 // Returns true if any stream data is lost (including fin) and needs to be
316 // retransmitted.
317 virtual bool HasPendingRetransmission() const;
318
319 // Returns true if any portion of data [offset, offset + data_length) is
320 // outstanding or fin is outstanding (if |fin| is true). Returns false
321 // otherwise.
322 bool IsStreamFrameOutstanding(QuicStreamOffset offset,
323 QuicByteCount data_length,
324 bool fin) const;
325
326 StreamType type() const { return type_; }
327
328 // Creates and sends a STOP_SENDING frame. This can be called regardless of
329 // the version that has been negotiated. If not IETF QUIC/Version 99 then the
330 // method is a noop, relieving the application of the necessity of
331 // understanding the connection's QUIC version and knowing whether it can call
332 // this method or not.
333 void SendStopSending(uint16_t code);
334
335 // Invoked when QUIC receives a STOP_SENDING frame for this stream, informing
336 // the application that the peer has sent a STOP_SENDING. The default
337 // implementation is a noop. Is to be overridden by the application-specific
338 // QuicStream class.
339 virtual void OnStopSending(uint16_t code);
340
341 // Close the write side of the socket. Further writes will fail.
342 // Can be called by the subclass or internally.
343 // Does not send a FIN. May cause the stream to be closed.
344 virtual void CloseWriteSide();
345
346 protected:
347 // Sends as many bytes in the first |count| buffers of |iov| to the connection
348 // as the connection will consume. If FIN is consumed, the write side is
349 // immediately closed.
350 // Returns the number of bytes consumed by the connection.
351 // Please note: Returned consumed data is the amount of data saved in send
352 // buffer. The data is not necessarily consumed by the connection. So write
353 // side is closed when FIN is sent.
354 // TODO(fayang): Let WritevData return boolean.
355 QuicConsumedData WritevData(const struct iovec* iov, int iov_count, bool fin);
356
357 // Allows override of the session level writev, for the force HOL
358 // blocking experiment.
359 virtual QuicConsumedData WritevDataInner(size_t write_length,
360 QuicStreamOffset offset,
361 bool fin);
362
363 // Close the read side of the socket. May cause the stream to be closed.
364 // Subclasses and consumers should use StopReading to terminate reading early
365 // if expecting a FIN. Can be used directly by subclasses if not expecting a
366 // FIN.
367 void CloseReadSide();
368
369 // Called when data of [offset, offset + data_length] is buffered in send
370 // buffer.
371 virtual void OnDataBuffered(
372 QuicStreamOffset offset,
373 QuicByteCount data_length,
374 const QuicReferenceCountedPointer<QuicAckListenerInterface>&
375 ack_listener) {}
376
377 // True if buffered data in send buffer is below buffered_data_threshold_.
378 bool CanWriteNewData() const;
379
380 // True if buffered data in send buffer is still below
381 // buffered_data_threshold_ even after writing |length| bytes.
382 bool CanWriteNewDataAfterData(QuicByteCount length) const;
383
384 // Called when upper layer can write new data.
385 virtual void OnCanWriteNewData() {}
386
387 // Called when |bytes_consumed| bytes has been consumed.
388 virtual void OnStreamDataConsumed(size_t bytes_consumed);
389
ianswett93637202019-04-03 08:05:29 -0700390 // Called by the stream sequencer as bytes are consumed from the buffer.
391 // If the receive window has dropped below the threshold, then send a
392 // WINDOW_UPDATE frame.
393 void AddBytesConsumed(QuicByteCount bytes) override;
394
QUICHE teama6ef0a62019-03-07 20:34:33 -0500395 // Writes pending retransmissions if any.
396 virtual void WritePendingRetransmission();
397
398 // This is called when stream tries to retransmit data after deadline_. Make
399 // this virtual so that subclasses can implement their own logics.
400 virtual void OnDeadlinePassed();
401
402 bool fin_buffered() const { return fin_buffered_; }
403
404 const QuicSession* session() const { return session_; }
405 QuicSession* session() { return session_; }
406
407 const QuicStreamSequencer* sequencer() const { return &sequencer_; }
408 QuicStreamSequencer* sequencer() { return &sequencer_; }
409
410 void DisableConnectionFlowControlForThisStream() {
411 stream_contributes_to_connection_flow_control_ = false;
412 }
413
414 const QuicIntervalSet<QuicStreamOffset>& bytes_acked() const;
415
416 const QuicStreamSendBuffer& send_buffer() const { return send_buffer_; }
417
418 QuicStreamSendBuffer& send_buffer() { return send_buffer_; }
419
420 private:
421 friend class test::QuicStreamPeer;
422 friend class QuicStreamUtils;
423
424 QuicStream(QuicStreamId id,
425 QuicSession* session,
426 QuicStreamSequencer sequencer,
427 bool is_static,
428 StreamType type,
429 uint64_t stream_bytes_read,
430 bool fin_received,
431 QuicFlowController flow_controller,
432 QuicFlowController* connection_flow_controller);
433
434 // Subclasses and consumers should use reading_stopped.
435 bool read_side_closed() const { return read_side_closed_; }
436
437 // Calls MaybeSendBlocked on the stream's flow controller and the connection
438 // level flow controller. If the stream is flow control blocked by the
439 // connection-level flow controller but not by the stream-level flow
440 // controller, marks this stream as connection-level write blocked.
441 void MaybeSendBlocked();
442
443 // Write buffered data in send buffer. TODO(fayang): Consider combine
444 // WriteOrBufferData, Writev and WriteBufferedData.
445 void WriteBufferedData();
446
ianswett93637202019-04-03 08:05:29 -0700447 // Called when bytes are sent to the peer.
448 void AddBytesSent(QuicByteCount bytes);
449
QUICHE teama6ef0a62019-03-07 20:34:33 -0500450 // Returns true if deadline_ has passed.
451 bool HasDeadlinePassed() const;
452
453 QuicStreamSequencer sequencer_;
454 QuicStreamId id_;
455 // Pointer to the owning QuicSession object.
456 QuicSession* session_;
457 // The priority of the stream, once parsed.
458 spdy::SpdyPriority priority_;
459 // Bytes read refers to payload bytes only: they do not include framing,
460 // encryption overhead etc.
461 uint64_t stream_bytes_read_;
462
463 // Stream error code received from a RstStreamFrame or error code sent by the
464 // visitor or sequencer in the RstStreamFrame.
465 QuicRstStreamErrorCode stream_error_;
466 // Connection error code due to which the stream was closed. |stream_error_|
467 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
468 // should check |connection_error_|.
469 QuicErrorCode connection_error_;
470
471 // True if the read side is closed and further frames should be rejected.
472 bool read_side_closed_;
473 // True if the write side is closed, and further writes should fail.
474 bool write_side_closed_;
475
476 // True if the subclass has written a FIN with WriteOrBufferData, but it was
477 // buffered in queued_data_ rather than being sent to the session.
478 bool fin_buffered_;
479 // True if a FIN has been sent to the session.
480 bool fin_sent_;
481 // True if a FIN is waiting to be acked.
482 bool fin_outstanding_;
483 // True if a FIN is lost.
484 bool fin_lost_;
485
486 // True if this stream has received (and the sequencer has accepted) a
487 // StreamFrame with the FIN set.
488 bool fin_received_;
489
490 // True if an RST_STREAM has been sent to the session.
491 // In combination with fin_sent_, used to ensure that a FIN and/or a
492 // RST_STREAM is always sent to terminate the stream.
493 bool rst_sent_;
494
495 // True if this stream has received a RST_STREAM frame.
496 bool rst_received_;
497
498 // Tracks if the session this stream is running under was created by a
499 // server or a client.
500 Perspective perspective_;
501
502 QuicFlowController flow_controller_;
503
504 // The connection level flow controller. Not owned.
505 QuicFlowController* connection_flow_controller_;
506
507 // Special streams, such as the crypto and headers streams, do not respect
508 // connection level flow control limits (but are stream level flow control
509 // limited).
510 bool stream_contributes_to_connection_flow_control_;
511
512 // A counter incremented when OnCanWrite() is called and no progress is made.
513 // For debugging only.
514 size_t busy_counter_;
515
516 // Indicates whether paddings will be added after the fin is consumed for this
517 // stream.
518 bool add_random_padding_after_fin_;
519
520 // Send buffer of this stream. Send buffer is cleaned up when data gets acked
521 // or discarded.
522 QuicStreamSendBuffer send_buffer_;
523
524 // Latched value of FLAGS_quic_buffered_data_threshold.
525 const QuicByteCount buffered_data_threshold_;
526
527 // If true, then this stream has precedence over other streams for write
528 // scheduling.
529 const bool is_static_;
530
531 // If initialized, reset this stream at this deadline.
532 QuicTime deadline_;
533
534 // Indicates whether this stream is bidirectional, read unidirectional or
535 // write unidirectional.
536 const StreamType type_;
537};
538
539} // namespace quic
540
541#endif // QUICHE_QUIC_CORE_QUIC_STREAM_H_