blob: 17f5b0617ddc0a27edc3cc73dd778e040edc08e4 [file] [log] [blame]
Paul Stewart3f43f432012-07-16 12:12:45 -07001// 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 Stewart6c72c972012-07-27 11:29:20 -07005#ifndef SHILL_LINK_MONITOR_H_
6#define SHILL_LINK_MONITOR_H_
7
8#include <time.h>
Paul Stewart3f43f432012-07-16 12:12:45 -07009
Ben Chan22f1fbc2014-10-17 14:18:07 -070010#include <memory>
Alex Vakulenko8a532292014-06-16 17:18:44 -070011#include <string>
12
Paul Stewart3f43f432012-07-16 12:12:45 -070013#include <base/callback.h>
Paul Stewart6c72c972012-07-27 11:29:20 -070014#include <base/cancelable_callback.h>
Paul Stewart3f43f432012-07-16 12:12:45 -070015
Peter Qiuffa56372015-01-22 14:25:23 -080016#include "shill/metrics.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070017#include "shill/net/byte_string.h"
Paul Stewart3f43f432012-07-16 12:12:45 -070018#include "shill/refptr_types.h"
19
20namespace shill {
21
Peter Qiuffa56372015-01-22 14:25:23 -080022class ActiveLinkMonitor;
Paul Stewart6c72c972012-07-27 11:29:20 -070023class DeviceInfo;
Paul Stewart3f43f432012-07-16 12:12:45 -070024class EventDispatcher;
Peter Qiuffa56372015-01-22 14:25:23 -080025class PassiveLinkMonitor;
Paul Stewart6c72c972012-07-27 11:29:20 -070026class Time;
Paul Stewart3f43f432012-07-16 12:12:45 -070027
Paul Stewart3f43f432012-07-16 12:12:45 -070028class LinkMonitor {
29 public:
Paul Stewart6c72c972012-07-27 11:29:20 -070030 typedef base::Closure FailureCallback;
Peter Qiub5d124f2014-04-14 12:05:02 -070031 typedef base::Closure GatewayChangeCallback;
Paul Stewart3f43f432012-07-16 12:12:45 -070032
Peter Qiuffa56372015-01-22 14:25:23 -080033 // The default number of milliseconds between ARP requests used by
34 // ActiveLinkMonitor. Needed by Metrics.
mukesh agrawalbb2231c2013-07-17 16:32:24 -070035 static const int kDefaultTestPeriodMilliseconds;
Paul Stewartff845fc2012-08-07 07:28:44 -070036
Paul Stewart036dba02012-08-07 12:34:41 -070037 // The default list of technologies for which link monitoring is enabled.
mukesh agrawalbb2231c2013-07-17 16:32:24 -070038 // Needed by DefaultProfile.
Paul Stewart036dba02012-08-07 12:34:41 -070039 static const char kDefaultLinkMonitorTechnologies[];
40
Peter Qiuffa56372015-01-22 14:25:23 -080041 // Failure threshold count used by ActiveLinkMonitor. Needed by Metrics.
Paul Stewartb434ce52013-09-23 13:53:49 -070042 static const int kFailureThreshold;
43
Paul Stewart8ae18742015-06-16 13:13:10 -070044 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 Stewart3f43f432012-07-16 12:12:45 -070050 virtual ~LinkMonitor();
51
Paul Stewart6c72c972012-07-27 11:29:20 -070052 // Starts link-monitoring on the selected connection. Returns
53 // true if successful, false otherwise.
54 virtual bool Start();
mukesh agrawalbb2231c2013-07-17 16:32:24 -070055 // Stop link-monitoring on the selected connection. Clears any
56 // accumulated statistics.
Paul Stewart6c72c972012-07-27 11:29:20 -070057 virtual void Stop();
58
mukesh agrawalbb2231c2013-07-17 16:32:24 -070059 // Inform LinkMonitor that the system is resuming from sleep.
Peter Qiuffa56372015-01-22 14:25:23 -080060 // LinkMonitor will immediately start the ActiveLinkMonitor, using a lower
mukesh agrawalbb2231c2013-07-17 16:32:24 -070061 // timeout than normal.
62 virtual void OnAfterResume();
63
Paul Stewart6c72c972012-07-27 11:29:20 -070064 // 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 Stewartf1961f82012-09-11 20:45:39 -070068 virtual int GetResponseTimeMilliseconds() const;
Paul Stewart9f7823e2012-08-09 10:58:26 -070069
70 // Returns true if the LinkMonitor was ever able to find the default
71 // gateway via broadcast ARP.
72 virtual bool IsGatewayFound() const;
Paul Stewart3f43f432012-07-16 12:12:45 -070073
Paul Stewart8ae18742015-06-16 13:13:10 -070074 const ByteString& gateway_mac_address() const {
Peter Qiub5d124f2014-04-14 12:05:02 -070075 return gateway_mac_address_;
76 }
77
Paul Stewart3f43f432012-07-16 12:12:45 -070078 private:
Paul Stewart6c72c972012-07-27 11:29:20 -070079 friend class LinkMonitorTest;
80
Peter Qiuffa56372015-01-22 14:25:23 -080081 void OnActiveLinkMonitorFailure(Metrics::LinkMonitorFailure failure,
82 int broadcast_failure_count,
83 int unicast_failure_count);
84 void OnActiveLinkMonitorSuccess();
85 void OnPassiveLinkMonitorResultCallback(bool status);
Paul Stewart6c72c972012-07-27 11:29:20 -070086
Paul Stewartf1961f82012-09-11 20:45:39 -070087 // The connection on which to perform link monitoring.
Paul Stewart3f43f432012-07-16 12:12:45 -070088 ConnectionRefPtr connection_;
Paul Stewartf1961f82012-09-11 20:45:39 -070089 // Dispatcher on which to create delayed tasks.
Paul Stewart8ae18742015-06-16 13:13:10 -070090 EventDispatcher* dispatcher_;
Paul Stewartf1961f82012-09-11 20:45:39 -070091 // Metrics instance on which to post performance results.
Paul Stewart8ae18742015-06-16 13:13:10 -070092 Metrics* metrics_;
Paul Stewartf1961f82012-09-11 20:45:39 -070093 // Failure callback method to call if LinkMonitor fails.
Paul Stewart3f43f432012-07-16 12:12:45 -070094 FailureCallback failure_callback_;
Peter Qiub5d124f2014-04-14 12:05:02 -070095 // Callback method to call if gateway mac address changes.
96 GatewayChangeCallback gateway_change_callback_;
Peter Qiuffa56372015-01-22 14:25:23 -080097 std::unique_ptr<ActiveLinkMonitor> active_link_monitor_;
98 std::unique_ptr<PassiveLinkMonitor> passive_link_monitor_;
Paul Stewartf1961f82012-09-11 20:45:39 -070099 // The MAC address of the default gateway.
Paul Stewart6c72c972012-07-27 11:29:20 -0700100 ByteString gateway_mac_address_;
Paul Stewart0443aa52012-08-09 10:43:50 -0700101 // The time at which the link monitor started.
102 struct timeval started_monitoring_at_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700103 // Time instance for performing GetTimeMonotonic().
Paul Stewart8ae18742015-06-16 13:13:10 -0700104 Time* time_;
Paul Stewart3f43f432012-07-16 12:12:45 -0700105
106 DISALLOW_COPY_AND_ASSIGN(LinkMonitor);
107};
108
109} // namespace shill
110
Paul Stewart6c72c972012-07-27 11:29:20 -0700111#endif // SHILL_LINK_MONITOR_H_