Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 1 | // Copyright (c) 2013 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/traffic_monitor.h" |
| 6 | |
| 7 | #include <base/bind.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 8 | #include <base/strings/stringprintf.h> |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 9 | #include <netinet/in.h> |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 10 | |
| 11 | #include "shill/device.h" |
| 12 | #include "shill/device_info.h" |
| 13 | #include "shill/event_dispatcher.h" |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 14 | #include "shill/logging.h" |
| 15 | #include "shill/socket_info_reader.h" |
| 16 | |
| 17 | using base::StringPrintf; |
| 18 | using std::string; |
| 19 | using std::vector; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 20 | |
| 21 | namespace shill { |
| 22 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 23 | namespace Logging { |
| 24 | static auto kModuleLogScope = ScopeLogger::kLink; |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 25 | static string ObjectID(Device* d) { return d->link_name(); } |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 28 | // static |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 29 | const uint16_t TrafficMonitor::kDnsPort = 53; |
| 30 | const int64_t TrafficMonitor::kDnsTimedOutThresholdSeconds = 15; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 31 | const int TrafficMonitor::kMinimumFailedSamplesToTrigger = 2; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 32 | const int64_t TrafficMonitor::kSamplingIntervalMilliseconds = 5000; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 33 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 34 | TrafficMonitor::TrafficMonitor(const DeviceRefPtr& device, |
| 35 | EventDispatcher* dispatcher) |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 36 | : device_(device), |
| 37 | dispatcher_(dispatcher), |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 38 | socket_info_reader_(new SocketInfoReader), |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 39 | accummulated_congested_tx_queues_samples_(0), |
| 40 | connection_info_reader_(new ConnectionInfoReader), |
| 41 | accummulated_dns_failures_samples_(0) { |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TrafficMonitor::~TrafficMonitor() { |
| 45 | Stop(); |
| 46 | } |
| 47 | |
| 48 | void TrafficMonitor::Start() { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 49 | SLOG(device_.get(), 2) << __func__; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 50 | Stop(); |
| 51 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 52 | sample_traffic_callback_.Reset(base::Bind(&TrafficMonitor::SampleTraffic, |
| 53 | base::Unretained(this))); |
| 54 | dispatcher_->PostDelayedTask(sample_traffic_callback_.callback(), |
| 55 | kSamplingIntervalMilliseconds); |
| 56 | } |
| 57 | |
| 58 | void TrafficMonitor::Stop() { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 59 | SLOG(device_.get(), 2) << __func__; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 60 | sample_traffic_callback_.Cancel(); |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 61 | ResetCongestedTxQueuesStats(); |
| 62 | ResetDnsFailingStats(); |
| 63 | } |
| 64 | |
| 65 | void TrafficMonitor::ResetCongestedTxQueuesStats() { |
| 66 | accummulated_congested_tx_queues_samples_ = 0; |
| 67 | } |
| 68 | |
| 69 | void TrafficMonitor::ResetCongestedTxQueuesStatsWithLogging() { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 70 | SLOG(device_.get(), 2) << __func__ << ": Tx-queues decongested"; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 71 | ResetCongestedTxQueuesStats(); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void TrafficMonitor::BuildIPPortToTxQueueLength( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 75 | const vector<SocketInfo>& socket_infos, |
| 76 | IPPortToTxQueueLengthMap* tx_queue_lengths) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 77 | SLOG(device_.get(), 3) << __func__; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 78 | string device_ip_address = device_->ipconfig()->properties().address; |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 79 | for (const auto& info : socket_infos) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 80 | SLOG(device_.get(), 4) << "SocketInfo(IP=" |
| 81 | << info.local_ip_address().ToString() |
| 82 | << ", TX=" << info.transmit_queue_value() |
| 83 | << ", State=" << info.connection_state() |
| 84 | << ", TimerState=" << info.timer_state(); |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 85 | if (info.local_ip_address().ToString() != device_ip_address || |
| 86 | info.transmit_queue_value() == 0 || |
| 87 | info.connection_state() != SocketInfo::kConnectionStateEstablished || |
| 88 | (info.timer_state() != SocketInfo::kTimerStateRetransmitTimerPending && |
| 89 | info.timer_state() != |
Arman Uguray | d42d8ec | 2013-04-08 19:28:21 -0700 | [diff] [blame] | 90 | SocketInfo::kTimerStateZeroWindowProbeTimerPending)) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 91 | SLOG(device_.get(), 4) << "Connection Filtered."; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 92 | continue; |
Arman Uguray | d42d8ec | 2013-04-08 19:28:21 -0700 | [diff] [blame] | 93 | } |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 94 | SLOG(device_.get(), 3) << "Monitoring connection: TX=" |
| 95 | << info.transmit_queue_value() |
| 96 | << " TimerState=" << info.timer_state(); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 97 | |
| 98 | string local_ip_port = |
| 99 | StringPrintf("%s:%d", |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 100 | info.local_ip_address().ToString().c_str(), |
| 101 | info.local_port()); |
| 102 | (*tx_queue_lengths)[local_ip_port] = info.transmit_queue_value(); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 103 | } |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 106 | bool TrafficMonitor::IsCongestedTxQueues() { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 107 | SLOG(device_.get(), 4) << __func__; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 108 | vector<SocketInfo> socket_infos; |
| 109 | if (!socket_info_reader_->LoadTcpSocketInfo(&socket_infos) || |
| 110 | socket_infos.empty()) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 111 | SLOG(device_.get(), 3) << __func__ << ": Empty socket info"; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 112 | ResetCongestedTxQueuesStatsWithLogging(); |
| 113 | return false; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 114 | } |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 115 | bool congested_tx_queues = true; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 116 | IPPortToTxQueueLengthMap curr_tx_queue_lengths; |
| 117 | BuildIPPortToTxQueueLength(socket_infos, &curr_tx_queue_lengths); |
| 118 | if (curr_tx_queue_lengths.empty()) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 119 | SLOG(device_.get(), 3) << __func__ << ": No interesting socket info"; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 120 | ResetCongestedTxQueuesStatsWithLogging(); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 121 | } else { |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 122 | for (const auto& length_entry : old_tx_queue_lengths_) { |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 123 | IPPortToTxQueueLengthMap::iterator curr_tx_queue_it = |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 124 | curr_tx_queue_lengths.find(length_entry.first); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 125 | if (curr_tx_queue_it == curr_tx_queue_lengths.end() || |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 126 | curr_tx_queue_it->second < length_entry.second) { |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 127 | congested_tx_queues = false; |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 128 | // TODO(armansito): If we had a false positive earlier, we may |
| 129 | // want to correct it here by invoking a "connection back to normal |
| 130 | // callback", so that the OutOfCredits property can be set to |
| 131 | // false. |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | } |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 135 | if (congested_tx_queues) { |
| 136 | ++accummulated_congested_tx_queues_samples_; |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 137 | SLOG(device_.get(), 2) << __func__ |
| 138 | << ": Congested tx-queues detected (" |
| 139 | << accummulated_congested_tx_queues_samples_ |
| 140 | << ")"; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 143 | old_tx_queue_lengths_ = curr_tx_queue_lengths; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 144 | |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 145 | return congested_tx_queues; |
| 146 | } |
| 147 | |
| 148 | void TrafficMonitor::ResetDnsFailingStats() { |
| 149 | accummulated_dns_failures_samples_ = 0; |
| 150 | } |
| 151 | |
| 152 | void TrafficMonitor::ResetDnsFailingStatsWithLogging() { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 153 | SLOG(device_.get(), 2) << __func__ << ": DNS queries restored"; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 154 | ResetDnsFailingStats(); |
| 155 | } |
| 156 | |
| 157 | bool TrafficMonitor::IsDnsFailing() { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 158 | SLOG(device_.get(), 4) << __func__; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 159 | vector<ConnectionInfo> connection_infos; |
| 160 | if (!connection_info_reader_->LoadConnectionInfo(&connection_infos) || |
| 161 | connection_infos.empty()) { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 162 | SLOG(device_.get(), 3) << __func__ << ": Empty connection info"; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 163 | } else { |
| 164 | // The time-to-expire counter is used to determine when a DNS request |
| 165 | // has timed out. This counter is the number of seconds remaining until |
| 166 | // the entry is removed from the system IP connection tracker. The |
| 167 | // default time is 30 seconds. This is too long of a wait. Instead, we |
| 168 | // want to time out at |kDnsTimedOutThresholdSeconds|. Unfortunately, |
| 169 | // we cannot simply look for entries less than |
| 170 | // |kDnsTimedOutThresholdSeconds| because we will count the entry |
| 171 | // multiple times once its time-to-expire is less than |
| 172 | // |kDnsTimedOutThresholdSeconds|. To ensure that we only count an |
| 173 | // entry once, we look for entries in this time window between |
| 174 | // |kDnsTimedOutThresholdSeconds| and |kDnsTimedOutLowerThresholdSeconds|. |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 175 | const int64_t kDnsTimedOutLowerThresholdSeconds = |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 176 | kDnsTimedOutThresholdSeconds - kSamplingIntervalMilliseconds / 1000; |
| 177 | string device_ip_address = device_->ipconfig()->properties().address; |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 178 | for (const auto& info : connection_infos) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 179 | if (info.protocol() != IPPROTO_UDP || |
| 180 | info.time_to_expire_seconds() > kDnsTimedOutThresholdSeconds || |
| 181 | info.time_to_expire_seconds() <= kDnsTimedOutLowerThresholdSeconds || |
| 182 | !info.is_unreplied() || |
| 183 | info.original_source_ip_address().ToString() != device_ip_address || |
| 184 | info.original_destination_port() != kDnsPort) |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 185 | continue; |
| 186 | |
| 187 | ++accummulated_dns_failures_samples_; |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 188 | SLOG(device_.get(), 2) << __func__ |
| 189 | << ": DNS failures detected (" |
| 190 | << accummulated_dns_failures_samples_ << ")"; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | } |
| 194 | ResetDnsFailingStatsWithLogging(); |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | void TrafficMonitor::SampleTraffic() { |
Alex Vakulenko | 0951ccb | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 199 | SLOG(device_.get(), 3) << __func__; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 200 | |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 201 | // Schedule the sample callback first, so it is possible for the network |
| 202 | // problem callback to stop the traffic monitor. |
| 203 | dispatcher_->PostDelayedTask(sample_traffic_callback_.callback(), |
| 204 | kSamplingIntervalMilliseconds); |
| 205 | |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 206 | if (IsCongestedTxQueues() && |
| 207 | accummulated_congested_tx_queues_samples_ == |
| 208 | kMinimumFailedSamplesToTrigger) { |
| 209 | LOG(WARNING) << "Congested tx queues detected, out-of-credits?"; |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 210 | network_problem_detected_callback_.Run(kNetworkProblemCongestedTxQueue); |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 211 | } else if (IsDnsFailing() && |
| 212 | accummulated_dns_failures_samples_ == |
| 213 | kMinimumFailedSamplesToTrigger) { |
| 214 | LOG(WARNING) << "DNS queries failing, out-of-credits?"; |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 215 | network_problem_detected_callback_.Run(kNetworkProblemDNSFailure); |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 216 | } |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | } // namespace shill |