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