Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / net / quic / quic_packet_generator.cc
blob4058efa6e555338273cbc4ea8a08f4c1a02cd8ae
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 should_fec_protect_(false),
48 should_send_ack_(false),
49 should_send_stop_waiting_(false),
50 ack_queued_(false),
51 stop_waiting_queued_(false) {
54 QuicPacketGenerator::~QuicPacketGenerator() {
55 for (QuicFrames::iterator it = queued_control_frames_.begin();
56 it != queued_control_frames_.end(); ++it) {
57 switch (it->type) {
58 case PADDING_FRAME:
59 delete it->padding_frame;
60 break;
61 case STREAM_FRAME:
62 delete it->stream_frame;
63 break;
64 case ACK_FRAME:
65 delete it->ack_frame;
66 break;
67 case RST_STREAM_FRAME:
68 delete it->rst_stream_frame;
69 break;
70 case CONNECTION_CLOSE_FRAME:
71 delete it->connection_close_frame;
72 break;
73 case GOAWAY_FRAME:
74 delete it->goaway_frame;
75 break;
76 case WINDOW_UPDATE_FRAME:
77 delete it->window_update_frame;
78 break;
79 case BLOCKED_FRAME:
80 delete it->blocked_frame;
81 break;
82 case STOP_WAITING_FRAME:
83 delete it->stop_waiting_frame;
84 break;
85 case PING_FRAME:
86 delete it->ping_frame;
87 break;
88 case NUM_FRAME_TYPES:
89 DCHECK(false) << "Cannot delete type: " << it->type;
94 void QuicPacketGenerator::OnCongestionWindowChange(
95 QuicPacketCount max_packets_in_flight) {
96 packet_creator_.set_max_packets_per_fec_group(
97 static_cast<size_t>(kMaxPacketsInFlightMultiplierForFecGroupSize *
98 max_packets_in_flight));
101 void QuicPacketGenerator::OnRttChange(QuicTime::Delta rtt) {
102 fec_timeout_ = rtt.Multiply(kRttMultiplierForFecTimeout);
105 void QuicPacketGenerator::SetShouldSendAck(bool also_send_stop_waiting) {
106 if (ack_queued_) {
107 // Ack already queued, nothing to do.
108 return;
111 if (also_send_stop_waiting && stop_waiting_queued_) {
112 LOG(DFATAL) << "Should only ever be one pending stop waiting frame.";
113 return;
116 should_send_ack_ = true;
117 should_send_stop_waiting_ = also_send_stop_waiting;
118 SendQueuedFrames(false);
121 void QuicPacketGenerator::AddControlFrame(const QuicFrame& frame) {
122 queued_control_frames_.push_back(frame);
123 SendQueuedFrames(false);
126 QuicConsumedData QuicPacketGenerator::ConsumeData(
127 QuicStreamId id,
128 const IOVector& data_to_write,
129 QuicStreamOffset offset,
130 bool fin,
131 FecProtection fec_protection,
132 QuicAckNotifier::DelegateInterface* delegate) {
133 bool has_handshake = id == kCryptoStreamId;
134 // To make reasoning about crypto frames easier, we don't combine them with
135 // other retransmittable frames in a single packet.
136 const bool flush =
137 has_handshake && packet_creator_.HasPendingRetransmittableFrames();
138 SendQueuedFrames(flush);
140 size_t total_bytes_consumed = 0;
141 bool fin_consumed = false;
143 if (!packet_creator_.HasRoomForStreamFrame(id, offset)) {
144 SerializeAndSendPacket();
147 if (fec_protection == MUST_FEC_PROTECT) {
148 MaybeStartFecProtection();
151 // This notifier will be owned by the AckNotifierManager (or deleted below) if
152 // not attached to a packet.
153 QuicAckNotifier* notifier = nullptr;
154 if (delegate != nullptr) {
155 notifier = new QuicAckNotifier(delegate);
158 IOVector data = data_to_write;
159 size_t data_size = data.TotalBufferSize();
160 if (!fin && (data_size == 0)) {
161 LOG(DFATAL) << "Attempt to consume empty data without FIN.";
162 return QuicConsumedData(0, false);
165 int frames_created = 0;
166 while (delegate_->ShouldGeneratePacket(
167 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA,
168 has_handshake ? IS_HANDSHAKE : NOT_HANDSHAKE)) {
169 QuicFrame frame;
170 size_t bytes_consumed = packet_creator_.CreateStreamFrame(
171 id, data, offset + total_bytes_consumed, fin, &frame);
172 ++frames_created;
174 // We want to track which packet this stream frame ends up in.
175 if (FLAGS_quic_attach_ack_notifiers_to_packets) {
176 if (notifier != nullptr) {
177 ack_notifiers_.push_back(notifier);
179 } else {
180 frame.stream_frame->notifier = notifier;
183 if (!AddFrame(frame)) {
184 LOG(DFATAL) << "Failed to add stream frame.";
185 // Inability to add a STREAM frame creates an unrecoverable hole in a
186 // the stream, so it's best to close the connection.
187 delegate_->CloseConnection(QUIC_INTERNAL_ERROR, false);
188 delete notifier;
189 return QuicConsumedData(0, false);
192 total_bytes_consumed += bytes_consumed;
193 fin_consumed = fin && total_bytes_consumed == data_size;
194 data.Consume(bytes_consumed);
195 DCHECK(data.Empty() || packet_creator_.BytesFree() == 0u);
197 // TODO(ianswett): Restore packet reordering.
198 if (!InBatchMode() || !packet_creator_.HasRoomForStreamFrame(id, offset)) {
199 SerializeAndSendPacket();
202 if (data.Empty()) {
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;
211 break;
215 if (notifier != nullptr && frames_created == 0) {
216 // Safe to delete the AckNotifer as it was never attached to a packet.
217 delete notifier;
220 // Don't allow the handshake to be bundled with other retransmittable frames.
221 if (has_handshake) {
222 SendQueuedFrames(true);
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);
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(NOT_RETRANSMISSION, retransmittable,
243 NOT_HANDSHAKE);
246 void QuicPacketGenerator::SendQueuedFrames(bool flush) {
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);
261 void QuicPacketGenerator::MaybeStartFecProtection() {
262 if (!packet_creator_.IsFecEnabled()) {
263 return;
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.
269 return;
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(true);
279 packet_creator_.StartFecProtectingPackets();
280 DCHECK(packet_creator_.IsFecProtected());
283 void QuicPacketGenerator::MaybeSendFecPacketAndCloseGroup(bool force) {
284 if (!ShouldSendFecPacket(force)) {
285 return;
287 // TODO(jri): SerializeFec can return a NULL packet, and this should
288 // cause an early return, with a call to delegate_->OnPacketGenerationError.
289 SerializedPacket serialized_fec = packet_creator_.SerializeFec();
290 DCHECK(serialized_fec.packet);
291 delegate_->OnSerializedPacket(serialized_fec);
292 // Turn FEC protection off if creator's protection is on and the creator
293 // does not have an open FEC group.
294 // Note: We only wait until the frames queued in the creator are flushed;
295 // pending frames in the generator will not keep us from turning FEC off.
296 if (!should_fec_protect_ && !packet_creator_.IsFecGroupOpen()) {
297 packet_creator_.StopFecProtectingPackets();
298 DCHECK(!packet_creator_.IsFecProtected());
302 bool QuicPacketGenerator::ShouldSendFecPacket(bool force) {
303 return packet_creator_.IsFecProtected() &&
304 !packet_creator_.HasPendingFrames() &&
305 packet_creator_.ShouldSendFec(force);
308 void QuicPacketGenerator::OnFecTimeout() {
309 DCHECK(!InBatchMode());
310 if (!ShouldSendFecPacket(true)) {
311 LOG(DFATAL) << "No FEC packet to send on FEC timeout.";
312 return;
314 // Flush out any pending frames in the generator and the creator, and then
315 // send out FEC packet.
316 SendQueuedFrames(true);
317 MaybeSendFecPacketAndCloseGroup(/*force=*/true);
320 QuicTime::Delta QuicPacketGenerator::GetFecTimeout(
321 QuicPacketSequenceNumber sequence_number) {
322 // Do not set up FEC alarm for |sequence_number| it is not the first packet in
323 // the current group.
324 if (packet_creator_.IsFecGroupOpen() &&
325 (sequence_number == packet_creator_.fec_group_number())) {
326 return QuicTime::Delta::Max(
327 fec_timeout_, QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs));
329 return QuicTime::Delta::Infinite();
332 bool QuicPacketGenerator::InBatchMode() {
333 return batch_mode_;
336 void QuicPacketGenerator::StartBatchOperations() {
337 batch_mode_ = true;
340 void QuicPacketGenerator::FinishBatchOperations() {
341 batch_mode_ = false;
342 SendQueuedFrames(false);
345 void QuicPacketGenerator::FlushAllQueuedFrames() {
346 SendQueuedFrames(true);
349 bool QuicPacketGenerator::HasQueuedFrames() const {
350 return packet_creator_.HasPendingFrames() || HasPendingFrames();
353 bool QuicPacketGenerator::HasPendingFrames() const {
354 return should_send_ack_ || should_send_stop_waiting_ ||
355 !queued_control_frames_.empty();
358 bool QuicPacketGenerator::AddNextPendingFrame() {
359 if (should_send_ack_) {
360 delegate_->PopulateAckFrame(&pending_ack_frame_);
361 ack_queued_ = true;
362 // If we can't this add the frame now, then we still need to do so later.
363 should_send_ack_ = !AddFrame(QuicFrame(&pending_ack_frame_));
364 // Return success if we have cleared out this flag (i.e., added the frame).
365 // If we still need to send, then the frame is full, and we have failed.
366 return !should_send_ack_;
369 if (should_send_stop_waiting_) {
370 delegate_->PopulateStopWaitingFrame(&pending_stop_waiting_frame_);
371 stop_waiting_queued_ = true;
372 // If we can't this add the frame now, then we still need to do so later.
373 should_send_stop_waiting_ =
374 !AddFrame(QuicFrame(&pending_stop_waiting_frame_));
375 // Return success if we have cleared out this flag (i.e., added the frame).
376 // If we still need to send, then the frame is full, and we have failed.
377 return !should_send_stop_waiting_;
380 LOG_IF(DFATAL, queued_control_frames_.empty())
381 << "AddNextPendingFrame called with no queued control frames.";
382 if (!AddFrame(queued_control_frames_.back())) {
383 // Packet was full.
384 return false;
386 queued_control_frames_.pop_back();
387 return true;
390 bool QuicPacketGenerator::AddFrame(const QuicFrame& frame) {
391 bool success = packet_creator_.AddSavedFrame(frame);
392 if (success && debug_delegate_) {
393 debug_delegate_->OnFrameAddedToPacket(frame);
395 return success;
398 void QuicPacketGenerator::SerializeAndSendPacket() {
399 SerializedPacket serialized_packet = packet_creator_.SerializePacket();
400 DCHECK(serialized_packet.packet);
402 // There may be AckNotifiers interested in this packet.
403 if (FLAGS_quic_attach_ack_notifiers_to_packets) {
404 serialized_packet.notifiers.swap(ack_notifiers_);
405 ack_notifiers_.clear();
408 delegate_->OnSerializedPacket(serialized_packet);
409 MaybeSendFecPacketAndCloseGroup(/*force=*/false);
411 // The packet has now been serialized, so the frames are no longer queued.
412 ack_queued_ = false;
413 stop_waiting_queued_ = false;
416 void QuicPacketGenerator::StopSendingVersion() {
417 packet_creator_.StopSendingVersion();
420 QuicPacketSequenceNumber QuicPacketGenerator::sequence_number() const {
421 return packet_creator_.sequence_number();
424 QuicByteCount QuicPacketGenerator::max_packet_length() const {
425 return packet_creator_.max_packet_length();
428 void QuicPacketGenerator::set_max_packet_length(QuicByteCount length) {
429 packet_creator_.set_max_packet_length(length);
432 QuicEncryptedPacket* QuicPacketGenerator::SerializeVersionNegotiationPacket(
433 const QuicVersionVector& supported_versions) {
434 return packet_creator_.SerializeVersionNegotiationPacket(supported_versions);
437 SerializedPacket QuicPacketGenerator::ReserializeAllFrames(
438 const RetransmittableFrames& frames,
439 QuicSequenceNumberLength original_length) {
440 return packet_creator_.ReserializeAllFrames(frames, original_length);
443 void QuicPacketGenerator::UpdateSequenceNumberLength(
444 QuicPacketSequenceNumber least_packet_awaited_by_peer,
445 QuicPacketCount max_packets_in_flight) {
446 return packet_creator_.UpdateSequenceNumberLength(
447 least_packet_awaited_by_peer, max_packets_in_flight);
450 void QuicPacketGenerator::SetConnectionIdLength(uint32 length) {
451 if (length == 0) {
452 packet_creator_.set_connection_id_length(PACKET_0BYTE_CONNECTION_ID);
453 } else if (length == 1) {
454 packet_creator_.set_connection_id_length(PACKET_1BYTE_CONNECTION_ID);
455 } else if (length <= 4) {
456 packet_creator_.set_connection_id_length(PACKET_4BYTE_CONNECTION_ID);
457 } else {
458 packet_creator_.set_connection_id_length(PACKET_8BYTE_CONNECTION_ID);
463 void QuicPacketGenerator::set_encryption_level(EncryptionLevel level) {
464 packet_creator_.set_encryption_level(level);
467 } // namespace net