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 fec_send_policy_(FEC_ANY_TRIGGER
),
49 should_send_ack_(false),
50 should_send_stop_waiting_(false),
52 stop_waiting_queued_(false),
53 max_packet_length_(kDefaultMaxPacketSize
) {
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 QuicIOVector
& iov
,
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 if (!fin
&& (iov
.total_length
== 0)) {
160 LOG(DFATAL
) << "Attempt to consume empty data without FIN.";
161 return QuicConsumedData(0, false);
164 int frames_created
= 0;
165 while (delegate_
->ShouldGeneratePacket(
166 HAS_RETRANSMITTABLE_DATA
, has_handshake
? IS_HANDSHAKE
: NOT_HANDSHAKE
)) {
168 scoped_ptr
<char[]> buffer
;
169 size_t bytes_consumed
= packet_creator_
.CreateStreamFrame(
170 id
, iov
, total_bytes_consumed
, offset
+ total_bytes_consumed
, fin
,
174 // We want to track which packet this stream frame ends up in.
175 if (notifier
!= nullptr) {
176 ack_notifiers_
.push_back(notifier
);
179 if (!AddFrame(frame
, buffer
.get(), has_handshake
)) {
180 LOG(DFATAL
) << "Failed to add stream frame.";
181 // Inability to add a STREAM frame creates an unrecoverable hole in a
182 // the stream, so it's best to close the connection.
183 delegate_
->CloseConnection(QUIC_INTERNAL_ERROR
, false);
185 return QuicConsumedData(0, false);
187 // When AddFrame succeeds, it takes ownership of the buffer.
188 ignore_result(buffer
.release());
190 total_bytes_consumed
+= bytes_consumed
;
191 fin_consumed
= fin
&& total_bytes_consumed
== iov
.total_length
;
192 DCHECK(total_bytes_consumed
== iov
.total_length
||
193 packet_creator_
.BytesFree() == 0u);
195 if (!InBatchMode() || !packet_creator_
.HasRoomForStreamFrame(id
, offset
)) {
196 // TODO(rtenneti): remove MaybeSendFecPacketAndCloseGroup() from inside
197 // SerializeAndSendPacket() and make it an explicit call here (and
198 // elsewhere where we call SerializeAndSendPacket?).
199 SerializeAndSendPacket();
202 if (total_bytes_consumed
== iov
.total_length
) {
203 // We're done writing the data. Exit the loop.
204 // We don't make this a precondition because we could have 0 bytes of data
205 // if we're simply writing a fin.
206 if (fec_protection
== MUST_FEC_PROTECT
) {
207 // Turn off FEC protection when we're done writing protected data.
208 DVLOG(1) << "Turning FEC protection OFF";
209 should_fec_protect_
= false;
215 if (notifier
!= nullptr && frames_created
== 0) {
216 // Safe to delete the AckNotifer as it was never attached to a packet.
220 // Don't allow the handshake to be bundled with other retransmittable frames.
222 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/false);
225 // Try to close FEC group since we've either run out of data to send or we're
226 // blocked. If not in batch mode, force close the group.
227 MaybeSendFecPacketAndCloseGroup(/*force=*/false, /*is_fec_timeout=*/false);
229 DCHECK(InBatchMode() || !packet_creator_
.HasPendingFrames());
230 return QuicConsumedData(total_bytes_consumed
, fin_consumed
);
233 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const {
234 DCHECK(HasPendingFrames());
235 HasRetransmittableData retransmittable
=
236 (should_send_ack_
|| should_send_stop_waiting_
)
237 ? NO_RETRANSMITTABLE_DATA
238 : HAS_RETRANSMITTABLE_DATA
;
239 if (retransmittable
== HAS_RETRANSMITTABLE_DATA
) {
240 DCHECK(!queued_control_frames_
.empty()); // These are retransmittable.
242 return delegate_
->ShouldGeneratePacket(retransmittable
, NOT_HANDSHAKE
);
245 void QuicPacketGenerator::SendQueuedFrames(bool flush
, bool is_fec_timeout
) {
246 // Only add pending frames if we are SURE we can then send the whole packet.
247 while (HasPendingFrames() &&
248 (flush
|| CanSendWithNextPendingFrameAddition())) {
249 if (!AddNextPendingFrame()) {
250 // Packet was full, so serialize and send it.
251 SerializeAndSendPacket();
254 if (packet_creator_
.HasPendingFrames() && (flush
|| !InBatchMode())) {
255 SerializeAndSendPacket();
257 MaybeSendFecPacketAndCloseGroup(flush
, is_fec_timeout
);
260 void QuicPacketGenerator::MaybeStartFecProtection() {
261 if (!packet_creator_
.IsFecEnabled()) {
264 DVLOG(1) << "Turning FEC protection ON";
265 should_fec_protect_
= true;
266 if (packet_creator_
.IsFecProtected()) {
267 // Only start creator's FEC protection if not already on.
270 if (HasQueuedFrames()) {
271 // TODO(jri): This currently requires that the generator flush out any
272 // pending frames when FEC protection is turned on. If current packet can be
273 // converted to an FEC protected packet, do it. This will require the
274 // generator to check if the resulting expansion still allows the incoming
275 // frame to be added to the packet.
276 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/false);
278 packet_creator_
.StartFecProtectingPackets();
279 DCHECK(packet_creator_
.IsFecProtected());
282 void QuicPacketGenerator::MaybeSendFecPacketAndCloseGroup(bool force
,
283 bool is_fec_timeout
) {
284 if (!ShouldSendFecPacket(force
)) {
288 // If we want to send FEC packet only when FEC alaram goes off and if it is
289 // not a FEC timeout then close the group and dont send FEC packet.
290 if (fec_send_policy_
== FEC_ALARM_TRIGGER
&& !is_fec_timeout
) {
293 // TODO(jri): SerializeFec can return a NULL packet, and this should
294 // cause an early return, with a call to delegate_->OnPacketGenerationError.
295 char buffer
[kMaxPacketSize
];
296 SerializedPacket serialized_fec
=
297 packet_creator_
.SerializeFec(buffer
, kMaxPacketSize
);
298 DCHECK(serialized_fec
.packet
);
299 delegate_
->OnSerializedPacket(serialized_fec
);
301 // Turn FEC protection off if creator's protection is on and the creator
302 // does not have an open FEC group.
303 // Note: We only wait until the frames queued in the creator are flushed;
304 // pending frames in the generator will not keep us from turning FEC off.
305 if (!should_fec_protect_
&& !packet_creator_
.IsFecGroupOpen()) {
306 packet_creator_
.StopFecProtectingPackets();
307 DCHECK(!packet_creator_
.IsFecProtected());
311 bool QuicPacketGenerator::ShouldSendFecPacket(bool force
) {
312 return packet_creator_
.IsFecProtected() &&
313 !packet_creator_
.HasPendingFrames() &&
314 packet_creator_
.ShouldSendFec(force
);
317 void QuicPacketGenerator::ResetFecGroup() {
318 DCHECK(packet_creator_
.IsFecGroupOpen());
319 packet_creator_
.ResetFecGroup();
320 delegate_
->OnResetFecGroup();
323 void QuicPacketGenerator::OnFecTimeout() {
324 DCHECK(!InBatchMode());
325 if (!ShouldSendFecPacket(true)) {
326 LOG(DFATAL
) << "No FEC packet to send on FEC timeout.";
329 // Flush out any pending frames in the generator and the creator, and then
330 // send out FEC packet.
331 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/true);
332 MaybeSendFecPacketAndCloseGroup(/*force=*/true, /*is_fec_timeout=*/true);
335 QuicTime::Delta
QuicPacketGenerator::GetFecTimeout(
336 QuicPacketSequenceNumber sequence_number
) {
337 // Do not set up FEC alarm for |sequence_number| it is not the first packet in
338 // the current group.
339 if (packet_creator_
.IsFecGroupOpen() &&
340 (sequence_number
== packet_creator_
.fec_group_number())) {
341 return QuicTime::Delta::Max(
342 fec_timeout_
, QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs
));
344 return QuicTime::Delta::Infinite();
347 bool QuicPacketGenerator::InBatchMode() {
351 void QuicPacketGenerator::StartBatchOperations() {
355 void QuicPacketGenerator::FinishBatchOperations() {
357 SendQueuedFrames(/*flush=*/false, /*is_fec_timeout=*/false);
360 void QuicPacketGenerator::FlushAllQueuedFrames() {
361 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/false);
364 bool QuicPacketGenerator::HasQueuedFrames() const {
365 return packet_creator_
.HasPendingFrames() || HasPendingFrames();
368 bool QuicPacketGenerator::HasPendingFrames() const {
369 return should_send_ack_
|| should_send_stop_waiting_
||
370 !queued_control_frames_
.empty();
373 bool QuicPacketGenerator::AddNextPendingFrame() {
374 if (should_send_ack_
) {
375 delegate_
->PopulateAckFrame(&pending_ack_frame_
);
377 // If we can't this add the frame now, then we still need to do so later.
378 should_send_ack_
= !AddFrame(QuicFrame(&pending_ack_frame_
), nullptr,
379 /*needs_padding=*/false);
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 /*needs_padding=*/false);
392 // Return success if we have cleared out this flag (i.e., added the frame).
393 // If we still need to send, then the frame is full, and we have failed.
394 return !should_send_stop_waiting_
;
397 LOG_IF(DFATAL
, queued_control_frames_
.empty())
398 << "AddNextPendingFrame called with no queued control frames.";
399 if (!AddFrame(queued_control_frames_
.back(), nullptr,
400 /*needs_padding=*/false)) {
404 queued_control_frames_
.pop_back();
408 bool QuicPacketGenerator::AddFrame(const QuicFrame
& frame
,
410 bool needs_padding
) {
411 bool success
= needs_padding
412 ? packet_creator_
.AddPaddedSavedFrame(frame
, buffer
)
413 : packet_creator_
.AddSavedFrame(frame
, buffer
);
414 if (success
&& debug_delegate_
) {
415 debug_delegate_
->OnFrameAddedToPacket(frame
);
420 void QuicPacketGenerator::SerializeAndSendPacket() {
421 char buffer
[kMaxPacketSize
];
422 SerializedPacket serialized_packet
=
423 packet_creator_
.SerializePacket(buffer
, kMaxPacketSize
);
424 DCHECK(serialized_packet
.packet
);
426 // There may be AckNotifiers interested in this packet.
427 serialized_packet
.notifiers
.swap(ack_notifiers_
);
428 ack_notifiers_
.clear();
430 delegate_
->OnSerializedPacket(serialized_packet
);
431 MaybeSendFecPacketAndCloseGroup(/*force=*/false, /*is_fec_timeout=*/false);
433 // Maximum packet size may be only enacted while no packet is currently being
434 // constructed, so here we have a good opportunity to actually change it.
435 if (packet_creator_
.CanSetMaxPacketLength()) {
436 packet_creator_
.SetMaxPacketLength(max_packet_length_
);
439 // The packet has now been serialized, so the frames are no longer queued.
441 stop_waiting_queued_
= false;
444 void QuicPacketGenerator::StopSendingVersion() {
445 packet_creator_
.StopSendingVersion();
448 QuicPacketSequenceNumber
QuicPacketGenerator::sequence_number() const {
449 return packet_creator_
.sequence_number();
452 QuicByteCount
QuicPacketGenerator::GetMaxPacketLength() const {
453 return max_packet_length_
;
456 QuicByteCount
QuicPacketGenerator::GetCurrentMaxPacketLength() const {
457 return packet_creator_
.max_packet_length();
460 void QuicPacketGenerator::SetMaxPacketLength(QuicByteCount length
, bool force
) {
461 // If we cannot immediately set new maximum packet length, and the |force|
462 // flag is set, we have to flush the contents of the queue and close existing
464 if (!packet_creator_
.CanSetMaxPacketLength() && force
) {
465 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/false);
466 MaybeSendFecPacketAndCloseGroup(/*force=*/true, /*is_fec_timeout=*/false);
467 DCHECK(packet_creator_
.CanSetMaxPacketLength());
470 max_packet_length_
= length
;
471 if (packet_creator_
.CanSetMaxPacketLength()) {
472 packet_creator_
.SetMaxPacketLength(length
);
476 QuicEncryptedPacket
* QuicPacketGenerator::SerializeVersionNegotiationPacket(
477 const QuicVersionVector
& supported_versions
) {
478 return packet_creator_
.SerializeVersionNegotiationPacket(supported_versions
);
481 SerializedPacket
QuicPacketGenerator::ReserializeAllFrames(
482 const RetransmittableFrames
& frames
,
483 QuicSequenceNumberLength original_length
,
486 return packet_creator_
.ReserializeAllFrames(frames
, original_length
, buffer
,
490 void QuicPacketGenerator::UpdateSequenceNumberLength(
491 QuicPacketSequenceNumber least_packet_awaited_by_peer
,
492 QuicPacketCount max_packets_in_flight
) {
493 return packet_creator_
.UpdateSequenceNumberLength(
494 least_packet_awaited_by_peer
, max_packets_in_flight
);
497 void QuicPacketGenerator::SetConnectionIdLength(uint32 length
) {
499 packet_creator_
.set_connection_id_length(PACKET_0BYTE_CONNECTION_ID
);
500 } else if (length
== 1) {
501 packet_creator_
.set_connection_id_length(PACKET_1BYTE_CONNECTION_ID
);
502 } else if (length
<= 4) {
503 packet_creator_
.set_connection_id_length(PACKET_4BYTE_CONNECTION_ID
);
505 packet_creator_
.set_connection_id_length(PACKET_8BYTE_CONNECTION_ID
);
509 void QuicPacketGenerator::set_encryption_level(EncryptionLevel level
) {
510 packet_creator_
.set_encryption_level(level
);
513 void QuicPacketGenerator::SetEncrypter(EncryptionLevel level
,
514 QuicEncrypter
* encrypter
) {
515 packet_creator_
.SetEncrypter(level
, encrypter
);