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 | 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 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 11 | #include "shill/active_link_monitor.h" |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 12 | #include "shill/connection.h" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 13 | #include "shill/device_info.h" |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 14 | #include "shill/event_dispatcher.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 15 | #include "shill/logging.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 16 | #include "shill/net/shill_time.h" |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 17 | #include "shill/passive_link_monitor.h" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 18 | |
| 19 | using base::Bind; |
| 20 | using base::Unretained; |
| 21 | using std::string; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 22 | |
| 23 | namespace shill { |
| 24 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 25 | namespace Logging { |
| 26 | static auto kModuleLogScope = ScopeLogger::kLink; |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 27 | static string ObjectID(Connection* c) { return c->interface_name(); } |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 30 | const int LinkMonitor::kDefaultTestPeriodMilliseconds = |
| 31 | ActiveLinkMonitor::kDefaultTestPeriodMilliseconds; |
| 32 | const int LinkMonitor::kFailureThreshold = |
| 33 | ActiveLinkMonitor::kFailureThreshold; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 34 | const char LinkMonitor::kDefaultLinkMonitorTechnologies[] = "wifi"; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 35 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 36 | LinkMonitor::LinkMonitor(const ConnectionRefPtr& connection, |
| 37 | EventDispatcher* dispatcher, |
| 38 | Metrics* metrics, |
| 39 | DeviceInfo* device_info, |
| 40 | const FailureCallback& failure_callback, |
| 41 | const GatewayChangeCallback& gateway_change_callback) |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 42 | : connection_(connection), |
| 43 | dispatcher_(dispatcher), |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 44 | metrics_(metrics), |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 45 | failure_callback_(failure_callback), |
Peter Qiu | b5d124f | 2014-04-14 12:05:02 -0700 | [diff] [blame] | 46 | gateway_change_callback_(gateway_change_callback), |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 47 | active_link_monitor_( |
| 48 | new ActiveLinkMonitor( |
| 49 | connection, |
| 50 | dispatcher, |
| 51 | metrics, |
| 52 | device_info, |
| 53 | Bind(&LinkMonitor::OnActiveLinkMonitorFailure, |
| 54 | Unretained(this)), |
| 55 | Bind(&LinkMonitor::OnActiveLinkMonitorSuccess, |
| 56 | Unretained(this)))), |
| 57 | passive_link_monitor_( |
| 58 | new PassiveLinkMonitor( |
| 59 | connection, |
| 60 | dispatcher, |
| 61 | Bind(&LinkMonitor::OnPassiveLinkMonitorResultCallback, |
| 62 | Unretained(this)))), |
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(); |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 72 | time_->GetTimeMonotonic(&started_monitoring_at_); |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 73 | // Start active link monitor. |
| 74 | return active_link_monitor_->Start( |
| 75 | ActiveLinkMonitor::kDefaultTestPeriodMilliseconds); |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void LinkMonitor::Stop() { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 79 | SLOG(connection_.get(), 2) << "In " << __func__ << "."; |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 80 | timerclear(&started_monitoring_at_); |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 81 | active_link_monitor_->Stop(); |
| 82 | passive_link_monitor_->Stop(); |
| 83 | gateway_mac_address_.Clear(); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 84 | } |
| 85 | |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 86 | void LinkMonitor::OnAfterResume() { |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 87 | // Preserve gateway settings across resume. |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 88 | ByteString prior_gateway_mac_address(gateway_mac_address_); |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 89 | bool gateway_supports_unicast_arp = |
| 90 | active_link_monitor_->gateway_supports_unicast_arp(); |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 91 | Stop(); |
| 92 | gateway_mac_address_ = prior_gateway_mac_address; |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 93 | active_link_monitor_->set_gateway_mac_address(gateway_mac_address_); |
| 94 | active_link_monitor_->set_gateway_supports_unicast_arp( |
| 95 | gateway_supports_unicast_arp); |
| 96 | |
| 97 | active_link_monitor_->Start(ActiveLinkMonitor::kFastTestPeriodMilliseconds); |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 100 | int LinkMonitor::GetResponseTimeMilliseconds() const { |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 101 | return active_link_monitor_->GetResponseTimeMilliseconds(); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 104 | bool LinkMonitor::IsGatewayFound() const { |
| 105 | return !gateway_mac_address_.IsZero(); |
| 106 | } |
| 107 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 108 | void LinkMonitor::OnActiveLinkMonitorFailure( |
| 109 | Metrics::LinkMonitorFailure failure, |
| 110 | int broadcast_failure_count, |
| 111 | int unicast_failure_count) { |
| 112 | failure_callback_.Run(); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 113 | |
| 114 | struct timeval now, elapsed_time; |
| 115 | time_->GetTimeMonotonic(&now); |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 116 | timersub(&now, &started_monitoring_at_, &elapsed_time); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 117 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 118 | metrics_->NotifyLinkMonitorFailure( |
| 119 | connection_->technology(), |
| 120 | failure, |
| 121 | elapsed_time.tv_sec, |
| 122 | broadcast_failure_count, |
| 123 | unicast_failure_count); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 124 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 125 | Stop(); |
| 126 | } |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 127 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 128 | void LinkMonitor::OnActiveLinkMonitorSuccess() { |
| 129 | if (!gateway_mac_address_.Equals( |
| 130 | active_link_monitor_->gateway_mac_address())) { |
| 131 | gateway_mac_address_ = active_link_monitor_->gateway_mac_address(); |
Peter Qiu | b5d124f | 2014-04-14 12:05:02 -0700 | [diff] [blame] | 132 | // Notify device of the new gateway mac address. |
| 133 | gateway_change_callback_.Run(); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 136 | // Start passive link monitoring. |
| 137 | passive_link_monitor_->Start(PassiveLinkMonitor::kDefaultMonitorCycles); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 140 | void LinkMonitor::OnPassiveLinkMonitorResultCallback(bool status) { |
| 141 | // TODO(zqiu): Add metrics for tracking passive link monitor results. |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 142 | |
Peter Qiu | ffa5637 | 2015-01-22 14:25:23 -0800 | [diff] [blame] | 143 | // Start active monitor |
| 144 | active_link_monitor_->Start( |
| 145 | ActiveLinkMonitor::kDefaultTestPeriodMilliseconds); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 148 | } // namespace shill |