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
3 // found in the LICENSE file.
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
7 // FEC packets based on previously created packets.
9 #ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_
10 #define NET_QUIC_QUIC_PACKET_CREATOR_H_
16 #include "base/memory/scoped_ptr.h"
17 #include "base/strings/string_piece.h"
18 #include "net/quic/quic_fec_group.h"
19 #include "net/quic/quic_framer.h"
20 #include "net/quic/quic_protocol.h"
24 class QuicPacketCreatorPeer
;
27 class QuicAckNotifier
;
29 class QuicRandomBoolSource
;
31 class NET_EXPORT_PRIVATE QuicPacketCreator
{
33 // QuicRandom* required for packet entropy.
34 QuicPacketCreator(QuicConnectionId connection_id
,
36 QuicRandom
* random_generator
);
40 // Turn on FEC protection for subsequently created packets. FEC should be
41 // enabled first (max_packets_per_fec_group should be non-zero) for FEC
42 // protection to start.
43 void StartFecProtectingPackets();
45 // Turn off FEC protection for subsequently created packets. If the creator
46 // has any open FEC group, call will fail. It is the caller's responsibility
47 // to flush out FEC packets in generation, and to verify with ShouldSendFec()
48 // that there is no open FEC group.
49 void StopFecProtectingPackets();
51 // Checks if it's time to send an FEC packet. |force_close| forces this to
52 // return true if an FEC group is open.
53 bool ShouldSendFec(bool force_close
) const;
55 // Resets (closes) the FEC group. This method should only be called on a
59 // Returns true if an FEC packet is under construction.
60 bool IsFecGroupOpen() const;
62 // Makes the framer not serialize the protocol version in sent packets.
63 void StopSendingVersion();
65 // Update the sequence number length to use in future packets as soon as it
66 // can be safely changed.
67 void UpdateSequenceNumberLength(
68 QuicPacketSequenceNumber least_packet_awaited_by_peer
,
69 QuicPacketCount max_packets_in_flight
);
71 // The overhead the framing will add for a packet with one frame.
72 static size_t StreamFramePacketOverhead(
73 QuicConnectionIdLength connection_id_length
,
75 QuicSequenceNumberLength sequence_number_length
,
76 QuicStreamOffset offset
,
77 InFecGroup is_in_fec_group
);
79 bool HasRoomForStreamFrame(QuicStreamId id
, QuicStreamOffset offset
) const;
81 // Converts a raw payload to a frame which fits into the currently open
82 // packet if there is one. Returns the number of bytes consumed from data.
83 // If data is empty and fin is true, the expected behavior is to consume the
84 // fin but return 0. If any data is consumed, it will be copied into a
85 // new buffer that |frame| will point to and will be stored in |buffer|.
86 size_t CreateStreamFrame(QuicStreamId id
,
88 QuicStreamOffset offset
,
91 scoped_ptr
<char[]>* buffer
);
93 // Serializes all frames into a single packet. All frames must fit into a
94 // single packet. Also, sets the entropy hash of the serialized packet to a
95 // random bool and returns that value as a member of SerializedPacket.
96 // Never returns a RetransmittableFrames in SerializedPacket.
97 SerializedPacket
SerializeAllFrames(const QuicFrames
& frames
,
101 // Re-serializes frames with the original packet's sequence number length.
102 // Used for retransmitting packets to ensure they aren't too long.
103 // Caller must ensure that any open FEC group is closed before calling this
105 SerializedPacket
ReserializeAllFrames(
106 const RetransmittableFrames
& frames
,
107 QuicSequenceNumberLength original_length
,
111 // Returns true if there are frames pending to be serialized.
112 bool HasPendingFrames() const;
114 // Returns true if there are retransmittable frames pending to be serialized.
115 bool HasPendingRetransmittableFrames() const;
117 // TODO(jri): Remove this method.
118 // Returns whether FEC protection is currently enabled. Note: Enabled does not
119 // mean that an FEC group is currently active; i.e., IsFecProtected() may
120 // still return false.
121 bool IsFecEnabled() const;
123 // Returns true if subsequent packets will be FEC protected. Note: True does
124 // not mean that an FEC packet is currently under construction; i.e.,
125 // fec_group_.get() may still be nullptr, until MaybeStartFec() is called.
126 bool IsFecProtected() const;
128 // Returns the number of bytes which are available to be used by additional
129 // frames in the packet. Since stream frames are slightly smaller when they
130 // are the last frame in a packet, this method will return a different
131 // value than max_packet_size - PacketSize(), in this case.
132 size_t BytesFree() const;
134 // Returns the number of bytes that the packet will expand by if a new frame
135 // is added to the packet. If the last frame was a stream frame, it will
136 // expand slightly when a new frame is added, and this method returns the
137 // amount of expected expansion. If the packet is in an FEC group, no
138 // expansion happens and this method always returns zero.
139 size_t ExpansionOnNewFrame() const;
141 // Returns the number of bytes in the current packet, including the header,
142 // if serialized with the current frames. Adding a frame to the packet
143 // may change the serialized length of existing frames, as per the comment
145 size_t PacketSize() const;
147 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
148 // if it is a stream frame, not other types of frames. Fix this API;
149 // add a AddNonSavedFrame method.
150 // Adds |frame| to the packet creator's list of frames to be serialized.
151 // Returns false if the frame doesn't fit into the current packet.
152 bool AddSavedFrame(const QuicFrame
& frame
);
154 // Identical to AddSavedFrame, but takes ownership of the buffer if it returns
156 bool AddSavedFrame(const QuicFrame
& frame
, char* buffer
);
158 // Serializes all frames which have been added and adds any which should be
159 // retransmitted to |retransmittable_frames| if it's not nullptr. All frames
160 // must fit into a single packet. Sets the entropy hash of the serialized
161 // packet to a random bool and returns that value as a member of
162 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to
163 // the corresponding RetransmittableFrames if any frames are to be
165 // Fails if |buffer_len| isn't long enough for the encrypted packet.
166 SerializedPacket
SerializePacket(char* encrypted_buffer
, size_t buffer_len
);
168 // Packetize FEC data. All frames must fit into a single packet. Also, sets
169 // the entropy hash of the serialized packet to a random bool and returns
170 // that value as a member of SerializedPacket.
171 // Fails if |buffer_len| isn't long enough for the encrypted packet.
172 SerializedPacket
SerializeFec(char* buffer
, size_t buffer_len
);
174 // Creates a version negotiation packet which supports |supported_versions|.
175 // Caller owns the created packet. Also, sets the entropy hash of the
176 // serialized packet to a random bool and returns that value as a member of
178 QuicEncryptedPacket
* SerializeVersionNegotiationPacket(
179 const QuicVersionVector
& supported_versions
);
181 // Returns a dummy packet that is valid but contains no useful information.
182 static SerializedPacket
NoPacket();
184 // Sets the encryption level that will be applied to new packets.
185 void set_encryption_level(EncryptionLevel level
) {
186 encryption_level_
= level
;
189 // Sequence number of the last created packet, or 0 if no packets have been
191 QuicPacketSequenceNumber
sequence_number() const {
192 return sequence_number_
;
195 QuicConnectionIdLength
connection_id_length() const {
196 return connection_id_length_
;
199 void set_connection_id_length(QuicConnectionIdLength length
) {
200 connection_id_length_
= length
;
203 QuicByteCount
max_packet_length() const {
204 return max_packet_length_
;
207 // Sets the encrypter to use for the encryption level and updates the max
209 void SetEncrypter(EncryptionLevel level
, QuicEncrypter
* encrypter
);
211 // Sets the maximum packet length.
212 void SetMaxPacketLength(QuicByteCount length
);
214 // Returns current max number of packets covered by an FEC group.
215 size_t max_packets_per_fec_group() const {
216 return max_packets_per_fec_group_
;
219 // Sets creator's max number of packets covered by an FEC group.
220 // Note: While there are no constraints on |max_packets_per_fec_group|,
221 // this setter enforces a min value of kLowestMaxPacketsPerFecGroup.
222 // To turn off FEC protection, use StopFecProtectingPackets().
223 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group
);
225 // Returns the currently open FEC group's number. If there isn't an open FEC
226 // group, returns the last closed FEC group number. Returns 0 when FEC is
227 // disabled or no FEC group has been created yet.
228 QuicFecGroupNumber
fec_group_number() { return fec_group_number_
; }
231 friend class test::QuicPacketCreatorPeer
;
233 static bool ShouldRetransmit(const QuicFrame
& frame
);
235 // Updates lengths and also starts an FEC group if FEC protection is on and
236 // there is not already an FEC group open.
237 InFecGroup
MaybeUpdateLengthsAndStartFec();
239 // Called when a data packet is constructed that is part of an FEC group.
240 // |payload| is the non-encrypted FEC protected payload of the packet.
241 void OnBuiltFecProtectedPayload(const QuicPacketHeader
& header
,
242 base::StringPiece payload
);
244 void FillPacketHeader(QuicFecGroupNumber fec_group
,
246 QuicPacketHeader
* header
);
248 // Allows a frame to be added without creating retransmittable frames.
249 // Particularly useful for retransmits using SerializeAllFrames().
250 bool AddFrame(const QuicFrame
& frame
,
251 bool save_retransmittable_frames
,
254 // Adds a padding frame to the current packet only if the current packet
255 // contains a handshake message, and there is sufficient room to fit a
257 void MaybeAddPadding();
259 QuicConnectionId connection_id_
;
260 EncryptionLevel encryption_level_
;
262 scoped_ptr
<QuicRandomBoolSource
> random_bool_source_
;
263 QuicPacketSequenceNumber sequence_number_
;
264 // If true, any created packets will be FEC protected.
265 bool should_fec_protect_
;
266 QuicFecGroupNumber fec_group_number_
;
267 scoped_ptr
<QuicFecGroup
> fec_group_
;
268 // Controls whether protocol version should be included while serializing the
270 bool send_version_in_packet_
;
271 // Maximum length including headers and encryption (UDP payload length.)
272 QuicByteCount max_packet_length_
;
273 // 0 indicates FEC is disabled.
274 size_t max_packets_per_fec_group_
;
275 // Length of connection_id to send over the wire.
276 QuicConnectionIdLength connection_id_length_
;
277 // Staging variable to hold next packet sequence number length. When sequence
278 // number length is to be changed, this variable holds the new length until
279 // a packet or FEC group boundary, when the creator's sequence_number_length_
280 // can be changed to this new value.
281 QuicSequenceNumberLength next_sequence_number_length_
;
282 // Sequence number length for the current packet and for the current FEC group
283 // when FEC is enabled. Mutable so PacketSize() can adjust it when the packet
285 mutable QuicSequenceNumberLength sequence_number_length_
;
286 // packet_size_ is mutable because it's just a cache of the current size.
287 // packet_size should never be read directly, use PacketSize() instead.
288 mutable size_t packet_size_
;
289 mutable size_t max_plaintext_size_
;
290 QuicFrames queued_frames_
;
291 scoped_ptr
<RetransmittableFrames
> queued_retransmittable_frames_
;
293 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator
);
298 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_