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