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