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 // Accumulates frames for the next packet until more frames no longer fit or | 5 // Accumulates frames for the next packet until more frames no longer fit or |
6 // it's time to create a packet from them. If multipath enabled, only creates | 6 // it's time to create a packet from them. If multipath enabled, only creates |
7 // packets on one path at the same time. Currently, next packet number is | 7 // packets on one path at the same time. Currently, next packet number is |
8 // tracked per-path. | 8 // tracked per-path. |
9 | 9 |
10 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_ | 10 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_ |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "base/strings/string_piece.h" | 22 #include "base/strings/string_piece.h" |
23 #include "net/base/net_export.h" | 23 #include "net/base/net_export.h" |
24 #include "net/quic/core/quic_framer.h" | 24 #include "net/quic/core/quic_framer.h" |
25 #include "net/quic/core/quic_protocol.h" | 25 #include "net/quic/core/quic_protocol.h" |
26 | 26 |
27 namespace net { | 27 namespace net { |
28 namespace test { | 28 namespace test { |
29 class QuicPacketCreatorPeer; | 29 class QuicPacketCreatorPeer; |
30 } | 30 } |
31 | 31 |
32 class QuicRandom; | |
33 | |
34 class NET_EXPORT_PRIVATE QuicPacketCreator { | 32 class NET_EXPORT_PRIVATE QuicPacketCreator { |
35 public: | 33 public: |
36 // A delegate interface for further processing serialized packet. | 34 // A delegate interface for further processing serialized packet. |
37 class NET_EXPORT_PRIVATE DelegateInterface | 35 class NET_EXPORT_PRIVATE DelegateInterface |
38 : public QuicConnectionCloseDelegateInterface { | 36 : public QuicConnectionCloseDelegateInterface { |
39 public: | 37 public: |
40 ~DelegateInterface() override {} | 38 ~DelegateInterface() override {} |
41 // Called when a packet is serialized. Delegate does not take the ownership | 39 // Called when a packet is serialized. Delegate does not take the ownership |
42 // of |serialized_packet|, but takes ownership of any frames it removes | 40 // of |serialized_packet|, but takes ownership of any frames it removes |
43 // from |packet.retransmittable_frames|. | 41 // from |packet.retransmittable_frames|. |
44 virtual void OnSerializedPacket(SerializedPacket* serialized_packet) = 0; | 42 virtual void OnSerializedPacket(SerializedPacket* serialized_packet) = 0; |
45 }; | 43 }; |
46 | 44 |
47 // Interface which gets callbacks from the QuicPacketCreator at interesting | 45 // Interface which gets callbacks from the QuicPacketCreator at interesting |
48 // points. Implementations must not mutate the state of the creator | 46 // points. Implementations must not mutate the state of the creator |
49 // as a result of these callbacks. | 47 // as a result of these callbacks. |
50 class NET_EXPORT_PRIVATE DebugDelegate { | 48 class NET_EXPORT_PRIVATE DebugDelegate { |
51 public: | 49 public: |
52 virtual ~DebugDelegate() {} | 50 virtual ~DebugDelegate() {} |
53 | 51 |
54 // Called when a frame has been added to the current packet. | 52 // Called when a frame has been added to the current packet. |
55 virtual void OnFrameAddedToPacket(const QuicFrame& frame) {} | 53 virtual void OnFrameAddedToPacket(const QuicFrame& frame) {} |
56 }; | 54 }; |
57 | 55 |
58 // QuicRandom* required for packet entropy. | |
59 QuicPacketCreator(QuicConnectionId connection_id, | 56 QuicPacketCreator(QuicConnectionId connection_id, |
60 QuicFramer* framer, | 57 QuicFramer* framer, |
61 QuicRandom* random_generator, | |
62 QuicBufferAllocator* buffer_allocator, | 58 QuicBufferAllocator* buffer_allocator, |
63 DelegateInterface* delegate); | 59 DelegateInterface* delegate); |
64 | 60 |
65 ~QuicPacketCreator(); | 61 ~QuicPacketCreator(); |
66 | 62 |
67 // Makes the framer not serialize the protocol version in sent packets. | 63 // Makes the framer not serialize the protocol version in sent packets. |
68 void StopSendingVersion(); | 64 void StopSendingVersion(); |
69 | 65 |
70 // SetDiversificationNonce sets the nonce that will be sent in each public | 66 // SetDiversificationNonce sets the nonce that will be sent in each public |
71 // header of packets encrypted at the initial encryption level. Should only | 67 // header of packets encrypted at the initial encryption level. Should only |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 | 155 |
160 // Identical to AddSavedFrame, but allows the frame to be padded. | 156 // Identical to AddSavedFrame, but allows the frame to be padded. |
161 bool AddPaddedSavedFrame(const QuicFrame& frame); | 157 bool AddPaddedSavedFrame(const QuicFrame& frame); |
162 | 158 |
163 // Adds |listener| to the next serialized packet and notifies the | 159 // Adds |listener| to the next serialized packet and notifies the |
164 // std::listener with |length| as the number of acked bytes. | 160 // std::listener with |length| as the number of acked bytes. |
165 void AddAckListener(QuicAckListenerInterface* listener, | 161 void AddAckListener(QuicAckListenerInterface* listener, |
166 QuicPacketLength length); | 162 QuicPacketLength length); |
167 | 163 |
168 // Creates a version negotiation packet which supports |supported_versions|. | 164 // Creates a version negotiation packet which supports |supported_versions|. |
169 // Also, sets the entropy hash of the serialized packet to a random bool and | |
170 // returns that value as a member of SerializedPacket. | |
171 std::unique_ptr<QuicEncryptedPacket> SerializeVersionNegotiationPacket( | 165 std::unique_ptr<QuicEncryptedPacket> SerializeVersionNegotiationPacket( |
172 const QuicVersionVector& supported_versions); | 166 const QuicVersionVector& supported_versions); |
173 | 167 |
174 // Returns a dummy packet that is valid but contains no useful information. | 168 // Returns a dummy packet that is valid but contains no useful information. |
175 static SerializedPacket NoPacket(); | 169 static SerializedPacket NoPacket(); |
176 | 170 |
177 // Sets the encryption level that will be applied to new packets. | 171 // Sets the encryption level that will be applied to new packets. |
178 void set_encryption_level(EncryptionLevel level) { | 172 void set_encryption_level(EncryptionLevel level) { |
179 packet_.encryption_level = level; | 173 packet_.encryption_level = level; |
180 } | 174 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 QuicPacketNumber least_packet_awaited_by_peer, | 210 QuicPacketNumber least_packet_awaited_by_peer, |
217 QuicPacketCount max_packets_in_flight); | 211 QuicPacketCount max_packets_in_flight); |
218 | 212 |
219 void set_debug_delegate(DebugDelegate* debug_delegate) { | 213 void set_debug_delegate(DebugDelegate* debug_delegate) { |
220 debug_delegate_ = debug_delegate; | 214 debug_delegate_ = debug_delegate; |
221 } | 215 } |
222 | 216 |
223 private: | 217 private: |
224 friend class test::QuicPacketCreatorPeer; | 218 friend class test::QuicPacketCreatorPeer; |
225 | 219 |
226 // A QuicRandom wrapper that gets a bucket of entropy and distributes it | |
227 // bit-by-bit. Replenishes the bucket as needed. Not thread-safe. Expose this | |
228 // class if single bit randomness is needed elsewhere. | |
229 class QuicRandomBoolSource { | |
230 public: | |
231 // random: Source of entropy. Not owned. | |
232 explicit QuicRandomBoolSource(QuicRandom* random); | |
233 | |
234 ~QuicRandomBoolSource(); | |
235 | |
236 // Returns the next random bit from the bucket. | |
237 bool RandBool(); | |
238 | |
239 private: | |
240 // Source of entropy. | |
241 QuicRandom* random_; | |
242 // Stored random bits. | |
243 uint64_t bit_bucket_; | |
244 // The next available bit has "1" in the mask. Zero means empty bucket. | |
245 uint64_t bit_mask_; | |
246 | |
247 DISALLOW_COPY_AND_ASSIGN(QuicRandomBoolSource); | |
248 }; | |
249 | |
250 static bool ShouldRetransmit(const QuicFrame& frame); | 220 static bool ShouldRetransmit(const QuicFrame& frame); |
251 | 221 |
252 // Converts a raw payload to a frame which fits into the current open | 222 // Converts a raw payload to a frame which fits into the current open |
253 // packet. The payload begins at |iov_offset| into the |iov|. | 223 // packet. The payload begins at |iov_offset| into the |iov|. |
254 // If data is empty and fin is true, the expected behavior is to consume the | 224 // If data is empty and fin is true, the expected behavior is to consume the |
255 // fin but return 0. If any data is consumed, it will be copied into a | 225 // fin but return 0. If any data is consumed, it will be copied into a |
256 // new buffer that |frame| will point to and own. | 226 // new buffer that |frame| will point to and own. |
257 void CreateStreamFrame(QuicStreamId id, | 227 void CreateStreamFrame(QuicStreamId id, |
258 QuicIOVector iov, | 228 QuicIOVector iov, |
259 size_t iov_offset, | 229 size_t iov_offset, |
(...skipping 16 matching lines...) Expand all Loading... |
276 // saves the |frame| in the next SerializedPacket. | 246 // saves the |frame| in the next SerializedPacket. |
277 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames); | 247 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames); |
278 | 248 |
279 // Adds a padding frame to the current packet only if the current packet | 249 // Adds a padding frame to the current packet only if the current packet |
280 // contains a handshake message, and there is sufficient room to fit a | 250 // contains a handshake message, and there is sufficient room to fit a |
281 // padding frame. | 251 // padding frame. |
282 void MaybeAddPadding(); | 252 void MaybeAddPadding(); |
283 | 253 |
284 // Serializes all frames which have been added and adds any which should be | 254 // Serializes all frames which have been added and adds any which should be |
285 // retransmitted to packet_.retransmittable_frames. All frames must fit into | 255 // retransmitted to packet_.retransmittable_frames. All frames must fit into |
286 // a single packet. Sets the entropy hash of the serialized packet to a | 256 // a single packet. |
287 // random bool. | |
288 // Fails if |buffer_len| isn't long enough for the encrypted packet. | 257 // Fails if |buffer_len| isn't long enough for the encrypted packet. |
289 void SerializePacket(char* encrypted_buffer, size_t buffer_len); | 258 void SerializePacket(char* encrypted_buffer, size_t buffer_len); |
290 | 259 |
291 // Called after a new SerialiedPacket is created to call the delegate's | 260 // Called after a new SerialiedPacket is created to call the delegate's |
292 // OnSerializedPacket and reset state. | 261 // OnSerializedPacket and reset state. |
293 void OnSerializedPacket(); | 262 void OnSerializedPacket(); |
294 | 263 |
295 // Clears all fields of packet_ that should be cleared between serializations. | 264 // Clears all fields of packet_ that should be cleared between serializations. |
296 void ClearPacket(); | 265 void ClearPacket(); |
297 | 266 |
298 // Returns true if a diversification nonce should be included in the current | 267 // Returns true if a diversification nonce should be included in the current |
299 // packet's public header. | 268 // packet's public header. |
300 bool IncludeNonceInPublicHeader(); | 269 bool IncludeNonceInPublicHeader(); |
301 | 270 |
302 // Does not own these delegates or the framer. | 271 // Does not own these delegates or the framer. |
303 DelegateInterface* delegate_; | 272 DelegateInterface* delegate_; |
304 DebugDelegate* debug_delegate_; | 273 DebugDelegate* debug_delegate_; |
305 QuicFramer* framer_; | 274 QuicFramer* framer_; |
306 | 275 |
307 QuicRandomBoolSource random_bool_source_; | |
308 QuicBufferAllocator* const buffer_allocator_; | 276 QuicBufferAllocator* const buffer_allocator_; |
309 | 277 |
310 // Controls whether version should be included while serializing the packet. | 278 // Controls whether version should be included while serializing the packet. |
311 bool send_version_in_packet_; | 279 bool send_version_in_packet_; |
312 // Controls whether path id should be included while serializing the packet. | 280 // Controls whether path id should be included while serializing the packet. |
313 bool send_path_id_in_packet_; | 281 bool send_path_id_in_packet_; |
314 // Staging variable to hold next packet number length. When sequence | 282 // Staging variable to hold next packet number length. When sequence |
315 // number length is to be changed, this variable holds the new length until | 283 // number length is to be changed, this variable holds the new length until |
316 // a packet boundary, when the creator's packet_number_length_ can be changed | 284 // a packet boundary, when the creator's packet_number_length_ can be changed |
317 // to this new value. | 285 // to this new value. |
(...skipping 22 matching lines...) Expand all Loading... |
340 | 308 |
341 // Map mapping path_id to last sent packet number on the path. | 309 // Map mapping path_id to last sent packet number on the path. |
342 std::unordered_map<QuicPathId, QuicPacketNumber> multipath_packet_number_; | 310 std::unordered_map<QuicPathId, QuicPacketNumber> multipath_packet_number_; |
343 | 311 |
344 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator); | 312 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator); |
345 }; | 313 }; |
346 | 314 |
347 } // namespace net | 315 } // namespace net |
348 | 316 |
349 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_ | 317 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_ |
OLD | NEW |