blob: 3b95269c3e31bff64b5a471cb958a5daabd20919 [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
10#include <base/callback.h>
Paul Stewart6c72c972012-07-27 11:29:20 -070011#include <base/cancelable_callback.h>
Paul Stewart3f43f432012-07-16 12:12:45 -070012#include <base/memory/scoped_ptr.h>
13
Paul Stewart6c72c972012-07-27 11:29:20 -070014#include "shill/byte_string.h"
Paul Stewart3f43f432012-07-16 12:12:45 -070015#include "shill/refptr_types.h"
16
17namespace shill {
18
Paul Stewart6c72c972012-07-27 11:29:20 -070019class ArpClient;
20class DeviceInfo;
Paul Stewart3f43f432012-07-16 12:12:45 -070021class EventDispatcher;
Paul Stewart6c72c972012-07-27 11:29:20 -070022class IOHandler;
Paul Stewartff845fc2012-08-07 07:28:44 -070023class Metrics;
Paul Stewart6c72c972012-07-27 11:29:20 -070024class Time;
Paul Stewart3f43f432012-07-16 12:12:45 -070025
26// LinkMonitor tracks the status of a connection by sending ARP
27// messages to the default gateway for a connection. It keeps
Paul Stewart6c72c972012-07-27 11:29:20 -070028// 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 Stewart3f43f432012-07-16 12:12:45 -070031class LinkMonitor {
32 public:
Paul Stewart6c72c972012-07-27 11:29:20 -070033 typedef base::Closure FailureCallback;
Paul Stewart3f43f432012-07-16 12:12:45 -070034
mukesh agrawalbb2231c2013-07-17 16:32:24 -070035 // The default number of milliseconds between ARP requests. Needed by Metrics.
36 static const int kDefaultTestPeriodMilliseconds;
Paul Stewartff845fc2012-08-07 07:28:44 -070037
Paul Stewart036dba02012-08-07 12:34:41 -070038 // The default list of technologies for which link monitoring is enabled.
mukesh agrawalbb2231c2013-07-17 16:32:24 -070039 // Needed by DefaultProfile.
Paul Stewart036dba02012-08-07 12:34:41 -070040 static const char kDefaultLinkMonitorTechnologies[];
41
Paul Stewartb434ce52013-09-23 13:53:49 -070042 // When the sum of consecutive counted unicast and broadcast failures
43 // equals this value, the failure callback is called, the counters
44 // are reset, and the link monitoring quiesces. Needed by Metrics.
45 static const int kFailureThreshold;
46
Paul Stewart3f43f432012-07-16 12:12:45 -070047 LinkMonitor(const ConnectionRefPtr &connection,
Paul Stewartf1961f82012-09-11 20:45:39 -070048 EventDispatcher *dispatcher, // Owned by caller; can't be NULL.
49 Metrics *metrics, // Owned by caller; must not be NULL.
Paul Stewart6c72c972012-07-27 11:29:20 -070050 DeviceInfo *device_info,
Paul Stewart3f43f432012-07-16 12:12:45 -070051 const FailureCallback &failure_callback);
52 virtual ~LinkMonitor();
53
Paul Stewart6c72c972012-07-27 11:29:20 -070054 // Starts link-monitoring on the selected connection. Returns
55 // true if successful, false otherwise.
56 virtual bool Start();
mukesh agrawalbb2231c2013-07-17 16:32:24 -070057 // Stop link-monitoring on the selected connection. Clears any
58 // accumulated statistics.
Paul Stewart6c72c972012-07-27 11:29:20 -070059 virtual void Stop();
60
mukesh agrawalbb2231c2013-07-17 16:32:24 -070061 // Inform LinkMonitor that the system is resuming from sleep.
62 // LinkMonitor will immediately probe the gateway, using a lower
63 // timeout than normal.
64 virtual void OnAfterResume();
65
Paul Stewart6c72c972012-07-27 11:29:20 -070066 // Return modified cumulative average of the gateway ARP response
67 // time. Returns zero if no samples are available. For each
68 // missed ARP response, the sample is assumed to be the full
69 // test period.
Paul Stewartf1961f82012-09-11 20:45:39 -070070 virtual int GetResponseTimeMilliseconds() const;
Paul Stewart9f7823e2012-08-09 10:58:26 -070071
72 // Returns true if the LinkMonitor was ever able to find the default
73 // gateway via broadcast ARP.
74 virtual bool IsGatewayFound() const;
Paul Stewart3f43f432012-07-16 12:12:45 -070075
76 private:
Paul Stewart6c72c972012-07-27 11:29:20 -070077 friend class LinkMonitorForTest;
78 friend class LinkMonitorTest;
79
mukesh agrawalbb2231c2013-07-17 16:32:24 -070080 // The number of milliseconds between ARP requests when running a quick test.
81 // Needed by unit tests.
82 static const int kFastTestPeriodMilliseconds;
83
Paul Stewart6c72c972012-07-27 11:29:20 -070084 // The number of samples to compute a "strict" average over. When
85 // more samples than this number arrive, this determines how "slow"
86 // our simple low-pass filter works.
Paul Stewartf1961f82012-09-11 20:45:39 -070087 static const int kMaxResponseSampleFilterDepth;
Paul Stewart6c72c972012-07-27 11:29:20 -070088
Paul Stewartb434ce52013-09-23 13:53:49 -070089 // When the sum of consecutive unicast successes equals this value,
90 // we can assume that in general this gateway supports unicast ARP
91 // requests, and we will count future unicast failures.
92 static const int kUnicastReplyReliabilityThreshold;
93
mukesh agrawalbb2231c2013-07-17 16:32:24 -070094 // Similar to Start, except that the initial probes use
95 // |probe_period_milliseconds|. After successfully probing with both
96 // broadcast and unicast ARPs (at least one of each), LinkMonitor
97 // switches itself to kDefaultTestPeriodMilliseconds.
98 virtual bool StartInternal(int probe_period_milliseconds);
Paul Stewart6c72c972012-07-27 11:29:20 -070099 // Add a response time sample to the buffer.
Paul Stewartf1961f82012-09-11 20:45:39 -0700100 void AddResponseTimeSample(int response_time_milliseconds);
Paul Stewart6c72c972012-07-27 11:29:20 -0700101 // Create an ArpClient instance so we can receive and transmit ARP
102 // packets. This method is virtual so it can be overridden in
103 // unit tests.
104 virtual bool CreateClient();
105 // Convert a hardware address byte-string to a colon-separated string.
106 static std::string HardwareAddressToString(const ByteString &address);
107 // Denote a missed response. Returns true if this loss has caused us
108 // to exceed the failure threshold.
109 bool AddMissedResponse();
110 // This I/O callback is triggered whenever the ARP reception socket
111 // has data available to be received.
112 void ReceiveResponse(int fd);
113 // Send the next ARP request. Returns true if successful, false
114 // otherwise.
115 bool SendRequest();
Paul Stewart6c72c972012-07-27 11:29:20 -0700116
Paul Stewartf1961f82012-09-11 20:45:39 -0700117 // The connection on which to perform link monitoring.
Paul Stewart3f43f432012-07-16 12:12:45 -0700118 ConnectionRefPtr connection_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700119 // Dispatcher on which to create delayed tasks.
Paul Stewart3f43f432012-07-16 12:12:45 -0700120 EventDispatcher *dispatcher_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700121 // Metrics instance on which to post performance results.
Paul Stewartff845fc2012-08-07 07:28:44 -0700122 Metrics *metrics_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700123 // DeviceInfo instance for retrieving the MAC address of a device.
Paul Stewart6c72c972012-07-27 11:29:20 -0700124 DeviceInfo *device_info_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700125 // Failure callback method to call if LinkMonitor fails.
Paul Stewart3f43f432012-07-16 12:12:45 -0700126 FailureCallback failure_callback_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700127 // The MAC address of device associated with this connection.
Paul Stewart6c72c972012-07-27 11:29:20 -0700128 ByteString local_mac_address_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700129 // The MAC address of the default gateway.
Paul Stewart6c72c972012-07-27 11:29:20 -0700130 ByteString gateway_mac_address_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700131 // ArpClient instance used for performing link tests.
Paul Stewart6c72c972012-07-27 11:29:20 -0700132 scoped_ptr<ArpClient> arp_client_;
133
mukesh agrawalbb2231c2013-07-17 16:32:24 -0700134 // How frequently we send an ARP request. This is also the timeout
135 // for a pending request.
136 int test_period_milliseconds_;
Paul Stewart6c72c972012-07-27 11:29:20 -0700137 // The number of consecutive times we have failed in receiving
138 // responses to broadcast ARP requests.
Paul Stewartf1961f82012-09-11 20:45:39 -0700139 int broadcast_failure_count_;
Paul Stewart6c72c972012-07-27 11:29:20 -0700140 // The number of consecutive times we have failed in receiving
141 // responses to unicast ARP requests.
Paul Stewartf1961f82012-09-11 20:45:39 -0700142 int unicast_failure_count_;
mukesh agrawalbb2231c2013-07-17 16:32:24 -0700143 // The number of consecutive times we have succeeded in receiving
144 // responses to broadcast ARP requests.
145 int broadcast_success_count_;
146 // The number of consecutive times we have succeeded in receiving
147 // responses to unicast ARP requests.
148 int unicast_success_count_;
Paul Stewart6c72c972012-07-27 11:29:20 -0700149
150 // Whether this iteration of the test was a unicast request
151 // to the gateway instead of broadcast. The link monitor
152 // alternates between unicast and broadcast requests so that
153 // both types of network traffic is monitored.
154 bool is_unicast_;
155
Paul Stewartb434ce52013-09-23 13:53:49 -0700156 // Whether we have observed that the gateway reliably responds
157 // to unicast ARP requests.
158 bool gateway_supports_unicast_arp_;
159
Paul Stewartf1961f82012-09-11 20:45:39 -0700160 // Number of response samples received in our rolling averge.
161 int response_sample_count_;
162 // The sum of response samples in our rolling average.
163 int response_sample_bucket_;
Paul Stewart6c72c972012-07-27 11:29:20 -0700164
Paul Stewartf1961f82012-09-11 20:45:39 -0700165 // IOCallback that fires when the socket associated with our ArpClient
166 // has a packet to be received. Calls ReceiveResponse().
Paul Stewart6c72c972012-07-27 11:29:20 -0700167 scoped_ptr<IOHandler> receive_response_handler_;
168 // Callback method used for periodic transmission of ARP requests.
169 // When the timer expires this will call SendRequest() through the
170 // void callback function SendRequestTask().
171 base::CancelableClosure send_request_callback_;
172
Paul Stewart0443aa52012-08-09 10:43:50 -0700173 // The time at which the link monitor started.
174 struct timeval started_monitoring_at_;
175
Paul Stewart6c72c972012-07-27 11:29:20 -0700176 // The time at which the last ARP request was sent.
177 struct timeval sent_request_at_;
Paul Stewartf1961f82012-09-11 20:45:39 -0700178 // Time instance for performing GetTimeMonotonic().
Paul Stewart6c72c972012-07-27 11:29:20 -0700179 Time *time_;
Paul Stewart3f43f432012-07-16 12:12:45 -0700180
181 DISALLOW_COPY_AND_ASSIGN(LinkMonitor);
182};
183
184} // namespace shill
185
Paul Stewart6c72c972012-07-27 11:29:20 -0700186#endif // SHILL_LINK_MONITOR_H_