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