Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / tools / quic / test_tools / packet_dropping_test_writer.cc
blob79573803cbeb3441c064449bc7cb490fa6077a42
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.
5 #include "net/tools/quic/test_tools/packet_dropping_test_writer.h"
7 #include <limits>
9 #include "base/rand_util.h"
10 #include "net/tools/quic/quic_epoll_connection_helper.h"
11 #include "net/tools/quic/quic_socket_utils.h"
13 namespace net {
14 namespace tools {
15 namespace test {
17 // An alarm that is scheduled if a blocked socket is simulated to indicate
18 // it's writable again.
19 class WriteUnblockedAlarm : public QuicAlarm::Delegate {
20 public:
21 explicit WriteUnblockedAlarm(PacketDroppingTestWriter* writer)
22 : writer_(writer) {}
24 QuicTime OnAlarm() override {
25 DVLOG(1) << "Unblocking socket.";
26 writer_->OnCanWrite();
27 return QuicTime::Zero();
30 private:
31 PacketDroppingTestWriter* writer_;
34 // An alarm that is scheduled every time a new packet is to be written at a
35 // later point.
36 class DelayAlarm : public QuicAlarm::Delegate {
37 public:
38 explicit DelayAlarm(PacketDroppingTestWriter* writer) : writer_(writer) {}
40 QuicTime OnAlarm() override { return writer_->ReleaseOldPackets(); }
42 private:
43 PacketDroppingTestWriter* writer_;
46 PacketDroppingTestWriter::PacketDroppingTestWriter()
47 : clock_(nullptr),
48 cur_buffer_size_(0),
49 num_calls_to_write_(0),
50 config_mutex_(),
51 fake_packet_loss_percentage_(0),
52 fake_drop_first_n_packets_(0),
53 fake_blocked_socket_percentage_(0),
54 fake_packet_reorder_percentage_(0),
55 fake_packet_delay_(QuicTime::Delta::Zero()),
56 fake_bandwidth_(QuicBandwidth::Zero()),
57 buffer_size_(0) {
58 uint32 seed = base::RandInt(0, std::numeric_limits<int32>::max());
59 VLOG(1) << "Seeding packet loss with " << seed;
60 simple_random_.set_seed(seed);
63 PacketDroppingTestWriter::~PacketDroppingTestWriter() {}
65 void PacketDroppingTestWriter::Initialize(
66 QuicConnectionHelperInterface* helper,
67 Delegate* on_can_write) {
68 clock_ = helper->GetClock();
69 write_unblocked_alarm_.reset(
70 helper->CreateAlarm(new WriteUnblockedAlarm(this)));
71 delay_alarm_.reset(
72 helper->CreateAlarm(new DelayAlarm(this)));
73 on_can_write_.reset(on_can_write);
76 WriteResult PacketDroppingTestWriter::WritePacket(
77 const char* buffer,
78 size_t buf_len,
79 const net::IPAddressNumber& self_address,
80 const net::IPEndPoint& peer_address) {
81 ++num_calls_to_write_;
82 ReleaseOldPackets();
84 base::AutoLock locked(config_mutex_);
85 if (fake_drop_first_n_packets_ > 0 &&
86 num_calls_to_write_ <= static_cast<uint64>(fake_drop_first_n_packets_)) {
87 DVLOG(1) << "Dropping first " << fake_drop_first_n_packets_
88 << " packets (packet number " << num_calls_to_write_ << ")";
89 return WriteResult(WRITE_STATUS_OK, buf_len);
91 if (fake_packet_loss_percentage_ > 0 &&
92 simple_random_.RandUint64() % 100 <
93 static_cast<uint64>(fake_packet_loss_percentage_)) {
94 DVLOG(1) << "Dropping packet.";
95 return WriteResult(WRITE_STATUS_OK, buf_len);
97 if (fake_blocked_socket_percentage_ > 0 &&
98 simple_random_.RandUint64() % 100 <
99 static_cast<uint64>(fake_blocked_socket_percentage_)) {
100 CHECK(on_can_write_.get() != nullptr);
101 DVLOG(1) << "Blocking socket.";
102 if (!write_unblocked_alarm_->IsSet()) {
103 // Set the alarm to fire immediately.
104 write_unblocked_alarm_->Set(clock_->ApproximateNow());
106 return WriteResult(WRITE_STATUS_BLOCKED, EAGAIN);
109 if (!fake_packet_delay_.IsZero() || !fake_bandwidth_.IsZero()) {
110 if (buffer_size_ > 0 && buf_len + cur_buffer_size_ > buffer_size_) {
111 // Drop packets which do not fit into the buffer.
112 DVLOG(1) << "Dropping packet because the buffer is full.";
113 return WriteResult(WRITE_STATUS_OK, buf_len);
116 // Queue it to be sent.
117 QuicTime send_time = clock_->ApproximateNow().Add(fake_packet_delay_);
118 if (!fake_bandwidth_.IsZero()) {
119 // Calculate a time the bandwidth limit would impose.
120 QuicTime::Delta bandwidth_delay = QuicTime::Delta::FromMicroseconds(
121 (buf_len * kNumMicrosPerSecond) /
122 fake_bandwidth_.ToBytesPerSecond());
123 send_time = delayed_packets_.empty() ?
124 send_time.Add(bandwidth_delay) :
125 delayed_packets_.back().send_time.Add(bandwidth_delay);
127 delayed_packets_.push_back(DelayedWrite(buffer, buf_len, self_address,
128 peer_address, send_time));
129 cur_buffer_size_ += buf_len;
131 // Set the alarm if it's not yet set.
132 if (!delay_alarm_->IsSet()) {
133 delay_alarm_->Set(send_time);
136 return WriteResult(WRITE_STATUS_OK, buf_len);
139 return QuicPacketWriterWrapper::WritePacket(
140 buffer, buf_len, self_address, peer_address);
143 bool PacketDroppingTestWriter::IsWriteBlocked() const {
144 if (write_unblocked_alarm_.get() != nullptr &&
145 write_unblocked_alarm_->IsSet()) {
146 return true;
148 return QuicPacketWriterWrapper::IsWriteBlocked();
151 void PacketDroppingTestWriter::SetWritable() {
152 if (write_unblocked_alarm_.get() != nullptr &&
153 write_unblocked_alarm_->IsSet()) {
154 write_unblocked_alarm_->Cancel();
156 QuicPacketWriterWrapper::SetWritable();
159 QuicTime PacketDroppingTestWriter::ReleaseNextPacket() {
160 if (delayed_packets_.empty()) {
161 return QuicTime::Zero();
163 base::AutoLock locked(config_mutex_);
164 DelayedPacketList::iterator iter = delayed_packets_.begin();
165 // Determine if we should re-order.
166 if (delayed_packets_.size() > 1 && fake_packet_reorder_percentage_ > 0 &&
167 simple_random_.RandUint64() % 100 <
168 static_cast<uint64>(fake_packet_reorder_percentage_)) {
169 DVLOG(1) << "Reordering packets.";
170 ++iter;
171 // Swap the send times when re-ordering packets.
172 delayed_packets_.begin()->send_time = iter->send_time;
175 DVLOG(1) << "Releasing packet. " << (delayed_packets_.size() - 1)
176 << " remaining.";
177 // Grab the next one off the queue and send it.
178 QuicPacketWriterWrapper::WritePacket(
179 iter->buffer.data(), iter->buffer.length(),
180 iter->self_address, iter->peer_address);
181 DCHECK_GE(cur_buffer_size_, iter->buffer.length());
182 cur_buffer_size_ -= iter->buffer.length();
183 delayed_packets_.erase(iter);
185 // If there are others, find the time for the next to be sent.
186 if (delayed_packets_.empty()) {
187 return QuicTime::Zero();
189 return delayed_packets_.begin()->send_time;
192 QuicTime PacketDroppingTestWriter::ReleaseOldPackets() {
193 while (!delayed_packets_.empty()) {
194 QuicTime next_send_time = delayed_packets_.front().send_time;
195 if (next_send_time > clock_->Now()) {
196 return next_send_time;
198 ReleaseNextPacket();
200 return QuicTime::Zero();
203 void PacketDroppingTestWriter::OnCanWrite() {
204 on_can_write_->OnCanWrite();
207 PacketDroppingTestWriter::DelayedWrite::DelayedWrite(
208 const char* buffer,
209 size_t buf_len,
210 const net::IPAddressNumber& self_address,
211 const net::IPEndPoint& peer_address,
212 QuicTime send_time)
213 : buffer(buffer, buf_len),
214 self_address(self_address),
215 peer_address(peer_address),
216 send_time(send_time) {}
218 PacketDroppingTestWriter::DelayedWrite::~DelayedWrite() {}
220 } // namespace test
221 } // namespace tools
222 } // namespace net