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 | #ifndef SHILL_TRAFFIC_MONITOR_H_ |
| 6 | #define SHILL_TRAFFIC_MONITOR_H_ |
| 7 | |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 13 | #include <base/callback.h> |
| 14 | #include <base/cancelable_callback.h> |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 15 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 16 | |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 17 | #include "shill/connection_info.h" |
| 18 | #include "shill/connection_info_reader.h" |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 19 | #include "shill/refptr_types.h" |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 20 | #include "shill/socket_info.h" |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 21 | |
| 22 | namespace shill { |
| 23 | |
| 24 | class EventDispatcher; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 25 | class SocketInfoReader; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 26 | |
| 27 | // TrafficMonitor detects certain abnormal scenarios on a network interface |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 28 | // and notifies an observer of various scenarios via callbacks. |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 29 | class TrafficMonitor { |
| 30 | public: |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 31 | // Network problem detected by traffic monitor. |
| 32 | enum NetworkProblem { |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 33 | kNetworkProblemCongestedTxQueue = 0, |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 34 | kNetworkProblemDNSFailure, |
| 35 | kNetworkProblemMax |
| 36 | }; |
| 37 | |
| 38 | typedef base::Callback<void(int)> NetworkProblemDetectedCallback; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 39 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 40 | TrafficMonitor(const DeviceRefPtr& device, EventDispatcher* dispatcher); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 41 | virtual ~TrafficMonitor(); |
| 42 | |
| 43 | // Starts traffic monitoring on the selected device. |
| 44 | virtual void Start(); |
| 45 | |
| 46 | // Stops traffic monitoring on the selected device. |
| 47 | virtual void Stop(); |
| 48 | |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 49 | // Sets the callback to invoke, if the traffic monitor detects a network |
| 50 | // problem, either too many packets are failing to get transmitted over a |
| 51 | // TCP connection or DNS is failing. |
| 52 | void set_network_problem_detected_callback( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 53 | const NetworkProblemDetectedCallback& callback) { |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 54 | network_problem_detected_callback_ = callback; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private: |
| 58 | friend class TrafficMonitorTest; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 59 | FRIEND_TEST(TrafficMonitorTest, |
| 60 | BuildIPPortToTxQueueLengthInvalidConnectionState); |
| 61 | FRIEND_TEST(TrafficMonitorTest, BuildIPPortToTxQueueLengthInvalidDevice); |
| 62 | FRIEND_TEST(TrafficMonitorTest, BuildIPPortToTxQueueLengthInvalidTimerState); |
| 63 | FRIEND_TEST(TrafficMonitorTest, BuildIPPortToTxQueueLengthMultipleEntries); |
| 64 | FRIEND_TEST(TrafficMonitorTest, BuildIPPortToTxQueueLengthValid); |
| 65 | FRIEND_TEST(TrafficMonitorTest, BuildIPPortToTxQueueLengthZero); |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 66 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficDnsFailureThenSuccess); |
| 67 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficDnsOutstanding); |
| 68 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficDnsStatsReset); |
| 69 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficDnsSuccessful); |
| 70 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficDnsTimedOut); |
| 71 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficDnsTimedOutInvalidProtocol); |
| 72 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficDnsTimedOutInvalidSourceIp); |
| 73 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficDnsTimedOutOutsideTimeWindow); |
| 74 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficNonDnsTimedOut); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 75 | FRIEND_TEST(TrafficMonitorTest, |
| 76 | SampleTrafficStuckTxQueueIncreasingQueueLength); |
| 77 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficStuckTxQueueSameQueueLength); |
| 78 | FRIEND_TEST(TrafficMonitorTest, |
| 79 | SampleTrafficStuckTxQueueVariousQueueLengths); |
| 80 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficUnstuckTxQueueNoConnection); |
| 81 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficUnstuckTxQueueStateChanged); |
| 82 | FRIEND_TEST(TrafficMonitorTest, SampleTrafficUnstuckTxQueueZeroQueueLength); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 83 | FRIEND_TEST(TrafficMonitorTest, StartAndStop); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 84 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 85 | typedef std::map<std::string, uint64_t> IPPortToTxQueueLengthMap; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 86 | |
| 87 | // The minimum number of samples that indicate an abnormal scenario |
| 88 | // required to trigger the callback. |
| 89 | static const int kMinimumFailedSamplesToTrigger; |
| 90 | // The frequency at which to sample the TCP connections. |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 91 | static const int64_t kSamplingIntervalMilliseconds; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 92 | // DNS port. |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 93 | static const uint16_t kDnsPort; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 94 | // If a DNS "connection" time-to-expire falls below this threshold, then |
| 95 | // it's considered a timed out DNS request. |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 96 | static const int64_t kDnsTimedOutThresholdSeconds; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 97 | |
| 98 | // Resets congested tx-queues tracking statistics. |
| 99 | void ResetCongestedTxQueuesStats(); |
| 100 | void ResetCongestedTxQueuesStatsWithLogging(); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 101 | |
| 102 | // Builds map of IP address/port to tx queue lengths from socket info vector. |
| 103 | // Skips sockets not on device, tx queue length is 0, connection state is not |
| 104 | // established or does not have a pending retransmit timer. |
| 105 | void BuildIPPortToTxQueueLength( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 106 | const std::vector<SocketInfo>& socket_infos, |
| 107 | IPPortToTxQueueLengthMap* tx_queue_length); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 108 | |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 109 | // Checks for congested tx-queue via network statistics. |
| 110 | // Returns |true| if tx-queue is congested. |
| 111 | bool IsCongestedTxQueues(); |
| 112 | |
| 113 | // Resets failing DNS queries tracking statistics. |
| 114 | void ResetDnsFailingStats(); |
| 115 | void ResetDnsFailingStatsWithLogging(); |
| 116 | |
| 117 | // Checks to see for failed DNS queries. |
| 118 | bool IsDnsFailing(); |
| 119 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 120 | // Samples traffic (e.g. receive and transmit byte counts) on the |
| 121 | // selected device and invokes appropriate callbacks when certain |
| 122 | // abnormal scenarios are detected. |
| 123 | void SampleTraffic(); |
| 124 | |
| 125 | // The device on which to perform traffic monitoring. |
| 126 | DeviceRefPtr device_; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 127 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 128 | // Dispatcher on which to create delayed tasks. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 129 | EventDispatcher* dispatcher_; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 130 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 131 | // Callback to invoke when TrafficMonitor needs to sample traffic |
| 132 | // of the network interface. |
| 133 | base::CancelableClosure sample_traffic_callback_; |
| 134 | |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 135 | // Callback to invoke when we detect a network problem. Possible network |
| 136 | // problems that can be detected are congested TCP TX queue and DNS failure. |
| 137 | // Refer to enum NetworkProblem for all possible network problems that can be |
| 138 | // detected by Traffic Monitor. |
| 139 | NetworkProblemDetectedCallback network_problem_detected_callback_; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 140 | |
| 141 | // Reads and parses socket information from the system. |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 142 | std::unique_ptr<SocketInfoReader> socket_info_reader_; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 143 | |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 144 | // Number of consecutive congested tx-queue cases sampled. |
| 145 | int accummulated_congested_tx_queues_samples_; |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 146 | |
| 147 | // Map of tx queue lengths from previous sampling pass. |
| 148 | IPPortToTxQueueLengthMap old_tx_queue_lengths_; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 149 | |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 150 | // Reads and parses connection information from the system. |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 151 | std::unique_ptr<ConnectionInfoReader> connection_info_reader_; |
Thieu Le | fa7960e | 2013-04-15 13:14:55 -0700 | [diff] [blame] | 152 | |
| 153 | // Number of consecutive sample intervals that contains failed DNS requests. |
| 154 | int accummulated_dns_failures_samples_; |
| 155 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 156 | DISALLOW_COPY_AND_ASSIGN(TrafficMonitor); |
| 157 | }; |
| 158 | |
| 159 | } // namespace shill |
| 160 | |
| 161 | #endif // SHILL_TRAFFIC_MONITOR_H_ |