Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 5 | #ifndef SHILL_LINK_MONITOR_H_ |
| 6 | #define SHILL_LINK_MONITOR_H_ |
| 7 | |
| 8 | #include <time.h> |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 9 | |
| 10 | #include <base/callback.h> |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 11 | #include <base/cancelable_callback.h> |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 12 | #include <base/memory/scoped_ptr.h> |
| 13 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 14 | #include "shill/byte_string.h" |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 15 | #include "shill/refptr_types.h" |
| 16 | |
| 17 | namespace shill { |
| 18 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 19 | class ArpClient; |
| 20 | class DeviceInfo; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 21 | class EventDispatcher; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 22 | class IOHandler; |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 23 | class Metrics; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 24 | class Time; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 25 | |
| 26 | // LinkMonitor tracks the status of a connection by sending ARP |
| 27 | // messages to the default gateway for a connection. It keeps |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 28 | // track of response times which can be an indicator of link |
| 29 | // quality. It signals to caller that the link has failed if |
| 30 | // too many requests go unanswered. |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 31 | class LinkMonitor { |
| 32 | public: |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 33 | typedef base::Closure FailureCallback; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 34 | |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 35 | // When the sum of consecutive unicast and broadcast failures |
| 36 | // equals this value, the failure callback is called, the counters |
| 37 | // are reset, and the link monitoring quiesces. Needed by Metrics. |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 38 | static const int kFailureThreshold; |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 39 | |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 40 | // The default number of milliseconds between ARP requests. Needed by Metrics. |
| 41 | static const int kDefaultTestPeriodMilliseconds; |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 42 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 43 | // The default list of technologies for which link monitoring is enabled. |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 44 | // Needed by DefaultProfile. |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 45 | static const char kDefaultLinkMonitorTechnologies[]; |
| 46 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 47 | LinkMonitor(const ConnectionRefPtr &connection, |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 48 | EventDispatcher *dispatcher, // Owned by caller; can't be NULL. |
| 49 | Metrics *metrics, // Owned by caller; must not be NULL. |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 50 | DeviceInfo *device_info, |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 51 | const FailureCallback &failure_callback); |
| 52 | virtual ~LinkMonitor(); |
| 53 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 54 | // Starts link-monitoring on the selected connection. Returns |
| 55 | // true if successful, false otherwise. |
| 56 | virtual bool Start(); |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 57 | // Stop link-monitoring on the selected connection. Clears any |
| 58 | // accumulated statistics. |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 59 | virtual void Stop(); |
| 60 | |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 61 | // Inform LinkMonitor that the system is resuming from sleep. |
| 62 | // LinkMonitor will immediately probe the gateway, using a lower |
| 63 | // timeout than normal. |
| 64 | virtual void OnAfterResume(); |
| 65 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 66 | // Return modified cumulative average of the gateway ARP response |
| 67 | // time. Returns zero if no samples are available. For each |
| 68 | // missed ARP response, the sample is assumed to be the full |
| 69 | // test period. |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 70 | virtual int GetResponseTimeMilliseconds() const; |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 71 | |
| 72 | // Returns true if the LinkMonitor was ever able to find the default |
| 73 | // gateway via broadcast ARP. |
| 74 | virtual bool IsGatewayFound() const; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 75 | |
| 76 | private: |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 77 | friend class LinkMonitorForTest; |
| 78 | friend class LinkMonitorTest; |
| 79 | |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 80 | // The number of milliseconds between ARP requests when running a quick test. |
| 81 | // Needed by unit tests. |
| 82 | static const int kFastTestPeriodMilliseconds; |
| 83 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 84 | // The number of samples to compute a "strict" average over. When |
| 85 | // more samples than this number arrive, this determines how "slow" |
| 86 | // our simple low-pass filter works. |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 87 | static const int kMaxResponseSampleFilterDepth; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 88 | |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 89 | // Similar to Start, except that the initial probes use |
| 90 | // |probe_period_milliseconds|. After successfully probing with both |
| 91 | // broadcast and unicast ARPs (at least one of each), LinkMonitor |
| 92 | // switches itself to kDefaultTestPeriodMilliseconds. |
| 93 | virtual bool StartInternal(int probe_period_milliseconds); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 94 | // Add a response time sample to the buffer. |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 95 | void AddResponseTimeSample(int response_time_milliseconds); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 96 | // Create an ArpClient instance so we can receive and transmit ARP |
| 97 | // packets. This method is virtual so it can be overridden in |
| 98 | // unit tests. |
| 99 | virtual bool CreateClient(); |
| 100 | // Convert a hardware address byte-string to a colon-separated string. |
| 101 | static std::string HardwareAddressToString(const ByteString &address); |
| 102 | // Denote a missed response. Returns true if this loss has caused us |
| 103 | // to exceed the failure threshold. |
| 104 | bool AddMissedResponse(); |
| 105 | // This I/O callback is triggered whenever the ARP reception socket |
| 106 | // has data available to be received. |
| 107 | void ReceiveResponse(int fd); |
| 108 | // Send the next ARP request. Returns true if successful, false |
| 109 | // otherwise. |
| 110 | bool SendRequest(); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 111 | |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 112 | // The connection on which to perform link monitoring. |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 113 | ConnectionRefPtr connection_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 114 | // Dispatcher on which to create delayed tasks. |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 115 | EventDispatcher *dispatcher_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 116 | // Metrics instance on which to post performance results. |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 117 | Metrics *metrics_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 118 | // DeviceInfo instance for retrieving the MAC address of a device. |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 119 | DeviceInfo *device_info_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 120 | // Failure callback method to call if LinkMonitor fails. |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 121 | FailureCallback failure_callback_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 122 | // The MAC address of device associated with this connection. |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 123 | ByteString local_mac_address_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 124 | // The MAC address of the default gateway. |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 125 | ByteString gateway_mac_address_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 126 | // ArpClient instance used for performing link tests. |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 127 | scoped_ptr<ArpClient> arp_client_; |
| 128 | |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 129 | // How frequently we send an ARP request. This is also the timeout |
| 130 | // for a pending request. |
| 131 | int test_period_milliseconds_; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 132 | // The number of consecutive times we have failed in receiving |
| 133 | // responses to broadcast ARP requests. |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 134 | int broadcast_failure_count_; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 135 | // The number of consecutive times we have failed in receiving |
| 136 | // responses to unicast ARP requests. |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 137 | int unicast_failure_count_; |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 138 | // The number of consecutive times we have succeeded in receiving |
| 139 | // responses to broadcast ARP requests. |
| 140 | int broadcast_success_count_; |
| 141 | // The number of consecutive times we have succeeded in receiving |
| 142 | // responses to unicast ARP requests. |
| 143 | int unicast_success_count_; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 144 | |
| 145 | // Whether this iteration of the test was a unicast request |
| 146 | // to the gateway instead of broadcast. The link monitor |
| 147 | // alternates between unicast and broadcast requests so that |
| 148 | // both types of network traffic is monitored. |
| 149 | bool is_unicast_; |
| 150 | |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 151 | // Number of response samples received in our rolling averge. |
| 152 | int response_sample_count_; |
| 153 | // The sum of response samples in our rolling average. |
| 154 | int response_sample_bucket_; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 155 | |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 156 | // IOCallback that fires when the socket associated with our ArpClient |
| 157 | // has a packet to be received. Calls ReceiveResponse(). |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 158 | scoped_ptr<IOHandler> receive_response_handler_; |
| 159 | // Callback method used for periodic transmission of ARP requests. |
| 160 | // When the timer expires this will call SendRequest() through the |
| 161 | // void callback function SendRequestTask(). |
| 162 | base::CancelableClosure send_request_callback_; |
| 163 | |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 164 | // The time at which the link monitor started. |
| 165 | struct timeval started_monitoring_at_; |
| 166 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 167 | // The time at which the last ARP request was sent. |
| 168 | struct timeval sent_request_at_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 169 | // Time instance for performing GetTimeMonotonic(). |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 170 | Time *time_; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 171 | |
| 172 | DISALLOW_COPY_AND_ASSIGN(LinkMonitor); |
| 173 | }; |
| 174 | |
| 175 | } // namespace shill |
| 176 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 177 | #endif // SHILL_LINK_MONITOR_H_ |