blob: 48e9996a7308dd0d1e3677d9836009a0b717c03e [file] [log] [blame]
[email protected]e13201d82012-12-12 05:00:321// 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#ifndef NET_QUIC_QUIC_STREAM_FACTORY_H_
6#define NET_QUIC_QUIC_STREAM_FACTORY_H_
7
[email protected]1cd2a5f2014-03-14 06:33:258#include <list>
[email protected]e13201d82012-12-12 05:00:329#include <map>
[email protected]41d6b172013-01-29 16:10:5710#include <string>
[email protected]6e12d702013-11-13 00:17:1711#include <vector>
[email protected]e13201d82012-12-12 05:00:3212
[email protected]e8cf7555b2014-02-28 23:52:5313#include "base/logging.h"
[email protected]e13201d82012-12-12 05:00:3214#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/net_log.h"
[email protected]f698a012013-05-06 20:18:5919#include "net/base/network_change_notifier.h"
[email protected]d7d1e50b2013-11-25 22:08:0920#include "net/cert/cert_database.h"
[email protected]e13201d82012-12-12 05:00:3221#include "net/proxy/proxy_server.h"
rtenneti041b2992015-02-23 23:03:2822#include "net/quic/network_connection.h"
[email protected]ef95114d2013-04-17 17:57:0123#include "net/quic/quic_config.h"
24#include "net/quic/quic_crypto_stream.h"
[email protected]e13201d82012-12-12 05:00:3225#include "net/quic/quic_http_stream.h"
26#include "net/quic/quic_protocol.h"
27
28namespace net {
29
[email protected]6d1b4ed2013-07-10 03:57:5430class CertVerifier;
[email protected]6b8a3c742014-07-25 00:25:3531class ChannelIDService;
[email protected]e13201d82012-12-12 05:00:3232class ClientSocketFactory;
[email protected]6d1b4ed2013-07-10 03:57:5433class HostResolver;
[email protected]77c6c162013-08-17 02:57:4534class HttpServerProperties;
[email protected]e13201d82012-12-12 05:00:3235class QuicClock;
36class QuicClientSession;
[email protected]2cfc6bb82013-10-27 03:40:4437class QuicConnectionHelper;
[email protected]e8ff26842013-03-22 21:02:0538class QuicCryptoClientStreamFactory;
[email protected]9558c5d32012-12-22 00:08:1439class QuicRandom;
[email protected]7832eeb2014-01-25 10:10:4340class QuicServerInfoFactory;
[email protected]257f24f2014-04-01 09:15:3741class QuicServerId;
[email protected]e13201d82012-12-12 05:00:3242class QuicStreamFactory;
[email protected]080b77932014-08-04 01:22:4643class TransportSecurityState;
[email protected]e13201d82012-12-12 05:00:3244
[email protected]c49ff182013-09-28 08:33:2645namespace test {
46class QuicStreamFactoryPeer;
47} // namespace test
48
[email protected]e13201d82012-12-12 05:00:3249// Encapsulates a pending request for a QuicHttpStream.
50// If the request is still pending when it is destroyed, it will
51// cancel the request with the factory.
52class NET_EXPORT_PRIVATE QuicStreamRequest {
53 public:
54 explicit QuicStreamRequest(QuicStreamFactory* factory);
55 ~QuicStreamRequest();
56
[email protected]0cceb922014-07-01 02:00:5657 // For http, |is_https| is false.
[email protected]bf4ea2f2014-03-10 22:57:5358 int Request(const HostPortPair& host_port_pair,
[email protected]6d1b4ed2013-07-10 03:57:5459 bool is_https,
[email protected]9dd3ff0f2014-03-26 09:51:2860 PrivacyMode privacy_mode,
[email protected]974849d2014-02-06 01:32:5961 base::StringPiece method,
[email protected]e13201d82012-12-12 05:00:3262 const BoundNetLog& net_log,
63 const CompletionCallback& callback);
64
65 void OnRequestComplete(int rv);
66
67 scoped_ptr<QuicHttpStream> ReleaseStream();
68
69 void set_stream(scoped_ptr<QuicHttpStream> stream);
70
71 const BoundNetLog& net_log() const{
72 return net_log_;
73 }
74
75 private:
76 QuicStreamFactory* factory_;
[email protected]bf4ea2f2014-03-10 22:57:5377 HostPortPair host_port_pair_;
[email protected]e13201d82012-12-12 05:00:3278 BoundNetLog net_log_;
79 CompletionCallback callback_;
80 scoped_ptr<QuicHttpStream> stream_;
81
82 DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest);
83};
84
85// A factory for creating new QuicHttpStreams on top of a pool of
86// QuicClientSessions.
[email protected]f698a012013-05-06 20:18:5987class NET_EXPORT_PRIVATE QuicStreamFactory
[email protected]d7d1e50b2013-11-25 22:08:0988 : public NetworkChangeNotifier::IPAddressObserver,
89 public CertDatabase::Observer {
[email protected]e13201d82012-12-12 05:00:3290 public:
[email protected]e8ff26842013-03-22 21:02:0591 QuicStreamFactory(
92 HostResolver* host_resolver,
93 ClientSocketFactory* client_socket_factory,
[email protected]77c6c162013-08-17 02:57:4594 base::WeakPtr<HttpServerProperties> http_server_properties,
[email protected]59c0bbd2014-03-22 04:08:1295 CertVerifier* cert_verifier,
[email protected]6b8a3c742014-07-25 00:25:3596 ChannelIDService* channel_id_service,
[email protected]080b77932014-08-04 01:22:4697 TransportSecurityState* transport_security_state,
[email protected]e8ff26842013-03-22 21:02:0598 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
99 QuicRandom* random_generator,
[email protected]256fe9b2013-11-27 01:58:02100 QuicClock* clock,
[email protected]1e960032013-12-20 19:00:20101 size_t max_packet_length,
[email protected]0c4017ca2014-06-06 03:30:45102 const std::string& user_agent_id,
[email protected]376d38a2014-01-22 03:47:35103 const QuicVersionVector& supported_versions,
[email protected]c80f7c92014-02-27 13:12:02104 bool enable_port_selection,
jri2b966f22014-09-02 22:25:36105 bool always_require_handshake_confirmation,
jri584002d12014-09-09 00:51:28106 bool disable_connection_pooling,
rtenneti2912825c2015-01-06 01:19:46107 float load_server_info_timeout_srtt_multiplier,
rtenneti4f809972015-02-11 19:38:34108 bool enable_connection_racing,
qyearsley3257b7de2015-02-28 06:59:03109 bool enable_non_blocking_io,
rtenneti34dffe752015-02-24 23:27:32110 bool disable_disk_cache,
rtenneti85dcfac22015-03-27 20:22:19111 int max_number_of_lossy_connections,
112 float packet_loss_threshold,
rchc7433572015-02-27 18:16:51113 int socket_receive_buffer_size,
[email protected]4b4efab32014-07-01 02:36:16114 const QuicTagVector& connection_options);
dchengb03027d2014-10-21 12:00:20115 ~QuicStreamFactory() override;
[email protected]e13201d82012-12-12 05:00:32116
[email protected]bf4ea2f2014-03-10 22:57:53117 // Creates a new QuicHttpStream to |host_port_pair| which will be
[email protected]6d1b4ed2013-07-10 03:57:54118 // owned by |request|. |is_https| specifies if the protocol is https or not.
[email protected]0cceb922014-07-01 02:00:56119 // If a matching session already exists, this method will return OK. If no
120 // matching session exists, this will return ERR_IO_PENDING and will invoke
121 // OnRequestComplete asynchronously.
[email protected]bf4ea2f2014-03-10 22:57:53122 int Create(const HostPortPair& host_port_pair,
[email protected]6d1b4ed2013-07-10 03:57:54123 bool is_https,
[email protected]9dd3ff0f2014-03-26 09:51:28124 PrivacyMode privacy_mode,
[email protected]974849d2014-02-06 01:32:59125 base::StringPiece method,
[email protected]e13201d82012-12-12 05:00:32126 const BoundNetLog& net_log,
127 QuicStreamRequest* request);
128
rtenneti85dcfac22015-03-27 20:22:19129 // Returns false if |packet_loss_rate| is less than |packet_loss_threshold_|
130 // otherwise it returns true and closes the session and marks QUIC as recently
131 // broken for the port of the session. Increments
132 // |number_of_lossy_connections_| by port.
133 bool OnHandshakeConfirmed(QuicClientSession* session, float packet_loss_rate);
134
135 // Returns true if QUIC is disabled for this port.
136 bool IsQuicDisabled(uint16 port);
137
[email protected]e13201d82012-12-12 05:00:32138 // Called by a session when it becomes idle.
139 void OnIdleSession(QuicClientSession* session);
140
[email protected]4d283b32013-10-17 12:57:27141 // Called by a session when it is going away and no more streams should be
142 // created on it.
143 void OnSessionGoingAway(QuicClientSession* session);
144
[email protected]e13201d82012-12-12 05:00:32145 // Called by a session after it shuts down.
[email protected]4d283b32013-10-17 12:57:27146 void OnSessionClosed(QuicClientSession* session);
[email protected]e13201d82012-12-12 05:00:32147
[email protected]65768442014-06-06 23:37:03148 // Called by a session whose connection has timed out.
149 void OnSessionConnectTimeout(QuicClientSession* session);
150
[email protected]e13201d82012-12-12 05:00:32151 // Cancels a pending request.
152 void CancelRequest(QuicStreamRequest* request);
153
[email protected]56dfb902013-01-03 23:17:55154 // Closes all current sessions.
155 void CloseAllSessions(int error);
156
[email protected]c5b061b2013-01-05 00:31:34157 base::Value* QuicStreamFactoryInfoToValue() const;
158
[email protected]f7e21a432014-04-21 22:17:57159 // Delete all cached state objects in |crypto_config_|.
[email protected]60cf50e2014-04-28 23:23:18160 void ClearCachedStatesInCryptoConfig();
[email protected]f7e21a432014-04-21 22:17:57161
[email protected]f698a012013-05-06 20:18:59162 // NetworkChangeNotifier::IPAddressObserver methods:
163
164 // Until the servers support roaming, close all connections when the local
165 // IP address changes.
dchengb03027d2014-10-21 12:00:20166 void OnIPAddressChanged() override;
[email protected]f698a012013-05-06 20:18:59167
[email protected]d7d1e50b2013-11-25 22:08:09168 // CertDatabase::Observer methods:
169
170 // We close all sessions when certificate database is changed.
dchengb03027d2014-10-21 12:00:20171 void OnCertAdded(const X509Certificate* cert) override;
172 void OnCACertChanged(const X509Certificate* cert) override;
[email protected]d7d1e50b2013-11-25 22:08:09173
jri2b966f22014-09-02 22:25:36174 bool require_confirmation() const {
175 return require_confirmation_;
176 }
[email protected]11c05872013-08-20 02:04:12177
rtennetifc47e0e2014-09-26 02:54:05178 void set_require_confirmation(bool require_confirmation);
[email protected]11c05872013-08-20 02:04:12179
[email protected]2cfc6bb82013-10-27 03:40:44180 QuicConnectionHelper* helper() { return helper_.get(); }
181
[email protected]376d38a2014-01-22 03:47:35182 bool enable_port_selection() const { return enable_port_selection_; }
183
[email protected]a4205202014-06-02 16:03:08184 bool has_quic_server_info_factory() {
185 return quic_server_info_factory_ != NULL;
186 }
187
[email protected]e8cf7555b2014-02-28 23:52:53188 void set_quic_server_info_factory(
189 QuicServerInfoFactory* quic_server_info_factory) {
190 DCHECK(!quic_server_info_factory_);
191 quic_server_info_factory_ = quic_server_info_factory;
192 }
193
rtenneti14abd312015-02-06 21:56:01194 bool enable_connection_racing() const { return enable_connection_racing_; }
195 void set_enable_connection_racing(bool enable_connection_racing) {
196 enable_connection_racing_ = enable_connection_racing;
197 }
198
[email protected]e13201d82012-12-12 05:00:32199 private:
200 class Job;
[email protected]c49ff182013-09-28 08:33:26201 friend class test::QuicStreamFactoryPeer;
[email protected]e13201d82012-12-12 05:00:32202
[email protected]9dd3ff0f2014-03-26 09:51:28203 // The key used to find session by ip. Includes
[email protected]df157d9d2014-03-10 07:27:27204 // the ip address, port, and scheme.
205 struct NET_EXPORT_PRIVATE IpAliasKey {
206 IpAliasKey();
207 IpAliasKey(IPEndPoint ip_endpoint, bool is_https);
208 ~IpAliasKey();
209
210 IPEndPoint ip_endpoint;
211 bool is_https;
212
213 // Needed to be an element of std::set.
214 bool operator<(const IpAliasKey &other) const;
215 bool operator==(const IpAliasKey &other) const;
216 };
217
[email protected]257f24f2014-04-01 09:15:37218 typedef std::map<QuicServerId, QuicClientSession*> SessionMap;
[email protected]4d590c9c2014-05-02 05:14:33219 typedef std::map<QuicClientSession*, QuicServerId> SessionIdMap;
[email protected]257f24f2014-04-01 09:15:37220 typedef std::set<QuicServerId> AliasSet;
[email protected]e13201d82012-12-12 05:00:32221 typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap;
222 typedef std::set<QuicClientSession*> SessionSet;
[email protected]df157d9d2014-03-10 07:27:27223 typedef std::map<IpAliasKey, SessionSet> IPAliasMap;
[email protected]257f24f2014-04-01 09:15:37224 typedef std::map<QuicServerId, QuicCryptoClientConfig*> CryptoConfigMap;
rtenneti14abd312015-02-06 21:56:01225 typedef std::set<Job*> JobSet;
226 typedef std::map<QuicServerId, JobSet> JobMap;
227 typedef std::map<QuicStreamRequest*, QuicServerId> RequestMap;
[email protected]e13201d82012-12-12 05:00:32228 typedef std::set<QuicStreamRequest*> RequestSet;
rtenneti14abd312015-02-06 21:56:01229 typedef std::map<QuicServerId, RequestSet> ServerIDRequestsMap;
230
231 // Creates a job which doesn't wait for server config to be loaded from the
232 // disk cache. This job is started via a PostTask.
233 void CreateAuxilaryJob(const QuicServerId server_id,
234 bool is_post,
235 const BoundNetLog& net_log);
[email protected]e13201d82012-12-12 05:00:32236
[email protected]df157d9d2014-03-10 07:27:27237 // Returns a newly created QuicHttpStream owned by the caller, if a
238 // matching session already exists. Returns NULL otherwise.
[email protected]257f24f2014-04-01 09:15:37239 scoped_ptr<QuicHttpStream> CreateIfSessionExists(const QuicServerId& key,
[email protected]df157d9d2014-03-10 07:27:27240 const BoundNetLog& net_log);
241
[email protected]257f24f2014-04-01 09:15:37242 bool OnResolution(const QuicServerId& server_id,
[email protected]eed749f92013-12-23 18:57:38243 const AddressList& address_list);
[email protected]e13201d82012-12-12 05:00:32244 void OnJobComplete(Job* job, int rv);
[email protected]257f24f2014-04-01 09:15:37245 bool HasActiveSession(const QuicServerId& server_id) const;
246 bool HasActiveJob(const QuicServerId& server_id) const;
247 int CreateSession(const QuicServerId& server_id,
[email protected]17bf15c2014-03-14 10:08:04248 scoped_ptr<QuicServerInfo> quic_server_info,
[email protected]338e7982013-12-13 11:15:32249 const AddressList& address_list,
rtennetif4f08852015-02-27 17:50:04250 base::TimeTicks dns_resolution_end_time,
[email protected]338e7982013-12-13 11:15:32251 const BoundNetLog& net_log,
252 QuicClientSession** session);
[email protected]257f24f2014-04-01 09:15:37253 void ActivateSession(const QuicServerId& key,
[email protected]e13201d82012-12-12 05:00:32254 QuicClientSession* session);
255
rtenneti2912825c2015-01-06 01:19:46256 // Returns |srtt| in micro seconds from ServerNetworkStats. Returns 0 if there
257 // is no |http_server_properties_| or if |http_server_properties_| doesn't
258 // have ServerNetworkStats for the given |server_id|.
259 int64 GetServerNetworkStatsSmoothedRttInMicroseconds(
260 const QuicServerId& server_id) const;
261
bnccacc0992015-03-20 20:22:22262 // Helper methods.
263 bool WasQuicRecentlyBroken(const QuicServerId& server_id) const;
rtenneti14abd312015-02-06 21:56:01264 bool CryptoConfigCacheIsEmpty(const QuicServerId& server_id);
265
[email protected]257f24f2014-04-01 09:15:37266 // Initializes the cached state associated with |server_id| in
[email protected]59c0bbd2014-03-22 04:08:12267 // |crypto_config_| with the information in |server_info|.
[email protected]60cf50e2014-04-28 23:23:18268 void InitializeCachedStateInCryptoConfig(
269 const QuicServerId& server_id,
270 const scoped_ptr<QuicServerInfo>& server_info);
[email protected]b694e48c2014-03-18 17:10:13271
[email protected]4d590c9c2014-05-02 05:14:33272 void ProcessGoingAwaySession(QuicClientSession* session,
[email protected]eb71ab62014-05-23 07:57:53273 const QuicServerId& server_id,
274 bool was_session_active);
[email protected]4d590c9c2014-05-02 05:14:33275
[email protected]11c05872013-08-20 02:04:12276 bool require_confirmation_;
[email protected]e13201d82012-12-12 05:00:32277 HostResolver* host_resolver_;
278 ClientSocketFactory* client_socket_factory_;
[email protected]77c6c162013-08-17 02:57:45279 base::WeakPtr<HttpServerProperties> http_server_properties_;
[email protected]5db452202014-08-19 05:22:15280 TransportSecurityState* transport_security_state_;
[email protected]7832eeb2014-01-25 10:10:43281 QuicServerInfoFactory* quic_server_info_factory_;
[email protected]e8ff26842013-03-22 21:02:05282 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
[email protected]9558c5d32012-12-22 00:08:14283 QuicRandom* random_generator_;
[email protected]f1e97e92012-12-16 04:53:25284 scoped_ptr<QuicClock> clock_;
[email protected]256fe9b2013-11-27 01:58:02285 const size_t max_packet_length_;
[email protected]e13201d82012-12-12 05:00:32286
[email protected]2cfc6bb82013-10-27 03:40:44287 // The helper used for all connections.
288 scoped_ptr<QuicConnectionHelper> helper_;
289
[email protected]e13201d82012-12-12 05:00:32290 // Contains owning pointers to all sessions that currently exist.
[email protected]4d590c9c2014-05-02 05:14:33291 SessionIdMap all_sessions_;
[email protected]e13201d82012-12-12 05:00:32292 // Contains non-owning pointers to currently active session
293 // (not going away session, once they're implemented).
294 SessionMap active_sessions_;
[email protected]eed749f92013-12-23 18:57:38295 // Map from session to set of aliases that this session is known by.
[email protected]e13201d82012-12-12 05:00:32296 SessionAliasMap session_aliases_;
[email protected]eed749f92013-12-23 18:57:38297 // Map from IP address to sessions which are connected to this address.
298 IPAliasMap ip_aliases_;
[email protected]e13201d82012-12-12 05:00:32299
[email protected]d8e2abf82014-03-06 10:30:10300 // Origins which have gone away recently.
301 AliasSet gone_away_aliases_;
302
[email protected]fd276a282014-06-11 04:26:14303 const QuicConfig config_;
[email protected]59c0bbd2014-03-22 04:08:12304 QuicCryptoClientConfig crypto_config_;
[email protected]b064310782013-05-30 21:12:17305
[email protected]e13201d82012-12-12 05:00:32306 JobMap active_jobs_;
rtenneti14abd312015-02-06 21:56:01307 ServerIDRequestsMap job_requests_map_;
[email protected]e13201d82012-12-12 05:00:32308 RequestMap active_requests_;
309
[email protected]1e960032013-12-20 19:00:20310 QuicVersionVector supported_versions_;
[email protected]e13201d82012-12-12 05:00:32311
[email protected]376d38a2014-01-22 03:47:35312 // Determine if we should consistently select a client UDP port. If false,
313 // then we will just let the OS select a random client port for each new
314 // connection.
315 bool enable_port_selection_;
316
jri2b966f22014-09-02 22:25:36317 // Set if we always require handshake confirmation. If true, this will
318 // introduce at least one RTT for the handshake before the client sends data.
319 bool always_require_handshake_confirmation_;
320
jri584002d12014-09-09 00:51:28321 // Set if we do not want connection pooling.
322 bool disable_connection_pooling_;
323
rtenneti2912825c2015-01-06 01:19:46324 // Specifies the ratio between time to load QUIC server information from disk
325 // cache to 'smoothed RTT'. This ratio is used to calculate the timeout in
326 // milliseconds to wait for loading of QUIC server information. If we don't
327 // want to timeout, set |load_server_info_timeout_srtt_multiplier_| to 0.
328 float load_server_info_timeout_srtt_multiplier_;
329
rtenneti14abd312015-02-06 21:56:01330 // Set if we want to race connections - one connection that sends
331 // INCHOATE_HELLO and another connection that sends CHLO after loading server
332 // config from the disk cache.
333 bool enable_connection_racing_;
334
qyearsley3257b7de2015-02-28 06:59:03335 // Set if experimental non-blocking IO should be used on windows sockets.
336 bool enable_non_blocking_io_;
337
rtenneti34dffe752015-02-24 23:27:32338 // Set if we do not want to load server config from the disk cache.
339 bool disable_disk_cache_;
340
rtenneti85dcfac22015-03-27 20:22:19341 // Set if we want to disable QUIC when there is high packet loss rate.
342 // Specifies the maximum number of connections with high packet loss in a row
343 // after which QUIC will be disabled.
344 int max_number_of_lossy_connections_;
345 // Specifies packet loss rate in franction after which a connection is closed
346 // and is considered as a lossy connection.
347 float packet_loss_threshold_;
348 // Count number of lossy connections by port.
349 std::map<uint16, int> number_of_lossy_connections_;
350
rchc7433572015-02-27 18:16:51351 // Size of the UDP receive buffer.
352 int socket_receive_buffer_size_;
353
354 // Each profile will (probably) have a unique port_seed_ value. This value
355 // is used to help seed a pseudo-random number generator (PortSuggester) so
356 // that we consistently (within this profile) suggest the same ephemeral
357 // port when we re-connect to any given server/port. The differences between
358 // profiles (probablistically) prevent two profiles from colliding in their
359 // ephemeral port requests.
[email protected]337e1452013-12-16 23:57:50360 uint64 port_seed_;
[email protected]7034cf12013-12-13 22:47:07361
rtennetifc47e0e2014-09-26 02:54:05362 // Local address of socket that was created in CreateSession.
363 IPEndPoint local_address_;
364 bool check_persisted_supports_quic_;
rtenneti1681f852014-11-13 20:34:03365 std::set<HostPortPair> quic_supported_servers_at_startup_;
rtennetifc47e0e2014-09-26 02:54:05366
rtenneti041b2992015-02-23 23:03:28367 NetworkConnection network_connection_;
368
rtenneti38f5cd52014-10-28 20:28:28369 base::TaskRunner* task_runner_;
370
[email protected]1e960032013-12-20 19:00:20371 base::WeakPtrFactory<QuicStreamFactory> weak_factory_;
372
[email protected]e13201d82012-12-12 05:00:32373 DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory);
374};
375
376} // namespace net
377
378#endif // NET_QUIC_QUIC_STREAM_FACTORY_H_