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 #ifndef NET_QUIC_QUIC_STREAM_FACTORY_H_
6 #define NET_QUIC_QUIC_STREAM_FACTORY_H_
13 #include "base/logging.h"
14 #include "base/memory/weak_ptr.h"
15 #include "net/base/address_list.h"
16 #include "net/base/completion_callback.h"
17 #include "net/base/host_port_pair.h"
18 #include "net/base/network_change_notifier.h"
19 #include "net/cert/cert_database.h"
20 #include "net/log/net_log.h"
21 #include "net/proxy/proxy_server.h"
22 #include "net/quic/network_connection.h"
23 #include "net/quic/quic_chromium_client_session.h"
24 #include "net/quic/quic_config.h"
25 #include "net/quic/quic_crypto_stream.h"
26 #include "net/quic/quic_http_stream.h"
27 #include "net/quic/quic_protocol.h"
32 class ChannelIDService
;
33 class ClientSocketFactory
;
35 class HttpServerProperties
;
37 class QuicChromiumClientSession
;
38 class QuicConnectionHelper
;
39 class QuicCryptoClientStreamFactory
;
41 class QuicServerInfoFactory
;
43 class QuicStreamFactory
;
44 class TransportSecurityState
;
47 class QuicStreamFactoryPeer
;
50 // Encapsulates a pending request for a QuicHttpStream.
51 // If the request is still pending when it is destroyed, it will
52 // cancel the request with the factory.
53 class NET_EXPORT_PRIVATE QuicStreamRequest
{
55 explicit QuicStreamRequest(QuicStreamFactory
* factory
);
58 // For http, |is_https| is false.
59 // |cert_verify_flags| is bitwise OR'd of CertVerifier::VerifyFlags and it is
60 // passed to CertVerifier::Verify.
61 int Request(const HostPortPair
& host_port_pair
,
63 PrivacyMode privacy_mode
,
64 int cert_verify_flags
,
65 base::StringPiece origin_host
,
66 base::StringPiece method
,
67 const BoundNetLog
& net_log
,
68 const CompletionCallback
& callback
);
70 void OnRequestComplete(int rv
);
72 scoped_ptr
<QuicHttpStream
> ReleaseStream();
74 void set_stream(scoped_ptr
<QuicHttpStream
> stream
);
76 const std::string
origin_host() const { return origin_host_
; }
78 PrivacyMode
privacy_mode() const { return privacy_mode_
; }
80 const BoundNetLog
& net_log() const{
85 QuicStreamFactory
* factory_
;
86 HostPortPair host_port_pair_
;
87 std::string origin_host_
;
88 PrivacyMode privacy_mode_
;
90 CompletionCallback callback_
;
91 scoped_ptr
<QuicHttpStream
> stream_
;
93 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest
);
96 // A factory for creating new QuicHttpStreams on top of a pool of
97 // QuicChromiumClientSessions.
98 class NET_EXPORT_PRIVATE QuicStreamFactory
99 : public NetworkChangeNotifier::IPAddressObserver
,
100 public CertDatabase::Observer
{
103 HostResolver
* host_resolver
,
104 ClientSocketFactory
* client_socket_factory
,
105 base::WeakPtr
<HttpServerProperties
> http_server_properties
,
106 CertVerifier
* cert_verifier
,
107 ChannelIDService
* channel_id_service
,
108 TransportSecurityState
* transport_security_state
,
109 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory
,
110 QuicRandom
* random_generator
,
112 size_t max_packet_length
,
113 const std::string
& user_agent_id
,
114 const QuicVersionVector
& supported_versions
,
115 bool enable_port_selection
,
116 bool always_require_handshake_confirmation
,
117 bool disable_connection_pooling
,
118 float load_server_info_timeout_srtt_multiplier
,
119 bool enable_connection_racing
,
120 bool enable_non_blocking_io
,
121 bool disable_disk_cache
,
123 int max_number_of_lossy_connections
,
124 float packet_loss_threshold
,
125 int max_recent_disabled_reasons
,
126 int threshold_timeouts_with_streams_open
,
127 int threshold_public_resets_post_handshake
,
128 int socket_receive_buffer_size
,
129 const QuicTagVector
& connection_options
);
130 ~QuicStreamFactory() override
;
132 // Creates a new QuicHttpStream to |host_port_pair| which will be
133 // owned by |request|. |is_https| specifies if the protocol is https or not.
134 // If a matching session already exists, this method will return OK. If no
135 // matching session exists, this will return ERR_IO_PENDING and will invoke
136 // OnRequestComplete asynchronously.
137 int Create(const HostPortPair
& host_port_pair
,
139 PrivacyMode privacy_mode
,
140 int cert_verify_flags
,
141 base::StringPiece origin_host
,
142 base::StringPiece method
,
143 const BoundNetLog
& net_log
,
144 QuicStreamRequest
* request
);
146 // If |packet_loss_rate| is greater than or equal to |packet_loss_threshold_|
147 // it marks QUIC as recently broken for the port of the session. Increments
148 // |number_of_lossy_connections_| by port. If |number_of_lossy_connections_|
149 // is greater than or equal to |max_number_of_lossy_connections_| then it
150 // disables QUIC. If QUIC is disabled then it closes the connection.
152 // Returns true if QUIC is disabled for the port of the session.
153 bool OnHandshakeConfirmed(QuicChromiumClientSession
* session
,
154 float packet_loss_rate
);
156 // Returns true if QUIC is disabled for this port.
157 bool IsQuicDisabled(uint16 port
);
159 // Returns reason QUIC is disabled for this port, or QUIC_DISABLED_NOT if not.
160 QuicChromiumClientSession::QuicDisabledReason
QuicDisabledReason(
163 // Returns reason QUIC is disabled as string for net-internals, or
164 // returns empty string if QUIC is not disabled.
165 const char* QuicDisabledReasonString() const;
167 // Called by a session when it becomes idle.
168 void OnIdleSession(QuicChromiumClientSession
* session
);
170 // Called by a session when it is going away and no more streams should be
172 void OnSessionGoingAway(QuicChromiumClientSession
* session
);
174 // Called by a session after it shuts down.
175 void OnSessionClosed(QuicChromiumClientSession
* session
);
177 // Called by a session whose connection has timed out.
178 void OnSessionConnectTimeout(QuicChromiumClientSession
* session
);
180 // Cancels a pending request.
181 void CancelRequest(QuicStreamRequest
* request
);
183 // Closes all current sessions.
184 void CloseAllSessions(int error
);
186 scoped_ptr
<base::Value
> QuicStreamFactoryInfoToValue() const;
188 // Delete all cached state objects in |crypto_config_|.
189 void ClearCachedStatesInCryptoConfig();
191 // NetworkChangeNotifier::IPAddressObserver methods:
193 // Until the servers support roaming, close all connections when the local
194 // IP address changes.
195 void OnIPAddressChanged() override
;
197 // CertDatabase::Observer methods:
199 // We close all sessions when certificate database is changed.
200 void OnCertAdded(const X509Certificate
* cert
) override
;
201 void OnCACertChanged(const X509Certificate
* cert
) override
;
203 bool require_confirmation() const {
204 return require_confirmation_
;
207 void set_require_confirmation(bool require_confirmation
);
209 QuicConnectionHelper
* helper() { return helper_
.get(); }
211 bool enable_port_selection() const { return enable_port_selection_
; }
213 bool has_quic_server_info_factory() {
214 return quic_server_info_factory_
!= NULL
;
217 void set_quic_server_info_factory(
218 QuicServerInfoFactory
* quic_server_info_factory
) {
219 DCHECK(!quic_server_info_factory_
);
220 quic_server_info_factory_
= quic_server_info_factory
;
223 bool enable_connection_racing() const { return enable_connection_racing_
; }
224 void set_enable_connection_racing(bool enable_connection_racing
) {
225 enable_connection_racing_
= enable_connection_racing
;
228 int socket_receive_buffer_size() const { return socket_receive_buffer_size_
; }
232 friend class test::QuicStreamFactoryPeer
;
233 FRIEND_TEST_ALL_PREFIXES(HttpStreamFactoryTest
, QuicLossyProxyMarkedAsBad
);
235 // The key used to find session by ip. Includes
236 // the ip address, port, and scheme.
237 struct NET_EXPORT_PRIVATE IpAliasKey
{
239 IpAliasKey(IPEndPoint ip_endpoint
, bool is_https
);
242 IPEndPoint ip_endpoint
;
245 // Needed to be an element of std::set.
246 bool operator<(const IpAliasKey
&other
) const;
247 bool operator==(const IpAliasKey
&other
) const;
250 typedef std::map
<QuicServerId
, QuicChromiumClientSession
*> SessionMap
;
251 typedef std::map
<QuicChromiumClientSession
*, QuicServerId
> SessionIdMap
;
252 typedef std::set
<QuicServerId
> AliasSet
;
253 typedef std::map
<QuicChromiumClientSession
*, AliasSet
> SessionAliasMap
;
254 typedef std::set
<QuicChromiumClientSession
*> SessionSet
;
255 typedef std::map
<IpAliasKey
, SessionSet
> IPAliasMap
;
256 typedef std::map
<QuicServerId
, QuicCryptoClientConfig
*> CryptoConfigMap
;
257 typedef std::set
<Job
*> JobSet
;
258 typedef std::map
<QuicServerId
, JobSet
> JobMap
;
259 typedef std::map
<QuicStreamRequest
*, QuicServerId
> RequestMap
;
260 typedef std::set
<QuicStreamRequest
*> RequestSet
;
261 typedef std::map
<QuicServerId
, RequestSet
> ServerIDRequestsMap
;
262 typedef std::deque
<enum QuicChromiumClientSession::QuicDisabledReason
>
263 DisabledReasonsQueue
;
265 // Creates a job which doesn't wait for server config to be loaded from the
266 // disk cache. This job is started via a PostTask.
267 void CreateAuxilaryJob(const QuicServerId server_id
,
268 int cert_verify_flags
,
269 bool server_and_origin_have_same_host
,
271 const BoundNetLog
& net_log
);
273 // Returns a newly created QuicHttpStream owned by the caller.
274 scoped_ptr
<QuicHttpStream
> CreateFromSession(QuicChromiumClientSession
*);
276 bool OnResolution(const QuicServerId
& server_id
,
277 const AddressList
& address_list
);
278 void OnJobComplete(Job
* job
, int rv
);
279 bool HasActiveSession(const QuicServerId
& server_id
) const;
280 bool HasActiveJob(const QuicServerId
& server_id
) const;
281 int CreateSession(const QuicServerId
& server_id
,
282 int cert_verify_flags
,
283 scoped_ptr
<QuicServerInfo
> quic_server_info
,
284 const AddressList
& address_list
,
285 base::TimeTicks dns_resolution_end_time
,
286 const BoundNetLog
& net_log
,
287 QuicChromiumClientSession
** session
);
288 void ActivateSession(const QuicServerId
& key
,
289 QuicChromiumClientSession
* session
);
291 // Returns |srtt| in micro seconds from ServerNetworkStats. Returns 0 if there
292 // is no |http_server_properties_| or if |http_server_properties_| doesn't
293 // have ServerNetworkStats for the given |server_id|.
294 int64
GetServerNetworkStatsSmoothedRttInMicroseconds(
295 const QuicServerId
& server_id
) const;
298 bool WasQuicRecentlyBroken(const QuicServerId
& server_id
) const;
299 bool CryptoConfigCacheIsEmpty(const QuicServerId
& server_id
);
301 // Initializes the cached state associated with |server_id| in
302 // |crypto_config_| with the information in |server_info|.
303 void InitializeCachedStateInCryptoConfig(
304 const QuicServerId
& server_id
,
305 const scoped_ptr
<QuicServerInfo
>& server_info
);
307 void ProcessGoingAwaySession(QuicChromiumClientSession
* session
,
308 const QuicServerId
& server_id
,
309 bool was_session_active
);
311 // Collect stats from recent connections, possibly disabling Quic.
312 void MaybeDisableQuic(QuicChromiumClientSession
* session
);
314 bool require_confirmation_
;
315 HostResolver
* host_resolver_
;
316 ClientSocketFactory
* client_socket_factory_
;
317 base::WeakPtr
<HttpServerProperties
> http_server_properties_
;
318 TransportSecurityState
* transport_security_state_
;
319 QuicServerInfoFactory
* quic_server_info_factory_
;
320 QuicCryptoClientStreamFactory
* quic_crypto_client_stream_factory_
;
321 QuicRandom
* random_generator_
;
322 scoped_ptr
<QuicClock
> clock_
;
323 const size_t max_packet_length_
;
325 // The helper used for all connections.
326 scoped_ptr
<QuicConnectionHelper
> helper_
;
328 // Contains owning pointers to all sessions that currently exist.
329 SessionIdMap all_sessions_
;
330 // Contains non-owning pointers to currently active session
331 // (not going away session, once they're implemented).
332 SessionMap active_sessions_
;
333 // Map from session to set of aliases that this session is known by.
334 SessionAliasMap session_aliases_
;
335 // Map from IP address to sessions which are connected to this address.
336 IPAliasMap ip_aliases_
;
338 // Origins which have gone away recently.
339 AliasSet gone_away_aliases_
;
341 const QuicConfig config_
;
342 QuicCryptoClientConfig crypto_config_
;
345 ServerIDRequestsMap job_requests_map_
;
346 RequestMap active_requests_
;
348 QuicVersionVector supported_versions_
;
350 // Determine if we should consistently select a client UDP port. If false,
351 // then we will just let the OS select a random client port for each new
353 bool enable_port_selection_
;
355 // Set if we always require handshake confirmation. If true, this will
356 // introduce at least one RTT for the handshake before the client sends data.
357 bool always_require_handshake_confirmation_
;
359 // Set if we do not want connection pooling.
360 bool disable_connection_pooling_
;
362 // Specifies the ratio between time to load QUIC server information from disk
363 // cache to 'smoothed RTT'. This ratio is used to calculate the timeout in
364 // milliseconds to wait for loading of QUIC server information. If we don't
365 // want to timeout, set |load_server_info_timeout_srtt_multiplier_| to 0.
366 float load_server_info_timeout_srtt_multiplier_
;
368 // Set if we want to race connections - one connection that sends
369 // INCHOATE_HELLO and another connection that sends CHLO after loading server
370 // config from the disk cache.
371 bool enable_connection_racing_
;
373 // Set if experimental non-blocking IO should be used on windows sockets.
374 bool enable_non_blocking_io_
;
376 // Set if we do not want to load server config from the disk cache.
377 bool disable_disk_cache_
;
379 // Set if AES-GCM should be preferred, even if there is no hardware support.
382 // Set if we want to disable QUIC when there is high packet loss rate.
383 // Specifies the maximum number of connections with high packet loss in a row
384 // after which QUIC will be disabled.
385 int max_number_of_lossy_connections_
;
386 // Specifies packet loss rate in fraction after which a connection is closed
387 // and is considered as a lossy connection.
388 float packet_loss_threshold_
;
389 // Count number of lossy connections by port.
390 std::map
<uint16
, int> number_of_lossy_connections_
;
392 // Keep track of stats for recently closed connections, using a
394 int max_disabled_reasons_
;
395 DisabledReasonsQueue disabled_reasons_
;
396 // Events that can trigger disabling QUIC
397 int num_public_resets_post_handshake_
;
398 int num_timeouts_with_open_streams_
;
399 // Keep track the largest values for UMA histograms, that will help
400 // determine good threshold values.
401 int max_public_resets_post_handshake_
;
402 int max_timeouts_with_open_streams_
;
403 // Thresholds if greater than zero, determine when to
404 int threshold_timeouts_with_open_streams_
;
405 int threshold_public_resets_post_handshake_
;
407 // Size of the UDP receive buffer.
408 int socket_receive_buffer_size_
;
410 // Each profile will (probably) have a unique port_seed_ value. This value
411 // is used to help seed a pseudo-random number generator (PortSuggester) so
412 // that we consistently (within this profile) suggest the same ephemeral
413 // port when we re-connect to any given server/port. The differences between
414 // profiles (probablistically) prevent two profiles from colliding in their
415 // ephemeral port requests.
418 // Local address of socket that was created in CreateSession.
419 IPEndPoint local_address_
;
420 bool check_persisted_supports_quic_
;
421 std::set
<HostPortPair
> quic_supported_servers_at_startup_
;
423 NetworkConnection network_connection_
;
425 base::TaskRunner
* task_runner_
;
427 base::WeakPtrFactory
<QuicStreamFactory
> weak_factory_
;
429 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory
);
434 #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_