OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 #ifndef NET_QUIC_TEST_TOOLS_SIMULATOR_TRAFFIC_POLICER_H_ |
| 6 #define NET_QUIC_TEST_TOOLS_SIMULATOR_TRAFFIC_POLICER_H_ |
| 7 |
| 8 #include <unordered_map> |
| 9 |
| 10 #include "net/quic/test_tools/simulator/port.h" |
| 11 |
| 12 namespace net { |
| 13 namespace simulator { |
| 14 |
| 15 // 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 // 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 // are hashed by the source only. |
| 20 class TrafficPolicer : public Actor, public ConstrainedPortInterface { |
| 21 public: |
| 22 TrafficPolicer(Simulator* simulator, |
| 23 std::string name, |
| 24 QuicByteCount initial_bucket_size, |
| 25 QuicByteCount max_bucket_size, |
| 26 QuicBandwidth target_bandwidth, |
| 27 Endpoint* input); |
| 28 ~TrafficPolicer() override; |
| 29 |
| 30 Endpoint* input() { return input_; } |
| 31 Endpoint* output() { return &output_; } |
| 32 |
| 33 void AcceptPacket(std::unique_ptr<Packet> packet) override; |
| 34 QuicTime::Delta TimeUntilAvailable() override; |
| 35 |
| 36 void Act() override; |
| 37 |
| 38 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 |
| 53 // |last_refill_time_|. |
| 54 void Refill(); |
| 55 |
| 56 QuicByteCount initial_bucket_size_; |
| 57 QuicByteCount max_bucket_size_; |
| 58 QuicBandwidth target_bandwidth_; |
| 59 |
| 60 // The time at which the token buckets were last refilled. |
| 61 QuicTime last_refill_time_; |
| 62 |
| 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. |
| 69 std::unordered_map<std::string, QuicByteCount> token_buckets_; |
| 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(TrafficPolicer); |
| 72 }; |
| 73 |
| 74 } // namespace simulator |
| 75 } // namespace net |
| 76 |
| 77 #endif // NET_QUIC_TEST_TOOLS_SIMULATOR_TRAFFIC_POLICER_H_ |
OLD | NEW |