blob: 1d8dd1b203af81008e239f3d2ef5b8005ed84f0c [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
5#include "shill/link_monitor.h"
6
Paul Stewart6c72c972012-07-27 11:29:20 -07007#include <vector>
Paul Stewart3f43f432012-07-16 12:12:45 -07008
Paul Stewart6c72c972012-07-27 11:29:20 -07009#include <base/bind.h>
Paul Stewart6c72c972012-07-27 11:29:20 -070010#include <base/stringprintf.h>
11#include <base/string_util.h>
12
13#include "shill/arp_client.h"
14#include "shill/arp_packet.h"
15#include "shill/byte_string.h"
Paul Stewart3f43f432012-07-16 12:12:45 -070016#include "shill/connection.h"
Paul Stewart6c72c972012-07-27 11:29:20 -070017#include "shill/device_info.h"
Paul Stewart3f43f432012-07-16 12:12:45 -070018#include "shill/event_dispatcher.h"
Paul Stewart6c72c972012-07-27 11:29:20 -070019#include "shill/ip_address.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070020#include "shill/logging.h"
Paul Stewartff845fc2012-08-07 07:28:44 -070021#include "shill/metrics.h"
Paul Stewart6c72c972012-07-27 11:29:20 -070022#include "shill/shill_time.h"
23
24using base::Bind;
25using base::Unretained;
26using std::string;
Paul Stewart3f43f432012-07-16 12:12:45 -070027
28namespace shill {
29
Paul Stewart6c72c972012-07-27 11:29:20 -070030const unsigned int LinkMonitor::kTestPeriodMilliseconds = 5000;
31const unsigned int LinkMonitor::kFailureThreshold = 5;
32const unsigned int LinkMonitor::kMaxResponseSampleFilterDepth = 5;
33
Paul Stewart3f43f432012-07-16 12:12:45 -070034LinkMonitor::LinkMonitor(const ConnectionRefPtr &connection,
35 EventDispatcher *dispatcher,
Paul Stewartff845fc2012-08-07 07:28:44 -070036 Metrics *metrics,
Paul Stewart6c72c972012-07-27 11:29:20 -070037 DeviceInfo *device_info,
Paul Stewart3f43f432012-07-16 12:12:45 -070038 const FailureCallback &failure_callback)
39 : connection_(connection),
40 dispatcher_(dispatcher),
Paul Stewartff845fc2012-08-07 07:28:44 -070041 metrics_(metrics),
Paul Stewart6c72c972012-07-27 11:29:20 -070042 device_info_(device_info),
Paul Stewart3f43f432012-07-16 12:12:45 -070043 failure_callback_(failure_callback),
Paul Stewart6c72c972012-07-27 11:29:20 -070044 broadcast_failure_count_(0),
45 unicast_failure_count_(0),
46 is_unicast_(false),
47 response_sample_count_(0),
48 response_sample_bucket_(0),
49 time_(Time::GetInstance()) {}
Paul Stewart3f43f432012-07-16 12:12:45 -070050
Paul Stewart6c72c972012-07-27 11:29:20 -070051LinkMonitor::~LinkMonitor() {
52 Stop();
53}
Paul Stewart3f43f432012-07-16 12:12:45 -070054
55bool LinkMonitor::Start() {
Paul Stewart6c72c972012-07-27 11:29:20 -070056 Stop();
57
58 if (!device_info_->GetMACAddress(
59 connection_->interface_index(), &local_mac_address_)) {
60 LOG(ERROR) << "Could not get local MAC address.";
Paul Stewartff845fc2012-08-07 07:28:44 -070061 metrics_->NotifyLinkMonitorFailure(
Paul Stewart0443aa52012-08-09 10:43:50 -070062 connection_->technology(),
63 Metrics::kLinkMonitorMacAddressNotFound,
64 0, 0, 0);
Paul Stewart6c72c972012-07-27 11:29:20 -070065 Stop();
66 return false;
67 }
68 gateway_mac_address_ = ByteString(local_mac_address_.GetLength());
69 send_request_callback_.Reset(
70 Bind(&LinkMonitor::SendRequestTask, Unretained(this)));
Paul Stewart0443aa52012-08-09 10:43:50 -070071 time_->GetTimeMonotonic(&started_monitoring_at_);
Paul Stewart6c72c972012-07-27 11:29:20 -070072 return SendRequest();
Paul Stewart3f43f432012-07-16 12:12:45 -070073}
74
75void LinkMonitor::Stop() {
Paul Stewart6c72c972012-07-27 11:29:20 -070076 SLOG(Link, 2) << "In " << __func__ << ".";
77 local_mac_address_.Clear();
78 gateway_mac_address_.Clear();
79 arp_client_.reset();
80 broadcast_failure_count_ = 0;
81 unicast_failure_count_ = 0;
82 is_unicast_ = false;
83 response_sample_bucket_ = 0;
84 response_sample_count_ = 0;
85 receive_response_handler_.reset();
86 send_request_callback_.Cancel();
Paul Stewart0443aa52012-08-09 10:43:50 -070087 timerclear(&started_monitoring_at_);
Paul Stewart6c72c972012-07-27 11:29:20 -070088 timerclear(&sent_request_at_);
89}
90
91unsigned int LinkMonitor::GetResponseTimeMilliseconds() {
92 return response_sample_count_ ?
93 response_sample_bucket_ / response_sample_count_ : 0;
94}
95
96void LinkMonitor::AddResponseTimeSample(
97 unsigned int response_time_milliseconds) {
98 SLOG(Link, 2) << "In " << __func__ << " with sample "
99 << response_time_milliseconds << ".";
Paul Stewartff845fc2012-08-07 07:28:44 -0700100 metrics_->NotifyLinkMonitorResponseTimeSampleAdded(
101 connection_->technology(), response_time_milliseconds);
Paul Stewart6c72c972012-07-27 11:29:20 -0700102 response_sample_bucket_ += response_time_milliseconds;
103 if (response_sample_count_ < kMaxResponseSampleFilterDepth) {
104 ++response_sample_count_;
105 } else {
106 response_sample_bucket_ =
107 response_sample_bucket_ * kMaxResponseSampleFilterDepth /
108 (kMaxResponseSampleFilterDepth + 1);
109 }
110}
111
112// static
113string LinkMonitor::HardwareAddressToString(const ByteString &address) {
114 std::vector<string> address_parts;
115 for (size_t i = 0; i < address.GetLength(); ++i) {
116 address_parts.push_back(
117 base::StringPrintf("%02x", address.GetConstData()[i]));
118 }
119 return JoinString(address_parts, ':');
120}
121
122bool LinkMonitor::CreateClient() {
123 arp_client_.reset(new ArpClient(connection_->interface_index()));
124
125 if (!arp_client_->Start()) {
Paul Stewartff845fc2012-08-07 07:28:44 -0700126 return false;
Paul Stewart6c72c972012-07-27 11:29:20 -0700127 }
128 receive_response_handler_.reset(
129 dispatcher_->CreateReadyHandler(
130 arp_client_->socket(),
131 IOHandler::kModeInput,
132 Bind(&LinkMonitor::ReceiveResponse, Unretained(this))));
133 return true;
134}
135
136bool LinkMonitor::AddMissedResponse() {
137 SLOG(Link, 2) << "In " << __func__ << ".";
138 AddResponseTimeSample(kTestPeriodMilliseconds);
139
140 if (is_unicast_) {
141 ++unicast_failure_count_;
142 } else {
143 ++broadcast_failure_count_;
144 }
145
146 if (unicast_failure_count_ + broadcast_failure_count_ >= kFailureThreshold) {
147 LOG(ERROR) << "Link monitor has reached the failure threshold with "
148 << broadcast_failure_count_
149 << " broadcast failures and "
150 << unicast_failure_count_
151 << " unicast failures.";
152 failure_callback_.Run();
Paul Stewart0443aa52012-08-09 10:43:50 -0700153
154 struct timeval now, elapsed_time;
155 time_->GetTimeMonotonic(&now);
156 timersub(&now, &started_monitoring_at_, &elapsed_time);
157
Paul Stewartff845fc2012-08-07 07:28:44 -0700158 metrics_->NotifyLinkMonitorFailure(
159 connection_->technology(),
Paul Stewart0443aa52012-08-09 10:43:50 -0700160 Metrics::kLinkMonitorFailureThresholdReached,
161 elapsed_time.tv_sec,
162 broadcast_failure_count_,
163 unicast_failure_count_);
164
165 Stop();
Paul Stewart6c72c972012-07-27 11:29:20 -0700166 return true;
167 }
168 is_unicast_ = !is_unicast_;
169 return false;
170}
171
172void LinkMonitor::ReceiveResponse(int fd) {
173 SLOG(Link, 2) << "In " << __func__ << ".";
174 ArpPacket packet;
175 ByteString sender;
176 if (!arp_client_->ReceiveReply(&packet, &sender)) {
177 return;
178 }
179
180 if (!connection_->local().Equals(packet.local_ip_address())) {
181 SLOG(Link, 4) << "Response is not for our IP address.";
182 return;
183 }
184
185 if (!local_mac_address_.Equals(packet.local_mac_address())) {
186 SLOG(Link, 4) << "Response is not for our MAC address.";
187 return;
188 }
189
190 if (!connection_->gateway().Equals(packet.remote_ip_address())) {
191 SLOG(Link, 4) << "Response is not from the gateway IP address.";
192 return;
193 }
194
195 struct timeval now, elapsed_time;
196 time_->GetTimeMonotonic(&now);
197 timersub(&now, &sent_request_at_, &elapsed_time);
198
199 AddResponseTimeSample(elapsed_time.tv_sec * 1000 +
200 elapsed_time.tv_usec / 1000);
201
202 receive_response_handler_.reset();
203 arp_client_.reset();
204
205 if (is_unicast_) {
206 unicast_failure_count_ = 0;
207 } else {
208 broadcast_failure_count_ = 0;
209 }
210
211 if (!gateway_mac_address_.Equals(packet.remote_mac_address())) {
212 const ByteString &new_mac_address = packet.remote_mac_address();
213 if (gateway_mac_address_.IsZero()) {
214 SLOG(Link, 2) << "Found gateway at "
215 << HardwareAddressToString(new_mac_address);
216 } else {
217 SLOG(Link, 2) << "Gateway MAC address changed.";
218 }
219 gateway_mac_address_ = new_mac_address;
220 }
221
222 is_unicast_ = !is_unicast_;
223}
224
225bool LinkMonitor::SendRequest() {
226 SLOG(Link, 2) << "In " << __func__ << ".";
227 if (!arp_client_.get()) {
228 if (!CreateClient()) {
229 LOG(ERROR) << "Failed to start ARP client.";
230 Stop();
Paul Stewartff845fc2012-08-07 07:28:44 -0700231 metrics_->NotifyLinkMonitorFailure(
Paul Stewart0443aa52012-08-09 10:43:50 -0700232 connection_->technology(),
233 Metrics::kLinkMonitorClientStartFailure,
234 0, 0, 0);
Paul Stewart6c72c972012-07-27 11:29:20 -0700235 return false;
236 }
237 } else if (AddMissedResponse()) {
238 // If an ARP client is still listening, this means we have timed
239 // out reception of the ARP reply.
240 return false;
241 } else {
242 // We already have an ArpClient instance running. These aren't
243 // bound sockets in the conventional sense, and we cannot distinguish
244 // which request (from which trial, or even from which component
245 // in the local system) an ARP reply was sent in response to.
246 // Therefore we keep the already open ArpClient in the case of
247 // a non-fatal timeout.
248 }
249
250 ByteString destination_mac_address(gateway_mac_address_.GetLength());
251 if (gateway_mac_address_.IsZero()) {
252 // The remote MAC addess is set by convention to be all-zeroes in the
253 // ARP header if not known. The ArpClient will translate an all-zeroes
254 // remote address into a send to the broadcast (all-ones) address in
255 // the Ethernet frame header.
256 SLOG_IF(Link, 2, is_unicast_) << "Sending broadcast since "
257 << "gateway MAC is unknown";
258 is_unicast_ = false;
259 } else if (is_unicast_) {
260 destination_mac_address = gateway_mac_address_;
261 }
262
263 ArpPacket request(connection_->local(), connection_->gateway(),
264 local_mac_address_, destination_mac_address);
265 if (!arp_client_->TransmitRequest(request)) {
266 LOG(ERROR) << "Failed to send ARP request. Stopping.";
267 Stop();
Paul Stewartff845fc2012-08-07 07:28:44 -0700268 metrics_->NotifyLinkMonitorFailure(
Paul Stewart0443aa52012-08-09 10:43:50 -0700269 connection_->technology(), Metrics::kLinkMonitorTransmitFailure,
270 0, 0, 0);
Paul Stewart6c72c972012-07-27 11:29:20 -0700271 return false;
272 }
273
274 time_->GetTimeMonotonic(&sent_request_at_);
275
276 dispatcher_->PostDelayedTask(send_request_callback_.callback(),
277 kTestPeriodMilliseconds);
278 return true;
279}
280
281void LinkMonitor::SendRequestTask() {
282 SendRequest();
Paul Stewart3f43f432012-07-16 12:12:45 -0700283}
284
285} // namespace shill