Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 16 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 17 | #ifndef SHILL_LINK_MONITOR_H_ |
| 18 | #define SHILL_LINK_MONITOR_H_ |
| 19 | |
| 20 | #include <time.h> |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 21 | |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 22 | #include <memory> |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 25 | #include <base/callback.h> |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 26 | #include <base/cancelable_callback.h> |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 27 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 28 | #include "shill/metrics.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 29 | #include "shill/net/byte_string.h" |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 30 | #include "shill/refptr_types.h" |
| 31 | |
| 32 | namespace shill { |
| 33 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 34 | class ActiveLinkMonitor; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 35 | class DeviceInfo; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 36 | class EventDispatcher; |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 37 | class PassiveLinkMonitor; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 38 | class Time; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 39 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 40 | class LinkMonitor { |
| 41 | public: |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 42 | typedef base::Closure FailureCallback; |
Peter Qiu | b5d124f | 2014-04-14 12:05:02 -0700 | [diff] [blame] | 43 | typedef base::Closure GatewayChangeCallback; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 44 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 45 | // The default number of milliseconds between ARP requests used by |
| 46 | // ActiveLinkMonitor. Needed by Metrics. |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 47 | static const int kDefaultTestPeriodMilliseconds; |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 48 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 49 | // The default list of technologies for which link monitoring is enabled. |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 50 | // Needed by DefaultProfile. |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 51 | static const char kDefaultLinkMonitorTechnologies[]; |
| 52 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 53 | // Failure threshold count used by ActiveLinkMonitor. Needed by Metrics. |
Paul Stewart | b434ce5 | 2013-09-23 13:53:49 -0700 | [diff] [blame] | 54 | static const int kFailureThreshold; |
| 55 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 56 | LinkMonitor(const ConnectionRefPtr& connection, |
| 57 | EventDispatcher* dispatcher, // Owned by caller; can't be NULL. |
| 58 | Metrics* metrics, // Owned by caller; must not be NULL. |
| 59 | DeviceInfo* device_info, |
| 60 | const FailureCallback& failure_callback, |
| 61 | const GatewayChangeCallback& gateway_change_callback); |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 62 | virtual ~LinkMonitor(); |
| 63 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 64 | // Starts link-monitoring on the selected connection. Returns |
| 65 | // true if successful, false otherwise. |
| 66 | virtual bool Start(); |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 67 | // Stop link-monitoring on the selected connection. Clears any |
| 68 | // accumulated statistics. |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 69 | virtual void Stop(); |
| 70 | |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 71 | // Inform LinkMonitor that the system is resuming from sleep. |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 72 | // LinkMonitor will immediately start the ActiveLinkMonitor, using a lower |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 73 | // timeout than normal. |
| 74 | virtual void OnAfterResume(); |
| 75 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 76 | // Return modified cumulative average of the gateway ARP response |
| 77 | // time. Returns zero if no samples are available. For each |
| 78 | // missed ARP response, the sample is assumed to be the full |
| 79 | // test period. |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 80 | virtual int GetResponseTimeMilliseconds() const; |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 81 | |
| 82 | // Returns true if the LinkMonitor was ever able to find the default |
| 83 | // gateway via broadcast ARP. |
| 84 | virtual bool IsGatewayFound() const; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 85 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 86 | const ByteString& gateway_mac_address() const { |
Peter Qiu | b5d124f | 2014-04-14 12:05:02 -0700 | [diff] [blame] | 87 | return gateway_mac_address_; |
| 88 | } |
| 89 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 90 | private: |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 91 | friend class LinkMonitorTest; |
| 92 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 93 | void OnActiveLinkMonitorFailure(Metrics::LinkMonitorFailure failure, |
| 94 | int broadcast_failure_count, |
| 95 | int unicast_failure_count); |
| 96 | void OnActiveLinkMonitorSuccess(); |
| 97 | void OnPassiveLinkMonitorResultCallback(bool status); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 98 | |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 99 | // The connection on which to perform link monitoring. |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 100 | ConnectionRefPtr connection_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 101 | // Dispatcher on which to create delayed tasks. |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 102 | EventDispatcher* dispatcher_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 103 | // Metrics instance on which to post performance results. |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 104 | Metrics* metrics_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 105 | // Failure callback method to call if LinkMonitor fails. |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 106 | FailureCallback failure_callback_; |
Peter Qiu | b5d124f | 2014-04-14 12:05:02 -0700 | [diff] [blame] | 107 | // Callback method to call if gateway mac address changes. |
| 108 | GatewayChangeCallback gateway_change_callback_; |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 109 | std::unique_ptr<ActiveLinkMonitor> active_link_monitor_; |
| 110 | std::unique_ptr<PassiveLinkMonitor> passive_link_monitor_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 111 | // The MAC address of the default gateway. |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 112 | ByteString gateway_mac_address_; |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 113 | // The time at which the link monitor started. |
| 114 | struct timeval started_monitoring_at_; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 115 | // Time instance for performing GetTimeMonotonic(). |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 116 | Time* time_; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 117 | |
| 118 | DISALLOW_COPY_AND_ASSIGN(LinkMonitor); |
| 119 | }; |
| 120 | |
| 121 | } // namespace shill |
| 122 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 123 | #endif // SHILL_LINK_MONITOR_H_ |