blob: 9a3276b9ecc16eaa44d99de601a2a3e4be2560e1 [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;
23class Time;
Paul Stewart3f43f432012-07-16 12:12:45 -070024
25// LinkMonitor tracks the status of a connection by sending ARP
26// messages to the default gateway for a connection. It keeps
Paul Stewart6c72c972012-07-27 11:29:20 -070027// track of response times which can be an indicator of link
28// quality. It signals to caller that the link has failed if
29// too many requests go unanswered.
Paul Stewart3f43f432012-07-16 12:12:45 -070030class LinkMonitor {
31 public:
Paul Stewart6c72c972012-07-27 11:29:20 -070032 typedef base::Closure FailureCallback;
Paul Stewart3f43f432012-07-16 12:12:45 -070033
34 LinkMonitor(const ConnectionRefPtr &connection,
35 EventDispatcher *dispatcher,
Paul Stewart6c72c972012-07-27 11:29:20 -070036 DeviceInfo *device_info,
Paul Stewart3f43f432012-07-16 12:12:45 -070037 const FailureCallback &failure_callback);
38 virtual ~LinkMonitor();
39
Paul Stewart6c72c972012-07-27 11:29:20 -070040 // Starts link-monitoring on the selected connection. Returns
41 // true if successful, false otherwise.
42 virtual bool Start();
43 virtual void Stop();
44
45 // Return modified cumulative average of the gateway ARP response
46 // time. Returns zero if no samples are available. For each
47 // missed ARP response, the sample is assumed to be the full
48 // test period.
49 virtual unsigned int GetResponseTimeMilliseconds();
Paul Stewart3f43f432012-07-16 12:12:45 -070050
51 private:
Paul Stewart6c72c972012-07-27 11:29:20 -070052 friend class LinkMonitorForTest;
53 friend class LinkMonitorTest;
54
55 // The number of milliseconds between ARP requests.
56 static const unsigned int kTestPeriodMilliseconds;
57
58 // When the sum of consecutive unicast and broadcast failures
59 // equals this value, the failure callback is called, the counters
60 // are reset, and the link monitoring quiesces.
61 static const unsigned int kFailureThreshold;
62
63 // The number of samples to compute a "strict" average over. When
64 // more samples than this number arrive, this determines how "slow"
65 // our simple low-pass filter works.
66 static const unsigned int kMaxResponseSampleFilterDepth;
67
68 // Add a response time sample to the buffer.
69 void AddResponseTimeSample(unsigned int response_time_milliseconds);
70 // Create an ArpClient instance so we can receive and transmit ARP
71 // packets. This method is virtual so it can be overridden in
72 // unit tests.
73 virtual bool CreateClient();
74 // Convert a hardware address byte-string to a colon-separated string.
75 static std::string HardwareAddressToString(const ByteString &address);
76 // Denote a missed response. Returns true if this loss has caused us
77 // to exceed the failure threshold.
78 bool AddMissedResponse();
79 // This I/O callback is triggered whenever the ARP reception socket
80 // has data available to be received.
81 void ReceiveResponse(int fd);
82 // Send the next ARP request. Returns true if successful, false
83 // otherwise.
84 bool SendRequest();
85 // Timer callback which calls SendRequest().
86 void SendRequestTask();
87
Paul Stewart3f43f432012-07-16 12:12:45 -070088 ConnectionRefPtr connection_;
89 EventDispatcher *dispatcher_;
Paul Stewart6c72c972012-07-27 11:29:20 -070090 DeviceInfo *device_info_;
Paul Stewart3f43f432012-07-16 12:12:45 -070091 FailureCallback failure_callback_;
Paul Stewart6c72c972012-07-27 11:29:20 -070092 ByteString local_mac_address_;
93 ByteString gateway_mac_address_;
94 scoped_ptr<ArpClient> arp_client_;
95
96 // The number of consecutive times we have failed in receiving
97 // responses to broadcast ARP requests.
98 unsigned int broadcast_failure_count_;
99 // The number of consecutive times we have failed in receiving
100 // responses to unicast ARP requests.
101 unsigned int unicast_failure_count_;
102
103 // Whether this iteration of the test was a unicast request
104 // to the gateway instead of broadcast. The link monitor
105 // alternates between unicast and broadcast requests so that
106 // both types of network traffic is monitored.
107 bool is_unicast_;
108
109 // Maintain a pseudo-average of response time.
110 unsigned int response_sample_count_;
111 unsigned int response_sample_bucket_;
112
113 scoped_ptr<IOHandler> receive_response_handler_;
114 // Callback method used for periodic transmission of ARP requests.
115 // When the timer expires this will call SendRequest() through the
116 // void callback function SendRequestTask().
117 base::CancelableClosure send_request_callback_;
118
119 // The time at which the last ARP request was sent.
120 struct timeval sent_request_at_;
121 Time *time_;
Paul Stewart3f43f432012-07-16 12:12:45 -0700122
123 DISALLOW_COPY_AND_ASSIGN(LinkMonitor);
124};
125
126} // namespace shill
127
Paul Stewart6c72c972012-07-27 11:29:20 -0700128#endif // SHILL_LINK_MONITOR_H_