| 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. Also provides packet creation of | 6 // it's time to create a packet from them. Also provides packet creation of |
| 7 // FEC packets based on previously created packets. If multipath enabled, only | 7 // FEC packets based on previously created packets. If multipath enabled, only |
| 8 // creates packets on one path at the same time. Currently, next packet number | 8 // creates packets on one path at the same time. Currently, next packet number |
| 9 // is tracked per-path. | 9 // is tracked per-path. |
| 10 | 10 |
| 11 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_ | 11 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_ |
| 12 #define NET_QUIC_QUIC_PACKET_CREATOR_H_ | 12 #define NET_QUIC_QUIC_PACKET_CREATOR_H_ |
| 13 | 13 |
| 14 #include <stddef.h> | 14 #include <stddef.h> |
| 15 | 15 |
| 16 #include <string> | 16 #include <string> |
| 17 #include <unordered_map> |
| 17 #include <utility> | 18 #include <utility> |
| 18 #include <vector> | 19 #include <vector> |
| 19 | 20 |
| 20 #include "base/macros.h" | 21 #include "base/macros.h" |
| 21 #include "base/memory/scoped_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
| 22 #include "base/strings/string_piece.h" | 23 #include "base/strings/string_piece.h" |
| 23 #include "net/quic/quic_fec_group.h" | 24 #include "net/quic/quic_fec_group.h" |
| 24 #include "net/quic/quic_framer.h" | 25 #include "net/quic/quic_framer.h" |
| 25 #include "net/quic/quic_protocol.h" | 26 #include "net/quic/quic_protocol.h" |
| 26 | 27 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 QuicIOVector iov, | 121 QuicIOVector iov, |
| 121 size_t iov_offset, | 122 size_t iov_offset, |
| 122 QuicStreamOffset offset, | 123 QuicStreamOffset offset, |
| 123 bool fin, | 124 bool fin, |
| 124 bool needs_padding, | 125 bool needs_padding, |
| 125 QuicFrame* frame, | 126 QuicFrame* frame, |
| 126 FecProtection fec_protection); | 127 FecProtection fec_protection); |
| 127 | 128 |
| 128 // Returns true if current open packet can accommodate more stream frames of | 129 // Returns true if current open packet can accommodate more stream frames of |
| 129 // stream |id| at |offset|, false otherwise. | 130 // stream |id| at |offset|, false otherwise. |
| 130 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const; | 131 bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset); |
| 131 | |
| 132 // Serializes all frames into a single packet. All frames must fit into a | |
| 133 // single packet. Also, sets the entropy hash of the serialized packet to a | |
| 134 // random bool and returns that value as a member of SerializedPacket. | |
| 135 // Never returns a RetransmittableFrames in SerializedPacket. | |
| 136 SerializedPacket SerializeAllFrames(const QuicFrames& frames, | |
| 137 char* buffer, | |
| 138 size_t buffer_len); | |
| 139 | 132 |
| 140 // Re-serializes frames with the original packet's packet number length. | 133 // Re-serializes frames with the original packet's packet number length. |
| 141 // Used for retransmitting packets to ensure they aren't too long. | 134 // Used for retransmitting packets to ensure they aren't too long. |
| 142 // Caller must ensure that any open FEC group is closed before calling this | 135 // Caller must ensure that any open FEC group is closed before calling this |
| 143 // method. | 136 // method. |
| 144 SerializedPacket ReserializeAllFrames( | 137 SerializedPacket ReserializeAllFrames( |
| 145 const PendingRetransmission& retransmission, | 138 const PendingRetransmission& retransmission, |
| 146 char* buffer, | 139 char* buffer, |
| 147 size_t buffer_len); | 140 size_t buffer_len); |
| 148 | 141 |
| 149 // Serializes all added frames into a single packet and invokes the delegate_ | 142 // Serializes all added frames into a single packet and invokes the delegate_ |
| 150 // to further process the SerializedPacket. | 143 // to further process the SerializedPacket. |
| 151 void Flush(); | 144 void Flush(); |
| 152 | 145 |
| 153 // Returns true if there are frames pending to be serialized. | 146 // Returns true if there are frames pending to be serialized. |
| 154 bool HasPendingFrames() const; | 147 bool HasPendingFrames() const; |
| 155 | 148 |
| 156 // Returns true if there are retransmittable frames pending to be serialized. | 149 // Returns true if there are retransmittable frames pending to be serialized. |
| 157 bool HasPendingRetransmittableFrames() const; | 150 bool HasPendingRetransmittableFrames() const; |
| 158 | 151 |
| 159 // Returns the number of bytes which are available to be used by additional | 152 // Returns the number of bytes which are available to be used by additional |
| 160 // frames in the packet. Since stream frames are slightly smaller when they | 153 // frames in the packet. Since stream frames are slightly smaller when they |
| 161 // are the last frame in a packet, this method will return a different | 154 // are the last frame in a packet, this method will return a different |
| 162 // value than max_packet_size - PacketSize(), in this case. | 155 // value than max_packet_size - PacketSize(), in this case. |
| 163 size_t BytesFree() const; | 156 size_t BytesFree(); |
| 164 | 157 |
| 165 // Returns the number of bytes that the packet will expand by if a new frame | 158 // Returns the number of bytes that the packet will expand by if a new frame |
| 166 // is added to the packet. If the last frame was a stream frame, it will | 159 // is added to the packet. If the last frame was a stream frame, it will |
| 167 // expand slightly when a new frame is added, and this method returns the | 160 // expand slightly when a new frame is added, and this method returns the |
| 168 // amount of expected expansion. If the packet is in an FEC group, no | 161 // amount of expected expansion. If the packet is in an FEC group, no |
| 169 // expansion happens and this method always returns zero. | 162 // expansion happens and this method always returns zero. |
| 170 size_t ExpansionOnNewFrame() const; | 163 size_t ExpansionOnNewFrame() const; |
| 171 | 164 |
| 172 // Returns the number of bytes in the current packet, including the header, | 165 // Returns the number of bytes in the current packet, including the header, |
| 173 // if serialized with the current frames. Adding a frame to the packet | 166 // if serialized with the current frames. Adding a frame to the packet |
| 174 // may change the serialized length of existing frames, as per the comment | 167 // may change the serialized length of existing frames, as per the comment |
| 175 // in BytesFree. | 168 // in BytesFree. |
| 176 size_t PacketSize() const; | 169 size_t PacketSize(); |
| 177 | 170 |
| 178 // Tries to add |frame| to the packet creator's list of frames to be | 171 // Tries to add |frame| to the packet creator's list of frames to be |
| 179 // serialized. If the frame does not fit into the current packet, flushes the | 172 // serialized. If the frame does not fit into the current packet, flushes the |
| 180 // packet and returns false. | 173 // packet and returns false. |
| 181 bool AddSavedFrame(const QuicFrame& frame); | 174 bool AddSavedFrame(const QuicFrame& frame); |
| 182 | 175 |
| 183 // Identical to AddSavedFrame, but allows the frame to be padded. | 176 // Identical to AddSavedFrame, but allows the frame to be padded. |
| 184 bool AddPaddedSavedFrame(const QuicFrame& frame); | 177 bool AddPaddedSavedFrame(const QuicFrame& frame); |
| 185 | 178 |
| 186 // Adds |listener| to the next serialized packet and notifies the | 179 // Adds |listener| to the next serialized packet and notifies the |
| (...skipping 13 matching lines...) Expand all Loading... |
| 200 static SerializedPacket NoPacket(); | 193 static SerializedPacket NoPacket(); |
| 201 | 194 |
| 202 // Called when the congestion window has changed. | 195 // Called when the congestion window has changed. |
| 203 void OnCongestionWindowChange(QuicPacketCount max_packets_in_flight); | 196 void OnCongestionWindowChange(QuicPacketCount max_packets_in_flight); |
| 204 | 197 |
| 205 // Called when the RTT may have changed. | 198 // Called when the RTT may have changed. |
| 206 void OnRttChange(QuicTime::Delta rtt); | 199 void OnRttChange(QuicTime::Delta rtt); |
| 207 | 200 |
| 208 // Sets the encryption level that will be applied to new packets. | 201 // Sets the encryption level that will be applied to new packets. |
| 209 void set_encryption_level(EncryptionLevel level) { | 202 void set_encryption_level(EncryptionLevel level) { |
| 210 encryption_level_ = level; | 203 packet_.encryption_level = level; |
| 211 } | 204 } |
| 212 | 205 |
| 213 // packet number of the last created packet, or 0 if no packets have been | 206 // packet number of the last created packet, or 0 if no packets have been |
| 214 // created. | 207 // created. |
| 215 QuicPacketNumber packet_number() const { return packet_number_; } | 208 QuicPacketNumber packet_number() const { return packet_.packet_number; } |
| 216 | 209 |
| 217 QuicConnectionIdLength connection_id_length() const { | 210 QuicConnectionIdLength connection_id_length() const { |
| 218 return connection_id_length_; | 211 return connection_id_length_; |
| 219 } | 212 } |
| 220 | 213 |
| 221 void set_connection_id_length(QuicConnectionIdLength length) { | 214 void set_connection_id_length(QuicConnectionIdLength length) { |
| 222 connection_id_length_ = length; | 215 connection_id_length_ = length; |
| 223 } | 216 } |
| 224 | 217 |
| 225 QuicByteCount max_packet_length() const { return max_packet_length_; } | 218 QuicByteCount max_packet_length() const { return max_packet_length_; } |
| 226 | 219 |
| 227 bool has_ack() const { return has_ack_; } | 220 bool has_ack() const { return packet_.has_ack; } |
| 228 | 221 |
| 229 bool has_stop_waiting() const { return has_stop_waiting_; } | 222 bool has_stop_waiting() const { return packet_.has_stop_waiting; } |
| 230 | 223 |
| 231 // Sets the encrypter to use for the encryption level and updates the max | 224 // Sets the encrypter to use for the encryption level and updates the max |
| 232 // plaintext size. | 225 // plaintext size. |
| 233 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter); | 226 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter); |
| 234 | 227 |
| 235 // Indicates whether the packet creator is in a state where it can change | 228 // Indicates whether the packet creator is in a state where it can change |
| 236 // current maximum packet length. | 229 // current maximum packet length. |
| 237 bool CanSetMaxPacketLength() const; | 230 bool CanSetMaxPacketLength() const; |
| 238 | 231 |
| 239 // Sets the maximum packet length. | 232 // Sets the maximum packet length. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 // frames if there isn't room. If |save_retransmittable_frames| is true, | 310 // frames if there isn't room. If |save_retransmittable_frames| is true, |
| 318 // saves the |frame| in the next SerializedPacket. | 311 // saves the |frame| in the next SerializedPacket. |
| 319 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames); | 312 bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames); |
| 320 | 313 |
| 321 // Adds a padding frame to the current packet only if the current packet | 314 // Adds a padding frame to the current packet only if the current packet |
| 322 // contains a handshake message, and there is sufficient room to fit a | 315 // contains a handshake message, and there is sufficient room to fit a |
| 323 // padding frame. | 316 // padding frame. |
| 324 void MaybeAddPadding(); | 317 void MaybeAddPadding(); |
| 325 | 318 |
| 326 // Serializes all frames which have been added and adds any which should be | 319 // Serializes all frames which have been added and adds any which should be |
| 327 // retransmitted to queued_retransmittable_frames_ if it's not nullptr. All | 320 // retransmitted to packet_.retransmittable_frames. All frames must fit into |
| 328 // frames must fit into a single packet. Sets the entropy hash of the | 321 // a single packet. Sets the entropy hash of the serialized packet to a |
| 329 // serialized packet to a random bool and returns that value as a member of | 322 // random bool. |
| 330 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to | |
| 331 // the corresponding RetransmittableFrames if any frames are to be | |
| 332 // retransmitted. | |
| 333 // Fails if |buffer_len| isn't long enough for the encrypted packet. | 323 // Fails if |buffer_len| isn't long enough for the encrypted packet. |
| 334 SerializedPacket SerializePacket(char* encrypted_buffer, size_t buffer_len); | 324 void SerializePacket(char* encrypted_buffer, size_t buffer_len); |
| 335 | 325 |
| 336 // Called after a new SerialiedPacket is created to call the delegate's | 326 // Called after a new SerialiedPacket is created to call the delegate's |
| 337 // OnSerializedPacket, reset state, and potentially flush FEC groups. | 327 // OnSerializedPacket, reset state, and potentially flush FEC groups. |
| 338 void OnSerializedPacket(SerializedPacket* packet); | 328 void OnSerializedPacket(); |
| 329 |
| 330 // Clears all fields of packet_ that should be cleared between serializations. |
| 331 void ClearPacket(); |
| 339 | 332 |
| 340 // Turn on FEC protection for subsequent packets. If no FEC group is currently | 333 // Turn on FEC protection for subsequent packets. If no FEC group is currently |
| 341 // open, this method flushes current open packet and then turns FEC on. | 334 // open, this method flushes current open packet and then turns FEC on. |
| 342 void MaybeStartFecProtection(); | 335 void MaybeStartFecProtection(); |
| 343 | 336 |
| 344 // Turn on FEC protection for subsequently created packets. FEC should be | 337 // Turn on FEC protection for subsequently created packets. FEC should be |
| 345 // enabled first (max_packets_per_fec_group should be non-zero) for FEC | 338 // enabled first (max_packets_per_fec_group should be non-zero) for FEC |
| 346 // protection to start. | 339 // protection to start. |
| 347 void StartFecProtectingPackets(); | 340 void StartFecProtectingPackets(); |
| 348 | 341 |
| 349 // Turn off FEC protection for subsequently created packets. If the creator | 342 // Turn off FEC protection for subsequently created packets. If the creator |
| 350 // has any open FEC group, call will fail. It is the caller's responsibility | 343 // has any open FEC group, call will fail. It is the caller's responsibility |
| 351 // to flush out FEC packets in generation, and to verify with ShouldSendFec() | 344 // to flush out FEC packets in generation, and to verify with ShouldSendFec() |
| 352 // that there is no open FEC group. | 345 // that there is no open FEC group. |
| 353 void StopFecProtectingPackets(); | 346 void StopFecProtectingPackets(); |
| 354 | 347 |
| 355 // Resets (closes) the FEC group. This method should only be called on a | 348 // Resets (closes) the FEC group. This method should only be called on a |
| 356 // packet boundary. | 349 // packet boundary. |
| 357 void ResetFecGroup(); | 350 void ResetFecGroup(); |
| 358 | 351 |
| 359 // Packetize FEC data. All frames must fit into a single packet. Also, sets | 352 // Packetize FEC data. Sets the entropy hash of the serialized packet to a |
| 360 // the entropy hash of the serialized packet to a random bool and returns | 353 // random bool. |
| 361 // that value as a member of SerializedPacket. | |
| 362 // Fails if |buffer_len| isn't long enough for the encrypted packet. | 354 // Fails if |buffer_len| isn't long enough for the encrypted packet. |
| 363 SerializedPacket SerializeFec(char* buffer, size_t buffer_len); | 355 void SerializeFec(char* buffer, size_t buffer_len); |
| 364 | 356 |
| 365 // Does not own these delegates or the framer. | 357 // Does not own these delegates or the framer. |
| 366 DelegateInterface* delegate_; | 358 DelegateInterface* delegate_; |
| 367 DebugDelegate* debug_delegate_; | 359 DebugDelegate* debug_delegate_; |
| 368 QuicFramer* framer_; | 360 QuicFramer* framer_; |
| 369 | 361 |
| 370 scoped_ptr<QuicRandomBoolSource> random_bool_source_; | 362 scoped_ptr<QuicRandomBoolSource> random_bool_source_; |
| 371 QuicBufferAllocator* const buffer_allocator_; | 363 QuicBufferAllocator* const buffer_allocator_; |
| 372 | 364 |
| 373 // Controls whether version should be included while serializing the packet. | 365 // Controls whether version should be included while serializing the packet. |
| 374 bool send_version_in_packet_; | 366 bool send_version_in_packet_; |
| 375 // Controls whether path id should be included while serializing the packet. | 367 // Controls whether path id should be included while serializing the packet. |
| 376 bool send_path_id_in_packet_; | 368 bool send_path_id_in_packet_; |
| 377 // Staging variable to hold next packet number length. When sequence | 369 // Staging variable to hold next packet number length. When sequence |
| 378 // number length is to be changed, this variable holds the new length until | 370 // number length is to be changed, this variable holds the new length until |
| 379 // a packet or FEC group boundary, when the creator's packet_number_length_ | 371 // a packet or FEC group boundary, when the creator's packet_number_length_ |
| 380 // can be changed to this new value. | 372 // can be changed to this new value. |
| 381 QuicPacketNumberLength next_packet_number_length_; | 373 QuicPacketNumberLength next_packet_number_length_; |
| 382 // Maximum length including headers and encryption (UDP payload length.) | 374 // Maximum length including headers and encryption (UDP payload length.) |
| 383 QuicByteCount max_packet_length_; | 375 QuicByteCount max_packet_length_; |
| 384 mutable size_t max_plaintext_size_; | 376 size_t max_plaintext_size_; |
| 377 // Length of connection_id to send over the wire. |
| 378 QuicConnectionIdLength connection_id_length_; |
| 385 | 379 |
| 386 // Frames to be added to the next SerializedPacket | 380 // Frames to be added to the next SerializedPacket |
| 387 QuicFrames queued_frames_; | 381 QuicFrames queued_frames_; |
| 388 | 382 |
| 389 // Fields used to populate SerializedPacket. | 383 // packet_size should never be read directly, use PacketSize() instead. |
| 384 // TODO(ianswett): Move packet_size_ into SerializedPacket once |
| 385 // QuicEncryptedPacket has been flattened into SerializedPacket. |
| 386 size_t packet_size_; |
| 390 QuicConnectionId connection_id_; | 387 QuicConnectionId connection_id_; |
| 391 EncryptionLevel encryption_level_; | 388 |
| 392 // True if an ack is queued in queued_frames_. | 389 // Packet used to invoke OnSerializedPacket. |
| 393 bool has_ack_; | 390 SerializedPacket packet_; |
| 394 // True if a stop waiting frame is queued in queued_frames_. | |
| 395 bool has_stop_waiting_; | |
| 396 // The path which current constructed packet will be sent on. | |
| 397 QuicPathId current_path_; | |
| 398 QuicPacketNumber packet_number_; | |
| 399 // Length of connection_id to send over the wire. | |
| 400 QuicConnectionIdLength connection_id_length_; | |
| 401 // packet number length for the current packet and for the current FEC group | |
| 402 // when FEC is enabled. Mutable so PacketSize() can adjust it when the packet | |
| 403 // is empty. | |
| 404 mutable QuicPacketNumberLength packet_number_length_; | |
| 405 // packet_size_ is mutable because it's just a cache of the current size. | |
| 406 // packet_size should never be read directly, use PacketSize() instead. | |
| 407 mutable size_t packet_size_; | |
| 408 scoped_ptr<QuicFrames> queued_retransmittable_frames_; | |
| 409 // If true, the packet will be padded up to |max_packet_length_|. | |
| 410 bool needs_padding_; | |
| 411 IsHandshake has_crypto_handshake_; | |
| 412 // Stores ack std::listeners that should be attached to the next packet. | |
| 413 std::list<AckListenerWrapper> ack_listeners_; | |
| 414 | 391 |
| 415 // Map mapping path_id to last sent packet number on the path. | 392 // Map mapping path_id to last sent packet number on the path. |
| 416 hash_map<QuicPathId, QuicPacketNumber> multipath_packet_number_; | 393 std::unordered_map<QuicPathId, QuicPacketNumber> multipath_packet_number_; |
| 417 | 394 |
| 418 // FEC related fields. | 395 // FEC related fields. |
| 419 // True when creator is requested to turn on FEC protection. False otherwise. | 396 // True when creator is requested to turn on FEC protection. False otherwise. |
| 420 // There is a time difference between should_fec_protect_next_packet_ is | 397 // There is a time difference between should_fec_protect_next_packet_ is |
| 421 // true/false and FEC is actually turned on/off (e.g., The creator may have an | 398 // true/false and FEC is actually turned on/off (e.g., The creator may have an |
| 422 // open FEC group even if this variable is false). | 399 // open FEC group even if this variable is false). |
| 423 bool should_fec_protect_next_packet_; | 400 bool should_fec_protect_next_packet_; |
| 424 // If true, any created packets will be FEC protected. | 401 // If true, any created packets will be FEC protected. |
| 425 // TODO(fayang): Combine should_fec_protect_next_packet and fec_protect_ to | 402 // TODO(fayang): Combine should_fec_protect_next_packet and fec_protect_ to |
| 426 // one variable. | 403 // one variable. |
| 427 bool fec_protect_; | 404 bool fec_protect_; |
| 428 scoped_ptr<QuicFecGroup> fec_group_; | 405 scoped_ptr<QuicFecGroup> fec_group_; |
| 429 // 0 indicates FEC is disabled. | 406 // 0 indicates FEC is disabled. |
| 430 size_t max_packets_per_fec_group_; | 407 size_t max_packets_per_fec_group_; |
| 431 // FEC policy that specifies when to send FEC packet. | 408 // FEC policy that specifies when to send FEC packet. |
| 432 FecSendPolicy fec_send_policy_; | 409 FecSendPolicy fec_send_policy_; |
| 433 // Timeout used for FEC alarm. Can be set to zero initially or if the SRTT has | 410 // Timeout used for FEC alarm. Can be set to zero initially or if the SRTT has |
| 434 // not yet been set. | 411 // not yet been set. |
| 435 QuicTime::Delta fec_timeout_; | 412 QuicTime::Delta fec_timeout_; |
| 436 // The multiplication factor for FEC timeout based on RTT. | 413 // The multiplication factor for FEC timeout based on RTT. |
| 437 // TODO(rtenneti): Delete this code after the 0.25 RTT FEC experiment. | 414 // TODO(rtenneti): Delete this code after the 0.25 RTT FEC experiment. |
| 438 float rtt_multiplier_for_fec_timeout_; | 415 float rtt_multiplier_for_fec_timeout_; |
| 439 | 416 |
| 440 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator); | 417 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator); |
| 441 }; | 418 }; |
| 442 | 419 |
| 443 } // namespace net | 420 } // namespace net |
| 444 | 421 |
| 445 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_ | 422 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_ |
| OLD | NEW |