OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/core/quic_packet_creator.h" | 5 #include "net/quic/core/quic_packet_creator.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstdint> | 8 #include <cstdint> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 buffer_allocator_(buffer_allocator), | 37 buffer_allocator_(buffer_allocator), |
38 send_version_in_packet_(framer->perspective() == Perspective::IS_CLIENT), | 38 send_version_in_packet_(framer->perspective() == Perspective::IS_CLIENT), |
39 next_packet_number_length_(PACKET_1BYTE_PACKET_NUMBER), | 39 next_packet_number_length_(PACKET_1BYTE_PACKET_NUMBER), |
40 have_diversification_nonce_(false), | 40 have_diversification_nonce_(false), |
41 max_packet_length_(0), | 41 max_packet_length_(0), |
42 connection_id_length_(PACKET_8BYTE_CONNECTION_ID), | 42 connection_id_length_(PACKET_8BYTE_CONNECTION_ID), |
43 packet_size_(0), | 43 packet_size_(0), |
44 connection_id_(connection_id), | 44 connection_id_(connection_id), |
45 packet_(0, PACKET_1BYTE_PACKET_NUMBER, nullptr, 0, false, false), | 45 packet_(0, PACKET_1BYTE_PACKET_NUMBER, nullptr, 0, false, false), |
46 latched_flag_no_stop_waiting_frames_( | 46 latched_flag_no_stop_waiting_frames_( |
47 FLAGS_quic_reloadable_flag_quic_no_stop_waiting_frames) { | 47 FLAGS_quic_reloadable_flag_quic_no_stop_waiting_frames), |
| 48 pending_padding_bytes_(0), |
| 49 needs_full_padding_(false) { |
48 SetMaxPacketLength(kDefaultMaxPacketSize); | 50 SetMaxPacketLength(kDefaultMaxPacketSize); |
49 } | 51 } |
50 | 52 |
51 QuicPacketCreator::~QuicPacketCreator() { | 53 QuicPacketCreator::~QuicPacketCreator() { |
52 DeleteFrames(&packet_.retransmittable_frames); | 54 DeleteFrames(&packet_.retransmittable_frames); |
53 } | 55 } |
54 | 56 |
55 void QuicPacketCreator::SetEncrypter(EncryptionLevel level, | 57 void QuicPacketCreator::SetEncrypter(EncryptionLevel level, |
56 QuicEncrypter* encrypter) { | 58 QuicEncrypter* encrypter) { |
57 framer_->SetEncrypter(level, encrypter); | 59 framer_->SetEncrypter(level, encrypter); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 delete frame->stream_frame; | 145 delete frame->stream_frame; |
144 return false; | 146 return false; |
145 } | 147 } |
146 } | 148 } |
147 if (!AddFrame(*frame, /*save_retransmittable_frames=*/true)) { | 149 if (!AddFrame(*frame, /*save_retransmittable_frames=*/true)) { |
148 // Fails if we try to write unencrypted stream data. | 150 // Fails if we try to write unencrypted stream data. |
149 delete frame->stream_frame; | 151 delete frame->stream_frame; |
150 return false; | 152 return false; |
151 } | 153 } |
152 if (needs_full_padding) { | 154 if (needs_full_padding) { |
153 packet_.num_padding_bytes = -1; | 155 needs_full_padding_ = true; |
154 } | 156 } |
| 157 |
155 return true; | 158 return true; |
156 } | 159 } |
157 | 160 |
158 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, | 161 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, |
159 QuicStreamOffset offset) { | 162 QuicStreamOffset offset) { |
160 return BytesFree() > QuicFramer::GetMinStreamFrameSize(id, offset, true); | 163 return BytesFree() > QuicFramer::GetMinStreamFrameSize(id, offset, true); |
161 } | 164 } |
162 | 165 |
163 // static | 166 // static |
164 size_t QuicPacketCreator::StreamFramePacketOverhead( | 167 size_t QuicPacketCreator::StreamFramePacketOverhead( |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 char* buffer, | 275 char* buffer, |
273 size_t buffer_len) { | 276 size_t buffer_len) { |
274 DCHECK(queued_frames_.empty()); | 277 DCHECK(queued_frames_.empty()); |
275 DCHECK_EQ(0, packet_.num_padding_bytes); | 278 DCHECK_EQ(0, packet_.num_padding_bytes); |
276 QUIC_BUG_IF(retransmission.retransmittable_frames.empty()) | 279 QUIC_BUG_IF(retransmission.retransmittable_frames.empty()) |
277 << "Attempt to serialize empty packet"; | 280 << "Attempt to serialize empty packet"; |
278 const EncryptionLevel default_encryption_level = packet_.encryption_level; | 281 const EncryptionLevel default_encryption_level = packet_.encryption_level; |
279 | 282 |
280 // Temporarily set the packet number length and change the encryption level. | 283 // Temporarily set the packet number length and change the encryption level. |
281 packet_.packet_number_length = retransmission.packet_number_length; | 284 packet_.packet_number_length = retransmission.packet_number_length; |
282 packet_.num_padding_bytes = retransmission.num_padding_bytes; | 285 if (retransmission.num_padding_bytes == -1) { |
| 286 // Only retransmit padding when original packet needs full padding. Padding |
| 287 // from pending_padding_bytes_ are not retransmitted. |
| 288 needs_full_padding_ = true; |
| 289 } |
283 // Only preserve the original encryption level if it's a handshake packet or | 290 // Only preserve the original encryption level if it's a handshake packet or |
284 // if we haven't gone forward secure. | 291 // if we haven't gone forward secure. |
285 if (retransmission.has_crypto_handshake || | 292 if (retransmission.has_crypto_handshake || |
286 packet_.encryption_level != ENCRYPTION_FORWARD_SECURE) { | 293 packet_.encryption_level != ENCRYPTION_FORWARD_SECURE) { |
287 packet_.encryption_level = retransmission.encryption_level; | 294 packet_.encryption_level = retransmission.encryption_level; |
288 } | 295 } |
289 | 296 |
290 // Serialize the packet and restore packet number length state. | 297 // Serialize the packet and restore packet number length state. |
291 for (const QuicFrame& frame : retransmission.retransmittable_frames) { | 298 for (const QuicFrame& frame : retransmission.retransmittable_frames) { |
292 bool success = AddFrame(frame, false); | 299 bool success = AddFrame(frame, false); |
293 QUIC_BUG_IF(!success) << " Failed to add frame of type:" << frame.type | 300 QUIC_BUG_IF(!success) << " Failed to add frame of type:" << frame.type |
294 << " num_frames:" | 301 << " num_frames:" |
295 << retransmission.retransmittable_frames.size() | 302 << retransmission.retransmittable_frames.size() |
296 << " retransmission.packet_number_length:" | 303 << " retransmission.packet_number_length:" |
297 << retransmission.packet_number_length | 304 << retransmission.packet_number_length |
298 << " packet_.packet_number_length:" | 305 << " packet_.packet_number_length:" |
299 << packet_.packet_number_length; | 306 << packet_.packet_number_length; |
300 } | 307 } |
301 SerializePacket(buffer, buffer_len); | 308 SerializePacket(buffer, buffer_len); |
302 packet_.original_packet_number = retransmission.packet_number; | 309 packet_.original_packet_number = retransmission.packet_number; |
303 packet_.transmission_type = retransmission.transmission_type; | 310 packet_.transmission_type = retransmission.transmission_type; |
304 OnSerializedPacket(); | 311 OnSerializedPacket(); |
305 // Restore old values. | 312 // Restore old values. |
306 packet_.encryption_level = default_encryption_level; | 313 packet_.encryption_level = default_encryption_level; |
307 } | 314 } |
308 | 315 |
309 void QuicPacketCreator::Flush() { | 316 void QuicPacketCreator::Flush() { |
310 if (!HasPendingFrames()) { | 317 if (!HasPendingFrames() && pending_padding_bytes_ == 0) { |
311 return; | 318 return; |
312 } | 319 } |
313 | 320 |
314 QUIC_CACHELINE_ALIGNED char seralized_packet_buffer[kMaxPacketSize]; | 321 QUIC_CACHELINE_ALIGNED char seralized_packet_buffer[kMaxPacketSize]; |
315 SerializePacket(seralized_packet_buffer, kMaxPacketSize); | 322 SerializePacket(seralized_packet_buffer, kMaxPacketSize); |
316 OnSerializedPacket(); | 323 OnSerializedPacket(); |
317 } | 324 } |
318 | 325 |
319 void QuicPacketCreator::OnSerializedPacket() { | 326 void QuicPacketCreator::OnSerializedPacket() { |
320 if (packet_.encrypted_buffer == nullptr) { | 327 if (packet_.encrypted_buffer == nullptr) { |
(...skipping 14 matching lines...) Expand all Loading... |
335 packet_.has_stop_waiting = false; | 342 packet_.has_stop_waiting = false; |
336 packet_.has_crypto_handshake = NOT_HANDSHAKE; | 343 packet_.has_crypto_handshake = NOT_HANDSHAKE; |
337 packet_.num_padding_bytes = 0; | 344 packet_.num_padding_bytes = 0; |
338 packet_.original_packet_number = 0; | 345 packet_.original_packet_number = 0; |
339 packet_.transmission_type = NOT_RETRANSMISSION; | 346 packet_.transmission_type = NOT_RETRANSMISSION; |
340 packet_.encrypted_buffer = nullptr; | 347 packet_.encrypted_buffer = nullptr; |
341 packet_.encrypted_length = 0; | 348 packet_.encrypted_length = 0; |
342 DCHECK(packet_.retransmittable_frames.empty()); | 349 DCHECK(packet_.retransmittable_frames.empty()); |
343 packet_.listeners.clear(); | 350 packet_.listeners.clear(); |
344 packet_.largest_acked = 0; | 351 packet_.largest_acked = 0; |
| 352 needs_full_padding_ = false; |
345 } | 353 } |
346 | 354 |
347 void QuicPacketCreator::CreateAndSerializeStreamFrame( | 355 void QuicPacketCreator::CreateAndSerializeStreamFrame( |
348 QuicStreamId id, | 356 QuicStreamId id, |
349 const QuicIOVector& iov, | 357 const QuicIOVector& iov, |
350 QuicStreamOffset iov_offset, | 358 QuicStreamOffset iov_offset, |
351 QuicStreamOffset stream_offset, | 359 QuicStreamOffset stream_offset, |
352 bool fin, | 360 bool fin, |
353 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener, | 361 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener, |
354 size_t* num_bytes_consumed) { | 362 size_t* num_bytes_consumed) { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 IncludeNonceInPublicHeader(), packet_.packet_number_length); | 456 IncludeNonceInPublicHeader(), packet_.packet_number_length); |
449 return packet_size_; | 457 return packet_size_; |
450 } | 458 } |
451 | 459 |
452 bool QuicPacketCreator::AddSavedFrame(const QuicFrame& frame) { | 460 bool QuicPacketCreator::AddSavedFrame(const QuicFrame& frame) { |
453 return AddFrame(frame, /*save_retransmittable_frames=*/true); | 461 return AddFrame(frame, /*save_retransmittable_frames=*/true); |
454 } | 462 } |
455 | 463 |
456 bool QuicPacketCreator::AddPaddedSavedFrame(const QuicFrame& frame) { | 464 bool QuicPacketCreator::AddPaddedSavedFrame(const QuicFrame& frame) { |
457 if (AddFrame(frame, /*save_retransmittable_frames=*/true)) { | 465 if (AddFrame(frame, /*save_retransmittable_frames=*/true)) { |
458 packet_.num_padding_bytes = -1; | 466 needs_full_padding_ = true; |
459 return true; | 467 return true; |
460 } | 468 } |
461 return false; | 469 return false; |
462 } | 470 } |
463 | 471 |
464 void QuicPacketCreator::AddAckListener( | 472 void QuicPacketCreator::AddAckListener( |
465 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener, | 473 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener, |
466 QuicPacketLength length) { | 474 QuicPacketLength length) { |
467 DCHECK(!queued_frames_.empty()); | 475 DCHECK(!queued_frames_.empty()); |
468 packet_.listeners.emplace_back(std::move(ack_listener), length); | 476 packet_.listeners.emplace_back(std::move(ack_listener), length); |
469 } | 477 } |
470 | 478 |
471 void QuicPacketCreator::SerializePacket(char* encrypted_buffer, | 479 void QuicPacketCreator::SerializePacket(char* encrypted_buffer, |
472 size_t encrypted_buffer_len) { | 480 size_t encrypted_buffer_len) { |
473 DCHECK_LT(0u, encrypted_buffer_len); | 481 DCHECK_LT(0u, encrypted_buffer_len); |
474 QUIC_BUG_IF(queued_frames_.empty()) << "Attempt to serialize empty packet"; | 482 QUIC_BUG_IF(queued_frames_.empty() && pending_padding_bytes_ == 0) |
| 483 << "Attempt to serialize empty packet"; |
475 QuicPacketHeader header; | 484 QuicPacketHeader header; |
476 // FillPacketHeader increments packet_number_. | 485 // FillPacketHeader increments packet_number_. |
477 FillPacketHeader(&header); | 486 FillPacketHeader(&header); |
478 | 487 |
479 MaybeAddPadding(); | 488 MaybeAddPadding(); |
480 | 489 |
481 DCHECK_GE(max_plaintext_size_, packet_size_); | 490 DCHECK_GE(max_plaintext_size_, packet_size_); |
482 // Use the packet_size_ instead of the buffer size to ensure smaller | 491 // Use the packet_size_ instead of the buffer size to ensure smaller |
483 // packet sizes are properly used. | 492 // packet sizes are properly used. |
484 size_t length = framer_->BuildDataPacket(header, queued_frames_, | 493 size_t length = framer_->BuildDataPacket(header, queued_frames_, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 packet_.has_stop_waiting = true; | 618 packet_.has_stop_waiting = true; |
610 } | 619 } |
611 if (debug_delegate_ != nullptr) { | 620 if (debug_delegate_ != nullptr) { |
612 debug_delegate_->OnFrameAddedToPacket(frame); | 621 debug_delegate_->OnFrameAddedToPacket(frame); |
613 } | 622 } |
614 | 623 |
615 return true; | 624 return true; |
616 } | 625 } |
617 | 626 |
618 void QuicPacketCreator::MaybeAddPadding() { | 627 void QuicPacketCreator::MaybeAddPadding() { |
619 if (packet_.num_padding_bytes == 0) { | 628 // The current packet should have no padding bytes because padding is only |
620 return; | 629 // added when this method is called just before the packet is serialized. |
621 } | 630 DCHECK_EQ(0, packet_.num_padding_bytes); |
622 | |
623 if (BytesFree() == 0) { | 631 if (BytesFree() == 0) { |
624 // Don't pad full packets. | 632 // Don't pad full packets. |
625 return; | 633 return; |
626 } | 634 } |
627 | 635 |
| 636 if (!needs_full_padding_ && pending_padding_bytes_ == 0) { |
| 637 // Do not need padding. |
| 638 return; |
| 639 } |
| 640 |
| 641 if (needs_full_padding_) { |
| 642 // Full padding does not consume pending padding bytes. |
| 643 packet_.num_padding_bytes = -1; |
| 644 } else { |
| 645 packet_.num_padding_bytes = |
| 646 std::min<int16_t>(pending_padding_bytes_, BytesFree()); |
| 647 pending_padding_bytes_ -= packet_.num_padding_bytes; |
| 648 } |
| 649 |
628 bool success = | 650 bool success = |
629 AddFrame(QuicFrame(QuicPaddingFrame(packet_.num_padding_bytes)), false); | 651 AddFrame(QuicFrame(QuicPaddingFrame(packet_.num_padding_bytes)), false); |
630 DCHECK(success); | 652 DCHECK(success); |
631 } | 653 } |
632 | 654 |
633 bool QuicPacketCreator::IncludeNonceInPublicHeader() { | 655 bool QuicPacketCreator::IncludeNonceInPublicHeader() { |
634 return have_diversification_nonce_ && | 656 return have_diversification_nonce_ && |
635 packet_.encryption_level == ENCRYPTION_INITIAL; | 657 packet_.encryption_level == ENCRYPTION_INITIAL; |
636 } | 658 } |
637 | 659 |
| 660 void QuicPacketCreator::AddPendingPadding(QuicByteCount size) { |
| 661 pending_padding_bytes_ += size; |
| 662 } |
| 663 |
638 } // namespace net | 664 } // namespace net |
OLD | NEW |