blob: 3a420cf5ae3fd381acb1b5a6558cb010357629dd [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
Paul Stewart0443aa52012-08-09 10:43:50 -070035 // When the sum of consecutive unicast and broadcast failures
36 // equals this value, the failure callback is called, the counters
37 // are reset, and the link monitoring quiesces. Needed by Metrics.
38 static const unsigned int kFailureThreshold;
39
Paul Stewartff845fc2012-08-07 07:28:44 -070040 // The number of milliseconds between ARP requests. Needed by Metrics.
41 static const unsigned int kTestPeriodMilliseconds;
42
Paul Stewart3f43f432012-07-16 12:12:45 -070043 LinkMonitor(const ConnectionRefPtr &connection,
44 EventDispatcher *dispatcher,
Paul Stewartff845fc2012-08-07 07:28:44 -070045 Metrics *metrics,
Paul Stewart6c72c972012-07-27 11:29:20 -070046 DeviceInfo *device_info,
Paul Stewart3f43f432012-07-16 12:12:45 -070047 const FailureCallback &failure_callback);
48 virtual ~LinkMonitor();
49
Paul Stewart6c72c972012-07-27 11:29:20 -070050 // Starts link-monitoring on the selected connection. Returns
51 // true if successful, false otherwise.
52 virtual bool Start();
53 virtual void Stop();
54
55 // Return modified cumulative average of the gateway ARP response
56 // time. Returns zero if no samples are available. For each
57 // missed ARP response, the sample is assumed to be the full
58 // test period.
Paul Stewart9f7823e2012-08-09 10:58:26 -070059 virtual unsigned int GetResponseTimeMilliseconds() const;
60
61 // Returns true if the LinkMonitor was ever able to find the default
62 // gateway via broadcast ARP.
63 virtual bool IsGatewayFound() const;
Paul Stewart3f43f432012-07-16 12:12:45 -070064
65 private:
Paul Stewart6c72c972012-07-27 11:29:20 -070066 friend class LinkMonitorForTest;
67 friend class LinkMonitorTest;
68
Paul Stewart6c72c972012-07-27 11:29:20 -070069 // The number of samples to compute a "strict" average over. When
70 // more samples than this number arrive, this determines how "slow"
71 // our simple low-pass filter works.
72 static const unsigned int kMaxResponseSampleFilterDepth;
73
74 // Add a response time sample to the buffer.
75 void AddResponseTimeSample(unsigned int response_time_milliseconds);
76 // Create an ArpClient instance so we can receive and transmit ARP
77 // packets. This method is virtual so it can be overridden in
78 // unit tests.
79 virtual bool CreateClient();
80 // Convert a hardware address byte-string to a colon-separated string.
81 static std::string HardwareAddressToString(const ByteString &address);
82 // Denote a missed response. Returns true if this loss has caused us
83 // to exceed the failure threshold.
84 bool AddMissedResponse();
85 // This I/O callback is triggered whenever the ARP reception socket
86 // has data available to be received.
87 void ReceiveResponse(int fd);
88 // Send the next ARP request. Returns true if successful, false
89 // otherwise.
90 bool SendRequest();
91 // Timer callback which calls SendRequest().
92 void SendRequestTask();
93
Paul Stewart3f43f432012-07-16 12:12:45 -070094 ConnectionRefPtr connection_;
95 EventDispatcher *dispatcher_;
Paul Stewartff845fc2012-08-07 07:28:44 -070096 Metrics *metrics_;
Paul Stewart6c72c972012-07-27 11:29:20 -070097 DeviceInfo *device_info_;
Paul Stewart3f43f432012-07-16 12:12:45 -070098 FailureCallback failure_callback_;
Paul Stewart6c72c972012-07-27 11:29:20 -070099 ByteString local_mac_address_;
100 ByteString gateway_mac_address_;
101 scoped_ptr<ArpClient> arp_client_;
102
103 // The number of consecutive times we have failed in receiving
104 // responses to broadcast ARP requests.
105 unsigned int broadcast_failure_count_;
106 // The number of consecutive times we have failed in receiving
107 // responses to unicast ARP requests.
108 unsigned int unicast_failure_count_;
109
110 // Whether this iteration of the test was a unicast request
111 // to the gateway instead of broadcast. The link monitor
112 // alternates between unicast and broadcast requests so that
113 // both types of network traffic is monitored.
114 bool is_unicast_;
115
116 // Maintain a pseudo-average of response time.
117 unsigned int response_sample_count_;
118 unsigned int response_sample_bucket_;
119
120 scoped_ptr<IOHandler> receive_response_handler_;
121 // Callback method used for periodic transmission of ARP requests.
122 // When the timer expires this will call SendRequest() through the
123 // void callback function SendRequestTask().
124 base::CancelableClosure send_request_callback_;
125
Paul Stewart0443aa52012-08-09 10:43:50 -0700126 // The time at which the link monitor started.
127 struct timeval started_monitoring_at_;
128
Paul Stewart6c72c972012-07-27 11:29:20 -0700129 // The time at which the last ARP request was sent.
130 struct timeval sent_request_at_;
131 Time *time_;
Paul Stewart3f43f432012-07-16 12:12:45 -0700132
133 DISALLOW_COPY_AND_ASSIGN(LinkMonitor);
134};
135
136} // namespace shill
137
Paul Stewart6c72c972012-07-27 11:29:20 -0700138#endif // SHILL_LINK_MONITOR_H_