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 | #ifndef SHILL_CONNECTION_HEALTH_CHECKER_H_ |
| 6 | #define SHILL_CONNECTION_HEALTH_CHECKER_H_ |
| 7 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 8 | #include <string> |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 9 | #include <vector> |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 10 | |
| 11 | #include <base/basictypes.h> |
| 12 | #include <base/callback.h> |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 13 | #include <base/cancelable_callback.h> |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 14 | #include <base/memory/scoped_ptr.h> |
| 15 | #include <base/memory/scoped_vector.h> |
| 16 | #include <base/memory/weak_ptr.h> |
| 17 | #include <gtest/gtest_prod.h> |
| 18 | |
| 19 | #include "shill/refptr_types.h" |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 20 | #include "shill/socket_info.h" |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame^] | 21 | #include "shill/sockets.h" |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 22 | |
| 23 | namespace shill { |
| 24 | |
| 25 | class AsyncConnection; |
| 26 | class DNSClient; |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 27 | class DNSClientFactory; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 28 | class Error; |
| 29 | class EventDispatcher; |
| 30 | class IPAddress; |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 31 | class IPAddressStore; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 32 | class SocketInfoReader; |
| 33 | |
| 34 | // The ConnectionHealthChecker class implements the facilities to test |
| 35 | // connectivity status on some connection asynchronously. |
| 36 | // In particular, the class can distinguish between three states of the |
| 37 | // connection: |
| 38 | // -(1)- No connectivity (TCP connection can not be established) |
| 39 | // -(2)- Partial connectivity (TCP connection can be established, but no data |
| 40 | // transfer) |
| 41 | // -(3)- Connectivity OK (TCP connection established, is healthy) |
| 42 | class ConnectionHealthChecker { |
| 43 | public: |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 44 | enum Result { |
| 45 | // There was some problem in the setup of ConnctionHealthChecker. |
| 46 | // Could not attempt a tcp connection. |
| 47 | kResultUnknown, |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 48 | // Failed to create TCP connection. Condition -(1)-. |
| 49 | kResultConnectionFailure, |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 50 | // Failed to send data on TCP connection. Condition -(2)-. |
| 51 | kResultCongestedTxQueue, |
| 52 | // Condition -(3)-. |
| 53 | kResultSuccess |
| 54 | }; |
| 55 | |
| 56 | ConnectionHealthChecker(ConnectionRefPtr connection, |
| 57 | EventDispatcher *dispatcher, |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 58 | IPAddressStore *remote_ips, |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 59 | const base::Callback<void(Result)> &result_callback); |
| 60 | virtual ~ConnectionHealthChecker(); |
| 61 | |
| 62 | // A new ConnectionHealthChecker is created with a default URL to attempt the |
| 63 | // TCP connection with. Add a URL to try. |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 64 | virtual void AddRemoteURL(const std::string &url_string); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 65 | |
| 66 | // Name resolution can fail in conditions -(1)- and -(2)-. Add an IP address |
| 67 | // to attempt the TCP connection with. |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 68 | virtual void AddRemoteIP(IPAddress ip); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 69 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 70 | // Change the associated Connection on the Device. |
| 71 | // This will restart any ongoing health check. Any ongoing DNS query will be |
| 72 | // dropped (not restarted). |
| 73 | virtual void SetConnection(ConnectionRefPtr connection); |
| 74 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 75 | // Start a connection health check. The health check involves one or more |
| 76 | // attempts at establishing and using a TCP connection. |result_callback_| is |
| 77 | // called with the final result of the check. |result_callback_| will always |
| 78 | // be called after a call to Start() unless Stop() is called in the meantime. |
| 79 | // |result_callback_| may be called before Start() completes. |
| 80 | // |
| 81 | // Calling Start() while a health check is in progress is a no-op. |
| 82 | virtual void Start(); |
| 83 | |
| 84 | // Stop the current health check. No callback is called as a side effect of |
| 85 | // this function. |
| 86 | // |
| 87 | // Calling Stop() on a Stop()ed health check is a no-op. |
| 88 | virtual void Stop(); |
| 89 | |
Prathmesh Prabhu | 5489b7a | 2013-04-10 13:33:59 -0700 | [diff] [blame] | 90 | static const char *ResultToString(Result result); |
| 91 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 92 | // Accessors. |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 93 | const IPAddressStore *remote_ips() const { return remote_ips_; } |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 94 | virtual bool health_check_in_progress() const; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 95 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 96 | protected: |
| 97 | // For unit-tests. |
| 98 | void set_dispatcher(EventDispatcher *dispatcher) { |
| 99 | dispatcher_ = dispatcher; |
| 100 | } |
| 101 | void set_sock_fd(int sock_fd) { sock_fd_ = sock_fd; } |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 102 | int16_t num_connection_failures() const { return num_connection_failures_; } |
| 103 | void set_num_connection_failures(int16_t val) { |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 104 | num_connection_failures_ = val; |
| 105 | } |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 106 | int16_t num_tx_queue_polling_attempts() const { |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 107 | return num_tx_queue_polling_attempts_; |
| 108 | } |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 109 | void set_num_tx_queue_polling_attempts(int16_t val) { |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 110 | num_tx_queue_polling_attempts_ = val; |
| 111 | } |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 112 | int16_t num_congested_queue_detected() const { |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 113 | return num_congested_queue_detected_; |
| 114 | } |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 115 | void set_num_congested_queue_detected(int16_t val) { |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 116 | num_congested_queue_detected_ = val; |
| 117 | } |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 118 | int16_t num_successful_sends() const { return num_successful_sends_; } |
| 119 | void set_num_successful_sends(int16_t val) { |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 120 | num_successful_sends_ = val; |
| 121 | } |
| 122 | void set_old_transmit_queue_value(uint64 val) { |
| 123 | old_transmit_queue_value_ = val; |
| 124 | } |
| 125 | Result health_check_result() const { return health_check_result_; } |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 126 | AsyncConnection *tcp_connection() const { return tcp_connection_.get(); } |
| 127 | Connection *connection() const { return connection_.get(); } |
| 128 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 129 | private: |
| 130 | friend class ConnectionHealthCheckerTest; |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 131 | FRIEND_TEST(ConnectionHealthCheckerTest, GarbageCollectDNSClients); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 132 | FRIEND_TEST(ConnectionHealthCheckerTest, GetSocketInfo); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 133 | FRIEND_TEST(ConnectionHealthCheckerTest, NextHealthCheckSample); |
| 134 | FRIEND_TEST(ConnectionHealthCheckerTest, OnConnectionComplete); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 135 | FRIEND_TEST(ConnectionHealthCheckerTest, SetConnection); |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 136 | FRIEND_TEST(ConnectionHealthCheckerTest, VerifySentData); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 137 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 138 | // List of static IPs for connection health check. |
| 139 | static const char *kDefaultRemoteIPPool[]; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 140 | // Time to wait for DNS server. |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 141 | static const int kDNSTimeoutMilliseconds; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 142 | static const int kInvalidSocket; |
| 143 | // After |kMaxFailedConnectionAttempts| failed attempts to connect, give up |
| 144 | // health check and return failure. |
| 145 | static const int kMaxFailedConnectionAttempts; |
| 146 | // After sending a small amount of data, attempt |kMaxSentDataPollingAttempts| |
| 147 | // times to see if the data was sent successfully. |
| 148 | static const int kMaxSentDataPollingAttempts; |
| 149 | // After |kMinCongestedQueueAttempts| to send data indicate a congested tx |
| 150 | // queue, finish health check and report a congested queue. |
| 151 | static const int kMinCongestedQueueAttempts; |
| 152 | // After sending data |kMinSuccessfulAttempts| times succesfully, finish |
| 153 | // health check and report a healthy connection. |
| 154 | static const int kMinSuccessfulSendAttempts; |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 155 | // Number of DNS queries to be spawned when a new remote URL is added. |
| 156 | static const int kNumDNSQueries; |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 157 | static const uint16 kRemotePort; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 158 | // Time to wait before testing successful data transfer / disconnect after |
| 159 | // request is made on the device. |
| 160 | static const int kTCPStateUpdateWaitMilliseconds; |
| 161 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 162 | // Callback for DnsClient |
| 163 | void GetDNSResult(const Error &error, const IPAddress &ip); |
| 164 | void GarbageCollectDNSClients(); |
| 165 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 166 | // Start a new AsyncConnection with callback set to OnConnectionComplete(). |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 167 | void NextHealthCheckSample(); |
| 168 | void ReportResult(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 169 | |
| 170 | // Callback for AsyncConnection. |
| 171 | // Observe the setup connection to test health state |
| 172 | void OnConnectionComplete(bool success, int sock_fd); |
| 173 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 174 | void VerifySentData(); |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 175 | bool GetSocketInfo(int sock_fd, SocketInfo *sock_info); |
| 176 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 177 | void SetSocketDescriptor(int sock_fd); |
| 178 | void ClearSocketDescriptor(); |
| 179 | |
| 180 | // The connection on which the health check is being run. |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 181 | ConnectionRefPtr connection_; |
| 182 | EventDispatcher *dispatcher_; |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 183 | // Set of IPs to create TCP connection with for the health check. |
| 184 | IPAddressStore *remote_ips_; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 185 | base::Callback<void(Result)> result_callback_; |
| 186 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 187 | scoped_ptr<Sockets> socket_; |
| 188 | base::WeakPtrFactory<ConnectionHealthChecker> weak_ptr_factory_; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 189 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 190 | // Callback passed to |tcp_connection_| to report an established TCP |
| 191 | // connection. |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 192 | const base::Callback<void(bool, int)> connection_complete_callback_; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 193 | // Active TCP connection during health check. |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 194 | scoped_ptr<AsyncConnection> tcp_connection_; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 195 | const base::Callback<void(void)> report_result_; |
| 196 | // Active socket for |tcp_connection_| during an active health check. |
| 197 | int sock_fd_; |
| 198 | // Interface to read TCP connection information from the system. |
| 199 | scoped_ptr<SocketInfoReader> socket_info_reader_; |
| 200 | |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 201 | DNSClientFactory *dns_client_factory_; |
| 202 | ScopedVector<DNSClient> dns_clients_; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 203 | const base::Callback<void(const Error&, const IPAddress&)> |
| 204 | dns_client_callback_; |
| 205 | |
| 206 | // Store the old value of the transmit queue to verify that data sent on the |
| 207 | // connection is actually transmitted. |
| 208 | uint64 old_transmit_queue_value_; |
| 209 | // Callback to post a delayed check on whether data sent on the TCP connection |
| 210 | // was successfully transmitted. |
| 211 | base::CancelableClosure verify_sent_data_callback_; |
| 212 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 213 | bool health_check_in_progress_; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 214 | // Number of connection failures in currently active health check. |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 215 | int16_t num_connection_failures_; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 216 | // Number of times we have checked the tx-queue for the current send attempt. |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 217 | int16_t num_tx_queue_polling_attempts_; |
| 218 | // Number of out of credit scenarios detected in current health check. |
| 219 | int16_t num_congested_queue_detected_; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 220 | // Number of successful send attempts currently active health check. |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 221 | int16_t num_successful_sends_; |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 222 | |
Prathmesh Prabhu | 81404c6 | 2013-05-08 17:04:28 -0700 | [diff] [blame] | 223 | // Snooze time while polling for updated /proc/tcpinfo |
| 224 | int tcp_state_update_wait_milliseconds_; |
| 225 | |
Prathmesh Prabhu | ade9b9a | 2013-04-22 18:01:19 -0700 | [diff] [blame] | 226 | // Temporarily store the result of health check so that |report_result_| |
| 227 | // can report it. |
| 228 | Result health_check_result_; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 229 | |
| 230 | DISALLOW_COPY_AND_ASSIGN(ConnectionHealthChecker); |
| 231 | }; |
| 232 | |
| 233 | } // namespace shill |
| 234 | |
| 235 | #endif // SHILL_CONNECTION_HEALTH_CHECKER_H_ |