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/socket/transport_client_socket_pool.h"
9 #include "base/compiler_specific.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/histogram_macros.h"
14 #include "base/profiler/scoped_tracker.h"
15 #include "base/strings/string_util.h"
16 #include "base/synchronization/lock.h"
17 #include "base/time/time.h"
18 #include "base/values.h"
19 #include "net/base/ip_endpoint.h"
20 #include "net/base/net_errors.h"
21 #include "net/log/net_log.h"
22 #include "net/socket/client_socket_factory.h"
23 #include "net/socket/client_socket_handle.h"
24 #include "net/socket/client_socket_pool_base.h"
25 #include "net/socket/socket_net_log_params.h"
26 #include "net/socket/tcp_client_socket.h"
28 using base::TimeDelta
;
32 // TODO(willchan): Base this off RTT instead of statically setting it. Note we
33 // choose a timeout that is different from the backup connect job timer so they
35 const int TransportConnectJobHelper::kIPv6FallbackTimerInMs
= 300;
39 // Returns true iff all addresses in |list| are in the IPv6 family.
40 bool AddressListOnlyContainsIPv6(const AddressList
& list
) {
41 DCHECK(!list
.empty());
42 for (AddressList::const_iterator iter
= list
.begin(); iter
!= list
.end();
44 if (iter
->GetFamily() != ADDRESS_FAMILY_IPV6
)
52 // This lock protects |g_last_connect_time|.
53 static base::LazyInstance
<base::Lock
>::Leaky
54 g_last_connect_time_lock
= LAZY_INSTANCE_INITIALIZER
;
56 // |g_last_connect_time| has the last time a connect() call is made.
57 static base::LazyInstance
<base::TimeTicks
>::Leaky
58 g_last_connect_time
= LAZY_INSTANCE_INITIALIZER
;
60 TransportSocketParams::TransportSocketParams(
61 const HostPortPair
& host_port_pair
,
62 bool disable_resolver_cache
,
64 const OnHostResolutionCallback
& host_resolution_callback
,
65 CombineConnectAndWritePolicy combine_connect_and_write_if_supported
)
66 : destination_(host_port_pair
),
67 ignore_limits_(ignore_limits
),
68 host_resolution_callback_(host_resolution_callback
),
69 combine_connect_and_write_(combine_connect_and_write_if_supported
) {
70 if (disable_resolver_cache
)
71 destination_
.set_allow_cached_response(false);
72 // combine_connect_and_write currently translates to TCP FastOpen.
73 // Enable TCP FastOpen if user wants it.
74 if (combine_connect_and_write_
== COMBINE_CONNECT_AND_WRITE_DEFAULT
) {
75 IsTCPFastOpenUserEnabled() ? combine_connect_and_write_
=
76 COMBINE_CONNECT_AND_WRITE_DESIRED
:
77 COMBINE_CONNECT_AND_WRITE_PROHIBITED
;
81 TransportSocketParams::~TransportSocketParams() {}
83 // TransportConnectJobs will time out after this many seconds. Note this is
84 // the total time, including both host resolution and TCP connect() times.
86 // TODO(eroman): The use of this constant needs to be re-evaluated. The time
87 // needed for TCPClientSocketXXX::Connect() can be arbitrarily long, since
88 // the address list may contain many alternatives, and most of those may
89 // timeout. Even worse, the per-connect timeout threshold varies greatly
90 // between systems (anywhere from 20 seconds to 190 seconds).
91 // See comment #12 at http://crbug.com/23364 for specifics.
92 static const int kTransportConnectJobTimeoutInSeconds
= 240; // 4 minutes.
94 TransportConnectJobHelper::TransportConnectJobHelper(
95 const scoped_refptr
<TransportSocketParams
>& params
,
96 ClientSocketFactory
* client_socket_factory
,
97 HostResolver
* host_resolver
,
98 LoadTimingInfo::ConnectTiming
* connect_timing
)
100 client_socket_factory_(client_socket_factory
),
101 resolver_(host_resolver
),
102 next_state_(STATE_NONE
),
103 connect_timing_(connect_timing
) {}
105 TransportConnectJobHelper::~TransportConnectJobHelper() {}
107 int TransportConnectJobHelper::DoResolveHost(RequestPriority priority
,
108 const BoundNetLog
& net_log
) {
109 next_state_
= STATE_RESOLVE_HOST_COMPLETE
;
110 connect_timing_
->dns_start
= base::TimeTicks::Now();
112 return resolver_
.Resolve(
113 params_
->destination(), priority
, &addresses_
, on_io_complete_
, net_log
);
116 int TransportConnectJobHelper::DoResolveHostComplete(
118 const BoundNetLog
& net_log
) {
119 connect_timing_
->dns_end
= base::TimeTicks::Now();
120 // Overwrite connection start time, since for connections that do not go
121 // through proxies, |connect_start| should not include dns lookup time.
122 connect_timing_
->connect_start
= connect_timing_
->dns_end
;
125 // Invoke callback, and abort if it fails.
126 if (!params_
->host_resolution_callback().is_null())
127 result
= params_
->host_resolution_callback().Run(addresses_
, net_log
);
130 next_state_
= STATE_TRANSPORT_CONNECT
;
135 base::TimeDelta
TransportConnectJobHelper::HistogramDuration(
136 ConnectionLatencyHistogram race_result
) {
137 DCHECK(!connect_timing_
->connect_start
.is_null());
138 DCHECK(!connect_timing_
->dns_start
.is_null());
139 base::TimeTicks now
= base::TimeTicks::Now();
140 base::TimeDelta total_duration
= now
- connect_timing_
->dns_start
;
141 UMA_HISTOGRAM_CUSTOM_TIMES("Net.DNS_Resolution_And_TCP_Connection_Latency2",
143 base::TimeDelta::FromMilliseconds(1),
144 base::TimeDelta::FromMinutes(10),
147 base::TimeDelta connect_duration
= now
- connect_timing_
->connect_start
;
148 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency",
150 base::TimeDelta::FromMilliseconds(1),
151 base::TimeDelta::FromMinutes(10),
154 switch (race_result
) {
155 case CONNECTION_LATENCY_IPV4_WINS_RACE
:
156 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv4_Wins_Race",
158 base::TimeDelta::FromMilliseconds(1),
159 base::TimeDelta::FromMinutes(10),
163 case CONNECTION_LATENCY_IPV4_NO_RACE
:
164 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv4_No_Race",
166 base::TimeDelta::FromMilliseconds(1),
167 base::TimeDelta::FromMinutes(10),
171 case CONNECTION_LATENCY_IPV6_RACEABLE
:
172 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv6_Raceable",
174 base::TimeDelta::FromMilliseconds(1),
175 base::TimeDelta::FromMinutes(10),
179 case CONNECTION_LATENCY_IPV6_SOLO
:
180 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv6_Solo",
182 base::TimeDelta::FromMilliseconds(1),
183 base::TimeDelta::FromMinutes(10),
192 return connect_duration
;
195 TransportConnectJob::TransportConnectJob(
196 const std::string
& group_name
,
197 RequestPriority priority
,
198 const scoped_refptr
<TransportSocketParams
>& params
,
199 base::TimeDelta timeout_duration
,
200 ClientSocketFactory
* client_socket_factory
,
201 HostResolver
* host_resolver
,
204 : ConnectJob(group_name
,
208 BoundNetLog::Make(net_log
, NetLog::SOURCE_CONNECT_JOB
)),
209 helper_(params
, client_socket_factory
, host_resolver
, &connect_timing_
),
210 interval_between_connects_(CONNECT_INTERVAL_GT_20MS
),
211 resolve_result_(OK
) {
212 helper_
.SetOnIOComplete(this);
215 TransportConnectJob::~TransportConnectJob() {
216 // We don't worry about cancelling the host resolution and TCP connect, since
217 // ~SingleRequestHostResolver and ~StreamSocket will take care of it.
220 LoadState
TransportConnectJob::GetLoadState() const {
221 switch (helper_
.next_state()) {
222 case TransportConnectJobHelper::STATE_RESOLVE_HOST
:
223 case TransportConnectJobHelper::STATE_RESOLVE_HOST_COMPLETE
:
224 return LOAD_STATE_RESOLVING_HOST
;
225 case TransportConnectJobHelper::STATE_TRANSPORT_CONNECT
:
226 case TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE
:
227 return LOAD_STATE_CONNECTING
;
228 case TransportConnectJobHelper::STATE_NONE
:
229 return LOAD_STATE_IDLE
;
232 return LOAD_STATE_IDLE
;
235 void TransportConnectJob::GetAdditionalErrorState(ClientSocketHandle
* handle
) {
236 // If hostname resolution failed, record an empty endpoint and the result.
237 // Also record any attempts made on either of the sockets.
238 ConnectionAttempts attempts
;
239 if (resolve_result_
!= OK
) {
240 DCHECK_EQ(0u, helper_
.addresses().size());
241 attempts
.push_back(ConnectionAttempt(IPEndPoint(), resolve_result_
));
243 attempts
.insert(attempts
.begin(), connection_attempts_
.begin(),
244 connection_attempts_
.end());
245 attempts
.insert(attempts
.begin(), fallback_connection_attempts_
.begin(),
246 fallback_connection_attempts_
.end());
247 handle
->set_connection_attempts(attempts
);
251 void TransportConnectJob::MakeAddressListStartWithIPv4(AddressList
* list
) {
252 for (AddressList::iterator i
= list
->begin(); i
!= list
->end(); ++i
) {
253 if (i
->GetFamily() == ADDRESS_FAMILY_IPV4
) {
254 std::rotate(list
->begin(), i
, list
->end());
260 int TransportConnectJob::DoResolveHost() {
261 // TODO(ricea): Remove ScopedTracker below once crbug.com/436634 is fixed.
262 tracked_objects::ScopedTracker
tracking_profile(
263 FROM_HERE_WITH_EXPLICIT_FUNCTION(
264 "436634 TransportConnectJob::DoResolveHost"));
266 return helper_
.DoResolveHost(priority(), net_log());
269 int TransportConnectJob::DoResolveHostComplete(int result
) {
270 resolve_result_
= result
;
271 return helper_
.DoResolveHostComplete(result
, net_log());
274 int TransportConnectJob::DoTransportConnect() {
275 base::TimeTicks now
= base::TimeTicks::Now();
276 base::TimeTicks last_connect_time
;
278 base::AutoLock
lock(g_last_connect_time_lock
.Get());
279 last_connect_time
= g_last_connect_time
.Get();
280 *g_last_connect_time
.Pointer() = now
;
282 if (last_connect_time
.is_null()) {
283 interval_between_connects_
= CONNECT_INTERVAL_GT_20MS
;
285 int64 interval
= (now
- last_connect_time
).InMilliseconds();
287 interval_between_connects_
= CONNECT_INTERVAL_LE_10MS
;
288 else if (interval
<= 20)
289 interval_between_connects_
= CONNECT_INTERVAL_LE_20MS
;
291 interval_between_connects_
= CONNECT_INTERVAL_GT_20MS
;
294 helper_
.set_next_state(
295 TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE
);
297 helper_
.client_socket_factory()->CreateTransportClientSocket(
298 helper_
.addresses(), net_log().net_log(), net_log().source());
300 // If the list contains IPv6 and IPv4 addresses, the first address will
301 // be IPv6, and the IPv4 addresses will be tried as fallback addresses,
302 // per "Happy Eyeballs" (RFC 6555).
303 bool try_ipv6_connect_with_ipv4_fallback
=
304 helper_
.addresses().front().GetFamily() == ADDRESS_FAMILY_IPV6
&&
305 !AddressListOnlyContainsIPv6(helper_
.addresses());
307 // Enable TCP FastOpen if indicated by transport socket params.
308 // Note: We currently do not turn on TCP FastOpen for destinations where
309 // we try a TCP connect over IPv6 with fallback to IPv4.
310 if (!try_ipv6_connect_with_ipv4_fallback
&&
311 helper_
.params()->combine_connect_and_write() ==
312 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED
) {
313 transport_socket_
->EnableTCPFastOpenIfSupported();
316 int rv
= transport_socket_
->Connect(helper_
.on_io_complete());
317 if (rv
== ERR_IO_PENDING
&& try_ipv6_connect_with_ipv4_fallback
) {
318 fallback_timer_
.Start(
320 base::TimeDelta::FromMilliseconds(
321 TransportConnectJobHelper::kIPv6FallbackTimerInMs
),
323 &TransportConnectJob::DoIPv6FallbackTransportConnect
);
328 int TransportConnectJob::DoTransportConnectComplete(int result
) {
330 // Success will be returned via the main socket, so also include connection
331 // attempts made on the fallback socket up to this point. (Unfortunately,
332 // the only simple way to return information in the success case is through
333 // the successfully-connected socket.)
334 if (fallback_transport_socket_
) {
335 ConnectionAttempts fallback_attempts
;
336 fallback_transport_socket_
->GetConnectionAttempts(&fallback_attempts
);
337 transport_socket_
->AddConnectionAttempts(fallback_attempts
);
341 helper_
.addresses().front().GetFamily() == ADDRESS_FAMILY_IPV4
;
342 TransportConnectJobHelper::ConnectionLatencyHistogram race_result
=
343 TransportConnectJobHelper::CONNECTION_LATENCY_UNKNOWN
;
345 race_result
= TransportConnectJobHelper::CONNECTION_LATENCY_IPV4_NO_RACE
;
347 if (AddressListOnlyContainsIPv6(helper_
.addresses())) {
348 race_result
= TransportConnectJobHelper::CONNECTION_LATENCY_IPV6_SOLO
;
351 TransportConnectJobHelper::CONNECTION_LATENCY_IPV6_RACEABLE
;
354 base::TimeDelta connect_duration
= helper_
.HistogramDuration(race_result
);
355 switch (interval_between_connects_
) {
356 case CONNECT_INTERVAL_LE_10MS
:
357 UMA_HISTOGRAM_CUSTOM_TIMES(
358 "Net.TCP_Connection_Latency_Interval_LessThanOrEqual_10ms",
360 base::TimeDelta::FromMilliseconds(1),
361 base::TimeDelta::FromMinutes(10),
364 case CONNECT_INTERVAL_LE_20MS
:
365 UMA_HISTOGRAM_CUSTOM_TIMES(
366 "Net.TCP_Connection_Latency_Interval_LessThanOrEqual_20ms",
368 base::TimeDelta::FromMilliseconds(1),
369 base::TimeDelta::FromMinutes(10),
372 case CONNECT_INTERVAL_GT_20MS
:
373 UMA_HISTOGRAM_CUSTOM_TIMES(
374 "Net.TCP_Connection_Latency_Interval_GreaterThan_20ms",
376 base::TimeDelta::FromMilliseconds(1),
377 base::TimeDelta::FromMinutes(10),
385 SetSocket(transport_socket_
.Pass());
387 // Failure will be returned via |GetAdditionalErrorState|, so save
388 // connection attempts from both sockets for use there.
389 CopyConnectionAttemptsFromSockets();
391 transport_socket_
.reset();
394 fallback_timer_
.Stop();
395 fallback_transport_socket_
.reset();
396 fallback_addresses_
.reset();
401 void TransportConnectJob::DoIPv6FallbackTransportConnect() {
402 // The timer should only fire while we're waiting for the main connect to
404 if (helper_
.next_state() !=
405 TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE
) {
410 DCHECK(!fallback_transport_socket_
.get());
411 DCHECK(!fallback_addresses_
.get());
413 fallback_addresses_
.reset(new AddressList(helper_
.addresses()));
414 MakeAddressListStartWithIPv4(fallback_addresses_
.get());
415 fallback_transport_socket_
=
416 helper_
.client_socket_factory()->CreateTransportClientSocket(
417 *fallback_addresses_
, net_log().net_log(), net_log().source());
418 fallback_connect_start_time_
= base::TimeTicks::Now();
419 int rv
= fallback_transport_socket_
->Connect(
421 &TransportConnectJob::DoIPv6FallbackTransportConnectComplete
,
422 base::Unretained(this)));
423 if (rv
!= ERR_IO_PENDING
)
424 DoIPv6FallbackTransportConnectComplete(rv
);
427 void TransportConnectJob::DoIPv6FallbackTransportConnectComplete(int result
) {
428 // This should only happen when we're waiting for the main connect to succeed.
429 if (helper_
.next_state() !=
430 TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE
) {
435 DCHECK_NE(ERR_IO_PENDING
, result
);
436 DCHECK(fallback_transport_socket_
.get());
437 DCHECK(fallback_addresses_
.get());
440 DCHECK(!fallback_connect_start_time_
.is_null());
442 // Success will be returned via the fallback socket, so also include
443 // connection attempts made on the main socket up to this point.
444 // (Unfortunately, the only simple way to return information in the success
445 // case is through the successfully-connected socket.)
446 if (transport_socket_
) {
447 ConnectionAttempts attempts
;
448 transport_socket_
->GetConnectionAttempts(&attempts
);
449 fallback_transport_socket_
->AddConnectionAttempts(attempts
);
452 connect_timing_
.connect_start
= fallback_connect_start_time_
;
453 helper_
.HistogramDuration(
454 TransportConnectJobHelper::CONNECTION_LATENCY_IPV4_WINS_RACE
);
455 SetSocket(fallback_transport_socket_
.Pass());
456 helper_
.set_next_state(TransportConnectJobHelper::STATE_NONE
);
458 // Failure will be returned via |GetAdditionalErrorState|, so save
459 // connection attempts from both sockets for use there.
460 CopyConnectionAttemptsFromSockets();
462 fallback_transport_socket_
.reset();
463 fallback_addresses_
.reset();
466 transport_socket_
.reset();
468 NotifyDelegateOfCompletion(result
); // Deletes |this|
471 int TransportConnectJob::ConnectInternal() {
472 return helper_
.DoConnectInternal(this);
475 void TransportConnectJob::CopyConnectionAttemptsFromSockets() {
476 if (transport_socket_
)
477 transport_socket_
->GetConnectionAttempts(&connection_attempts_
);
478 if (fallback_transport_socket_
) {
479 fallback_transport_socket_
->GetConnectionAttempts(
480 &fallback_connection_attempts_
);
484 scoped_ptr
<ConnectJob
>
485 TransportClientSocketPool::TransportConnectJobFactory::NewConnectJob(
486 const std::string
& group_name
,
487 const PoolBase::Request
& request
,
488 ConnectJob::Delegate
* delegate
) const {
489 return scoped_ptr
<ConnectJob
>(
490 new TransportConnectJob(group_name
,
494 client_socket_factory_
,
501 TransportClientSocketPool::TransportConnectJobFactory::ConnectionTimeout()
503 return base::TimeDelta::FromSeconds(kTransportConnectJobTimeoutInSeconds
);
506 TransportClientSocketPool::TransportClientSocketPool(
508 int max_sockets_per_group
,
509 HostResolver
* host_resolver
,
510 ClientSocketFactory
* client_socket_factory
,
514 max_sockets_per_group
,
515 ClientSocketPool::unused_idle_socket_timeout(),
516 ClientSocketPool::used_idle_socket_timeout(),
517 new TransportConnectJobFactory(client_socket_factory
,
520 base_
.EnableConnectBackupJobs();
523 TransportClientSocketPool::~TransportClientSocketPool() {}
525 int TransportClientSocketPool::RequestSocket(
526 const std::string
& group_name
,
528 RequestPriority priority
,
529 ClientSocketHandle
* handle
,
530 const CompletionCallback
& callback
,
531 const BoundNetLog
& net_log
) {
532 const scoped_refptr
<TransportSocketParams
>* casted_params
=
533 static_cast<const scoped_refptr
<TransportSocketParams
>*>(params
);
535 NetLogTcpClientSocketPoolRequestedSocket(net_log
, casted_params
);
537 return base_
.RequestSocket(group_name
, *casted_params
, priority
, handle
,
541 void TransportClientSocketPool::NetLogTcpClientSocketPoolRequestedSocket(
542 const BoundNetLog
& net_log
,
543 const scoped_refptr
<TransportSocketParams
>* casted_params
) {
544 if (net_log
.IsCapturing()) {
545 // TODO(eroman): Split out the host and port parameters.
547 NetLog::TYPE_TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET
,
548 CreateNetLogHostPortPairCallback(
549 &casted_params
->get()->destination().host_port_pair()));
553 void TransportClientSocketPool::RequestSockets(
554 const std::string
& group_name
,
557 const BoundNetLog
& net_log
) {
558 const scoped_refptr
<TransportSocketParams
>* casted_params
=
559 static_cast<const scoped_refptr
<TransportSocketParams
>*>(params
);
561 if (net_log
.IsCapturing()) {
562 // TODO(eroman): Split out the host and port parameters.
564 NetLog::TYPE_TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKETS
,
565 CreateNetLogHostPortPairCallback(
566 &casted_params
->get()->destination().host_port_pair()));
569 base_
.RequestSockets(group_name
, *casted_params
, num_sockets
, net_log
);
572 void TransportClientSocketPool::CancelRequest(
573 const std::string
& group_name
,
574 ClientSocketHandle
* handle
) {
575 base_
.CancelRequest(group_name
, handle
);
578 void TransportClientSocketPool::ReleaseSocket(
579 const std::string
& group_name
,
580 scoped_ptr
<StreamSocket
> socket
,
582 base_
.ReleaseSocket(group_name
, socket
.Pass(), id
);
585 void TransportClientSocketPool::FlushWithError(int error
) {
586 base_
.FlushWithError(error
);
589 void TransportClientSocketPool::CloseIdleSockets() {
590 base_
.CloseIdleSockets();
593 int TransportClientSocketPool::IdleSocketCount() const {
594 return base_
.idle_socket_count();
597 int TransportClientSocketPool::IdleSocketCountInGroup(
598 const std::string
& group_name
) const {
599 return base_
.IdleSocketCountInGroup(group_name
);
602 LoadState
TransportClientSocketPool::GetLoadState(
603 const std::string
& group_name
, const ClientSocketHandle
* handle
) const {
604 return base_
.GetLoadState(group_name
, handle
);
607 scoped_ptr
<base::DictionaryValue
> TransportClientSocketPool::GetInfoAsValue(
608 const std::string
& name
,
609 const std::string
& type
,
610 bool include_nested_pools
) const {
611 return base_
.GetInfoAsValue(name
, type
);
614 base::TimeDelta
TransportClientSocketPool::ConnectionTimeout() const {
615 return base_
.ConnectionTimeout();
618 bool TransportClientSocketPool::IsStalled() const {
619 return base_
.IsStalled();
622 void TransportClientSocketPool::AddHigherLayeredPool(
623 HigherLayeredPool
* higher_pool
) {
624 base_
.AddHigherLayeredPool(higher_pool
);
627 void TransportClientSocketPool::RemoveHigherLayeredPool(
628 HigherLayeredPool
* higher_pool
) {
629 base_
.RemoveHigherLayeredPool(higher_pool
);