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 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/quic_alarm.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/quic_packet_writer_wrapper.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_clock.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_macros.h" |
| 16 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_client.h" |
| 17 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 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 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame^] | 57 | char* GetNextWriteLocation( |
| 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. |
| 62 | return nullptr; |
| 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. |
| 76 | void set_fake_packet_loss_percentage(int32_t fake_packet_loss_percentage); |
| 77 | |
| 78 | // Simulate dropping the first n packets unconditionally. |
| 79 | // Subsequent packets will be lost at fake_packet_loss_percentage_ if set. |
| 80 | void set_fake_drop_first_n_packets(int32_t fake_drop_first_n_packets) { |
| 81 | QuicWriterMutexLock lock(&config_mutex_); |
| 82 | fake_drop_first_n_packets_ = fake_drop_first_n_packets; |
| 83 | } |
| 84 | |
| 85 | // The percent of time WritePacket will block and set WriteResult's status |
| 86 | // to WRITE_STATUS_BLOCKED. |
| 87 | void set_fake_blocked_socket_percentage( |
| 88 | int32_t fake_blocked_socket_percentage) { |
| 89 | DCHECK(clock_); |
| 90 | QuicWriterMutexLock lock(&config_mutex_); |
| 91 | fake_blocked_socket_percentage_ = fake_blocked_socket_percentage; |
| 92 | } |
| 93 | |
| 94 | // The percent of time a packet is simulated as being reordered. |
| 95 | void set_fake_reorder_percentage(int32_t fake_packet_reorder_percentage) { |
| 96 | DCHECK(clock_); |
| 97 | QuicWriterMutexLock lock(&config_mutex_); |
| 98 | DCHECK(!fake_packet_delay_.IsZero()); |
| 99 | fake_packet_reorder_percentage_ = fake_packet_reorder_percentage; |
| 100 | } |
| 101 | |
| 102 | // The delay before writing this packet. |
| 103 | void set_fake_packet_delay(QuicTime::Delta fake_packet_delay) { |
| 104 | DCHECK(clock_); |
| 105 | QuicWriterMutexLock lock(&config_mutex_); |
| 106 | fake_packet_delay_ = fake_packet_delay; |
| 107 | } |
| 108 | |
| 109 | // The maximum bandwidth and buffer size of the connection. When these are |
| 110 | // set, packets will be delayed until a connection with that bandwidth would |
| 111 | // transmit it. Once the |buffer_size| is reached, all new packets are |
| 112 | // dropped. |
| 113 | void set_max_bandwidth_and_buffer_size(QuicBandwidth fake_bandwidth, |
| 114 | QuicByteCount buffer_size) { |
| 115 | DCHECK(clock_); |
| 116 | QuicWriterMutexLock lock(&config_mutex_); |
| 117 | fake_bandwidth_ = fake_bandwidth; |
| 118 | buffer_size_ = buffer_size; |
| 119 | } |
| 120 | |
| 121 | // Useful for reproducing very flaky issues. |
| 122 | QUIC_UNUSED void set_seed(uint64_t seed) { simple_random_.set_seed(seed); } |
| 123 | |
| 124 | private: |
| 125 | // Writes out the next packet to the contained writer and returns the time |
| 126 | // for the next delayed packet to be written. |
| 127 | QuicTime ReleaseNextPacket(); |
| 128 | |
| 129 | // A single packet which will be sent at the supplied send_time. |
| 130 | struct DelayedWrite { |
| 131 | public: |
| 132 | DelayedWrite(const char* buffer, |
| 133 | size_t buf_len, |
| 134 | const QuicIpAddress& self_address, |
| 135 | const QuicSocketAddress& peer_address, |
| 136 | std::unique_ptr<PerPacketOptions> options, |
| 137 | QuicTime send_time); |
| 138 | DelayedWrite(const DelayedWrite&) = delete; |
| 139 | DelayedWrite(DelayedWrite&&) = default; |
| 140 | DelayedWrite& operator=(const DelayedWrite&) = delete; |
| 141 | DelayedWrite& operator=(DelayedWrite&&) = default; |
| 142 | ~DelayedWrite(); |
| 143 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 144 | std::string buffer; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 145 | QuicIpAddress self_address; |
| 146 | QuicSocketAddress peer_address; |
| 147 | std::unique_ptr<PerPacketOptions> options; |
| 148 | QuicTime send_time; |
| 149 | }; |
| 150 | |
| 151 | typedef std::list<DelayedWrite> DelayedPacketList; |
| 152 | |
| 153 | const QuicClock* clock_; |
| 154 | std::unique_ptr<QuicAlarm> write_unblocked_alarm_; |
| 155 | std::unique_ptr<QuicAlarm> delay_alarm_; |
| 156 | std::unique_ptr<Delegate> on_can_write_; |
| 157 | SimpleRandom simple_random_; |
| 158 | // Stored packets delayed by fake packet delay or bandwidth restrictions. |
| 159 | DelayedPacketList delayed_packets_; |
| 160 | QuicByteCount cur_buffer_size_; |
| 161 | uint64_t num_calls_to_write_; |
| 162 | |
| 163 | QuicMutex config_mutex_; |
| 164 | int32_t fake_packet_loss_percentage_ GUARDED_BY(config_mutex_); |
| 165 | int32_t fake_drop_first_n_packets_ GUARDED_BY(config_mutex_); |
| 166 | int32_t fake_blocked_socket_percentage_ GUARDED_BY(config_mutex_); |
| 167 | int32_t fake_packet_reorder_percentage_ GUARDED_BY(config_mutex_); |
| 168 | QuicTime::Delta fake_packet_delay_ GUARDED_BY(config_mutex_); |
| 169 | QuicBandwidth fake_bandwidth_ GUARDED_BY(config_mutex_); |
| 170 | QuicByteCount buffer_size_ GUARDED_BY(config_mutex_); |
| 171 | int32_t num_consecutive_packet_lost_ GUARDED_BY(config_mutex_); |
| 172 | }; |
| 173 | |
| 174 | } // namespace test |
| 175 | } // namespace quic |
| 176 | |
| 177 | #endif // QUICHE_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_ |