[email protected] | d0d49dd8 | 2012-01-26 00:03:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/dns/dns_session.h" |
| 6 | |
avi | 6516805 | 2015-12-01 19:27:07 | [diff] [blame] | 7 | #include <stdint.h> |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 8 | |
avi | 6516805 | 2015-12-01 19:27:07 | [diff] [blame] | 9 | #include <limits> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 10 | #include <utility> |
avi | 6516805 | 2015-12-01 19:27:07 | [diff] [blame] | 11 | |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 14 | #include "base/macros.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame] | 15 | #include "base/metrics/histogram_macros.h" |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 16 | #include "base/metrics/sample_vector.h" |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 17 | #include "base/rand_util.h" |
| 18 | #include "base/stl_util.h" |
[email protected] | 66e96c4 | 2013-06-28 15:20:31 | [diff] [blame] | 19 | #include "base/time/time.h" |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 20 | #include "net/base/ip_endpoint.h" |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 21 | #include "net/base/net_errors.h" |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 22 | #include "net/dns/dns_config.h" |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 23 | #include "net/dns/dns_socket_pool.h" |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 24 | #include "net/dns/dns_util.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 25 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 26 | #include "net/log/net_log_source.h" |
| 27 | #include "net/log/net_log_with_source.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 28 | #include "net/socket/datagram_client_socket.h" |
[email protected] | bdb6598 | 2012-12-20 20:44:59 | [diff] [blame] | 29 | #include "net/socket/stream_socket.h" |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 30 | |
| 31 | namespace net { |
| 32 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 33 | namespace { |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 34 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 35 | // Set min timeout, in case we are talking to a local DNS proxy. |
| 36 | const unsigned kMinTimeoutMs = 10; |
| 37 | |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 38 | // Default maximum timeout between queries, even with exponential backoff. |
| 39 | // (Can be overridden by field trial.) |
| 40 | const unsigned kDefaultMaxTimeoutMs = 5000; |
| 41 | |
| 42 | // Maximum RTT that will fit in the RTT histograms. |
| 43 | const int32_t kRTTMaxMs = 30000; |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 44 | // Number of buckets in the histogram of observed RTTs. |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 45 | const size_t kRTTBucketCount = 350; |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 46 | // Target percentile in the RTT histogram used for retransmission timeout. |
| 47 | const unsigned kRTOPercentile = 99; |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 48 | // Number of samples to seed the histogram with. |
| 49 | const unsigned kNumSeeds = 2; |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 50 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 51 | } // namespace |
| 52 | |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 53 | // Runtime statistics of DNS server. |
| 54 | struct DnsSession::ServerStats { |
| 55 | ServerStats(base::TimeDelta rtt_estimate_param, RttBuckets* buckets) |
| 56 | : last_failure_count(0), rtt_estimate(rtt_estimate_param) { |
| 57 | rtt_histogram.reset(new base::SampleVector(buckets)); |
[email protected] | a144bd2 | 2013-07-29 21:53:10 | [diff] [blame] | 58 | // Seed histogram with 2 samples at |rtt_estimate| timeout. |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 59 | rtt_histogram->Accumulate( |
| 60 | static_cast<base::HistogramBase::Sample>(rtt_estimate.InMilliseconds()), |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 61 | kNumSeeds); |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Count of consecutive failures after last success. |
| 65 | int last_failure_count; |
| 66 | |
| 67 | // Last time when server returned failure or timeout. |
| 68 | base::Time last_failure; |
| 69 | // Last time when server returned success. |
| 70 | base::Time last_success; |
| 71 | |
| 72 | // Estimated RTT using moving average. |
| 73 | base::TimeDelta rtt_estimate; |
| 74 | // Estimated error in the above. |
| 75 | base::TimeDelta rtt_deviation; |
| 76 | |
| 77 | // A histogram of observed RTT . |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 78 | std::unique_ptr<base::SampleVector> rtt_histogram; |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 79 | |
| 80 | DISALLOW_COPY_AND_ASSIGN(ServerStats); |
| 81 | }; |
| 82 | |
| 83 | // static |
| 84 | base::LazyInstance<DnsSession::RttBuckets>::Leaky DnsSession::rtt_buckets_ = |
| 85 | LAZY_INSTANCE_INITIALIZER; |
| 86 | |
| 87 | DnsSession::RttBuckets::RttBuckets() : base::BucketRanges(kRTTBucketCount + 1) { |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 88 | base::Histogram::InitializeBucketRanges(1, kRTTMaxMs, this); |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 89 | } |
| 90 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 91 | DnsSession::SocketLease::SocketLease( |
| 92 | scoped_refptr<DnsSession> session, |
| 93 | unsigned server_index, |
| 94 | std::unique_ptr<DatagramClientSocket> socket) |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 95 | : session_(session), |
| 96 | server_index_(server_index), |
| 97 | socket_(std::move(socket)) {} |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 98 | |
| 99 | DnsSession::SocketLease::~SocketLease() { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 100 | session_->FreeSocket(server_index_, std::move(socket_)); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 103 | DnsSession::DnsSession(const DnsConfig& config, |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 104 | std::unique_ptr<DnsSocketPool> socket_pool, |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 105 | const RandIntCallback& rand_int_callback, |
| 106 | NetLog* net_log) |
| 107 | : config_(config), |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 108 | socket_pool_(std::move(socket_pool)), |
avi | 6516805 | 2015-12-01 19:27:07 | [diff] [blame] | 109 | rand_callback_(base::Bind(rand_int_callback, |
| 110 | 0, |
| 111 | std::numeric_limits<uint16_t>::max())), |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 112 | net_log_(net_log), |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 113 | server_index_(0) { |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 114 | socket_pool_->Initialize(&config_.nameservers, net_log); |
drbasic | f0d1b26 | 2016-08-23 06:10:42 | [diff] [blame] | 115 | UMA_HISTOGRAM_CUSTOM_COUNTS("AsyncDNS.ServerCount", |
| 116 | config_.nameservers.size(), 1, 10, 11); |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 117 | UpdateTimeouts(NetworkChangeNotifier::GetConnectionType()); |
| 118 | InitializeServerStats(); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 119 | } |
| 120 | |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 121 | DnsSession::~DnsSession() = default; |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 122 | |
| 123 | void DnsSession::UpdateTimeouts(NetworkChangeNotifier::ConnectionType type) { |
| 124 | initial_timeout_ = GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault( |
| 125 | "AsyncDnsInitialTimeoutMsByConnectionType", config_.timeout, type); |
| 126 | max_timeout_ = GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault( |
| 127 | "AsyncDnsMaxTimeoutMsByConnectionType", |
| 128 | base::TimeDelta::FromMilliseconds(kDefaultMaxTimeoutMs), type); |
| 129 | } |
| 130 | |
| 131 | void DnsSession::InitializeServerStats() { |
| 132 | server_stats_.clear(); |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 133 | for (size_t i = 0; i < config_.nameservers.size(); ++i) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 134 | server_stats_.push_back(std::make_unique<ServerStats>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 135 | initial_timeout_, rtt_buckets_.Pointer())); |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 136 | } |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 137 | |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 138 | doh_server_stats_.clear(); |
| 139 | for (size_t i = 0; i < config_.dns_over_https_servers.size(); ++i) { |
| 140 | doh_server_stats_.push_back(std::make_pair( |
| 141 | std::make_unique<ServerStats>(initial_timeout_, rtt_buckets_.Pointer()), |
| 142 | false)); |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 143 | } |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 144 | } |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 145 | |
avi | 6516805 | 2015-12-01 19:27:07 | [diff] [blame] | 146 | uint16_t DnsSession::NextQueryId() const { |
| 147 | return static_cast<uint16_t>(rand_callback_.Run()); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 148 | } |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 149 | |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 150 | unsigned DnsSession::NextFirstServerIndex() { |
| 151 | unsigned index = NextGoodServerIndex(server_index_); |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 152 | if (config_.rotate) |
| 153 | server_index_ = (server_index_ + 1) % config_.nameservers.size(); |
[email protected] | d0d49dd8 | 2012-01-26 00:03:59 | [diff] [blame] | 154 | return index; |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 157 | unsigned DnsSession::NextGoodServerIndex(unsigned server_index) { |
Brad Lassey | 786929ad | 2018-02-21 20:54:27 | [diff] [blame] | 158 | DCHECK_GE(server_index, 0u); |
| 159 | DCHECK_LT(server_index, config_.nameservers.size()); |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 160 | unsigned index = server_index; |
| 161 | base::Time oldest_server_failure(base::Time::Now()); |
| 162 | unsigned oldest_server_failure_index = 0; |
| 163 | |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 164 | do { |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 165 | // If number of failures on this server doesn't exceed number of allowed |
| 166 | // attempts, return its index. |
| 167 | if (server_stats_[server_index]->last_failure_count < config_.attempts) { |
| 168 | return index; |
| 169 | } |
| 170 | // Track oldest failed server. |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 171 | base::Time cur_server_failure = server_stats_[index]->last_failure; |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 172 | if (cur_server_failure < oldest_server_failure) { |
| 173 | oldest_server_failure = cur_server_failure; |
| 174 | oldest_server_failure_index = index; |
| 175 | } |
| 176 | index = (index + 1) % config_.nameservers.size(); |
| 177 | } while (index != server_index); |
| 178 | |
| 179 | // If we are here it means that there are no successful servers, so we have |
| 180 | // to use one that has failed oldest. |
| 181 | return oldest_server_failure_index; |
| 182 | } |
| 183 | |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 184 | int DnsSession::NextGoodDohServerIndex( |
| 185 | unsigned doh_server_index, |
| 186 | DnsConfig::SecureDnsMode secure_dns_mode) { |
| 187 | DCHECK_GE(doh_server_index, 0u); |
| 188 | DCHECK_LT(doh_server_index, config_.dns_over_https_servers.size()); |
| 189 | unsigned index = doh_server_index; |
Brad Lassey | 786929ad | 2018-02-21 20:54:27 | [diff] [blame] | 190 | base::Time oldest_server_failure(base::Time::Now()); |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 191 | int oldest_available_server_failure_index = -1; |
Brad Lassey | 786929ad | 2018-02-21 20:54:27 | [diff] [blame] | 192 | |
| 193 | do { |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 194 | // For a server to be considered "available", the server must have a |
| 195 | // successful probe status if we are in AUTOMATIC mode. |
| 196 | if (secure_dns_mode == DnsConfig::SecureDnsMode::SECURE || |
| 197 | doh_server_stats_[index].second) { |
| 198 | // If number of failures on this server doesn't exceed |config_.attempts|, |
| 199 | // return its index. |config_.attempts| will generally be more restrictive |
| 200 | // than |kAutomaticModeFailureLimit|, although this is not guaranteed. |
| 201 | const ServerStats* stats = |
| 202 | GetServerStats(index, true /* is_doh_server */); |
| 203 | if (stats->last_failure_count < config_.attempts) { |
| 204 | return index; |
| 205 | } |
| 206 | // Track oldest failed available server. |
| 207 | base::Time cur_server_failure = stats->last_failure; |
| 208 | if (cur_server_failure < oldest_server_failure) { |
| 209 | oldest_server_failure = cur_server_failure; |
| 210 | oldest_available_server_failure_index = index; |
| 211 | } |
Brad Lassey | 786929ad | 2018-02-21 20:54:27 | [diff] [blame] | 212 | } |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 213 | index = (index + 1) % config_.dns_over_https_servers.size(); |
| 214 | } while (index != doh_server_index); |
Brad Lassey | 786929ad | 2018-02-21 20:54:27 | [diff] [blame] | 215 | |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 216 | // If we are here it means that there are either no available DoH servers or |
| 217 | // that all available DoH servers have at least |config_.attempts| consecutive |
| 218 | // failures. In the latter case, we'll return the available DoH server that |
| 219 | // failed least recently. In the former case we return -1. |
| 220 | return oldest_available_server_failure_index; |
Brad Lassey | 786929ad | 2018-02-21 20:54:27 | [diff] [blame] | 221 | } |
| 222 | |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 223 | bool DnsSession::HasAvailableDohServer() { |
| 224 | for (const auto& doh_stats_ : doh_server_stats_) { |
| 225 | if (doh_stats_.second) |
| 226 | return true; |
| 227 | } |
| 228 | return false; |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 229 | } |
| 230 | |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 231 | unsigned DnsSession::NumAvailableDohServers() { |
| 232 | unsigned count = 0; |
| 233 | for (const auto& doh_stats_ : doh_server_stats_) { |
| 234 | if (doh_stats_.second) |
| 235 | count++; |
| 236 | } |
| 237 | return count; |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 238 | } |
| 239 | |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 240 | DnsSession::ServerStats* DnsSession::GetServerStats(unsigned server_index, |
| 241 | bool is_doh_server) { |
| 242 | DCHECK_GE(server_index, 0u); |
| 243 | if (!is_doh_server) { |
| 244 | DCHECK_LT(server_index, config_.nameservers.size()); |
| 245 | return server_stats_[server_index].get(); |
| 246 | } else { |
| 247 | DCHECK_LT(server_index, config_.dns_over_https_servers.size()); |
| 248 | return doh_server_stats_[server_index].first.get(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void DnsSession::RecordServerFailure(unsigned server_index, |
| 253 | bool is_doh_server) { |
| 254 | ServerStats* stats = GetServerStats(server_index, is_doh_server); |
| 255 | ++(stats->last_failure_count); |
| 256 | stats->last_failure = base::Time::Now(); |
| 257 | |
| 258 | if (is_doh_server && |
| 259 | stats->last_failure_count >= kAutomaticModeFailureLimit) { |
| 260 | SetProbeSuccess(server_index, false /* success */); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void DnsSession::RecordServerSuccess(unsigned server_index, |
| 265 | bool is_doh_server) { |
| 266 | ServerStats* stats = GetServerStats(server_index, is_doh_server); |
| 267 | |
| 268 | // DoH queries can be sent using more than one URLRequestContext. A success |
| 269 | // from one URLRequestContext shouldn't zero out failures that may be |
| 270 | // consistently occurring for another URLRequestContext. |
| 271 | if (!is_doh_server) |
| 272 | stats->last_failure_count = 0; |
| 273 | stats->last_failure = base::Time(); |
| 274 | stats->last_success = base::Time::Now(); |
| 275 | } |
| 276 | |
| 277 | void DnsSession::SetProbeSuccess(unsigned doh_server_index, bool success) { |
| 278 | DCHECK_GE(doh_server_index, 0u); |
| 279 | DCHECK_LT(doh_server_index, config_.dns_over_https_servers.size()); |
| 280 | doh_server_stats_[doh_server_index].second = success; |
| 281 | } |
| 282 | |
| 283 | void DnsSession::RecordRTT(unsigned server_index, |
| 284 | bool is_doh_server, |
| 285 | bool is_probe, |
| 286 | base::TimeDelta rtt) { |
| 287 | ServerStats* stats = GetServerStats(server_index, is_doh_server); |
| 288 | // If the histogram has not yet been populated beyond the initial seed values |
| 289 | // and this was a probe query, replace the seed values with a multiple of |
| 290 | // the probe's RTT. |
| 291 | if (is_probe && stats->rtt_histogram->TotalCount() == kNumSeeds) { |
| 292 | DCHECK(is_doh_server); |
| 293 | doh_server_stats_[server_index].first = std::make_unique<ServerStats>( |
| 294 | rtt * kDohProbeTimeMultiplier, rtt_buckets_.Pointer()); |
| 295 | return; |
| 296 | } |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 297 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 298 | // Jacobson/Karels algorithm for TCP. |
| 299 | // Using parameters: alpha = 1/8, delta = 1/4, beta = 4 |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 300 | base::TimeDelta& estimate = stats->rtt_estimate; |
| 301 | base::TimeDelta& deviation = stats->rtt_deviation; |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 302 | base::TimeDelta current_error = rtt - estimate; |
| 303 | estimate += current_error / 8; // * alpha |
| 304 | base::TimeDelta abs_error = base::TimeDelta::FromInternalValue( |
| 305 | std::abs(current_error.ToInternalValue())); |
| 306 | deviation += (abs_error - deviation) / 4; // * delta |
| 307 | |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 308 | // RTT values shouldn't be less than 0, but it shouldn't cause a crash if |
| 309 | // they are anyway, so clip to 0. See https://crbug.com/753568. |
Miriam Gershenson | 68378c6 | 2017-08-10 22:26:25 | [diff] [blame] | 310 | int32_t rtt_ms = rtt.InMilliseconds(); |
| 311 | if (rtt_ms < 0) |
| 312 | rtt_ms = 0; |
| 313 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 314 | // Histogram-based method. |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 315 | stats->rtt_histogram->Accumulate( |
Miriam Gershenson | 68378c6 | 2017-08-10 22:26:25 | [diff] [blame] | 316 | static_cast<base::HistogramBase::Sample>(rtt_ms), 1); |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 317 | } |
| 318 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 319 | base::TimeDelta DnsSession::NextTimeout(unsigned server_index, int attempt) { |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 320 | return NextTimeoutHelper( |
| 321 | GetServerStats(server_index, false /* is _doh_server */), |
| 322 | attempt / config_.nameservers.size()); |
| 323 | } |
| 324 | |
| 325 | base::TimeDelta DnsSession::NextDohTimeout(unsigned doh_server_index) { |
| 326 | return NextTimeoutHelper( |
| 327 | GetServerStats(doh_server_index, true /* is _doh_server */), |
| 328 | 0 /* num_backoffs */); |
| 329 | } |
| 330 | |
| 331 | base::TimeDelta DnsSession::NextTimeoutHelper(ServerStats* server_stats, |
| 332 | int num_backoffs) { |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 333 | // Respect initial timeout (from config or field trial) if it exceeds max. |
| 334 | if (initial_timeout_ > max_timeout_) |
| 335 | return initial_timeout_; |
Paul Jensen | f359324 | 2018-12-04 19:15:18 | [diff] [blame] | 336 | |
Paul Jensen | f359324 | 2018-12-04 19:15:18 | [diff] [blame] | 337 | static_assert(std::numeric_limits<base::HistogramBase::Count>::is_signed, |
| 338 | "histogram base count assumed to be signed"); |
| 339 | |
| 340 | // Use fixed percentile of observed samples. |
dalyk | c2adf18 | 2019-09-02 14:31:22 | [diff] [blame^] | 341 | const base::SampleVector& samples = *server_stats->rtt_histogram; |
Paul Jensen | f359324 | 2018-12-04 19:15:18 | [diff] [blame] | 342 | |
| 343 | base::HistogramBase::Count total = samples.TotalCount(); |
| 344 | base::HistogramBase::Count remaining_count = kRTOPercentile * total / 100; |
| 345 | size_t index = 0; |
| 346 | while (remaining_count > 0 && index < rtt_buckets_.Get().size()) { |
| 347 | remaining_count -= samples.GetCountAtIndex(index); |
| 348 | ++index; |
| 349 | } |
| 350 | |
| 351 | base::TimeDelta timeout = |
| 352 | base::TimeDelta::FromMilliseconds(rtt_buckets_.Get().range(index)); |
| 353 | |
| 354 | timeout = std::max(timeout, base::TimeDelta::FromMilliseconds(kMinTimeoutMs)); |
| 355 | |
Paul Jensen | f359324 | 2018-12-04 19:15:18 | [diff] [blame] | 356 | return std::min(timeout * (1 << num_backoffs), max_timeout_); |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 357 | } |
| 358 | |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 359 | // Allocate a socket, already connected to the server address. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 360 | std::unique_ptr<DnsSession::SocketLease> DnsSession::AllocateSocket( |
| 361 | unsigned server_index, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 362 | const NetLogSource& source) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 363 | std::unique_ptr<DatagramClientSocket> socket; |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 364 | |
| 365 | socket = socket_pool_->AllocateSocket(server_index); |
[email protected] | dd946bb | 2013-06-12 22:53:01 | [diff] [blame] | 366 | if (!socket.get()) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 367 | return std::unique_ptr<SocketLease>(); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 368 | |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 369 | socket->NetLog().BeginEventReferencingSource(NetLogEventType::SOCKET_IN_USE, |
| 370 | source); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 371 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 372 | SocketLease* lease = new SocketLease(this, server_index, std::move(socket)); |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 373 | return std::unique_ptr<SocketLease>(lease); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 374 | } |
| 375 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 376 | std::unique_ptr<StreamSocket> DnsSession::CreateTCPSocket( |
| 377 | unsigned server_index, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 378 | const NetLogSource& source) { |
[email protected] | bdb6598 | 2012-12-20 20:44:59 | [diff] [blame] | 379 | return socket_pool_->CreateTCPSocket(server_index, source); |
| 380 | } |
| 381 | |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 382 | // Release a socket. |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 383 | void DnsSession::FreeSocket(unsigned server_index, |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 384 | std::unique_ptr<DatagramClientSocket> socket) { |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 385 | DCHECK(socket.get()); |
| 386 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 387 | socket->NetLog().EndEvent(NetLogEventType::SOCKET_IN_USE); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 388 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 389 | socket_pool_->FreeSocket(server_index, std::move(socket)); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 390 | } |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 391 | |
| 392 | } // namespace net |