| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 #ifndef NET_QUIC_TEST_TOOLS_SIMULATOR_TRAFFIC_POLICER_H_ | 5 #ifndef NET_QUIC_TEST_TOOLS_SIMULATOR_TRAFFIC_POLICER_H_ |
| 6 #define NET_QUIC_TEST_TOOLS_SIMULATOR_TRAFFIC_POLICER_H_ | 6 #define NET_QUIC_TEST_TOOLS_SIMULATOR_TRAFFIC_POLICER_H_ |
| 7 | 7 |
| 8 #include <unordered_map> | 8 #include <unordered_map> |
| 9 | 9 |
| 10 #include "net/quic/test_tools/simulator/packet_filter.h" |
| 10 #include "net/quic/test_tools/simulator/port.h" | 11 #include "net/quic/test_tools/simulator/port.h" |
| 11 | 12 |
| 12 namespace net { | 13 namespace net { |
| 13 namespace simulator { | 14 namespace simulator { |
| 14 | 15 |
| 15 // Traffic policer uses a token bucket to limit the bandwidth of the traffic | 16 // Traffic policer uses a token bucket to limit the bandwidth of the traffic |
| 16 // passing through. It wraps around an input port and exposes an output port. | 17 // passing through. It wraps around an input port and exposes an output port. |
| 17 // Only the traffic from input to the output is policed, so in case when | 18 // Only the traffic from input to the output is policed, so in case when |
| 18 // bidirectional policing is desired, two policers have to be used. The flows | 19 // bidirectional policing is desired, two policers have to be used. The flows |
| 19 // are hashed by the source only. | 20 // are hashed by the destination only. |
| 20 class TrafficPolicer : public Actor, public ConstrainedPortInterface { | 21 class TrafficPolicer : public PacketFilter { |
| 21 public: | 22 public: |
| 22 TrafficPolicer(Simulator* simulator, | 23 TrafficPolicer(Simulator* simulator, |
| 23 std::string name, | 24 std::string name, |
| 24 QuicByteCount initial_bucket_size, | 25 QuicByteCount initial_bucket_size, |
| 25 QuicByteCount max_bucket_size, | 26 QuicByteCount max_bucket_size, |
| 26 QuicBandwidth target_bandwidth, | 27 QuicBandwidth target_bandwidth, |
| 27 Endpoint* input); | 28 Endpoint* input); |
| 28 ~TrafficPolicer() override; | 29 ~TrafficPolicer() override; |
| 29 | 30 |
| 30 Endpoint* input() { return input_; } | 31 protected: |
| 31 Endpoint* output() { return &output_; } | 32 bool FilterPacket(const Packet& packet) override; |
| 32 | |
| 33 void AcceptPacket(std::unique_ptr<Packet> packet) override; | |
| 34 QuicTime::Delta TimeUntilAvailable() override; | |
| 35 | |
| 36 void Act() override; | |
| 37 | 33 |
| 38 private: | 34 private: |
| 39 class Output : public Endpoint { | |
| 40 public: | |
| 41 explicit Output(TrafficPolicer* policer); | |
| 42 ~Output() override; | |
| 43 | |
| 44 UnconstrainedPortInterface* GetRxPort() override; | |
| 45 void SetTxPort(ConstrainedPortInterface* port) override; | |
| 46 void Act() override; | |
| 47 | |
| 48 private: | |
| 49 TrafficPolicer* policer_; | |
| 50 }; | |
| 51 | |
| 52 // Refill the token buckets with all the tokens that have been granted since | 35 // Refill the token buckets with all the tokens that have been granted since |
| 53 // |last_refill_time_|. | 36 // |last_refill_time_|. |
| 54 void Refill(); | 37 void Refill(); |
| 55 | 38 |
| 56 QuicByteCount initial_bucket_size_; | 39 QuicByteCount initial_bucket_size_; |
| 57 QuicByteCount max_bucket_size_; | 40 QuicByteCount max_bucket_size_; |
| 58 QuicBandwidth target_bandwidth_; | 41 QuicBandwidth target_bandwidth_; |
| 59 | 42 |
| 60 // The time at which the token buckets were last refilled. | 43 // The time at which the token buckets were last refilled. |
| 61 QuicTime last_refill_time_; | 44 QuicTime last_refill_time_; |
| 62 | 45 |
| 63 ConstrainedPortInterface* output_tx_port_; | |
| 64 | |
| 65 Endpoint* input_; | |
| 66 Output output_; | |
| 67 | |
| 68 // Maps each destination to the number of tokens it has left. | 46 // Maps each destination to the number of tokens it has left. |
| 69 std::unordered_map<std::string, QuicByteCount> token_buckets_; | 47 std::unordered_map<std::string, QuicByteCount> token_buckets_; |
| 70 | 48 |
| 71 DISALLOW_COPY_AND_ASSIGN(TrafficPolicer); | 49 DISALLOW_COPY_AND_ASSIGN(TrafficPolicer); |
| 72 }; | 50 }; |
| 73 | 51 |
| 74 } // namespace simulator | 52 } // namespace simulator |
| 75 } // namespace net | 53 } // namespace net |
| 76 | 54 |
| 77 #endif // NET_QUIC_TEST_TOOLS_SIMULATOR_TRAFFIC_POLICER_H_ | 55 #endif // NET_QUIC_TEST_TOOLS_SIMULATOR_TRAFFIC_POLICER_H_ |
| OLD | NEW |