1 // Copyright (c) 2012 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/quic/quic_packet_generator.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "net/quic/quic_ack_notifier.h"
10 #include "net/quic/quic_fec_group.h"
11 #include "net/quic/quic_flags.h"
12 #include "net/quic/quic_utils.h"
14 using base::StringPiece
;
20 // We want to put some space between a protected packet and the FEC packet to
21 // avoid losing them both within the same loss episode. On the other hand, we
22 // expect to be able to recover from any loss in about an RTT. We resolve this
23 // tradeoff by sending an FEC packet atmost half an RTT, or equivalently, half
24 // the max number of in-flight packets, the first protected packet. Since we
25 // don't want to delay an FEC packet past half an RTT, we set the max FEC group
26 // size to be half the current congestion window.
27 const float kMaxPacketsInFlightMultiplierForFecGroupSize
= 0.5;
28 const float kRttMultiplierForFecTimeout
= 0.5;
30 // Minimum timeout for FEC alarm, set to half the minimum Tail Loss Probe
32 const int64 kMinFecTimeoutMs
= 5u;
36 class QuicAckNotifier
;
38 QuicPacketGenerator::QuicPacketGenerator(QuicConnectionId connection_id
,
40 QuicRandom
* random_generator
,
41 DelegateInterface
* delegate
)
42 : delegate_(delegate
),
43 debug_delegate_(nullptr),
44 packet_creator_(connection_id
, framer
, random_generator
),
46 fec_timeout_(QuicTime::Delta::Zero()),
47 should_fec_protect_(false),
48 // TODO(rtenneti): Add the ability to set a different policy.
49 fec_send_policy_(FEC_ANY_TRIGGER
),
50 should_send_ack_(false),
51 should_send_stop_waiting_(false),
53 stop_waiting_queued_(false) {
56 QuicPacketGenerator::~QuicPacketGenerator() {
57 for (QuicFrame
& frame
: queued_control_frames_
) {
60 delete frame
.padding_frame
;
63 delete frame
.stream_frame
;
66 delete frame
.ack_frame
;
68 case RST_STREAM_FRAME
:
69 delete frame
.rst_stream_frame
;
71 case CONNECTION_CLOSE_FRAME
:
72 delete frame
.connection_close_frame
;
75 delete frame
.goaway_frame
;
77 case WINDOW_UPDATE_FRAME
:
78 delete frame
.window_update_frame
;
81 delete frame
.blocked_frame
;
83 case STOP_WAITING_FRAME
:
84 delete frame
.stop_waiting_frame
;
87 delete frame
.ping_frame
;
90 DCHECK(false) << "Cannot delete type: " << frame
.type
;
95 void QuicPacketGenerator::OnCongestionWindowChange(
96 QuicPacketCount max_packets_in_flight
) {
97 packet_creator_
.set_max_packets_per_fec_group(
98 static_cast<size_t>(kMaxPacketsInFlightMultiplierForFecGroupSize
*
99 max_packets_in_flight
));
102 void QuicPacketGenerator::OnRttChange(QuicTime::Delta rtt
) {
103 fec_timeout_
= rtt
.Multiply(kRttMultiplierForFecTimeout
);
106 void QuicPacketGenerator::SetShouldSendAck(bool also_send_stop_waiting
) {
108 // Ack already queued, nothing to do.
112 if (also_send_stop_waiting
&& stop_waiting_queued_
) {
113 LOG(DFATAL
) << "Should only ever be one pending stop waiting frame.";
117 should_send_ack_
= true;
118 should_send_stop_waiting_
= also_send_stop_waiting
;
119 SendQueuedFrames(/*flush=*/false, /*is_fec_timeout=*/false);
122 void QuicPacketGenerator::AddControlFrame(const QuicFrame
& frame
) {
123 queued_control_frames_
.push_back(frame
);
124 SendQueuedFrames(/*flush=*/false, /*is_fec_timeout=*/false);
127 QuicConsumedData
QuicPacketGenerator::ConsumeData(
129 const IOVector
& data_to_write
,
130 QuicStreamOffset offset
,
132 FecProtection fec_protection
,
133 QuicAckNotifier::DelegateInterface
* delegate
) {
134 bool has_handshake
= id
== kCryptoStreamId
;
135 // To make reasoning about crypto frames easier, we don't combine them with
136 // other retransmittable frames in a single packet.
138 has_handshake
&& packet_creator_
.HasPendingRetransmittableFrames();
139 SendQueuedFrames(flush
, /*is_fec_timeout=*/false);
141 size_t total_bytes_consumed
= 0;
142 bool fin_consumed
= false;
144 if (!packet_creator_
.HasRoomForStreamFrame(id
, offset
)) {
145 SerializeAndSendPacket();
148 if (fec_protection
== MUST_FEC_PROTECT
) {
149 MaybeStartFecProtection();
152 // This notifier will be owned by the AckNotifierManager (or deleted below) if
153 // not attached to a packet.
154 QuicAckNotifier
* notifier
= nullptr;
155 if (delegate
!= nullptr) {
156 notifier
= new QuicAckNotifier(delegate
);
159 IOVector data
= data_to_write
;
160 size_t data_size
= data
.TotalBufferSize();
161 if (!fin
&& (data_size
== 0)) {
162 LOG(DFATAL
) << "Attempt to consume empty data without FIN.";
163 return QuicConsumedData(0, false);
166 int frames_created
= 0;
167 while (delegate_
->ShouldGeneratePacket(
168 HAS_RETRANSMITTABLE_DATA
, has_handshake
? IS_HANDSHAKE
: NOT_HANDSHAKE
)) {
170 scoped_ptr
<char[]> buffer
;
171 size_t bytes_consumed
= packet_creator_
.CreateStreamFrame(
172 id
, &data
, offset
+ total_bytes_consumed
, fin
, &frame
, &buffer
);
175 // We want to track which packet this stream frame ends up in.
176 if (notifier
!= nullptr) {
177 ack_notifiers_
.push_back(notifier
);
180 if (!AddFrame(frame
, buffer
.get())) {
181 LOG(DFATAL
) << "Failed to add stream frame.";
182 // Inability to add a STREAM frame creates an unrecoverable hole in a
183 // the stream, so it's best to close the connection.
184 delegate_
->CloseConnection(QUIC_INTERNAL_ERROR
, false);
186 return QuicConsumedData(0, false);
188 // When AddFrame succeeds, it takes ownership of the buffer.
189 ignore_result(buffer
.release());
191 total_bytes_consumed
+= bytes_consumed
;
192 fin_consumed
= fin
&& total_bytes_consumed
== data_size
;
193 DCHECK(data
.Empty() || packet_creator_
.BytesFree() == 0u);
195 // TODO(ianswett): Restore packet reordering.
196 if (!InBatchMode() || !packet_creator_
.HasRoomForStreamFrame(id
, offset
)) {
197 // TODO(rtenneti): remove MaybeSendFecPacketAndCloseGroup() from inside
198 // SerializeAndSendPacket() and make it an explicit call here (and
199 // elsewhere where we call SerializeAndSendPacket?).
200 SerializeAndSendPacket();
204 // We're done writing the data. Exit the loop.
205 // We don't make this a precondition because we could have 0 bytes of data
206 // if we're simply writing a fin.
207 if (fec_protection
== MUST_FEC_PROTECT
) {
208 // Turn off FEC protection when we're done writing protected data.
209 DVLOG(1) << "Turning FEC protection OFF";
210 should_fec_protect_
= false;
216 if (notifier
!= nullptr && frames_created
== 0) {
217 // Safe to delete the AckNotifer as it was never attached to a packet.
221 // Don't allow the handshake to be bundled with other retransmittable frames.
223 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/false);
226 // Try to close FEC group since we've either run out of data to send or we're
227 // blocked. If not in batch mode, force close the group.
228 MaybeSendFecPacketAndCloseGroup(/*force=*/false, /*is_fec_timeout=*/false);
230 DCHECK(InBatchMode() || !packet_creator_
.HasPendingFrames());
231 return QuicConsumedData(total_bytes_consumed
, fin_consumed
);
234 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const {
235 DCHECK(HasPendingFrames());
236 HasRetransmittableData retransmittable
=
237 (should_send_ack_
|| should_send_stop_waiting_
)
238 ? NO_RETRANSMITTABLE_DATA
239 : HAS_RETRANSMITTABLE_DATA
;
240 if (retransmittable
== HAS_RETRANSMITTABLE_DATA
) {
241 DCHECK(!queued_control_frames_
.empty()); // These are retransmittable.
243 return delegate_
->ShouldGeneratePacket(retransmittable
, NOT_HANDSHAKE
);
246 void QuicPacketGenerator::SendQueuedFrames(bool flush
, bool is_fec_timeout
) {
247 // Only add pending frames if we are SURE we can then send the whole packet.
248 while (HasPendingFrames() &&
249 (flush
|| CanSendWithNextPendingFrameAddition())) {
250 if (!AddNextPendingFrame()) {
251 // Packet was full, so serialize and send it.
252 SerializeAndSendPacket();
255 if (packet_creator_
.HasPendingFrames() && (flush
|| !InBatchMode())) {
256 SerializeAndSendPacket();
258 MaybeSendFecPacketAndCloseGroup(flush
, is_fec_timeout
);
261 void QuicPacketGenerator::MaybeStartFecProtection() {
262 if (!packet_creator_
.IsFecEnabled()) {
265 DVLOG(1) << "Turning FEC protection ON";
266 should_fec_protect_
= true;
267 if (packet_creator_
.IsFecProtected()) {
268 // Only start creator's FEC protection if not already on.
271 if (HasQueuedFrames()) {
272 // TODO(jri): This currently requires that the generator flush out any
273 // pending frames when FEC protection is turned on. If current packet can be
274 // converted to an FEC protected packet, do it. This will require the
275 // generator to check if the resulting expansion still allows the incoming
276 // frame to be added to the packet.
277 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/false);
279 packet_creator_
.StartFecProtectingPackets();
280 DCHECK(packet_creator_
.IsFecProtected());
283 void QuicPacketGenerator::MaybeSendFecPacketAndCloseGroup(bool force
,
284 bool is_fec_timeout
) {
285 if (!ShouldSendFecPacket(force
)) {
289 // If we want to send FEC packet only when FEC alaram goes off and if it is
290 // not a FEC timeout then close the group and dont send FEC packet.
291 if (fec_send_policy_
== FEC_ALARM_TRIGGER
&& !is_fec_timeout
) {
294 // TODO(jri): SerializeFec can return a NULL packet, and this should
295 // cause an early return, with a call to delegate_->OnPacketGenerationError.
296 char buffer
[kMaxPacketSize
];
297 SerializedPacket serialized_fec
=
298 packet_creator_
.SerializeFec(buffer
, kMaxPacketSize
);
299 DCHECK(serialized_fec
.packet
);
300 delegate_
->OnSerializedPacket(serialized_fec
);
302 // Turn FEC protection off if creator's protection is on and the creator
303 // does not have an open FEC group.
304 // Note: We only wait until the frames queued in the creator are flushed;
305 // pending frames in the generator will not keep us from turning FEC off.
306 if (!should_fec_protect_
&& !packet_creator_
.IsFecGroupOpen()) {
307 packet_creator_
.StopFecProtectingPackets();
308 DCHECK(!packet_creator_
.IsFecProtected());
312 bool QuicPacketGenerator::ShouldSendFecPacket(bool force
) {
313 return packet_creator_
.IsFecProtected() &&
314 !packet_creator_
.HasPendingFrames() &&
315 packet_creator_
.ShouldSendFec(force
);
318 void QuicPacketGenerator::ResetFecGroup() {
319 DCHECK(packet_creator_
.IsFecGroupOpen());
320 packet_creator_
.ResetFecGroup();
321 delegate_
->OnResetFecGroup();
324 void QuicPacketGenerator::OnFecTimeout() {
325 DCHECK(!InBatchMode());
326 if (!ShouldSendFecPacket(true)) {
327 LOG(DFATAL
) << "No FEC packet to send on FEC timeout.";
330 // Flush out any pending frames in the generator and the creator, and then
331 // send out FEC packet.
332 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/true);
333 MaybeSendFecPacketAndCloseGroup(/*force=*/true, /*is_fec_timeout=*/true);
336 QuicTime::Delta
QuicPacketGenerator::GetFecTimeout(
337 QuicPacketSequenceNumber sequence_number
) {
338 // Do not set up FEC alarm for |sequence_number| it is not the first packet in
339 // the current group.
340 if (packet_creator_
.IsFecGroupOpen() &&
341 (sequence_number
== packet_creator_
.fec_group_number())) {
342 return QuicTime::Delta::Max(
343 fec_timeout_
, QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs
));
345 return QuicTime::Delta::Infinite();
348 bool QuicPacketGenerator::InBatchMode() {
352 void QuicPacketGenerator::StartBatchOperations() {
356 void QuicPacketGenerator::FinishBatchOperations() {
358 SendQueuedFrames(/*flush=*/false, /*is_fec_timeout=*/false);
361 void QuicPacketGenerator::FlushAllQueuedFrames() {
362 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/false);
365 bool QuicPacketGenerator::HasQueuedFrames() const {
366 return packet_creator_
.HasPendingFrames() || HasPendingFrames();
369 bool QuicPacketGenerator::HasPendingFrames() const {
370 return should_send_ack_
|| should_send_stop_waiting_
||
371 !queued_control_frames_
.empty();
374 bool QuicPacketGenerator::AddNextPendingFrame() {
375 if (should_send_ack_
) {
376 delegate_
->PopulateAckFrame(&pending_ack_frame_
);
378 // If we can't this add the frame now, then we still need to do so later.
379 should_send_ack_
= !AddFrame(QuicFrame(&pending_ack_frame_
), nullptr);
380 // Return success if we have cleared out this flag (i.e., added the frame).
381 // If we still need to send, then the frame is full, and we have failed.
382 return !should_send_ack_
;
385 if (should_send_stop_waiting_
) {
386 delegate_
->PopulateStopWaitingFrame(&pending_stop_waiting_frame_
);
387 stop_waiting_queued_
= true;
388 // If we can't this add the frame now, then we still need to do so later.
389 should_send_stop_waiting_
=
390 !AddFrame(QuicFrame(&pending_stop_waiting_frame_
), nullptr);
391 // Return success if we have cleared out this flag (i.e., added the frame).
392 // If we still need to send, then the frame is full, and we have failed.
393 return !should_send_stop_waiting_
;
396 LOG_IF(DFATAL
, queued_control_frames_
.empty())
397 << "AddNextPendingFrame called with no queued control frames.";
398 if (!AddFrame(queued_control_frames_
.back(), nullptr)) {
402 queued_control_frames_
.pop_back();
406 bool QuicPacketGenerator::AddFrame(const QuicFrame
& frame
, char* buffer
) {
407 bool success
= packet_creator_
.AddSavedFrame(frame
, buffer
);
408 if (success
&& debug_delegate_
) {
409 debug_delegate_
->OnFrameAddedToPacket(frame
);
414 void QuicPacketGenerator::SerializeAndSendPacket() {
415 char buffer
[kMaxPacketSize
];
416 SerializedPacket serialized_packet
=
417 packet_creator_
.SerializePacket(buffer
, kMaxPacketSize
);
418 DCHECK(serialized_packet
.packet
);
420 // There may be AckNotifiers interested in this packet.
421 serialized_packet
.notifiers
.swap(ack_notifiers_
);
422 ack_notifiers_
.clear();
424 delegate_
->OnSerializedPacket(serialized_packet
);
425 MaybeSendFecPacketAndCloseGroup(/*force=*/false, /*is_fec_timeout=*/false);
427 // The packet has now been serialized, so the frames are no longer queued.
429 stop_waiting_queued_
= false;
432 void QuicPacketGenerator::StopSendingVersion() {
433 packet_creator_
.StopSendingVersion();
436 QuicPacketSequenceNumber
QuicPacketGenerator::sequence_number() const {
437 return packet_creator_
.sequence_number();
440 QuicByteCount
QuicPacketGenerator::max_packet_length() const {
441 return packet_creator_
.max_packet_length();
444 void QuicPacketGenerator::set_max_packet_length(QuicByteCount length
) {
445 packet_creator_
.SetMaxPacketLength(length
);
448 QuicEncryptedPacket
* QuicPacketGenerator::SerializeVersionNegotiationPacket(
449 const QuicVersionVector
& supported_versions
) {
450 return packet_creator_
.SerializeVersionNegotiationPacket(supported_versions
);
453 SerializedPacket
QuicPacketGenerator::ReserializeAllFrames(
454 const RetransmittableFrames
& frames
,
455 QuicSequenceNumberLength original_length
,
458 return packet_creator_
.ReserializeAllFrames(frames
, original_length
, buffer
,
462 void QuicPacketGenerator::UpdateSequenceNumberLength(
463 QuicPacketSequenceNumber least_packet_awaited_by_peer
,
464 QuicPacketCount max_packets_in_flight
) {
465 return packet_creator_
.UpdateSequenceNumberLength(
466 least_packet_awaited_by_peer
, max_packets_in_flight
);
469 void QuicPacketGenerator::SetConnectionIdLength(uint32 length
) {
471 packet_creator_
.set_connection_id_length(PACKET_0BYTE_CONNECTION_ID
);
472 } else if (length
== 1) {
473 packet_creator_
.set_connection_id_length(PACKET_1BYTE_CONNECTION_ID
);
474 } else if (length
<= 4) {
475 packet_creator_
.set_connection_id_length(PACKET_4BYTE_CONNECTION_ID
);
477 packet_creator_
.set_connection_id_length(PACKET_8BYTE_CONNECTION_ID
);
481 void QuicPacketGenerator::set_encryption_level(EncryptionLevel level
) {
482 packet_creator_
.set_encryption_level(level
);
485 void QuicPacketGenerator::SetEncrypter(EncryptionLevel level
,
486 QuicEncrypter
* encrypter
) {
487 packet_creator_
.SetEncrypter(level
, encrypter
);