blob: 2d0f252a66d0ca5d64b79f233e12a0ccc686acdd [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
QUICHE teama6ef0a62019-03-07 20:34:33 -050017#include "net/third_party/quiche/src/quic/core/legacy_quic_stream_id_manager.h"
18#include "net/third_party/quiche/src/quic/core/quic_connection.h"
19#include "net/third_party/quiche/src/quic/core/quic_control_frame_manager.h"
20#include "net/third_party/quiche/src/quic/core/quic_crypto_stream.h"
wub2b5942f2019-04-11 13:22:50 -070021#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050022#include "net/third_party/quiche/src/quic/core/quic_packet_creator.h"
23#include "net/third_party/quiche/src/quic/core/quic_packets.h"
24#include "net/third_party/quiche/src/quic/core/quic_stream.h"
25#include "net/third_party/quiche/src/quic/core/quic_stream_frame_data_producer.h"
renjietang686ce582019-10-17 14:28:16 -070026#include "net/third_party/quiche/src/quic/core/quic_types.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050027#include "net/third_party/quiche/src/quic/core/quic_write_blocked_list.h"
28#include "net/third_party/quiche/src/quic/core/session_notifier_interface.h"
29#include "net/third_party/quiche/src/quic/core/uber_quic_stream_id_manager.h"
30#include "net/third_party/quiche/src/quic/platform/api/quic_containers.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_socket_address.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050033
34namespace quic {
35
36class QuicCryptoStream;
37class QuicFlowController;
38class QuicStream;
39class QuicStreamIdManager;
40
41namespace test {
42class QuicSessionPeer;
43} // namespace test
44
rcha8b56e42019-09-20 10:41:48 -070045class QUIC_EXPORT_PRIVATE QuicSession
46 : public QuicConnectionVisitorInterface,
47 public SessionNotifierInterface,
48 public QuicStreamFrameDataProducer,
49 public QuicStreamIdManager::DelegateInterface {
QUICHE teama6ef0a62019-03-07 20:34:33 -050050 public:
51 // An interface from the session to the entity owning the session.
52 // This lets the session notify its owner (the Dispatcher) when the connection
53 // is closed, blocked, or added/removed from the time-wait list.
dschinazif25169a2019-10-23 08:12:18 -070054 class QUIC_EXPORT_PRIVATE Visitor {
QUICHE teama6ef0a62019-03-07 20:34:33 -050055 public:
56 virtual ~Visitor() {}
57
58 // Called when the connection is closed after the streams have been closed.
dschinazi7b9278c2019-05-20 07:36:21 -070059 virtual void OnConnectionClosed(QuicConnectionId server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -050060 QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -070061 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050062 ConnectionCloseSource source) = 0;
63
64 // Called when the session has become write blocked.
65 virtual void OnWriteBlocked(QuicBlockedWriterInterface* blocked_writer) = 0;
66
67 // Called when the session receives reset on a stream from the peer.
68 virtual void OnRstStreamReceived(const QuicRstStreamFrame& frame) = 0;
69
70 // Called when the session receives a STOP_SENDING for a stream from the
71 // peer.
72 virtual void OnStopSendingReceived(const QuicStopSendingFrame& frame) = 0;
73 };
74
75 // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
fayangfa3b1d62019-11-18 08:02:13 -080076 // TODO(fayang): Replace this enum and with HandshakeState.
QUICHE teama6ef0a62019-03-07 20:34:33 -050077 enum CryptoHandshakeEvent {
renjietangea71d6f2019-08-19 12:22:28 -070078 // ENCRYPTION_ESTABLISHED indicates that a client hello has been sent and
79 // subsequent packets will be encrypted. (Client only.)
80 ENCRYPTION_ESTABLISHED,
QUICHE teama6ef0a62019-03-07 20:34:33 -050081 // HANDSHAKE_CONFIRMED, in a client, indicates the server has accepted
82 // our handshake. In a server it indicates that a full, valid client hello
83 // has been received. (Client and server.)
84 HANDSHAKE_CONFIRMED,
85 };
86
87 // Does not take ownership of |connection| or |visitor|.
88 QuicSession(QuicConnection* connection,
89 Visitor* owner,
90 const QuicConfig& config,
renjietang216dc012019-08-27 11:28:27 -070091 const ParsedQuicVersionVector& supported_versions,
92 QuicStreamCount num_expected_unidirectional_static_streams);
QUICHE teama6ef0a62019-03-07 20:34:33 -050093 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;
fkastenholz5d880a92019-06-21 09:01:56 -0700108 void OnConnectionClosed(const QuicConnectionCloseFrame& frame,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500109 ConnectionCloseSource source) override;
110 void OnWriteBlocked() override;
111 void OnSuccessfulVersionNegotiation(
112 const ParsedQuicVersion& version) override;
zhongyi83161e42019-08-19 09:06:25 -0700113 void OnPacketReceived(const QuicSocketAddress& self_address,
114 const QuicSocketAddress& peer_address,
115 bool is_connectivity_probe) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500116 void OnCanWrite() override;
QUICHE teamb8343252019-04-29 13:58:01 -0700117 bool SendProbingData() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500118 void OnCongestionWindowChange(QuicTime /*now*/) override {}
dschinazi17d42422019-06-18 16:35:07 -0700119 void OnConnectionMigration(AddressChangeType /*type*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500120 // Adds a connection level WINDOW_UPDATE frame.
121 void OnAckNeedsRetransmittableFrame() override;
122 void SendPing() override;
123 bool WillingAndAbleToWrite() const override;
124 bool HasPendingHandshake() const override;
125 void OnPathDegrading() override;
126 bool AllowSelfAddressChange() const override;
127 void OnForwardProgressConfirmed() override;
fkastenholz3c4eabf2019-04-22 07:49:59 -0700128 bool OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame) override;
129 bool OnStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame) override;
renjietangeab918f2019-10-28 12:10:32 -0700130 void OnStopSendingFrame(const QuicStopSendingFrame& frame) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500131
132 // QuicStreamFrameDataProducer
133 WriteStreamDataResult WriteStreamData(QuicStreamId id,
134 QuicStreamOffset offset,
135 QuicByteCount data_length,
136 QuicDataWriter* writer) override;
137 bool WriteCryptoData(EncryptionLevel level,
138 QuicStreamOffset offset,
139 QuicByteCount data_length,
140 QuicDataWriter* writer) override;
141
142 // SessionNotifierInterface methods:
143 bool OnFrameAcked(const QuicFrame& frame,
QUICHE team9467db02019-05-30 09:38:45 -0700144 QuicTime::Delta ack_delay_time,
145 QuicTime receive_timestamp) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500146 void OnStreamFrameRetransmitted(const QuicStreamFrame& frame) override;
147 void OnFrameLost(const QuicFrame& frame) override;
148 void RetransmitFrames(const QuicFrames& frames,
149 TransmissionType type) override;
150 bool IsFrameOutstanding(const QuicFrame& frame) const override;
151 bool HasUnackedCryptoData() const override;
zhongyi1b2f7832019-06-14 13:31:34 -0700152 bool HasUnackedStreamData() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500153
rcha8b56e42019-09-20 10:41:48 -0700154 // QuicStreamIdManager::DelegateInterface methods:
155 void OnError(QuicErrorCode error_code, std::string error_details) override;
156 void SendMaxStreams(QuicStreamCount stream_count,
157 bool unidirectional) override;
158 void SendStreamsBlocked(QuicStreamCount stream_count,
159 bool unidirectional) override;
160 // The default implementation does nothing. Subclasses should override if
161 // for example they queue up stream requests.
162 void OnCanCreateNewOutgoingStream(bool unidirectional) override;
163
QUICHE teama6ef0a62019-03-07 20:34:33 -0500164 // Called on every incoming packet. Passes |packet| through to |connection_|.
165 virtual void ProcessUdpPacket(const QuicSocketAddress& self_address,
166 const QuicSocketAddress& peer_address,
167 const QuicReceivedPacket& packet);
168
169 // Called by streams when they want to write data to the peer.
170 // Returns a pair with the number of bytes consumed from data, and a boolean
171 // indicating if the fin bit was consumed. This does not indicate the data
172 // has been sent on the wire: it may have been turned into a packet and queued
173 // if the socket was unexpectedly blocked.
174 virtual QuicConsumedData WritevData(QuicStream* stream,
175 QuicStreamId id,
176 size_t write_length,
177 QuicStreamOffset offset,
178 StreamSendingState state);
179
180 // Called by application to send |message|. Data copy can be avoided if
181 // |message| is provided in reference counted memory.
182 // Please note, |message| provided in reference counted memory would be moved
183 // internally when message is successfully sent. Thereafter, it would be
184 // undefined behavior if callers try to access the slices through their own
185 // copy of the span object.
186 // Returns the message result which includes the message status and message ID
187 // (valid if the write succeeds). SendMessage flushes a message packet even it
188 // is not full. If the application wants to bundle other data in the same
189 // packet, please consider adding a packet flusher around the SendMessage
190 // and/or WritevData calls.
191 //
192 // OnMessageAcked and OnMessageLost are called when a particular message gets
193 // acked or lost.
194 //
195 // Note that SendMessage will fail with status = MESSAGE_STATUS_BLOCKED
196 // if connection is congestion control blocked or underlying socket is write
197 // blocked. In this case the caller can retry sending message again when
198 // connection becomes available, for example after getting OnCanWrite()
199 // callback.
200 MessageResult SendMessage(QuicMemSliceSpan message);
201
QUICHE team350e9e62019-11-19 13:16:24 -0800202 // Same as above SendMessage, except caller can specify if the given |message|
203 // should be flushed even if the underlying connection is deemed unwritable.
204 MessageResult SendMessage(QuicMemSliceSpan message, bool flush);
205
QUICHE teama6ef0a62019-03-07 20:34:33 -0500206 // Called when message with |message_id| gets acked.
QUICHE team9467db02019-05-30 09:38:45 -0700207 virtual void OnMessageAcked(QuicMessageId message_id,
208 QuicTime receive_timestamp);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500209
210 // Called when message with |message_id| is considered as lost.
211 virtual void OnMessageLost(QuicMessageId message_id);
212
213 // Called by control frame manager when it wants to write control frames to
214 // the peer. Returns true if |frame| is consumed, false otherwise.
215 virtual bool WriteControlFrame(const QuicFrame& frame);
216
renjietang64881612019-11-05 17:39:04 -0800217 // Close the stream in both directions.
218 // TODO(renjietang): rename this method as it sends both RST_STREAM and
219 // STOP_SENDING in IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500220 virtual void SendRstStream(QuicStreamId id,
221 QuicRstStreamErrorCode error,
222 QuicStreamOffset bytes_written);
223
224 // Called when the session wants to go away and not accept any new streams.
vasilvvc48c8712019-03-11 13:38:16 -0700225 virtual void SendGoAway(QuicErrorCode error_code, const std::string& reason);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500226
227 // Sends a BLOCKED frame.
228 virtual void SendBlocked(QuicStreamId id);
229
230 // Sends a WINDOW_UPDATE frame.
231 virtual void SendWindowUpdate(QuicStreamId id, QuicStreamOffset byte_offset);
232
QUICHE teama6ef0a62019-03-07 20:34:33 -0500233 // Create and transmit a STOP_SENDING frame
234 virtual void SendStopSending(uint16_t code, QuicStreamId stream_id);
235
236 // Removes the stream associated with 'stream_id' from the active stream map.
237 virtual void CloseStream(QuicStreamId stream_id);
238
239 // Returns true if outgoing packets will be encrypted, even if the server
240 // hasn't confirmed the handshake yet.
241 virtual bool IsEncryptionEstablished() const;
242
243 // For a client, returns true if the server has confirmed our handshake. For
244 // a server, returns true if a full, valid client hello has been received.
renjietang4f8e0bc2019-10-14 17:53:49 -0700245 bool IsCryptoHandshakeConfirmed() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500246
247 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
248 virtual void OnConfigNegotiated();
249
250 // Called by the QuicCryptoStream when the handshake enters a new state.
251 //
252 // Clients will call this function in the order:
renjietangea71d6f2019-08-19 12:22:28 -0700253 // zero or more ENCRYPTION_ESTABLISHED
QUICHE teama6ef0a62019-03-07 20:34:33 -0500254 // HANDSHAKE_CONFIRMED
255 //
256 // Servers will simply call it once with HANDSHAKE_CONFIRMED.
257 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event);
258
259 // Called by the QuicCryptoStream when a handshake message is sent.
260 virtual void OnCryptoHandshakeMessageSent(
261 const CryptoHandshakeMessage& message);
262
263 // Called by the QuicCryptoStream when a handshake message is received.
264 virtual void OnCryptoHandshakeMessageReceived(
265 const CryptoHandshakeMessage& message);
266
267 // Called by the stream on creation to set priority in the write blocked list.
fayang476683a2019-07-25 12:42:16 -0700268 virtual void RegisterStreamPriority(
269 QuicStreamId id,
270 bool is_static,
271 const spdy::SpdyStreamPrecedence& precedence);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500272 // Called by the stream on deletion to clear priority from the write blocked
273 // list.
274 virtual void UnregisterStreamPriority(QuicStreamId id, bool is_static);
275 // Called by the stream on SetPriority to update priority on the write blocked
276 // list.
fayang476683a2019-07-25 12:42:16 -0700277 virtual void UpdateStreamPriority(
278 QuicStreamId id,
279 const spdy::SpdyStreamPrecedence& new_precedence);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500280
281 // Returns mutable config for this session. Returned config is owned
282 // by QuicSession.
283 QuicConfig* config();
284
285 // Returns true if the stream existed previously and has been closed.
286 // Returns false if the stream is still active or if the stream has
287 // not yet been created.
288 bool IsClosedStream(QuicStreamId id);
289
290 QuicConnection* connection() { return connection_; }
291 const QuicConnection* connection() const { return connection_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500292 const QuicSocketAddress& peer_address() const {
293 return connection_->peer_address();
294 }
295 const QuicSocketAddress& self_address() const {
296 return connection_->self_address();
297 }
298 QuicConnectionId connection_id() const {
299 return connection_->connection_id();
300 }
301
renjietang69a8eaf2019-08-06 15:55:58 -0700302 // Returns the number of currently open streams, excluding static streams, and
303 // never counting unfinished streams.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500304 size_t GetNumActiveStreams() const;
305
306 // Returns the number of currently draining streams.
307 size_t GetNumDrainingStreams() const;
308
renjietang69a8eaf2019-08-06 15:55:58 -0700309 // Returns the number of currently open peer initiated streams, excluding
310 // static streams.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500311 size_t GetNumOpenIncomingStreams() const;
312
renjietang69a8eaf2019-08-06 15:55:58 -0700313 // Returns the number of currently open self initiated streams, excluding
314 // static streams.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500315 size_t GetNumOpenOutgoingStreams() const;
316
renjietangfbeb5bf2019-04-19 15:06:20 -0700317 // Returns the number of open peer initiated static streams.
318 size_t num_incoming_static_streams() const {
319 return num_incoming_static_streams_;
320 }
321
322 // Returns the number of open self initiated static streams.
323 size_t num_outgoing_static_streams() const {
324 return num_outgoing_static_streams_;
325 }
326
QUICHE teama6ef0a62019-03-07 20:34:33 -0500327 // Add the stream to the session's write-blocked list because it is blocked by
328 // connection-level flow control but not by its own stream-level flow control.
329 // The stream will be given a chance to write when a connection-level
330 // WINDOW_UPDATE arrives.
QUICHE teamdf0b19f2019-08-13 16:55:42 -0700331 virtual void MarkConnectionLevelWriteBlocked(QuicStreamId id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500332
333 // Called when stream |id| is done waiting for acks either because all data
334 // gets acked or is not interested in data being acked (which happens when
335 // a stream is reset because of an error).
336 void OnStreamDoneWaitingForAcks(QuicStreamId id);
337
zhongyi1b2f7832019-06-14 13:31:34 -0700338 // Called when stream |id| is newly waiting for acks.
339 void OnStreamWaitingForAcks(QuicStreamId id);
340
QUICHE teama6ef0a62019-03-07 20:34:33 -0500341 // Called to cancel retransmission of unencypted crypto stream data.
342 void NeuterUnencryptedData();
343
344 // Returns true if the session has data to be sent, either queued in the
345 // connection, or in a write-blocked stream.
346 bool HasDataToWrite() const;
347
348 // Returns the largest payload that will fit into a single MESSAGE frame.
349 // Because overhead can vary during a connection, this method should be
350 // checked for every message.
ianswettb239f862019-04-05 09:15:06 -0700351 QuicPacketLength GetCurrentLargestMessagePayload() const;
352
353 // Returns the largest payload that will fit into a single MESSAGE frame at
354 // any point during the connection. This assumes the version and
355 // connection ID lengths do not change.
356 QuicPacketLength GetGuaranteedLargestMessagePayload() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500357
358 bool goaway_sent() const { return goaway_sent_; }
359
360 bool goaway_received() const { return goaway_received_; }
361
fkastenholz488a4622019-08-26 06:24:46 -0700362 // Returns the Google QUIC error code
363 QuicErrorCode error() const { return on_closed_frame_.extracted_error_code; }
wub43652ca2019-09-05 11:18:19 -0700364 const std::string& error_details() const {
365 return on_closed_frame_.error_details;
366 }
fkastenholz488a4622019-08-26 06:24:46 -0700367 uint64_t transport_close_frame_type() const {
368 return on_closed_frame_.transport_close_frame_type;
369 }
370 QuicConnectionCloseType close_type() const {
371 return on_closed_frame_.close_type;
372 }
373 QuicIetfTransportErrorCodes transport_error_code() const {
374 return on_closed_frame_.transport_error_code;
375 }
376 uint16_t application_error_code() const {
377 return on_closed_frame_.application_error_code;
378 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500379
380 Perspective perspective() const { return connection_->perspective(); }
381
382 QuicFlowController* flow_controller() { return &flow_controller_; }
383
384 // Returns true if connection is flow controller blocked.
385 bool IsConnectionFlowControlBlocked() const;
386
387 // Returns true if any stream is flow controller blocked.
388 bool IsStreamFlowControlBlocked();
389
390 size_t max_open_incoming_bidirectional_streams() const;
391 size_t max_open_incoming_unidirectional_streams() const;
392
393 size_t MaxAvailableBidirectionalStreams() const;
394 size_t MaxAvailableUnidirectionalStreams() const;
395
renjietang55d182a2019-07-12 10:26:25 -0700396 // Returns existing stream with id = |stream_id|. If no
397 // such stream exists, and |stream_id| is a peer-created stream id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500398 // then a new stream is created and returned. In all other cases, nullptr is
399 // returned.
renjietang880d2432019-07-16 13:14:37 -0700400 // Caller does not own the returned stream.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500401 QuicStream* GetOrCreateStream(const QuicStreamId stream_id);
402
403 // Mark a stream as draining.
404 virtual void StreamDraining(QuicStreamId id);
405
406 // Returns true if this stream should yield writes to another blocked stream.
QUICHE teamdf0b19f2019-08-13 16:55:42 -0700407 virtual bool ShouldYield(QuicStreamId stream_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500408
409 // Set transmission type of next sending packets.
410 void SetTransmissionType(TransmissionType type);
411
412 // Clean up closed_streams_.
413 void CleanUpClosedStreams();
414
QUICHE teama6ef0a62019-03-07 20:34:33 -0500415 const ParsedQuicVersionVector& supported_versions() const {
416 return supported_versions_;
417 }
418
QUICHE teama6ef0a62019-03-07 20:34:33 -0500419 QuicStreamId next_outgoing_bidirectional_stream_id() const;
420 QuicStreamId next_outgoing_unidirectional_stream_id() const;
421
422 // Return true if given stream is peer initiated.
423 bool IsIncomingStream(QuicStreamId id) const;
424
425 size_t GetNumLocallyClosedOutgoingStreamsHighestOffset() const;
426
427 size_t num_locally_closed_incoming_streams_highest_offset() const {
428 return num_locally_closed_incoming_streams_highest_offset_;
429 }
430
431 // Does actual work of sending reset-stream or reset-stream&stop-sending
432 // If the connection is not version 99/IETF QUIC, will always send a
433 // RESET_STREAM and close_write_side_only is ignored. If the connection is
434 // IETF QUIC/Version 99 then will send a RESET_STREAM and STOP_SENDING if
435 // close_write_side_only is false, just a RESET_STREAM if
436 // close_write_side_only is true.
437 virtual void SendRstStreamInner(QuicStreamId id,
438 QuicRstStreamErrorCode error,
439 QuicStreamOffset bytes_written,
440 bool close_write_side_only);
441
wub2b5942f2019-04-11 13:22:50 -0700442 // Record errors when a connection is closed at the server side, should only
443 // be called from server's perspective.
444 // Noop if |error| is QUIC_NO_ERROR.
445 static void RecordConnectionCloseAtServer(QuicErrorCode error,
446 ConnectionCloseSource source);
447
fkastenholzd3a1de92019-05-15 07:00:07 -0700448 inline QuicTransportVersion transport_version() const {
449 return connection_->transport_version();
450 }
451
fayang944cfbc2019-07-31 09:15:00 -0700452 bool use_http2_priority_write_scheduler() const {
453 return use_http2_priority_write_scheduler_;
454 }
455
fkastenholz9b4b0ad2019-08-20 05:10:40 -0700456 bool is_configured() const { return is_configured_; }
457
renjietang216dc012019-08-27 11:28:27 -0700458 QuicStreamCount num_expected_unidirectional_static_streams() const {
459 return num_expected_unidirectional_static_streams_;
460 }
461
462 // Set the number of unidirectional stream that the peer is allowed to open to
463 // be |max_stream| + |num_expected_static_streams_|.
464 void ConfigureMaxIncomingDynamicStreamsToSend(QuicStreamCount max_stream) {
465 config_.SetMaxIncomingUnidirectionalStreamsToSend(
466 max_stream + num_expected_unidirectional_static_streams_);
467 }
468
vasilvv4724c9c2019-08-29 11:52:11 -0700469 // Returns the ALPN values to negotiate on this session.
vasilvvad7424f2019-08-30 00:27:14 -0700470 virtual std::vector<std::string> GetAlpnsToOffer() const {
vasilvv4724c9c2019-08-29 11:52:11 -0700471 // TODO(vasilvv): this currently sets HTTP/3 by default. Switch all
472 // non-HTTP applications to appropriate ALPNs.
473 return std::vector<std::string>({AlpnForVersion(connection()->version())});
474 }
475
vasilvvad7424f2019-08-30 00:27:14 -0700476 // Provided a list of ALPNs offered by the client, selects an ALPN from the
477 // list, or alpns.end() if none of the ALPNs are acceptable.
478 virtual std::vector<QuicStringPiece>::const_iterator SelectAlpn(
479 const std::vector<QuicStringPiece>& alpns) const;
480
481 // Called when the ALPN of the connection is established for a connection that
482 // uses TLS handshake.
483 virtual void OnAlpnSelected(QuicStringPiece alpn);
484
QUICHE teama6ef0a62019-03-07 20:34:33 -0500485 protected:
renjietang55d182a2019-07-12 10:26:25 -0700486 using StreamMap = QuicSmallMap<QuicStreamId, std::unique_ptr<QuicStream>, 10>;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500487
488 using PendingStreamMap =
489 QuicSmallMap<QuicStreamId, std::unique_ptr<PendingStream>, 10>;
490
491 using ClosedStreams = std::vector<std::unique_ptr<QuicStream>>;
492
493 using ZombieStreamMap =
494 QuicSmallMap<QuicStreamId, std::unique_ptr<QuicStream>, 10>;
495
496 // Creates a new stream to handle a peer-initiated stream.
497 // Caller does not own the returned stream.
498 // Returns nullptr and does error handling if the stream can not be created.
499 virtual QuicStream* CreateIncomingStream(QuicStreamId id) = 0;
renjietangbaea59c2019-05-29 15:08:14 -0700500 virtual QuicStream* CreateIncomingStream(PendingStream* pending) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500501
502 // Return the reserved crypto stream.
503 virtual QuicCryptoStream* GetMutableCryptoStream() = 0;
504
505 // Return the reserved crypto stream as a constant pointer.
506 virtual const QuicCryptoStream* GetCryptoStream() const = 0;
507
renjietang55d182a2019-07-12 10:26:25 -0700508 // Adds |stream| to the stream map.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500509 virtual void ActivateStream(std::unique_ptr<QuicStream> stream);
510
511 // Returns the stream ID for a new outgoing bidirectional/unidirectional
512 // stream, and increments the underlying counter.
513 QuicStreamId GetNextOutgoingBidirectionalStreamId();
514 QuicStreamId GetNextOutgoingUnidirectionalStreamId();
515
516 // Indicates whether the next outgoing bidirectional/unidirectional stream ID
517 // can be allocated or not. The test for version-99/IETF QUIC is whether it
518 // will exceed the maximum-stream-id or not. For non-version-99 (Google) QUIC
519 // it checks whether the next stream would exceed the limit on the number of
520 // open streams.
521 bool CanOpenNextOutgoingBidirectionalStream();
522 bool CanOpenNextOutgoingUnidirectionalStream();
523
524 // Returns the number of open dynamic streams.
525 uint64_t GetNumOpenDynamicStreams() const;
526
QUICHE teama6ef0a62019-03-07 20:34:33 -0500527 // Performs the work required to close |stream_id|. If |locally_reset|
528 // then the stream has been reset by this endpoint, not by the peer.
529 virtual void CloseStreamInner(QuicStreamId stream_id, bool locally_reset);
530
531 // When a stream is closed locally, it may not yet know how many bytes the
532 // peer sent on that stream.
533 // When this data arrives (via stream frame w. FIN, trailing headers, or RST)
534 // this method is called, and correctly updates the connection level flow
535 // controller.
536 virtual void OnFinalByteOffsetReceived(QuicStreamId id,
537 QuicStreamOffset final_byte_offset);
538
renjietange76b2da2019-05-13 14:50:23 -0700539 // Returns true if incoming unidirectional streams should be buffered until
540 // the first byte of the stream arrives.
541 // If a subclass returns true here, it should make sure to implement
542 // ProcessPendingStream().
543 virtual bool UsesPendingStreams() const { return false; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500544
renjietang55d182a2019-07-12 10:26:25 -0700545 StreamMap& stream_map() { return stream_map_; }
546 const StreamMap& stream_map() const { return stream_map_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500547
renjietang56d2ed22019-10-22 14:11:55 -0700548 const PendingStreamMap& pending_streams() const {
549 return pending_stream_map_;
550 }
551
QUICHE teama6ef0a62019-03-07 20:34:33 -0500552 ClosedStreams* closed_streams() { return &closed_streams_; }
553
554 const ZombieStreamMap& zombie_streams() const { return zombie_streams_; }
555
556 void set_largest_peer_created_stream_id(
557 QuicStreamId largest_peer_created_stream_id);
558
QUICHE teama6ef0a62019-03-07 20:34:33 -0500559 QuicWriteBlockedList* write_blocked_streams() {
560 return &write_blocked_streams_;
561 }
562
563 size_t GetNumDynamicOutgoingStreams() const;
564
565 size_t GetNumDrainingOutgoingStreams() const;
566
567 // Returns true if the stream is still active.
568 bool IsOpenStream(QuicStreamId id);
569
rchda26cdb2019-05-17 11:57:37 -0700570 // Returns true if the stream is a static stream.
571 bool IsStaticStream(QuicStreamId id) const;
572
renjietang5c729f02019-09-06 12:43:48 -0700573 // Close connection when receive a frame for a locally-created nonexistent
QUICHE teama6ef0a62019-03-07 20:34:33 -0500574 // stream.
575 // Prerequisite: IsClosedStream(stream_id) == false
576 // Server session might need to override this method to allow server push
577 // stream to be promised before creating an active stream.
578 virtual void HandleFrameOnNonexistentOutgoingStream(QuicStreamId stream_id);
579
580 virtual bool MaybeIncreaseLargestPeerStreamId(const QuicStreamId stream_id);
581
582 void InsertLocallyClosedStreamsHighestOffset(const QuicStreamId id,
583 QuicStreamOffset offset);
584 // If stream is a locally closed stream, this RST will update FIN offset.
585 // Otherwise stream is a preserved stream and the behavior of it depends on
586 // derived class's own implementation.
587 virtual void HandleRstOnValidNonexistentStream(
588 const QuicRstStreamFrame& frame);
589
590 // Returns a stateless reset token which will be included in the public reset
591 // packet.
592 virtual QuicUint128 GetStatelessResetToken() const;
593
594 QuicControlFrameManager& control_frame_manager() {
595 return control_frame_manager_;
596 }
597
598 const LegacyQuicStreamIdManager& stream_id_manager() const {
599 return stream_id_manager_;
600 }
601
renjietang0c558862019-05-08 13:26:23 -0700602 // Processes the stream type information of |pending| depending on
renjietangbb1c4892019-05-24 15:58:44 -0700603 // different kinds of sessions' own rules. Returns true if the pending stream
604 // is converted into a normal stream.
dschinazi17d42422019-06-18 16:35:07 -0700605 virtual bool ProcessPendingStream(PendingStream* /*pending*/) {
606 return false;
607 }
renjietang0c558862019-05-08 13:26:23 -0700608
renjietang686ce582019-10-17 14:28:16 -0700609 // Return the largest peer created stream id depending on directionality
610 // indicated by |unidirectional|.
611 QuicStreamId GetLargestPeerCreatedStreamId(bool unidirectional) const;
612
QUICHE teama6ef0a62019-03-07 20:34:33 -0500613 private:
614 friend class test::QuicSessionPeer;
615
616 // Called in OnConfigNegotiated when we receive a new stream level flow
617 // control window in a negotiated config. Closes the connection if invalid.
618 void OnNewStreamFlowControlWindow(QuicStreamOffset new_window);
619
dschinazi18cdf132019-10-09 16:08:18 -0700620 // Called in OnConfigNegotiated when we receive a new unidirectional stream
621 // flow control window in a negotiated config.
622 void OnNewStreamUnidirectionalFlowControlWindow(QuicStreamOffset new_window);
623
624 // Called in OnConfigNegotiated when we receive a new outgoing bidirectional
625 // stream flow control window in a negotiated config.
626 void OnNewStreamOutgoingBidirectionalFlowControlWindow(
627 QuicStreamOffset new_window);
628
629 // Called in OnConfigNegotiated when we receive a new incoming bidirectional
630 // stream flow control window in a negotiated config.
631 void OnNewStreamIncomingBidirectionalFlowControlWindow(
632 QuicStreamOffset new_window);
633
QUICHE teama6ef0a62019-03-07 20:34:33 -0500634 // Called in OnConfigNegotiated when we receive a new connection level flow
635 // control window in a negotiated config. Closes the connection if invalid.
636 void OnNewSessionFlowControlWindow(QuicStreamOffset new_window);
637
638 // Debug helper for |OnCanWrite()|, check that OnStreamWrite() makes
639 // forward progress. Returns false if busy loop detected.
640 bool CheckStreamNotBusyLooping(QuicStream* stream,
641 uint64_t previous_bytes_written,
642 bool previous_fin_sent);
643
644 // Debug helper for OnCanWrite. Check that after QuicStream::OnCanWrite(),
645 // if stream has buffered data and is not stream level flow control blocked,
646 // it has to be in the write blocked list.
647 bool CheckStreamWriteBlocked(QuicStream* stream) const;
648
649 // Called in OnConfigNegotiated for Finch trials to measure performance of
650 // starting with larger flow control receive windows.
651 void AdjustInitialFlowControlWindows(size_t stream_window);
652
653 // Find stream with |id|, returns nullptr if the stream does not exist or
654 // closed.
655 QuicStream* GetStream(QuicStreamId id) const;
656
renjietange76b2da2019-05-13 14:50:23 -0700657 PendingStream* GetOrCreatePendingStream(QuicStreamId stream_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500658
659 // Let streams and control frame managers retransmit lost data, returns true
660 // if all lost data is retransmitted. Returns false otherwise.
661 bool RetransmitLostData();
662
663 // Closes the pending stream |stream_id| before it has been created.
664 void ClosePendingStream(QuicStreamId stream_id);
665
renjietange76b2da2019-05-13 14:50:23 -0700666 // Creates or gets pending stream, feeds it with |frame|, and processes the
667 // pending stream.
668 void PendingStreamOnStreamFrame(const QuicStreamFrame& frame);
669
670 // Creates or gets pending strea, feed it with |frame|, and closes the pending
671 // stream.
672 void PendingStreamOnRstStream(const QuicRstStreamFrame& frame);
673
QUICHE teama6ef0a62019-03-07 20:34:33 -0500674 // Keep track of highest received byte offset of locally closed streams, while
675 // waiting for a definitive final highest offset from the peer.
676 std::map<QuicStreamId, QuicStreamOffset>
677 locally_closed_streams_highest_offset_;
678
679 QuicConnection* connection_;
680
681 // May be null.
682 Visitor* visitor_;
683
684 // A list of streams which need to write more data. Stream register
685 // themselves in their constructor, and unregisterm themselves in their
686 // destructors, so the write blocked list must outlive all streams.
687 QuicWriteBlockedList write_blocked_streams_;
688
689 ClosedStreams closed_streams_;
690 // Streams which are closed, but need to be kept alive. Currently, the only
691 // reason is the stream's sent data (including FIN) does not get fully acked.
692 ZombieStreamMap zombie_streams_;
693
694 QuicConfig config_;
695
QUICHE teama6ef0a62019-03-07 20:34:33 -0500696 // Map from StreamId to pointers to streams. Owns the streams.
renjietang55d182a2019-07-12 10:26:25 -0700697 StreamMap stream_map_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500698
699 // Map from StreamId to PendingStreams for peer-created unidirectional streams
700 // which are waiting for the first byte of payload to arrive.
701 PendingStreamMap pending_stream_map_;
702
703 // Set of stream ids that are "draining" -- a FIN has been sent and received,
704 // but the stream object still exists because not all the received data has
705 // been consumed.
706 QuicUnorderedSet<QuicStreamId> draining_streams_;
707
zhongyi1b2f7832019-06-14 13:31:34 -0700708 // Set of stream ids that are waiting for acks excluding crypto stream id.
709 QuicUnorderedSet<QuicStreamId> streams_waiting_for_acks_;
710
QUICHE teama6ef0a62019-03-07 20:34:33 -0500711 // TODO(fayang): Consider moving LegacyQuicStreamIdManager into
712 // UberQuicStreamIdManager.
713 // Manages stream IDs for Google QUIC.
714 LegacyQuicStreamIdManager stream_id_manager_;
715
716 // Manages stream IDs for version99/IETF QUIC
717 UberQuicStreamIdManager v99_streamid_manager_;
718
renjietang55d182a2019-07-12 10:26:25 -0700719 // A counter for peer initiated dynamic streams which are in the stream_map_.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500720 size_t num_dynamic_incoming_streams_;
721
722 // A counter for peer initiated streams which are in the draining_streams_.
723 size_t num_draining_incoming_streams_;
724
renjietangfbeb5bf2019-04-19 15:06:20 -0700725 // A counter for self initiated static streams which are in
renjietang55d182a2019-07-12 10:26:25 -0700726 // stream_map_.
renjietangfbeb5bf2019-04-19 15:06:20 -0700727 size_t num_outgoing_static_streams_;
728
729 // A counter for peer initiated static streams which are in
renjietang55d182a2019-07-12 10:26:25 -0700730 // stream_map_.
renjietangfbeb5bf2019-04-19 15:06:20 -0700731 size_t num_incoming_static_streams_;
732
QUICHE teama6ef0a62019-03-07 20:34:33 -0500733 // A counter for peer initiated streams which are in the
734 // locally_closed_streams_highest_offset_.
735 size_t num_locally_closed_incoming_streams_highest_offset_;
736
fkastenholz488a4622019-08-26 06:24:46 -0700737 // Received information for a connection close.
738 QuicConnectionCloseFrame on_closed_frame_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500739
740 // Used for connection-level flow control.
741 QuicFlowController flow_controller_;
742
743 // The stream id which was last popped in OnCanWrite, or 0, if not under the
744 // call stack of OnCanWrite.
745 QuicStreamId currently_writing_stream_id_;
746
QUICHE teama6ef0a62019-03-07 20:34:33 -0500747 // Whether a GoAway has been sent.
748 bool goaway_sent_;
749
750 // Whether a GoAway has been received.
751 bool goaway_received_;
752
753 QuicControlFrameManager control_frame_manager_;
754
755 // Id of latest successfully sent message.
756 QuicMessageId last_message_id_;
757
758 // TODO(fayang): switch to linked_hash_set when chromium supports it. The bool
759 // is not used here.
760 // List of streams with pending retransmissions.
761 QuicLinkedHashMap<QuicStreamId, bool> streams_with_pending_retransmission_;
762
763 // Clean up closed_streams_ when this alarm fires.
764 std::unique_ptr<QuicAlarm> closed_streams_clean_up_alarm_;
765
766 // Supported version list used by the crypto handshake only. Please note, this
767 // list may be a superset of the connection framer's supported versions.
768 ParsedQuicVersionVector supported_versions_;
fayang944cfbc2019-07-31 09:15:00 -0700769
770 // If true, write_blocked_streams_ uses HTTP2 (tree-style) priority write
771 // scheduler.
772 bool use_http2_priority_write_scheduler_;
fkastenholz9b4b0ad2019-08-20 05:10:40 -0700773
774 // Initialized to false. Set to true when the session has been properly
775 // configured and is ready for general operation.
776 bool is_configured_;
renjietang216dc012019-08-27 11:28:27 -0700777
778 // The number of expected static streams.
779 QuicStreamCount num_expected_unidirectional_static_streams_;
fayang1b11b962019-09-16 14:01:48 -0700780
781 // If true, enables round robin scheduling.
782 bool enable_round_robin_scheduling_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500783};
784
785} // namespace quic
786
787#endif // QUICHE_QUIC_CORE_QUIC_SESSION_H_