Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 1 | // 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 Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 7 | #include <string> |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 8 | #include <vector> |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 9 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 10 | #include <base/bind.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 11 | #include <base/strings/stringprintf.h> |
| 12 | #include <base/strings/string_util.h> |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 13 | |
| 14 | #include "shill/arp_client.h" |
| 15 | #include "shill/arp_packet.h" |
| 16 | #include "shill/byte_string.h" |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 17 | #include "shill/connection.h" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 18 | #include "shill/device_info.h" |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 19 | #include "shill/event_dispatcher.h" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 20 | #include "shill/ip_address.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 21 | #include "shill/logging.h" |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 22 | #include "shill/metrics.h" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 23 | #include "shill/shill_time.h" |
| 24 | |
| 25 | using base::Bind; |
| 26 | using base::Unretained; |
| 27 | using std::string; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 28 | |
| 29 | namespace shill { |
| 30 | |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 31 | const int LinkMonitor::kDefaultTestPeriodMilliseconds = 5000; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 32 | const char LinkMonitor::kDefaultLinkMonitorTechnologies[] = "wifi"; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 33 | const int LinkMonitor::kFailureThreshold = 5; |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 34 | const int LinkMonitor::kFastTestPeriodMilliseconds = 200; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 35 | const int LinkMonitor::kMaxResponseSampleFilterDepth = 5; |
Paul Stewart | b434ce5 | 2013-09-23 13:53:49 -0700 | [diff] [blame] | 36 | const int LinkMonitor::kUnicastReplyReliabilityThreshold = 10; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 37 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 38 | LinkMonitor::LinkMonitor(const ConnectionRefPtr &connection, |
| 39 | EventDispatcher *dispatcher, |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 40 | Metrics *metrics, |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 41 | DeviceInfo *device_info, |
Peter Qiu | b5d124f | 2014-04-14 12:05:02 -0700 | [diff] [blame] | 42 | const FailureCallback &failure_callback, |
| 43 | const GatewayChangeCallback &gateway_change_callback) |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 44 | : connection_(connection), |
| 45 | dispatcher_(dispatcher), |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 46 | metrics_(metrics), |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 47 | device_info_(device_info), |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 48 | failure_callback_(failure_callback), |
Peter Qiu | b5d124f | 2014-04-14 12:05:02 -0700 | [diff] [blame] | 49 | gateway_change_callback_(gateway_change_callback), |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 50 | test_period_milliseconds_(kDefaultTestPeriodMilliseconds), |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 51 | broadcast_failure_count_(0), |
| 52 | unicast_failure_count_(0), |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 53 | broadcast_success_count_(0), |
| 54 | unicast_success_count_(0), |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 55 | is_unicast_(false), |
Paul Stewart | b434ce5 | 2013-09-23 13:53:49 -0700 | [diff] [blame] | 56 | gateway_supports_unicast_arp_(false), |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 57 | response_sample_count_(0), |
| 58 | response_sample_bucket_(0), |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 59 | time_(Time::GetInstance()) { |
| 60 | } |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 61 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 62 | LinkMonitor::~LinkMonitor() { |
| 63 | Stop(); |
| 64 | } |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 65 | |
| 66 | bool LinkMonitor::Start() { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 67 | Stop(); |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 68 | return StartInternal(kDefaultTestPeriodMilliseconds); |
| 69 | } |
| 70 | |
| 71 | bool LinkMonitor::StartInternal(int probe_period_milliseconds) { |
| 72 | test_period_milliseconds_ = probe_period_milliseconds; |
| 73 | if (test_period_milliseconds_ > kDefaultTestPeriodMilliseconds) { |
| 74 | LOG(WARNING) << "Long test period; UMA stats will be truncated."; |
| 75 | } |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 76 | |
| 77 | if (!device_info_->GetMACAddress( |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 78 | connection_->interface_index(), &local_mac_address_)) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 79 | LOG(ERROR) << "Could not get local MAC address."; |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 80 | metrics_->NotifyLinkMonitorFailure( |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 81 | connection_->technology(), |
| 82 | Metrics::kLinkMonitorMacAddressNotFound, |
| 83 | 0, 0, 0); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 84 | Stop(); |
| 85 | return false; |
| 86 | } |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 87 | if (gateway_mac_address_.IsEmpty()) { |
| 88 | gateway_mac_address_ = ByteString(local_mac_address_.GetLength()); |
| 89 | } |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 90 | send_request_callback_.Reset( |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 91 | Bind(base::IgnoreResult(&LinkMonitor::SendRequest), Unretained(this))); |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 92 | time_->GetTimeMonotonic(&started_monitoring_at_); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 93 | return SendRequest(); |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void LinkMonitor::Stop() { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 97 | SLOG(Link, 2) << "In " << __func__ << "."; |
| 98 | local_mac_address_.Clear(); |
| 99 | gateway_mac_address_.Clear(); |
| 100 | arp_client_.reset(); |
| 101 | broadcast_failure_count_ = 0; |
| 102 | unicast_failure_count_ = 0; |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 103 | broadcast_success_count_ = 0; |
| 104 | unicast_success_count_ = 0; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 105 | is_unicast_ = false; |
Paul Stewart | b434ce5 | 2013-09-23 13:53:49 -0700 | [diff] [blame] | 106 | gateway_supports_unicast_arp_ = false; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 107 | response_sample_bucket_ = 0; |
| 108 | response_sample_count_ = 0; |
| 109 | receive_response_handler_.reset(); |
| 110 | send_request_callback_.Cancel(); |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 111 | timerclear(&started_monitoring_at_); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 112 | timerclear(&sent_request_at_); |
| 113 | } |
| 114 | |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 115 | void LinkMonitor::OnAfterResume() { |
| 116 | ByteString prior_gateway_mac_address(gateway_mac_address_); |
Paul Stewart | b434ce5 | 2013-09-23 13:53:49 -0700 | [diff] [blame] | 117 | bool gateway_supports_unicast_arp = gateway_supports_unicast_arp_; |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 118 | Stop(); |
| 119 | gateway_mac_address_ = prior_gateway_mac_address; |
Paul Stewart | b434ce5 | 2013-09-23 13:53:49 -0700 | [diff] [blame] | 120 | gateway_supports_unicast_arp_ = gateway_supports_unicast_arp; |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 121 | StartInternal(kFastTestPeriodMilliseconds); |
| 122 | } |
| 123 | |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 124 | int LinkMonitor::GetResponseTimeMilliseconds() const { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 125 | return response_sample_count_ ? |
| 126 | response_sample_bucket_ / response_sample_count_ : 0; |
| 127 | } |
| 128 | |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 129 | void LinkMonitor::AddResponseTimeSample(int response_time_milliseconds) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 130 | SLOG(Link, 2) << "In " << __func__ << " with sample " |
| 131 | << response_time_milliseconds << "."; |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 132 | metrics_->NotifyLinkMonitorResponseTimeSampleAdded( |
| 133 | connection_->technology(), response_time_milliseconds); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 134 | response_sample_bucket_ += response_time_milliseconds; |
| 135 | if (response_sample_count_ < kMaxResponseSampleFilterDepth) { |
| 136 | ++response_sample_count_; |
| 137 | } else { |
| 138 | response_sample_bucket_ = |
| 139 | response_sample_bucket_ * kMaxResponseSampleFilterDepth / |
| 140 | (kMaxResponseSampleFilterDepth + 1); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // static |
| 145 | string LinkMonitor::HardwareAddressToString(const ByteString &address) { |
| 146 | std::vector<string> address_parts; |
| 147 | for (size_t i = 0; i < address.GetLength(); ++i) { |
| 148 | address_parts.push_back( |
| 149 | base::StringPrintf("%02x", address.GetConstData()[i])); |
| 150 | } |
| 151 | return JoinString(address_parts, ':'); |
| 152 | } |
| 153 | |
| 154 | bool LinkMonitor::CreateClient() { |
| 155 | arp_client_.reset(new ArpClient(connection_->interface_index())); |
| 156 | |
| 157 | if (!arp_client_->Start()) { |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 158 | return false; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 159 | } |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 160 | SLOG(Link, 4) << "Created ARP client; listening on socket " |
| 161 | << arp_client_->socket() << "."; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 162 | receive_response_handler_.reset( |
| 163 | dispatcher_->CreateReadyHandler( |
| 164 | arp_client_->socket(), |
| 165 | IOHandler::kModeInput, |
| 166 | Bind(&LinkMonitor::ReceiveResponse, Unretained(this)))); |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | bool LinkMonitor::AddMissedResponse() { |
| 171 | SLOG(Link, 2) << "In " << __func__ << "."; |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 172 | AddResponseTimeSample(test_period_milliseconds_); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 173 | |
| 174 | if (is_unicast_) { |
Paul Stewart | b434ce5 | 2013-09-23 13:53:49 -0700 | [diff] [blame] | 175 | if (gateway_supports_unicast_arp_) { |
| 176 | ++unicast_failure_count_; |
| 177 | } |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 178 | unicast_success_count_ = 0; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 179 | } else { |
| 180 | ++broadcast_failure_count_; |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 181 | broadcast_success_count_ = 0; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (unicast_failure_count_ + broadcast_failure_count_ >= kFailureThreshold) { |
| 185 | LOG(ERROR) << "Link monitor has reached the failure threshold with " |
| 186 | << broadcast_failure_count_ |
| 187 | << " broadcast failures and " |
| 188 | << unicast_failure_count_ |
| 189 | << " unicast failures."; |
| 190 | failure_callback_.Run(); |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 191 | |
| 192 | struct timeval now, elapsed_time; |
| 193 | time_->GetTimeMonotonic(&now); |
| 194 | timersub(&now, &started_monitoring_at_, &elapsed_time); |
| 195 | |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 196 | metrics_->NotifyLinkMonitorFailure( |
| 197 | connection_->technology(), |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 198 | Metrics::kLinkMonitorFailureThresholdReached, |
| 199 | elapsed_time.tv_sec, |
| 200 | broadcast_failure_count_, |
| 201 | unicast_failure_count_); |
| 202 | |
| 203 | Stop(); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | is_unicast_ = !is_unicast_; |
| 207 | return false; |
| 208 | } |
| 209 | |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 210 | bool LinkMonitor::IsGatewayFound() const { |
| 211 | return !gateway_mac_address_.IsZero(); |
| 212 | } |
| 213 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 214 | void LinkMonitor::ReceiveResponse(int fd) { |
| 215 | SLOG(Link, 2) << "In " << __func__ << "."; |
| 216 | ArpPacket packet; |
| 217 | ByteString sender; |
| 218 | if (!arp_client_->ReceiveReply(&packet, &sender)) { |
| 219 | return; |
| 220 | } |
| 221 | |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 222 | if (!connection_->local().address().Equals( |
| 223 | packet.remote_ip_address().address())) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 224 | SLOG(Link, 4) << "Response is not for our IP address."; |
| 225 | return; |
| 226 | } |
| 227 | |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 228 | if (!local_mac_address_.Equals(packet.remote_mac_address())) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 229 | SLOG(Link, 4) << "Response is not for our MAC address."; |
| 230 | return; |
| 231 | } |
| 232 | |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 233 | if (!connection_->gateway().address().Equals( |
| 234 | packet.local_ip_address().address())) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 235 | SLOG(Link, 4) << "Response is not from the gateway IP address."; |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | struct timeval now, elapsed_time; |
| 240 | time_->GetTimeMonotonic(&now); |
| 241 | timersub(&now, &sent_request_at_, &elapsed_time); |
| 242 | |
| 243 | AddResponseTimeSample(elapsed_time.tv_sec * 1000 + |
| 244 | elapsed_time.tv_usec / 1000); |
| 245 | |
| 246 | receive_response_handler_.reset(); |
| 247 | arp_client_.reset(); |
| 248 | |
| 249 | if (is_unicast_) { |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 250 | ++unicast_success_count_; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 251 | unicast_failure_count_ = 0; |
Paul Stewart | b434ce5 | 2013-09-23 13:53:49 -0700 | [diff] [blame] | 252 | if (unicast_success_count_ >= kUnicastReplyReliabilityThreshold) { |
| 253 | SLOG_IF(Link, 2, !gateway_supports_unicast_arp_) |
| 254 | << "Gateway is now considered a reliable unicast responder. " |
| 255 | "Unicast failures will now count."; |
| 256 | gateway_supports_unicast_arp_ = true; |
| 257 | } |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 258 | } else { |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 259 | ++broadcast_success_count_; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 260 | broadcast_failure_count_ = 0; |
| 261 | } |
| 262 | |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 263 | if (!gateway_mac_address_.Equals(packet.local_mac_address())) { |
| 264 | const ByteString &new_mac_address = packet.local_mac_address(); |
| 265 | if (!IsGatewayFound()) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 266 | SLOG(Link, 2) << "Found gateway at " |
| 267 | << HardwareAddressToString(new_mac_address); |
| 268 | } else { |
| 269 | SLOG(Link, 2) << "Gateway MAC address changed."; |
| 270 | } |
| 271 | gateway_mac_address_ = new_mac_address; |
Peter Qiu | b5d124f | 2014-04-14 12:05:02 -0700 | [diff] [blame] | 272 | |
| 273 | // Notify device of the new gateway mac address. |
| 274 | gateway_change_callback_.Run(); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | is_unicast_ = !is_unicast_; |
Paul Stewart | b434ce5 | 2013-09-23 13:53:49 -0700 | [diff] [blame] | 278 | if ((unicast_success_count_ || !gateway_supports_unicast_arp_) |
| 279 | && broadcast_success_count_) { |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 280 | test_period_milliseconds_ = kDefaultTestPeriodMilliseconds; |
| 281 | } |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | bool LinkMonitor::SendRequest() { |
| 285 | SLOG(Link, 2) << "In " << __func__ << "."; |
| 286 | if (!arp_client_.get()) { |
| 287 | if (!CreateClient()) { |
| 288 | LOG(ERROR) << "Failed to start ARP client."; |
| 289 | Stop(); |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 290 | metrics_->NotifyLinkMonitorFailure( |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 291 | connection_->technology(), |
| 292 | Metrics::kLinkMonitorClientStartFailure, |
| 293 | 0, 0, 0); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | } else if (AddMissedResponse()) { |
| 297 | // If an ARP client is still listening, this means we have timed |
| 298 | // out reception of the ARP reply. |
| 299 | return false; |
| 300 | } else { |
| 301 | // We already have an ArpClient instance running. These aren't |
| 302 | // bound sockets in the conventional sense, and we cannot distinguish |
| 303 | // which request (from which trial, or even from which component |
| 304 | // in the local system) an ARP reply was sent in response to. |
| 305 | // Therefore we keep the already open ArpClient in the case of |
| 306 | // a non-fatal timeout. |
| 307 | } |
| 308 | |
| 309 | ByteString destination_mac_address(gateway_mac_address_.GetLength()); |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 310 | if (!IsGatewayFound()) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 311 | // The remote MAC addess is set by convention to be all-zeroes in the |
| 312 | // ARP header if not known. The ArpClient will translate an all-zeroes |
| 313 | // remote address into a send to the broadcast (all-ones) address in |
| 314 | // the Ethernet frame header. |
| 315 | SLOG_IF(Link, 2, is_unicast_) << "Sending broadcast since " |
| 316 | << "gateway MAC is unknown"; |
| 317 | is_unicast_ = false; |
| 318 | } else if (is_unicast_) { |
| 319 | destination_mac_address = gateway_mac_address_; |
| 320 | } |
| 321 | |
| 322 | ArpPacket request(connection_->local(), connection_->gateway(), |
| 323 | local_mac_address_, destination_mac_address); |
| 324 | if (!arp_client_->TransmitRequest(request)) { |
| 325 | LOG(ERROR) << "Failed to send ARP request. Stopping."; |
| 326 | Stop(); |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 327 | metrics_->NotifyLinkMonitorFailure( |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 328 | connection_->technology(), Metrics::kLinkMonitorTransmitFailure, |
| 329 | 0, 0, 0); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | |
| 333 | time_->GetTimeMonotonic(&sent_request_at_); |
| 334 | |
| 335 | dispatcher_->PostDelayedTask(send_request_callback_.callback(), |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 336 | test_period_milliseconds_); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 337 | return true; |
| 338 | } |
| 339 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 340 | } // namespace shill |