Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS 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. |
| 4 | |
| 5 | #include "shill/connection_health_checker.h" |
| 6 | |
| 7 | #include <arpa/inet.h> |
| 8 | #include <netinet/in.h> |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 9 | #include <stdlib.h> |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 10 | #include <sys/socket.h> |
| 11 | #include <sys/types.h> |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 12 | #include <time.h> |
| 13 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
| 16 | #include <base/bind.h> |
| 17 | |
| 18 | #include "shill/async_connection.h" |
| 19 | #include "shill/connection.h" |
| 20 | #include "shill/dns_client.h" |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 21 | #include "shill/dns_client_factory.h" |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 22 | #include "shill/error.h" |
| 23 | #include "shill/http_url.h" |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 24 | #include "shill/ip_address_store.h" |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 25 | #include "shill/logging.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 26 | #include "shill/net/ip_address.h" |
| 27 | #include "shill/net/sockets.h" |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 28 | #include "shill/socket_info.h" |
| 29 | #include "shill/socket_info_reader.h" |
| 30 | |
| 31 | using base::Bind; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 32 | using base::Unretained; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 33 | using std::string; |
| 34 | using std::vector; |
| 35 | |
| 36 | namespace shill { |
| 37 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 38 | namespace Logging { |
| 39 | static auto kModuleLogScope = ScopeLogger::kConnection; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 40 | static string ObjectID(Connection* c) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 41 | return c->interface_name(); |
| 42 | } |
| 43 | } |
| 44 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 45 | // static |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 46 | const char* ConnectionHealthChecker::kDefaultRemoteIPPool[] = { |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 47 | "74.125.224.47", |
| 48 | "74.125.224.79", |
| 49 | "74.125.224.111", |
| 50 | "74.125.224.143" |
| 51 | }; |
| 52 | // static |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 53 | const int ConnectionHealthChecker::kDNSTimeoutMilliseconds = 5000; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 54 | // static |
| 55 | const int ConnectionHealthChecker::kInvalidSocket = -1; |
| 56 | // static |
| 57 | const int ConnectionHealthChecker::kMaxFailedConnectionAttempts = 2; |
| 58 | // static |
| 59 | const int ConnectionHealthChecker::kMaxSentDataPollingAttempts = 2; |
| 60 | // static |
| 61 | const int ConnectionHealthChecker::kMinCongestedQueueAttempts = 2; |
| 62 | // static |
| 63 | const int ConnectionHealthChecker::kMinSuccessfulSendAttempts = 1; |
| 64 | // static |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 65 | const int ConnectionHealthChecker::kNumDNSQueries = 5; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 66 | // static |
| 67 | const int ConnectionHealthChecker::kTCPStateUpdateWaitMilliseconds = 5000; |
| 68 | // static |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 69 | const uint16_t ConnectionHealthChecker::kRemotePort = 80; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 70 | |
| 71 | ConnectionHealthChecker::ConnectionHealthChecker( |
| 72 | ConnectionRefPtr connection, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 73 | EventDispatcher* dispatcher, |
| 74 | IPAddressStore* remote_ips, |
| 75 | const base::Callback<void(Result)>& result_callback) |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 76 | : connection_(connection), |
| 77 | dispatcher_(dispatcher), |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 78 | remote_ips_(remote_ips), |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 79 | result_callback_(result_callback), |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 80 | socket_(new Sockets()), |
| 81 | weak_ptr_factory_(this), |
| 82 | connection_complete_callback_( |
| 83 | Bind(&ConnectionHealthChecker::OnConnectionComplete, |
| 84 | weak_ptr_factory_.GetWeakPtr())), |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 85 | tcp_connection_(new AsyncConnection(connection_->interface_name(), |
| 86 | dispatcher_, |
| 87 | socket_.get(), |
| 88 | connection_complete_callback_)), |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 89 | report_result_( |
| 90 | Bind(&ConnectionHealthChecker::ReportResult, |
| 91 | weak_ptr_factory_.GetWeakPtr())), |
| 92 | sock_fd_(kInvalidSocket), |
| 93 | socket_info_reader_(new SocketInfoReader()), |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 94 | dns_client_factory_(DNSClientFactory::GetInstance()), |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 95 | dns_client_callback_(Bind(&ConnectionHealthChecker::GetDNSResult, |
| 96 | weak_ptr_factory_.GetWeakPtr())), |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 97 | health_check_in_progress_(false), |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 98 | num_connection_failures_(0), |
| 99 | num_congested_queue_detected_(0), |
Prathmesh Prabhu | 81404c6 | 2013-05-08 17:04:28 -0700 | [diff] [blame] | 100 | num_successful_sends_(0), |
| 101 | tcp_state_update_wait_milliseconds_(kTCPStateUpdateWaitMilliseconds) { |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 102 | for (size_t i = 0; i < arraysize(kDefaultRemoteIPPool); ++i) { |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 103 | const char* ip_string = kDefaultRemoteIPPool[i]; |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 104 | IPAddress ip(IPAddress::kFamilyIPv4); |
| 105 | ip.SetAddressFromString(ip_string); |
| 106 | remote_ips_->AddUnique(ip); |
| 107 | } |
| 108 | } |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 109 | |
| 110 | ConnectionHealthChecker::~ConnectionHealthChecker() { |
| 111 | Stop(); |
| 112 | } |
| 113 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 114 | bool ConnectionHealthChecker::health_check_in_progress() const { |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 115 | return health_check_in_progress_; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 118 | void ConnectionHealthChecker::AddRemoteIP(IPAddress ip) { |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 119 | remote_ips_->AddUnique(ip); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 122 | void ConnectionHealthChecker::AddRemoteURL(const string& url_string) { |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 123 | GarbageCollectDNSClients(); |
| 124 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 125 | HTTPURL url; |
| 126 | if (!url.ParseFromString(url_string)) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 127 | SLOG(connection_.get(), 2) << __func__ << ": Malformed url: " |
| 128 | << url_string << "."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | if (url.port() != kRemotePort) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 132 | SLOG(connection_.get(), 2) << __func__ |
| 133 | << ": Remote connections only supported " |
| 134 | << " to port 80, requested " << url.port() |
| 135 | << "."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 136 | return; |
| 137 | } |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 138 | for (int i = 0; i < kNumDNSQueries; ++i) { |
| 139 | Error error; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 140 | DNSClient* dns_client = |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 141 | dns_client_factory_->CreateDNSClient(IPAddress::kFamilyIPv4, |
| 142 | connection_->interface_name(), |
| 143 | connection_->dns_servers(), |
| 144 | kDNSTimeoutMilliseconds, |
| 145 | dispatcher_, |
| 146 | dns_client_callback_); |
| 147 | dns_clients_.push_back(dns_client); |
| 148 | if (!dns_clients_[i]->Start(url.host(), &error)) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 149 | SLOG(connection_.get(), 2) << __func__ << ": Failed to start DNS client " |
| 150 | << "(query #" << i << "): " |
| 151 | << error.message(); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 152 | } |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | void ConnectionHealthChecker::Start() { |
| 157 | if (health_check_in_progress_) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 158 | SLOG(connection_.get(), 2) << __func__ |
| 159 | << ": Health Check already in progress."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | if (!connection_.get()) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 163 | SLOG(connection_.get(), 2) << __func__ << ": Connection not ready yet."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 164 | result_callback_.Run(kResultUnknown); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | health_check_in_progress_ = true; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 169 | num_connection_failures_ = 0; |
| 170 | num_congested_queue_detected_ = 0; |
| 171 | num_successful_sends_ = 0; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 172 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 173 | if (remote_ips_->Empty()) { |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 174 | // Nothing to try. |
| 175 | Stop(); |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 176 | SLOG(connection_.get(), 2) << __func__ << ": Not enough IPs."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 177 | result_callback_.Run(kResultUnknown); |
| 178 | return; |
| 179 | } |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 180 | |
| 181 | // Initiate the first attempt. |
| 182 | NextHealthCheckSample(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void ConnectionHealthChecker::Stop() { |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 186 | if (tcp_connection_.get() != nullptr) |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 187 | tcp_connection_->Stop(); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 188 | verify_sent_data_callback_.Cancel(); |
| 189 | ClearSocketDescriptor(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 190 | health_check_in_progress_ = false; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 191 | num_connection_failures_ = 0; |
| 192 | num_congested_queue_detected_ = 0; |
| 193 | num_successful_sends_ = 0; |
| 194 | num_tx_queue_polling_attempts_ = 0; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 197 | void ConnectionHealthChecker::SetConnection(ConnectionRefPtr connection) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 198 | SLOG(connection_.get(), 3) << __func__; |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 199 | connection_ = connection; |
| 200 | tcp_connection_.reset(new AsyncConnection(connection_->interface_name(), |
| 201 | dispatcher_, |
| 202 | socket_.get(), |
| 203 | connection_complete_callback_)); |
| 204 | dns_clients_.clear(); |
| 205 | bool restart = health_check_in_progress(); |
| 206 | Stop(); |
| 207 | if (restart) |
| 208 | Start(); |
| 209 | } |
| 210 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 211 | const char* ConnectionHealthChecker::ResultToString( |
Prathmesh Prabhu | 5489b7a | 2013-04-10 13:33:59 -0700 | [diff] [blame] | 212 | ConnectionHealthChecker::Result result) { |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 213 | switch (result) { |
Prathmesh Prabhu | 5489b7a | 2013-04-10 13:33:59 -0700 | [diff] [blame] | 214 | case kResultUnknown: |
| 215 | return "Unknown"; |
Prathmesh Prabhu | 5489b7a | 2013-04-10 13:33:59 -0700 | [diff] [blame] | 216 | case kResultConnectionFailure: |
| 217 | return "ConnectionFailure"; |
Prathmesh Prabhu | 5489b7a | 2013-04-10 13:33:59 -0700 | [diff] [blame] | 218 | case kResultCongestedTxQueue: |
| 219 | return "CongestedTxQueue"; |
| 220 | case kResultSuccess: |
| 221 | return "Success"; |
| 222 | default: |
| 223 | return "Invalid"; |
| 224 | } |
| 225 | } |
| 226 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 227 | void ConnectionHealthChecker::GetDNSResult(const Error& error, |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 228 | const IPAddress& ip) { |
| 229 | if (!error.IsSuccess()) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 230 | SLOG(connection_.get(), 2) << __func__ << "DNSClient returned failure: " |
| 231 | << error.message(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 232 | return; |
| 233 | } |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 234 | remote_ips_->AddUnique(ip); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 237 | void ConnectionHealthChecker::GarbageCollectDNSClients() { |
| 238 | ScopedVector<DNSClient> keep; |
| 239 | ScopedVector<DNSClient> discard; |
| 240 | for (size_t i = 0; i < dns_clients_.size(); ++i) { |
| 241 | if (dns_clients_[i]->IsActive()) |
| 242 | keep.push_back(dns_clients_[i]); |
| 243 | else |
| 244 | discard.push_back(dns_clients_[i]); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 245 | } |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 246 | dns_clients_.weak_clear(); |
| 247 | dns_clients_ = keep.Pass(); // Passes ownership of contents. |
| 248 | discard.clear(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 251 | void ConnectionHealthChecker::NextHealthCheckSample() { |
| 252 | // Finish conditions: |
| 253 | if (num_connection_failures_ == kMaxFailedConnectionAttempts) { |
| 254 | health_check_result_ = kResultConnectionFailure; |
| 255 | dispatcher_->PostTask(report_result_); |
| 256 | return; |
| 257 | } |
| 258 | if (num_congested_queue_detected_ == kMinCongestedQueueAttempts) { |
| 259 | health_check_result_ = kResultCongestedTxQueue; |
| 260 | dispatcher_->PostTask(report_result_); |
| 261 | return; |
| 262 | } |
| 263 | if (num_successful_sends_ == kMinSuccessfulSendAttempts) { |
| 264 | health_check_result_ = kResultSuccess; |
| 265 | dispatcher_->PostTask(report_result_); |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | // Pick a random IP from the set of IPs. |
| 270 | // This guards against |
| 271 | // (1) Repeated failed attempts for the same IP at start-up everytime. |
| 272 | // (2) All users attempting to connect to the same IP. |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 273 | IPAddress ip = remote_ips_->GetRandomIP(); |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 274 | SLOG(connection_.get(), 3) << __func__ << ": Starting connection at " |
| 275 | << ip.ToString(); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 276 | if (!tcp_connection_->Start(ip, kRemotePort)) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 277 | SLOG(connection_.get(), 2) << __func__ << ": Connection attempt failed."; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 278 | ++num_connection_failures_; |
| 279 | NextHealthCheckSample(); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void ConnectionHealthChecker::OnConnectionComplete(bool success, int sock_fd) { |
| 284 | if (!success) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 285 | SLOG(connection_.get(), 2) << __func__ |
| 286 | << ": AsyncConnection connection attempt failed " |
| 287 | << "with error: " |
| 288 | << tcp_connection_->error(); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 289 | ++num_connection_failures_; |
| 290 | NextHealthCheckSample(); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | SetSocketDescriptor(sock_fd); |
| 295 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 296 | SocketInfo sock_info; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 297 | if (!GetSocketInfo(sock_fd_, &sock_info) || |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 298 | sock_info.connection_state() != |
| 299 | SocketInfo::kConnectionStateEstablished) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 300 | SLOG(connection_.get(), 2) << __func__ |
| 301 | << ": Connection originally not in established " |
| 302 | "state."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 303 | // Count this as a failed connection attempt. |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 304 | ++num_connection_failures_; |
| 305 | ClearSocketDescriptor(); |
| 306 | NextHealthCheckSample(); |
| 307 | return; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 308 | } |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 309 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 310 | old_transmit_queue_value_ = sock_info.transmit_queue_value(); |
| 311 | num_tx_queue_polling_attempts_ = 0; |
| 312 | |
| 313 | // Send data on the connection and post a delayed task to check successful |
| 314 | // transfer. |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 315 | char buf; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 316 | if (socket_->Send(sock_fd_, &buf, sizeof(buf), 0) == -1) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 317 | SLOG(connection_.get(), 2) << __func__ << ": " << socket_->ErrorString(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 318 | // Count this as a failed connection attempt. |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 319 | ++num_connection_failures_; |
| 320 | ClearSocketDescriptor(); |
| 321 | NextHealthCheckSample(); |
| 322 | return; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 325 | verify_sent_data_callback_.Reset( |
| 326 | Bind(&ConnectionHealthChecker::VerifySentData, Unretained(this))); |
| 327 | dispatcher_->PostDelayedTask(verify_sent_data_callback_.callback(), |
Prathmesh Prabhu | 81404c6 | 2013-05-08 17:04:28 -0700 | [diff] [blame] | 328 | tcp_state_update_wait_milliseconds_); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 331 | void ConnectionHealthChecker::VerifySentData() { |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 332 | SocketInfo sock_info; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 333 | bool sock_info_found = GetSocketInfo(sock_fd_, &sock_info); |
| 334 | // Acceptable TCP connection states after sending the data: |
| 335 | // kConnectionStateEstablished: No change in connection state since the send. |
| 336 | // kConnectionStateCloseWait: The remote host recieved the sent data and |
| 337 | // requested connection close. |
| 338 | if (!sock_info_found || |
| 339 | (sock_info.connection_state() != |
| 340 | SocketInfo::kConnectionStateEstablished && |
| 341 | sock_info.connection_state() != |
| 342 | SocketInfo::kConnectionStateCloseWait)) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 343 | SLOG(connection_.get(), 2) |
| 344 | << __func__ << ": Connection not in acceptable state after send."; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 345 | if (sock_info_found) |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 346 | SLOG(connection_.get(), 3) << "Found socket info but in state: " |
| 347 | << sock_info.connection_state(); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 348 | ++num_connection_failures_; |
| 349 | } else if (sock_info.transmit_queue_value() > old_transmit_queue_value_ && |
| 350 | sock_info.timer_state() == |
| 351 | SocketInfo::kTimerStateRetransmitTimerPending) { |
| 352 | if (num_tx_queue_polling_attempts_ < kMaxSentDataPollingAttempts) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 353 | SLOG(connection_.get(), 2) << __func__ |
| 354 | << ": Polling again."; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 355 | ++num_tx_queue_polling_attempts_; |
| 356 | verify_sent_data_callback_.Reset( |
| 357 | Bind(&ConnectionHealthChecker::VerifySentData, Unretained(this))); |
| 358 | dispatcher_->PostDelayedTask(verify_sent_data_callback_.callback(), |
Prathmesh Prabhu | 81404c6 | 2013-05-08 17:04:28 -0700 | [diff] [blame] | 359 | tcp_state_update_wait_milliseconds_); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 360 | return; |
| 361 | } |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 362 | SLOG(connection_.get(), 2) << __func__ << ": Sampled congested Tx-Queue"; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 363 | ++num_congested_queue_detected_; |
| 364 | } else { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 365 | SLOG(connection_.get(), 2) << __func__ << ": Sampled successful send."; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 366 | ++num_successful_sends_; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 367 | } |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 368 | ClearSocketDescriptor(); |
| 369 | NextHealthCheckSample(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 372 | // TODO(pprabhu): Scrub IP address logging. |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 373 | bool ConnectionHealthChecker::GetSocketInfo(int sock_fd, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 374 | SocketInfo* sock_info) { |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 375 | struct sockaddr_storage addr; |
| 376 | socklen_t addrlen = sizeof(addr); |
| 377 | memset(&addr, 0, sizeof(addr)); |
| 378 | if (socket_->GetSockName(sock_fd, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 379 | reinterpret_cast<struct sockaddr*>(&addr), |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 380 | &addrlen) != 0) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 381 | SLOG(connection_.get(), 2) << __func__ |
| 382 | << ": Failed to get address of created socket."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 383 | return false; |
| 384 | } |
| 385 | if (addr.ss_family != AF_INET) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 386 | SLOG(connection_.get(), 2) << __func__ << ": IPv6 socket address found."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 387 | return false; |
| 388 | } |
| 389 | |
| 390 | CHECK_EQ(sizeof(struct sockaddr_in), addrlen); |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 391 | struct sockaddr_in* addr_in = reinterpret_cast<sockaddr_in*>(&addr); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 392 | uint16_t local_port = ntohs(addr_in->sin_port); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 393 | char ipstr[INET_ADDRSTRLEN]; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 394 | const char* res = inet_ntop(AF_INET, &addr_in->sin_addr, |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 395 | ipstr, sizeof(ipstr)); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 396 | if (res == nullptr) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 397 | SLOG(connection_.get(), 2) << __func__ |
| 398 | << ": Could not convert IP address to string."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | |
| 402 | IPAddress local_ip_address(IPAddress::kFamilyIPv4); |
| 403 | CHECK(local_ip_address.SetAddressFromString(ipstr)); |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 404 | SLOG(connection_.get(), 3) << "Local IP = " << local_ip_address.ToString() |
| 405 | << ":" << local_port; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 406 | |
| 407 | vector<SocketInfo> info_list; |
| 408 | if (!socket_info_reader_->LoadTcpSocketInfo(&info_list)) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 409 | SLOG(connection_.get(), 2) << __func__ |
| 410 | << ": Failed to load TCP socket info."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 411 | return false; |
| 412 | } |
| 413 | |
| 414 | for (vector<SocketInfo>::const_iterator info_list_it = info_list.begin(); |
| 415 | info_list_it != info_list.end(); |
| 416 | ++info_list_it) { |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 417 | const SocketInfo& cur_sock_info = *info_list_it; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 418 | |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 419 | SLOG(connection_.get(), 4) |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 420 | << "Testing against IP = " |
| 421 | << cur_sock_info.local_ip_address().ToString() |
| 422 | << ":" << cur_sock_info.local_port() |
| 423 | << " (addresses equal:" |
| 424 | << cur_sock_info.local_ip_address().Equals(local_ip_address) |
| 425 | << ", ports equal:" << (cur_sock_info.local_port() == local_port) |
| 426 | << ")"; |
| 427 | |
| 428 | if (cur_sock_info.local_ip_address().Equals(local_ip_address) && |
| 429 | cur_sock_info.local_port() == local_port) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 430 | SLOG(connection_.get(), 3) << __func__ |
| 431 | << ": Found matching TCP socket info."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 432 | *sock_info = cur_sock_info; |
| 433 | return true; |
| 434 | } |
| 435 | } |
| 436 | |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 437 | SLOG(connection_.get(), 2) << __func__ << ": No matching TCP socket info."; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 438 | return false; |
| 439 | } |
| 440 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 441 | void ConnectionHealthChecker::ReportResult() { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 442 | SLOG(connection_.get(), 2) << __func__ << ": Result: " |
| 443 | << ResultToString(health_check_result_); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 444 | Stop(); |
| 445 | result_callback_.Run(health_check_result_); |
| 446 | } |
| 447 | |
| 448 | void ConnectionHealthChecker::SetSocketDescriptor(int sock_fd) { |
| 449 | if (sock_fd_ != kInvalidSocket) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 450 | SLOG(connection_.get(), 4) << "Closing socket"; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 451 | socket_->Close(sock_fd_); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 452 | } |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 453 | sock_fd_ = sock_fd; |
| 454 | } |
| 455 | |
| 456 | void ConnectionHealthChecker::ClearSocketDescriptor() { |
| 457 | SetSocketDescriptor(kInvalidSocket); |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 460 | } // namespace shill |