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 | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 7 | #include <vector> |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 8 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 9 | #include <base/bind.h> |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 10 | #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 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" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 19 | #include "shill/ip_address.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 20 | #include "shill/logging.h" |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 21 | #include "shill/metrics.h" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 22 | #include "shill/shill_time.h" |
| 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 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 30 | const unsigned int LinkMonitor::kTestPeriodMilliseconds = 5000; |
| 31 | const unsigned int LinkMonitor::kFailureThreshold = 5; |
| 32 | const unsigned int LinkMonitor::kMaxResponseSampleFilterDepth = 5; |
| 33 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 34 | LinkMonitor::LinkMonitor(const ConnectionRefPtr &connection, |
| 35 | EventDispatcher *dispatcher, |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 36 | Metrics *metrics, |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 37 | DeviceInfo *device_info, |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 38 | const FailureCallback &failure_callback) |
| 39 | : connection_(connection), |
| 40 | dispatcher_(dispatcher), |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 41 | metrics_(metrics), |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 42 | device_info_(device_info), |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 43 | failure_callback_(failure_callback), |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 44 | 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 Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 50 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 51 | LinkMonitor::~LinkMonitor() { |
| 52 | Stop(); |
| 53 | } |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 54 | |
| 55 | bool LinkMonitor::Start() { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 56 | Stop(); |
| 57 | |
| 58 | if (!device_info_->GetMACAddress( |
| 59 | connection_->interface_index(), &local_mac_address_)) { |
| 60 | LOG(ERROR) << "Could not get local MAC address."; |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 61 | metrics_->NotifyLinkMonitorFailure( |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame^] | 62 | connection_->technology(), |
| 63 | Metrics::kLinkMonitorMacAddressNotFound, |
| 64 | 0, 0, 0); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 65 | 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 Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame^] | 71 | time_->GetTimeMonotonic(&started_monitoring_at_); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 72 | return SendRequest(); |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void LinkMonitor::Stop() { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 76 | 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 Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame^] | 87 | timerclear(&started_monitoring_at_); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 88 | timerclear(&sent_request_at_); |
| 89 | } |
| 90 | |
| 91 | unsigned int LinkMonitor::GetResponseTimeMilliseconds() { |
| 92 | return response_sample_count_ ? |
| 93 | response_sample_bucket_ / response_sample_count_ : 0; |
| 94 | } |
| 95 | |
| 96 | void LinkMonitor::AddResponseTimeSample( |
| 97 | unsigned int response_time_milliseconds) { |
| 98 | SLOG(Link, 2) << "In " << __func__ << " with sample " |
| 99 | << response_time_milliseconds << "."; |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 100 | metrics_->NotifyLinkMonitorResponseTimeSampleAdded( |
| 101 | connection_->technology(), response_time_milliseconds); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 102 | 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 |
| 113 | string 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 | |
| 122 | bool LinkMonitor::CreateClient() { |
| 123 | arp_client_.reset(new ArpClient(connection_->interface_index())); |
| 124 | |
| 125 | if (!arp_client_->Start()) { |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 126 | return false; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 127 | } |
| 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 | |
| 136 | bool 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 Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame^] | 153 | |
| 154 | struct timeval now, elapsed_time; |
| 155 | time_->GetTimeMonotonic(&now); |
| 156 | timersub(&now, &started_monitoring_at_, &elapsed_time); |
| 157 | |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 158 | metrics_->NotifyLinkMonitorFailure( |
| 159 | connection_->technology(), |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame^] | 160 | Metrics::kLinkMonitorFailureThresholdReached, |
| 161 | elapsed_time.tv_sec, |
| 162 | broadcast_failure_count_, |
| 163 | unicast_failure_count_); |
| 164 | |
| 165 | Stop(); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | is_unicast_ = !is_unicast_; |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | void 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 | |
| 225 | bool 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 Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 231 | metrics_->NotifyLinkMonitorFailure( |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame^] | 232 | connection_->technology(), |
| 233 | Metrics::kLinkMonitorClientStartFailure, |
| 234 | 0, 0, 0); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 235 | 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 Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 268 | metrics_->NotifyLinkMonitorFailure( |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame^] | 269 | connection_->technology(), Metrics::kLinkMonitorTransmitFailure, |
| 270 | 0, 0, 0); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 271 | 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 | |
| 281 | void LinkMonitor::SendRequestTask() { |
| 282 | SendRequest(); |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | } // namespace shill |