QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 2013 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 QUICHE_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_ |
| 6 | #define QUICHE_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_ |
| 7 | |
| 8 | #include <cstdint> |
| 9 | #include <list> |
| 10 | #include <memory> |
| 11 | |
vasilvv | 8f9591b | 2020-10-26 17:01:14 -0700 | [diff] [blame] | 12 | #include "absl/base/attributes.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 13 | #include "quic/core/quic_alarm.h" |
| 14 | #include "quic/core/quic_clock.h" |
| 15 | #include "quic/core/quic_packet_writer_wrapper.h" |
| 16 | #include "quic/test_tools/quic_test_client.h" |
| 17 | #include "quic/test_tools/quic_test_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | |
| 19 | namespace quic { |
| 20 | namespace test { |
| 21 | |
| 22 | // Simulates a connection that drops packets a configured percentage of the time |
| 23 | // and has a blocked socket a configured percentage of the time. Also provides |
| 24 | // the options to delay packets and reorder packets if delay is enabled. |
| 25 | class PacketDroppingTestWriter : public QuicPacketWriterWrapper { |
| 26 | public: |
| 27 | class Delegate { |
| 28 | public: |
| 29 | virtual ~Delegate() {} |
| 30 | virtual void OnCanWrite() = 0; |
| 31 | }; |
| 32 | |
| 33 | PacketDroppingTestWriter(); |
| 34 | PacketDroppingTestWriter(const PacketDroppingTestWriter&) = delete; |
| 35 | PacketDroppingTestWriter& operator=(const PacketDroppingTestWriter&) = delete; |
| 36 | |
| 37 | ~PacketDroppingTestWriter() override; |
| 38 | |
| 39 | // Must be called before blocking, reordering or delaying (loss is OK). May be |
| 40 | // called after connecting if the helper is not available before. |
| 41 | // |on_can_write| will be triggered when fake-unblocking. |
| 42 | void Initialize(QuicConnectionHelperInterface* helper, |
| 43 | QuicAlarmFactory* alarm_factory, |
| 44 | std::unique_ptr<Delegate> on_can_write); |
| 45 | |
| 46 | // QuicPacketWriter methods: |
| 47 | WriteResult WritePacket(const char* buffer, |
| 48 | size_t buf_len, |
| 49 | const QuicIpAddress& self_address, |
| 50 | const QuicSocketAddress& peer_address, |
| 51 | PerPacketOptions* options) override; |
| 52 | |
| 53 | bool IsWriteBlocked() const override; |
| 54 | |
| 55 | void SetWritable() override; |
| 56 | |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 57 | QuicPacketBuffer GetNextWriteLocation( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 58 | const QuicIpAddress& /*self_address*/, |
| 59 | const QuicSocketAddress& /*peer_address*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 60 | // If the wrapped writer supports zero-copy, disable it, because it is not |
| 61 | // compatible with delayed writes in this class. |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 62 | return {nullptr, nullptr}; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Writes out any packet which should have been sent by now |
| 66 | // to the contained writer and returns the time |
| 67 | // for the next delayed packet to be written. |
| 68 | QuicTime ReleaseOldPackets(); |
| 69 | |
| 70 | // Sets |delay_alarm_| to fire at |new_deadline|. |
| 71 | void SetDelayAlarm(QuicTime new_deadline); |
| 72 | |
| 73 | void OnCanWrite(); |
| 74 | |
| 75 | // The percent of time a packet is simulated as being lost. |
bnc | ed1fe4f | 2020-07-01 04:48:53 -0700 | [diff] [blame] | 76 | // If |fake_packet_loss_percentage| is 100, then all packages are lost. |
| 77 | // Otherwise actual percentage will be lower than |
| 78 | // |fake_packet_loss_percentage|, because every dropped package is followed by |
| 79 | // a minimum number of successfully written packets. |
| 80 | void set_fake_packet_loss_percentage(int32_t fake_packet_loss_percentage) { |
| 81 | QuicWriterMutexLock lock(&config_mutex_); |
| 82 | fake_packet_loss_percentage_ = fake_packet_loss_percentage; |
| 83 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 84 | |
| 85 | // Simulate dropping the first n packets unconditionally. |
| 86 | // Subsequent packets will be lost at fake_packet_loss_percentage_ if set. |
| 87 | void set_fake_drop_first_n_packets(int32_t fake_drop_first_n_packets) { |
| 88 | QuicWriterMutexLock lock(&config_mutex_); |
| 89 | fake_drop_first_n_packets_ = fake_drop_first_n_packets; |
| 90 | } |
| 91 | |
| 92 | // The percent of time WritePacket will block and set WriteResult's status |
| 93 | // to WRITE_STATUS_BLOCKED. |
| 94 | void set_fake_blocked_socket_percentage( |
| 95 | int32_t fake_blocked_socket_percentage) { |
vasilvv | 5cef78e | 2021-01-30 11:11:14 -0800 | [diff] [blame] | 96 | QUICHE_DCHECK(clock_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 97 | QuicWriterMutexLock lock(&config_mutex_); |
| 98 | fake_blocked_socket_percentage_ = fake_blocked_socket_percentage; |
| 99 | } |
| 100 | |
| 101 | // The percent of time a packet is simulated as being reordered. |
| 102 | void set_fake_reorder_percentage(int32_t fake_packet_reorder_percentage) { |
vasilvv | 5cef78e | 2021-01-30 11:11:14 -0800 | [diff] [blame] | 103 | QUICHE_DCHECK(clock_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 104 | QuicWriterMutexLock lock(&config_mutex_); |
vasilvv | 5cef78e | 2021-01-30 11:11:14 -0800 | [diff] [blame] | 105 | QUICHE_DCHECK(!fake_packet_delay_.IsZero()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 106 | fake_packet_reorder_percentage_ = fake_packet_reorder_percentage; |
| 107 | } |
| 108 | |
| 109 | // The delay before writing this packet. |
| 110 | void set_fake_packet_delay(QuicTime::Delta fake_packet_delay) { |
vasilvv | 5cef78e | 2021-01-30 11:11:14 -0800 | [diff] [blame] | 111 | QUICHE_DCHECK(clock_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 112 | QuicWriterMutexLock lock(&config_mutex_); |
| 113 | fake_packet_delay_ = fake_packet_delay; |
| 114 | } |
| 115 | |
| 116 | // The maximum bandwidth and buffer size of the connection. When these are |
| 117 | // set, packets will be delayed until a connection with that bandwidth would |
| 118 | // transmit it. Once the |buffer_size| is reached, all new packets are |
| 119 | // dropped. |
| 120 | void set_max_bandwidth_and_buffer_size(QuicBandwidth fake_bandwidth, |
| 121 | QuicByteCount buffer_size) { |
vasilvv | 5cef78e | 2021-01-30 11:11:14 -0800 | [diff] [blame] | 122 | QUICHE_DCHECK(clock_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 123 | QuicWriterMutexLock lock(&config_mutex_); |
| 124 | fake_bandwidth_ = fake_bandwidth; |
| 125 | buffer_size_ = buffer_size; |
| 126 | } |
| 127 | |
| 128 | // Useful for reproducing very flaky issues. |
vasilvv | 8f9591b | 2020-10-26 17:01:14 -0700 | [diff] [blame] | 129 | ABSL_ATTRIBUTE_UNUSED void set_seed(uint64_t seed) { |
| 130 | simple_random_.set_seed(seed); |
| 131 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 132 | |
| 133 | private: |
| 134 | // Writes out the next packet to the contained writer and returns the time |
| 135 | // for the next delayed packet to be written. |
| 136 | QuicTime ReleaseNextPacket(); |
| 137 | |
| 138 | // A single packet which will be sent at the supplied send_time. |
| 139 | struct DelayedWrite { |
| 140 | public: |
| 141 | DelayedWrite(const char* buffer, |
| 142 | size_t buf_len, |
| 143 | const QuicIpAddress& self_address, |
| 144 | const QuicSocketAddress& peer_address, |
| 145 | std::unique_ptr<PerPacketOptions> options, |
| 146 | QuicTime send_time); |
| 147 | DelayedWrite(const DelayedWrite&) = delete; |
| 148 | DelayedWrite(DelayedWrite&&) = default; |
| 149 | DelayedWrite& operator=(const DelayedWrite&) = delete; |
| 150 | DelayedWrite& operator=(DelayedWrite&&) = default; |
| 151 | ~DelayedWrite(); |
| 152 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 153 | std::string buffer; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 154 | QuicIpAddress self_address; |
| 155 | QuicSocketAddress peer_address; |
| 156 | std::unique_ptr<PerPacketOptions> options; |
| 157 | QuicTime send_time; |
| 158 | }; |
| 159 | |
renjietang | 58b3af3 | 2020-11-11 15:48:58 -0800 | [diff] [blame] | 160 | using DelayedPacketList = std::list<DelayedWrite>; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 161 | |
| 162 | const QuicClock* clock_; |
| 163 | std::unique_ptr<QuicAlarm> write_unblocked_alarm_; |
| 164 | std::unique_ptr<QuicAlarm> delay_alarm_; |
| 165 | std::unique_ptr<Delegate> on_can_write_; |
| 166 | SimpleRandom simple_random_; |
| 167 | // Stored packets delayed by fake packet delay or bandwidth restrictions. |
| 168 | DelayedPacketList delayed_packets_; |
| 169 | QuicByteCount cur_buffer_size_; |
| 170 | uint64_t num_calls_to_write_; |
bnc | ed1fe4f | 2020-07-01 04:48:53 -0700 | [diff] [blame] | 171 | int32_t num_consecutive_succesful_writes_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 172 | |
| 173 | QuicMutex config_mutex_; |
rch | 52cb79f | 2019-08-30 13:35:57 -0700 | [diff] [blame] | 174 | int32_t fake_packet_loss_percentage_ QUIC_GUARDED_BY(config_mutex_); |
| 175 | int32_t fake_drop_first_n_packets_ QUIC_GUARDED_BY(config_mutex_); |
| 176 | int32_t fake_blocked_socket_percentage_ QUIC_GUARDED_BY(config_mutex_); |
| 177 | int32_t fake_packet_reorder_percentage_ QUIC_GUARDED_BY(config_mutex_); |
| 178 | QuicTime::Delta fake_packet_delay_ QUIC_GUARDED_BY(config_mutex_); |
| 179 | QuicBandwidth fake_bandwidth_ QUIC_GUARDED_BY(config_mutex_); |
| 180 | QuicByteCount buffer_size_ QUIC_GUARDED_BY(config_mutex_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | } // namespace test |
| 184 | } // namespace quic |
| 185 | |
| 186 | #endif // QUICHE_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_ |