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 packet number length to use in future packets as soon as it
66 // can be safely changed.
67 void UpdatePacketNumberLength(QuicPacketNumber least_packet_awaited_by_peer
,
68 QuicPacketCount max_packets_in_flight
);
70 // The overhead the framing will add for a packet with one frame.
71 static size_t StreamFramePacketOverhead(
72 QuicConnectionIdLength connection_id_length
,
74 QuicPacketNumberLength packet_number_length
,
75 QuicStreamOffset offset
,
76 InFecGroup is_in_fec_group
);
78 bool HasRoomForStreamFrame(QuicStreamId id
, QuicStreamOffset offset
) const;
80 // Converts a raw payload to a frame which fits into the currently open
81 // packet. The payload begins at |iov_offset| into the |iov|.
82 // 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
,
87 const QuicIOVector
& iov
,
89 QuicStreamOffset offset
,
92 scoped_ptr
<char[]>* buffer
);
94 // Serializes all frames into a single packet. All frames must fit into a
95 // single packet. Also, sets the entropy hash of the serialized packet to a
96 // random bool and returns that value as a member of SerializedPacket.
97 // Never returns a RetransmittableFrames in SerializedPacket.
98 SerializedPacket
SerializeAllFrames(const QuicFrames
& frames
,
102 // Re-serializes frames with the original packet's packet number length.
103 // Used for retransmitting packets to ensure they aren't too long.
104 // Caller must ensure that any open FEC group is closed before calling this
106 SerializedPacket
ReserializeAllFrames(const RetransmittableFrames
& frames
,
107 QuicPacketNumberLength 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 // Identical to AddSavedFrame, but takes ownership of the buffer if it returns
159 // true, and allows to cause the packet to be padded.
160 bool AddPaddedSavedFrame(const QuicFrame
& frame
, char* buffer
);
162 // Serializes all frames which have been added and adds any which should be
163 // retransmitted to |retransmittable_frames| if it's not nullptr. All frames
164 // must fit into a single packet. Sets the entropy hash of the serialized
165 // packet to a random bool and returns that value as a member of
166 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to
167 // the corresponding RetransmittableFrames if any frames are to be
169 // Fails if |buffer_len| isn't long enough for the encrypted packet.
170 SerializedPacket
SerializePacket(char* encrypted_buffer
, size_t buffer_len
);
172 // Packetize FEC data. All frames must fit into a single packet. Also, sets
173 // the entropy hash of the serialized packet to a random bool and returns
174 // that value as a member of SerializedPacket.
175 // Fails if |buffer_len| isn't long enough for the encrypted packet.
176 SerializedPacket
SerializeFec(char* buffer
, size_t buffer_len
);
178 // Creates a version negotiation packet which supports |supported_versions|.
179 // Caller owns the created packet. Also, sets the entropy hash of the
180 // serialized packet to a random bool and returns that value as a member of
182 QuicEncryptedPacket
* SerializeVersionNegotiationPacket(
183 const QuicVersionVector
& supported_versions
);
185 // Returns a dummy packet that is valid but contains no useful information.
186 static SerializedPacket
NoPacket();
188 // Sets the encryption level that will be applied to new packets.
189 void set_encryption_level(EncryptionLevel level
) {
190 encryption_level_
= level
;
193 // packet number of the last created packet, or 0 if no packets have been
195 QuicPacketNumber
packet_number() const { return packet_number_
; }
197 QuicConnectionIdLength
connection_id_length() const {
198 return connection_id_length_
;
201 void set_connection_id_length(QuicConnectionIdLength length
) {
202 connection_id_length_
= length
;
205 QuicByteCount
max_packet_length() const {
206 return max_packet_length_
;
209 // Sets the encrypter to use for the encryption level and updates the max
211 void SetEncrypter(EncryptionLevel level
, QuicEncrypter
* encrypter
);
213 // Indicates whether the packet creator is in a state where it can change
214 // current maximum packet length.
215 bool CanSetMaxPacketLength() const;
217 // Sets the maximum packet length.
218 void SetMaxPacketLength(QuicByteCount length
);
220 // Returns current max number of packets covered by an FEC group.
221 size_t max_packets_per_fec_group() const {
222 return max_packets_per_fec_group_
;
225 // Sets creator's max number of packets covered by an FEC group.
226 // Note: While there are no constraints on |max_packets_per_fec_group|,
227 // this setter enforces a min value of kLowestMaxPacketsPerFecGroup.
228 // To turn off FEC protection, use StopFecProtectingPackets().
229 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group
);
231 // Returns the currently open FEC group's number. If there isn't an open FEC
232 // group, returns the last closed FEC group number. Returns 0 when FEC is
233 // disabled or no FEC group has been created yet.
234 QuicFecGroupNumber
fec_group_number() { return fec_group_number_
; }
237 friend class test::QuicPacketCreatorPeer
;
239 static bool ShouldRetransmit(const QuicFrame
& frame
);
241 // Copies |length| bytes from iov starting at offset |iov_offset| into buffer.
242 // |iov| must be at least iov_offset+length total length and buffer must be
243 // at least |length| long.
244 static void CopyToBuffer(const QuicIOVector
& iov
,
249 // Updates lengths and also starts an FEC group if FEC protection is on and
250 // there is not already an FEC group open.
251 InFecGroup
MaybeUpdateLengthsAndStartFec();
253 // Called when a data packet is constructed that is part of an FEC group.
254 // |payload| is the non-encrypted FEC protected payload of the packet.
255 void OnBuiltFecProtectedPayload(const QuicPacketHeader
& header
,
256 base::StringPiece payload
);
258 void FillPacketHeader(QuicFecGroupNumber fec_group
,
260 QuicPacketHeader
* header
);
262 // Allows a frame to be added without creating retransmittable frames.
263 // Particularly useful for retransmits using SerializeAllFrames().
264 bool AddFrame(const QuicFrame
& frame
,
265 bool save_retransmittable_frames
,
269 // Adds a padding frame to the current packet only if the current packet
270 // contains a handshake message, and there is sufficient room to fit a
272 void MaybeAddPadding();
274 QuicConnectionId connection_id_
;
275 EncryptionLevel encryption_level_
;
277 scoped_ptr
<QuicRandomBoolSource
> random_bool_source_
;
278 QuicPacketNumber packet_number_
;
279 // If true, any created packets will be FEC protected.
280 bool should_fec_protect_
;
281 QuicFecGroupNumber fec_group_number_
;
282 scoped_ptr
<QuicFecGroup
> fec_group_
;
283 // Controls whether protocol version should be included while serializing the
285 bool send_version_in_packet_
;
286 // Maximum length including headers and encryption (UDP payload length.)
287 QuicByteCount max_packet_length_
;
288 // 0 indicates FEC is disabled.
289 size_t max_packets_per_fec_group_
;
290 // Length of connection_id to send over the wire.
291 QuicConnectionIdLength connection_id_length_
;
292 // Staging variable to hold next packet number length. When sequence
293 // number length is to be changed, this variable holds the new length until
294 // a packet or FEC group boundary, when the creator's packet_number_length_
295 // can be changed to this new value.
296 QuicPacketNumberLength next_packet_number_length_
;
297 // packet number length for the current packet and for the current FEC group
298 // when FEC is enabled. Mutable so PacketSize() can adjust it when the packet
300 mutable QuicPacketNumberLength packet_number_length_
;
301 // packet_size_ is mutable because it's just a cache of the current size.
302 // packet_size should never be read directly, use PacketSize() instead.
303 mutable size_t packet_size_
;
304 mutable size_t max_plaintext_size_
;
305 QuicFrames queued_frames_
;
306 scoped_ptr
<RetransmittableFrames
> queued_retransmittable_frames_
;
307 // If true, the packet will be padded up to |max_packet_length_|.
310 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator
);
315 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_