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