blob: a76e7df6c222b97b4831e69944501d0d51c599b5 [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// A QuicSession, which demuxes a single connection to individual streams.
6
7#ifndef QUICHE_QUIC_CORE_QUIC_SESSION_H_
8#define QUICHE_QUIC_CORE_QUIC_SESSION_H_
9
10#include <cstddef>
11#include <map>
12#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -070013#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050014#include <vector>
15
QUICHE teama6ef0a62019-03-07 20:34:33 -050016#include "net/third_party/quiche/src/quic/core/legacy_quic_stream_id_manager.h"
17#include "net/third_party/quiche/src/quic/core/quic_connection.h"
18#include "net/third_party/quiche/src/quic/core/quic_control_frame_manager.h"
19#include "net/third_party/quiche/src/quic/core/quic_crypto_stream.h"
wub2b5942f2019-04-11 13:22:50 -070020#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050021#include "net/third_party/quiche/src/quic/core/quic_packet_creator.h"
22#include "net/third_party/quiche/src/quic/core/quic_packets.h"
23#include "net/third_party/quiche/src/quic/core/quic_stream.h"
24#include "net/third_party/quiche/src/quic/core/quic_stream_frame_data_producer.h"
25#include "net/third_party/quiche/src/quic/core/quic_write_blocked_list.h"
26#include "net/third_party/quiche/src/quic/core/session_notifier_interface.h"
27#include "net/third_party/quiche/src/quic/core/uber_quic_stream_id_manager.h"
28#include "net/third_party/quiche/src/quic/platform/api/quic_containers.h"
29#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
30#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050031
32namespace quic {
33
34class QuicCryptoStream;
35class QuicFlowController;
36class QuicStream;
37class QuicStreamIdManager;
38
39namespace test {
40class QuicSessionPeer;
41} // namespace test
42
43class QUIC_EXPORT_PRIVATE QuicSession : public QuicConnectionVisitorInterface,
44 public SessionNotifierInterface,
45 public QuicStreamFrameDataProducer {
46 public:
47 // An interface from the session to the entity owning the session.
48 // This lets the session notify its owner (the Dispatcher) when the connection
49 // is closed, blocked, or added/removed from the time-wait list.
50 class Visitor {
51 public:
52 virtual ~Visitor() {}
53
54 // Called when the connection is closed after the streams have been closed.
dschinazi7b9278c2019-05-20 07:36:21 -070055 virtual void OnConnectionClosed(QuicConnectionId server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -050056 QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -070057 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050058 ConnectionCloseSource source) = 0;
59
60 // Called when the session has become write blocked.
61 virtual void OnWriteBlocked(QuicBlockedWriterInterface* blocked_writer) = 0;
62
63 // Called when the session receives reset on a stream from the peer.
64 virtual void OnRstStreamReceived(const QuicRstStreamFrame& frame) = 0;
65
66 // Called when the session receives a STOP_SENDING for a stream from the
67 // peer.
68 virtual void OnStopSendingReceived(const QuicStopSendingFrame& frame) = 0;
69 };
70
71 // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
72 enum CryptoHandshakeEvent {
73 // ENCRYPTION_FIRST_ESTABLISHED indicates that a full client hello has been
74 // sent by a client and that subsequent packets will be encrypted. (Client
75 // only.)
76 ENCRYPTION_FIRST_ESTABLISHED,
77 // ENCRYPTION_REESTABLISHED indicates that a client hello was rejected by
78 // the server and thus the encryption key has been updated. Therefore the
79 // connection should resend any packets that were sent under
80 // ENCRYPTION_ZERO_RTT. (Client only.)
81 ENCRYPTION_REESTABLISHED,
82 // HANDSHAKE_CONFIRMED, in a client, indicates the server has accepted
83 // our handshake. In a server it indicates that a full, valid client hello
84 // has been received. (Client and server.)
85 HANDSHAKE_CONFIRMED,
86 };
87
88 // Does not take ownership of |connection| or |visitor|.
89 QuicSession(QuicConnection* connection,
90 Visitor* owner,
91 const QuicConfig& config,
92 const ParsedQuicVersionVector& supported_versions);
93 QuicSession(const QuicSession&) = delete;
94 QuicSession& operator=(const QuicSession&) = delete;
95
96 ~QuicSession() override;
97
98 virtual void Initialize();
99
100 // QuicConnectionVisitorInterface methods:
101 void OnStreamFrame(const QuicStreamFrame& frame) override;
102 void OnCryptoFrame(const QuicCryptoFrame& frame) override;
103 void OnRstStream(const QuicRstStreamFrame& frame) override;
104 void OnGoAway(const QuicGoAwayFrame& frame) override;
105 void OnMessageReceived(QuicStringPiece message) override;
106 void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override;
107 void OnBlockedFrame(const QuicBlockedFrame& frame) override;
108 void OnConnectionClosed(QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -0700109 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500110 ConnectionCloseSource source) override;
111 void OnWriteBlocked() override;
112 void OnSuccessfulVersionNegotiation(
113 const ParsedQuicVersion& version) override;
114 void OnConnectivityProbeReceived(
115 const QuicSocketAddress& self_address,
116 const QuicSocketAddress& peer_address) override;
117 void OnCanWrite() override;
QUICHE teamb8343252019-04-29 13:58:01 -0700118 bool SendProbingData() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500119 void OnCongestionWindowChange(QuicTime /*now*/) override {}
120 void OnConnectionMigration(AddressChangeType type) override {}
121 // Adds a connection level WINDOW_UPDATE frame.
122 void OnAckNeedsRetransmittableFrame() override;
123 void SendPing() override;
124 bool WillingAndAbleToWrite() const override;
125 bool HasPendingHandshake() const override;
126 void OnPathDegrading() override;
127 bool AllowSelfAddressChange() const override;
128 void OnForwardProgressConfirmed() override;
fkastenholz3c4eabf2019-04-22 07:49:59 -0700129 bool OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame) override;
130 bool OnStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500131 bool OnStopSendingFrame(const QuicStopSendingFrame& frame) override;
132
133 // QuicStreamFrameDataProducer
134 WriteStreamDataResult WriteStreamData(QuicStreamId id,
135 QuicStreamOffset offset,
136 QuicByteCount data_length,
137 QuicDataWriter* writer) override;
138 bool WriteCryptoData(EncryptionLevel level,
139 QuicStreamOffset offset,
140 QuicByteCount data_length,
141 QuicDataWriter* writer) override;
142
143 // SessionNotifierInterface methods:
144 bool OnFrameAcked(const QuicFrame& frame,
QUICHE team9467db02019-05-30 09:38:45 -0700145 QuicTime::Delta ack_delay_time,
146 QuicTime receive_timestamp) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500147 void OnStreamFrameRetransmitted(const QuicStreamFrame& frame) override;
148 void OnFrameLost(const QuicFrame& frame) override;
149 void RetransmitFrames(const QuicFrames& frames,
150 TransmissionType type) override;
151 bool IsFrameOutstanding(const QuicFrame& frame) const override;
152 bool HasUnackedCryptoData() const override;
zhongyi1b2f7832019-06-14 13:31:34 -0700153 bool HasUnackedStreamData() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500154
155 // Called on every incoming packet. Passes |packet| through to |connection_|.
156 virtual void ProcessUdpPacket(const QuicSocketAddress& self_address,
157 const QuicSocketAddress& peer_address,
158 const QuicReceivedPacket& packet);
159
160 // Called by streams when they want to write data to the peer.
161 // Returns a pair with the number of bytes consumed from data, and a boolean
162 // indicating if the fin bit was consumed. This does not indicate the data
163 // has been sent on the wire: it may have been turned into a packet and queued
164 // if the socket was unexpectedly blocked.
165 virtual QuicConsumedData WritevData(QuicStream* stream,
166 QuicStreamId id,
167 size_t write_length,
168 QuicStreamOffset offset,
169 StreamSendingState state);
170
171 // Called by application to send |message|. Data copy can be avoided if
172 // |message| is provided in reference counted memory.
173 // Please note, |message| provided in reference counted memory would be moved
174 // internally when message is successfully sent. Thereafter, it would be
175 // undefined behavior if callers try to access the slices through their own
176 // copy of the span object.
177 // Returns the message result which includes the message status and message ID
178 // (valid if the write succeeds). SendMessage flushes a message packet even it
179 // is not full. If the application wants to bundle other data in the same
180 // packet, please consider adding a packet flusher around the SendMessage
181 // and/or WritevData calls.
182 //
183 // OnMessageAcked and OnMessageLost are called when a particular message gets
184 // acked or lost.
185 //
186 // Note that SendMessage will fail with status = MESSAGE_STATUS_BLOCKED
187 // if connection is congestion control blocked or underlying socket is write
188 // blocked. In this case the caller can retry sending message again when
189 // connection becomes available, for example after getting OnCanWrite()
190 // callback.
191 MessageResult SendMessage(QuicMemSliceSpan message);
192
193 // Called when message with |message_id| gets acked.
QUICHE team9467db02019-05-30 09:38:45 -0700194 virtual void OnMessageAcked(QuicMessageId message_id,
195 QuicTime receive_timestamp);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500196
197 // Called when message with |message_id| is considered as lost.
198 virtual void OnMessageLost(QuicMessageId message_id);
199
200 // Called by control frame manager when it wants to write control frames to
201 // the peer. Returns true if |frame| is consumed, false otherwise.
202 virtual bool WriteControlFrame(const QuicFrame& frame);
203
204 // Called by streams when they want to close the stream in both directions.
205 virtual void SendRstStream(QuicStreamId id,
206 QuicRstStreamErrorCode error,
207 QuicStreamOffset bytes_written);
208
209 // Called when the session wants to go away and not accept any new streams.
vasilvvc48c8712019-03-11 13:38:16 -0700210 virtual void SendGoAway(QuicErrorCode error_code, const std::string& reason);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500211
212 // Sends a BLOCKED frame.
213 virtual void SendBlocked(QuicStreamId id);
214
215 // Sends a WINDOW_UPDATE frame.
216 virtual void SendWindowUpdate(QuicStreamId id, QuicStreamOffset byte_offset);
217
fkastenholz3c4eabf2019-04-22 07:49:59 -0700218 // Send a MAX_STREAMS frame.
219 void SendMaxStreams(QuicStreamCount stream_count, bool unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500220
fkastenholz3c4eabf2019-04-22 07:49:59 -0700221 // Send a STREAMS_BLOCKED frame.
222 void SendStreamsBlocked(QuicStreamCount stream_count, bool unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500223
224 // Create and transmit a STOP_SENDING frame
225 virtual void SendStopSending(uint16_t code, QuicStreamId stream_id);
226
227 // Removes the stream associated with 'stream_id' from the active stream map.
228 virtual void CloseStream(QuicStreamId stream_id);
229
230 // Returns true if outgoing packets will be encrypted, even if the server
231 // hasn't confirmed the handshake yet.
232 virtual bool IsEncryptionEstablished() const;
233
234 // For a client, returns true if the server has confirmed our handshake. For
235 // a server, returns true if a full, valid client hello has been received.
236 virtual bool IsCryptoHandshakeConfirmed() const;
237
238 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
239 virtual void OnConfigNegotiated();
240
241 // Called by the QuicCryptoStream when the handshake enters a new state.
242 //
243 // Clients will call this function in the order:
244 // ENCRYPTION_FIRST_ESTABLISHED
245 // zero or more ENCRYPTION_REESTABLISHED
246 // HANDSHAKE_CONFIRMED
247 //
248 // Servers will simply call it once with HANDSHAKE_CONFIRMED.
249 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event);
250
251 // Called by the QuicCryptoStream when a handshake message is sent.
252 virtual void OnCryptoHandshakeMessageSent(
253 const CryptoHandshakeMessage& message);
254
255 // Called by the QuicCryptoStream when a handshake message is received.
256 virtual void OnCryptoHandshakeMessageReceived(
257 const CryptoHandshakeMessage& message);
258
259 // Called by the stream on creation to set priority in the write blocked list.
260 virtual void RegisterStreamPriority(QuicStreamId id,
261 bool is_static,
262 spdy::SpdyPriority priority);
263 // Called by the stream on deletion to clear priority from the write blocked
264 // list.
265 virtual void UnregisterStreamPriority(QuicStreamId id, bool is_static);
266 // Called by the stream on SetPriority to update priority on the write blocked
267 // list.
268 virtual void UpdateStreamPriority(QuicStreamId id,
269 spdy::SpdyPriority new_priority);
270
271 // Returns mutable config for this session. Returned config is owned
272 // by QuicSession.
273 QuicConfig* config();
274
275 // Returns true if the stream existed previously and has been closed.
276 // Returns false if the stream is still active or if the stream has
277 // not yet been created.
278 bool IsClosedStream(QuicStreamId id);
279
280 QuicConnection* connection() { return connection_; }
281 const QuicConnection* connection() const { return connection_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500282 const QuicSocketAddress& peer_address() const {
283 return connection_->peer_address();
284 }
285 const QuicSocketAddress& self_address() const {
286 return connection_->self_address();
287 }
288 QuicConnectionId connection_id() const {
289 return connection_->connection_id();
290 }
291
292 // Returns the number of currently open streams, excluding the reserved
293 // headers and crypto streams, and never counting unfinished streams.
294 size_t GetNumActiveStreams() const;
295
296 // Returns the number of currently draining streams.
297 size_t GetNumDrainingStreams() const;
298
299 // Returns the number of currently open peer initiated streams, excluding the
300 // reserved headers and crypto streams.
301 size_t GetNumOpenIncomingStreams() const;
302
303 // Returns the number of currently open self initiated streams, excluding the
304 // reserved headers and crypto streams.
305 size_t GetNumOpenOutgoingStreams() const;
306
renjietangfbeb5bf2019-04-19 15:06:20 -0700307 // Returns the number of open peer initiated static streams.
308 size_t num_incoming_static_streams() const {
309 return num_incoming_static_streams_;
310 }
311
312 // Returns the number of open self initiated static streams.
313 size_t num_outgoing_static_streams() const {
314 return num_outgoing_static_streams_;
315 }
316
QUICHE teama6ef0a62019-03-07 20:34:33 -0500317 // Add the stream to the session's write-blocked list because it is blocked by
318 // connection-level flow control but not by its own stream-level flow control.
319 // The stream will be given a chance to write when a connection-level
320 // WINDOW_UPDATE arrives.
321 void MarkConnectionLevelWriteBlocked(QuicStreamId id);
322
323 // Called when stream |id| is done waiting for acks either because all data
324 // gets acked or is not interested in data being acked (which happens when
325 // a stream is reset because of an error).
326 void OnStreamDoneWaitingForAcks(QuicStreamId id);
327
zhongyi1b2f7832019-06-14 13:31:34 -0700328 // Called when stream |id| is newly waiting for acks.
329 void OnStreamWaitingForAcks(QuicStreamId id);
330
QUICHE teama6ef0a62019-03-07 20:34:33 -0500331 // Called to cancel retransmission of unencypted crypto stream data.
332 void NeuterUnencryptedData();
333
334 // Returns true if the session has data to be sent, either queued in the
335 // connection, or in a write-blocked stream.
336 bool HasDataToWrite() const;
337
338 // Returns the largest payload that will fit into a single MESSAGE frame.
339 // Because overhead can vary during a connection, this method should be
340 // checked for every message.
ianswettb239f862019-04-05 09:15:06 -0700341 QuicPacketLength GetCurrentLargestMessagePayload() const;
342
343 // Returns the largest payload that will fit into a single MESSAGE frame at
344 // any point during the connection. This assumes the version and
345 // connection ID lengths do not change.
346 QuicPacketLength GetGuaranteedLargestMessagePayload() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500347
348 bool goaway_sent() const { return goaway_sent_; }
349
350 bool goaway_received() const { return goaway_received_; }
351
352 QuicErrorCode error() const { return error_; }
353
354 Perspective perspective() const { return connection_->perspective(); }
355
356 QuicFlowController* flow_controller() { return &flow_controller_; }
357
358 // Returns true if connection is flow controller blocked.
359 bool IsConnectionFlowControlBlocked() const;
360
361 // Returns true if any stream is flow controller blocked.
362 bool IsStreamFlowControlBlocked();
363
364 size_t max_open_incoming_bidirectional_streams() const;
365 size_t max_open_incoming_unidirectional_streams() const;
366
367 size_t MaxAvailableBidirectionalStreams() const;
368 size_t MaxAvailableUnidirectionalStreams() const;
369
370 // Returns existing static or dynamic stream with id = |stream_id|. If no
371 // such stream exists, and |stream_id| is a peer-created dynamic stream id,
372 // then a new stream is created and returned. In all other cases, nullptr is
373 // returned.
374 QuicStream* GetOrCreateStream(const QuicStreamId stream_id);
375
376 // Mark a stream as draining.
377 virtual void StreamDraining(QuicStreamId id);
378
379 // Returns true if this stream should yield writes to another blocked stream.
380 bool ShouldYield(QuicStreamId stream_id);
381
382 // Set transmission type of next sending packets.
383 void SetTransmissionType(TransmissionType type);
384
385 // Clean up closed_streams_.
386 void CleanUpClosedStreams();
387
388 bool session_decides_what_to_write() const;
389
390 const ParsedQuicVersionVector& supported_versions() const {
391 return supported_versions_;
392 }
393
394 // Called when new outgoing streams are available to be opened. This occurs
395 // when an extant, open, stream is moved to draining or closed. The default
396 // implementation does nothing.
397 virtual void OnCanCreateNewOutgoingStream();
398
399 QuicStreamId next_outgoing_bidirectional_stream_id() const;
400 QuicStreamId next_outgoing_unidirectional_stream_id() const;
401
402 // Return true if given stream is peer initiated.
403 bool IsIncomingStream(QuicStreamId id) const;
404
405 size_t GetNumLocallyClosedOutgoingStreamsHighestOffset() const;
406
407 size_t num_locally_closed_incoming_streams_highest_offset() const {
408 return num_locally_closed_incoming_streams_highest_offset_;
409 }
410
411 // Does actual work of sending reset-stream or reset-stream&stop-sending
412 // If the connection is not version 99/IETF QUIC, will always send a
413 // RESET_STREAM and close_write_side_only is ignored. If the connection is
414 // IETF QUIC/Version 99 then will send a RESET_STREAM and STOP_SENDING if
415 // close_write_side_only is false, just a RESET_STREAM if
416 // close_write_side_only is true.
417 virtual void SendRstStreamInner(QuicStreamId id,
418 QuicRstStreamErrorCode error,
419 QuicStreamOffset bytes_written,
420 bool close_write_side_only);
421
wub2b5942f2019-04-11 13:22:50 -0700422 // Record errors when a connection is closed at the server side, should only
423 // be called from server's perspective.
424 // Noop if |error| is QUIC_NO_ERROR.
425 static void RecordConnectionCloseAtServer(QuicErrorCode error,
426 ConnectionCloseSource source);
427
fkastenholzd3a1de92019-05-15 07:00:07 -0700428 inline QuicTransportVersion transport_version() const {
429 return connection_->transport_version();
430 }
431
QUICHE teama6ef0a62019-03-07 20:34:33 -0500432 protected:
433 using StaticStreamMap = QuicSmallMap<QuicStreamId, QuicStream*, 2>;
434
435 using DynamicStreamMap =
436 QuicSmallMap<QuicStreamId, std::unique_ptr<QuicStream>, 10>;
437
438 using PendingStreamMap =
439 QuicSmallMap<QuicStreamId, std::unique_ptr<PendingStream>, 10>;
440
441 using ClosedStreams = std::vector<std::unique_ptr<QuicStream>>;
442
443 using ZombieStreamMap =
444 QuicSmallMap<QuicStreamId, std::unique_ptr<QuicStream>, 10>;
445
446 // Creates a new stream to handle a peer-initiated stream.
447 // Caller does not own the returned stream.
448 // Returns nullptr and does error handling if the stream can not be created.
449 virtual QuicStream* CreateIncomingStream(QuicStreamId id) = 0;
renjietangbaea59c2019-05-29 15:08:14 -0700450 virtual QuicStream* CreateIncomingStream(PendingStream* pending) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500451
452 // Return the reserved crypto stream.
453 virtual QuicCryptoStream* GetMutableCryptoStream() = 0;
454
455 // Return the reserved crypto stream as a constant pointer.
456 virtual const QuicCryptoStream* GetCryptoStream() const = 0;
457
458 // Adds |stream| to the dynamic stream map.
459 virtual void ActivateStream(std::unique_ptr<QuicStream> stream);
460
461 // Returns the stream ID for a new outgoing bidirectional/unidirectional
462 // stream, and increments the underlying counter.
463 QuicStreamId GetNextOutgoingBidirectionalStreamId();
464 QuicStreamId GetNextOutgoingUnidirectionalStreamId();
465
466 // Indicates whether the next outgoing bidirectional/unidirectional stream ID
467 // can be allocated or not. The test for version-99/IETF QUIC is whether it
468 // will exceed the maximum-stream-id or not. For non-version-99 (Google) QUIC
469 // it checks whether the next stream would exceed the limit on the number of
470 // open streams.
471 bool CanOpenNextOutgoingBidirectionalStream();
472 bool CanOpenNextOutgoingUnidirectionalStream();
473
474 // Returns the number of open dynamic streams.
475 uint64_t GetNumOpenDynamicStreams() const;
476
477 // Returns existing stream with id = |stream_id|. If no such stream exists,
478 // and |stream_id| is a peer-created id, then a new stream is created and
479 // returned. However if |stream_id| is a locally-created id and no such stream
480 // exists, the connection is closed.
481 // Caller does not own the returned stream.
482 QuicStream* GetOrCreateDynamicStream(QuicStreamId stream_id);
483
484 // Performs the work required to close |stream_id|. If |locally_reset|
485 // then the stream has been reset by this endpoint, not by the peer.
486 virtual void CloseStreamInner(QuicStreamId stream_id, bool locally_reset);
487
488 // When a stream is closed locally, it may not yet know how many bytes the
489 // peer sent on that stream.
490 // When this data arrives (via stream frame w. FIN, trailing headers, or RST)
491 // this method is called, and correctly updates the connection level flow
492 // controller.
493 virtual void OnFinalByteOffsetReceived(QuicStreamId id,
494 QuicStreamOffset final_byte_offset);
495
renjietange76b2da2019-05-13 14:50:23 -0700496 // Returns true if incoming unidirectional streams should be buffered until
497 // the first byte of the stream arrives.
498 // If a subclass returns true here, it should make sure to implement
499 // ProcessPendingStream().
500 virtual bool UsesPendingStreams() const { return false; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500501
502 // Register (|id|, |stream|) with the static stream map. Override previous
503 // registrations with the same id.
504 void RegisterStaticStream(QuicStreamId id, QuicStream* stream);
renjietangfbeb5bf2019-04-19 15:06:20 -0700505 // TODO(renjietang): Replace the original Register method with the new one
506 // once flag is deprecated.
renjietang3a1bb802019-06-11 10:42:41 -0700507 void RegisterStaticStreamNew(std::unique_ptr<QuicStream> stream,
508 bool stream_already_counted);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500509 const StaticStreamMap& static_streams() const { return static_stream_map_; }
510
511 DynamicStreamMap& dynamic_streams() { return dynamic_stream_map_; }
512 const DynamicStreamMap& dynamic_streams() const {
513 return dynamic_stream_map_;
514 }
515
516 ClosedStreams* closed_streams() { return &closed_streams_; }
517
518 const ZombieStreamMap& zombie_streams() const { return zombie_streams_; }
519
520 void set_largest_peer_created_stream_id(
521 QuicStreamId largest_peer_created_stream_id);
522
523 void set_error(QuicErrorCode error) { error_ = error; }
524 QuicWriteBlockedList* write_blocked_streams() {
525 return &write_blocked_streams_;
526 }
527
528 size_t GetNumDynamicOutgoingStreams() const;
529
530 size_t GetNumDrainingOutgoingStreams() const;
531
532 // Returns true if the stream is still active.
533 bool IsOpenStream(QuicStreamId id);
534
rchda26cdb2019-05-17 11:57:37 -0700535 // Returns true if the stream is a static stream.
536 bool IsStaticStream(QuicStreamId id) const;
537
QUICHE teama6ef0a62019-03-07 20:34:33 -0500538 // Close connection when receive a frame for a locally-created nonexistant
539 // stream.
540 // Prerequisite: IsClosedStream(stream_id) == false
541 // Server session might need to override this method to allow server push
542 // stream to be promised before creating an active stream.
543 virtual void HandleFrameOnNonexistentOutgoingStream(QuicStreamId stream_id);
544
545 virtual bool MaybeIncreaseLargestPeerStreamId(const QuicStreamId stream_id);
546
547 void InsertLocallyClosedStreamsHighestOffset(const QuicStreamId id,
548 QuicStreamOffset offset);
549 // If stream is a locally closed stream, this RST will update FIN offset.
550 // Otherwise stream is a preserved stream and the behavior of it depends on
551 // derived class's own implementation.
552 virtual void HandleRstOnValidNonexistentStream(
553 const QuicRstStreamFrame& frame);
554
555 // Returns a stateless reset token which will be included in the public reset
556 // packet.
557 virtual QuicUint128 GetStatelessResetToken() const;
558
559 QuicControlFrameManager& control_frame_manager() {
560 return control_frame_manager_;
561 }
562
563 const LegacyQuicStreamIdManager& stream_id_manager() const {
564 return stream_id_manager_;
565 }
566
renjietang0c558862019-05-08 13:26:23 -0700567 // Processes the stream type information of |pending| depending on
renjietangbb1c4892019-05-24 15:58:44 -0700568 // different kinds of sessions' own rules. Returns true if the pending stream
569 // is converted into a normal stream.
570 virtual bool ProcessPendingStream(PendingStream* pending) { return false; }
renjietang0c558862019-05-08 13:26:23 -0700571
QUICHE teame3954c22019-05-07 00:30:32 -0700572 bool eliminate_static_stream_map() const {
renjietang615f13b2019-05-06 17:08:02 -0700573 return eliminate_static_stream_map_;
574 }
575
QUICHE teama6ef0a62019-03-07 20:34:33 -0500576 private:
577 friend class test::QuicSessionPeer;
578
579 // Called in OnConfigNegotiated when we receive a new stream level flow
580 // control window in a negotiated config. Closes the connection if invalid.
581 void OnNewStreamFlowControlWindow(QuicStreamOffset new_window);
582
583 // Called in OnConfigNegotiated when we receive a new connection level flow
584 // control window in a negotiated config. Closes the connection if invalid.
585 void OnNewSessionFlowControlWindow(QuicStreamOffset new_window);
586
587 // Debug helper for |OnCanWrite()|, check that OnStreamWrite() makes
588 // forward progress. Returns false if busy loop detected.
589 bool CheckStreamNotBusyLooping(QuicStream* stream,
590 uint64_t previous_bytes_written,
591 bool previous_fin_sent);
592
593 // Debug helper for OnCanWrite. Check that after QuicStream::OnCanWrite(),
594 // if stream has buffered data and is not stream level flow control blocked,
595 // it has to be in the write blocked list.
596 bool CheckStreamWriteBlocked(QuicStream* stream) const;
597
598 // Called in OnConfigNegotiated for Finch trials to measure performance of
599 // starting with larger flow control receive windows.
600 void AdjustInitialFlowControlWindows(size_t stream_window);
601
602 // Find stream with |id|, returns nullptr if the stream does not exist or
603 // closed.
604 QuicStream* GetStream(QuicStreamId id) const;
605
renjietange76b2da2019-05-13 14:50:23 -0700606 PendingStream* GetOrCreatePendingStream(QuicStreamId stream_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500607
608 // Let streams and control frame managers retransmit lost data, returns true
609 // if all lost data is retransmitted. Returns false otherwise.
610 bool RetransmitLostData();
611
612 // Closes the pending stream |stream_id| before it has been created.
613 void ClosePendingStream(QuicStreamId stream_id);
614
renjietange76b2da2019-05-13 14:50:23 -0700615 // Creates or gets pending stream, feeds it with |frame|, and processes the
616 // pending stream.
617 void PendingStreamOnStreamFrame(const QuicStreamFrame& frame);
618
619 // Creates or gets pending strea, feed it with |frame|, and closes the pending
620 // stream.
621 void PendingStreamOnRstStream(const QuicRstStreamFrame& frame);
622
zhongyi1b2f7832019-06-14 13:31:34 -0700623 bool ignore_tlpr_if_no_pending_stream_data() const {
624 return connection_->sent_packet_manager()
625 .ignore_tlpr_if_no_pending_stream_data();
626 }
627
QUICHE teama6ef0a62019-03-07 20:34:33 -0500628 // Keep track of highest received byte offset of locally closed streams, while
629 // waiting for a definitive final highest offset from the peer.
630 std::map<QuicStreamId, QuicStreamOffset>
631 locally_closed_streams_highest_offset_;
632
633 QuicConnection* connection_;
634
635 // May be null.
636 Visitor* visitor_;
637
638 // A list of streams which need to write more data. Stream register
639 // themselves in their constructor, and unregisterm themselves in their
640 // destructors, so the write blocked list must outlive all streams.
641 QuicWriteBlockedList write_blocked_streams_;
642
643 ClosedStreams closed_streams_;
644 // Streams which are closed, but need to be kept alive. Currently, the only
645 // reason is the stream's sent data (including FIN) does not get fully acked.
646 ZombieStreamMap zombie_streams_;
647
648 QuicConfig config_;
649
650 // Static streams, such as crypto and header streams. Owned by child classes
651 // that create these streams.
652 StaticStreamMap static_stream_map_;
653
654 // Map from StreamId to pointers to streams. Owns the streams.
655 DynamicStreamMap dynamic_stream_map_;
656
657 // Map from StreamId to PendingStreams for peer-created unidirectional streams
658 // which are waiting for the first byte of payload to arrive.
659 PendingStreamMap pending_stream_map_;
660
661 // Set of stream ids that are "draining" -- a FIN has been sent and received,
662 // but the stream object still exists because not all the received data has
663 // been consumed.
664 QuicUnorderedSet<QuicStreamId> draining_streams_;
665
zhongyi1b2f7832019-06-14 13:31:34 -0700666 // Set of stream ids that are waiting for acks excluding crypto stream id.
667 QuicUnorderedSet<QuicStreamId> streams_waiting_for_acks_;
668
QUICHE teama6ef0a62019-03-07 20:34:33 -0500669 // TODO(fayang): Consider moving LegacyQuicStreamIdManager into
670 // UberQuicStreamIdManager.
671 // Manages stream IDs for Google QUIC.
672 LegacyQuicStreamIdManager stream_id_manager_;
673
674 // Manages stream IDs for version99/IETF QUIC
675 UberQuicStreamIdManager v99_streamid_manager_;
676
677 // A counter for peer initiated streams which are in the dynamic_stream_map_.
678 size_t num_dynamic_incoming_streams_;
679
680 // A counter for peer initiated streams which are in the draining_streams_.
681 size_t num_draining_incoming_streams_;
682
renjietangfbeb5bf2019-04-19 15:06:20 -0700683 // A counter for self initiated static streams which are in
684 // dynamic_stream_map_.
685 size_t num_outgoing_static_streams_;
686
687 // A counter for peer initiated static streams which are in
688 // dynamic_stream_map_.
689 size_t num_incoming_static_streams_;
690
QUICHE teama6ef0a62019-03-07 20:34:33 -0500691 // A counter for peer initiated streams which are in the
692 // locally_closed_streams_highest_offset_.
693 size_t num_locally_closed_incoming_streams_highest_offset_;
694
695 // The latched error with which the connection was closed.
696 QuicErrorCode error_;
697
698 // Used for connection-level flow control.
699 QuicFlowController flow_controller_;
700
701 // The stream id which was last popped in OnCanWrite, or 0, if not under the
702 // call stack of OnCanWrite.
703 QuicStreamId currently_writing_stream_id_;
704
705 // The largest stream id in |static_stream_map_|.
706 QuicStreamId largest_static_stream_id_;
707
708 // Cached value of whether the crypto handshake has been confirmed.
709 bool is_handshake_confirmed_;
710
711 // Whether a GoAway has been sent.
712 bool goaway_sent_;
713
714 // Whether a GoAway has been received.
715 bool goaway_received_;
716
717 QuicControlFrameManager control_frame_manager_;
718
719 // Id of latest successfully sent message.
720 QuicMessageId last_message_id_;
721
722 // TODO(fayang): switch to linked_hash_set when chromium supports it. The bool
723 // is not used here.
724 // List of streams with pending retransmissions.
725 QuicLinkedHashMap<QuicStreamId, bool> streams_with_pending_retransmission_;
726
727 // Clean up closed_streams_ when this alarm fires.
728 std::unique_ptr<QuicAlarm> closed_streams_clean_up_alarm_;
729
730 // Supported version list used by the crypto handshake only. Please note, this
731 // list may be a superset of the connection framer's supported versions.
732 ParsedQuicVersionVector supported_versions_;
renjietang615f13b2019-05-06 17:08:02 -0700733
zhongyi1b2f7832019-06-14 13:31:34 -0700734 // Latched value of quic_eliminate_static_stream_map.
renjietang615f13b2019-05-06 17:08:02 -0700735 const bool eliminate_static_stream_map_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500736};
737
738} // namespace quic
739
740#endif // QUICHE_QUIC_CORE_QUIC_SESSION_H_