Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / quic / quic_packet_generator.h
blob4f738bda3cf5f55717defe77b8aa0e0a7d065995
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.
4 //
5 // Responsible for generating packets on behalf of a QuicConnection.
6 // Packets are serialized just-in-time. Control frames are queued.
7 // Ack and Feedback frames will be requested from the Connection
8 // just-in-time. When a packet needs to be sent, the Generator
9 // will serialize a packet and pass it to QuicConnection::SendOrQueuePacket()
11 // The Generator's mode of operation is controlled by two conditions:
13 // 1) Is the Delegate writable?
15 // If the Delegate is not writable, then no operations will cause
16 // a packet to be serialized. In particular:
17 // * SetShouldSendAck will simply record that an ack is to be sent.
18 // * AddControlFrame will enqueue the control frame.
19 // * ConsumeData will do nothing.
21 // If the Delegate is writable, then the behavior depends on the second
22 // condition:
24 // 2) Is the Generator in batch mode?
26 // If the Generator is NOT in batch mode, then each call to a write
27 // operation will serialize one or more packets. The contents will
28 // include any previous queued frames. If an ack should be sent
29 // but has not been sent, then the Delegate will be asked to create
30 // an Ack frame which will then be included in the packet. When
31 // the write call completes, the current packet will be serialized
32 // and sent to the Delegate, even if it is not full.
34 // If the Generator is in batch mode, then each write operation will
35 // add data to the "current" packet. When the current packet becomes
36 // full, it will be serialized and sent to the packet. When batch
37 // mode is ended via |FinishBatchOperations|, the current packet
38 // will be serialzied, even if it is not full.
40 // FEC behavior also depends on batch mode. In batch mode, FEC packets
41 // will be sent after |max_packets_per_group| have been sent, as well
42 // as after batch operations are complete. When not in batch mode,
43 // an FEC packet will be sent after each write call completes.
45 // TODO(rch): This behavior should probably be tuned. When not in batch
46 // mode, we should probably set a timer so that several independent
47 // operations can be grouped into the same FEC group.
49 // When an FEC packet is generated, it will be send to the Delegate,
50 // even if the Delegate has become unwritable after handling the
51 // data packet immediately proceeding the FEC packet.
53 #ifndef NET_QUIC_QUIC_PACKET_GENERATOR_H_
54 #define NET_QUIC_QUIC_PACKET_GENERATOR_H_
56 #include <list>
58 #include "net/quic/quic_ack_notifier.h"
59 #include "net/quic/quic_packet_creator.h"
60 #include "net/quic/quic_sent_packet_manager.h"
61 #include "net/quic/quic_types.h"
63 namespace net {
65 namespace test {
66 class QuicPacketGeneratorPeer;
67 } // namespace test
69 class NET_EXPORT_PRIVATE QuicPacketGenerator {
70 public:
71 class NET_EXPORT_PRIVATE DelegateInterface {
72 public:
73 virtual ~DelegateInterface() {}
74 virtual bool ShouldGeneratePacket(HasRetransmittableData retransmittable,
75 IsHandshake handshake) = 0;
76 virtual void PopulateAckFrame(QuicAckFrame* ack) = 0;
77 virtual void PopulateStopWaitingFrame(
78 QuicStopWaitingFrame* stop_waiting) = 0;
79 // Takes ownership of |packet.packet| and |packet.retransmittable_frames|.
80 virtual void OnSerializedPacket(const SerializedPacket& packet) = 0;
81 virtual void CloseConnection(QuicErrorCode error, bool from_peer) = 0;
82 // Called when a FEC Group is reset (closed).
83 virtual void OnResetFecGroup() = 0;
86 // Interface which gets callbacks from the QuicPacketGenerator at interesting
87 // points. Implementations must not mutate the state of the generator
88 // as a result of these callbacks.
89 class NET_EXPORT_PRIVATE DebugDelegate {
90 public:
91 virtual ~DebugDelegate() {}
93 // Called when a frame has been added to the current packet.
94 virtual void OnFrameAddedToPacket(const QuicFrame& frame) {}
97 QuicPacketGenerator(QuicConnectionId connection_id,
98 QuicFramer* framer,
99 QuicRandom* random_generator,
100 DelegateInterface* delegate);
102 virtual ~QuicPacketGenerator();
104 // Called by the connection in the event of the congestion window changing.
105 void OnCongestionWindowChange(QuicPacketCount max_packets_in_flight);
107 // Called by the connection when the RTT may have changed.
108 void OnRttChange(QuicTime::Delta rtt);
110 // Indicates that an ACK frame should be sent.
111 // If |also_send_stop_waiting| is true, then it also indicates that a
112 // STOP_WAITING frame should be sent as well.
113 // The contents of the frame(s) will be generated via a call to the delegate
114 // CreateAckFrame() when the packet is serialized.
115 void SetShouldSendAck(bool also_send_stop_waiting);
117 void AddControlFrame(const QuicFrame& frame);
119 // Given some data, may consume part or all of it and pass it to the
120 // packet creator to be serialized into packets. If not in batch
121 // mode, these packets will also be sent during this call.
122 // |delegate| (if not nullptr) will be informed once all packets sent as a
123 // result of this call are ACKed by the peer.
124 QuicConsumedData ConsumeData(QuicStreamId id,
125 const QuicIOVector& iov,
126 QuicStreamOffset offset,
127 bool fin,
128 FecProtection fec_protection,
129 QuicAckNotifier::DelegateInterface* delegate);
131 // Generates an MTU discovery packet of specified size.
132 void GenerateMtuDiscoveryPacket(QuicByteCount target_mtu,
133 QuicAckNotifier::DelegateInterface* delegate);
135 // Indicates whether batch mode is currently enabled.
136 bool InBatchMode();
137 // Disables flushing.
138 void StartBatchOperations();
139 // Enables flushing and flushes queued data which can be sent.
140 void FinishBatchOperations();
142 // Flushes all queued frames, even frames which are not sendable.
143 void FlushAllQueuedFrames();
145 bool HasQueuedFrames() const;
147 // Makes the framer not serialize the protocol version in sent packets.
148 void StopSendingVersion();
150 // Creates a version negotiation packet which supports |supported_versions|.
151 // Caller owns the created packet. Also, sets the entropy hash of the
152 // serialized packet to a random bool and returns that value as a member of
153 // SerializedPacket.
154 QuicEncryptedPacket* SerializeVersionNegotiationPacket(
155 const QuicVersionVector& supported_versions);
157 // Re-serializes frames with the original packet's packet number length.
158 // Used for retransmitting packets to ensure they aren't too long.
159 // Caller must ensure that any open FEC group is closed before calling this
160 // method.
161 SerializedPacket ReserializeAllFrames(const RetransmittableFrames& frames,
162 QuicPacketNumberLength original_length,
163 char* buffer,
164 size_t buffer_len);
166 // Update the packet number length to use in future packets as soon as it
167 // can be safely changed.
168 void UpdateSequenceNumberLength(QuicPacketNumber least_packet_awaited_by_peer,
169 QuicPacketCount max_packets_in_flight);
171 // Set the minimum number of bytes for the connection id length;
172 void SetConnectionIdLength(uint32 length);
174 // Called when the FEC alarm fires.
175 void OnFecTimeout();
177 // Called after sending |packet_number| to determine whether an FEC alarm
178 // should be set for sending out an FEC packet. Returns a positive and finite
179 // timeout if an FEC alarm should be set, and infinite if no alarm should be
180 // set. OnFecTimeout should be called to send the FEC packet when the alarm
181 // fires.
182 QuicTime::Delta GetFecTimeout(QuicPacketNumber packet_number);
184 // Sets the encrypter to use for the encryption level.
185 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter);
187 // Sets the encryption level that will be applied to new packets.
188 void set_encryption_level(EncryptionLevel level);
190 // packet number of the last created packet, or 0 if no packets have been
191 // created.
192 QuicPacketNumber packet_number() const;
194 // Returns the maximum packet length. Note that this is the long-term maximum
195 // packet length, and it may not be the maximum length of the current packet,
196 // if the generator is in the middle of the packet (in batch mode) or FEC
197 // group.
198 QuicByteCount GetMaxPacketLength() const;
199 // Returns the maximum length current packet can actually have.
200 QuicByteCount GetCurrentMaxPacketLength() const;
202 // Set maximum packet length sent. If |force| is set to true, all pending
203 // unfinished packets and FEC groups are closed, and the change is enacted
204 // immediately. Otherwise, it is enacted at the next opportunity.
205 void SetMaxPacketLength(QuicByteCount length, bool force);
207 void set_debug_delegate(DebugDelegate* debug_delegate) {
208 debug_delegate_ = debug_delegate;
211 // TODO(rtenneti): Delete this code after the 0.25 RTT FEC experiment.
212 float rtt_multiplier_for_fec_timeout() {
213 return rtt_multiplier_for_fec_timeout_;
215 void set_rtt_multiplier_for_fec_timeout(
216 float rtt_multiplier_for_fec_timeout) {
217 rtt_multiplier_for_fec_timeout_ = rtt_multiplier_for_fec_timeout;
220 FecSendPolicy fec_send_policy() { return fec_send_policy_; }
221 void set_fec_send_policy(FecSendPolicy fec_send_policy) {
222 fec_send_policy_ = fec_send_policy;
225 private:
226 friend class test::QuicPacketGeneratorPeer;
228 // Turn on FEC protection for subsequent packets in the generator.
229 // If no FEC group is currently open in the creator, this method flushes any
230 // queued frames in the generator and in the creator, and it then turns FEC on
231 // in the creator. This method may be called with an open FEC group in the
232 // creator, in which case, only the generator's state is altered.
233 void MaybeStartFecProtection();
235 // Serializes and calls the delegate on an FEC packet if one was under
236 // construction in the creator. When |force| is false, it relies on the
237 // creator being ready to send an FEC packet, otherwise FEC packet is sent
238 // as long as one is under construction in the creator. Also tries to turn
239 // off FEC protection in the creator if it's off in the generator.
240 // When |fec_send_policy_| is FEC_SEND_QUIESCENCE, then send FEC
241 // packet if |is_fec_timeout| is true otherwise close the FEC group.
242 void MaybeSendFecPacketAndCloseGroup(bool force, bool is_fec_timeout);
244 // Returns true if an FEC packet should be generated based on |force| and
245 // current state of the generator and the creator.
246 bool ShouldSendFecPacket(bool force);
248 // Resets (closes) the FEC group and calls the Delegate's OnResetFecGroup.
249 // Asserts that FEC group is open.
250 void ResetFecGroup();
252 void SendQueuedFrames(bool flush, bool is_fec_timeout);
254 // Test to see if we have pending ack, or control frames.
255 bool HasPendingFrames() const;
256 // Test to see if the addition of a pending frame (which might be
257 // retransmittable) would still allow the resulting packet to be sent now.
258 bool CanSendWithNextPendingFrameAddition() const;
259 // Add exactly one pending frame, preferring ack frames over control frames.
260 bool AddNextPendingFrame();
261 // Adds a frame and takes ownership of the underlying buffer if the addition
262 // was successful.
263 bool AddFrame(const QuicFrame& frame, char* buffer, bool needs_padding);
265 void SerializeAndSendPacket();
267 DelegateInterface* delegate_;
268 DebugDelegate* debug_delegate_;
270 QuicPacketCreator packet_creator_;
271 QuicFrames queued_control_frames_;
273 // True if batch mode is currently enabled.
274 bool batch_mode_;
276 // Timeout used for FEC alarm. Can be set to zero initially or if the SRTT has
277 // not yet been set.
278 QuicTime::Delta fec_timeout_;
280 // The multiplication factor for FEC timeout based on RTT.
281 // TODO(rtenneti): Delete this code after the 0.25 RTT FEC experiment.
282 float rtt_multiplier_for_fec_timeout_;
284 // True if FEC protection is on. The creator may have an open FEC group even
285 // if this variable is false.
286 bool should_fec_protect_;
288 // FEC policy that specifies when to send FEC packet.
289 FecSendPolicy fec_send_policy_;
291 // Flags to indicate the need for just-in-time construction of a frame.
292 bool should_send_ack_;
293 bool should_send_stop_waiting_;
294 // If we put a non-retransmittable frame in this packet, then we have to hold
295 // a reference to it until we flush (and serialize it). Retransmittable frames
296 // are referenced elsewhere so that they can later be (optionally)
297 // retransmitted.
298 QuicAckFrame pending_ack_frame_;
299 QuicStopWaitingFrame pending_stop_waiting_frame_;
300 // True if an ack or stop waiting frame is already queued, and should not be
301 // re-added.
302 bool ack_queued_;
303 bool stop_waiting_queued_;
305 // Stores notifiers that should be attached to the next serialized packet.
306 std::list<QuicAckNotifier*> ack_notifiers_;
308 // Stores the maximum packet size we are allowed to send. This might not be
309 // the maximum size we are actually using now, if we are in the middle of the
310 // packet.
311 QuicByteCount max_packet_length_;
313 DISALLOW_COPY_AND_ASSIGN(QuicPacketGenerator);
316 } // namespace net
318 #endif // NET_QUIC_QUIC_PACKET_GENERATOR_H_