blob: 758106f9b0bff1091b034e51cf2dabb8c335f7c7 [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"
26#include "net/third_party/quiche/src/quic/core/quic_write_blocked_list.h"
27#include "net/third_party/quiche/src/quic/core/session_notifier_interface.h"
28#include "net/third_party/quiche/src/quic/core/uber_quic_stream_id_manager.h"
29#include "net/third_party/quiche/src/quic/platform/api/quic_containers.h"
30#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
31#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050032
33namespace quic {
34
35class QuicCryptoStream;
36class QuicFlowController;
37class QuicStream;
38class QuicStreamIdManager;
39
40namespace test {
41class QuicSessionPeer;
42} // namespace test
43
rcha8b56e42019-09-20 10:41:48 -070044class QUIC_EXPORT_PRIVATE QuicSession
45 : public QuicConnectionVisitorInterface,
46 public SessionNotifierInterface,
47 public QuicStreamFrameDataProducer,
48 public QuicStreamIdManager::DelegateInterface {
QUICHE teama6ef0a62019-03-07 20:34:33 -050049 public:
50 // An interface from the session to the entity owning the session.
51 // This lets the session notify its owner (the Dispatcher) when the connection
52 // is closed, blocked, or added/removed from the time-wait list.
53 class Visitor {
54 public:
55 virtual ~Visitor() {}
56
57 // Called when the connection is closed after the streams have been closed.
dschinazi7b9278c2019-05-20 07:36:21 -070058 virtual void OnConnectionClosed(QuicConnectionId server_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -050059 QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -070060 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050061 ConnectionCloseSource source) = 0;
62
63 // Called when the session has become write blocked.
64 virtual void OnWriteBlocked(QuicBlockedWriterInterface* blocked_writer) = 0;
65
66 // Called when the session receives reset on a stream from the peer.
67 virtual void OnRstStreamReceived(const QuicRstStreamFrame& frame) = 0;
68
69 // Called when the session receives a STOP_SENDING for a stream from the
70 // peer.
71 virtual void OnStopSendingReceived(const QuicStopSendingFrame& frame) = 0;
72 };
73
74 // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
75 enum CryptoHandshakeEvent {
renjietangea71d6f2019-08-19 12:22:28 -070076 // ENCRYPTION_ESTABLISHED indicates that a client hello has been sent and
77 // subsequent packets will be encrypted. (Client only.)
78 ENCRYPTION_ESTABLISHED,
QUICHE teama6ef0a62019-03-07 20:34:33 -050079 // HANDSHAKE_CONFIRMED, in a client, indicates the server has accepted
80 // our handshake. In a server it indicates that a full, valid client hello
81 // has been received. (Client and server.)
82 HANDSHAKE_CONFIRMED,
83 };
84
85 // Does not take ownership of |connection| or |visitor|.
86 QuicSession(QuicConnection* connection,
87 Visitor* owner,
88 const QuicConfig& config,
renjietang216dc012019-08-27 11:28:27 -070089 const ParsedQuicVersionVector& supported_versions,
90 QuicStreamCount num_expected_unidirectional_static_streams);
QUICHE teama6ef0a62019-03-07 20:34:33 -050091 QuicSession(const QuicSession&) = delete;
92 QuicSession& operator=(const QuicSession&) = delete;
93
94 ~QuicSession() override;
95
96 virtual void Initialize();
97
98 // QuicConnectionVisitorInterface methods:
99 void OnStreamFrame(const QuicStreamFrame& frame) override;
100 void OnCryptoFrame(const QuicCryptoFrame& frame) override;
101 void OnRstStream(const QuicRstStreamFrame& frame) override;
102 void OnGoAway(const QuicGoAwayFrame& frame) override;
103 void OnMessageReceived(QuicStringPiece message) override;
104 void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override;
105 void OnBlockedFrame(const QuicBlockedFrame& frame) override;
fkastenholz5d880a92019-06-21 09:01:56 -0700106 void OnConnectionClosed(const QuicConnectionCloseFrame& frame,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500107 ConnectionCloseSource source) override;
108 void OnWriteBlocked() override;
109 void OnSuccessfulVersionNegotiation(
110 const ParsedQuicVersion& version) override;
zhongyi83161e42019-08-19 09:06:25 -0700111 void OnPacketReceived(const QuicSocketAddress& self_address,
112 const QuicSocketAddress& peer_address,
113 bool is_connectivity_probe) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500114 void OnCanWrite() override;
QUICHE teamb8343252019-04-29 13:58:01 -0700115 bool SendProbingData() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500116 void OnCongestionWindowChange(QuicTime /*now*/) override {}
dschinazi17d42422019-06-18 16:35:07 -0700117 void OnConnectionMigration(AddressChangeType /*type*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500118 // Adds a connection level WINDOW_UPDATE frame.
119 void OnAckNeedsRetransmittableFrame() override;
120 void SendPing() override;
121 bool WillingAndAbleToWrite() const override;
122 bool HasPendingHandshake() const override;
123 void OnPathDegrading() override;
124 bool AllowSelfAddressChange() const override;
125 void OnForwardProgressConfirmed() override;
fkastenholz3c4eabf2019-04-22 07:49:59 -0700126 bool OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame) override;
127 bool OnStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500128 bool OnStopSendingFrame(const QuicStopSendingFrame& frame) override;
129
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:
153 void OnError(QuicErrorCode error_code, std::string error_details) override;
154 void SendMaxStreams(QuicStreamCount stream_count,
155 bool unidirectional) override;
156 void SendStreamsBlocked(QuicStreamCount stream_count,
157 bool unidirectional) override;
158 // The default implementation does nothing. Subclasses should override if
159 // for example they queue up stream requests.
160 void OnCanCreateNewOutgoingStream(bool unidirectional) override;
161
QUICHE teama6ef0a62019-03-07 20:34:33 -0500162 // Called on every incoming packet. Passes |packet| through to |connection_|.
163 virtual void ProcessUdpPacket(const QuicSocketAddress& self_address,
164 const QuicSocketAddress& peer_address,
165 const QuicReceivedPacket& packet);
166
167 // Called by streams when they want to write data to the peer.
168 // Returns a pair with the number of bytes consumed from data, and a boolean
169 // indicating if the fin bit was consumed. This does not indicate the data
170 // has been sent on the wire: it may have been turned into a packet and queued
171 // if the socket was unexpectedly blocked.
172 virtual QuicConsumedData WritevData(QuicStream* stream,
173 QuicStreamId id,
174 size_t write_length,
175 QuicStreamOffset offset,
176 StreamSendingState state);
177
178 // Called by application to send |message|. Data copy can be avoided if
179 // |message| is provided in reference counted memory.
180 // Please note, |message| provided in reference counted memory would be moved
181 // internally when message is successfully sent. Thereafter, it would be
182 // undefined behavior if callers try to access the slices through their own
183 // copy of the span object.
184 // Returns the message result which includes the message status and message ID
185 // (valid if the write succeeds). SendMessage flushes a message packet even it
186 // is not full. If the application wants to bundle other data in the same
187 // packet, please consider adding a packet flusher around the SendMessage
188 // and/or WritevData calls.
189 //
190 // OnMessageAcked and OnMessageLost are called when a particular message gets
191 // acked or lost.
192 //
193 // Note that SendMessage will fail with status = MESSAGE_STATUS_BLOCKED
194 // if connection is congestion control blocked or underlying socket is write
195 // blocked. In this case the caller can retry sending message again when
196 // connection becomes available, for example after getting OnCanWrite()
197 // callback.
198 MessageResult SendMessage(QuicMemSliceSpan message);
199
200 // Called when message with |message_id| gets acked.
QUICHE team9467db02019-05-30 09:38:45 -0700201 virtual void OnMessageAcked(QuicMessageId message_id,
202 QuicTime receive_timestamp);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500203
204 // Called when message with |message_id| is considered as lost.
205 virtual void OnMessageLost(QuicMessageId message_id);
206
207 // Called by control frame manager when it wants to write control frames to
208 // the peer. Returns true if |frame| is consumed, false otherwise.
209 virtual bool WriteControlFrame(const QuicFrame& frame);
210
211 // Called by streams when they want to close the stream in both directions.
212 virtual void SendRstStream(QuicStreamId id,
213 QuicRstStreamErrorCode error,
214 QuicStreamOffset bytes_written);
215
216 // Called when the session wants to go away and not accept any new streams.
vasilvvc48c8712019-03-11 13:38:16 -0700217 virtual void SendGoAway(QuicErrorCode error_code, const std::string& reason);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500218
219 // Sends a BLOCKED frame.
220 virtual void SendBlocked(QuicStreamId id);
221
222 // Sends a WINDOW_UPDATE frame.
223 virtual void SendWindowUpdate(QuicStreamId id, QuicStreamOffset byte_offset);
224
QUICHE teama6ef0a62019-03-07 20:34:33 -0500225 // Create and transmit a STOP_SENDING frame
226 virtual void SendStopSending(uint16_t code, QuicStreamId stream_id);
227
228 // Removes the stream associated with 'stream_id' from the active stream map.
229 virtual void CloseStream(QuicStreamId stream_id);
230
231 // Returns true if outgoing packets will be encrypted, even if the server
232 // hasn't confirmed the handshake yet.
233 virtual bool IsEncryptionEstablished() const;
234
235 // For a client, returns true if the server has confirmed our handshake. For
236 // a server, returns true if a full, valid client hello has been received.
237 virtual bool IsCryptoHandshakeConfirmed() const;
238
239 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
240 virtual void OnConfigNegotiated();
241
242 // Called by the QuicCryptoStream when the handshake enters a new state.
243 //
244 // Clients will call this function in the order:
renjietangea71d6f2019-08-19 12:22:28 -0700245 // zero or more ENCRYPTION_ESTABLISHED
QUICHE teama6ef0a62019-03-07 20:34:33 -0500246 // 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.
fayang476683a2019-07-25 12:42:16 -0700260 virtual void RegisterStreamPriority(
261 QuicStreamId id,
262 bool is_static,
263 const spdy::SpdyStreamPrecedence& precedence);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500264 // Called by the stream on deletion to clear priority from the write blocked
265 // list.
266 virtual void UnregisterStreamPriority(QuicStreamId id, bool is_static);
267 // Called by the stream on SetPriority to update priority on the write blocked
268 // list.
fayang476683a2019-07-25 12:42:16 -0700269 virtual void UpdateStreamPriority(
270 QuicStreamId id,
271 const spdy::SpdyStreamPrecedence& new_precedence);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500272
273 // Returns mutable config for this session. Returned config is owned
274 // by QuicSession.
275 QuicConfig* config();
276
277 // Returns true if the stream existed previously and has been closed.
278 // Returns false if the stream is still active or if the stream has
279 // not yet been created.
280 bool IsClosedStream(QuicStreamId id);
281
282 QuicConnection* connection() { return connection_; }
283 const QuicConnection* connection() const { return connection_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500284 const QuicSocketAddress& peer_address() const {
285 return connection_->peer_address();
286 }
287 const QuicSocketAddress& self_address() const {
288 return connection_->self_address();
289 }
290 QuicConnectionId connection_id() const {
291 return connection_->connection_id();
292 }
293
renjietang69a8eaf2019-08-06 15:55:58 -0700294 // Returns the number of currently open streams, excluding static streams, and
295 // never counting unfinished streams.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500296 size_t GetNumActiveStreams() const;
297
298 // Returns the number of currently draining streams.
299 size_t GetNumDrainingStreams() const;
300
renjietang69a8eaf2019-08-06 15:55:58 -0700301 // Returns the number of currently open peer initiated streams, excluding
302 // static streams.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500303 size_t GetNumOpenIncomingStreams() const;
304
renjietang69a8eaf2019-08-06 15:55:58 -0700305 // Returns the number of currently open self initiated streams, excluding
306 // static streams.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500307 size_t GetNumOpenOutgoingStreams() const;
308
renjietangfbeb5bf2019-04-19 15:06:20 -0700309 // Returns the number of open peer initiated static streams.
310 size_t num_incoming_static_streams() const {
311 return num_incoming_static_streams_;
312 }
313
314 // Returns the number of open self initiated static streams.
315 size_t num_outgoing_static_streams() const {
316 return num_outgoing_static_streams_;
317 }
318
QUICHE teama6ef0a62019-03-07 20:34:33 -0500319 // Add the stream to the session's write-blocked list because it is blocked by
320 // connection-level flow control but not by its own stream-level flow control.
321 // The stream will be given a chance to write when a connection-level
322 // WINDOW_UPDATE arrives.
QUICHE teamdf0b19f2019-08-13 16:55:42 -0700323 virtual void MarkConnectionLevelWriteBlocked(QuicStreamId id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500324
325 // Called when stream |id| is done waiting for acks either because all data
326 // gets acked or is not interested in data being acked (which happens when
327 // a stream is reset because of an error).
328 void OnStreamDoneWaitingForAcks(QuicStreamId id);
329
zhongyi1b2f7832019-06-14 13:31:34 -0700330 // Called when stream |id| is newly waiting for acks.
331 void OnStreamWaitingForAcks(QuicStreamId id);
332
QUICHE teama6ef0a62019-03-07 20:34:33 -0500333 // Called to cancel retransmission of unencypted crypto stream data.
334 void NeuterUnencryptedData();
335
336 // Returns true if the session has data to be sent, either queued in the
337 // connection, or in a write-blocked stream.
338 bool HasDataToWrite() const;
339
340 // Returns the largest payload that will fit into a single MESSAGE frame.
341 // Because overhead can vary during a connection, this method should be
342 // checked for every message.
ianswettb239f862019-04-05 09:15:06 -0700343 QuicPacketLength GetCurrentLargestMessagePayload() const;
344
345 // Returns the largest payload that will fit into a single MESSAGE frame at
346 // any point during the connection. This assumes the version and
347 // connection ID lengths do not change.
348 QuicPacketLength GetGuaranteedLargestMessagePayload() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500349
350 bool goaway_sent() const { return goaway_sent_; }
351
352 bool goaway_received() const { return goaway_received_; }
353
fkastenholz488a4622019-08-26 06:24:46 -0700354 // Returns the Google QUIC error code
355 QuicErrorCode error() const { return on_closed_frame_.extracted_error_code; }
wub43652ca2019-09-05 11:18:19 -0700356 const std::string& error_details() const {
357 return on_closed_frame_.error_details;
358 }
fkastenholz488a4622019-08-26 06:24:46 -0700359 uint64_t transport_close_frame_type() const {
360 return on_closed_frame_.transport_close_frame_type;
361 }
362 QuicConnectionCloseType close_type() const {
363 return on_closed_frame_.close_type;
364 }
365 QuicIetfTransportErrorCodes transport_error_code() const {
366 return on_closed_frame_.transport_error_code;
367 }
368 uint16_t application_error_code() const {
369 return on_closed_frame_.application_error_code;
370 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500371
372 Perspective perspective() const { return connection_->perspective(); }
373
374 QuicFlowController* flow_controller() { return &flow_controller_; }
375
376 // Returns true if connection is flow controller blocked.
377 bool IsConnectionFlowControlBlocked() const;
378
379 // Returns true if any stream is flow controller blocked.
380 bool IsStreamFlowControlBlocked();
381
382 size_t max_open_incoming_bidirectional_streams() const;
383 size_t max_open_incoming_unidirectional_streams() const;
384
385 size_t MaxAvailableBidirectionalStreams() const;
386 size_t MaxAvailableUnidirectionalStreams() const;
387
renjietang55d182a2019-07-12 10:26:25 -0700388 // Returns existing stream with id = |stream_id|. If no
389 // such stream exists, and |stream_id| is a peer-created stream id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500390 // then a new stream is created and returned. In all other cases, nullptr is
391 // returned.
renjietang880d2432019-07-16 13:14:37 -0700392 // Caller does not own the returned stream.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500393 QuicStream* GetOrCreateStream(const QuicStreamId stream_id);
394
395 // Mark a stream as draining.
396 virtual void StreamDraining(QuicStreamId id);
397
398 // Returns true if this stream should yield writes to another blocked stream.
QUICHE teamdf0b19f2019-08-13 16:55:42 -0700399 virtual bool ShouldYield(QuicStreamId stream_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500400
401 // Set transmission type of next sending packets.
402 void SetTransmissionType(TransmissionType type);
403
404 // Clean up closed_streams_.
405 void CleanUpClosedStreams();
406
407 bool session_decides_what_to_write() const;
408
409 const ParsedQuicVersionVector& supported_versions() const {
410 return supported_versions_;
411 }
412
QUICHE teama6ef0a62019-03-07 20:34:33 -0500413 QuicStreamId next_outgoing_bidirectional_stream_id() const;
414 QuicStreamId next_outgoing_unidirectional_stream_id() const;
415
416 // Return true if given stream is peer initiated.
417 bool IsIncomingStream(QuicStreamId id) const;
418
419 size_t GetNumLocallyClosedOutgoingStreamsHighestOffset() const;
420
421 size_t num_locally_closed_incoming_streams_highest_offset() const {
422 return num_locally_closed_incoming_streams_highest_offset_;
423 }
424
425 // Does actual work of sending reset-stream or reset-stream&stop-sending
426 // If the connection is not version 99/IETF QUIC, will always send a
427 // RESET_STREAM and close_write_side_only is ignored. If the connection is
428 // IETF QUIC/Version 99 then will send a RESET_STREAM and STOP_SENDING if
429 // close_write_side_only is false, just a RESET_STREAM if
430 // close_write_side_only is true.
431 virtual void SendRstStreamInner(QuicStreamId id,
432 QuicRstStreamErrorCode error,
433 QuicStreamOffset bytes_written,
434 bool close_write_side_only);
435
wub2b5942f2019-04-11 13:22:50 -0700436 // Record errors when a connection is closed at the server side, should only
437 // be called from server's perspective.
438 // Noop if |error| is QUIC_NO_ERROR.
439 static void RecordConnectionCloseAtServer(QuicErrorCode error,
440 ConnectionCloseSource source);
441
fkastenholzd3a1de92019-05-15 07:00:07 -0700442 inline QuicTransportVersion transport_version() const {
443 return connection_->transport_version();
444 }
445
fayang944cfbc2019-07-31 09:15:00 -0700446 bool use_http2_priority_write_scheduler() const {
447 return use_http2_priority_write_scheduler_;
448 }
449
fkastenholz9b4b0ad2019-08-20 05:10:40 -0700450 bool is_configured() const { return is_configured_; }
451
renjietang216dc012019-08-27 11:28:27 -0700452 QuicStreamCount num_expected_unidirectional_static_streams() const {
453 return num_expected_unidirectional_static_streams_;
454 }
455
456 // Set the number of unidirectional stream that the peer is allowed to open to
457 // be |max_stream| + |num_expected_static_streams_|.
458 void ConfigureMaxIncomingDynamicStreamsToSend(QuicStreamCount max_stream) {
459 config_.SetMaxIncomingUnidirectionalStreamsToSend(
460 max_stream + num_expected_unidirectional_static_streams_);
461 }
462
vasilvv4724c9c2019-08-29 11:52:11 -0700463 // Returns the ALPN values to negotiate on this session.
vasilvvad7424f2019-08-30 00:27:14 -0700464 virtual std::vector<std::string> GetAlpnsToOffer() const {
vasilvv4724c9c2019-08-29 11:52:11 -0700465 // TODO(vasilvv): this currently sets HTTP/3 by default. Switch all
466 // non-HTTP applications to appropriate ALPNs.
467 return std::vector<std::string>({AlpnForVersion(connection()->version())});
468 }
469
vasilvvad7424f2019-08-30 00:27:14 -0700470 // Provided a list of ALPNs offered by the client, selects an ALPN from the
471 // list, or alpns.end() if none of the ALPNs are acceptable.
472 virtual std::vector<QuicStringPiece>::const_iterator SelectAlpn(
473 const std::vector<QuicStringPiece>& alpns) const;
474
475 // Called when the ALPN of the connection is established for a connection that
476 // uses TLS handshake.
477 virtual void OnAlpnSelected(QuicStringPiece alpn);
478
QUICHE teama6ef0a62019-03-07 20:34:33 -0500479 protected:
renjietang55d182a2019-07-12 10:26:25 -0700480 using StreamMap = QuicSmallMap<QuicStreamId, std::unique_ptr<QuicStream>, 10>;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500481
482 using PendingStreamMap =
483 QuicSmallMap<QuicStreamId, std::unique_ptr<PendingStream>, 10>;
484
485 using ClosedStreams = std::vector<std::unique_ptr<QuicStream>>;
486
487 using ZombieStreamMap =
488 QuicSmallMap<QuicStreamId, std::unique_ptr<QuicStream>, 10>;
489
490 // Creates a new stream to handle a peer-initiated stream.
491 // Caller does not own the returned stream.
492 // Returns nullptr and does error handling if the stream can not be created.
493 virtual QuicStream* CreateIncomingStream(QuicStreamId id) = 0;
renjietangbaea59c2019-05-29 15:08:14 -0700494 virtual QuicStream* CreateIncomingStream(PendingStream* pending) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500495
496 // Return the reserved crypto stream.
497 virtual QuicCryptoStream* GetMutableCryptoStream() = 0;
498
499 // Return the reserved crypto stream as a constant pointer.
500 virtual const QuicCryptoStream* GetCryptoStream() const = 0;
501
renjietang55d182a2019-07-12 10:26:25 -0700502 // Adds |stream| to the stream map.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500503 virtual void ActivateStream(std::unique_ptr<QuicStream> stream);
504
505 // Returns the stream ID for a new outgoing bidirectional/unidirectional
506 // stream, and increments the underlying counter.
507 QuicStreamId GetNextOutgoingBidirectionalStreamId();
508 QuicStreamId GetNextOutgoingUnidirectionalStreamId();
509
510 // Indicates whether the next outgoing bidirectional/unidirectional stream ID
511 // can be allocated or not. The test for version-99/IETF QUIC is whether it
512 // will exceed the maximum-stream-id or not. For non-version-99 (Google) QUIC
513 // it checks whether the next stream would exceed the limit on the number of
514 // open streams.
515 bool CanOpenNextOutgoingBidirectionalStream();
516 bool CanOpenNextOutgoingUnidirectionalStream();
517
518 // Returns the number of open dynamic streams.
519 uint64_t GetNumOpenDynamicStreams() const;
520
QUICHE teama6ef0a62019-03-07 20:34:33 -0500521 // Performs the work required to close |stream_id|. If |locally_reset|
522 // then the stream has been reset by this endpoint, not by the peer.
523 virtual void CloseStreamInner(QuicStreamId stream_id, bool locally_reset);
524
525 // When a stream is closed locally, it may not yet know how many bytes the
526 // peer sent on that stream.
527 // When this data arrives (via stream frame w. FIN, trailing headers, or RST)
528 // this method is called, and correctly updates the connection level flow
529 // controller.
530 virtual void OnFinalByteOffsetReceived(QuicStreamId id,
531 QuicStreamOffset final_byte_offset);
532
renjietange76b2da2019-05-13 14:50:23 -0700533 // Returns true if incoming unidirectional streams should be buffered until
534 // the first byte of the stream arrives.
535 // If a subclass returns true here, it should make sure to implement
536 // ProcessPendingStream().
537 virtual bool UsesPendingStreams() const { return false; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500538
renjietang55d182a2019-07-12 10:26:25 -0700539 StreamMap& stream_map() { return stream_map_; }
540 const StreamMap& stream_map() const { return stream_map_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500541
542 ClosedStreams* closed_streams() { return &closed_streams_; }
543
544 const ZombieStreamMap& zombie_streams() const { return zombie_streams_; }
545
546 void set_largest_peer_created_stream_id(
547 QuicStreamId largest_peer_created_stream_id);
548
QUICHE teama6ef0a62019-03-07 20:34:33 -0500549 QuicWriteBlockedList* write_blocked_streams() {
550 return &write_blocked_streams_;
551 }
552
553 size_t GetNumDynamicOutgoingStreams() const;
554
555 size_t GetNumDrainingOutgoingStreams() const;
556
557 // Returns true if the stream is still active.
558 bool IsOpenStream(QuicStreamId id);
559
rchda26cdb2019-05-17 11:57:37 -0700560 // Returns true if the stream is a static stream.
561 bool IsStaticStream(QuicStreamId id) const;
562
renjietang5c729f02019-09-06 12:43:48 -0700563 // Close connection when receive a frame for a locally-created nonexistent
QUICHE teama6ef0a62019-03-07 20:34:33 -0500564 // stream.
565 // Prerequisite: IsClosedStream(stream_id) == false
566 // Server session might need to override this method to allow server push
567 // stream to be promised before creating an active stream.
568 virtual void HandleFrameOnNonexistentOutgoingStream(QuicStreamId stream_id);
569
570 virtual bool MaybeIncreaseLargestPeerStreamId(const QuicStreamId stream_id);
571
572 void InsertLocallyClosedStreamsHighestOffset(const QuicStreamId id,
573 QuicStreamOffset offset);
574 // If stream is a locally closed stream, this RST will update FIN offset.
575 // Otherwise stream is a preserved stream and the behavior of it depends on
576 // derived class's own implementation.
577 virtual void HandleRstOnValidNonexistentStream(
578 const QuicRstStreamFrame& frame);
579
580 // Returns a stateless reset token which will be included in the public reset
581 // packet.
582 virtual QuicUint128 GetStatelessResetToken() const;
583
584 QuicControlFrameManager& control_frame_manager() {
585 return control_frame_manager_;
586 }
587
588 const LegacyQuicStreamIdManager& stream_id_manager() const {
589 return stream_id_manager_;
590 }
591
renjietang0c558862019-05-08 13:26:23 -0700592 // Processes the stream type information of |pending| depending on
renjietangbb1c4892019-05-24 15:58:44 -0700593 // different kinds of sessions' own rules. Returns true if the pending stream
594 // is converted into a normal stream.
dschinazi17d42422019-06-18 16:35:07 -0700595 virtual bool ProcessPendingStream(PendingStream* /*pending*/) {
596 return false;
597 }
renjietang0c558862019-05-08 13:26:23 -0700598
QUICHE teamc258e4f2019-08-14 10:04:58 -0700599 bool IsHandshakeConfirmed() { return is_handshake_confirmed_; }
600
QUICHE teama6ef0a62019-03-07 20:34:33 -0500601 private:
602 friend class test::QuicSessionPeer;
603
604 // Called in OnConfigNegotiated when we receive a new stream level flow
605 // control window in a negotiated config. Closes the connection if invalid.
606 void OnNewStreamFlowControlWindow(QuicStreamOffset new_window);
607
608 // Called in OnConfigNegotiated when we receive a new connection level flow
609 // control window in a negotiated config. Closes the connection if invalid.
610 void OnNewSessionFlowControlWindow(QuicStreamOffset new_window);
611
612 // Debug helper for |OnCanWrite()|, check that OnStreamWrite() makes
613 // forward progress. Returns false if busy loop detected.
614 bool CheckStreamNotBusyLooping(QuicStream* stream,
615 uint64_t previous_bytes_written,
616 bool previous_fin_sent);
617
618 // Debug helper for OnCanWrite. Check that after QuicStream::OnCanWrite(),
619 // if stream has buffered data and is not stream level flow control blocked,
620 // it has to be in the write blocked list.
621 bool CheckStreamWriteBlocked(QuicStream* stream) const;
622
623 // Called in OnConfigNegotiated for Finch trials to measure performance of
624 // starting with larger flow control receive windows.
625 void AdjustInitialFlowControlWindows(size_t stream_window);
626
627 // Find stream with |id|, returns nullptr if the stream does not exist or
628 // closed.
629 QuicStream* GetStream(QuicStreamId id) const;
630
renjietange76b2da2019-05-13 14:50:23 -0700631 PendingStream* GetOrCreatePendingStream(QuicStreamId stream_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500632
633 // Let streams and control frame managers retransmit lost data, returns true
634 // if all lost data is retransmitted. Returns false otherwise.
635 bool RetransmitLostData();
636
637 // Closes the pending stream |stream_id| before it has been created.
638 void ClosePendingStream(QuicStreamId stream_id);
639
renjietange76b2da2019-05-13 14:50:23 -0700640 // Creates or gets pending stream, feeds it with |frame|, and processes the
641 // pending stream.
642 void PendingStreamOnStreamFrame(const QuicStreamFrame& frame);
643
644 // Creates or gets pending strea, feed it with |frame|, and closes the pending
645 // stream.
646 void PendingStreamOnRstStream(const QuicRstStreamFrame& frame);
647
QUICHE teama6ef0a62019-03-07 20:34:33 -0500648 // Keep track of highest received byte offset of locally closed streams, while
649 // waiting for a definitive final highest offset from the peer.
650 std::map<QuicStreamId, QuicStreamOffset>
651 locally_closed_streams_highest_offset_;
652
653 QuicConnection* connection_;
654
655 // May be null.
656 Visitor* visitor_;
657
658 // A list of streams which need to write more data. Stream register
659 // themselves in their constructor, and unregisterm themselves in their
660 // destructors, so the write blocked list must outlive all streams.
661 QuicWriteBlockedList write_blocked_streams_;
662
663 ClosedStreams closed_streams_;
664 // Streams which are closed, but need to be kept alive. Currently, the only
665 // reason is the stream's sent data (including FIN) does not get fully acked.
666 ZombieStreamMap zombie_streams_;
667
668 QuicConfig config_;
669
QUICHE teama6ef0a62019-03-07 20:34:33 -0500670 // Map from StreamId to pointers to streams. Owns the streams.
renjietang55d182a2019-07-12 10:26:25 -0700671 StreamMap stream_map_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500672
673 // Map from StreamId to PendingStreams for peer-created unidirectional streams
674 // which are waiting for the first byte of payload to arrive.
675 PendingStreamMap pending_stream_map_;
676
677 // Set of stream ids that are "draining" -- a FIN has been sent and received,
678 // but the stream object still exists because not all the received data has
679 // been consumed.
680 QuicUnorderedSet<QuicStreamId> draining_streams_;
681
zhongyi1b2f7832019-06-14 13:31:34 -0700682 // Set of stream ids that are waiting for acks excluding crypto stream id.
683 QuicUnorderedSet<QuicStreamId> streams_waiting_for_acks_;
684
QUICHE teama6ef0a62019-03-07 20:34:33 -0500685 // TODO(fayang): Consider moving LegacyQuicStreamIdManager into
686 // UberQuicStreamIdManager.
687 // Manages stream IDs for Google QUIC.
688 LegacyQuicStreamIdManager stream_id_manager_;
689
690 // Manages stream IDs for version99/IETF QUIC
691 UberQuicStreamIdManager v99_streamid_manager_;
692
renjietang55d182a2019-07-12 10:26:25 -0700693 // A counter for peer initiated dynamic streams which are in the stream_map_.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500694 size_t num_dynamic_incoming_streams_;
695
696 // A counter for peer initiated streams which are in the draining_streams_.
697 size_t num_draining_incoming_streams_;
698
renjietangfbeb5bf2019-04-19 15:06:20 -0700699 // A counter for self initiated static streams which are in
renjietang55d182a2019-07-12 10:26:25 -0700700 // stream_map_.
renjietangfbeb5bf2019-04-19 15:06:20 -0700701 size_t num_outgoing_static_streams_;
702
703 // A counter for peer initiated static streams which are in
renjietang55d182a2019-07-12 10:26:25 -0700704 // stream_map_.
renjietangfbeb5bf2019-04-19 15:06:20 -0700705 size_t num_incoming_static_streams_;
706
QUICHE teama6ef0a62019-03-07 20:34:33 -0500707 // A counter for peer initiated streams which are in the
708 // locally_closed_streams_highest_offset_.
709 size_t num_locally_closed_incoming_streams_highest_offset_;
710
fkastenholz488a4622019-08-26 06:24:46 -0700711 // Received information for a connection close.
712 QuicConnectionCloseFrame on_closed_frame_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500713
714 // Used for connection-level flow control.
715 QuicFlowController flow_controller_;
716
717 // The stream id which was last popped in OnCanWrite, or 0, if not under the
718 // call stack of OnCanWrite.
719 QuicStreamId currently_writing_stream_id_;
720
QUICHE teama6ef0a62019-03-07 20:34:33 -0500721 // Cached value of whether the crypto handshake has been confirmed.
722 bool is_handshake_confirmed_;
723
724 // Whether a GoAway has been sent.
725 bool goaway_sent_;
726
727 // Whether a GoAway has been received.
728 bool goaway_received_;
729
730 QuicControlFrameManager control_frame_manager_;
731
732 // Id of latest successfully sent message.
733 QuicMessageId last_message_id_;
734
735 // TODO(fayang): switch to linked_hash_set when chromium supports it. The bool
736 // is not used here.
737 // List of streams with pending retransmissions.
738 QuicLinkedHashMap<QuicStreamId, bool> streams_with_pending_retransmission_;
739
740 // Clean up closed_streams_ when this alarm fires.
741 std::unique_ptr<QuicAlarm> closed_streams_clean_up_alarm_;
742
743 // Supported version list used by the crypto handshake only. Please note, this
744 // list may be a superset of the connection framer's supported versions.
745 ParsedQuicVersionVector supported_versions_;
fayang944cfbc2019-07-31 09:15:00 -0700746
747 // If true, write_blocked_streams_ uses HTTP2 (tree-style) priority write
748 // scheduler.
749 bool use_http2_priority_write_scheduler_;
fkastenholz9b4b0ad2019-08-20 05:10:40 -0700750
751 // Initialized to false. Set to true when the session has been properly
752 // configured and is ready for general operation.
753 bool is_configured_;
renjietang216dc012019-08-27 11:28:27 -0700754
755 // The number of expected static streams.
756 QuicStreamCount num_expected_unidirectional_static_streams_;
fayang1b11b962019-09-16 14:01:48 -0700757
758 // If true, enables round robin scheduling.
759 bool enable_round_robin_scheduling_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500760};
761
762} // namespace quic
763
764#endif // QUICHE_QUIC_CORE_QUIC_SESSION_H_