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