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. |
| 38 | static const unsigned int kFailureThreshold; |
| 39 | |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 40 | // The number of milliseconds between ARP requests. Needed by Metrics. |
| 41 | static const unsigned int kTestPeriodMilliseconds; |
| 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. |
| 44 | static const char kDefaultLinkMonitorTechnologies[]; |
| 45 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 46 | LinkMonitor(const ConnectionRefPtr &connection, |
| 47 | EventDispatcher *dispatcher, |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 48 | Metrics *metrics, |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 49 | DeviceInfo *device_info, |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 50 | const FailureCallback &failure_callback); |
| 51 | virtual ~LinkMonitor(); |
| 52 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 53 | // Starts link-monitoring on the selected connection. Returns |
| 54 | // true if successful, false otherwise. |
| 55 | virtual bool Start(); |
| 56 | virtual void Stop(); |
| 57 | |
| 58 | // Return modified cumulative average of the gateway ARP response |
| 59 | // time. Returns zero if no samples are available. For each |
| 60 | // missed ARP response, the sample is assumed to be the full |
| 61 | // test period. |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 62 | virtual unsigned int GetResponseTimeMilliseconds() const; |
| 63 | |
| 64 | // Returns true if the LinkMonitor was ever able to find the default |
| 65 | // gateway via broadcast ARP. |
| 66 | virtual bool IsGatewayFound() const; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 67 | |
| 68 | private: |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 69 | friend class LinkMonitorForTest; |
| 70 | friend class LinkMonitorTest; |
| 71 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 72 | // The number of samples to compute a "strict" average over. When |
| 73 | // more samples than this number arrive, this determines how "slow" |
| 74 | // our simple low-pass filter works. |
| 75 | static const unsigned int kMaxResponseSampleFilterDepth; |
| 76 | |
| 77 | // Add a response time sample to the buffer. |
| 78 | void AddResponseTimeSample(unsigned int response_time_milliseconds); |
| 79 | // Create an ArpClient instance so we can receive and transmit ARP |
| 80 | // packets. This method is virtual so it can be overridden in |
| 81 | // unit tests. |
| 82 | virtual bool CreateClient(); |
| 83 | // Convert a hardware address byte-string to a colon-separated string. |
| 84 | static std::string HardwareAddressToString(const ByteString &address); |
| 85 | // Denote a missed response. Returns true if this loss has caused us |
| 86 | // to exceed the failure threshold. |
| 87 | bool AddMissedResponse(); |
| 88 | // This I/O callback is triggered whenever the ARP reception socket |
| 89 | // has data available to be received. |
| 90 | void ReceiveResponse(int fd); |
| 91 | // Send the next ARP request. Returns true if successful, false |
| 92 | // otherwise. |
| 93 | bool SendRequest(); |
| 94 | // Timer callback which calls SendRequest(). |
| 95 | void SendRequestTask(); |
| 96 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 97 | ConnectionRefPtr connection_; |
| 98 | EventDispatcher *dispatcher_; |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 99 | Metrics *metrics_; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 100 | DeviceInfo *device_info_; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 101 | FailureCallback failure_callback_; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 102 | ByteString local_mac_address_; |
| 103 | ByteString gateway_mac_address_; |
| 104 | scoped_ptr<ArpClient> arp_client_; |
| 105 | |
| 106 | // The number of consecutive times we have failed in receiving |
| 107 | // responses to broadcast ARP requests. |
| 108 | unsigned int broadcast_failure_count_; |
| 109 | // The number of consecutive times we have failed in receiving |
| 110 | // responses to unicast ARP requests. |
| 111 | unsigned int unicast_failure_count_; |
| 112 | |
| 113 | // Whether this iteration of the test was a unicast request |
| 114 | // to the gateway instead of broadcast. The link monitor |
| 115 | // alternates between unicast and broadcast requests so that |
| 116 | // both types of network traffic is monitored. |
| 117 | bool is_unicast_; |
| 118 | |
| 119 | // Maintain a pseudo-average of response time. |
| 120 | unsigned int response_sample_count_; |
| 121 | unsigned int response_sample_bucket_; |
| 122 | |
| 123 | scoped_ptr<IOHandler> receive_response_handler_; |
| 124 | // Callback method used for periodic transmission of ARP requests. |
| 125 | // When the timer expires this will call SendRequest() through the |
| 126 | // void callback function SendRequestTask(). |
| 127 | base::CancelableClosure send_request_callback_; |
| 128 | |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 129 | // The time at which the link monitor started. |
| 130 | struct timeval started_monitoring_at_; |
| 131 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 132 | // The time at which the last ARP request was sent. |
| 133 | struct timeval sent_request_at_; |
| 134 | Time *time_; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(LinkMonitor); |
| 137 | }; |
| 138 | |
| 139 | } // namespace shill |
| 140 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 141 | #endif // SHILL_LINK_MONITOR_H_ |