Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / socket / transport_client_socket_pool.cc
blob5263e7000c991146e864cc6c5429c351ceb7add5
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"
7 #include <algorithm>
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.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;
30 namespace net {
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
34 // don't synchronize.
35 const int TransportConnectJobHelper::kIPv6FallbackTimerInMs = 300;
37 namespace {
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();
43 ++iter) {
44 if (iter->GetFamily() != ADDRESS_FAMILY_IPV6)
45 return false;
47 return true;
50 } // namespace
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,
63 bool ignore_limits,
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)
99 : params_(params),
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(
117 int result,
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;
124 if (result == OK) {
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);
129 if (result == OK)
130 next_state_ = STATE_TRANSPORT_CONNECT;
132 return result;
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",
142 total_duration,
143 base::TimeDelta::FromMilliseconds(1),
144 base::TimeDelta::FromMinutes(10),
145 100);
147 base::TimeDelta connect_duration = now - connect_timing_->connect_start;
148 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency",
149 connect_duration,
150 base::TimeDelta::FromMilliseconds(1),
151 base::TimeDelta::FromMinutes(10),
152 100);
154 switch (race_result) {
155 case CONNECTION_LATENCY_IPV4_WINS_RACE:
156 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv4_Wins_Race",
157 connect_duration,
158 base::TimeDelta::FromMilliseconds(1),
159 base::TimeDelta::FromMinutes(10),
160 100);
161 break;
163 case CONNECTION_LATENCY_IPV4_NO_RACE:
164 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv4_No_Race",
165 connect_duration,
166 base::TimeDelta::FromMilliseconds(1),
167 base::TimeDelta::FromMinutes(10),
168 100);
169 break;
171 case CONNECTION_LATENCY_IPV6_RACEABLE:
172 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv6_Raceable",
173 connect_duration,
174 base::TimeDelta::FromMilliseconds(1),
175 base::TimeDelta::FromMinutes(10),
176 100);
177 break;
179 case CONNECTION_LATENCY_IPV6_SOLO:
180 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv6_Solo",
181 connect_duration,
182 base::TimeDelta::FromMilliseconds(1),
183 base::TimeDelta::FromMinutes(10),
184 100);
185 break;
187 default:
188 NOTREACHED();
189 break;
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,
202 Delegate* delegate,
203 NetLog* net_log)
204 : ConnectJob(group_name, timeout_duration, priority, delegate,
205 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)),
206 helper_(params, client_socket_factory, host_resolver, &connect_timing_),
207 interval_between_connects_(CONNECT_INTERVAL_GT_20MS) {
208 helper_.SetOnIOComplete(this);
211 TransportConnectJob::~TransportConnectJob() {
212 // We don't worry about cancelling the host resolution and TCP connect, since
213 // ~SingleRequestHostResolver and ~StreamSocket will take care of it.
216 LoadState TransportConnectJob::GetLoadState() const {
217 switch (helper_.next_state()) {
218 case TransportConnectJobHelper::STATE_RESOLVE_HOST:
219 case TransportConnectJobHelper::STATE_RESOLVE_HOST_COMPLETE:
220 return LOAD_STATE_RESOLVING_HOST;
221 case TransportConnectJobHelper::STATE_TRANSPORT_CONNECT:
222 case TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE:
223 return LOAD_STATE_CONNECTING;
224 case TransportConnectJobHelper::STATE_NONE:
225 return LOAD_STATE_IDLE;
227 NOTREACHED();
228 return LOAD_STATE_IDLE;
231 // static
232 void TransportConnectJob::MakeAddressListStartWithIPv4(AddressList* list) {
233 for (AddressList::iterator i = list->begin(); i != list->end(); ++i) {
234 if (i->GetFamily() == ADDRESS_FAMILY_IPV4) {
235 std::rotate(list->begin(), i, list->end());
236 break;
241 int TransportConnectJob::DoResolveHost() {
242 // TODO(ricea): Remove ScopedTracker below once crbug.com/436634 is fixed.
243 tracked_objects::ScopedTracker tracking_profile(
244 FROM_HERE_WITH_EXPLICIT_FUNCTION(
245 "436634 TransportConnectJob::DoResolveHost"));
247 return helper_.DoResolveHost(priority(), net_log());
250 int TransportConnectJob::DoResolveHostComplete(int result) {
251 return helper_.DoResolveHostComplete(result, net_log());
254 int TransportConnectJob::DoTransportConnect() {
255 base::TimeTicks now = base::TimeTicks::Now();
256 base::TimeTicks last_connect_time;
258 base::AutoLock lock(g_last_connect_time_lock.Get());
259 last_connect_time = g_last_connect_time.Get();
260 *g_last_connect_time.Pointer() = now;
262 if (last_connect_time.is_null()) {
263 interval_between_connects_ = CONNECT_INTERVAL_GT_20MS;
264 } else {
265 int64 interval = (now - last_connect_time).InMilliseconds();
266 if (interval <= 10)
267 interval_between_connects_ = CONNECT_INTERVAL_LE_10MS;
268 else if (interval <= 20)
269 interval_between_connects_ = CONNECT_INTERVAL_LE_20MS;
270 else
271 interval_between_connects_ = CONNECT_INTERVAL_GT_20MS;
274 helper_.set_next_state(
275 TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE);
276 transport_socket_ =
277 helper_.client_socket_factory()->CreateTransportClientSocket(
278 helper_.addresses(), net_log().net_log(), net_log().source());
280 // If the list contains IPv6 and IPv4 addresses, the first address will
281 // be IPv6, and the IPv4 addresses will be tried as fallback addresses,
282 // per "Happy Eyeballs" (RFC 6555).
283 bool try_ipv6_connect_with_ipv4_fallback =
284 helper_.addresses().front().GetFamily() == ADDRESS_FAMILY_IPV6 &&
285 !AddressListOnlyContainsIPv6(helper_.addresses());
287 // Enable TCP FastOpen if indicated by transport socket params.
288 // Note: We currently do not turn on TCP FastOpen for destinations where
289 // we try a TCP connect over IPv6 with fallback to IPv4.
290 if (!try_ipv6_connect_with_ipv4_fallback &&
291 helper_.params()->combine_connect_and_write() ==
292 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED) {
293 transport_socket_->EnableTCPFastOpenIfSupported();
296 int rv = transport_socket_->Connect(helper_.on_io_complete());
297 if (rv == ERR_IO_PENDING && try_ipv6_connect_with_ipv4_fallback) {
298 fallback_timer_.Start(
299 FROM_HERE,
300 base::TimeDelta::FromMilliseconds(
301 TransportConnectJobHelper::kIPv6FallbackTimerInMs),
302 this,
303 &TransportConnectJob::DoIPv6FallbackTransportConnect);
305 return rv;
308 int TransportConnectJob::DoTransportConnectComplete(int result) {
309 if (result == OK) {
310 bool is_ipv4 =
311 helper_.addresses().front().GetFamily() == ADDRESS_FAMILY_IPV4;
312 TransportConnectJobHelper::ConnectionLatencyHistogram race_result =
313 TransportConnectJobHelper::CONNECTION_LATENCY_UNKNOWN;
314 if (is_ipv4) {
315 race_result = TransportConnectJobHelper::CONNECTION_LATENCY_IPV4_NO_RACE;
316 } else {
317 if (AddressListOnlyContainsIPv6(helper_.addresses())) {
318 race_result = TransportConnectJobHelper::CONNECTION_LATENCY_IPV6_SOLO;
319 } else {
320 race_result =
321 TransportConnectJobHelper::CONNECTION_LATENCY_IPV6_RACEABLE;
324 base::TimeDelta connect_duration = helper_.HistogramDuration(race_result);
325 switch (interval_between_connects_) {
326 case CONNECT_INTERVAL_LE_10MS:
327 UMA_HISTOGRAM_CUSTOM_TIMES(
328 "Net.TCP_Connection_Latency_Interval_LessThanOrEqual_10ms",
329 connect_duration,
330 base::TimeDelta::FromMilliseconds(1),
331 base::TimeDelta::FromMinutes(10),
332 100);
333 break;
334 case CONNECT_INTERVAL_LE_20MS:
335 UMA_HISTOGRAM_CUSTOM_TIMES(
336 "Net.TCP_Connection_Latency_Interval_LessThanOrEqual_20ms",
337 connect_duration,
338 base::TimeDelta::FromMilliseconds(1),
339 base::TimeDelta::FromMinutes(10),
340 100);
341 break;
342 case CONNECT_INTERVAL_GT_20MS:
343 UMA_HISTOGRAM_CUSTOM_TIMES(
344 "Net.TCP_Connection_Latency_Interval_GreaterThan_20ms",
345 connect_duration,
346 base::TimeDelta::FromMilliseconds(1),
347 base::TimeDelta::FromMinutes(10),
348 100);
349 break;
350 default:
351 NOTREACHED();
352 break;
355 SetSocket(transport_socket_.Pass());
356 fallback_timer_.Stop();
357 } else {
358 // Be a bit paranoid and kill off the fallback members to prevent reuse.
359 fallback_transport_socket_.reset();
360 fallback_addresses_.reset();
363 return result;
366 void TransportConnectJob::DoIPv6FallbackTransportConnect() {
367 // The timer should only fire while we're waiting for the main connect to
368 // succeed.
369 if (helper_.next_state() !=
370 TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE) {
371 NOTREACHED();
372 return;
375 DCHECK(!fallback_transport_socket_.get());
376 DCHECK(!fallback_addresses_.get());
378 fallback_addresses_.reset(new AddressList(helper_.addresses()));
379 MakeAddressListStartWithIPv4(fallback_addresses_.get());
380 fallback_transport_socket_ =
381 helper_.client_socket_factory()->CreateTransportClientSocket(
382 *fallback_addresses_, net_log().net_log(), net_log().source());
383 fallback_connect_start_time_ = base::TimeTicks::Now();
384 int rv = fallback_transport_socket_->Connect(
385 base::Bind(
386 &TransportConnectJob::DoIPv6FallbackTransportConnectComplete,
387 base::Unretained(this)));
388 if (rv != ERR_IO_PENDING)
389 DoIPv6FallbackTransportConnectComplete(rv);
392 void TransportConnectJob::DoIPv6FallbackTransportConnectComplete(int result) {
393 // This should only happen when we're waiting for the main connect to succeed.
394 if (helper_.next_state() !=
395 TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE) {
396 NOTREACHED();
397 return;
400 DCHECK_NE(ERR_IO_PENDING, result);
401 DCHECK(fallback_transport_socket_.get());
402 DCHECK(fallback_addresses_.get());
404 if (result == OK) {
405 DCHECK(!fallback_connect_start_time_.is_null());
406 connect_timing_.connect_start = fallback_connect_start_time_;
407 helper_.HistogramDuration(
408 TransportConnectJobHelper::CONNECTION_LATENCY_IPV4_WINS_RACE);
409 SetSocket(fallback_transport_socket_.Pass());
410 helper_.set_next_state(TransportConnectJobHelper::STATE_NONE);
411 transport_socket_.reset();
412 } else {
413 // Be a bit paranoid and kill off the fallback members to prevent reuse.
414 fallback_transport_socket_.reset();
415 fallback_addresses_.reset();
417 NotifyDelegateOfCompletion(result); // Deletes |this|
420 int TransportConnectJob::ConnectInternal() {
421 return helper_.DoConnectInternal(this);
424 scoped_ptr<ConnectJob>
425 TransportClientSocketPool::TransportConnectJobFactory::NewConnectJob(
426 const std::string& group_name,
427 const PoolBase::Request& request,
428 ConnectJob::Delegate* delegate) const {
429 return scoped_ptr<ConnectJob>(
430 new TransportConnectJob(group_name,
431 request.priority(),
432 request.params(),
433 ConnectionTimeout(),
434 client_socket_factory_,
435 host_resolver_,
436 delegate,
437 net_log_));
440 base::TimeDelta
441 TransportClientSocketPool::TransportConnectJobFactory::ConnectionTimeout()
442 const {
443 return base::TimeDelta::FromSeconds(kTransportConnectJobTimeoutInSeconds);
446 TransportClientSocketPool::TransportClientSocketPool(
447 int max_sockets,
448 int max_sockets_per_group,
449 HostResolver* host_resolver,
450 ClientSocketFactory* client_socket_factory,
451 NetLog* net_log)
452 : base_(NULL,
453 max_sockets,
454 max_sockets_per_group,
455 ClientSocketPool::unused_idle_socket_timeout(),
456 ClientSocketPool::used_idle_socket_timeout(),
457 new TransportConnectJobFactory(client_socket_factory,
458 host_resolver,
459 net_log)) {
460 base_.EnableConnectBackupJobs();
463 TransportClientSocketPool::~TransportClientSocketPool() {}
465 int TransportClientSocketPool::RequestSocket(
466 const std::string& group_name,
467 const void* params,
468 RequestPriority priority,
469 ClientSocketHandle* handle,
470 const CompletionCallback& callback,
471 const BoundNetLog& net_log) {
472 const scoped_refptr<TransportSocketParams>* casted_params =
473 static_cast<const scoped_refptr<TransportSocketParams>*>(params);
475 NetLogTcpClientSocketPoolRequestedSocket(net_log, casted_params);
477 return base_.RequestSocket(group_name, *casted_params, priority, handle,
478 callback, net_log);
481 void TransportClientSocketPool::NetLogTcpClientSocketPoolRequestedSocket(
482 const BoundNetLog& net_log,
483 const scoped_refptr<TransportSocketParams>* casted_params) {
484 if (net_log.GetCaptureMode().enabled()) {
485 // TODO(eroman): Split out the host and port parameters.
486 net_log.AddEvent(
487 NetLog::TYPE_TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET,
488 CreateNetLogHostPortPairCallback(
489 &casted_params->get()->destination().host_port_pair()));
493 void TransportClientSocketPool::RequestSockets(
494 const std::string& group_name,
495 const void* params,
496 int num_sockets,
497 const BoundNetLog& net_log) {
498 const scoped_refptr<TransportSocketParams>* casted_params =
499 static_cast<const scoped_refptr<TransportSocketParams>*>(params);
501 if (net_log.GetCaptureMode().enabled()) {
502 // TODO(eroman): Split out the host and port parameters.
503 net_log.AddEvent(
504 NetLog::TYPE_TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKETS,
505 CreateNetLogHostPortPairCallback(
506 &casted_params->get()->destination().host_port_pair()));
509 base_.RequestSockets(group_name, *casted_params, num_sockets, net_log);
512 void TransportClientSocketPool::CancelRequest(
513 const std::string& group_name,
514 ClientSocketHandle* handle) {
515 base_.CancelRequest(group_name, handle);
518 void TransportClientSocketPool::ReleaseSocket(
519 const std::string& group_name,
520 scoped_ptr<StreamSocket> socket,
521 int id) {
522 base_.ReleaseSocket(group_name, socket.Pass(), id);
525 void TransportClientSocketPool::FlushWithError(int error) {
526 base_.FlushWithError(error);
529 void TransportClientSocketPool::CloseIdleSockets() {
530 base_.CloseIdleSockets();
533 int TransportClientSocketPool::IdleSocketCount() const {
534 return base_.idle_socket_count();
537 int TransportClientSocketPool::IdleSocketCountInGroup(
538 const std::string& group_name) const {
539 return base_.IdleSocketCountInGroup(group_name);
542 LoadState TransportClientSocketPool::GetLoadState(
543 const std::string& group_name, const ClientSocketHandle* handle) const {
544 return base_.GetLoadState(group_name, handle);
547 base::DictionaryValue* TransportClientSocketPool::GetInfoAsValue(
548 const std::string& name,
549 const std::string& type,
550 bool include_nested_pools) const {
551 return base_.GetInfoAsValue(name, type);
554 base::TimeDelta TransportClientSocketPool::ConnectionTimeout() const {
555 return base_.ConnectionTimeout();
558 bool TransportClientSocketPool::IsStalled() const {
559 return base_.IsStalled();
562 void TransportClientSocketPool::AddHigherLayeredPool(
563 HigherLayeredPool* higher_pool) {
564 base_.AddHigherLayeredPool(higher_pool);
567 void TransportClientSocketPool::RemoveHigherLayeredPool(
568 HigherLayeredPool* higher_pool) {
569 base_.RemoveHigherLayeredPool(higher_pool);
572 } // namespace net