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