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/quic_packet_creator.h" | 5 #include "net/quic/quic_packet_creator.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 next_packet_number_length_(PACKET_1BYTE_PACKET_NUMBER), | 95 next_packet_number_length_(PACKET_1BYTE_PACKET_NUMBER), |
96 max_packet_length_(0), | 96 max_packet_length_(0), |
97 connection_id_length_(PACKET_8BYTE_CONNECTION_ID), | 97 connection_id_length_(PACKET_8BYTE_CONNECTION_ID), |
98 packet_size_(0), | 98 packet_size_(0), |
99 connection_id_(connection_id), | 99 connection_id_(connection_id), |
100 packet_(kDefaultPathId, | 100 packet_(kDefaultPathId, |
101 0, | 101 0, |
102 next_packet_number_length_, | 102 next_packet_number_length_, |
103 nullptr, | 103 nullptr, |
104 0, | 104 0, |
| 105 0, |
105 false, | 106 false, |
106 false), | 107 false), |
107 should_fec_protect_next_packet_(false), | 108 should_fec_protect_next_packet_(false), |
108 fec_protect_(false), | 109 fec_protect_(false), |
109 max_packets_per_fec_group_(kDefaultMaxPacketsPerFecGroup), | 110 max_packets_per_fec_group_(kDefaultMaxPacketsPerFecGroup), |
110 fec_send_policy_(FEC_ANY_TRIGGER), | 111 fec_send_policy_(FEC_ANY_TRIGGER), |
111 fec_timeout_(QuicTime::Delta::Zero()), | 112 fec_timeout_(QuicTime::Delta::Zero()), |
112 rtt_multiplier_for_fec_timeout_(kRttMultiplierForFecTimeout) { | 113 rtt_multiplier_for_fec_timeout_(kRttMultiplierForFecTimeout) { |
113 SetMaxPacketLength(kDefaultMaxPacketSize); | 114 SetMaxPacketLength(kDefaultMaxPacketSize); |
114 } | 115 } |
115 | 116 |
116 QuicPacketCreator::~QuicPacketCreator() { | 117 QuicPacketCreator::~QuicPacketCreator() { |
117 QuicUtils::DeleteFrames(&packet_.retransmittable_frames); | 118 QuicUtils::DeleteFrames(&packet_.retransmittable_frames); |
118 if (packet_.packet != nullptr) { | |
119 delete packet_.packet; | |
120 } | |
121 } | 119 } |
122 | 120 |
123 void QuicPacketCreator::OnBuiltFecProtectedPayload( | 121 void QuicPacketCreator::OnBuiltFecProtectedPayload( |
124 const QuicPacketHeader& header, | 122 const QuicPacketHeader& header, |
125 StringPiece payload) { | 123 StringPiece payload) { |
126 if (fec_group_.get() != nullptr) { | 124 if (fec_group_.get() != nullptr) { |
127 DCHECK_NE(0u, header.fec_group); | 125 DCHECK_NE(0u, header.fec_group); |
128 fec_group_->Update(packet_.encryption_level, header, payload); | 126 fec_group_->Update(packet_.encryption_level, header, payload); |
129 } | 127 } |
130 } | 128 } |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 } | 467 } |
470 | 468 |
471 // TODO(rtenneti): Change the default 64 alignas value (used the default | 469 // TODO(rtenneti): Change the default 64 alignas value (used the default |
472 // value from CACHELINE_SIZE). | 470 // value from CACHELINE_SIZE). |
473 ALIGNAS(64) char seralized_packet_buffer[kMaxPacketSize]; | 471 ALIGNAS(64) char seralized_packet_buffer[kMaxPacketSize]; |
474 SerializePacket(seralized_packet_buffer, kMaxPacketSize); | 472 SerializePacket(seralized_packet_buffer, kMaxPacketSize); |
475 OnSerializedPacket(); | 473 OnSerializedPacket(); |
476 } | 474 } |
477 | 475 |
478 void QuicPacketCreator::OnSerializedPacket() { | 476 void QuicPacketCreator::OnSerializedPacket() { |
479 if (packet_.packet == nullptr) { | 477 if (packet_.encrypted_buffer == nullptr) { |
480 QUIC_BUG << "Failed to SerializePacket. fec_policy:" << fec_send_policy() | 478 QUIC_BUG << "Failed to SerializePacket. fec_policy:" << fec_send_policy() |
481 << " should_fec_protect_:" << should_fec_protect_next_packet_; | 479 << " should_fec_protect_:" << should_fec_protect_next_packet_; |
482 delegate_->CloseConnection(QUIC_FAILED_TO_SERIALIZE_PACKET, false); | 480 delegate_->CloseConnection(QUIC_FAILED_TO_SERIALIZE_PACKET, false); |
483 return; | 481 return; |
484 } | 482 } |
485 | 483 |
486 delegate_->OnSerializedPacket(&packet_); | 484 delegate_->OnSerializedPacket(&packet_); |
487 ClearPacket(); | 485 ClearPacket(); |
488 MaybeSendFecPacketAndCloseGroup(/*force_send_fec=*/false, | 486 MaybeSendFecPacketAndCloseGroup(/*force_send_fec=*/false, |
489 /*is_fec_timeout=*/false); | 487 /*is_fec_timeout=*/false); |
490 // Maximum packet size may be only enacted while no packet is currently being | 488 // Maximum packet size may be only enacted while no packet is currently being |
491 // constructed, so here we have a good opportunity to actually change it. | 489 // constructed, so here we have a good opportunity to actually change it. |
492 if (CanSetMaxPacketLength()) { | 490 if (CanSetMaxPacketLength()) { |
493 SetMaxPacketLength(max_packet_length_); | 491 SetMaxPacketLength(max_packet_length_); |
494 } | 492 } |
495 } | 493 } |
496 | 494 |
497 void QuicPacketCreator::ClearPacket() { | 495 void QuicPacketCreator::ClearPacket() { |
498 packet_.has_ack = false; | 496 packet_.has_ack = false; |
499 packet_.has_stop_waiting = false; | 497 packet_.has_stop_waiting = false; |
500 packet_.has_crypto_handshake = NOT_HANDSHAKE; | 498 packet_.has_crypto_handshake = NOT_HANDSHAKE; |
501 packet_.needs_padding = false; | 499 packet_.needs_padding = false; |
502 packet_.is_fec_packet = false; | 500 packet_.is_fec_packet = false; |
503 packet_.original_packet_number = 0; | 501 packet_.original_packet_number = 0; |
504 packet_.transmission_type = NOT_RETRANSMISSION; | 502 packet_.transmission_type = NOT_RETRANSMISSION; |
505 packet_.packet = nullptr; | 503 packet_.encrypted_buffer = nullptr; |
| 504 packet_.encrypted_length = 0; |
506 DCHECK(packet_.retransmittable_frames.empty()); | 505 DCHECK(packet_.retransmittable_frames.empty()); |
507 packet_.listeners.clear(); | 506 packet_.listeners.clear(); |
508 } | 507 } |
509 | 508 |
510 bool QuicPacketCreator::HasPendingFrames() const { | 509 bool QuicPacketCreator::HasPendingFrames() const { |
511 return !queued_frames_.empty(); | 510 return !queued_frames_.empty(); |
512 } | 511 } |
513 | 512 |
514 bool QuicPacketCreator::HasPendingRetransmittableFrames() const { | 513 bool QuicPacketCreator::HasPendingRetransmittableFrames() const { |
515 return !packet_.retransmittable_frames.empty(); | 514 return !packet_.retransmittable_frames.empty(); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 packet_.encryption_level, packet_.path_id, packet_.packet_number, packet, | 616 packet_.encryption_level, packet_.path_id, packet_.packet_number, packet, |
618 encrypted_buffer, encrypted_buffer_len); | 617 encrypted_buffer, encrypted_buffer_len); |
619 if (encrypted_length == 0) { | 618 if (encrypted_length == 0) { |
620 QUIC_BUG << "Failed to encrypt packet number " << packet_.packet_number; | 619 QUIC_BUG << "Failed to encrypt packet number " << packet_.packet_number; |
621 return; | 620 return; |
622 } | 621 } |
623 | 622 |
624 packet_size_ = 0; | 623 packet_size_ = 0; |
625 queued_frames_.clear(); | 624 queued_frames_.clear(); |
626 packet_.entropy_hash = QuicFramer::GetPacketEntropyHash(header); | 625 packet_.entropy_hash = QuicFramer::GetPacketEntropyHash(header); |
627 packet_.packet = | 626 packet_.encrypted_buffer = encrypted_buffer; |
628 new QuicEncryptedPacket(encrypted_buffer, encrypted_length, false); | 627 packet_.encrypted_length = encrypted_length; |
629 } | 628 } |
630 | 629 |
631 void QuicPacketCreator::SerializeFec(char* buffer, size_t buffer_len) { | 630 void QuicPacketCreator::SerializeFec(char* buffer, size_t buffer_len) { |
632 DCHECK_LT(0u, buffer_len); | 631 DCHECK_LT(0u, buffer_len); |
633 if (fec_group_.get() == nullptr || fec_group_->NumReceivedPackets() <= 0) { | 632 if (fec_group_.get() == nullptr || fec_group_->NumReceivedPackets() <= 0) { |
634 QUIC_BUG << "SerializeFEC called but no group or zero packets in group."; | 633 QUIC_BUG << "SerializeFEC called but no group or zero packets in group."; |
635 return; | 634 return; |
636 } | 635 } |
637 if (FLAGS_quic_no_unencrypted_fec && | 636 if (FLAGS_quic_no_unencrypted_fec && |
638 packet_.encryption_level == ENCRYPTION_NONE) { | 637 packet_.encryption_level == ENCRYPTION_NONE) { |
(...skipping 14 matching lines...) Expand all Loading... |
653 // Immediately encrypt the packet, to ensure we don't encrypt the same packet | 652 // Immediately encrypt the packet, to ensure we don't encrypt the same packet |
654 // packet number multiple times. | 653 // packet number multiple times. |
655 size_t encrypted_length = framer_->EncryptPayload( | 654 size_t encrypted_length = framer_->EncryptPayload( |
656 packet_.encryption_level, packet_.path_id, packet_.packet_number, *packet, | 655 packet_.encryption_level, packet_.path_id, packet_.packet_number, *packet, |
657 buffer, buffer_len); | 656 buffer, buffer_len); |
658 if (encrypted_length == 0) { | 657 if (encrypted_length == 0) { |
659 QUIC_BUG << "Failed to encrypt packet number " << packet_.packet_number; | 658 QUIC_BUG << "Failed to encrypt packet number " << packet_.packet_number; |
660 return; | 659 return; |
661 } | 660 } |
662 packet_.entropy_hash = QuicFramer::GetPacketEntropyHash(header); | 661 packet_.entropy_hash = QuicFramer::GetPacketEntropyHash(header); |
663 packet_.packet = new QuicEncryptedPacket(buffer, encrypted_length, false); | 662 packet_.encrypted_buffer = buffer; |
| 663 packet_.encrypted_length = encrypted_length; |
664 packet_.is_fec_packet = true; | 664 packet_.is_fec_packet = true; |
665 } | 665 } |
666 | 666 |
667 QuicEncryptedPacket* QuicPacketCreator::SerializeVersionNegotiationPacket( | 667 QuicEncryptedPacket* QuicPacketCreator::SerializeVersionNegotiationPacket( |
668 const QuicVersionVector& supported_versions) { | 668 const QuicVersionVector& supported_versions) { |
669 DCHECK_EQ(Perspective::IS_SERVER, framer_->perspective()); | 669 DCHECK_EQ(Perspective::IS_SERVER, framer_->perspective()); |
670 QuicEncryptedPacket* encrypted = QuicFramer::BuildVersionNegotiationPacket( | 670 QuicEncryptedPacket* encrypted = QuicFramer::BuildVersionNegotiationPacket( |
671 connection_id_, supported_versions); | 671 connection_id_, supported_versions); |
672 DCHECK(encrypted); | 672 DCHECK(encrypted); |
673 DCHECK_GE(max_packet_length_, encrypted->length()); | 673 DCHECK_GE(max_packet_length_, encrypted->length()); |
674 return encrypted; | 674 return encrypted; |
675 } | 675 } |
676 | 676 |
677 // TODO(jri): Make this a public method of framer? | 677 // TODO(jri): Make this a public method of framer? |
678 SerializedPacket QuicPacketCreator::NoPacket() { | 678 SerializedPacket QuicPacketCreator::NoPacket() { |
679 return SerializedPacket(kInvalidPathId, 0, PACKET_1BYTE_PACKET_NUMBER, | 679 return SerializedPacket(kInvalidPathId, 0, PACKET_1BYTE_PACKET_NUMBER, |
680 nullptr, 0, false, false); | 680 nullptr, 0, 0, false, false); |
681 } | 681 } |
682 | 682 |
683 void QuicPacketCreator::FillPacketHeader(QuicFecGroupNumber fec_group, | 683 void QuicPacketCreator::FillPacketHeader(QuicFecGroupNumber fec_group, |
684 bool fec_flag, | 684 bool fec_flag, |
685 QuicPacketHeader* header) { | 685 QuicPacketHeader* header) { |
686 header->public_header.connection_id = connection_id_; | 686 header->public_header.connection_id = connection_id_; |
687 header->public_header.connection_id_length = connection_id_length_; | 687 header->public_header.connection_id_length = connection_id_length_; |
688 header->public_header.multipath_flag = send_path_id_in_packet_; | 688 header->public_header.multipath_flag = send_path_id_in_packet_; |
689 header->public_header.reset_flag = false; | 689 header->public_header.reset_flag = false; |
690 header->public_header.version_flag = send_version_in_packet_; | 690 header->public_header.version_flag = send_version_in_packet_; |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
855 packet_.packet_number = it == multipath_packet_number_.end() ? 0 : it->second; | 855 packet_.packet_number = it == multipath_packet_number_.end() ? 0 : it->second; |
856 packet_.path_id = path_id; | 856 packet_.path_id = path_id; |
857 DCHECK(packet_.path_id != kInvalidPathId); | 857 DCHECK(packet_.path_id != kInvalidPathId); |
858 // Send path in packet if current path is not the default path. | 858 // Send path in packet if current path is not the default path. |
859 send_path_id_in_packet_ = packet_.path_id != kDefaultPathId ? true : false; | 859 send_path_id_in_packet_ = packet_.path_id != kDefaultPathId ? true : false; |
860 // Switching path needs to update packet number length. | 860 // Switching path needs to update packet number length. |
861 UpdatePacketNumberLength(least_packet_awaited_by_peer, max_packets_in_flight); | 861 UpdatePacketNumberLength(least_packet_awaited_by_peer, max_packets_in_flight); |
862 } | 862 } |
863 | 863 |
864 } // namespace net | 864 } // namespace net |
OLD | NEW |