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"
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"
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
{
21 explicit WriteUnblockedAlarm(PacketDroppingTestWriter
* writer
)
24 virtual QuicTime
OnAlarm() override
{
25 DVLOG(1) << "Unblocking socket.";
26 writer_
->OnCanWrite();
27 return QuicTime::Zero();
31 PacketDroppingTestWriter
* writer_
;
34 // An alarm that is scheduled every time a new packet is to be written at a
36 class DelayAlarm
: public QuicAlarm::Delegate
{
38 explicit DelayAlarm(PacketDroppingTestWriter
* writer
) : writer_(writer
) {}
40 virtual QuicTime
OnAlarm() override
{
41 return writer_
->ReleaseOldPackets();
45 PacketDroppingTestWriter
* writer_
;
48 PacketDroppingTestWriter::PacketDroppingTestWriter()
52 fake_packet_loss_percentage_(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()),
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 QuicEpollConnectionHelper
* helper
,
67 Delegate
* on_can_write
) {
68 clock_
= helper
->GetClock();
69 write_unblocked_alarm_
.reset(
70 helper
->CreateAlarm(new WriteUnblockedAlarm(this)));
72 helper
->CreateAlarm(new DelayAlarm(this)));
73 on_can_write_
.reset(on_can_write
);
76 WriteResult
PacketDroppingTestWriter::WritePacket(
79 const net::IPAddressNumber
& self_address
,
80 const net::IPEndPoint
& peer_address
) {
83 base::AutoLock
locked(config_mutex_
);
84 if (fake_packet_loss_percentage_
> 0 &&
85 simple_random_
.RandUint64() % 100 <
86 static_cast<uint64
>(fake_packet_loss_percentage_
)) {
87 DVLOG(1) << "Dropping packet.";
88 return WriteResult(WRITE_STATUS_OK
, buf_len
);
90 if (fake_blocked_socket_percentage_
> 0 &&
91 simple_random_
.RandUint64() % 100 <
92 static_cast<uint64
>(fake_blocked_socket_percentage_
)) {
93 CHECK(on_can_write_
.get() != nullptr);
94 DVLOG(1) << "Blocking socket.";
95 if (!write_unblocked_alarm_
->IsSet()) {
96 // Set the alarm to fire immediately.
97 write_unblocked_alarm_
->Set(clock_
->ApproximateNow());
99 return WriteResult(WRITE_STATUS_BLOCKED
, EAGAIN
);
102 if (!fake_packet_delay_
.IsZero() || !fake_bandwidth_
.IsZero()) {
103 if (buffer_size_
> 0 && buf_len
+ cur_buffer_size_
> buffer_size_
) {
104 // Drop packets which do not fit into the buffer.
105 DVLOG(1) << "Dropping packet because the buffer is full.";
106 return WriteResult(WRITE_STATUS_OK
, buf_len
);
109 // Queue it to be sent.
110 QuicTime send_time
= clock_
->ApproximateNow().Add(fake_packet_delay_
);
111 if (!fake_bandwidth_
.IsZero()) {
112 // Calculate a time the bandwidth limit would impose.
113 QuicTime::Delta bandwidth_delay
= QuicTime::Delta::FromMicroseconds(
114 (buf_len
* kNumMicrosPerSecond
) /
115 fake_bandwidth_
.ToBytesPerSecond());
116 send_time
= delayed_packets_
.empty() ?
117 send_time
.Add(bandwidth_delay
) :
118 delayed_packets_
.back().send_time
.Add(bandwidth_delay
);
120 delayed_packets_
.push_back(DelayedWrite(buffer
, buf_len
, self_address
,
121 peer_address
, send_time
));
122 cur_buffer_size_
+= buf_len
;
124 // Set the alarm if it's not yet set.
125 if (!delay_alarm_
->IsSet()) {
126 delay_alarm_
->Set(send_time
);
129 return WriteResult(WRITE_STATUS_OK
, buf_len
);
132 return QuicPacketWriterWrapper::WritePacket(
133 buffer
, buf_len
, self_address
, peer_address
);
136 bool PacketDroppingTestWriter::IsWriteBlocked() const {
137 if (write_unblocked_alarm_
.get() != nullptr &&
138 write_unblocked_alarm_
->IsSet()) {
141 return QuicPacketWriterWrapper::IsWriteBlocked();
144 void PacketDroppingTestWriter::SetWritable() {
145 if (write_unblocked_alarm_
.get() != nullptr &&
146 write_unblocked_alarm_
->IsSet()) {
147 write_unblocked_alarm_
->Cancel();
149 QuicPacketWriterWrapper::SetWritable();
152 QuicTime
PacketDroppingTestWriter::ReleaseNextPacket() {
153 if (delayed_packets_
.empty()) {
154 return QuicTime::Zero();
156 base::AutoLock
locked(config_mutex_
);
157 DelayedPacketList::iterator iter
= delayed_packets_
.begin();
158 // Determine if we should re-order.
159 if (delayed_packets_
.size() > 1 && fake_packet_reorder_percentage_
> 0 &&
160 simple_random_
.RandUint64() % 100 <
161 static_cast<uint64
>(fake_packet_reorder_percentage_
)) {
162 DVLOG(1) << "Reordering packets.";
164 // Swap the send times when re-ordering packets.
165 delayed_packets_
.begin()->send_time
= iter
->send_time
;
168 DVLOG(1) << "Releasing packet. " << (delayed_packets_
.size() - 1)
170 // Grab the next one off the queue and send it.
171 QuicPacketWriterWrapper::WritePacket(
172 iter
->buffer
.data(), iter
->buffer
.length(),
173 iter
->self_address
, iter
->peer_address
);
174 DCHECK_GE(cur_buffer_size_
, iter
->buffer
.length());
175 cur_buffer_size_
-= iter
->buffer
.length();
176 delayed_packets_
.erase(iter
);
178 // If there are others, find the time for the next to be sent.
179 if (delayed_packets_
.empty()) {
180 return QuicTime::Zero();
182 return delayed_packets_
.begin()->send_time
;
185 QuicTime
PacketDroppingTestWriter::ReleaseOldPackets() {
186 while (!delayed_packets_
.empty()) {
187 QuicTime next_send_time
= delayed_packets_
.front().send_time
;
188 if (next_send_time
> clock_
->Now()) {
189 return next_send_time
;
193 return QuicTime::Zero();
196 void PacketDroppingTestWriter::OnCanWrite() {
197 on_can_write_
->OnCanWrite();
200 PacketDroppingTestWriter::DelayedWrite::DelayedWrite(
203 const net::IPAddressNumber
& self_address
,
204 const net::IPEndPoint
& peer_address
,
206 : buffer(buffer
, buf_len
),
207 self_address(self_address
),
208 peer_address(peer_address
),
209 send_time(send_time
) {}
211 PacketDroppingTestWriter::DelayedWrite::~DelayedWrite() {}