Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / quic_packet_generator.cc
blobe058178ac3c9ef34515d3a80a364124517a4caad
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;
16 namespace net {
18 namespace {
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
31 // timeout of 10ms.
32 const int64 kMinFecTimeoutMs = 5u;
34 } // namespace
36 class QuicAckNotifier;
38 QuicPacketGenerator::QuicPacketGenerator(QuicConnectionId connection_id,
39 QuicFramer* framer,
40 QuicRandom* random_generator,
41 DelegateInterface* delegate)
42 : delegate_(delegate),
43 debug_delegate_(nullptr),
44 packet_creator_(connection_id, framer, random_generator),
45 batch_mode_(false),
46 fec_timeout_(QuicTime::Delta::Zero()),
47 rtt_multiplier_for_fec_timeout_(kRttMultiplierForFecTimeout),
48 should_fec_protect_(false),
49 fec_send_policy_(FEC_ANY_TRIGGER),
50 should_send_ack_(false),
51 should_send_stop_waiting_(false),
52 ack_queued_(false),
53 stop_waiting_queued_(false),
54 max_packet_length_(kDefaultMaxPacketSize) {}
56 QuicPacketGenerator::~QuicPacketGenerator() {
57 for (QuicFrame& frame : queued_control_frames_) {
58 switch (frame.type) {
59 case PADDING_FRAME:
60 delete frame.padding_frame;
61 break;
62 case STREAM_FRAME:
63 delete frame.stream_frame;
64 break;
65 case ACK_FRAME:
66 delete frame.ack_frame;
67 break;
68 case MTU_DISCOVERY_FRAME:
69 delete frame.mtu_discovery_frame;
70 break;
71 case RST_STREAM_FRAME:
72 delete frame.rst_stream_frame;
73 break;
74 case CONNECTION_CLOSE_FRAME:
75 delete frame.connection_close_frame;
76 break;
77 case GOAWAY_FRAME:
78 delete frame.goaway_frame;
79 break;
80 case WINDOW_UPDATE_FRAME:
81 delete frame.window_update_frame;
82 break;
83 case BLOCKED_FRAME:
84 delete frame.blocked_frame;
85 break;
86 case STOP_WAITING_FRAME:
87 delete frame.stop_waiting_frame;
88 break;
89 case PING_FRAME:
90 delete frame.ping_frame;
91 break;
92 case NUM_FRAME_TYPES:
93 DCHECK(false) << "Cannot delete type: " << frame.type;
98 void QuicPacketGenerator::OnCongestionWindowChange(
99 QuicPacketCount max_packets_in_flight) {
100 packet_creator_.set_max_packets_per_fec_group(
101 static_cast<size_t>(kMaxPacketsInFlightMultiplierForFecGroupSize *
102 max_packets_in_flight));
105 void QuicPacketGenerator::OnRttChange(QuicTime::Delta rtt) {
106 fec_timeout_ = rtt.Multiply(rtt_multiplier_for_fec_timeout_);
109 void QuicPacketGenerator::SetShouldSendAck(bool also_send_stop_waiting) {
110 if (ack_queued_) {
111 // Ack already queued, nothing to do.
112 return;
115 if (also_send_stop_waiting && stop_waiting_queued_) {
116 LOG(DFATAL) << "Should only ever be one pending stop waiting frame.";
117 return;
120 should_send_ack_ = true;
121 should_send_stop_waiting_ = also_send_stop_waiting;
122 SendQueuedFrames(/*flush=*/false, /*is_fec_timeout=*/false);
125 void QuicPacketGenerator::AddControlFrame(const QuicFrame& frame) {
126 queued_control_frames_.push_back(frame);
127 SendQueuedFrames(/*flush=*/false, /*is_fec_timeout=*/false);
130 QuicConsumedData QuicPacketGenerator::ConsumeData(
131 QuicStreamId id,
132 const QuicIOVector& iov,
133 QuicStreamOffset offset,
134 bool fin,
135 FecProtection fec_protection,
136 QuicAckNotifier::DelegateInterface* delegate) {
137 bool has_handshake = id == kCryptoStreamId;
138 // To make reasoning about crypto frames easier, we don't combine them with
139 // other retransmittable frames in a single packet.
140 const bool flush =
141 has_handshake && packet_creator_.HasPendingRetransmittableFrames();
142 SendQueuedFrames(flush, /*is_fec_timeout=*/false);
144 size_t total_bytes_consumed = 0;
145 bool fin_consumed = false;
147 if (!packet_creator_.HasRoomForStreamFrame(id, offset)) {
148 SerializeAndSendPacket();
151 if (fec_protection == MUST_FEC_PROTECT) {
152 MaybeStartFecProtection();
155 // This notifier will be owned by the AckNotifierManager (or deleted below) if
156 // not attached to a packet.
157 QuicAckNotifier* notifier = nullptr;
158 if (delegate != nullptr) {
159 notifier = new QuicAckNotifier(delegate);
162 if (!fin && (iov.total_length == 0)) {
163 LOG(DFATAL) << "Attempt to consume empty data without FIN.";
164 return QuicConsumedData(0, false);
167 int frames_created = 0;
168 while (delegate_->ShouldGeneratePacket(
169 HAS_RETRANSMITTABLE_DATA, has_handshake ? IS_HANDSHAKE : NOT_HANDSHAKE)) {
170 QuicFrame frame;
171 scoped_ptr<char[]> buffer;
172 size_t bytes_consumed = packet_creator_.CreateStreamFrame(
173 id, iov, total_bytes_consumed, offset + total_bytes_consumed, fin,
174 &frame, &buffer);
175 ++frames_created;
177 // We want to track which packet this stream frame ends up in.
178 if (notifier != nullptr) {
179 ack_notifiers_.push_back(notifier);
182 if (!AddFrame(frame, buffer.get(), has_handshake)) {
183 LOG(DFATAL) << "Failed to add stream frame.";
184 // Inability to add a STREAM frame creates an unrecoverable hole in a
185 // the stream, so it's best to close the connection.
186 delegate_->CloseConnection(QUIC_INTERNAL_ERROR, false);
187 delete notifier;
188 return QuicConsumedData(0, false);
190 // When AddFrame succeeds, it takes ownership of the buffer.
191 ignore_result(buffer.release());
193 total_bytes_consumed += bytes_consumed;
194 fin_consumed = fin && total_bytes_consumed == iov.total_length;
195 DCHECK(total_bytes_consumed == iov.total_length ||
196 packet_creator_.BytesFree() == 0u);
198 if (!InBatchMode() || !packet_creator_.HasRoomForStreamFrame(id, offset)) {
199 // TODO(rtenneti): remove MaybeSendFecPacketAndCloseGroup() from inside
200 // SerializeAndSendPacket() and make it an explicit call here (and
201 // elsewhere where we call SerializeAndSendPacket?).
202 SerializeAndSendPacket();
205 if (total_bytes_consumed == iov.total_length) {
206 // We're done writing the data. Exit the loop.
207 // We don't make this a precondition because we could have 0 bytes of data
208 // if we're simply writing a fin.
209 if (fec_protection == MUST_FEC_PROTECT) {
210 // Turn off FEC protection when we're done writing protected data.
211 DVLOG(1) << "Turning FEC protection OFF";
212 should_fec_protect_ = false;
214 break;
218 if (notifier != nullptr && frames_created == 0) {
219 // Safe to delete the AckNotifer as it was never attached to a packet.
220 delete notifier;
223 // Don't allow the handshake to be bundled with other retransmittable frames.
224 if (has_handshake) {
225 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/false);
228 // Try to close FEC group since we've either run out of data to send or we're
229 // blocked. If not in batch mode, force close the group.
230 MaybeSendFecPacketAndCloseGroup(/*force=*/false, /*is_fec_timeout=*/false);
232 DCHECK(InBatchMode() || !packet_creator_.HasPendingFrames());
233 return QuicConsumedData(total_bytes_consumed, fin_consumed);
236 void QuicPacketGenerator::GenerateMtuDiscoveryPacket(
237 QuicByteCount target_mtu,
238 QuicAckNotifier::DelegateInterface* delegate) {
239 // MTU discovery frames must be sent by themselves.
240 DCHECK(!InBatchMode() && !packet_creator_.HasPendingFrames());
242 // If an ack notifier delegate is provided, register it.
243 if (delegate) {
244 QuicAckNotifier* ack_notifier = new QuicAckNotifier(delegate);
245 // The notifier manager will take the ownership of the notifier after the
246 // packet is sent.
247 ack_notifiers_.push_back(ack_notifier);
250 const QuicByteCount current_mtu = GetMaxPacketLength();
252 // The MTU discovery frame is allocated on the stack, since it is going to be
253 // serialized within this function.
254 QuicMtuDiscoveryFrame mtu_discovery_frame;
255 QuicFrame frame(&mtu_discovery_frame);
257 // Send the probe packet with the new length.
258 SetMaxPacketLength(target_mtu, /*force=*/true);
259 const bool success = AddFrame(frame, nullptr, /*needs_padding=*/true);
260 SerializeAndSendPacket();
261 // The only reason AddFrame can fail is that the packet is too full to fit in
262 // a ping. This is not possible for any sane MTU.
263 DCHECK(success);
265 // Reset the packet length back.
266 SetMaxPacketLength(current_mtu, /*force=*/true);
269 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const {
270 DCHECK(HasPendingFrames());
271 HasRetransmittableData retransmittable =
272 (should_send_ack_ || should_send_stop_waiting_)
273 ? NO_RETRANSMITTABLE_DATA
274 : HAS_RETRANSMITTABLE_DATA;
275 if (retransmittable == HAS_RETRANSMITTABLE_DATA) {
276 DCHECK(!queued_control_frames_.empty()); // These are retransmittable.
278 return delegate_->ShouldGeneratePacket(retransmittable, NOT_HANDSHAKE);
281 void QuicPacketGenerator::SendQueuedFrames(bool flush, bool is_fec_timeout) {
282 // Only add pending frames if we are SURE we can then send the whole packet.
283 while (HasPendingFrames() &&
284 (flush || CanSendWithNextPendingFrameAddition())) {
285 if (!AddNextPendingFrame()) {
286 // Packet was full, so serialize and send it.
287 SerializeAndSendPacket();
290 if (packet_creator_.HasPendingFrames() && (flush || !InBatchMode())) {
291 SerializeAndSendPacket();
293 MaybeSendFecPacketAndCloseGroup(flush, is_fec_timeout);
296 void QuicPacketGenerator::MaybeStartFecProtection() {
297 if (!packet_creator_.IsFecEnabled()) {
298 return;
300 DVLOG(1) << "Turning FEC protection ON";
301 should_fec_protect_ = true;
302 if (packet_creator_.IsFecProtected()) {
303 // Only start creator's FEC protection if not already on.
304 return;
306 if (HasQueuedFrames()) {
307 // TODO(jri): This currently requires that the generator flush out any
308 // pending frames when FEC protection is turned on. If current packet can be
309 // converted to an FEC protected packet, do it. This will require the
310 // generator to check if the resulting expansion still allows the incoming
311 // frame to be added to the packet.
312 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/false);
314 packet_creator_.StartFecProtectingPackets();
315 DCHECK(packet_creator_.IsFecProtected());
318 void QuicPacketGenerator::MaybeSendFecPacketAndCloseGroup(bool force,
319 bool is_fec_timeout) {
320 if (!ShouldSendFecPacket(force)) {
321 return;
324 // If we want to send FEC packet only when FEC alaram goes off and if it is
325 // not a FEC timeout then close the group and dont send FEC packet.
326 if (fec_send_policy_ == FEC_ALARM_TRIGGER && !is_fec_timeout) {
327 ResetFecGroup();
328 } else {
329 // TODO(jri): SerializeFec can return a NULL packet, and this should
330 // cause an early return, with a call to delegate_->OnPacketGenerationError.
331 char buffer[kMaxPacketSize];
332 SerializedPacket serialized_fec =
333 packet_creator_.SerializeFec(buffer, kMaxPacketSize);
334 DCHECK(serialized_fec.packet);
335 delegate_->OnSerializedPacket(serialized_fec);
337 // Turn FEC protection off if creator's protection is on and the creator
338 // does not have an open FEC group.
339 // Note: We only wait until the frames queued in the creator are flushed;
340 // pending frames in the generator will not keep us from turning FEC off.
341 if (!should_fec_protect_ && !packet_creator_.IsFecGroupOpen()) {
342 packet_creator_.StopFecProtectingPackets();
343 DCHECK(!packet_creator_.IsFecProtected());
347 bool QuicPacketGenerator::ShouldSendFecPacket(bool force) {
348 return packet_creator_.IsFecProtected() &&
349 !packet_creator_.HasPendingFrames() &&
350 packet_creator_.ShouldSendFec(force);
353 void QuicPacketGenerator::ResetFecGroup() {
354 DCHECK(packet_creator_.IsFecGroupOpen());
355 packet_creator_.ResetFecGroup();
356 delegate_->OnResetFecGroup();
359 void QuicPacketGenerator::OnFecTimeout() {
360 DCHECK(!InBatchMode());
361 if (!ShouldSendFecPacket(true)) {
362 LOG(DFATAL) << "No FEC packet to send on FEC timeout.";
363 return;
365 // Flush out any pending frames in the generator and the creator, and then
366 // send out FEC packet.
367 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/true);
368 MaybeSendFecPacketAndCloseGroup(/*force=*/true, /*is_fec_timeout=*/true);
371 QuicTime::Delta QuicPacketGenerator::GetFecTimeout(
372 QuicPacketNumber packet_number) {
373 // Do not set up FEC alarm for |packet_number| it is not the first packet in
374 // the current group.
375 if (packet_creator_.IsFecGroupOpen() &&
376 (packet_number == packet_creator_.fec_group_number())) {
377 return QuicTime::Delta::Max(
378 fec_timeout_, QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs));
380 return QuicTime::Delta::Infinite();
383 bool QuicPacketGenerator::InBatchMode() {
384 return batch_mode_;
387 void QuicPacketGenerator::StartBatchOperations() {
388 batch_mode_ = true;
391 void QuicPacketGenerator::FinishBatchOperations() {
392 batch_mode_ = false;
393 SendQueuedFrames(/*flush=*/false, /*is_fec_timeout=*/false);
396 void QuicPacketGenerator::FlushAllQueuedFrames() {
397 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/false);
400 bool QuicPacketGenerator::HasQueuedFrames() const {
401 return packet_creator_.HasPendingFrames() || HasPendingFrames();
404 bool QuicPacketGenerator::HasPendingFrames() const {
405 return should_send_ack_ || should_send_stop_waiting_ ||
406 !queued_control_frames_.empty();
409 bool QuicPacketGenerator::AddNextPendingFrame() {
410 if (should_send_ack_) {
411 delegate_->PopulateAckFrame(&pending_ack_frame_);
412 ack_queued_ = true;
413 // If we can't this add the frame now, then we still need to do so later.
414 should_send_ack_ = !AddFrame(QuicFrame(&pending_ack_frame_), nullptr,
415 /*needs_padding=*/false);
416 // Return success if we have cleared out this flag (i.e., added the frame).
417 // If we still need to send, then the frame is full, and we have failed.
418 return !should_send_ack_;
421 if (should_send_stop_waiting_) {
422 delegate_->PopulateStopWaitingFrame(&pending_stop_waiting_frame_);
423 stop_waiting_queued_ = true;
424 // If we can't this add the frame now, then we still need to do so later.
425 should_send_stop_waiting_ =
426 !AddFrame(QuicFrame(&pending_stop_waiting_frame_), nullptr,
427 /*needs_padding=*/false);
428 // Return success if we have cleared out this flag (i.e., added the frame).
429 // If we still need to send, then the frame is full, and we have failed.
430 return !should_send_stop_waiting_;
433 LOG_IF(DFATAL, queued_control_frames_.empty())
434 << "AddNextPendingFrame called with no queued control frames.";
435 if (!AddFrame(queued_control_frames_.back(), nullptr,
436 /*needs_padding=*/false)) {
437 // Packet was full.
438 return false;
440 queued_control_frames_.pop_back();
441 return true;
444 bool QuicPacketGenerator::AddFrame(const QuicFrame& frame,
445 char* buffer,
446 bool needs_padding) {
447 bool success = needs_padding
448 ? packet_creator_.AddPaddedSavedFrame(frame, buffer)
449 : packet_creator_.AddSavedFrame(frame, buffer);
450 if (success && debug_delegate_) {
451 debug_delegate_->OnFrameAddedToPacket(frame);
453 return success;
456 void QuicPacketGenerator::SerializeAndSendPacket() {
457 char buffer[kMaxPacketSize];
458 SerializedPacket serialized_packet =
459 packet_creator_.SerializePacket(buffer, kMaxPacketSize);
460 if (serialized_packet.packet == nullptr) {
461 LOG(DFATAL) << "Failed to SerializePacket. fec_policy:" << fec_send_policy_
462 << " should_fec_protect_:" << should_fec_protect_;
463 delegate_->CloseConnection(QUIC_FAILED_TO_SERIALIZE_PACKET, false);
464 return;
467 // There may be AckNotifiers interested in this packet.
468 serialized_packet.notifiers.swap(ack_notifiers_);
469 ack_notifiers_.clear();
471 delegate_->OnSerializedPacket(serialized_packet);
472 MaybeSendFecPacketAndCloseGroup(/*force=*/false, /*is_fec_timeout=*/false);
474 // Maximum packet size may be only enacted while no packet is currently being
475 // constructed, so here we have a good opportunity to actually change it.
476 if (packet_creator_.CanSetMaxPacketLength()) {
477 packet_creator_.SetMaxPacketLength(max_packet_length_);
480 // The packet has now been serialized, so the frames are no longer queued.
481 ack_queued_ = false;
482 stop_waiting_queued_ = false;
485 void QuicPacketGenerator::StopSendingVersion() {
486 packet_creator_.StopSendingVersion();
489 QuicPacketNumber QuicPacketGenerator::packet_number() const {
490 return packet_creator_.packet_number();
493 QuicByteCount QuicPacketGenerator::GetMaxPacketLength() const {
494 return max_packet_length_;
497 QuicByteCount QuicPacketGenerator::GetCurrentMaxPacketLength() const {
498 return packet_creator_.max_packet_length();
501 void QuicPacketGenerator::SetMaxPacketLength(QuicByteCount length, bool force) {
502 // If we cannot immediately set new maximum packet length, and the |force|
503 // flag is set, we have to flush the contents of the queue and close existing
504 // FEC group.
505 if (!packet_creator_.CanSetMaxPacketLength() && force) {
506 SendQueuedFrames(/*flush=*/true, /*is_fec_timeout=*/false);
507 MaybeSendFecPacketAndCloseGroup(/*force=*/true, /*is_fec_timeout=*/false);
508 DCHECK(packet_creator_.CanSetMaxPacketLength());
511 max_packet_length_ = length;
512 if (packet_creator_.CanSetMaxPacketLength()) {
513 packet_creator_.SetMaxPacketLength(length);
517 QuicEncryptedPacket* QuicPacketGenerator::SerializeVersionNegotiationPacket(
518 const QuicVersionVector& supported_versions) {
519 return packet_creator_.SerializeVersionNegotiationPacket(supported_versions);
522 SerializedPacket QuicPacketGenerator::ReserializeAllFrames(
523 const RetransmittableFrames& frames,
524 QuicPacketNumberLength original_length,
525 char* buffer,
526 size_t buffer_len) {
527 return packet_creator_.ReserializeAllFrames(frames, original_length, buffer,
528 buffer_len);
531 void QuicPacketGenerator::UpdateSequenceNumberLength(
532 QuicPacketNumber least_packet_awaited_by_peer,
533 QuicPacketCount max_packets_in_flight) {
534 return packet_creator_.UpdatePacketNumberLength(least_packet_awaited_by_peer,
535 max_packets_in_flight);
538 void QuicPacketGenerator::SetConnectionIdLength(uint32 length) {
539 if (length == 0) {
540 packet_creator_.set_connection_id_length(PACKET_0BYTE_CONNECTION_ID);
541 } else if (length == 1) {
542 packet_creator_.set_connection_id_length(PACKET_1BYTE_CONNECTION_ID);
543 } else if (length <= 4) {
544 packet_creator_.set_connection_id_length(PACKET_4BYTE_CONNECTION_ID);
545 } else {
546 packet_creator_.set_connection_id_length(PACKET_8BYTE_CONNECTION_ID);
550 void QuicPacketGenerator::set_encryption_level(EncryptionLevel level) {
551 packet_creator_.set_encryption_level(level);
554 void QuicPacketGenerator::SetEncrypter(EncryptionLevel level,
555 QuicEncrypter* encrypter) {
556 packet_creator_.SetEncrypter(level, encrypter);
559 } // namespace net