Re-land: C++ readability review
[chromium-blink-merge.git] / net / quic / quic_stream_factory.cc
blob9776448cc5f0aa3948d3998b395675b637c8b55e
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 #include "net/quic/quic_stream_factory.h"
7 #include <set>
9 #include "base/cpu.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/metrics/field_trial.h"
13 #include "base/metrics/histogram.h"
14 #include "base/metrics/sparse_histogram.h"
15 #include "base/rand_util.h"
16 #include "base/stl_util.h"
17 #include "base/strings/string_util.h"
18 #include "base/values.h"
19 #include "net/base/net_errors.h"
20 #include "net/cert/cert_verifier.h"
21 #include "net/dns/host_resolver.h"
22 #include "net/dns/single_request_host_resolver.h"
23 #include "net/http/http_server_properties.h"
24 #include "net/quic/crypto/channel_id_chromium.h"
25 #include "net/quic/crypto/proof_verifier_chromium.h"
26 #include "net/quic/crypto/quic_random.h"
27 #include "net/quic/crypto/quic_server_info.h"
28 #include "net/quic/port_suggester.h"
29 #include "net/quic/quic_client_session.h"
30 #include "net/quic/quic_clock.h"
31 #include "net/quic/quic_connection.h"
32 #include "net/quic/quic_connection_helper.h"
33 #include "net/quic/quic_crypto_client_stream_factory.h"
34 #include "net/quic/quic_default_packet_writer.h"
35 #include "net/quic/quic_flags.h"
36 #include "net/quic/quic_http_stream.h"
37 #include "net/quic/quic_protocol.h"
38 #include "net/quic/quic_server_id.h"
39 #include "net/socket/client_socket_factory.h"
40 #include "net/udp/udp_client_socket.h"
42 #if defined(OS_WIN)
43 #include "base/win/windows_version.h"
44 #endif
46 namespace net {
48 namespace {
50 enum CreateSessionFailure {
51 CREATION_ERROR_CONNECTING_SOCKET,
52 CREATION_ERROR_SETTING_RECEIVE_BUFFER,
53 CREATION_ERROR_SETTING_SEND_BUFFER,
54 CREATION_ERROR_MAX
57 // When a connection is idle for 30 seconds it will be closed.
58 const int kIdleConnectionTimeoutSeconds = 30;
60 // The initial receive window size for both streams and sessions.
61 const int32 kInitialReceiveWindowSize = 10 * 1024 * 1024; // 10MB
63 // Set the maximum number of undecryptable packets the connection will store.
64 const int32 kMaxUndecryptablePackets = 100;
66 void HistogramCreateSessionFailure(enum CreateSessionFailure error) {
67 UMA_HISTOGRAM_ENUMERATION("Net.QuicSession.CreationError", error,
68 CREATION_ERROR_MAX);
71 bool IsEcdsaSupported() {
72 #if defined(OS_WIN)
73 if (base::win::GetVersion() < base::win::VERSION_VISTA)
74 return false;
75 #endif
77 return true;
80 QuicConfig InitializeQuicConfig(const QuicTagVector& connection_options) {
81 QuicConfig config;
82 config.SetIdleConnectionStateLifetime(
83 QuicTime::Delta::FromSeconds(kIdleConnectionTimeoutSeconds),
84 QuicTime::Delta::FromSeconds(kIdleConnectionTimeoutSeconds));
85 config.SetConnectionOptionsToSend(connection_options);
86 return config;
89 class DefaultPacketWriterFactory : public QuicConnection::PacketWriterFactory {
90 public:
91 explicit DefaultPacketWriterFactory(DatagramClientSocket* socket)
92 : socket_(socket) {}
93 ~DefaultPacketWriterFactory() override {}
95 QuicPacketWriter* Create(QuicConnection* connection) const override;
97 private:
98 DatagramClientSocket* socket_;
101 QuicPacketWriter* DefaultPacketWriterFactory::Create(
102 QuicConnection* connection) const {
103 scoped_ptr<QuicDefaultPacketWriter> writer(
104 new QuicDefaultPacketWriter(socket_));
105 writer->SetConnection(connection);
106 return writer.release();
109 } // namespace
111 QuicStreamFactory::IpAliasKey::IpAliasKey() {}
113 QuicStreamFactory::IpAliasKey::IpAliasKey(IPEndPoint ip_endpoint,
114 bool is_https)
115 : ip_endpoint(ip_endpoint),
116 is_https(is_https) {}
118 QuicStreamFactory::IpAliasKey::~IpAliasKey() {}
120 bool QuicStreamFactory::IpAliasKey::operator<(
121 const QuicStreamFactory::IpAliasKey& other) const {
122 if (!(ip_endpoint == other.ip_endpoint)) {
123 return ip_endpoint < other.ip_endpoint;
125 return is_https < other.is_https;
128 bool QuicStreamFactory::IpAliasKey::operator==(
129 const QuicStreamFactory::IpAliasKey& other) const {
130 return is_https == other.is_https &&
131 ip_endpoint == other.ip_endpoint;
134 // Responsible for creating a new QUIC session to the specified server, and
135 // for notifying any associated requests when complete.
136 class QuicStreamFactory::Job {
137 public:
138 Job(QuicStreamFactory* factory,
139 HostResolver* host_resolver,
140 const HostPortPair& host_port_pair,
141 bool is_https,
142 bool was_alternate_protocol_recently_broken,
143 PrivacyMode privacy_mode,
144 bool is_post,
145 QuicServerInfo* server_info,
146 const BoundNetLog& net_log);
148 // Creates a new job to handle the resumption of for connecting an
149 // existing session.
150 Job(QuicStreamFactory* factory,
151 HostResolver* host_resolver,
152 QuicClientSession* session,
153 QuicServerId server_id);
155 ~Job();
157 int Run(const CompletionCallback& callback);
159 int DoLoop(int rv);
160 int DoResolveHost();
161 int DoResolveHostComplete(int rv);
162 int DoLoadServerInfo();
163 int DoLoadServerInfoComplete(int rv);
164 int DoConnect();
165 int DoResumeConnect();
166 int DoConnectComplete(int rv);
168 void OnIOComplete(int rv);
170 void RunAuxilaryJob();
172 void Cancel();
174 void CancelWaitForDataReadyCallback();
176 const QuicServerId server_id() const { return server_id_; }
178 base::WeakPtr<Job> GetWeakPtr() { return weak_factory_.GetWeakPtr(); }
180 private:
181 enum IoState {
182 STATE_NONE,
183 STATE_RESOLVE_HOST,
184 STATE_RESOLVE_HOST_COMPLETE,
185 STATE_LOAD_SERVER_INFO,
186 STATE_LOAD_SERVER_INFO_COMPLETE,
187 STATE_CONNECT,
188 STATE_RESUME_CONNECT,
189 STATE_CONNECT_COMPLETE,
191 IoState io_state_;
193 QuicStreamFactory* factory_;
194 SingleRequestHostResolver host_resolver_;
195 QuicServerId server_id_;
196 bool is_post_;
197 bool was_alternate_protocol_recently_broken_;
198 scoped_ptr<QuicServerInfo> server_info_;
199 bool started_another_job_;
200 const BoundNetLog net_log_;
201 QuicClientSession* session_;
202 CompletionCallback callback_;
203 AddressList address_list_;
204 base::TimeTicks dns_resolution_start_time_;
205 base::TimeTicks dns_resolution_end_time_;
206 base::WeakPtrFactory<Job> weak_factory_;
207 DISALLOW_COPY_AND_ASSIGN(Job);
210 QuicStreamFactory::Job::Job(QuicStreamFactory* factory,
211 HostResolver* host_resolver,
212 const HostPortPair& host_port_pair,
213 bool is_https,
214 bool was_alternate_protocol_recently_broken,
215 PrivacyMode privacy_mode,
216 bool is_post,
217 QuicServerInfo* server_info,
218 const BoundNetLog& net_log)
219 : io_state_(STATE_RESOLVE_HOST),
220 factory_(factory),
221 host_resolver_(host_resolver),
222 server_id_(host_port_pair, is_https, privacy_mode),
223 is_post_(is_post),
224 was_alternate_protocol_recently_broken_(
225 was_alternate_protocol_recently_broken),
226 server_info_(server_info),
227 started_another_job_(false),
228 net_log_(net_log),
229 session_(nullptr),
230 weak_factory_(this) {
233 QuicStreamFactory::Job::Job(QuicStreamFactory* factory,
234 HostResolver* host_resolver,
235 QuicClientSession* session,
236 QuicServerId server_id)
237 : io_state_(STATE_RESUME_CONNECT),
238 factory_(factory),
239 host_resolver_(host_resolver), // unused
240 server_id_(server_id),
241 is_post_(false), // unused
242 was_alternate_protocol_recently_broken_(false), // unused
243 started_another_job_(false), // unused
244 net_log_(session->net_log()), // unused
245 session_(session),
246 weak_factory_(this) {
249 QuicStreamFactory::Job::~Job() {
250 // If disk cache has a pending WaitForDataReadyCallback, cancel that callback.
251 if (server_info_)
252 server_info_->ResetWaitForDataReadyCallback();
255 int QuicStreamFactory::Job::Run(const CompletionCallback& callback) {
256 int rv = DoLoop(OK);
257 if (rv == ERR_IO_PENDING)
258 callback_ = callback;
260 return rv > 0 ? OK : rv;
263 int QuicStreamFactory::Job::DoLoop(int rv) {
264 do {
265 IoState state = io_state_;
266 io_state_ = STATE_NONE;
267 switch (state) {
268 case STATE_RESOLVE_HOST:
269 CHECK_EQ(OK, rv);
270 rv = DoResolveHost();
271 break;
272 case STATE_RESOLVE_HOST_COMPLETE:
273 rv = DoResolveHostComplete(rv);
274 break;
275 case STATE_LOAD_SERVER_INFO:
276 CHECK_EQ(OK, rv);
277 rv = DoLoadServerInfo();
278 break;
279 case STATE_LOAD_SERVER_INFO_COMPLETE:
280 rv = DoLoadServerInfoComplete(rv);
281 break;
282 case STATE_CONNECT:
283 CHECK_EQ(OK, rv);
284 rv = DoConnect();
285 break;
286 case STATE_RESUME_CONNECT:
287 CHECK_EQ(OK, rv);
288 rv = DoResumeConnect();
289 break;
290 case STATE_CONNECT_COMPLETE:
291 rv = DoConnectComplete(rv);
292 break;
293 default:
294 NOTREACHED() << "io_state_: " << io_state_;
295 break;
297 } while (io_state_ != STATE_NONE && rv != ERR_IO_PENDING);
298 return rv;
301 void QuicStreamFactory::Job::OnIOComplete(int rv) {
302 rv = DoLoop(rv);
303 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
304 callback_.Run(rv);
308 void QuicStreamFactory::Job::RunAuxilaryJob() {
309 int rv = Run(base::Bind(&QuicStreamFactory::OnJobComplete,
310 base::Unretained(factory_), this));
311 if (rv != ERR_IO_PENDING)
312 factory_->OnJobComplete(this, rv);
315 void QuicStreamFactory::Job::Cancel() {
316 callback_.Reset();
317 if (session_)
318 session_->connection()->SendConnectionClose(QUIC_CONNECTION_CANCELLED);
321 void QuicStreamFactory::Job::CancelWaitForDataReadyCallback() {
322 // If we are waiting for WaitForDataReadyCallback, then cancel the callback.
323 if (io_state_ != STATE_LOAD_SERVER_INFO_COMPLETE)
324 return;
325 server_info_->CancelWaitForDataReadyCallback();
326 OnIOComplete(OK);
329 int QuicStreamFactory::Job::DoResolveHost() {
330 // Start loading the data now, and wait for it after we resolve the host.
331 if (server_info_) {
332 server_info_->Start();
335 io_state_ = STATE_RESOLVE_HOST_COMPLETE;
336 dns_resolution_start_time_ = base::TimeTicks::Now();
337 return host_resolver_.Resolve(
338 HostResolver::RequestInfo(server_id_.host_port_pair()), DEFAULT_PRIORITY,
339 &address_list_,
340 base::Bind(&QuicStreamFactory::Job::OnIOComplete, GetWeakPtr()),
341 net_log_);
344 int QuicStreamFactory::Job::DoResolveHostComplete(int rv) {
345 dns_resolution_end_time_ = base::TimeTicks::Now();
346 UMA_HISTOGRAM_TIMES("Net.QuicSession.HostResolutionTime",
347 dns_resolution_end_time_ - dns_resolution_start_time_);
348 if (rv != OK)
349 return rv;
351 DCHECK(!factory_->HasActiveSession(server_id_));
353 // Inform the factory of this resolution, which will set up
354 // a session alias, if possible.
355 if (factory_->OnResolution(server_id_, address_list_)) {
356 return OK;
359 if (server_info_)
360 io_state_ = STATE_LOAD_SERVER_INFO;
361 else
362 io_state_ = STATE_CONNECT;
363 return OK;
366 int QuicStreamFactory::Job::DoLoadServerInfo() {
367 io_state_ = STATE_LOAD_SERVER_INFO_COMPLETE;
369 DCHECK(server_info_);
371 // To mitigate the effects of disk cache taking too long to load QUIC server
372 // information, set up a timer to cancel WaitForDataReady's callback.
373 if (factory_->load_server_info_timeout_srtt_multiplier_ > 0) {
374 int64 load_server_info_timeout_ms =
375 (factory_->load_server_info_timeout_srtt_multiplier_ *
376 factory_->GetServerNetworkStatsSmoothedRttInMicroseconds(server_id_)) /
377 1000;
378 if (load_server_info_timeout_ms > 0) {
379 factory_->task_runner_->PostDelayedTask(
380 FROM_HERE,
381 base::Bind(&QuicStreamFactory::Job::CancelWaitForDataReadyCallback,
382 GetWeakPtr()),
383 base::TimeDelta::FromMilliseconds(load_server_info_timeout_ms));
387 int rv = server_info_->WaitForDataReady(
388 base::Bind(&QuicStreamFactory::Job::OnIOComplete, GetWeakPtr()));
389 if (rv == ERR_IO_PENDING && factory_->enable_connection_racing()) {
390 // If we are waiting to load server config from the disk cache, then start
391 // another job.
392 started_another_job_ = true;
393 factory_->CreateAuxilaryJob(server_id_, is_post_, net_log_);
395 return rv;
398 int QuicStreamFactory::Job::DoLoadServerInfoComplete(int rv) {
399 UMA_HISTOGRAM_TIMES("Net.QuicServerInfo.DiskCacheWaitForDataReadyTime",
400 base::TimeTicks::Now() - dns_resolution_end_time_);
402 if (rv != OK)
403 server_info_.reset();
405 if (started_another_job_ &&
406 (!server_info_ || server_info_->state().server_config.empty() ||
407 !factory_->CryptoConfigCacheIsEmpty(server_id_))) {
408 // If we have started another job and if we didn't load the server config
409 // from the disk cache or if we have received a new server config from the
410 // server, then cancel the current job.
411 io_state_ = STATE_NONE;
412 return ERR_CONNECTION_CLOSED;
415 io_state_ = STATE_CONNECT;
416 return OK;
419 int QuicStreamFactory::Job::DoConnect() {
420 io_state_ = STATE_CONNECT_COMPLETE;
422 int rv =
423 factory_->CreateSession(server_id_, server_info_.Pass(), address_list_,
424 dns_resolution_end_time_, net_log_, &session_);
425 if (rv != OK) {
426 DCHECK(rv != ERR_IO_PENDING);
427 DCHECK(!session_);
428 return rv;
431 if (!session_->connection()->connected()) {
432 return ERR_CONNECTION_CLOSED;
435 session_->StartReading();
436 if (!session_->connection()->connected()) {
437 return ERR_QUIC_PROTOCOL_ERROR;
439 bool require_confirmation =
440 factory_->require_confirmation() || is_post_ ||
441 was_alternate_protocol_recently_broken_;
443 rv = session_->CryptoConnect(
444 require_confirmation,
445 base::Bind(&QuicStreamFactory::Job::OnIOComplete, GetWeakPtr()));
446 return rv;
449 int QuicStreamFactory::Job::DoResumeConnect() {
450 io_state_ = STATE_CONNECT_COMPLETE;
452 int rv = session_->ResumeCryptoConnect(
453 base::Bind(&QuicStreamFactory::Job::OnIOComplete, GetWeakPtr()));
455 return rv;
458 int QuicStreamFactory::Job::DoConnectComplete(int rv) {
459 if (rv != OK)
460 return rv;
462 DCHECK(!factory_->HasActiveSession(server_id_));
463 // There may well now be an active session for this IP. If so, use the
464 // existing session instead.
465 AddressList address(session_->connection()->peer_address());
466 if (factory_->OnResolution(server_id_, address)) {
467 session_->connection()->SendConnectionClose(QUIC_CONNECTION_IP_POOLED);
468 session_ = nullptr;
469 return OK;
472 factory_->ActivateSession(server_id_, session_);
474 return OK;
477 QuicStreamRequest::QuicStreamRequest(QuicStreamFactory* factory)
478 : factory_(factory) {}
480 QuicStreamRequest::~QuicStreamRequest() {
481 if (factory_ && !callback_.is_null())
482 factory_->CancelRequest(this);
485 int QuicStreamRequest::Request(const HostPortPair& host_port_pair,
486 bool is_https,
487 PrivacyMode privacy_mode,
488 base::StringPiece method,
489 const BoundNetLog& net_log,
490 const CompletionCallback& callback) {
491 DCHECK(!stream_);
492 DCHECK(callback_.is_null());
493 DCHECK(factory_);
494 int rv = factory_->Create(host_port_pair, is_https, privacy_mode, method,
495 net_log, this);
496 if (rv == ERR_IO_PENDING) {
497 host_port_pair_ = host_port_pair;
498 net_log_ = net_log;
499 callback_ = callback;
500 } else {
501 factory_ = nullptr;
503 if (rv == OK)
504 DCHECK(stream_);
505 return rv;
508 void QuicStreamRequest::set_stream(scoped_ptr<QuicHttpStream> stream) {
509 DCHECK(stream);
510 stream_ = stream.Pass();
513 void QuicStreamRequest::OnRequestComplete(int rv) {
514 factory_ = nullptr;
515 callback_.Run(rv);
518 scoped_ptr<QuicHttpStream> QuicStreamRequest::ReleaseStream() {
519 DCHECK(stream_);
520 return stream_.Pass();
523 QuicStreamFactory::QuicStreamFactory(
524 HostResolver* host_resolver,
525 ClientSocketFactory* client_socket_factory,
526 base::WeakPtr<HttpServerProperties> http_server_properties,
527 CertVerifier* cert_verifier,
528 ChannelIDService* channel_id_service,
529 TransportSecurityState* transport_security_state,
530 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory,
531 QuicRandom* random_generator,
532 QuicClock* clock,
533 size_t max_packet_length,
534 const std::string& user_agent_id,
535 const QuicVersionVector& supported_versions,
536 bool enable_port_selection,
537 bool always_require_handshake_confirmation,
538 bool disable_connection_pooling,
539 float load_server_info_timeout_srtt_multiplier,
540 bool enable_connection_racing,
541 bool enable_non_blocking_io,
542 bool disable_disk_cache,
543 int max_number_of_lossy_connections,
544 float packet_loss_threshold,
545 int socket_receive_buffer_size,
546 const QuicTagVector& connection_options)
547 : require_confirmation_(true),
548 host_resolver_(host_resolver),
549 client_socket_factory_(client_socket_factory),
550 http_server_properties_(http_server_properties),
551 transport_security_state_(transport_security_state),
552 quic_server_info_factory_(nullptr),
553 quic_crypto_client_stream_factory_(quic_crypto_client_stream_factory),
554 random_generator_(random_generator),
555 clock_(clock),
556 max_packet_length_(max_packet_length),
557 config_(InitializeQuicConfig(connection_options)),
558 supported_versions_(supported_versions),
559 enable_port_selection_(enable_port_selection),
560 always_require_handshake_confirmation_(
561 always_require_handshake_confirmation),
562 disable_connection_pooling_(disable_connection_pooling),
563 load_server_info_timeout_srtt_multiplier_(
564 load_server_info_timeout_srtt_multiplier),
565 enable_connection_racing_(enable_connection_racing),
566 enable_non_blocking_io_(enable_non_blocking_io),
567 disable_disk_cache_(disable_disk_cache),
568 max_number_of_lossy_connections_(max_number_of_lossy_connections),
569 packet_loss_threshold_(packet_loss_threshold),
570 socket_receive_buffer_size_(socket_receive_buffer_size),
571 port_seed_(random_generator_->RandUint64()),
572 check_persisted_supports_quic_(true),
573 task_runner_(nullptr),
574 weak_factory_(this) {
575 DCHECK(transport_security_state_);
576 crypto_config_.set_user_agent_id(user_agent_id);
577 crypto_config_.AddCanonicalSuffix(".c.youtube.com");
578 crypto_config_.AddCanonicalSuffix(".googlevideo.com");
579 crypto_config_.SetProofVerifier(
580 new ProofVerifierChromium(cert_verifier, transport_security_state));
581 crypto_config_.SetChannelIDSource(
582 new ChannelIDSourceChromium(channel_id_service));
583 base::CPU cpu;
584 if (cpu.has_aesni() && cpu.has_avx())
585 crypto_config_.PreferAesGcm();
586 if (!IsEcdsaSupported())
587 crypto_config_.DisableEcdsa();
590 QuicStreamFactory::~QuicStreamFactory() {
591 CloseAllSessions(ERR_ABORTED);
592 while (!all_sessions_.empty()) {
593 delete all_sessions_.begin()->first;
594 all_sessions_.erase(all_sessions_.begin());
596 while (!active_jobs_.empty()) {
597 const QuicServerId server_id = active_jobs_.begin()->first;
598 STLDeleteElements(&(active_jobs_[server_id]));
599 active_jobs_.erase(server_id);
603 void QuicStreamFactory::set_require_confirmation(bool require_confirmation) {
604 require_confirmation_ = require_confirmation;
605 if (http_server_properties_ && (!(local_address_ == IPEndPoint()))) {
606 http_server_properties_->SetSupportsQuic(!require_confirmation,
607 local_address_.address());
611 int QuicStreamFactory::Create(const HostPortPair& host_port_pair,
612 bool is_https,
613 PrivacyMode privacy_mode,
614 base::StringPiece method,
615 const BoundNetLog& net_log,
616 QuicStreamRequest* request) {
617 QuicServerId server_id(host_port_pair, is_https, privacy_mode);
618 if (HasActiveSession(server_id)) {
619 request->set_stream(CreateIfSessionExists(server_id, net_log));
620 return OK;
623 if (HasActiveJob(server_id)) {
624 active_requests_[request] = server_id;
625 job_requests_map_[server_id].insert(request);
626 return ERR_IO_PENDING;
629 // TODO(rtenneti): |task_runner_| is used by the Job. Initialize task_runner_
630 // in the constructor after WebRequestActionWithThreadsTest.* tests are fixed.
631 if (!task_runner_)
632 task_runner_ = base::MessageLoop::current()->message_loop_proxy().get();
634 QuicServerInfo* quic_server_info = nullptr;
635 if (quic_server_info_factory_) {
636 bool load_from_disk_cache = !disable_disk_cache_;
637 if (http_server_properties_) {
638 const AlternativeServiceMap& alternative_service_map =
639 http_server_properties_->alternative_service_map();
640 AlternativeServiceMap::const_iterator it =
641 alternative_service_map.Peek(server_id.host_port_pair());
642 if (it == alternative_service_map.end() ||
643 it->second.alternative_service.protocol != QUIC) {
644 // If there is no entry for QUIC, consider that as a new server and
645 // don't wait for Cache thread to load the data for that server.
646 load_from_disk_cache = false;
649 if (load_from_disk_cache && CryptoConfigCacheIsEmpty(server_id)) {
650 quic_server_info = quic_server_info_factory_->GetForServer(server_id);
654 scoped_ptr<Job> job(new Job(this, host_resolver_, host_port_pair, is_https,
655 WasQuicRecentlyBroken(server_id), privacy_mode,
656 method == "POST" /* is_post */, quic_server_info,
657 net_log));
658 int rv = job->Run(base::Bind(&QuicStreamFactory::OnJobComplete,
659 base::Unretained(this), job.get()));
660 if (rv == ERR_IO_PENDING) {
661 active_requests_[request] = server_id;
662 job_requests_map_[server_id].insert(request);
663 active_jobs_[server_id].insert(job.release());
664 return rv;
666 if (rv == OK) {
667 DCHECK(HasActiveSession(server_id));
668 request->set_stream(CreateIfSessionExists(server_id, net_log));
670 return rv;
673 void QuicStreamFactory::CreateAuxilaryJob(const QuicServerId server_id,
674 bool is_post,
675 const BoundNetLog& net_log) {
676 Job* aux_job = new Job(this, host_resolver_, server_id.host_port_pair(),
677 server_id.is_https(), WasQuicRecentlyBroken(server_id),
678 server_id.privacy_mode(), is_post, nullptr, net_log);
679 active_jobs_[server_id].insert(aux_job);
680 task_runner_->PostTask(FROM_HERE,
681 base::Bind(&QuicStreamFactory::Job::RunAuxilaryJob,
682 aux_job->GetWeakPtr()));
685 bool QuicStreamFactory::OnResolution(
686 const QuicServerId& server_id,
687 const AddressList& address_list) {
688 DCHECK(!HasActiveSession(server_id));
689 if (disable_connection_pooling_) {
690 return false;
692 for (const IPEndPoint& address : address_list) {
693 const IpAliasKey ip_alias_key(address, server_id.is_https());
694 if (!ContainsKey(ip_aliases_, ip_alias_key))
695 continue;
697 const SessionSet& sessions = ip_aliases_[ip_alias_key];
698 for (QuicClientSession* session : sessions) {
699 if (!session->CanPool(server_id.host(), server_id.privacy_mode()))
700 continue;
701 active_sessions_[server_id] = session;
702 session_aliases_[session].insert(server_id);
703 return true;
706 return false;
709 void QuicStreamFactory::OnJobComplete(Job* job, int rv) {
710 QuicServerId server_id = job->server_id();
711 if (rv != OK) {
712 JobSet* jobs = &(active_jobs_[server_id]);
713 if (jobs->size() > 1) {
714 // If there is another pending job, then we can delete this job and let
715 // the other job handle the request.
716 job->Cancel();
717 jobs->erase(job);
718 delete job;
719 return;
723 if (rv == OK) {
724 if (!always_require_handshake_confirmation_)
725 set_require_confirmation(false);
727 // Create all the streams, but do not notify them yet.
728 for (QuicStreamRequest* request : job_requests_map_[server_id]) {
729 DCHECK(HasActiveSession(server_id));
730 request->set_stream(CreateIfSessionExists(server_id, request->net_log()));
734 while (!job_requests_map_[server_id].empty()) {
735 RequestSet::iterator it = job_requests_map_[server_id].begin();
736 QuicStreamRequest* request = *it;
737 job_requests_map_[server_id].erase(it);
738 active_requests_.erase(request);
739 // Even though we're invoking callbacks here, we don't need to worry
740 // about |this| being deleted, because the factory is owned by the
741 // profile which can not be deleted via callbacks.
742 request->OnRequestComplete(rv);
745 for (Job* other_job : active_jobs_[server_id]) {
746 if (other_job != job)
747 other_job->Cancel();
750 STLDeleteElements(&(active_jobs_[server_id]));
751 active_jobs_.erase(server_id);
752 job_requests_map_.erase(server_id);
755 // Returns a newly created QuicHttpStream owned by the caller, if a
756 // matching session already exists. Returns nullptr otherwise.
757 scoped_ptr<QuicHttpStream> QuicStreamFactory::CreateIfSessionExists(
758 const QuicServerId& server_id,
759 const BoundNetLog& net_log) {
760 if (!HasActiveSession(server_id)) {
761 DVLOG(1) << "No active session";
762 return scoped_ptr<QuicHttpStream>();
765 QuicClientSession* session = active_sessions_[server_id];
766 DCHECK(session);
767 return scoped_ptr<QuicHttpStream>(
768 new QuicHttpStream(session->GetWeakPtr()));
771 bool QuicStreamFactory::IsQuicDisabled(uint16 port) {
772 return max_number_of_lossy_connections_ > 0 &&
773 number_of_lossy_connections_[port] >= max_number_of_lossy_connections_;
776 bool QuicStreamFactory::OnHandshakeConfirmed(QuicClientSession* session,
777 float packet_loss_rate) {
778 DCHECK(session);
779 uint16 port = session->server_id().port();
780 if (packet_loss_rate < packet_loss_threshold_) {
781 number_of_lossy_connections_[port] = 0;
782 return false;
785 if (http_server_properties_) {
786 // We mark it as recently broken, which means that 0-RTT will be disabled
787 // but we'll still race.
788 http_server_properties_->MarkAlternativeServiceRecentlyBroken(
789 AlternativeService(QUIC, session->server_id().host(), port));
792 // We abandon the connection if packet loss rate is too bad.
793 session->CloseSessionOnErrorAndNotifyFactoryLater(ERR_ABORTED,
794 QUIC_BAD_PACKET_LOSS_RATE);
796 if (IsQuicDisabled(port))
797 return true; // Exit if Quic is already disabled for this port.
799 if (++number_of_lossy_connections_[port] >=
800 max_number_of_lossy_connections_) {
801 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicStreamFactory.QuicIsDisabled", port);
804 return true;
807 void QuicStreamFactory::OnIdleSession(QuicClientSession* session) {
810 void QuicStreamFactory::OnSessionGoingAway(QuicClientSession* session) {
811 const AliasSet& aliases = session_aliases_[session];
812 for (AliasSet::const_iterator it = aliases.begin(); it != aliases.end();
813 ++it) {
814 DCHECK(active_sessions_.count(*it));
815 DCHECK_EQ(session, active_sessions_[*it]);
816 // Track sessions which have recently gone away so that we can disable
817 // port suggestions.
818 if (session->goaway_received()) {
819 gone_away_aliases_.insert(*it);
822 active_sessions_.erase(*it);
823 ProcessGoingAwaySession(session, *it, true);
825 ProcessGoingAwaySession(session, all_sessions_[session], false);
826 if (!aliases.empty()) {
827 const IpAliasKey ip_alias_key(session->connection()->peer_address(),
828 aliases.begin()->is_https());
829 ip_aliases_[ip_alias_key].erase(session);
830 if (ip_aliases_[ip_alias_key].empty()) {
831 ip_aliases_.erase(ip_alias_key);
834 session_aliases_.erase(session);
837 void QuicStreamFactory::OnSessionClosed(QuicClientSession* session) {
838 DCHECK_EQ(0u, session->GetNumOpenStreams());
839 OnSessionGoingAway(session);
840 delete session;
841 all_sessions_.erase(session);
844 void QuicStreamFactory::OnSessionConnectTimeout(
845 QuicClientSession* session) {
846 const AliasSet& aliases = session_aliases_[session];
847 for (AliasSet::const_iterator it = aliases.begin(); it != aliases.end();
848 ++it) {
849 DCHECK(active_sessions_.count(*it));
850 DCHECK_EQ(session, active_sessions_[*it]);
851 active_sessions_.erase(*it);
854 if (aliases.empty()) {
855 return;
858 const IpAliasKey ip_alias_key(session->connection()->peer_address(),
859 aliases.begin()->is_https());
860 ip_aliases_[ip_alias_key].erase(session);
861 if (ip_aliases_[ip_alias_key].empty()) {
862 ip_aliases_.erase(ip_alias_key);
864 QuicServerId server_id = *aliases.begin();
865 session_aliases_.erase(session);
866 Job* job = new Job(this, host_resolver_, session, server_id);
867 active_jobs_[server_id].insert(job);
868 int rv = job->Run(base::Bind(&QuicStreamFactory::OnJobComplete,
869 base::Unretained(this), job));
870 DCHECK_EQ(ERR_IO_PENDING, rv);
873 void QuicStreamFactory::CancelRequest(QuicStreamRequest* request) {
874 DCHECK(ContainsKey(active_requests_, request));
875 QuicServerId server_id = active_requests_[request];
876 job_requests_map_[server_id].erase(request);
877 active_requests_.erase(request);
880 void QuicStreamFactory::CloseAllSessions(int error) {
881 while (!active_sessions_.empty()) {
882 size_t initial_size = active_sessions_.size();
883 active_sessions_.begin()->second->CloseSessionOnError(error,
884 QUIC_INTERNAL_ERROR);
885 DCHECK_NE(initial_size, active_sessions_.size());
887 while (!all_sessions_.empty()) {
888 size_t initial_size = all_sessions_.size();
889 all_sessions_.begin()->first->CloseSessionOnError(error,
890 QUIC_INTERNAL_ERROR);
891 DCHECK_NE(initial_size, all_sessions_.size());
893 DCHECK(all_sessions_.empty());
896 base::Value* QuicStreamFactory::QuicStreamFactoryInfoToValue() const {
897 base::ListValue* list = new base::ListValue();
899 for (SessionMap::const_iterator it = active_sessions_.begin();
900 it != active_sessions_.end(); ++it) {
901 const QuicServerId& server_id = it->first;
902 QuicClientSession* session = it->second;
903 const AliasSet& aliases = session_aliases_.find(session)->second;
904 // Only add a session to the list once.
905 if (server_id == *aliases.begin()) {
906 std::set<HostPortPair> hosts;
907 for (AliasSet::const_iterator alias_it = aliases.begin();
908 alias_it != aliases.end(); ++alias_it) {
909 hosts.insert(alias_it->host_port_pair());
911 list->Append(session->GetInfoAsValue(hosts));
914 return list;
917 void QuicStreamFactory::ClearCachedStatesInCryptoConfig() {
918 crypto_config_.ClearCachedStates();
921 void QuicStreamFactory::OnIPAddressChanged() {
922 CloseAllSessions(ERR_NETWORK_CHANGED);
923 set_require_confirmation(true);
926 void QuicStreamFactory::OnCertAdded(const X509Certificate* cert) {
927 CloseAllSessions(ERR_CERT_DATABASE_CHANGED);
930 void QuicStreamFactory::OnCACertChanged(const X509Certificate* cert) {
931 // We should flush the sessions if we removed trust from a
932 // cert, because a previously trusted server may have become
933 // untrusted.
935 // We should not flush the sessions if we added trust to a cert.
937 // Since the OnCACertChanged method doesn't tell us what
938 // kind of change it is, we have to flush the socket
939 // pools to be safe.
940 CloseAllSessions(ERR_CERT_DATABASE_CHANGED);
943 bool QuicStreamFactory::HasActiveSession(
944 const QuicServerId& server_id) const {
945 return ContainsKey(active_sessions_, server_id);
948 bool QuicStreamFactory::HasActiveJob(const QuicServerId& key) const {
949 return ContainsKey(active_jobs_, key);
952 int QuicStreamFactory::CreateSession(const QuicServerId& server_id,
953 scoped_ptr<QuicServerInfo> server_info,
954 const AddressList& address_list,
955 base::TimeTicks dns_resolution_end_time,
956 const BoundNetLog& net_log,
957 QuicClientSession** session) {
958 bool enable_port_selection = enable_port_selection_;
959 if (enable_port_selection &&
960 ContainsKey(gone_away_aliases_, server_id)) {
961 // Disable port selection when the server is going away.
962 // There is no point in trying to return to the same server, if
963 // that server is no longer handling requests.
964 enable_port_selection = false;
965 gone_away_aliases_.erase(server_id);
968 QuicConnectionId connection_id = random_generator_->RandUint64();
969 IPEndPoint addr = *address_list.begin();
970 scoped_refptr<PortSuggester> port_suggester =
971 new PortSuggester(server_id.host_port_pair(), port_seed_);
972 DatagramSocket::BindType bind_type = enable_port_selection ?
973 DatagramSocket::RANDOM_BIND : // Use our callback.
974 DatagramSocket::DEFAULT_BIND; // Use OS to randomize.
975 scoped_ptr<DatagramClientSocket> socket(
976 client_socket_factory_->CreateDatagramClientSocket(
977 bind_type,
978 base::Bind(&PortSuggester::SuggestPort, port_suggester),
979 net_log.net_log(), net_log.source()));
981 if (enable_non_blocking_io_ &&
982 client_socket_factory_ == ClientSocketFactory::GetDefaultFactory()) {
983 #if defined(OS_WIN)
984 static_cast<UDPClientSocket*>(socket.get())->UseNonBlockingIO();
985 #endif
988 int rv = socket->Connect(addr);
990 if (rv != OK) {
991 HistogramCreateSessionFailure(CREATION_ERROR_CONNECTING_SOCKET);
992 return rv;
994 UMA_HISTOGRAM_COUNTS("Net.QuicEphemeralPortsSuggested",
995 port_suggester->call_count());
996 if (enable_port_selection) {
997 DCHECK_LE(1u, port_suggester->call_count());
998 } else {
999 DCHECK_EQ(0u, port_suggester->call_count());
1002 rv = socket->SetReceiveBufferSize(socket_receive_buffer_size_);
1003 if (rv != OK) {
1004 HistogramCreateSessionFailure(CREATION_ERROR_SETTING_RECEIVE_BUFFER);
1005 return rv;
1007 // Set a buffer large enough to contain the initial CWND's worth of packet
1008 // to work around the problem with CHLO packets being sent out with the
1009 // wrong encryption level, when the send buffer is full.
1010 rv = socket->SetSendBufferSize(kMaxPacketSize * 20);
1011 if (rv != OK) {
1012 HistogramCreateSessionFailure(CREATION_ERROR_SETTING_SEND_BUFFER);
1013 return rv;
1016 socket->GetLocalAddress(&local_address_);
1017 if (check_persisted_supports_quic_ && http_server_properties_) {
1018 check_persisted_supports_quic_ = false;
1019 IPAddressNumber last_address;
1020 if (http_server_properties_->GetSupportsQuic(&last_address) &&
1021 last_address == local_address_.address()) {
1022 require_confirmation_ = false;
1026 DefaultPacketWriterFactory packet_writer_factory(socket.get());
1028 if (!helper_.get()) {
1029 helper_.reset(new QuicConnectionHelper(
1030 base::MessageLoop::current()->message_loop_proxy().get(),
1031 clock_.get(), random_generator_));
1034 QuicConnection* connection = new QuicConnection(
1035 connection_id, addr, helper_.get(), packet_writer_factory,
1036 true /* owns_writer */, Perspective::IS_CLIENT, server_id.is_https(),
1037 supported_versions_);
1038 connection->set_max_packet_length(max_packet_length_);
1040 InitializeCachedStateInCryptoConfig(server_id, server_info);
1042 QuicConfig config = config_;
1043 config.SetSocketReceiveBufferToSend(socket_receive_buffer_size_);
1044 config.set_max_undecryptable_packets(kMaxUndecryptablePackets);
1045 config.SetInitialStreamFlowControlWindowToSend(kInitialReceiveWindowSize);
1046 config.SetInitialSessionFlowControlWindowToSend(kInitialReceiveWindowSize);
1047 int64 srtt = GetServerNetworkStatsSmoothedRttInMicroseconds(server_id);
1048 if (srtt > 0)
1049 config.SetInitialRoundTripTimeUsToSend(static_cast<uint32>(srtt));
1050 config.SetBytesForConnectionIdToSend(0);
1052 if (quic_server_info_factory_ && !server_info) {
1053 // Start the disk cache loading so that we can persist the newer QUIC server
1054 // information and/or inform the disk cache that we have reused
1055 // |server_info|.
1056 server_info.reset(quic_server_info_factory_->GetForServer(server_id));
1057 server_info->Start();
1060 *session = new QuicClientSession(
1061 connection, socket.Pass(), this, transport_security_state_,
1062 server_info.Pass(), config, network_connection_.GetDescription(),
1063 dns_resolution_end_time,
1064 base::MessageLoop::current()->message_loop_proxy().get(),
1065 net_log.net_log());
1067 all_sessions_[*session] = server_id; // owning pointer
1069 (*session)->InitializeSession(server_id, &crypto_config_,
1070 quic_crypto_client_stream_factory_);
1071 bool closed_during_initialize =
1072 !ContainsKey(all_sessions_, *session) ||
1073 !(*session)->connection()->connected();
1074 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.ClosedDuringInitializeSession",
1075 closed_during_initialize);
1076 if (closed_during_initialize) {
1077 DLOG(DFATAL) << "Session closed during initialize";
1078 *session = nullptr;
1079 return ERR_CONNECTION_CLOSED;
1081 return OK;
1084 void QuicStreamFactory::ActivateSession(
1085 const QuicServerId& server_id,
1086 QuicClientSession* session) {
1087 DCHECK(!HasActiveSession(server_id));
1088 UMA_HISTOGRAM_COUNTS("Net.QuicActiveSessions", active_sessions_.size());
1089 active_sessions_[server_id] = session;
1090 session_aliases_[session].insert(server_id);
1091 const IpAliasKey ip_alias_key(session->connection()->peer_address(),
1092 server_id.is_https());
1093 DCHECK(!ContainsKey(ip_aliases_[ip_alias_key], session));
1094 ip_aliases_[ip_alias_key].insert(session);
1097 int64 QuicStreamFactory::GetServerNetworkStatsSmoothedRttInMicroseconds(
1098 const QuicServerId& server_id) const {
1099 if (!http_server_properties_)
1100 return 0;
1101 const ServerNetworkStats* stats =
1102 http_server_properties_->GetServerNetworkStats(
1103 server_id.host_port_pair());
1104 if (stats == nullptr)
1105 return 0;
1106 return stats->srtt.InMicroseconds();
1109 bool QuicStreamFactory::WasQuicRecentlyBroken(
1110 const QuicServerId& server_id) const {
1111 if (!http_server_properties_)
1112 return false;
1113 const AlternativeService alternative_service(QUIC,
1114 server_id.host_port_pair());
1115 return http_server_properties_->WasAlternativeServiceRecentlyBroken(
1116 alternative_service);
1119 bool QuicStreamFactory::CryptoConfigCacheIsEmpty(
1120 const QuicServerId& server_id) {
1121 QuicCryptoClientConfig::CachedState* cached =
1122 crypto_config_.LookupOrCreate(server_id);
1123 return cached->IsEmpty();
1126 void QuicStreamFactory::InitializeCachedStateInCryptoConfig(
1127 const QuicServerId& server_id,
1128 const scoped_ptr<QuicServerInfo>& server_info) {
1129 // |server_info| will be NULL, if a non-empty server config already exists in
1130 // the memory cache. This is a minor optimization to avoid LookupOrCreate.
1131 if (!server_info)
1132 return;
1134 QuicCryptoClientConfig::CachedState* cached =
1135 crypto_config_.LookupOrCreate(server_id);
1136 if (!cached->IsEmpty())
1137 return;
1139 if (http_server_properties_) {
1140 if (quic_supported_servers_at_startup_.empty()) {
1141 for (const std::pair<const HostPortPair, AlternativeServiceInfo>&
1142 key_value : http_server_properties_->alternative_service_map()) {
1143 if (key_value.second.alternative_service.protocol == QUIC) {
1144 quic_supported_servers_at_startup_.insert(key_value.first);
1149 // TODO(rtenneti): Delete the following histogram after collecting stats.
1150 // If the AlternativeServiceMap contained an entry for this host, check if
1151 // the disk cache contained an entry for it.
1152 if (ContainsKey(quic_supported_servers_at_startup_,
1153 server_id.host_port_pair())) {
1154 UMA_HISTOGRAM_BOOLEAN(
1155 "Net.QuicServerInfo.ExpectConfigMissingFromDiskCache",
1156 server_info->state().server_config.empty());
1160 if (!cached->Initialize(server_info->state().server_config,
1161 server_info->state().source_address_token,
1162 server_info->state().certs,
1163 server_info->state().server_config_sig,
1164 clock_->WallNow()))
1165 return;
1167 if (!server_id.is_https()) {
1168 // Don't check the certificates for insecure QUIC.
1169 cached->SetProofValid();
1173 void QuicStreamFactory::ProcessGoingAwaySession(
1174 QuicClientSession* session,
1175 const QuicServerId& server_id,
1176 bool session_was_active) {
1177 if (!http_server_properties_)
1178 return;
1180 const QuicConnectionStats& stats = session->connection()->GetStats();
1181 const AlternativeService alternative_service(QUIC,
1182 server_id.host_port_pair());
1183 if (session->IsCryptoHandshakeConfirmed()) {
1184 http_server_properties_->ConfirmAlternativeService(alternative_service);
1185 ServerNetworkStats network_stats;
1186 network_stats.srtt = base::TimeDelta::FromMicroseconds(stats.srtt_us);
1187 network_stats.bandwidth_estimate = stats.estimated_bandwidth;
1188 http_server_properties_->SetServerNetworkStats(server_id.host_port_pair(),
1189 network_stats);
1190 return;
1193 UMA_HISTOGRAM_COUNTS("Net.QuicHandshakeNotConfirmedNumPacketsReceived",
1194 stats.packets_received);
1196 if (!session_was_active)
1197 return;
1199 // TODO(rch): In the special case where the session has received no
1200 // packets from the peer, we should consider blacklisting this
1201 // differently so that we still race TCP but we don't consider the
1202 // session connected until the handshake has been confirmed.
1203 HistogramBrokenAlternateProtocolLocation(
1204 BROKEN_ALTERNATE_PROTOCOL_LOCATION_QUIC_STREAM_FACTORY);
1206 // Since the session was active, there's no longer an
1207 // HttpStreamFactoryImpl::Job running which can mark it broken, unless the TCP
1208 // job also fails. So to avoid not using QUIC when we otherwise could, we mark
1209 // it as recently broken, which means that 0-RTT will be disabled but we'll
1210 // still race.
1211 http_server_properties_->MarkAlternativeServiceRecentlyBroken(
1212 alternative_service);
1215 } // namespace net