MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / net / quic / quic_packet_generator.cc
blob11a45cc984f652da63217c0def07422c98f28c5a
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_fec_group.h"
10 #include "net/quic/quic_utils.h"
12 using base::StringPiece;
14 namespace net {
16 namespace {
18 // We want to put some space between a protected packet and the FEC packet to
19 // avoid losing them both within the same loss episode. On the other hand,
20 // we expect to be able to recover from any loss in about an RTT.
21 // We resolve this tradeoff by sending an FEC packet atmost half an RTT,
22 // or equivalently, half a cwnd, after the first protected packet. Since we
23 // don't want to delay an FEC packet past half an RTT, we set the max FEC
24 // group size to be half the current congestion window.
25 const float kCongestionWindowMultiplierForFecGroupSize = 0.5;
27 } // namespace
29 class QuicAckNotifier;
31 QuicPacketGenerator::QuicPacketGenerator(QuicConnectionId connection_id,
32 QuicFramer* framer,
33 QuicRandom* random_generator,
34 DelegateInterface* delegate)
35 : delegate_(delegate),
36 debug_delegate_(nullptr),
37 packet_creator_(connection_id, framer, random_generator),
38 batch_mode_(false),
39 should_fec_protect_(false),
40 should_send_ack_(false),
41 should_send_feedback_(false),
42 should_send_stop_waiting_(false) {}
44 QuicPacketGenerator::~QuicPacketGenerator() {
45 for (QuicFrames::iterator it = queued_control_frames_.begin();
46 it != queued_control_frames_.end(); ++it) {
47 switch (it->type) {
48 case PADDING_FRAME:
49 delete it->padding_frame;
50 break;
51 case STREAM_FRAME:
52 delete it->stream_frame;
53 break;
54 case ACK_FRAME:
55 delete it->ack_frame;
56 break;
57 case CONGESTION_FEEDBACK_FRAME:
58 delete it->congestion_feedback_frame;
59 break;
60 case RST_STREAM_FRAME:
61 delete it->rst_stream_frame;
62 break;
63 case CONNECTION_CLOSE_FRAME:
64 delete it->connection_close_frame;
65 break;
66 case GOAWAY_FRAME:
67 delete it->goaway_frame;
68 break;
69 case WINDOW_UPDATE_FRAME:
70 delete it->window_update_frame;
71 break;
72 case BLOCKED_FRAME:
73 delete it->blocked_frame;
74 break;
75 case STOP_WAITING_FRAME:
76 delete it->stop_waiting_frame;
77 break;
78 case PING_FRAME:
79 delete it->ping_frame;
80 break;
81 case NUM_FRAME_TYPES:
82 DCHECK(false) << "Cannot delete type: " << it->type;
87 // NetworkChangeVisitor method.
88 void QuicPacketGenerator::OnCongestionWindowChange(
89 QuicByteCount congestion_window) {
90 packet_creator_.set_max_packets_per_fec_group(
91 static_cast<size_t>(kCongestionWindowMultiplierForFecGroupSize *
92 congestion_window / kDefaultTCPMSS));
95 void QuicPacketGenerator::SetShouldSendAck(bool also_send_feedback,
96 bool also_send_stop_waiting) {
97 should_send_ack_ = true;
98 should_send_feedback_ = also_send_feedback;
99 should_send_stop_waiting_ = also_send_stop_waiting;
100 SendQueuedFrames(false);
103 void QuicPacketGenerator::SetShouldSendStopWaiting() {
104 should_send_stop_waiting_ = true;
105 SendQueuedFrames(false);
108 void QuicPacketGenerator::AddControlFrame(const QuicFrame& frame) {
109 queued_control_frames_.push_back(frame);
110 SendQueuedFrames(false);
113 QuicConsumedData QuicPacketGenerator::ConsumeData(QuicStreamId id,
114 const IOVector& data_to_write,
115 QuicStreamOffset offset,
116 bool fin,
117 FecProtection fec_protection,
118 QuicAckNotifier* notifier) {
119 IsHandshake handshake = id == kCryptoStreamId ? IS_HANDSHAKE : NOT_HANDSHAKE;
120 // To make reasoning about crypto frames easier, we don't combine them with
121 // other retransmittable frames in a single packet.
122 const bool flush = handshake == IS_HANDSHAKE &&
123 packet_creator_.HasPendingRetransmittableFrames();
124 SendQueuedFrames(flush);
126 size_t total_bytes_consumed = 0;
127 bool fin_consumed = false;
129 if (!packet_creator_.HasRoomForStreamFrame(id, offset)) {
130 SerializeAndSendPacket();
133 if (fec_protection == MUST_FEC_PROTECT) {
134 MaybeStartFecProtection();
137 IOVector data = data_to_write;
138 size_t data_size = data.TotalBufferSize();
139 while (delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION,
140 HAS_RETRANSMITTABLE_DATA, handshake)) {
141 QuicFrame frame;
142 size_t bytes_consumed;
143 if (notifier != nullptr) {
144 // We want to track which packet this stream frame ends up in.
145 bytes_consumed = packet_creator_.CreateStreamFrameWithNotifier(
146 id, data, offset + total_bytes_consumed, fin, notifier, &frame);
147 } else {
148 bytes_consumed = packet_creator_.CreateStreamFrame(
149 id, data, offset + total_bytes_consumed, fin, &frame);
151 if (!AddFrame(frame)) {
152 LOG(DFATAL) << "Failed to add stream frame.";
153 // Inability to add a STREAM frame creates an unrecoverable hole in a
154 // the stream, so it's best to close the connection.
155 delegate_->CloseConnection(QUIC_INTERNAL_ERROR, false);
156 return QuicConsumedData(0, false);
159 total_bytes_consumed += bytes_consumed;
160 fin_consumed = fin && total_bytes_consumed == data_size;
161 data.Consume(bytes_consumed);
162 DCHECK(data.Empty() || packet_creator_.BytesFree() == 0u);
164 // TODO(ianswett): Restore packet reordering.
165 if (!InBatchMode() || !packet_creator_.HasRoomForStreamFrame(id, offset)) {
166 SerializeAndSendPacket();
169 if (data.Empty()) {
170 // We're done writing the data. Exit the loop.
171 // We don't make this a precondition because we could have 0 bytes of data
172 // if we're simply writing a fin.
173 if (fec_protection == MUST_FEC_PROTECT) {
174 // Turn off FEC protection when we're done writing protected data.
175 DVLOG(1) << "Turning FEC protection OFF";
176 should_fec_protect_ = false;
178 break;
182 // Don't allow the handshake to be bundled with other retransmittable frames.
183 if (handshake == IS_HANDSHAKE) {
184 SendQueuedFrames(true);
187 // Try to close FEC group since we've either run out of data to send or we're
188 // blocked. If not in batch mode, force close the group.
189 // TODO(jri): This method should be called with flush=false here
190 // once the timer-based FEC sending is done, to separate FEC sending from
191 // the end of batch operations.
192 MaybeSendFecPacketAndCloseGroup(!InBatchMode());
194 DCHECK(InBatchMode() || !packet_creator_.HasPendingFrames());
195 return QuicConsumedData(total_bytes_consumed, fin_consumed);
198 bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const {
199 DCHECK(HasPendingFrames());
200 HasRetransmittableData retransmittable =
201 (should_send_ack_ || should_send_feedback_ || should_send_stop_waiting_)
202 ? NO_RETRANSMITTABLE_DATA : HAS_RETRANSMITTABLE_DATA;
203 if (retransmittable == HAS_RETRANSMITTABLE_DATA) {
204 DCHECK(!queued_control_frames_.empty()); // These are retransmittable.
206 return delegate_->ShouldGeneratePacket(NOT_RETRANSMISSION, retransmittable,
207 NOT_HANDSHAKE);
210 void QuicPacketGenerator::SendQueuedFrames(bool flush) {
211 // Only add pending frames if we are SURE we can then send the whole packet.
212 while (HasPendingFrames() &&
213 (flush || CanSendWithNextPendingFrameAddition())) {
214 if (!AddNextPendingFrame()) {
215 // Packet was full, so serialize and send it.
216 SerializeAndSendPacket();
220 if (!InBatchMode() || flush) {
221 if (packet_creator_.HasPendingFrames()) {
222 SerializeAndSendPacket();
224 // Ensure the FEC group is closed at the end of this method unless other
225 // writes are pending.
226 MaybeSendFecPacketAndCloseGroup(true);
230 void QuicPacketGenerator::MaybeStartFecProtection() {
231 if (!packet_creator_.IsFecEnabled()) {
232 return;
234 DVLOG(1) << "Turning FEC protection ON";
235 should_fec_protect_ = true;
236 if (packet_creator_.IsFecProtected()) {
237 // Only start creator's FEC protection if not already on.
238 return;
240 if (HasQueuedFrames()) {
241 // TODO(jri): This currently requires that the generator flush out any
242 // pending frames when FEC protection is turned on. If current packet can be
243 // converted to an FEC protected packet, do it. This will require the
244 // generator to check if the resulting expansion still allows the incoming
245 // frame to be added to the packet.
246 SendQueuedFrames(true);
248 packet_creator_.StartFecProtectingPackets();
249 DCHECK(packet_creator_.IsFecProtected());
252 void QuicPacketGenerator::MaybeSendFecPacketAndCloseGroup(bool force) {
253 if (!packet_creator_.IsFecProtected() ||
254 packet_creator_.HasPendingFrames() ||
255 !packet_creator_.ShouldSendFec(force)) {
256 return;
258 // TODO(jri): SerializeFec can return a NULL packet, and this should
259 // cause an early return, with a call to delegate_->OnPacketGenerationError.
260 SerializedPacket serialized_fec = packet_creator_.SerializeFec();
261 DCHECK(serialized_fec.packet);
262 delegate_->OnSerializedPacket(serialized_fec);
263 // Turn FEC protection off if creator's protection is on and the creator
264 // does not have an open FEC group.
265 // Note: We only wait until the frames queued in the creator are flushed;
266 // pending frames in the generator will not keep us from turning FEC off.
267 if (!should_fec_protect_ && !packet_creator_.IsFecGroupOpen()) {
268 packet_creator_.StopFecProtectingPackets();
269 DCHECK(!packet_creator_.IsFecProtected());
273 bool QuicPacketGenerator::InBatchMode() {
274 return batch_mode_;
277 void QuicPacketGenerator::StartBatchOperations() {
278 batch_mode_ = true;
281 void QuicPacketGenerator::FinishBatchOperations() {
282 batch_mode_ = false;
283 SendQueuedFrames(false);
286 void QuicPacketGenerator::FlushAllQueuedFrames() {
287 SendQueuedFrames(true);
290 bool QuicPacketGenerator::HasQueuedFrames() const {
291 return packet_creator_.HasPendingFrames() || HasPendingFrames();
294 bool QuicPacketGenerator::HasPendingFrames() const {
295 return should_send_ack_ || should_send_feedback_ ||
296 should_send_stop_waiting_ || !queued_control_frames_.empty();
299 bool QuicPacketGenerator::AddNextPendingFrame() {
300 if (should_send_ack_) {
301 pending_ack_frame_.reset(delegate_->CreateAckFrame());
302 // If we can't this add the frame now, then we still need to do so later.
303 should_send_ack_ = !AddFrame(QuicFrame(pending_ack_frame_.get()));
304 // Return success if we have cleared out this flag (i.e., added the frame).
305 // If we still need to send, then the frame is full, and we have failed.
306 return !should_send_ack_;
309 if (should_send_feedback_) {
310 pending_feedback_frame_.reset(delegate_->CreateFeedbackFrame());
311 // If we can't this add the frame now, then we still need to do so later.
312 should_send_feedback_ = !AddFrame(QuicFrame(pending_feedback_frame_.get()));
313 // Return success if we have cleared out this flag (i.e., added the frame).
314 // If we still need to send, then the frame is full, and we have failed.
315 return !should_send_feedback_;
318 if (should_send_stop_waiting_) {
319 pending_stop_waiting_frame_.reset(delegate_->CreateStopWaitingFrame());
320 // If we can't this add the frame now, then we still need to do so later.
321 should_send_stop_waiting_ =
322 !AddFrame(QuicFrame(pending_stop_waiting_frame_.get()));
323 // Return success if we have cleared out this flag (i.e., added the frame).
324 // If we still need to send, then the frame is full, and we have failed.
325 return !should_send_stop_waiting_;
328 LOG_IF(DFATAL, queued_control_frames_.empty())
329 << "AddNextPendingFrame called with no queued control frames.";
330 if (!AddFrame(queued_control_frames_.back())) {
331 // Packet was full.
332 return false;
334 queued_control_frames_.pop_back();
335 return true;
338 bool QuicPacketGenerator::AddFrame(const QuicFrame& frame) {
339 bool success = packet_creator_.AddSavedFrame(frame);
340 if (success && debug_delegate_) {
341 debug_delegate_->OnFrameAddedToPacket(frame);
343 return success;
346 void QuicPacketGenerator::SerializeAndSendPacket() {
347 SerializedPacket serialized_packet = packet_creator_.SerializePacket();
348 DCHECK(serialized_packet.packet);
349 delegate_->OnSerializedPacket(serialized_packet);
350 MaybeSendFecPacketAndCloseGroup(false);
353 void QuicPacketGenerator::StopSendingVersion() {
354 packet_creator_.StopSendingVersion();
357 QuicPacketSequenceNumber QuicPacketGenerator::sequence_number() const {
358 return packet_creator_.sequence_number();
361 size_t QuicPacketGenerator::max_packet_length() const {
362 return packet_creator_.max_packet_length();
365 void QuicPacketGenerator::set_max_packet_length(size_t length) {
366 packet_creator_.set_max_packet_length(length);
369 QuicEncryptedPacket* QuicPacketGenerator::SerializeVersionNegotiationPacket(
370 const QuicVersionVector& supported_versions) {
371 return packet_creator_.SerializeVersionNegotiationPacket(supported_versions);
374 SerializedPacket QuicPacketGenerator::ReserializeAllFrames(
375 const QuicFrames& frames,
376 QuicSequenceNumberLength original_length) {
377 return packet_creator_.ReserializeAllFrames(frames, original_length);
380 void QuicPacketGenerator::UpdateSequenceNumberLength(
381 QuicPacketSequenceNumber least_packet_awaited_by_peer,
382 QuicByteCount congestion_window) {
383 return packet_creator_.UpdateSequenceNumberLength(
384 least_packet_awaited_by_peer, congestion_window);
387 void QuicPacketGenerator::set_encryption_level(EncryptionLevel level) {
388 packet_creator_.set_encryption_level(level);
391 } // namespace net