1 // 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.
5 // A QuicSession, which demuxes a single connection to individual streams.
7 #ifndef NET_QUIC_QUIC_SESSION_H_
8 #define NET_QUIC_QUIC_SESSION_H_
14 #include "build/build_config.h"
16 // TODO(rtenneti): Temporary while investigating crbug.com/473893.
17 // Note base::Debug::StackTrace() is not supported in NACL
18 // builds so conditionally disabled it there.
20 #define TEMP_INSTRUMENTATION_473893
23 #include "base/compiler_specific.h"
24 #include "base/containers/hash_tables.h"
25 #ifdef TEMP_INSTRUMENTATION_473893
26 #include "base/debug/stack_trace.h"
28 #include "base/memory/scoped_ptr.h"
29 #include "base/strings/string_piece.h"
30 #include "net/base/ip_endpoint.h"
31 #include "net/quic/quic_connection.h"
32 #include "net/quic/quic_crypto_stream.h"
33 #include "net/quic/quic_packet_creator.h"
34 #include "net/quic/quic_protocol.h"
35 #include "net/quic/quic_write_blocked_list.h"
36 #include "net/quic/reliable_quic_stream.h"
40 class QuicCryptoStream
;
41 class QuicFlowController
;
42 class ReliableQuicStream
;
46 class QuicSessionPeer
;
49 class NET_EXPORT_PRIVATE QuicSession
: public QuicConnectionVisitorInterface
{
51 // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
52 enum CryptoHandshakeEvent
{
53 // ENCRYPTION_FIRST_ESTABLISHED indicates that a full client hello has been
54 // sent by a client and that subsequent packets will be encrypted. (Client
56 ENCRYPTION_FIRST_ESTABLISHED
,
57 // ENCRYPTION_REESTABLISHED indicates that a client hello was rejected by
58 // the server and thus the encryption key has been updated. Therefore the
59 // connection should resend any packets that were sent under
60 // ENCRYPTION_INITIAL. (Client only.)
61 ENCRYPTION_REESTABLISHED
,
62 // HANDSHAKE_CONFIRMED, in a client, indicates the the server has accepted
63 // our handshake. In a server it indicates that a full, valid client hello
64 // has been received. (Client and server.)
68 QuicSession(QuicConnection
* connection
, const QuicConfig
& config
);
70 ~QuicSession() override
;
72 virtual void Initialize();
74 // QuicConnectionVisitorInterface methods:
75 void OnStreamFrame(const QuicStreamFrame
& frame
) override
;
76 void OnRstStream(const QuicRstStreamFrame
& frame
) override
;
77 void OnGoAway(const QuicGoAwayFrame
& frame
) override
;
78 void OnWindowUpdateFrame(const QuicWindowUpdateFrame
& frame
) override
;
79 void OnBlockedFrame(const QuicBlockedFrame
& frame
) override
;
80 void OnConnectionClosed(QuicErrorCode error
, bool from_peer
) override
;
81 void OnWriteBlocked() override
{}
82 void OnSuccessfulVersionNegotiation(const QuicVersion
& version
) override
;
83 void OnCanWrite() override
;
84 void OnCongestionWindowChange(QuicTime now
) override
{}
85 void OnConnectionMigration() override
{}
86 bool WillingAndAbleToWrite() const override
;
87 bool HasPendingHandshake() const override
;
88 bool HasOpenDynamicStreams() const override
;
90 // Called by streams when they want to write data to the peer.
91 // Returns a pair with the number of bytes consumed from data, and a boolean
92 // indicating if the fin bit was consumed. This does not indicate the data
93 // has been sent on the wire: it may have been turned into a packet and queued
94 // if the socket was unexpectedly blocked. |fec_protection| indicates if
95 // data is to be FEC protected. Note that data that is sent immediately
96 // following MUST_FEC_PROTECT data may get protected by falling within the
98 // If provided, |ack_notifier_delegate| will be registered to be notified when
99 // we have seen ACKs for all packets resulting from this call.
100 virtual QuicConsumedData
WritevData(
102 const QuicIOVector
& iov
,
103 QuicStreamOffset offset
,
105 FecProtection fec_protection
,
106 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
108 // Called by streams when they want to close the stream in both directions.
109 virtual void SendRstStream(QuicStreamId id
,
110 QuicRstStreamErrorCode error
,
111 QuicStreamOffset bytes_written
);
113 // Called when the session wants to go away and not accept any new streams.
114 void SendGoAway(QuicErrorCode error_code
, const std::string
& reason
);
116 // Removes the stream associated with 'stream_id' from the active stream map.
117 virtual void CloseStream(QuicStreamId stream_id
);
119 // Returns true if outgoing packets will be encrypted, even if the server
120 // hasn't confirmed the handshake yet.
121 virtual bool IsEncryptionEstablished();
123 // For a client, returns true if the server has confirmed our handshake. For
124 // a server, returns true if a full, valid client hello has been received.
125 virtual bool IsCryptoHandshakeConfirmed();
127 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
128 virtual void OnConfigNegotiated();
130 // Called by the QuicCryptoStream when the handshake enters a new state.
132 // Clients will call this function in the order:
133 // ENCRYPTION_FIRST_ESTABLISHED
134 // zero or more ENCRYPTION_REESTABLISHED
135 // HANDSHAKE_CONFIRMED
137 // Servers will simply call it once with HANDSHAKE_CONFIRMED.
138 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event
);
140 // Called by the QuicCryptoStream when a handshake message is sent.
141 virtual void OnCryptoHandshakeMessageSent(
142 const CryptoHandshakeMessage
& message
);
144 // Called by the QuicCryptoStream when a handshake message is received.
145 virtual void OnCryptoHandshakeMessageReceived(
146 const CryptoHandshakeMessage
& message
);
148 // Returns mutable config for this session. Returned config is owned
150 QuicConfig
* config();
152 // Returns true if the stream existed previously and has been closed.
153 // Returns false if the stream is still active or if the stream has
154 // not yet been created.
155 bool IsClosedStream(QuicStreamId id
);
157 QuicConnection
* connection() {
158 // TODO(rtenneti): Temporary while investigating crbug.com/473893
160 return connection_
.get();
162 const QuicConnection
* connection() const {
163 // TODO(rtenneti): Temporary while investigating crbug.com/473893
165 return connection_
.get();
167 size_t num_active_requests() const { return dynamic_stream_map_
.size(); }
168 const IPEndPoint
& peer_address() const {
169 return connection_
->peer_address();
171 QuicConnectionId
connection_id() const {
172 return connection_
->connection_id();
175 // Returns the number of currently open streams, including those which have
176 // been implicitly created, but excluding the reserved headers and crypto
178 virtual size_t GetNumOpenStreams() const;
180 // Add the stream to the session's write-blocked list because it is blocked by
181 // connection-level flow control but not by its own stream-level flow control.
182 // The stream will be given a chance to write when a connection-level
183 // WINDOW_UPDATE arrives.
184 void MarkConnectionLevelWriteBlocked(QuicStreamId id
, QuicPriority priority
);
186 // Returns true if the session has data to be sent, either queued in the
187 // connection, or in a write-blocked stream.
188 bool HasDataToWrite() const;
190 bool goaway_sent() const;
192 bool goaway_received() const;
194 QuicErrorCode
error() const { return error_
; }
196 Perspective
perspective() const { return connection_
->perspective(); }
198 QuicFlowController
* flow_controller() { return &flow_controller_
; }
200 // Returns true if connection is flow controller blocked.
201 bool IsConnectionFlowControlBlocked() const;
203 // Returns true if any stream is flow controller blocked.
204 bool IsStreamFlowControlBlocked();
206 // Returns true if this is a secure QUIC session.
207 bool IsSecure() const { return connection()->is_secure(); }
209 size_t get_max_open_streams() const { return max_open_streams_
; }
211 ReliableQuicStream
* GetStream(const QuicStreamId stream_id
);
213 // Mark a stream as draining.
214 void StreamDraining(QuicStreamId id
);
217 typedef base::hash_map
<QuicStreamId
, ReliableQuicStream
*> StreamMap
;
219 // Creates a new stream, owned by the caller, to handle a peer-initiated
220 // stream. Returns nullptr and does error handling if the stream can not be
222 virtual ReliableQuicStream
* CreateIncomingDynamicStream(QuicStreamId id
) = 0;
224 // Create a new stream, owned by the caller, to handle a locally-initiated
225 // stream. Returns nullptr if max streams have already been opened.
226 virtual ReliableQuicStream
* CreateOutgoingDynamicStream() = 0;
228 // Return the reserved crypto stream.
229 virtual QuicCryptoStream
* GetCryptoStream() = 0;
231 // Adds 'stream' to the active stream map.
232 virtual void ActivateStream(ReliableQuicStream
* stream
);
234 // Returns the stream id for a new stream.
235 QuicStreamId
GetNextStreamId();
237 ReliableQuicStream
* GetIncomingDynamicStream(QuicStreamId stream_id
);
239 ReliableQuicStream
* GetDynamicStream(const QuicStreamId stream_id
);
241 // This is called after every call other than OnConnectionClose from the
242 // QuicConnectionVisitor to allow post-processing once the work has been done.
243 // In this case, it deletes streams given that it's safe to do so (no other
244 // operations are being done on the streams at this time)
245 virtual void PostProcessAfterData();
247 StreamMap
& static_streams() { return static_stream_map_
; }
248 const StreamMap
& static_streams() const { return static_stream_map_
; }
250 StreamMap
& dynamic_streams() { return dynamic_stream_map_
; }
251 const StreamMap
& dynamic_streams() const { return dynamic_stream_map_
; }
253 std::vector
<ReliableQuicStream
*>* closed_streams() {
254 return &closed_streams_
;
257 void set_max_open_streams(size_t max_open_streams
);
259 void set_largest_peer_created_stream_id(
260 QuicStreamId largest_peer_created_stream_id
) {
261 largest_peer_created_stream_id_
= largest_peer_created_stream_id
;
265 friend class test::QuicSessionPeer
;
266 friend class VisitorShim
;
268 #ifdef TEMP_INSTRUMENTATION_473893
269 // TODO(rtenneti): Temporary while investigating crbug.com/473893
276 // Performs the work required to close |stream_id|. If |locally_reset|
277 // then the stream has been reset by this endpoint, not by the peer.
278 void CloseStreamInner(QuicStreamId stream_id
, bool locally_reset
);
280 // When a stream is closed locally, it may not yet know how many bytes the
281 // peer sent on that stream.
282 // When this data arrives (via stream frame w. FIN, or RST) this method
283 // is called, and correctly updates the connection level flow controller.
284 void UpdateFlowControlOnFinalReceivedByteOffset(
285 QuicStreamId id
, QuicStreamOffset final_byte_offset
);
287 // Called in OnConfigNegotiated when we receive a new stream level flow
288 // control window in a negotiated config. Closes the connection if invalid.
289 void OnNewStreamFlowControlWindow(QuicStreamOffset new_window
);
291 // Called in OnConfigNegotiated when we receive a new connection level flow
292 // control window in a negotiated config. Closes the connection if invalid.
293 void OnNewSessionFlowControlWindow(QuicStreamOffset new_window
);
295 // Called in OnConfigNegotiated when auto-tuning is enabled for flow
296 // control receive windows.
297 void EnableAutoTuneReceiveWindow();
299 // TODO(rtenneti): Temporary while investigating crbug.com/473893
300 void CrashIfInvalid() const;
302 // Keep track of highest received byte offset of locally closed streams, while
303 // waiting for a definitive final highest offset from the peer.
304 std::map
<QuicStreamId
, QuicStreamOffset
>
305 locally_closed_streams_highest_offset_
;
307 scoped_ptr
<QuicConnection
> connection_
;
309 // A shim to stand between the connection and the session, to handle stream
311 scoped_ptr
<VisitorShim
> visitor_shim_
;
313 std::vector
<ReliableQuicStream
*> closed_streams_
;
317 // Returns the maximum number of streams this connection can open.
318 size_t max_open_streams_
;
320 // Static streams, such as crypto and header streams. Owned by child classes
321 // that create these streams.
322 StreamMap static_stream_map_
;
324 // Map from StreamId to pointers to streams that are owned by the caller.
325 StreamMap dynamic_stream_map_
;
326 QuicStreamId next_stream_id_
;
328 // Set of stream ids that have been "implicitly created" by receipt
329 // of a stream id larger than the next expected stream id.
330 base::hash_set
<QuicStreamId
> implicitly_created_streams_
;
332 // Set of stream ids that are "draining" -- a FIN has been sent and received,
333 // but the stream object still exists because not all the received data has
335 base::hash_set
<QuicStreamId
> draining_streams_
;
337 // A list of streams which need to write more data.
338 QuicWriteBlockedList write_blocked_streams_
;
340 QuicStreamId largest_peer_created_stream_id_
;
342 // The latched error with which the connection was closed.
343 QuicErrorCode error_
;
345 // Used for connection-level flow control.
346 QuicFlowController flow_controller_
;
348 // Indicate if there is pending data for the crypto stream.
349 bool has_pending_handshake_
;
351 #ifdef TEMP_INSTRUMENTATION_473893
352 // TODO(rtenneti): Temporary while investigating crbug.com/473893
353 Liveness liveness_
= ALIVE
;
354 base::debug::StackTrace stack_trace_
;
357 DISALLOW_COPY_AND_ASSIGN(QuicSession
);
362 #endif // NET_QUIC_QUIC_SESSION_H_