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_creator.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "net/quic/crypto/quic_random.h"
10 #include "net/quic/quic_ack_notifier.h"
11 #include "net/quic/quic_data_writer.h"
12 #include "net/quic/quic_fec_group.h"
13 #include "net/quic/quic_utils.h"
15 using base::StringPiece
;
26 // Default max packets in an FEC group.
27 static const size_t kDefaultMaxPacketsPerFecGroup
= 10;
28 // Lowest max packets in an FEC group.
29 static const size_t kLowestMaxPacketsPerFecGroup
= 2;
33 // A QuicRandom wrapper that gets a bucket of entropy and distributes it
34 // bit-by-bit. Replenishes the bucket as needed. Not thread-safe. Expose this
35 // class if single bit randomness is needed elsewhere.
36 class QuicRandomBoolSource
{
38 // random: Source of entropy. Not owned.
39 explicit QuicRandomBoolSource(QuicRandom
* random
)
44 ~QuicRandomBoolSource() {}
46 // Returns the next random bit from the bucket.
49 bit_bucket_
= random_
->RandUint64();
52 bool result
= ((bit_bucket_
& bit_mask_
) != 0);
60 // Stored random bits.
62 // The next available bit has "1" in the mask. Zero means empty bucket.
65 DISALLOW_COPY_AND_ASSIGN(QuicRandomBoolSource
);
68 QuicPacketCreator::QuicPacketCreator(QuicConnectionId connection_id
,
70 QuicRandom
* random_generator
)
71 : connection_id_(connection_id
),
72 encryption_level_(ENCRYPTION_NONE
),
74 random_bool_source_(new QuicRandomBoolSource(random_generator
)),
76 should_fec_protect_(false),
78 send_version_in_packet_(!framer
->is_server()),
79 max_packet_length_(kDefaultMaxPacketSize
),
80 max_packets_per_fec_group_(kDefaultMaxPacketsPerFecGroup
),
81 connection_id_length_(PACKET_8BYTE_CONNECTION_ID
),
82 next_sequence_number_length_(PACKET_1BYTE_SEQUENCE_NUMBER
),
83 sequence_number_length_(next_sequence_number_length_
),
85 framer_
->set_fec_builder(this);
88 QuicPacketCreator::~QuicPacketCreator() {
91 void QuicPacketCreator::OnBuiltFecProtectedPayload(
92 const QuicPacketHeader
& header
, StringPiece payload
) {
93 if (fec_group_
.get()) {
94 DCHECK_NE(0u, header
.fec_group
);
95 fec_group_
->Update(encryption_level_
, header
, payload
);
99 void QuicPacketCreator::set_max_packets_per_fec_group(
100 size_t max_packets_per_fec_group
) {
101 max_packets_per_fec_group_
= max(kLowestMaxPacketsPerFecGroup
,
102 max_packets_per_fec_group
);
103 DCHECK_LT(0u, max_packets_per_fec_group_
);
106 bool QuicPacketCreator::ShouldSendFec(bool force_close
) const {
107 DCHECK(!HasPendingFrames());
108 return fec_group_
.get() != nullptr && fec_group_
->NumReceivedPackets() > 0 &&
110 fec_group_
->NumReceivedPackets() >= max_packets_per_fec_group_
);
113 bool QuicPacketCreator::IsFecGroupOpen() const {
114 return fec_group_
.get() != nullptr;
117 void QuicPacketCreator::StartFecProtectingPackets() {
118 if (!IsFecEnabled()) {
119 LOG(DFATAL
) << "Cannot start FEC protection when FEC is not enabled.";
122 // TODO(jri): This currently requires that the generator flush out any
123 // pending frames when FEC protection is turned on. If current packet can be
124 // converted to an FEC protected packet, do it. This will require the
125 // generator to check if the resulting expansion still allows the incoming
126 // frame to be added to the packet.
127 if (HasPendingFrames()) {
128 LOG(DFATAL
) << "Cannot start FEC protection with pending frames.";
131 DCHECK(!should_fec_protect_
);
132 should_fec_protect_
= true;
135 void QuicPacketCreator::StopFecProtectingPackets() {
136 if (fec_group_
.get() != nullptr) {
137 LOG(DFATAL
) << "Cannot stop FEC protection with open FEC group.";
140 DCHECK(should_fec_protect_
);
141 should_fec_protect_
= false;
142 fec_group_number_
= 0;
145 bool QuicPacketCreator::IsFecProtected() const {
146 return should_fec_protect_
;
149 bool QuicPacketCreator::IsFecEnabled() const {
150 return max_packets_per_fec_group_
> 0;
153 InFecGroup
QuicPacketCreator::MaybeUpdateLengthsAndStartFec() {
154 if (fec_group_
.get() != nullptr) {
155 // Don't update any lengths when an FEC group is open, to ensure same
156 // packet header size in all packets within a group.
159 if (!queued_frames_
.empty()) {
160 // Don't change creator state if there are frames queued.
161 return fec_group_
.get() == nullptr ? NOT_IN_FEC_GROUP
: IN_FEC_GROUP
;
164 // Update sequence number length only on packet and FEC group boundaries.
165 sequence_number_length_
= next_sequence_number_length_
;
167 if (!should_fec_protect_
) {
168 return NOT_IN_FEC_GROUP
;
170 // Start a new FEC group since protection is on. Set the fec group number to
171 // the sequence number of the next packet.
172 fec_group_number_
= sequence_number() + 1;
173 fec_group_
.reset(new QuicFecGroup());
177 // Stops serializing version of the protocol in packets sent after this call.
178 // A packet that is already open might send kQuicVersionSize bytes less than the
179 // maximum packet size if we stop sending version before it is serialized.
180 void QuicPacketCreator::StopSendingVersion() {
181 DCHECK(send_version_in_packet_
);
182 send_version_in_packet_
= false;
183 if (packet_size_
> 0) {
184 DCHECK_LT(kQuicVersionSize
, packet_size_
);
185 packet_size_
-= kQuicVersionSize
;
189 void QuicPacketCreator::UpdateSequenceNumberLength(
190 QuicPacketSequenceNumber least_packet_awaited_by_peer
,
191 QuicPacketCount max_packets_in_flight
) {
192 DCHECK_LE(least_packet_awaited_by_peer
, sequence_number_
+ 1);
193 // Since the packet creator will not change sequence number length mid FEC
194 // group, include the size of an FEC group to be safe.
195 const QuicPacketSequenceNumber current_delta
=
196 max_packets_per_fec_group_
+ sequence_number_
+ 1
197 - least_packet_awaited_by_peer
;
198 const uint64 delta
= max(current_delta
, max_packets_in_flight
);
199 next_sequence_number_length_
=
200 QuicFramer::GetMinSequenceNumberLength(delta
* 4);
203 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id
,
204 QuicStreamOffset offset
) const {
205 // TODO(jri): This is a simple safe decision for now, but make
206 // is_in_fec_group a parameter. Same as with all public methods in
207 // QuicPacketCreator.
209 QuicFramer::GetMinStreamFrameSize(id
, offset
, true,
210 should_fec_protect_
? IN_FEC_GROUP
:
215 size_t QuicPacketCreator::StreamFramePacketOverhead(
216 QuicConnectionIdLength connection_id_length
,
217 bool include_version
,
218 QuicSequenceNumberLength sequence_number_length
,
219 QuicStreamOffset offset
,
220 InFecGroup is_in_fec_group
) {
221 return GetPacketHeaderSize(connection_id_length
, include_version
,
222 sequence_number_length
, is_in_fec_group
) +
223 // Assumes this is a stream with a single lone packet.
224 QuicFramer::GetMinStreamFrameSize(1u, offset
, true, is_in_fec_group
);
227 size_t QuicPacketCreator::CreateStreamFrame(QuicStreamId id
,
228 const IOVector
& data
,
229 QuicStreamOffset offset
,
232 DCHECK_GT(max_packet_length_
, StreamFramePacketOverhead(
233 connection_id_length_
, kIncludeVersion
,
234 PACKET_6BYTE_SEQUENCE_NUMBER
, offset
, IN_FEC_GROUP
));
236 InFecGroup is_in_fec_group
= MaybeUpdateLengthsAndStartFec();
238 LOG_IF(DFATAL
, !HasRoomForStreamFrame(id
, offset
))
239 << "No room for Stream frame, BytesFree: " << BytesFree()
240 << " MinStreamFrameSize: "
241 << QuicFramer::GetMinStreamFrameSize(id
, offset
, true, is_in_fec_group
);
245 << "Creating a stream frame with no data or fin.";
246 // Create a new packet for the fin, if necessary.
247 *frame
= QuicFrame(new QuicStreamFrame(id
, true, offset
, data
));
251 const size_t data_size
= data
.TotalBufferSize();
252 size_t min_frame_size
= QuicFramer::GetMinStreamFrameSize(
253 id
, offset
, /* last_frame_in_packet= */ true, is_in_fec_group
);
254 size_t bytes_consumed
= min
<size_t>(BytesFree() - min_frame_size
, data_size
);
256 bool set_fin
= fin
&& bytes_consumed
== data_size
; // Last frame.
258 frame_data
.AppendIovecAtMostBytes(data
.iovec(), data
.Size(),
260 DCHECK_EQ(frame_data
.TotalBufferSize(), bytes_consumed
);
261 *frame
= QuicFrame(new QuicStreamFrame(id
, set_fin
, offset
, frame_data
));
262 return bytes_consumed
;
265 SerializedPacket
QuicPacketCreator::ReserializeAllFrames(
266 const RetransmittableFrames
& frames
,
267 QuicSequenceNumberLength original_length
) {
268 DCHECK(fec_group_
.get() == nullptr);
269 const QuicSequenceNumberLength saved_length
= sequence_number_length_
;
270 const QuicSequenceNumberLength saved_next_length
=
271 next_sequence_number_length_
;
272 const bool saved_should_fec_protect
= should_fec_protect_
;
273 const EncryptionLevel default_encryption_level
= encryption_level_
;
275 // Temporarily set the sequence number length, stop FEC protection,
276 // and change the encryption level.
277 sequence_number_length_
= original_length
;
278 next_sequence_number_length_
= original_length
;
279 should_fec_protect_
= false;
280 encryption_level_
= frames
.encryption_level();
282 // Serialize the packet and restore the FEC and sequence number length state.
283 SerializedPacket serialized_packet
= SerializeAllFrames(frames
.frames());
284 sequence_number_length_
= saved_length
;
285 next_sequence_number_length_
= saved_next_length
;
286 should_fec_protect_
= saved_should_fec_protect
;
287 encryption_level_
= default_encryption_level
;
289 return serialized_packet
;
292 // TODO(ianswett): Remove this method, because it's test only.
293 SerializedPacket
QuicPacketCreator::SerializeAllFrames(
294 const QuicFrames
& frames
) {
295 // TODO(satyamshekhar): Verify that this DCHECK won't fail. What about queued
296 // frames from SendStreamData()[send_stream_should_flush_ == false &&
297 // data.empty() == true] and retransmit due to RTO.
298 DCHECK_EQ(0u, queued_frames_
.size());
299 LOG_IF(DFATAL
, frames
.empty())
300 << "Attempt to serialize empty packet";
301 for (const QuicFrame
& frame
: frames
) {
302 bool success
= AddFrame(frame
, false);
305 SerializedPacket packet
= SerializePacket();
306 DCHECK(packet
.retransmittable_frames
== nullptr);
310 bool QuicPacketCreator::HasPendingFrames() const {
311 return !queued_frames_
.empty();
314 bool QuicPacketCreator::HasPendingRetransmittableFrames() const {
315 return queued_retransmittable_frames_
.get() != nullptr &&
316 !queued_retransmittable_frames_
->frames().empty();
319 size_t QuicPacketCreator::ExpansionOnNewFrame() const {
320 // If packet is FEC protected, there's no expansion.
321 if (should_fec_protect_
) {
324 // If the last frame in the packet is a stream frame, then it will expand to
325 // include the stream_length field when a new frame is added.
326 bool has_trailing_stream_frame
=
327 !queued_frames_
.empty() && queued_frames_
.back().type
== STREAM_FRAME
;
328 return has_trailing_stream_frame
? kQuicStreamPayloadLengthSize
: 0;
331 size_t QuicPacketCreator::BytesFree() const {
332 const size_t max_plaintext_size
=
333 framer_
->GetMaxPlaintextSize(max_packet_length_
);
334 DCHECK_GE(max_plaintext_size
, PacketSize());
335 return max_plaintext_size
- min(max_plaintext_size
, PacketSize()
336 + ExpansionOnNewFrame());
339 size_t QuicPacketCreator::PacketSize() const {
340 if (!queued_frames_
.empty()) {
343 if (fec_group_
.get() == nullptr) {
344 // Update sequence number length on packet and FEC boundary.
345 sequence_number_length_
= next_sequence_number_length_
;
347 packet_size_
= GetPacketHeaderSize(
348 connection_id_length_
, send_version_in_packet_
, sequence_number_length_
,
349 should_fec_protect_
? IN_FEC_GROUP
: NOT_IN_FEC_GROUP
);
353 bool QuicPacketCreator::AddSavedFrame(const QuicFrame
& frame
) {
354 return AddFrame(frame
, true);
357 SerializedPacket
QuicPacketCreator::SerializePacket() {
358 LOG_IF(DFATAL
, queued_frames_
.empty())
359 << "Attempt to serialize empty packet";
360 DCHECK_GE(sequence_number_
+ 1, fec_group_number_
);
361 QuicPacketHeader header
;
362 FillPacketHeader(should_fec_protect_
? fec_group_number_
: 0, false, &header
);
366 size_t max_plaintext_size
=
367 framer_
->GetMaxPlaintextSize(max_packet_length_
);
368 DCHECK_GE(max_plaintext_size
, packet_size_
);
369 // ACK Frames will be truncated due to length only if they're the only frame
370 // in the packet, and if packet_size_ was set to max_plaintext_size. If
371 // truncation due to length occurred, then GetSerializedFrameLength will have
372 // returned all bytes free.
373 bool possibly_truncated_by_length
= packet_size_
== max_plaintext_size
&&
374 queued_frames_
.size() == 1 &&
375 queued_frames_
.back().type
== ACK_FRAME
;
376 char buffer
[kMaxPacketSize
];
377 scoped_ptr
<QuicPacket
> packet
;
378 // Use the packet_size_ instead of the buffer size to ensure smaller
379 // packet sizes are properly used.
380 scoped_ptr
<char[]> large_buffer
;
381 if (packet_size_
<= kMaxPacketSize
) {
383 framer_
->BuildDataPacket(header
, queued_frames_
, buffer
, packet_size_
));
385 large_buffer
.reset(new char[packet_size_
]);
386 packet
.reset(framer_
->BuildDataPacket(header
, queued_frames_
,
387 large_buffer
.get(), packet_size_
));
389 LOG_IF(DFATAL
, packet
== nullptr) << "Failed to serialize "
390 << queued_frames_
.size() << " frames.";
391 // Because of possible truncation, we can't be confident that our
392 // packet size calculation worked correctly.
393 if (!possibly_truncated_by_length
) {
394 DCHECK_EQ(packet_size_
, packet
->length());
396 // Immediately encrypt the packet, to ensure we don't encrypt the same packet
397 // sequence number multiple times.
398 QuicEncryptedPacket
* encrypted
=
399 framer_
->EncryptPacket(encryption_level_
, sequence_number_
, *packet
);
400 if (encrypted
== nullptr) {
401 LOG(DFATAL
) << "Failed to encrypt packet number " << sequence_number_
;
402 SerializedPacket
kNoPacket(0, PACKET_1BYTE_SEQUENCE_NUMBER
, nullptr, 0,
408 queued_frames_
.clear();
409 return SerializedPacket(header
.packet_sequence_number
,
410 header
.public_header
.sequence_number_length
,
411 encrypted
, QuicFramer::GetPacketEntropyHash(header
),
412 queued_retransmittable_frames_
.release());
415 SerializedPacket
QuicPacketCreator::SerializeFec() {
416 if (fec_group_
.get() == nullptr || fec_group_
->NumReceivedPackets() <= 0) {
417 LOG(DFATAL
) << "SerializeFEC called but no group or zero packets in group.";
418 // TODO(jri): Make this a public method of framer?
419 SerializedPacket
kNoPacket(0, PACKET_1BYTE_SEQUENCE_NUMBER
, nullptr, 0,
423 DCHECK_EQ(0u, queued_frames_
.size());
424 QuicPacketHeader header
;
425 FillPacketHeader(fec_group_number_
, true, &header
);
426 QuicFecData fec_data
;
427 fec_data
.fec_group
= fec_group_
->min_protected_packet();
428 fec_data
.redundancy
= fec_group_
->payload_parity();
429 scoped_ptr
<QuicPacket
> packet(framer_
->BuildFecPacket(header
, fec_data
));
430 fec_group_
.reset(nullptr);
432 LOG_IF(DFATAL
, packet
== nullptr)
433 << "Failed to serialize fec packet for group:" << fec_data
.fec_group
;
434 DCHECK_GE(max_packet_length_
, packet
->length());
435 // Immediately encrypt the packet, to ensure we don't encrypt the same packet
436 // sequence number multiple times.
437 QuicEncryptedPacket
* encrypted
=
438 framer_
->EncryptPacket(encryption_level_
, sequence_number_
, *packet
);
439 if (encrypted
== nullptr) {
440 LOG(DFATAL
) << "Failed to encrypt packet number " << sequence_number_
;
441 SerializedPacket
kNoPacket(0, PACKET_1BYTE_SEQUENCE_NUMBER
, nullptr, 0,
445 SerializedPacket
serialized(
446 header
.packet_sequence_number
,
447 header
.public_header
.sequence_number_length
, encrypted
,
448 QuicFramer::GetPacketEntropyHash(header
), nullptr);
449 serialized
.is_fec_packet
= true;
453 SerializedPacket
QuicPacketCreator::SerializeConnectionClose(
454 QuicConnectionCloseFrame
* close_frame
) {
456 frames
.push_back(QuicFrame(close_frame
));
457 return SerializeAllFrames(frames
);
460 QuicEncryptedPacket
* QuicPacketCreator::SerializeVersionNegotiationPacket(
461 const QuicVersionVector
& supported_versions
) {
462 DCHECK(framer_
->is_server());
463 QuicPacketPublicHeader header
;
464 header
.connection_id
= connection_id_
;
465 header
.reset_flag
= false;
466 header
.version_flag
= true;
467 header
.versions
= supported_versions
;
468 QuicEncryptedPacket
* encrypted
=
469 framer_
->BuildVersionNegotiationPacket(header
, supported_versions
);
471 DCHECK_GE(max_packet_length_
, encrypted
->length());
475 void QuicPacketCreator::FillPacketHeader(QuicFecGroupNumber fec_group
,
477 QuicPacketHeader
* header
) {
478 header
->public_header
.connection_id
= connection_id_
;
479 header
->public_header
.connection_id_length
= connection_id_length_
;
480 header
->public_header
.reset_flag
= false;
481 header
->public_header
.version_flag
= send_version_in_packet_
;
482 header
->fec_flag
= fec_flag
;
483 header
->packet_sequence_number
= ++sequence_number_
;
484 header
->public_header
.sequence_number_length
= sequence_number_length_
;
485 header
->entropy_flag
= random_bool_source_
->RandBool();
486 header
->is_in_fec_group
= fec_group
== 0 ? NOT_IN_FEC_GROUP
: IN_FEC_GROUP
;
487 header
->fec_group
= fec_group
;
490 bool QuicPacketCreator::ShouldRetransmit(const QuicFrame
& frame
) {
491 switch (frame
.type
) {
494 case STOP_WAITING_FRAME
:
501 bool QuicPacketCreator::AddFrame(const QuicFrame
& frame
,
502 bool save_retransmittable_frames
) {
503 DVLOG(1) << "Adding frame: " << frame
;
504 InFecGroup is_in_fec_group
= MaybeUpdateLengthsAndStartFec();
506 size_t frame_len
= framer_
->GetSerializedFrameLength(
507 frame
, BytesFree(), queued_frames_
.empty(), true, is_in_fec_group
,
508 sequence_number_length_
);
509 if (frame_len
== 0) {
512 DCHECK_LT(0u, packet_size_
);
513 packet_size_
+= ExpansionOnNewFrame() + frame_len
;
515 if (save_retransmittable_frames
&& ShouldRetransmit(frame
)) {
516 if (queued_retransmittable_frames_
.get() == nullptr) {
517 queued_retransmittable_frames_
.reset(new RetransmittableFrames());
519 if (frame
.type
== STREAM_FRAME
) {
520 queued_frames_
.push_back(
521 queued_retransmittable_frames_
->AddStreamFrame(frame
.stream_frame
));
523 queued_frames_
.push_back(
524 queued_retransmittable_frames_
->AddNonStreamFrame(frame
));
527 queued_frames_
.push_back(frame
);
532 void QuicPacketCreator::MaybeAddPadding() {
533 if (BytesFree() == 0) {
534 // Don't pad full packets.
538 // Since ReserializeAllFrames does not populate queued_retransmittable_frames_
539 // it's not sufficient to simply call
540 // queued_retransmittable_frames_->HasCryptoHandshake().
541 // TODO(rch): we should really make ReserializeAllFrames not be a special
544 // If any of the frames in the current packet are on the crypto stream
545 // then they contain handshake messagses, and we should pad them.
546 bool is_handshake
= false;
547 for (const QuicFrame
& frame
: queued_frames_
) {
548 if (frame
.type
== STREAM_FRAME
&&
549 frame
.stream_frame
->stream_id
== kCryptoStreamId
) {
558 QuicPaddingFrame padding
;
559 bool success
= AddFrame(QuicFrame(&padding
), false);