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 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // change. | 76 // change. |
77 if (length == max_packet_length_) { | 77 if (length == max_packet_length_) { |
78 return; | 78 return; |
79 } | 79 } |
80 | 80 |
81 max_packet_length_ = length; | 81 max_packet_length_ = length; |
82 max_plaintext_size_ = framer_->GetMaxPlaintextSize(max_packet_length_); | 82 max_plaintext_size_ = framer_->GetMaxPlaintextSize(max_packet_length_); |
83 } | 83 } |
84 | 84 |
85 void QuicPacketCreator::MaybeUpdatePacketNumberLength() { | 85 void QuicPacketCreator::MaybeUpdatePacketNumberLength() { |
86 DCHECK(!FLAGS_quic_simple_packet_number_length); | 86 DCHECK(!FLAGS_quic_simple_packet_number_length_2); |
87 if (!queued_frames_.empty()) { | 87 if (!queued_frames_.empty()) { |
88 // Don't change creator state if there are frames queued. | 88 // Don't change creator state if there are frames queued. |
89 return; | 89 return; |
90 } | 90 } |
91 | 91 |
92 // Update packet number length only on packet boundary. | 92 // Update packet number length only on packet boundary. |
93 packet_.packet_number_length = next_packet_number_length_; | 93 packet_.packet_number_length = next_packet_number_length_; |
94 } | 94 } |
95 | 95 |
96 // Stops serializing version of the protocol in packets sent after this call. | 96 // Stops serializing version of the protocol in packets sent after this call. |
(...skipping 11 matching lines...) Expand all Loading... |
108 void QuicPacketCreator::SetDiversificationNonce( | 108 void QuicPacketCreator::SetDiversificationNonce( |
109 const DiversificationNonce nonce) { | 109 const DiversificationNonce nonce) { |
110 DCHECK(!have_diversification_nonce_); | 110 DCHECK(!have_diversification_nonce_); |
111 have_diversification_nonce_ = true; | 111 have_diversification_nonce_ = true; |
112 memcpy(&diversification_nonce_, nonce, sizeof(diversification_nonce_)); | 112 memcpy(&diversification_nonce_, nonce, sizeof(diversification_nonce_)); |
113 } | 113 } |
114 | 114 |
115 void QuicPacketCreator::UpdatePacketNumberLength( | 115 void QuicPacketCreator::UpdatePacketNumberLength( |
116 QuicPacketNumber least_packet_awaited_by_peer, | 116 QuicPacketNumber least_packet_awaited_by_peer, |
117 QuicPacketCount max_packets_in_flight) { | 117 QuicPacketCount max_packets_in_flight) { |
118 if (FLAGS_quic_simple_packet_number_length && !queued_frames_.empty()) { | 118 if (FLAGS_quic_simple_packet_number_length_2 && !queued_frames_.empty()) { |
119 // Don't change creator state if there are frames queued. | 119 // Don't change creator state if there are frames queued. |
120 QUIC_BUG << "Called UpdatePacketNumberLength with " << queued_frames_.size() | 120 QUIC_BUG << "Called UpdatePacketNumberLength with " << queued_frames_.size() |
121 << " queued_frames. First frame type:" | 121 << " queued_frames. First frame type:" |
122 << queued_frames_.front().type | 122 << queued_frames_.front().type |
123 << " last frame type:" << queued_frames_.back().type; | 123 << " last frame type:" << queued_frames_.back().type; |
124 return; | 124 return; |
125 } | 125 } |
126 | 126 |
127 DCHECK_LE(least_packet_awaited_by_peer, packet_.packet_number + 1); | 127 DCHECK_LE(least_packet_awaited_by_peer, packet_.packet_number + 1); |
128 const QuicPacketNumber current_delta = | 128 const QuicPacketNumber current_delta = |
129 packet_.packet_number + 1 - least_packet_awaited_by_peer; | 129 packet_.packet_number + 1 - least_packet_awaited_by_peer; |
130 const uint64_t delta = max(current_delta, max_packets_in_flight); | 130 const uint64_t delta = max(current_delta, max_packets_in_flight); |
131 if (FLAGS_quic_simple_packet_number_length) { | 131 if (FLAGS_quic_simple_packet_number_length_2) { |
132 packet_.packet_number_length = | 132 packet_.packet_number_length = |
133 QuicFramer::GetMinSequenceNumberLength(delta * 4); | 133 QuicFramer::GetMinSequenceNumberLength(delta * 4); |
134 } else { | 134 } else { |
135 next_packet_number_length_ = | 135 next_packet_number_length_ = |
136 QuicFramer::GetMinSequenceNumberLength(delta * 4); | 136 QuicFramer::GetMinSequenceNumberLength(delta * 4); |
137 } | 137 } |
138 } | 138 } |
139 | 139 |
140 bool QuicPacketCreator::ConsumeData(QuicStreamId id, | 140 bool QuicPacketCreator::ConsumeData(QuicStreamId id, |
141 QuicIOVector iov, | 141 QuicIOVector iov, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 size_t iov_offset, | 202 size_t iov_offset, |
203 QuicStreamOffset offset, | 203 QuicStreamOffset offset, |
204 bool fin, | 204 bool fin, |
205 QuicFrame* frame) { | 205 QuicFrame* frame) { |
206 DCHECK_GT(max_packet_length_, | 206 DCHECK_GT(max_packet_length_, |
207 StreamFramePacketOverhead(framer_->version(), connection_id_length_, | 207 StreamFramePacketOverhead(framer_->version(), connection_id_length_, |
208 kIncludeVersion, kIncludePathId, | 208 kIncludeVersion, kIncludePathId, |
209 IncludeNonceInPublicHeader(), | 209 IncludeNonceInPublicHeader(), |
210 PACKET_6BYTE_PACKET_NUMBER, offset)); | 210 PACKET_6BYTE_PACKET_NUMBER, offset)); |
211 | 211 |
212 if (!FLAGS_quic_simple_packet_number_length) { | 212 if (!FLAGS_quic_simple_packet_number_length_2) { |
213 MaybeUpdatePacketNumberLength(); | 213 MaybeUpdatePacketNumberLength(); |
214 } | 214 } |
215 QUIC_BUG_IF(!HasRoomForStreamFrame(id, offset)) | 215 QUIC_BUG_IF(!HasRoomForStreamFrame(id, offset)) |
216 << "No room for Stream frame, BytesFree: " << BytesFree() | 216 << "No room for Stream frame, BytesFree: " << BytesFree() |
217 << " MinStreamFrameSize: " | 217 << " MinStreamFrameSize: " |
218 << QuicFramer::GetMinStreamFrameSize(id, offset, true); | 218 << QuicFramer::GetMinStreamFrameSize(id, offset, true); |
219 | 219 |
220 if (iov_offset == iov.total_length) { | 220 if (iov_offset == iov.total_length) { |
221 QUIC_BUG_IF(!fin) << "Creating a stream frame with no data or fin."; | 221 QUIC_BUG_IF(!fin) << "Creating a stream frame with no data or fin."; |
222 // Create a new packet for the fin, if necessary. | 222 // Create a new packet for the fin, if necessary. |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 DCHECK(queued_frames_.empty()); | 297 DCHECK(queued_frames_.empty()); |
298 DCHECK_EQ(0, packet_.num_padding_bytes); | 298 DCHECK_EQ(0, packet_.num_padding_bytes); |
299 QUIC_BUG_IF(retransmission.retransmittable_frames.empty()) | 299 QUIC_BUG_IF(retransmission.retransmittable_frames.empty()) |
300 << "Attempt to serialize empty packet"; | 300 << "Attempt to serialize empty packet"; |
301 const QuicPacketNumberLength saved_length = packet_.packet_number_length; | 301 const QuicPacketNumberLength saved_length = packet_.packet_number_length; |
302 const QuicPacketNumberLength saved_next_length = next_packet_number_length_; | 302 const QuicPacketNumberLength saved_next_length = next_packet_number_length_; |
303 const EncryptionLevel default_encryption_level = packet_.encryption_level; | 303 const EncryptionLevel default_encryption_level = packet_.encryption_level; |
304 | 304 |
305 // Temporarily set the packet number length and change the encryption level. | 305 // Temporarily set the packet number length and change the encryption level. |
306 packet_.packet_number_length = retransmission.packet_number_length; | 306 packet_.packet_number_length = retransmission.packet_number_length; |
307 if (!FLAGS_quic_simple_packet_number_length) { | 307 if (!FLAGS_quic_simple_packet_number_length_2) { |
308 next_packet_number_length_ = retransmission.packet_number_length; | 308 next_packet_number_length_ = retransmission.packet_number_length; |
309 } | 309 } |
310 packet_.num_padding_bytes = retransmission.num_padding_bytes; | 310 packet_.num_padding_bytes = retransmission.num_padding_bytes; |
311 // Only preserve the original encryption level if it's a handshake packet or | 311 // Only preserve the original encryption level if it's a handshake packet or |
312 // if we haven't gone forward secure. | 312 // if we haven't gone forward secure. |
313 if (retransmission.has_crypto_handshake || | 313 if (retransmission.has_crypto_handshake || |
314 packet_.encryption_level != ENCRYPTION_FORWARD_SECURE) { | 314 packet_.encryption_level != ENCRYPTION_FORWARD_SECURE) { |
315 packet_.encryption_level = retransmission.encryption_level; | 315 packet_.encryption_level = retransmission.encryption_level; |
316 } | 316 } |
317 | 317 |
318 // Serialize the packet and restore packet number length state. | 318 // Serialize the packet and restore packet number length state. |
319 for (const QuicFrame& frame : retransmission.retransmittable_frames) { | 319 for (const QuicFrame& frame : retransmission.retransmittable_frames) { |
320 bool success = AddFrame(frame, false); | 320 bool success = AddFrame(frame, false); |
321 QUIC_BUG_IF(!success) << " Failed to add frame of type:" << frame.type | 321 QUIC_BUG_IF(!success) << " Failed to add frame of type:" << frame.type |
322 << " num_frames:" | 322 << " num_frames:" |
323 << retransmission.retransmittable_frames.size() | 323 << retransmission.retransmittable_frames.size() |
324 << " retransmission.packet_number_length:" | 324 << " retransmission.packet_number_length:" |
325 << retransmission.packet_number_length | 325 << retransmission.packet_number_length |
326 << " packet_.packet_number_length:" | 326 << " packet_.packet_number_length:" |
327 << packet_.packet_number_length; | 327 << packet_.packet_number_length; |
328 } | 328 } |
329 SerializePacket(buffer, buffer_len); | 329 SerializePacket(buffer, buffer_len); |
330 packet_.original_path_id = retransmission.path_id; | 330 packet_.original_path_id = retransmission.path_id; |
331 packet_.original_packet_number = retransmission.packet_number; | 331 packet_.original_packet_number = retransmission.packet_number; |
332 packet_.transmission_type = retransmission.transmission_type; | 332 packet_.transmission_type = retransmission.transmission_type; |
333 OnSerializedPacket(); | 333 OnSerializedPacket(); |
334 // Restore old values. | 334 // Restore old values. |
335 if (!FLAGS_quic_simple_packet_number_length) { | 335 if (!FLAGS_quic_simple_packet_number_length_2) { |
336 // OnSerializedPacket updates the packet_number_length, so it's incorrect to | 336 // OnSerializedPacket updates the packet_number_length, so it's incorrect to |
337 // restore it here. | 337 // restore it here. |
338 packet_.packet_number_length = saved_length; | 338 packet_.packet_number_length = saved_length; |
339 next_packet_number_length_ = saved_next_length; | 339 next_packet_number_length_ = saved_next_length; |
340 } | 340 } |
341 packet_.encryption_level = default_encryption_level; | 341 packet_.encryption_level = default_encryption_level; |
342 } | 342 } |
343 | 343 |
344 void QuicPacketCreator::Flush() { | 344 void QuicPacketCreator::Flush() { |
345 if (!HasPendingFrames()) { | 345 if (!HasPendingFrames()) { |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 DCHECK_GE(max_plaintext_size_, PacketSize()); | 480 DCHECK_GE(max_plaintext_size_, PacketSize()); |
481 return max_plaintext_size_ - | 481 return max_plaintext_size_ - |
482 min(max_plaintext_size_, PacketSize() + ExpansionOnNewFrame()); | 482 min(max_plaintext_size_, PacketSize() + ExpansionOnNewFrame()); |
483 } | 483 } |
484 | 484 |
485 size_t QuicPacketCreator::PacketSize() { | 485 size_t QuicPacketCreator::PacketSize() { |
486 if (!queued_frames_.empty()) { | 486 if (!queued_frames_.empty()) { |
487 return packet_size_; | 487 return packet_size_; |
488 } | 488 } |
489 // Update packet number length on packet boundary. | 489 // Update packet number length on packet boundary. |
490 if (!FLAGS_quic_simple_packet_number_length) { | 490 if (!FLAGS_quic_simple_packet_number_length_2) { |
491 packet_.packet_number_length = next_packet_number_length_; | 491 packet_.packet_number_length = next_packet_number_length_; |
492 } | 492 } |
493 packet_size_ = GetPacketHeaderSize( | 493 packet_size_ = GetPacketHeaderSize( |
494 framer_->version(), connection_id_length_, send_version_in_packet_, | 494 framer_->version(), connection_id_length_, send_version_in_packet_, |
495 send_path_id_in_packet_, IncludeNonceInPublicHeader(), | 495 send_path_id_in_packet_, IncludeNonceInPublicHeader(), |
496 packet_.packet_number_length); | 496 packet_.packet_number_length); |
497 return packet_size_; | 497 return packet_size_; |
498 } | 498 } |
499 | 499 |
500 bool QuicPacketCreator::AddSavedFrame(const QuicFrame& frame) { | 500 bool QuicPacketCreator::AddSavedFrame(const QuicFrame& frame) { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 if (FLAGS_quic_never_write_unencrypted_data && frame.type == STREAM_FRAME && | 615 if (FLAGS_quic_never_write_unencrypted_data && frame.type == STREAM_FRAME && |
616 frame.stream_frame->stream_id != kCryptoStreamId && | 616 frame.stream_frame->stream_id != kCryptoStreamId && |
617 packet_.encryption_level == ENCRYPTION_NONE) { | 617 packet_.encryption_level == ENCRYPTION_NONE) { |
618 const string error_details = "Cannot send stream data without encryption."; | 618 const string error_details = "Cannot send stream data without encryption."; |
619 QUIC_BUG << error_details; | 619 QUIC_BUG << error_details; |
620 delegate_->OnUnrecoverableError( | 620 delegate_->OnUnrecoverableError( |
621 QUIC_ATTEMPT_TO_SEND_UNENCRYPTED_STREAM_DATA, error_details, | 621 QUIC_ATTEMPT_TO_SEND_UNENCRYPTED_STREAM_DATA, error_details, |
622 ConnectionCloseSource::FROM_SELF); | 622 ConnectionCloseSource::FROM_SELF); |
623 return false; | 623 return false; |
624 } | 624 } |
625 if (!FLAGS_quic_simple_packet_number_length) { | 625 if (!FLAGS_quic_simple_packet_number_length_2) { |
626 MaybeUpdatePacketNumberLength(); | 626 MaybeUpdatePacketNumberLength(); |
627 } | 627 } |
628 size_t frame_len = framer_->GetSerializedFrameLength( | 628 size_t frame_len = framer_->GetSerializedFrameLength( |
629 frame, BytesFree(), queued_frames_.empty(), true, | 629 frame, BytesFree(), queued_frames_.empty(), true, |
630 packet_.packet_number_length); | 630 packet_.packet_number_length); |
631 if (frame_len == 0) { | 631 if (frame_len == 0) { |
632 // Current open packet is full. | 632 // Current open packet is full. |
633 Flush(); | 633 Flush(); |
634 return false; | 634 return false; |
635 } | 635 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
719 if (bit_mask_ == 0) { | 719 if (bit_mask_ == 0) { |
720 bit_bucket_ = random_->RandUint64(); | 720 bit_bucket_ = random_->RandUint64(); |
721 bit_mask_ = 1; | 721 bit_mask_ = 1; |
722 } | 722 } |
723 bool result = ((bit_bucket_ & bit_mask_) != 0); | 723 bool result = ((bit_bucket_ & bit_mask_) != 0); |
724 bit_mask_ <<= 1; | 724 bit_mask_ <<= 1; |
725 return result; | 725 return result; |
726 } | 726 } |
727 | 727 |
728 } // namespace net | 728 } // namespace net |
OLD | NEW |