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 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 DCHECK_GE(max_plaintext_size_, packet_size_); | 564 DCHECK_GE(max_plaintext_size_, packet_size_); |
565 // ACK Frames will be truncated due to length only if they're the only frame | 565 // ACK Frames will be truncated due to length only if they're the only frame |
566 // in the packet, and if packet_size_ was set to max_plaintext_size_. If | 566 // in the packet, and if packet_size_ was set to max_plaintext_size_. If |
567 // truncation due to length occurred, then GetSerializedFrameLength will have | 567 // truncation due to length occurred, then GetSerializedFrameLength will have |
568 // returned all bytes free. | 568 // returned all bytes free. |
569 bool possibly_truncated_by_length = packet_size_ == max_plaintext_size_ && | 569 bool possibly_truncated_by_length = packet_size_ == max_plaintext_size_ && |
570 queued_frames_.size() == 1 && | 570 queued_frames_.size() == 1 && |
571 queued_frames_.back().type == ACK_FRAME; | 571 queued_frames_.back().type == ACK_FRAME; |
572 // Use the packet_size_ instead of the buffer size to ensure smaller | 572 // Use the packet_size_ instead of the buffer size to ensure smaller |
573 // packet sizes are properly used. | 573 // packet sizes are properly used. |
574 size_t encrypted_length = 0u; | 574 size_t length = framer_->BuildDataPacket(header, queued_frames_, |
575 if (FLAGS_quic_inplace_encryption) { | 575 encrypted_buffer, packet_size_); |
576 size_t length = framer_->BuildDataPacket(header, queued_frames_, | 576 if (length == 0) { |
577 encrypted_buffer, packet_size_); | 577 QUIC_BUG << "Failed to serialize " << queued_frames_.size() << " frames."; |
578 if (length == 0) { | 578 return NoPacket(); |
579 QUIC_BUG << "Failed to serialize " << queued_frames_.size() << " frames."; | 579 } |
580 return NoPacket(); | |
581 } | |
582 | 580 |
583 // TODO(ianswett) Consider replacing QuicPacket with something else, since | 581 // TODO(ianswett) Consider replacing QuicPacket with something else, since |
584 // it's only used to provide convenience methods to FEC and encryption. | 582 // it's only used to provide convenience methods to FEC and encryption. |
585 QuicPacket packet(encrypted_buffer, length, | 583 QuicPacket packet(encrypted_buffer, length, |
586 /* owns_buffer */ false, | 584 /* owns_buffer */ false, |
587 header.public_header.connection_id_length, | 585 header.public_header.connection_id_length, |
588 header.public_header.version_flag, | 586 header.public_header.version_flag, |
589 header.public_header.packet_number_length); | 587 header.public_header.packet_number_length); |
590 OnBuiltFecProtectedPayload(header, packet.FecProtectedData()); | 588 OnBuiltFecProtectedPayload(header, packet.FecProtectedData()); |
591 | 589 |
592 // Because of possible truncation, we can't be confident that our | 590 // Because of possible truncation, we can't be confident that our |
593 // packet size calculation worked correctly. | 591 // packet size calculation worked correctly. |
594 if (!possibly_truncated_by_length) { | 592 if (!possibly_truncated_by_length) { |
595 DCHECK_EQ(packet_size_, length); | 593 DCHECK_EQ(packet_size_, length); |
596 } | |
597 // Immediately encrypt the packet, to ensure we don't encrypt the same | |
598 // packet number multiple times. | |
599 encrypted_length = | |
600 framer_->EncryptPayload(encryption_level_, packet_number_, packet, | |
601 encrypted_buffer, encrypted_buffer_len); | |
602 } else { | |
603 // The optimized encryption algorithm implementations run faster when | |
604 // operating on aligned memory. | |
605 // TODO(rtenneti): Change the default 64 alignas value (used the default | |
606 // value from CACHELINE_SIZE). | |
607 ALIGNAS(64) char buffer[kMaxPacketSize]; | |
608 size_t length = | |
609 framer_->BuildDataPacket(header, queued_frames_, buffer, packet_size_); | |
610 if (length == 0) { | |
611 QUIC_BUG << "Failed to serialize " << queued_frames_.size() << " frames."; | |
612 return NoPacket(); | |
613 } | |
614 | |
615 // TODO(ianswett) Consider replacing QuicPacket with something else, since | |
616 // it's only used to provide convenience methods to FEC and encryption. | |
617 QuicPacket packet(buffer, length, | |
618 /* owns_buffer */ false, | |
619 header.public_header.connection_id_length, | |
620 header.public_header.version_flag, | |
621 header.public_header.packet_number_length); | |
622 OnBuiltFecProtectedPayload(header, packet.FecProtectedData()); | |
623 | |
624 // Because of possible truncation, we can't be confident that our | |
625 // packet size calculation worked correctly. | |
626 if (!possibly_truncated_by_length) { | |
627 DCHECK_EQ(packet_size_, length); | |
628 } | |
629 // Immediately encrypt the packet, to ensure we don't encrypt the same | |
630 // packet number multiple times. | |
631 encrypted_length = | |
632 framer_->EncryptPayload(encryption_level_, packet_number_, packet, | |
633 encrypted_buffer, encrypted_buffer_len); | |
634 } | 594 } |
| 595 // Immediately encrypt the packet, to ensure we don't encrypt the same |
| 596 // packet number multiple times. |
| 597 size_t encrypted_length = |
| 598 framer_->EncryptPayload(encryption_level_, packet_number_, packet, |
| 599 encrypted_buffer, encrypted_buffer_len); |
635 if (encrypted_length == 0) { | 600 if (encrypted_length == 0) { |
636 QUIC_BUG << "Failed to encrypt packet number " << packet_number_; | 601 QUIC_BUG << "Failed to encrypt packet number " << packet_number_; |
637 return NoPacket(); | 602 return NoPacket(); |
638 } | 603 } |
639 | 604 |
640 // Update |needs_padding_| flag of |queued_retransmittable_frames_| here, and | 605 // Update |needs_padding_| flag of |queued_retransmittable_frames_| here, and |
641 // not in AddFrame, because when the first padded frame is added to the queue, | 606 // not in AddFrame, because when the first padded frame is added to the queue, |
642 // it might not be retransmittable, and hence the flag would end up being not | 607 // it might not be retransmittable, and hence the flag would end up being not |
643 // set. | 608 // set. |
644 if (queued_retransmittable_frames_.get() != nullptr) { | 609 if (queued_retransmittable_frames_.get() != nullptr) { |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
880 hash_map<QuicPathId, QuicPacketNumber>::iterator it = | 845 hash_map<QuicPathId, QuicPacketNumber>::iterator it = |
881 multipath_packet_number_.find(path_id); | 846 multipath_packet_number_.find(path_id); |
882 // If path_id is not in the map, it's a new path. Set packet_number to 0. | 847 // If path_id is not in the map, it's a new path. Set packet_number to 0. |
883 packet_number_ = it == multipath_packet_number_.end() ? 0 : it->second; | 848 packet_number_ = it == multipath_packet_number_.end() ? 0 : it->second; |
884 current_path_ = path_id; | 849 current_path_ = path_id; |
885 // Switching path needs to update packet number length. | 850 // Switching path needs to update packet number length. |
886 UpdatePacketNumberLength(least_packet_awaited_by_peer, max_packets_in_flight); | 851 UpdatePacketNumberLength(least_packet_awaited_by_peer, max_packets_in_flight); |
887 } | 852 } |
888 | 853 |
889 } // namespace net | 854 } // namespace net |
OLD | NEW |