1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/quic/congestion_control/tcp_loss_algorithm.h"
#include <algorithm>
#include "base/logging.h"
#include "base/stl_util.h"
#include "net/quic/congestion_control/rtt_stats.h"
#include "net/quic/quic_ack_notifier_manager.h"
#include "net/quic/quic_unacked_packet_map.h"
#include "net/quic/test_tools/mock_clock.h"
#include "testing/gtest/include/gtest/gtest.h"
using std::vector;
namespace net {
namespace test {
namespace {
// Default packet length.
const uint32 kDefaultLength = 1000;
class TcpLossAlgorithmTest : public ::testing::Test {
protected:
TcpLossAlgorithmTest() : unacked_packets_(&ack_notifier_manager_) {
rtt_stats_.UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
QuicTime::Delta::Zero(),
clock_.Now());
}
~TcpLossAlgorithmTest() override {
STLDeleteElements(&packets_);
}
void SendDataPacket(QuicPacketSequenceNumber sequence_number) {
packets_.push_back(new QuicEncryptedPacket(nullptr, kDefaultLength));
SerializedPacket packet(
sequence_number, PACKET_1BYTE_SEQUENCE_NUMBER, packets_.back(), 0,
new RetransmittableFrames(ENCRYPTION_NONE), false, false);
unacked_packets_.AddSentPacket(packet, 0, NOT_RETRANSMISSION, clock_.Now(),
1000, true);
}
void VerifyLosses(QuicPacketSequenceNumber largest_observed,
QuicPacketSequenceNumber* losses_expected,
size_t num_losses) {
SequenceNumberSet lost_packets =
loss_algorithm_.DetectLostPackets(
unacked_packets_, clock_.Now(), largest_observed, rtt_stats_);
EXPECT_EQ(num_losses, lost_packets.size());
for (size_t i = 0; i < num_losses; ++i) {
EXPECT_TRUE(ContainsKey(lost_packets, losses_expected[i]));
}
}
vector<QuicEncryptedPacket*> packets_;
AckNotifierManager ack_notifier_manager_;
QuicUnackedPacketMap unacked_packets_;
TCPLossAlgorithm loss_algorithm_;
RttStats rtt_stats_;
MockClock clock_;
};
TEST_F(TcpLossAlgorithmTest, NackRetransmit1Packet) {
const size_t kNumSentPackets = 5;
// Transmit 5 packets.
for (size_t i = 1; i <= kNumSentPackets; ++i) {
SendDataPacket(i);
}
// No loss on one ack.
unacked_packets_.RemoveFromInFlight(2);
unacked_packets_.NackPacket(1, 1);
VerifyLosses(2, nullptr, 0);
// No loss on two acks.
unacked_packets_.RemoveFromInFlight(3);
unacked_packets_.NackPacket(1, 2);
VerifyLosses(3, nullptr, 0);
// Loss on three acks.
unacked_packets_.RemoveFromInFlight(4);
unacked_packets_.NackPacket(1, 3);
QuicPacketSequenceNumber lost[] = {1};
VerifyLosses(4, lost, arraysize(lost));
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
}
// A stretch ack is an ack that covers more than 1 packet of previously
// unacknowledged data.
TEST_F(TcpLossAlgorithmTest, NackRetransmit1PacketWith1StretchAck) {
const size_t kNumSentPackets = 10;
// Transmit 10 packets.
for (size_t i = 1; i <= kNumSentPackets; ++i) {
SendDataPacket(i);
}
// Nack the first packet 3 times in a single StretchAck.
unacked_packets_.NackPacket(1, 3);
unacked_packets_.RemoveFromInFlight(2);
unacked_packets_.RemoveFromInFlight(3);
unacked_packets_.RemoveFromInFlight(4);
QuicPacketSequenceNumber lost[] = { 1 };
VerifyLosses(4, lost, arraysize(lost));
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
}
// Ack a packet 3 packets ahead, causing a retransmit.
TEST_F(TcpLossAlgorithmTest, NackRetransmit1PacketSingleAck) {
const size_t kNumSentPackets = 10;
// Transmit 10 packets.
for (size_t i = 1; i <= kNumSentPackets; ++i) {
SendDataPacket(i);
}
// Nack the first packet 3 times in an AckFrame with three missing packets.
unacked_packets_.NackPacket(1, 3);
unacked_packets_.NackPacket(2, 2);
unacked_packets_.NackPacket(3, 1);
unacked_packets_.RemoveFromInFlight(4);
QuicPacketSequenceNumber lost[] = { 1 };
VerifyLosses(4, lost, arraysize(lost));
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
}
TEST_F(TcpLossAlgorithmTest, EarlyRetransmit1Packet) {
const size_t kNumSentPackets = 2;
// Transmit 2 packets.
for (size_t i = 1; i <= kNumSentPackets; ++i) {
SendDataPacket(i);
}
// Early retransmit when the final packet gets acked and the first is nacked.
unacked_packets_.RemoveFromInFlight(2);
unacked_packets_.NackPacket(1, 1);
VerifyLosses(2, nullptr, 0);
EXPECT_EQ(clock_.Now().Add(rtt_stats_.smoothed_rtt().Multiply(1.25)),
loss_algorithm_.GetLossTimeout());
clock_.AdvanceTime(rtt_stats_.latest_rtt().Multiply(1.25));
QuicPacketSequenceNumber lost[] = { 1 };
VerifyLosses(2, lost, arraysize(lost));
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
}
TEST_F(TcpLossAlgorithmTest, EarlyRetransmitAllPackets) {
const size_t kNumSentPackets = 5;
for (size_t i = 1; i <= kNumSentPackets; ++i) {
SendDataPacket(i);
// Advance the time 1/4 RTT between 3 and 4.
if (i == 3) {
clock_.AdvanceTime(rtt_stats_.smoothed_rtt().Multiply(0.25));
}
}
// Early retransmit when the final packet gets acked and 1.25 RTTs have
// elapsed since the packets were sent.
unacked_packets_.RemoveFromInFlight(kNumSentPackets);
// This simulates a single ack following multiple missing packets with FACK.
for (size_t i = 1; i < kNumSentPackets; ++i) {
unacked_packets_.NackPacket(i, kNumSentPackets - i);
}
QuicPacketSequenceNumber lost[] = { 1, 2 };
VerifyLosses(kNumSentPackets, lost, arraysize(lost));
// The time has already advanced 1/4 an RTT, so ensure the timeout is set
// 1.25 RTTs after the earliest pending packet(3), not the last(4).
EXPECT_EQ(clock_.Now().Add(rtt_stats_.smoothed_rtt()),
loss_algorithm_.GetLossTimeout());
clock_.AdvanceTime(rtt_stats_.smoothed_rtt());
QuicPacketSequenceNumber lost2[] = { 1, 2, 3 };
VerifyLosses(kNumSentPackets, lost2, arraysize(lost2));
EXPECT_EQ(clock_.Now().Add(rtt_stats_.smoothed_rtt().Multiply(0.25)),
loss_algorithm_.GetLossTimeout());
clock_.AdvanceTime(rtt_stats_.smoothed_rtt().Multiply(0.25));
QuicPacketSequenceNumber lost3[] = { 1, 2, 3, 4 };
VerifyLosses(kNumSentPackets, lost3, arraysize(lost3));
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
}
TEST_F(TcpLossAlgorithmTest, DontEarlyRetransmitNeuteredPacket) {
const size_t kNumSentPackets = 2;
// Transmit 2 packets.
for (size_t i = 1; i <= kNumSentPackets; ++i) {
SendDataPacket(i);
}
// Neuter packet 1.
unacked_packets_.RemoveRetransmittability(1);
// Early retransmit when the final packet gets acked and the first is nacked.
unacked_packets_.IncreaseLargestObserved(2);
unacked_packets_.RemoveFromInFlight(2);
unacked_packets_.NackPacket(1, 1);
VerifyLosses(2, nullptr, 0);
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
}
TEST_F(TcpLossAlgorithmTest, AlwaysLosePacketSent1RTTEarlier) {
// Transmit 1 packet and then wait an rtt plus 1ms.
SendDataPacket(1);
clock_.AdvanceTime(
rtt_stats_.smoothed_rtt().Add(QuicTime::Delta::FromMilliseconds(1)));
// Transmit 2 packets.
SendDataPacket(2);
SendDataPacket(3);
// Wait another RTT and ack 2.
clock_.AdvanceTime(rtt_stats_.smoothed_rtt());
unacked_packets_.IncreaseLargestObserved(2);
unacked_packets_.RemoveFromInFlight(2);
unacked_packets_.NackPacket(1, 1);
QuicPacketSequenceNumber lost[] = {1};
VerifyLosses(2, lost, arraysize(lost));
}
} // namespace
} // namespace test
} // namespace net
|