Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 1 | // Copyright 2015 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/passive_link_monitor.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/bind.h> |
| 10 | |
| 11 | #include "shill/arp_client.h" |
| 12 | #include "shill/arp_packet.h" |
| 13 | #include "shill/connection.h" |
| 14 | #include "shill/event_dispatcher.h" |
| 15 | #include "shill/logging.h" |
| 16 | #include "shill/net/byte_string.h" |
| 17 | |
| 18 | using base::Bind; |
| 19 | using base::Unretained; |
| 20 | using std::string; |
| 21 | |
| 22 | namespace shill { |
| 23 | |
| 24 | namespace Logging { |
| 25 | static auto kModuleLogScope = ScopeLogger::kLink; |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 26 | static string ObjectID(Connection* c) { return c->interface_name(); } |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // static. |
| 30 | const int PassiveLinkMonitor::kDefaultMonitorCycles = 40; |
| 31 | const int PassiveLinkMonitor::kCyclePeriodMilliseconds = 25000; |
| 32 | const int PassiveLinkMonitor::kMinArpRequestsPerCycle = 5; |
| 33 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 34 | PassiveLinkMonitor::PassiveLinkMonitor(const ConnectionRefPtr& connection, |
| 35 | EventDispatcher* dispatcher, |
| 36 | const ResultCallback& result_callback) |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 37 | : connection_(connection), |
| 38 | dispatcher_(dispatcher), |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 39 | // Connection is not provided when this is used as a mock for testing |
| 40 | // purpose. |
| 41 | arp_client_( |
| 42 | new ArpClient(connection ? connection->interface_index() : 0)), |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 43 | result_callback_(result_callback), |
| 44 | num_cycles_to_monitor_(kDefaultMonitorCycles), |
| 45 | num_requests_received_(0), |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 46 | num_cycles_passed_(0) { |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | PassiveLinkMonitor::~PassiveLinkMonitor() { |
| 50 | Stop(); |
| 51 | } |
| 52 | |
| 53 | bool PassiveLinkMonitor::Start(int num_cycles) { |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 54 | SLOG(connection_.get(), 2) << "In " << __func__ << "."; |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 55 | Stop(); |
| 56 | |
| 57 | if (!StartArpClient()) { |
| 58 | return false; |
| 59 | } |
| 60 | // Start the monitor cycle. |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 61 | monitor_cycle_timeout_callback_.Reset( |
| 62 | Bind(&PassiveLinkMonitor::CycleTimeoutHandler, Unretained(this))); |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 63 | dispatcher_->PostDelayedTask(monitor_cycle_timeout_callback_.callback(), |
| 64 | kCyclePeriodMilliseconds); |
| 65 | num_cycles_to_monitor_ = num_cycles; |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | void PassiveLinkMonitor::Stop() { |
| 70 | SLOG(connection_.get(), 2) << "In " << __func__ << "."; |
| 71 | StopArpClient(); |
| 72 | num_requests_received_ = 0; |
| 73 | num_cycles_passed_ = 0; |
| 74 | monitor_cycle_timeout_callback_.Cancel(); |
Peter Qiu | bf767f2 | 2015-03-10 16:55:19 -0700 | [diff] [blame] | 75 | monitor_completed_callback_.Cancel(); |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | bool PassiveLinkMonitor::StartArpClient() { |
| 79 | if (!arp_client_->StartRequestListener()) { |
| 80 | return false; |
| 81 | } |
| 82 | receive_request_handler_.reset( |
| 83 | dispatcher_->CreateReadyHandler( |
| 84 | arp_client_->socket(), |
| 85 | IOHandler::kModeInput, |
| 86 | Bind(&PassiveLinkMonitor::ReceiveRequest, Unretained(this)))); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | void PassiveLinkMonitor::StopArpClient() { |
| 91 | arp_client_->Stop(); |
| 92 | receive_request_handler_.reset(); |
| 93 | } |
| 94 | |
| 95 | void PassiveLinkMonitor::ReceiveRequest(int fd) { |
| 96 | SLOG(connection_.get(), 2) << "In " << __func__ << "."; |
| 97 | ArpPacket packet; |
| 98 | ByteString sender; |
| 99 | |
| 100 | if (!arp_client_->ReceivePacket(&packet, &sender)) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if (packet.IsReply()) { |
| 105 | SLOG(connection_.get(), 4) << "This is not a request packet. Ignoring."; |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | num_requests_received_++; |
| 110 | // Stop ARP client if we receive enough requests for the current cycle. |
| 111 | if (num_requests_received_ >= kMinArpRequestsPerCycle) { |
| 112 | StopArpClient(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void PassiveLinkMonitor::CycleTimeoutHandler() { |
| 117 | bool status = false; |
| 118 | if (num_requests_received_ >= kMinArpRequestsPerCycle) { |
| 119 | num_requests_received_ = 0; |
| 120 | num_cycles_passed_++; |
| 121 | if (num_cycles_passed_ < num_cycles_to_monitor_) { |
| 122 | // Continue on with the next cycle. |
| 123 | StartArpClient(); |
| 124 | dispatcher_->PostDelayedTask(monitor_cycle_timeout_callback_.callback(), |
| 125 | kCyclePeriodMilliseconds); |
| 126 | return; |
| 127 | } |
| 128 | // Monitor completed. |
| 129 | status = true; |
| 130 | } |
| 131 | |
| 132 | // Post a task to perform cleanup and invoke result callback, since this |
| 133 | // function is invoked from the callback that will be cancelled during |
| 134 | // cleanup. |
Peter Qiu | bf767f2 | 2015-03-10 16:55:19 -0700 | [diff] [blame] | 135 | monitor_completed_callback_.Reset( |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 136 | Bind(&PassiveLinkMonitor::MonitorCompleted, Unretained(this), status)); |
Peter Qiu | bf767f2 | 2015-03-10 16:55:19 -0700 | [diff] [blame] | 137 | dispatcher_->PostTask(monitor_completed_callback_.callback()); |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void PassiveLinkMonitor::MonitorCompleted(bool status) { |
| 141 | // Stop the monitoring before invoking result callback, so that the ARP client |
| 142 | // is stopped by the time result callback is invoked. |
| 143 | Stop(); |
| 144 | result_callback_.Run(status); |
| 145 | } |
| 146 | |
| 147 | } // namespace shill |