blob: 84b15006fdd44f7dd802846d3b948f0e705a07a6 [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>
renjietang216dc012019-08-27 11:28:27 -070011#include <cstdint>
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include <map>
13#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -070014#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050015#include <vector>
16
fayangd58736d2019-11-27 13:35:31 -080017#include "net/third_party/quiche/src/quic/core/handshaker_delegate_interface.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050018#include "net/third_party/quiche/src/quic/core/legacy_quic_stream_id_manager.h"
19#include "net/third_party/quiche/src/quic/core/quic_connection.h"
20#include "net/third_party/quiche/src/quic/core/quic_control_frame_manager.h"
21#include "net/third_party/quiche/src/quic/core/quic_crypto_stream.h"
vasilvv2b0ab242020-01-07 07:32:09 -080022#include "net/third_party/quiche/src/quic/core/quic_datagram_queue.h"
wub2b5942f2019-04-11 13:22:50 -070023#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050024#include "net/third_party/quiche/src/quic/core/quic_packet_creator.h"
25#include "net/third_party/quiche/src/quic/core/quic_packets.h"
26#include "net/third_party/quiche/src/quic/core/quic_stream.h"
27#include "net/third_party/quiche/src/quic/core/quic_stream_frame_data_producer.h"
renjietang686ce582019-10-17 14:28:16 -070028#include "net/third_party/quiche/src/quic/core/quic_types.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050029#include "net/third_party/quiche/src/quic/core/quic_write_blocked_list.h"
30#include "net/third_party/quiche/src/quic/core/session_notifier_interface.h"
renjietangf196f6a2020-02-12 12:34:23 -080031#include "net/third_party/quiche/src/quic/core/stream_delegate_interface.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050032#include "net/third_party/quiche/src/quic/core/uber_quic_stream_id_manager.h"
33#include "net/third_party/quiche/src/quic/platform/api/quic_containers.h"
34#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
35#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
dmcardlecf0bfcf2019-12-13 08:08:21 -080036#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050037
38namespace quic {
39
40class QuicCryptoStream;
41class QuicFlowController;
42class QuicStream;
43class QuicStreamIdManager;
44
45namespace test {
46class QuicSessionPeer;
47} // namespace test
48
rcha8b56e42019-09-20 10:41:48 -070049class QUIC_EXPORT_PRIVATE QuicSession
50 : public QuicConnectionVisitorInterface,
51 public SessionNotifierInterface,
52 public QuicStreamFrameDataProducer,
fayangd58736d2019-11-27 13:35:31 -080053 public QuicStreamIdManager::DelegateInterface,
renjietangf196f6a2020-02-12 12:34:23 -080054 public HandshakerDelegateInterface,
55 public StreamDelegateInterface {
QUICHE teama6ef0a62019-03-07 20:34:33 -050056 public:
57 // An interface from the session to the entity owning the session.
58 // This lets the session notify its owner (the Dispatcher) when the connection
59 // is closed, blocked, or added/removed from the time-wait list.
dschinazif25169a2019-10-23 08:12:18 -070060 class QUIC_EXPORT_PRIVATE Visitor {
QUICHE teama6ef0a62019-03-07 20:34:33 -050061 public:
62 virtual ~Visitor() {}
63
64 // Called when the connection is closed after the streams have been closed.
dschinazi7b9278c2019-05-20 07:36:21 -070065 virtual void OnConnectionClosed(QuicConnectionId server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -050066 QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -070067 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050068 ConnectionCloseSource source) = 0;
69
70 // Called when the session has become write blocked.
71 virtual void OnWriteBlocked(QuicBlockedWriterInterface* blocked_writer) = 0;
72
73 // Called when the session receives reset on a stream from the peer.
74 virtual void OnRstStreamReceived(const QuicRstStreamFrame& frame) = 0;
75
76 // Called when the session receives a STOP_SENDING for a stream from the
77 // peer.
78 virtual void OnStopSendingReceived(const QuicStopSendingFrame& frame) = 0;
79 };
80
QUICHE teama6ef0a62019-03-07 20:34:33 -050081 // Does not take ownership of |connection| or |visitor|.
82 QuicSession(QuicConnection* connection,
83 Visitor* owner,
84 const QuicConfig& config,
renjietang216dc012019-08-27 11:28:27 -070085 const ParsedQuicVersionVector& supported_versions,
86 QuicStreamCount num_expected_unidirectional_static_streams);
QUICHE teama6ef0a62019-03-07 20:34:33 -050087 QuicSession(const QuicSession&) = delete;
88 QuicSession& operator=(const QuicSession&) = delete;
89
90 ~QuicSession() override;
91
92 virtual void Initialize();
93
94 // QuicConnectionVisitorInterface methods:
95 void OnStreamFrame(const QuicStreamFrame& frame) override;
96 void OnCryptoFrame(const QuicCryptoFrame& frame) override;
97 void OnRstStream(const QuicRstStreamFrame& frame) override;
98 void OnGoAway(const QuicGoAwayFrame& frame) override;
dmcardlecf0bfcf2019-12-13 08:08:21 -080099 void OnMessageReceived(quiche::QuicheStringPiece message) override;
fayang01062942020-01-22 07:23:23 -0800100 void OnHandshakeDoneReceived() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500101 void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override;
102 void OnBlockedFrame(const QuicBlockedFrame& frame) override;
fkastenholz5d880a92019-06-21 09:01:56 -0700103 void OnConnectionClosed(const QuicConnectionCloseFrame& frame,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500104 ConnectionCloseSource source) override;
105 void OnWriteBlocked() override;
106 void OnSuccessfulVersionNegotiation(
107 const ParsedQuicVersion& version) override;
zhongyi83161e42019-08-19 09:06:25 -0700108 void OnPacketReceived(const QuicSocketAddress& self_address,
109 const QuicSocketAddress& peer_address,
110 bool is_connectivity_probe) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500111 void OnCanWrite() override;
QUICHE teamb8343252019-04-29 13:58:01 -0700112 bool SendProbingData() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500113 void OnCongestionWindowChange(QuicTime /*now*/) override {}
dschinazi17d42422019-06-18 16:35:07 -0700114 void OnConnectionMigration(AddressChangeType /*type*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500115 // Adds a connection level WINDOW_UPDATE frame.
116 void OnAckNeedsRetransmittableFrame() override;
117 void SendPing() override;
118 bool WillingAndAbleToWrite() const override;
119 bool HasPendingHandshake() const override;
120 void OnPathDegrading() override;
121 bool AllowSelfAddressChange() const override;
fayangc67c5202020-01-22 07:43:15 -0800122 HandshakeState GetHandshakeState() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500123 void OnForwardProgressConfirmed() override;
fkastenholz3c4eabf2019-04-22 07:49:59 -0700124 bool OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame) override;
125 bool OnStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame) override;
renjietangeab918f2019-10-28 12:10:32 -0700126 void OnStopSendingFrame(const QuicStopSendingFrame& frame) override;
fayangd58736d2019-11-27 13:35:31 -0800127 void OnPacketDecrypted(EncryptionLevel level) override;
fayang2f2915d2020-01-24 06:47:15 -0800128 void OnOneRttPacketAcknowledged() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500129
130 // QuicStreamFrameDataProducer
131 WriteStreamDataResult WriteStreamData(QuicStreamId id,
132 QuicStreamOffset offset,
133 QuicByteCount data_length,
134 QuicDataWriter* writer) override;
135 bool WriteCryptoData(EncryptionLevel level,
136 QuicStreamOffset offset,
137 QuicByteCount data_length,
138 QuicDataWriter* writer) override;
139
140 // SessionNotifierInterface methods:
141 bool OnFrameAcked(const QuicFrame& frame,
QUICHE team9467db02019-05-30 09:38:45 -0700142 QuicTime::Delta ack_delay_time,
143 QuicTime receive_timestamp) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500144 void OnStreamFrameRetransmitted(const QuicStreamFrame& frame) override;
145 void OnFrameLost(const QuicFrame& frame) override;
146 void RetransmitFrames(const QuicFrames& frames,
147 TransmissionType type) override;
148 bool IsFrameOutstanding(const QuicFrame& frame) const override;
149 bool HasUnackedCryptoData() const override;
zhongyi1b2f7832019-06-14 13:31:34 -0700150 bool HasUnackedStreamData() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500151
rcha8b56e42019-09-20 10:41:48 -0700152 // QuicStreamIdManager::DelegateInterface methods:
renjietangff4b2b62020-02-12 16:52:32 -0800153 void OnStreamIdManagerError(QuicErrorCode error_code,
154 std::string error_details) override;
rcha8b56e42019-09-20 10:41:48 -0700155 void SendMaxStreams(QuicStreamCount stream_count,
156 bool unidirectional) override;
157 void SendStreamsBlocked(QuicStreamCount stream_count,
158 bool unidirectional) override;
159 // The default implementation does nothing. Subclasses should override if
160 // for example they queue up stream requests.
161 void OnCanCreateNewOutgoingStream(bool unidirectional) override;
162
QUICHE teama6ef0a62019-03-07 20:34:33 -0500163 // Called on every incoming packet. Passes |packet| through to |connection_|.
164 virtual void ProcessUdpPacket(const QuicSocketAddress& self_address,
165 const QuicSocketAddress& peer_address,
166 const QuicReceivedPacket& packet);
167
QUICHE teama6ef0a62019-03-07 20:34:33 -0500168 // Called by application to send |message|. Data copy can be avoided if
169 // |message| is provided in reference counted memory.
170 // Please note, |message| provided in reference counted memory would be moved
171 // internally when message is successfully sent. Thereafter, it would be
172 // undefined behavior if callers try to access the slices through their own
173 // copy of the span object.
174 // Returns the message result which includes the message status and message ID
175 // (valid if the write succeeds). SendMessage flushes a message packet even it
176 // is not full. If the application wants to bundle other data in the same
177 // packet, please consider adding a packet flusher around the SendMessage
178 // and/or WritevData calls.
179 //
180 // OnMessageAcked and OnMessageLost are called when a particular message gets
181 // acked or lost.
182 //
183 // Note that SendMessage will fail with status = MESSAGE_STATUS_BLOCKED
184 // if connection is congestion control blocked or underlying socket is write
185 // blocked. In this case the caller can retry sending message again when
186 // connection becomes available, for example after getting OnCanWrite()
187 // callback.
188 MessageResult SendMessage(QuicMemSliceSpan message);
189
QUICHE team350e9e62019-11-19 13:16:24 -0800190 // Same as above SendMessage, except caller can specify if the given |message|
191 // should be flushed even if the underlying connection is deemed unwritable.
192 MessageResult SendMessage(QuicMemSliceSpan message, bool flush);
193
QUICHE teama6ef0a62019-03-07 20:34:33 -0500194 // Called when message with |message_id| gets acked.
QUICHE team9467db02019-05-30 09:38:45 -0700195 virtual void OnMessageAcked(QuicMessageId message_id,
196 QuicTime receive_timestamp);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500197
198 // Called when message with |message_id| is considered as lost.
199 virtual void OnMessageLost(QuicMessageId message_id);
200
201 // Called by control frame manager when it wants to write control frames to
202 // the peer. Returns true if |frame| is consumed, false otherwise.
203 virtual bool WriteControlFrame(const QuicFrame& frame);
204
renjietang64881612019-11-05 17:39:04 -0800205 // Close the stream in both directions.
206 // TODO(renjietang): rename this method as it sends both RST_STREAM and
207 // STOP_SENDING in IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500208 virtual void SendRstStream(QuicStreamId id,
209 QuicRstStreamErrorCode error,
210 QuicStreamOffset bytes_written);
211
212 // Called when the session wants to go away and not accept any new streams.
vasilvvc48c8712019-03-11 13:38:16 -0700213 virtual void SendGoAway(QuicErrorCode error_code, const std::string& reason);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500214
215 // Sends a BLOCKED frame.
216 virtual void SendBlocked(QuicStreamId id);
217
218 // Sends a WINDOW_UPDATE frame.
219 virtual void SendWindowUpdate(QuicStreamId id, QuicStreamOffset byte_offset);
220
QUICHE teama6ef0a62019-03-07 20:34:33 -0500221 // Create and transmit a STOP_SENDING frame
222 virtual void SendStopSending(uint16_t code, QuicStreamId stream_id);
223
224 // Removes the stream associated with 'stream_id' from the active stream map.
225 virtual void CloseStream(QuicStreamId stream_id);
226
227 // Returns true if outgoing packets will be encrypted, even if the server
228 // hasn't confirmed the handshake yet.
229 virtual bool IsEncryptionEstablished() const;
230
fayanga3d8df72020-01-14 11:54:39 -0800231 // Returns true if 1RTT keys are available.
232 bool OneRttKeysAvailable() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500233
234 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
235 virtual void OnConfigNegotiated();
236
fayangd58736d2019-11-27 13:35:31 -0800237 // From HandshakerDelegateInterface
fayangd2866522020-02-12 11:15:27 -0800238 bool OnNewDecryptionKeyAvailable(EncryptionLevel level,
fayang3f7bcbe2020-02-10 11:08:47 -0800239 std::unique_ptr<QuicDecrypter> decrypter,
240 bool set_alternative_decrypter,
241 bool latch_once_used) override;
242 void OnNewEncryptionKeyAvailable(
243 EncryptionLevel level,
244 std::unique_ptr<QuicEncrypter> encrypter) override;
fayangd58736d2019-11-27 13:35:31 -0800245 void SetDefaultEncryptionLevel(EncryptionLevel level) override;
246 void DiscardOldDecryptionKey(EncryptionLevel level) override;
247 void DiscardOldEncryptionKey(EncryptionLevel level) override;
248 void NeuterUnencryptedData() override;
249 void NeuterHandshakeData() override;
250
renjietangf196f6a2020-02-12 12:34:23 -0800251 // Implement StreamDelegateInterface.
252 void OnStreamError(QuicErrorCode error_code,
253 std::string error_details) override;
renjietang35e49ed2020-02-19 10:55:01 -0800254 // Sets priority in the write blocked list.
255 void RegisterStreamPriority(
256 QuicStreamId id,
257 bool is_static,
258 const spdy::SpdyStreamPrecedence& precedence) override;
259 // Clears priority from the write blocked list.
260 void UnregisterStreamPriority(QuicStreamId id, bool is_static) override;
261 // Updates priority on the write blocked list.
262 void UpdateStreamPriority(
263 QuicStreamId id,
264 const spdy::SpdyStreamPrecedence& new_precedence) override;
renjietangf196f6a2020-02-12 12:34:23 -0800265
renjietang62539232020-02-18 14:11:53 -0800266 // Called by streams when they want to write data to the peer.
267 // Returns a pair with the number of bytes consumed from data, and a boolean
268 // indicating if the fin bit was consumed. This does not indicate the data
269 // has been sent on the wire: it may have been turned into a packet and queued
270 // if the socket was unexpectedly blocked.
renjietang41a1b412020-02-27 15:05:14 -0800271 QuicConsumedData WritevData(
272 QuicStreamId id,
273 size_t write_length,
274 QuicStreamOffset offset,
275 StreamSendingState state,
276 bool is_retransmission,
277 quiche::QuicheOptional<EncryptionLevel> level) override;
renjietang62539232020-02-18 14:11:53 -0800278
renjietang2abec1d2020-03-02 10:20:11 -0800279 size_t WriteCryptoData(EncryptionLevel level,
280 size_t write_length,
281 QuicStreamOffset offset) override;
282
QUICHE teama6ef0a62019-03-07 20:34:33 -0500283 // Called by the QuicCryptoStream when a handshake message is sent.
284 virtual void OnCryptoHandshakeMessageSent(
285 const CryptoHandshakeMessage& message);
286
287 // Called by the QuicCryptoStream when a handshake message is received.
288 virtual void OnCryptoHandshakeMessageReceived(
289 const CryptoHandshakeMessage& message);
290
QUICHE teama6ef0a62019-03-07 20:34:33 -0500291 // Returns mutable config for this session. Returned config is owned
292 // by QuicSession.
293 QuicConfig* config();
294
295 // Returns true if the stream existed previously and has been closed.
296 // Returns false if the stream is still active or if the stream has
297 // not yet been created.
298 bool IsClosedStream(QuicStreamId id);
299
300 QuicConnection* connection() { return connection_; }
301 const QuicConnection* connection() const { return connection_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500302 const QuicSocketAddress& peer_address() const {
303 return connection_->peer_address();
304 }
305 const QuicSocketAddress& self_address() const {
306 return connection_->self_address();
307 }
308 QuicConnectionId connection_id() const {
309 return connection_->connection_id();
310 }
311
renjietang69a8eaf2019-08-06 15:55:58 -0700312 // Returns the number of currently open streams, excluding static streams, and
313 // never counting unfinished streams.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500314 size_t GetNumActiveStreams() const;
315
316 // Returns the number of currently draining streams.
317 size_t GetNumDrainingStreams() const;
318
renjietang69a8eaf2019-08-06 15:55:58 -0700319 // Returns the number of currently open peer initiated streams, excluding
320 // static streams.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500321 size_t GetNumOpenIncomingStreams() const;
322
renjietang69a8eaf2019-08-06 15:55:58 -0700323 // Returns the number of currently open self initiated streams, excluding
324 // static streams.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500325 size_t GetNumOpenOutgoingStreams() const;
326
renjietangfbeb5bf2019-04-19 15:06:20 -0700327 // Returns the number of open peer initiated static streams.
328 size_t num_incoming_static_streams() const {
329 return num_incoming_static_streams_;
330 }
331
332 // Returns the number of open self initiated static streams.
333 size_t num_outgoing_static_streams() const {
334 return num_outgoing_static_streams_;
335 }
336
QUICHE teama6ef0a62019-03-07 20:34:33 -0500337 // Add the stream to the session's write-blocked list because it is blocked by
338 // connection-level flow control but not by its own stream-level flow control.
339 // The stream will be given a chance to write when a connection-level
340 // WINDOW_UPDATE arrives.
QUICHE teamdf0b19f2019-08-13 16:55:42 -0700341 virtual void MarkConnectionLevelWriteBlocked(QuicStreamId id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500342
343 // Called when stream |id| is done waiting for acks either because all data
344 // gets acked or is not interested in data being acked (which happens when
345 // a stream is reset because of an error).
346 void OnStreamDoneWaitingForAcks(QuicStreamId id);
347
zhongyi1b2f7832019-06-14 13:31:34 -0700348 // Called when stream |id| is newly waiting for acks.
349 void OnStreamWaitingForAcks(QuicStreamId id);
350
QUICHE teama6ef0a62019-03-07 20:34:33 -0500351 // Returns true if the session has data to be sent, either queued in the
352 // connection, or in a write-blocked stream.
353 bool HasDataToWrite() const;
354
355 // Returns the largest payload that will fit into a single MESSAGE frame.
356 // Because overhead can vary during a connection, this method should be
357 // checked for every message.
ianswettb239f862019-04-05 09:15:06 -0700358 QuicPacketLength GetCurrentLargestMessagePayload() const;
359
360 // Returns the largest payload that will fit into a single MESSAGE frame at
361 // any point during the connection. This assumes the version and
362 // connection ID lengths do not change.
363 QuicPacketLength GetGuaranteedLargestMessagePayload() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500364
365 bool goaway_sent() const { return goaway_sent_; }
366
367 bool goaway_received() const { return goaway_received_; }
368
fkastenholz488a4622019-08-26 06:24:46 -0700369 // Returns the Google QUIC error code
370 QuicErrorCode error() const { return on_closed_frame_.extracted_error_code; }
wub43652ca2019-09-05 11:18:19 -0700371 const std::string& error_details() const {
372 return on_closed_frame_.error_details;
373 }
fkastenholz488a4622019-08-26 06:24:46 -0700374 uint64_t transport_close_frame_type() const {
375 return on_closed_frame_.transport_close_frame_type;
376 }
377 QuicConnectionCloseType close_type() const {
378 return on_closed_frame_.close_type;
379 }
380 QuicIetfTransportErrorCodes transport_error_code() const {
381 return on_closed_frame_.transport_error_code;
382 }
383 uint16_t application_error_code() const {
384 return on_closed_frame_.application_error_code;
385 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500386
dschinazi31e94d42019-12-18 11:55:39 -0800387 Perspective perspective() const { return perspective_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500388
389 QuicFlowController* flow_controller() { return &flow_controller_; }
390
391 // Returns true if connection is flow controller blocked.
392 bool IsConnectionFlowControlBlocked() const;
393
394 // Returns true if any stream is flow controller blocked.
395 bool IsStreamFlowControlBlocked();
396
397 size_t max_open_incoming_bidirectional_streams() const;
398 size_t max_open_incoming_unidirectional_streams() const;
399
400 size_t MaxAvailableBidirectionalStreams() const;
401 size_t MaxAvailableUnidirectionalStreams() const;
402
renjietang55d182a2019-07-12 10:26:25 -0700403 // Returns existing stream with id = |stream_id|. If no
404 // such stream exists, and |stream_id| is a peer-created stream id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500405 // then a new stream is created and returned. In all other cases, nullptr is
406 // returned.
renjietang880d2432019-07-16 13:14:37 -0700407 // Caller does not own the returned stream.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500408 QuicStream* GetOrCreateStream(const QuicStreamId stream_id);
409
410 // Mark a stream as draining.
411 virtual void StreamDraining(QuicStreamId id);
412
413 // Returns true if this stream should yield writes to another blocked stream.
QUICHE teamdf0b19f2019-08-13 16:55:42 -0700414 virtual bool ShouldYield(QuicStreamId stream_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500415
416 // Set transmission type of next sending packets.
417 void SetTransmissionType(TransmissionType type);
418
419 // Clean up closed_streams_.
420 void CleanUpClosedStreams();
421
QUICHE teama6ef0a62019-03-07 20:34:33 -0500422 const ParsedQuicVersionVector& supported_versions() const {
423 return supported_versions_;
424 }
425
QUICHE teama6ef0a62019-03-07 20:34:33 -0500426 QuicStreamId next_outgoing_bidirectional_stream_id() const;
427 QuicStreamId next_outgoing_unidirectional_stream_id() const;
428
429 // Return true if given stream is peer initiated.
430 bool IsIncomingStream(QuicStreamId id) const;
431
432 size_t GetNumLocallyClosedOutgoingStreamsHighestOffset() const;
433
434 size_t num_locally_closed_incoming_streams_highest_offset() const {
435 return num_locally_closed_incoming_streams_highest_offset_;
436 }
437
wub2b5942f2019-04-11 13:22:50 -0700438 // Record errors when a connection is closed at the server side, should only
439 // be called from server's perspective.
440 // Noop if |error| is QUIC_NO_ERROR.
441 static void RecordConnectionCloseAtServer(QuicErrorCode error,
442 ConnectionCloseSource source);
443
fkastenholzd3a1de92019-05-15 07:00:07 -0700444 inline QuicTransportVersion transport_version() const {
445 return connection_->transport_version();
446 }
447
nharper46c1e672020-01-16 14:50:31 -0800448 inline ParsedQuicVersion version() const { return connection_->version(); }
449
fayang944cfbc2019-07-31 09:15:00 -0700450 bool use_http2_priority_write_scheduler() const {
451 return use_http2_priority_write_scheduler_;
452 }
453
fkastenholz9b4b0ad2019-08-20 05:10:40 -0700454 bool is_configured() const { return is_configured_; }
455
renjietang216dc012019-08-27 11:28:27 -0700456 QuicStreamCount num_expected_unidirectional_static_streams() const {
457 return num_expected_unidirectional_static_streams_;
458 }
459
460 // Set the number of unidirectional stream that the peer is allowed to open to
461 // be |max_stream| + |num_expected_static_streams_|.
renjietange6d94672020-01-07 10:30:10 -0800462 void ConfigureMaxDynamicStreamsToSend(QuicStreamCount max_stream) {
463 config_.SetMaxUnidirectionalStreamsToSend(
renjietang216dc012019-08-27 11:28:27 -0700464 max_stream + num_expected_unidirectional_static_streams_);
465 }
466
vasilvv4724c9c2019-08-29 11:52:11 -0700467 // Returns the ALPN values to negotiate on this session.
vasilvvad7424f2019-08-30 00:27:14 -0700468 virtual std::vector<std::string> GetAlpnsToOffer() const {
vasilvv4724c9c2019-08-29 11:52:11 -0700469 // TODO(vasilvv): this currently sets HTTP/3 by default. Switch all
470 // non-HTTP applications to appropriate ALPNs.
471 return std::vector<std::string>({AlpnForVersion(connection()->version())});
472 }
473
vasilvvad7424f2019-08-30 00:27:14 -0700474 // Provided a list of ALPNs offered by the client, selects an ALPN from the
475 // list, or alpns.end() if none of the ALPNs are acceptable.
dmcardlecf0bfcf2019-12-13 08:08:21 -0800476 virtual std::vector<quiche::QuicheStringPiece>::const_iterator SelectAlpn(
477 const std::vector<quiche::QuicheStringPiece>& alpns) const;
vasilvvad7424f2019-08-30 00:27:14 -0700478
479 // Called when the ALPN of the connection is established for a connection that
480 // uses TLS handshake.
dmcardlecf0bfcf2019-12-13 08:08:21 -0800481 virtual void OnAlpnSelected(quiche::QuicheStringPiece alpn);
vasilvvad7424f2019-08-30 00:27:14 -0700482
QUICHE teama6ef0a62019-03-07 20:34:33 -0500483 protected:
renjietang55d182a2019-07-12 10:26:25 -0700484 using StreamMap = QuicSmallMap<QuicStreamId, std::unique_ptr<QuicStream>, 10>;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500485
486 using PendingStreamMap =
487 QuicSmallMap<QuicStreamId, std::unique_ptr<PendingStream>, 10>;
488
489 using ClosedStreams = std::vector<std::unique_ptr<QuicStream>>;
490
491 using ZombieStreamMap =
492 QuicSmallMap<QuicStreamId, std::unique_ptr<QuicStream>, 10>;
493
494 // Creates a new stream to handle a peer-initiated stream.
495 // Caller does not own the returned stream.
496 // Returns nullptr and does error handling if the stream can not be created.
497 virtual QuicStream* CreateIncomingStream(QuicStreamId id) = 0;
renjietangbaea59c2019-05-29 15:08:14 -0700498 virtual QuicStream* CreateIncomingStream(PendingStream* pending) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500499
500 // Return the reserved crypto stream.
501 virtual QuicCryptoStream* GetMutableCryptoStream() = 0;
502
503 // Return the reserved crypto stream as a constant pointer.
504 virtual const QuicCryptoStream* GetCryptoStream() const = 0;
505
renjietang55d182a2019-07-12 10:26:25 -0700506 // Adds |stream| to the stream map.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500507 virtual void ActivateStream(std::unique_ptr<QuicStream> stream);
508
509 // Returns the stream ID for a new outgoing bidirectional/unidirectional
510 // stream, and increments the underlying counter.
511 QuicStreamId GetNextOutgoingBidirectionalStreamId();
512 QuicStreamId GetNextOutgoingUnidirectionalStreamId();
513
514 // Indicates whether the next outgoing bidirectional/unidirectional stream ID
515 // can be allocated or not. The test for version-99/IETF QUIC is whether it
516 // will exceed the maximum-stream-id or not. For non-version-99 (Google) QUIC
517 // it checks whether the next stream would exceed the limit on the number of
518 // open streams.
519 bool CanOpenNextOutgoingBidirectionalStream();
520 bool CanOpenNextOutgoingUnidirectionalStream();
521
522 // Returns the number of open dynamic streams.
523 uint64_t GetNumOpenDynamicStreams() const;
524
bnc41c19ca2020-01-21 18:55:26 -0800525 // Returns the maximum bidirectional streams parameter sent with the handshake
526 // as a transport parameter, or in the most recent MAX_STREAMS frame.
527 QuicStreamCount GetAdvertisedMaxIncomingBidirectionalStreams() const;
528
renjietang75bbf982020-02-03 16:40:05 -0800529 // Performs the work required to close |stream_id|. If |rst_sent| then a
530 // Reset Stream frame has already been sent for this stream.
531 virtual void CloseStreamInner(QuicStreamId stream_id, bool rst_sent);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500532
533 // When a stream is closed locally, it may not yet know how many bytes the
534 // peer sent on that stream.
535 // When this data arrives (via stream frame w. FIN, trailing headers, or RST)
536 // this method is called, and correctly updates the connection level flow
537 // controller.
538 virtual void OnFinalByteOffsetReceived(QuicStreamId id,
539 QuicStreamOffset final_byte_offset);
540
renjietange76b2da2019-05-13 14:50:23 -0700541 // Returns true if incoming unidirectional streams should be buffered until
542 // the first byte of the stream arrives.
543 // If a subclass returns true here, it should make sure to implement
544 // ProcessPendingStream().
545 virtual bool UsesPendingStreams() const { return false; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500546
renjietang55d182a2019-07-12 10:26:25 -0700547 StreamMap& stream_map() { return stream_map_; }
548 const StreamMap& stream_map() const { return stream_map_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500549
renjietang56d2ed22019-10-22 14:11:55 -0700550 const PendingStreamMap& pending_streams() const {
551 return pending_stream_map_;
552 }
553
QUICHE teama6ef0a62019-03-07 20:34:33 -0500554 ClosedStreams* closed_streams() { return &closed_streams_; }
555
556 const ZombieStreamMap& zombie_streams() const { return zombie_streams_; }
557
558 void set_largest_peer_created_stream_id(
559 QuicStreamId largest_peer_created_stream_id);
560
QUICHE teama6ef0a62019-03-07 20:34:33 -0500561 QuicWriteBlockedList* write_blocked_streams() {
562 return &write_blocked_streams_;
563 }
564
565 size_t GetNumDynamicOutgoingStreams() const;
566
567 size_t GetNumDrainingOutgoingStreams() const;
568
569 // Returns true if the stream is still active.
570 bool IsOpenStream(QuicStreamId id);
571
rchda26cdb2019-05-17 11:57:37 -0700572 // Returns true if the stream is a static stream.
573 bool IsStaticStream(QuicStreamId id) const;
574
renjietang5c729f02019-09-06 12:43:48 -0700575 // Close connection when receive a frame for a locally-created nonexistent
QUICHE teama6ef0a62019-03-07 20:34:33 -0500576 // stream.
577 // Prerequisite: IsClosedStream(stream_id) == false
578 // Server session might need to override this method to allow server push
579 // stream to be promised before creating an active stream.
580 virtual void HandleFrameOnNonexistentOutgoingStream(QuicStreamId stream_id);
581
582 virtual bool MaybeIncreaseLargestPeerStreamId(const QuicStreamId stream_id);
583
584 void InsertLocallyClosedStreamsHighestOffset(const QuicStreamId id,
585 QuicStreamOffset offset);
586 // If stream is a locally closed stream, this RST will update FIN offset.
587 // Otherwise stream is a preserved stream and the behavior of it depends on
588 // derived class's own implementation.
589 virtual void HandleRstOnValidNonexistentStream(
590 const QuicRstStreamFrame& frame);
591
592 // Returns a stateless reset token which will be included in the public reset
593 // packet.
594 virtual QuicUint128 GetStatelessResetToken() const;
595
596 QuicControlFrameManager& control_frame_manager() {
597 return control_frame_manager_;
598 }
599
600 const LegacyQuicStreamIdManager& stream_id_manager() const {
601 return stream_id_manager_;
602 }
603
vasilvv2b0ab242020-01-07 07:32:09 -0800604 QuicDatagramQueue* datagram_queue() { return &datagram_queue_; }
605
renjietang0c558862019-05-08 13:26:23 -0700606 // Processes the stream type information of |pending| depending on
renjietangbb1c4892019-05-24 15:58:44 -0700607 // different kinds of sessions' own rules. Returns true if the pending stream
608 // is converted into a normal stream.
dschinazi17d42422019-06-18 16:35:07 -0700609 virtual bool ProcessPendingStream(PendingStream* /*pending*/) {
610 return false;
611 }
renjietang0c558862019-05-08 13:26:23 -0700612
renjietang686ce582019-10-17 14:28:16 -0700613 // Return the largest peer created stream id depending on directionality
614 // indicated by |unidirectional|.
615 QuicStreamId GetLargestPeerCreatedStreamId(bool unidirectional) const;
616
ianswett6aefa0b2019-12-10 07:26:15 -0800617 // Deletes the connection and sets it to nullptr, so calling it mulitiple
618 // times is safe.
619 void DeleteConnection();
620
bncb4e7b992020-01-21 18:36:14 -0800621 // Call SetPriority() on stream id |id| and return true if stream is active.
622 bool MaybeSetStreamPriority(QuicStreamId stream_id,
623 const spdy::SpdyStreamPrecedence& precedence);
624
QUICHE teama6ef0a62019-03-07 20:34:33 -0500625 private:
626 friend class test::QuicSessionPeer;
627
628 // Called in OnConfigNegotiated when we receive a new stream level flow
629 // control window in a negotiated config. Closes the connection if invalid.
630 void OnNewStreamFlowControlWindow(QuicStreamOffset new_window);
631
dschinazi18cdf132019-10-09 16:08:18 -0700632 // Called in OnConfigNegotiated when we receive a new unidirectional stream
633 // flow control window in a negotiated config.
634 void OnNewStreamUnidirectionalFlowControlWindow(QuicStreamOffset new_window);
635
636 // Called in OnConfigNegotiated when we receive a new outgoing bidirectional
637 // stream flow control window in a negotiated config.
638 void OnNewStreamOutgoingBidirectionalFlowControlWindow(
639 QuicStreamOffset new_window);
640
641 // Called in OnConfigNegotiated when we receive a new incoming bidirectional
642 // stream flow control window in a negotiated config.
643 void OnNewStreamIncomingBidirectionalFlowControlWindow(
644 QuicStreamOffset new_window);
645
QUICHE teama6ef0a62019-03-07 20:34:33 -0500646 // Called in OnConfigNegotiated when we receive a new connection level flow
647 // control window in a negotiated config. Closes the connection if invalid.
648 void OnNewSessionFlowControlWindow(QuicStreamOffset new_window);
649
650 // Debug helper for |OnCanWrite()|, check that OnStreamWrite() makes
651 // forward progress. Returns false if busy loop detected.
652 bool CheckStreamNotBusyLooping(QuicStream* stream,
653 uint64_t previous_bytes_written,
654 bool previous_fin_sent);
655
656 // Debug helper for OnCanWrite. Check that after QuicStream::OnCanWrite(),
657 // if stream has buffered data and is not stream level flow control blocked,
658 // it has to be in the write blocked list.
659 bool CheckStreamWriteBlocked(QuicStream* stream) const;
660
661 // Called in OnConfigNegotiated for Finch trials to measure performance of
662 // starting with larger flow control receive windows.
663 void AdjustInitialFlowControlWindows(size_t stream_window);
664
665 // Find stream with |id|, returns nullptr if the stream does not exist or
666 // closed.
667 QuicStream* GetStream(QuicStreamId id) const;
668
renjietange76b2da2019-05-13 14:50:23 -0700669 PendingStream* GetOrCreatePendingStream(QuicStreamId stream_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500670
671 // Let streams and control frame managers retransmit lost data, returns true
672 // if all lost data is retransmitted. Returns false otherwise.
673 bool RetransmitLostData();
674
675 // Closes the pending stream |stream_id| before it has been created.
676 void ClosePendingStream(QuicStreamId stream_id);
677
renjietange76b2da2019-05-13 14:50:23 -0700678 // Creates or gets pending stream, feeds it with |frame|, and processes the
679 // pending stream.
680 void PendingStreamOnStreamFrame(const QuicStreamFrame& frame);
681
682 // Creates or gets pending strea, feed it with |frame|, and closes the pending
683 // stream.
684 void PendingStreamOnRstStream(const QuicRstStreamFrame& frame);
685
renjietang61cc2452019-11-26 10:57:10 -0800686 // Does actual work of sending RESET_STREAM, if the stream type allows.
687 void MaybeSendRstStreamFrame(QuicStreamId id,
688 QuicRstStreamErrorCode error,
689 QuicStreamOffset bytes_written);
690
691 // Sends a STOP_SENDING frame if the stream type allows.
692 void MaybeSendStopSendingFrame(QuicStreamId id, QuicRstStreamErrorCode error);
693
QUICHE teama6ef0a62019-03-07 20:34:33 -0500694 // Keep track of highest received byte offset of locally closed streams, while
695 // waiting for a definitive final highest offset from the peer.
696 std::map<QuicStreamId, QuicStreamOffset>
697 locally_closed_streams_highest_offset_;
698
699 QuicConnection* connection_;
700
dschinazi31e94d42019-12-18 11:55:39 -0800701 // Store perspective on QuicSession during the constructor as it may be needed
702 // during our destructor when connection_ may have already been destroyed.
703 Perspective perspective_;
704
QUICHE teama6ef0a62019-03-07 20:34:33 -0500705 // May be null.
706 Visitor* visitor_;
707
708 // A list of streams which need to write more data. Stream register
709 // themselves in their constructor, and unregisterm themselves in their
710 // destructors, so the write blocked list must outlive all streams.
711 QuicWriteBlockedList write_blocked_streams_;
712
713 ClosedStreams closed_streams_;
714 // Streams which are closed, but need to be kept alive. Currently, the only
715 // reason is the stream's sent data (including FIN) does not get fully acked.
716 ZombieStreamMap zombie_streams_;
717
718 QuicConfig config_;
719
QUICHE teama6ef0a62019-03-07 20:34:33 -0500720 // Map from StreamId to pointers to streams. Owns the streams.
renjietang55d182a2019-07-12 10:26:25 -0700721 StreamMap stream_map_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500722
723 // Map from StreamId to PendingStreams for peer-created unidirectional streams
724 // which are waiting for the first byte of payload to arrive.
725 PendingStreamMap pending_stream_map_;
726
727 // Set of stream ids that are "draining" -- a FIN has been sent and received,
728 // but the stream object still exists because not all the received data has
729 // been consumed.
730 QuicUnorderedSet<QuicStreamId> draining_streams_;
731
zhongyi1b2f7832019-06-14 13:31:34 -0700732 // Set of stream ids that are waiting for acks excluding crypto stream id.
733 QuicUnorderedSet<QuicStreamId> streams_waiting_for_acks_;
734
QUICHE teama6ef0a62019-03-07 20:34:33 -0500735 // TODO(fayang): Consider moving LegacyQuicStreamIdManager into
736 // UberQuicStreamIdManager.
737 // Manages stream IDs for Google QUIC.
738 LegacyQuicStreamIdManager stream_id_manager_;
739
740 // Manages stream IDs for version99/IETF QUIC
741 UberQuicStreamIdManager v99_streamid_manager_;
742
renjietang55d182a2019-07-12 10:26:25 -0700743 // A counter for peer initiated dynamic streams which are in the stream_map_.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500744 size_t num_dynamic_incoming_streams_;
745
746 // A counter for peer initiated streams which are in the draining_streams_.
747 size_t num_draining_incoming_streams_;
748
renjietangfbeb5bf2019-04-19 15:06:20 -0700749 // A counter for self initiated static streams which are in
renjietang55d182a2019-07-12 10:26:25 -0700750 // stream_map_.
renjietangfbeb5bf2019-04-19 15:06:20 -0700751 size_t num_outgoing_static_streams_;
752
753 // A counter for peer initiated static streams which are in
renjietang55d182a2019-07-12 10:26:25 -0700754 // stream_map_.
renjietangfbeb5bf2019-04-19 15:06:20 -0700755 size_t num_incoming_static_streams_;
756
QUICHE teama6ef0a62019-03-07 20:34:33 -0500757 // A counter for peer initiated streams which are in the
758 // locally_closed_streams_highest_offset_.
759 size_t num_locally_closed_incoming_streams_highest_offset_;
760
fkastenholz488a4622019-08-26 06:24:46 -0700761 // Received information for a connection close.
762 QuicConnectionCloseFrame on_closed_frame_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500763
764 // Used for connection-level flow control.
765 QuicFlowController flow_controller_;
766
767 // The stream id which was last popped in OnCanWrite, or 0, if not under the
768 // call stack of OnCanWrite.
769 QuicStreamId currently_writing_stream_id_;
770
QUICHE teama6ef0a62019-03-07 20:34:33 -0500771 // Whether a GoAway has been sent.
772 bool goaway_sent_;
773
774 // Whether a GoAway has been received.
775 bool goaway_received_;
776
777 QuicControlFrameManager control_frame_manager_;
778
779 // Id of latest successfully sent message.
780 QuicMessageId last_message_id_;
781
vasilvv2b0ab242020-01-07 07:32:09 -0800782 // The buffer used to queue the DATAGRAM frames.
783 QuicDatagramQueue datagram_queue_;
784
QUICHE teama6ef0a62019-03-07 20:34:33 -0500785 // TODO(fayang): switch to linked_hash_set when chromium supports it. The bool
786 // is not used here.
787 // List of streams with pending retransmissions.
788 QuicLinkedHashMap<QuicStreamId, bool> streams_with_pending_retransmission_;
789
790 // Clean up closed_streams_ when this alarm fires.
791 std::unique_ptr<QuicAlarm> closed_streams_clean_up_alarm_;
792
793 // Supported version list used by the crypto handshake only. Please note, this
794 // list may be a superset of the connection framer's supported versions.
795 ParsedQuicVersionVector supported_versions_;
fayang944cfbc2019-07-31 09:15:00 -0700796
797 // If true, write_blocked_streams_ uses HTTP2 (tree-style) priority write
798 // scheduler.
799 bool use_http2_priority_write_scheduler_;
fkastenholz9b4b0ad2019-08-20 05:10:40 -0700800
801 // Initialized to false. Set to true when the session has been properly
802 // configured and is ready for general operation.
803 bool is_configured_;
renjietang216dc012019-08-27 11:28:27 -0700804
805 // The number of expected static streams.
806 QuicStreamCount num_expected_unidirectional_static_streams_;
fayang1b11b962019-09-16 14:01:48 -0700807
808 // If true, enables round robin scheduling.
809 bool enable_round_robin_scheduling_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500810};
811
812} // namespace quic
813
814#endif // QUICHE_QUIC_CORE_QUIC_SESSION_H_