| 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/basictypes.h" | |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" |
| 11 #include "net/quic/crypto/quic_random.h" | 11 #include "net/quic/crypto/quic_random.h" |
| 12 #include "net/quic/quic_data_writer.h" | 12 #include "net/quic/quic_data_writer.h" |
| 13 #include "net/quic/quic_fec_group.h" | 13 #include "net/quic/quic_fec_group.h" |
| 14 #include "net/quic/quic_flags.h" | 14 #include "net/quic/quic_flags.h" |
| 15 #include "net/quic/quic_utils.h" | 15 #include "net/quic/quic_utils.h" |
| 16 | 16 |
| 17 using base::StringPiece; | 17 using base::StringPiece; |
| 18 using std::make_pair; | 18 using std::make_pair; |
| 19 using std::max; | 19 using std::max; |
| 20 using std::min; | 20 using std::min; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 35 // expect to be able to recover from any loss in about an RTT. We resolve this | 35 // expect to be able to recover from any loss in about an RTT. We resolve this |
| 36 // tradeoff by sending an FEC packet atmost half an RTT, or equivalently, half | 36 // tradeoff by sending an FEC packet atmost half an RTT, or equivalently, half |
| 37 // the max number of in-flight packets, the first protected packet. Since we | 37 // the max number of in-flight packets, the first protected packet. Since we |
| 38 // don't want to delay an FEC packet past half an RTT, we set the max FEC group | 38 // don't want to delay an FEC packet past half an RTT, we set the max FEC group |
| 39 // size to be half the current congestion window. | 39 // size to be half the current congestion window. |
| 40 const float kMaxPacketsInFlightMultiplierForFecGroupSize = 0.5; | 40 const float kMaxPacketsInFlightMultiplierForFecGroupSize = 0.5; |
| 41 const float kRttMultiplierForFecTimeout = 0.5; | 41 const float kRttMultiplierForFecTimeout = 0.5; |
| 42 | 42 |
| 43 // Minimum timeout for FEC alarm, set to half the minimum Tail Loss Probe | 43 // Minimum timeout for FEC alarm, set to half the minimum Tail Loss Probe |
| 44 // timeout of 10ms. | 44 // timeout of 10ms. |
| 45 const int64 kMinFecTimeoutMs = 5u; | 45 const int64_t kMinFecTimeoutMs = 5u; |
| 46 | 46 |
| 47 } // namespace | 47 } // namespace |
| 48 | 48 |
| 49 // A QuicRandom wrapper that gets a bucket of entropy and distributes it | 49 // A QuicRandom wrapper that gets a bucket of entropy and distributes it |
| 50 // bit-by-bit. Replenishes the bucket as needed. Not thread-safe. Expose this | 50 // bit-by-bit. Replenishes the bucket as needed. Not thread-safe. Expose this |
| 51 // class if single bit randomness is needed elsewhere. | 51 // class if single bit randomness is needed elsewhere. |
| 52 class QuicRandomBoolSource { | 52 class QuicRandomBoolSource { |
| 53 public: | 53 public: |
| 54 // random: Source of entropy. Not owned. | 54 // random: Source of entropy. Not owned. |
| 55 explicit QuicRandomBoolSource(QuicRandom* random) | 55 explicit QuicRandomBoolSource(QuicRandom* random) |
| 56 : random_(random), bit_bucket_(0), bit_mask_(0) {} | 56 : random_(random), bit_bucket_(0), bit_mask_(0) {} |
| 57 | 57 |
| 58 ~QuicRandomBoolSource() {} | 58 ~QuicRandomBoolSource() {} |
| 59 | 59 |
| 60 // Returns the next random bit from the bucket. | 60 // Returns the next random bit from the bucket. |
| 61 bool RandBool() { | 61 bool RandBool() { |
| 62 if (bit_mask_ == 0) { | 62 if (bit_mask_ == 0) { |
| 63 bit_bucket_ = random_->RandUint64(); | 63 bit_bucket_ = random_->RandUint64(); |
| 64 bit_mask_ = 1; | 64 bit_mask_ = 1; |
| 65 } | 65 } |
| 66 bool result = ((bit_bucket_ & bit_mask_) != 0); | 66 bool result = ((bit_bucket_ & bit_mask_) != 0); |
| 67 bit_mask_ <<= 1; | 67 bit_mask_ <<= 1; |
| 68 return result; | 68 return result; |
| 69 } | 69 } |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 // Source of entropy. | 72 // Source of entropy. |
| 73 QuicRandom* random_; | 73 QuicRandom* random_; |
| 74 // Stored random bits. | 74 // Stored random bits. |
| 75 uint64 bit_bucket_; | 75 uint64_t bit_bucket_; |
| 76 // The next available bit has "1" in the mask. Zero means empty bucket. | 76 // The next available bit has "1" in the mask. Zero means empty bucket. |
| 77 uint64 bit_mask_; | 77 uint64_t bit_mask_; |
| 78 | 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(QuicRandomBoolSource); | 79 DISALLOW_COPY_AND_ASSIGN(QuicRandomBoolSource); |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 QuicPacketCreator::QuicPacketCreator(QuicConnectionId connection_id, | 82 QuicPacketCreator::QuicPacketCreator(QuicConnectionId connection_id, |
| 83 QuicFramer* framer, | 83 QuicFramer* framer, |
| 84 QuicRandom* random_generator, | 84 QuicRandom* random_generator, |
| 85 DelegateInterface* delegate) | 85 DelegateInterface* delegate) |
| 86 : delegate_(delegate), | 86 : delegate_(delegate), |
| 87 debug_delegate_(nullptr), | 87 debug_delegate_(nullptr), |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 | 235 |
| 236 void QuicPacketCreator::UpdatePacketNumberLength( | 236 void QuicPacketCreator::UpdatePacketNumberLength( |
| 237 QuicPacketNumber least_packet_awaited_by_peer, | 237 QuicPacketNumber least_packet_awaited_by_peer, |
| 238 QuicPacketCount max_packets_in_flight) { | 238 QuicPacketCount max_packets_in_flight) { |
| 239 DCHECK_LE(least_packet_awaited_by_peer, packet_number_ + 1); | 239 DCHECK_LE(least_packet_awaited_by_peer, packet_number_ + 1); |
| 240 // Since the packet creator will not change packet number length mid FEC | 240 // Since the packet creator will not change packet number length mid FEC |
| 241 // group, include the size of an FEC group to be safe. | 241 // group, include the size of an FEC group to be safe. |
| 242 const QuicPacketNumber current_delta = max_packets_per_fec_group_ + | 242 const QuicPacketNumber current_delta = max_packets_per_fec_group_ + |
| 243 packet_number_ + 1 - | 243 packet_number_ + 1 - |
| 244 least_packet_awaited_by_peer; | 244 least_packet_awaited_by_peer; |
| 245 const uint64 delta = max(current_delta, max_packets_in_flight); | 245 const uint64_t delta = max(current_delta, max_packets_in_flight); |
| 246 next_packet_number_length_ = | 246 next_packet_number_length_ = |
| 247 QuicFramer::GetMinSequenceNumberLength(delta * 4); | 247 QuicFramer::GetMinSequenceNumberLength(delta * 4); |
| 248 } | 248 } |
| 249 | 249 |
| 250 bool QuicPacketCreator::ConsumeData(QuicStreamId id, | 250 bool QuicPacketCreator::ConsumeData(QuicStreamId id, |
| 251 QuicIOVector iov, | 251 QuicIOVector iov, |
| 252 size_t iov_offset, | 252 size_t iov_offset, |
| 253 QuicStreamOffset offset, | 253 QuicStreamOffset offset, |
| 254 bool fin, | 254 bool fin, |
| 255 bool needs_padding, | 255 bool needs_padding, |
| (...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 860 hash_map<QuicPathId, QuicPacketNumber>::iterator it = | 860 hash_map<QuicPathId, QuicPacketNumber>::iterator it = |
| 861 multipath_packet_number_.find(path_id); | 861 multipath_packet_number_.find(path_id); |
| 862 // If path_id is not in the map, it's a new path. Set packet_number to 0. | 862 // If path_id is not in the map, it's a new path. Set packet_number to 0. |
| 863 packet_number_ = it == multipath_packet_number_.end() ? 0 : it->second; | 863 packet_number_ = it == multipath_packet_number_.end() ? 0 : it->second; |
| 864 current_path_ = path_id; | 864 current_path_ = path_id; |
| 865 // Switching path needs to update packet number length. | 865 // Switching path needs to update packet number length. |
| 866 UpdatePacketNumberLength(least_packet_awaited_by_peer, max_packets_in_flight); | 866 UpdatePacketNumberLength(least_packet_awaited_by_peer, max_packets_in_flight); |
| 867 } | 867 } |
| 868 | 868 |
| 869 } // namespace net | 869 } // namespace net |
| OLD | NEW |