blob: 0234f59b0c7a435d4d5c07b281fd6705e0687916 [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
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 Stewart3f43f432012-07-16 12:12:45 -070016
Paul Stewart6c72c972012-07-27 11:29:20 -070017#ifndef SHILL_LINK_MONITOR_H_
18#define SHILL_LINK_MONITOR_H_
19
20#include <time.h>
Paul Stewart3f43f432012-07-16 12:12:45 -070021
Ben Chan22f1fbc2014-10-17 14:18:07 -070022#include <memory>
Alex Vakulenko8a532292014-06-16 17:18:44 -070023#include <string>
24
Paul Stewart3f43f432012-07-16 12:12:45 -070025#include <base/callback.h>
Paul Stewart6c72c972012-07-27 11:29:20 -070026#include <base/cancelable_callback.h>
Paul Stewart3f43f432012-07-16 12:12:45 -070027
Peter Qiuffa56372015-01-22 14:25:23 -080028#include "shill/metrics.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070029#include "shill/net/byte_string.h"
Paul Stewart3f43f432012-07-16 12:12:45 -070030#include "shill/refptr_types.h"
31
32namespace shill {
33
Peter Qiuffa56372015-01-22 14:25:23 -080034class ActiveLinkMonitor;
Paul Stewart6c72c972012-07-27 11:29:20 -070035class DeviceInfo;
Paul Stewart3f43f432012-07-16 12:12:45 -070036class EventDispatcher;
Peter Qiuffa56372015-01-22 14:25:23 -080037class PassiveLinkMonitor;
Paul Stewart6c72c972012-07-27 11:29:20 -070038class Time;
Paul Stewart3f43f432012-07-16 12:12:45 -070039
Paul Stewart3f43f432012-07-16 12:12:45 -070040class LinkMonitor {
41 public:
Paul Stewart6c72c972012-07-27 11:29:20 -070042 typedef base::Closure FailureCallback;
Peter Qiub5d124f2014-04-14 12:05:02 -070043 typedef base::Closure GatewayChangeCallback;
Paul Stewart3f43f432012-07-16 12:12:45 -070044
Peter Qiuffa56372015-01-22 14:25:23 -080045 // The default number of milliseconds between ARP requests used by
46 // ActiveLinkMonitor. Needed by Metrics.
mukesh agrawalbb2231c2013-07-17 16:32:24 -070047 static const int kDefaultTestPeriodMilliseconds;
Paul Stewartff845fc2012-08-07 07:28:44 -070048
Paul Stewart036dba02012-08-07 12:34:41 -070049 // The default list of technologies for which link monitoring is enabled.
mukesh agrawalbb2231c2013-07-17 16:32:24 -070050 // Needed by DefaultProfile.
Paul Stewart036dba02012-08-07 12:34:41 -070051 static const char kDefaultLinkMonitorTechnologies[];
52
Peter Qiuffa56372015-01-22 14:25:23 -080053 // Failure threshold count used by ActiveLinkMonitor. Needed by Metrics.
Paul Stewartb434ce52013-09-23 13:53:49 -070054 static const int kFailureThreshold;
55
Paul Stewart8ae18742015-06-16 13:13:10 -070056 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 Stewart3f43f432012-07-16 12:12:45 -070062 virtual ~LinkMonitor();
63
Paul Stewart6c72c972012-07-27 11:29:20 -070064 // Starts link-monitoring on the selected connection. Returns
65 // true if successful, false otherwise.
66 virtual bool Start();
mukesh agrawalbb2231c2013-07-17 16:32:24 -070067 // Stop link-monitoring on the selected connection. Clears any
68 // accumulated statistics.
Paul Stewart6c72c972012-07-27 11:29:20 -070069 virtual void Stop();
70
mukesh agrawalbb2231c2013-07-17 16:32:24 -070071 // Inform LinkMonitor that the system is resuming from sleep.
Peter Qiuffa56372015-01-22 14:25:23 -080072 // LinkMonitor will immediately start the ActiveLinkMonitor, using a lower
mukesh agrawalbb2231c2013-07-17 16:32:24 -070073 // timeout than normal.
74 virtual void OnAfterResume();
75
Paul Stewart6c72c972012-07-27 11:29:20 -070076 // 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 Stewartf1961f82012-09-11 20:45:39 -070080 virtual int GetResponseTimeMilliseconds() const;
Paul Stewart9f7823e2012-08-09 10:58:26 -070081
82 // Returns true if the LinkMonitor was ever able to find the default
83 // gateway via broadcast ARP.
84 virtual bool IsGatewayFound() const;
Paul Stewart3f43f432012-07-16 12:12:45 -070085
Paul Stewart8ae18742015-06-16 13:13:10 -070086 const ByteString& gateway_mac_address() const {
Peter Qiub5d124f2014-04-14 12:05:02 -070087 return gateway_mac_address_;
88 }
89
Paul Stewart3f43f432012-07-16 12:12:45 -070090 private:
Paul Stewart6c72c972012-07-27 11:29:20 -070091 friend class LinkMonitorTest;
92
Peter Qiuffa56372015-01-22 14:25:23 -080093 void OnActiveLinkMonitorFailure(Metrics::LinkMonitorFailure failure,
94 int broadcast_failure_count,
95 int unicast_failure_count);
96 void OnActiveLinkMonitorSuccess();
97 void OnPassiveLinkMonitorResultCallback(bool status);
Paul Stewart6c72c972012-07-27 11:29:20 -070098
Paul Stewartf1961f82012-09-11 20:45:39 -070099 // The connection on which to perform link monitoring.
Paul Stewart3f43f432012-07-16 12:12:45 -0700100 ConnectionRefPtr connection_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700101 // Dispatcher on which to create delayed tasks.
Paul Stewart8ae18742015-06-16 13:13:10 -0700102 EventDispatcher* dispatcher_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700103 // Metrics instance on which to post performance results.
Paul Stewart8ae18742015-06-16 13:13:10 -0700104 Metrics* metrics_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700105 // Failure callback method to call if LinkMonitor fails.
Paul Stewart3f43f432012-07-16 12:12:45 -0700106 FailureCallback failure_callback_;
Peter Qiub5d124f2014-04-14 12:05:02 -0700107 // Callback method to call if gateway mac address changes.
108 GatewayChangeCallback gateway_change_callback_;
Peter Qiuffa56372015-01-22 14:25:23 -0800109 std::unique_ptr<ActiveLinkMonitor> active_link_monitor_;
110 std::unique_ptr<PassiveLinkMonitor> passive_link_monitor_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700111 // The MAC address of the default gateway.
Paul Stewart6c72c972012-07-27 11:29:20 -0700112 ByteString gateway_mac_address_;
Paul Stewart0443aa52012-08-09 10:43:50 -0700113 // The time at which the link monitor started.
114 struct timeval started_monitoring_at_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700115 // Time instance for performing GetTimeMonotonic().
Paul Stewart8ae18742015-06-16 13:13:10 -0700116 Time* time_;
Paul Stewart3f43f432012-07-16 12:12:45 -0700117
118 DISALLOW_COPY_AND_ASSIGN(LinkMonitor);
119};
120
121} // namespace shill
122
Paul Stewart6c72c972012-07-27 11:29:20 -0700123#endif // SHILL_LINK_MONITOR_H_