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 // Returns true if an FEC packet is under construction.
56 bool IsFecGroupOpen() const;
58 // Makes the framer not serialize the protocol version in sent packets.
59 void StopSendingVersion();
61 // Update the sequence number length to use in future packets as soon as it
62 // can be safely changed.
63 void UpdateSequenceNumberLength(
64 QuicPacketSequenceNumber least_packet_awaited_by_peer
,
65 QuicPacketCount max_packets_in_flight
);
67 // The overhead the framing will add for a packet with one frame.
68 static size_t StreamFramePacketOverhead(
69 QuicConnectionIdLength connection_id_length
,
71 QuicSequenceNumberLength sequence_number_length
,
72 QuicStreamOffset offset
,
73 InFecGroup is_in_fec_group
);
75 bool HasRoomForStreamFrame(QuicStreamId id
, QuicStreamOffset offset
) const;
77 // Converts a raw payload to a frame which fits into the currently open
78 // packet if there is one. Returns the number of bytes consumed from data.
79 // If data is empty and fin is true, the expected behavior is to consume the
81 size_t CreateStreamFrame(QuicStreamId id
,
83 QuicStreamOffset offset
,
87 // Serializes all frames into a single packet. All frames must fit into a
88 // single packet. Also, sets the entropy hash of the serialized packet to a
89 // random bool and returns that value as a member of SerializedPacket.
90 // Never returns a RetransmittableFrames in SerializedPacket.
91 SerializedPacket
SerializeAllFrames(const QuicFrames
& frames
);
93 // Re-serializes frames with the original packet's sequence number length.
94 // Used for retransmitting packets to ensure they aren't too long.
95 // Caller must ensure that any open FEC group is closed before calling this
97 SerializedPacket
ReserializeAllFrames(
98 const RetransmittableFrames
& frames
,
99 QuicSequenceNumberLength original_length
);
101 // Returns true if there are frames pending to be serialized.
102 bool HasPendingFrames() const;
104 // Returns true if there are retransmittable frames pending to be serialized.
105 bool HasPendingRetransmittableFrames() const;
107 // TODO(jri): Remove this method.
108 // Returns whether FEC protection is currently enabled. Note: Enabled does not
109 // mean that an FEC group is currently active; i.e., IsFecProtected() may
110 // still return false.
111 bool IsFecEnabled() const;
113 // Returns true if subsequent packets will be FEC protected. Note: True does
114 // not mean that an FEC packet is currently under construction; i.e.,
115 // fec_group_.get() may still be nullptr, until MaybeStartFec() is called.
116 bool IsFecProtected() const;
118 // Returns the number of bytes which are available to be used by additional
119 // frames in the packet. Since stream frames are slightly smaller when they
120 // are the last frame in a packet, this method will return a different
121 // value than max_packet_size - PacketSize(), in this case.
122 size_t BytesFree() const;
124 // Returns the number of bytes that the packet will expand by if a new frame
125 // is added to the packet. If the last frame was a stream frame, it will
126 // expand slightly when a new frame is added, and this method returns the
127 // amount of expected expansion. If the packet is in an FEC group, no
128 // expansion happens and this method always returns zero.
129 size_t ExpansionOnNewFrame() const;
131 // Returns the number of bytes in the current packet, including the header,
132 // if serialized with the current frames. Adding a frame to the packet
133 // may change the serialized length of existing frames, as per the comment
135 size_t PacketSize() const;
137 // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
138 // if it is a stream frame, not other types of frames. Fix this API;
139 // add a AddNonSavedFrame method.
140 // Adds |frame| to the packet creator's list of frames to be serialized.
141 // Returns false if the frame doesn't fit into the current packet.
142 bool AddSavedFrame(const QuicFrame
& frame
);
144 // Serializes all frames which have been added and adds any which should be
145 // retransmitted to |retransmittable_frames| if it's not nullptr. All frames
146 // must fit into a single packet. Sets the entropy hash of the serialized
147 // packet to a random bool and returns that value as a member of
148 // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket to
149 // the corresponding RetransmittableFrames if any frames are to be
151 SerializedPacket
SerializePacket();
153 // Packetize FEC data. All frames must fit into a single packet. Also, sets
154 // the entropy hash of the serialized packet to a random bool and returns
155 // that value as a member of SerializedPacket.
156 SerializedPacket
SerializeFec();
158 // Creates a version negotiation packet which supports |supported_versions|.
159 // Caller owns the created packet. Also, sets the entropy hash of the
160 // serialized packet to a random bool and returns that value as a member of
162 QuicEncryptedPacket
* SerializeVersionNegotiationPacket(
163 const QuicVersionVector
& supported_versions
);
165 // Returns a dummy packet that is valid but contains no useful information.
166 static SerializedPacket
NoPacket();
168 // Sets the encryption level that will be applied to new packets.
169 void set_encryption_level(EncryptionLevel level
) {
170 encryption_level_
= level
;
173 // Sequence number of the last created packet, or 0 if no packets have been
175 QuicPacketSequenceNumber
sequence_number() const {
176 return sequence_number_
;
179 QuicConnectionIdLength
connection_id_length() const {
180 return connection_id_length_
;
183 void set_connection_id_length(QuicConnectionIdLength length
) {
184 connection_id_length_
= length
;
187 QuicByteCount
max_packet_length() const {
188 return max_packet_length_
;
191 // Sets the encrypter to use for the encryption level and updates the max
193 void SetEncrypter(EncryptionLevel level
, QuicEncrypter
* encrypter
);
195 // Sets the maximum packet length.
196 void SetMaxPacketLength(QuicByteCount length
);
198 // Returns current max number of packets covered by an FEC group.
199 size_t max_packets_per_fec_group() const {
200 return max_packets_per_fec_group_
;
203 // Sets creator's max number of packets covered by an FEC group.
204 // Note: While there are no constraints on |max_packets_per_fec_group|,
205 // this setter enforces a min value of kLowestMaxPacketsPerFecGroup.
206 // To turn off FEC protection, use StopFecProtectingPackets().
207 void set_max_packets_per_fec_group(size_t max_packets_per_fec_group
);
209 // Returns the currently open FEC group's number. If there isn't an open FEC
210 // group, returns the last closed FEC group number. Returns 0 when FEC is
211 // disabled or no FEC group has been created yet.
212 QuicFecGroupNumber
fec_group_number() { return fec_group_number_
; }
215 friend class test::QuicPacketCreatorPeer
;
217 static bool ShouldRetransmit(const QuicFrame
& frame
);
219 // Updates lengths and also starts an FEC group if FEC protection is on and
220 // there is not already an FEC group open.
221 InFecGroup
MaybeUpdateLengthsAndStartFec();
223 // Called when a data packet is constructed that is part of an FEC group.
224 // |payload| is the non-encrypted FEC protected payload of the packet.
225 void OnBuiltFecProtectedPayload(const QuicPacketHeader
& header
,
226 base::StringPiece payload
);
228 void FillPacketHeader(QuicFecGroupNumber fec_group
,
230 QuicPacketHeader
* header
);
232 // Allows a frame to be added without creating retransmittable frames.
233 // Particularly useful for retransmits using SerializeAllFrames().
234 bool AddFrame(const QuicFrame
& frame
, bool save_retransmittable_frames
);
236 // Adds a padding frame to the current packet only if the current packet
237 // contains a handshake message, and there is sufficient room to fit a
239 void MaybeAddPadding();
241 QuicConnectionId connection_id_
;
242 EncryptionLevel encryption_level_
;
244 scoped_ptr
<QuicRandomBoolSource
> random_bool_source_
;
245 QuicPacketSequenceNumber sequence_number_
;
246 // If true, any created packets will be FEC protected.
247 bool should_fec_protect_
;
248 QuicFecGroupNumber fec_group_number_
;
249 scoped_ptr
<QuicFecGroup
> fec_group_
;
250 // Controls whether protocol version should be included while serializing the
252 bool send_version_in_packet_
;
253 // Maximum length including headers and encryption (UDP payload length.)
254 QuicByteCount max_packet_length_
;
255 // 0 indicates FEC is disabled.
256 size_t max_packets_per_fec_group_
;
257 // Length of connection_id to send over the wire.
258 QuicConnectionIdLength connection_id_length_
;
259 // Staging variable to hold next packet sequence number length. When sequence
260 // number length is to be changed, this variable holds the new length until
261 // a packet or FEC group boundary, when the creator's sequence_number_length_
262 // can be changed to this new value.
263 QuicSequenceNumberLength next_sequence_number_length_
;
264 // Sequence number length for the current packet and for the current FEC group
265 // when FEC is enabled. Mutable so PacketSize() can adjust it when the packet
267 mutable QuicSequenceNumberLength sequence_number_length_
;
268 // packet_size_ is mutable because it's just a cache of the current size.
269 // packet_size should never be read directly, use PacketSize() instead.
270 mutable size_t packet_size_
;
271 mutable size_t max_plaintext_size_
;
272 QuicFrames queued_frames_
;
273 scoped_ptr
<RetransmittableFrames
> queued_retransmittable_frames_
;
275 DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator
);
280 #endif // NET_QUIC_QUIC_PACKET_CREATOR_H_