OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/transport_client_socket_pool.h" | 5 #include "net/socket/transport_client_socket_pool.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "net/base/ip_endpoint.h" | 18 #include "net/base/ip_endpoint.h" |
19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
20 #include "net/base/net_log.h" | 20 #include "net/base/net_log.h" |
21 #include "net/socket/client_socket_factory.h" | 21 #include "net/socket/client_socket_factory.h" |
22 #include "net/socket/client_socket_handle.h" | 22 #include "net/socket/client_socket_handle.h" |
23 #include "net/socket/client_socket_pool_base.h" | 23 #include "net/socket/client_socket_pool_base.h" |
24 #include "net/socket/socket_net_log_params.h" | 24 #include "net/socket/socket_net_log_params.h" |
25 #include "net/socket/tcp_client_socket.h" | 25 #include "net/socket/tcp_client_socket.h" |
26 | 26 |
27 using base::TimeDelta; | 27 using base::TimeDelta; |
| 28 extern bool g_tcp_fastopen_user_enabled; |
| 29 extern bool g_tcp_fastopen_supported; |
28 | 30 |
29 namespace net { | 31 namespace net { |
30 | 32 |
31 // TODO(willchan): Base this off RTT instead of statically setting it. Note we | 33 // TODO(willchan): Base this off RTT instead of statically setting it. Note we |
32 // choose a timeout that is different from the backup connect job timer so they | 34 // choose a timeout that is different from the backup connect job timer so they |
33 // don't synchronize. | 35 // don't synchronize. |
34 const int TransportConnectJobHelper::kIPv6FallbackTimerInMs = 300; | 36 const int TransportConnectJobHelper::kIPv6FallbackTimerInMs = 300; |
35 | 37 |
36 namespace { | 38 namespace { |
37 | 39 |
(...skipping 15 matching lines...) Expand all Loading... |
53 g_last_connect_time_lock = LAZY_INSTANCE_INITIALIZER; | 55 g_last_connect_time_lock = LAZY_INSTANCE_INITIALIZER; |
54 | 56 |
55 // |g_last_connect_time| has the last time a connect() call is made. | 57 // |g_last_connect_time| has the last time a connect() call is made. |
56 static base::LazyInstance<base::TimeTicks>::Leaky | 58 static base::LazyInstance<base::TimeTicks>::Leaky |
57 g_last_connect_time = LAZY_INSTANCE_INITIALIZER; | 59 g_last_connect_time = LAZY_INSTANCE_INITIALIZER; |
58 | 60 |
59 TransportSocketParams::TransportSocketParams( | 61 TransportSocketParams::TransportSocketParams( |
60 const HostPortPair& host_port_pair, | 62 const HostPortPair& host_port_pair, |
61 bool disable_resolver_cache, | 63 bool disable_resolver_cache, |
62 bool ignore_limits, | 64 bool ignore_limits, |
63 const OnHostResolutionCallback& host_resolution_callback) | 65 const OnHostResolutionCallback& host_resolution_callback, |
| 66 CombineConnectAndWritePolicy combine_connect_and_write_if_supported) |
64 : destination_(host_port_pair), | 67 : destination_(host_port_pair), |
65 ignore_limits_(ignore_limits), | 68 ignore_limits_(ignore_limits), |
66 host_resolution_callback_(host_resolution_callback) { | 69 host_resolution_callback_(host_resolution_callback), |
| 70 combine_connect_and_write_(combine_connect_and_write_if_supported) { |
67 if (disable_resolver_cache) | 71 if (disable_resolver_cache) |
68 destination_.set_allow_cached_response(false); | 72 destination_.set_allow_cached_response(false); |
| 73 // combine_connect_and_write currently translates to TCP FastOpen. |
| 74 // Enable TCP FastOpen if user wants it and system supports it. |
| 75 if (!g_tcp_fastopen_supported) { |
| 76 combine_connect_and_write_ = COMBINE_CONNECT_AND_WRITE_PROHIBITED; |
| 77 } else { |
| 78 if (g_tcp_fastopen_user_enabled && |
| 79 combine_connect_and_write_ == COMBINE_CONNECT_AND_WRITE_DEFAULT) { |
| 80 combine_connect_and_write_ = COMBINE_CONNECT_AND_WRITE_DESIRED; |
| 81 } |
| 82 } |
69 } | 83 } |
70 | 84 |
71 TransportSocketParams::~TransportSocketParams() {} | 85 TransportSocketParams::~TransportSocketParams() {} |
72 | 86 |
73 // TransportConnectJobs will time out after this many seconds. Note this is | 87 // TransportConnectJobs will time out after this many seconds. Note this is |
74 // the total time, including both host resolution and TCP connect() times. | 88 // the total time, including both host resolution and TCP connect() times. |
75 // | 89 // |
76 // TODO(eroman): The use of this constant needs to be re-evaluated. The time | 90 // TODO(eroman): The use of this constant needs to be re-evaluated. The time |
77 // needed for TCPClientSocketXXX::Connect() can be arbitrarily long, since | 91 // needed for TCPClientSocketXXX::Connect() can be arbitrarily long, since |
78 // the address list may contain many alternatives, and most of those may | 92 // the address list may contain many alternatives, and most of those may |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 interval_between_connects_ = CONNECT_INTERVAL_LE_20MS; | 268 interval_between_connects_ = CONNECT_INTERVAL_LE_20MS; |
255 else | 269 else |
256 interval_between_connects_ = CONNECT_INTERVAL_GT_20MS; | 270 interval_between_connects_ = CONNECT_INTERVAL_GT_20MS; |
257 } | 271 } |
258 | 272 |
259 helper_.set_next_state( | 273 helper_.set_next_state( |
260 TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE); | 274 TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE); |
261 transport_socket_ = | 275 transport_socket_ = |
262 helper_.client_socket_factory()->CreateTransportClientSocket( | 276 helper_.client_socket_factory()->CreateTransportClientSocket( |
263 helper_.addresses(), net_log().net_log(), net_log().source()); | 277 helper_.addresses(), net_log().net_log(), net_log().source()); |
| 278 |
| 279 // If the list contains IPv6 and IPv4 addresses, the first address will |
| 280 // be IPv6, and the IPv4 addresses will be tried as fallback addresses, |
| 281 // per "Happy Eyeballs" (RFC 6555). |
| 282 bool try_ipv6_connect_with_ipv4_fallback = |
| 283 helper_.addresses().front().GetFamily() == ADDRESS_FAMILY_IPV6 && |
| 284 !AddressListOnlyContainsIPv6(helper_.addresses()); |
| 285 |
| 286 // Enable TCP FastOpen if indicated by transport socket params. |
| 287 // Note: We currently do not turn on TCP FastOpen for destinations where |
| 288 // we try a TCP connect over IPv6 with fallback to IPv4. |
| 289 if (!try_ipv6_connect_with_ipv4_fallback && |
| 290 helper_.params()->combine_connect_and_write() == |
| 291 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED) { |
| 292 transport_socket_->EnableTCPFastOpen(); |
| 293 } |
| 294 |
264 int rv = transport_socket_->Connect(helper_.on_io_complete()); | 295 int rv = transport_socket_->Connect(helper_.on_io_complete()); |
265 if (rv == ERR_IO_PENDING && | 296 if (rv == ERR_IO_PENDING && try_ipv6_connect_with_ipv4_fallback) { |
266 helper_.addresses().front().GetFamily() == ADDRESS_FAMILY_IPV6 && | |
267 !AddressListOnlyContainsIPv6(helper_.addresses())) { | |
268 fallback_timer_.Start( | 297 fallback_timer_.Start( |
269 FROM_HERE, | 298 FROM_HERE, |
270 base::TimeDelta::FromMilliseconds( | 299 base::TimeDelta::FromMilliseconds( |
271 TransportConnectJobHelper::kIPv6FallbackTimerInMs), | 300 TransportConnectJobHelper::kIPv6FallbackTimerInMs), |
272 this, | 301 this, |
273 &TransportConnectJob::DoIPv6FallbackTransportConnect); | 302 &TransportConnectJob::DoIPv6FallbackTransportConnect); |
274 } | 303 } |
275 return rv; | 304 return rv; |
276 } | 305 } |
277 | 306 |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 HigherLayeredPool* higher_pool) { | 564 HigherLayeredPool* higher_pool) { |
536 base_.AddHigherLayeredPool(higher_pool); | 565 base_.AddHigherLayeredPool(higher_pool); |
537 } | 566 } |
538 | 567 |
539 void TransportClientSocketPool::RemoveHigherLayeredPool( | 568 void TransportClientSocketPool::RemoveHigherLayeredPool( |
540 HigherLayeredPool* higher_pool) { | 569 HigherLayeredPool* higher_pool) { |
541 base_.RemoveHigherLayeredPool(higher_pool); | 570 base_.RemoveHigherLayeredPool(higher_pool); |
542 } | 571 } |
543 | 572 |
544 } // namespace net | 573 } // namespace net |
OLD | NEW |