quic_packet_creator.h revision d57369da7c6519fef57db42085f7b42d4c8845c1
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// 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.
8
9#ifndef NET_QUIC_QUIC_PACKET_CREATOR_H_
10#define NET_QUIC_QUIC_PACKET_CREATOR_H_
11
12#include <utility>
13#include <vector>
14
15#include "base/memory/scoped_ptr.h"
16#include "base/strings/string_piece.h"
17#include "net/quic/quic_fec_group.h"
18#include "net/quic/quic_framer.h"
19#include "net/quic/quic_protocol.h"
20
21namespace net {
22namespace test {
23class QuicPacketCreatorPeer;
24}
25
26class QuicAckNotifier;
27class QuicRandom;
28class QuicRandomBoolSource;
29
30class NET_EXPORT_PRIVATE QuicPacketCreator : public QuicFecBuilderInterface {
31 public:
32  // Options for controlling how packets are created.
33  struct Options {
34    Options()
35        : max_packet_length(kDefaultMaxPacketSize),
36          max_packets_per_fec_group(0),
37          send_guid_length(PACKET_8BYTE_GUID),
38          send_sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER) {}
39
40    size_t max_packet_length;
41    // 0 indicates fec is disabled.
42    size_t max_packets_per_fec_group;
43    // Length of guid to send over the wire.
44    QuicGuidLength send_guid_length;
45    QuicSequenceNumberLength send_sequence_number_length;
46  };
47
48  // QuicRandom* required for packet entropy.
49  QuicPacketCreator(QuicGuid guid,
50                    QuicFramer* framer,
51                    QuicRandom* random_generator,
52                    bool is_server);
53
54  virtual ~QuicPacketCreator();
55
56  // QuicFecBuilderInterface
57  virtual void OnBuiltFecProtectedPayload(const QuicPacketHeader& header,
58                                          base::StringPiece payload) OVERRIDE;
59
60  // Checks if it's time to send an FEC packet.  |force_close| forces this to
61  // return true if an fec group is open.
62  bool ShouldSendFec(bool force_close) const;
63
64  // Makes the framer not serialize the protocol version in sent packets.
65  void StopSendingVersion();
66
67  // Update the sequence number length to use in future packets as soon as it
68  // can be safely changed.
69  void UpdateSequenceNumberLength(
70      QuicPacketSequenceNumber least_packet_awaited_by_peer,
71      QuicByteCount bytes_per_second);
72
73  // The overhead the framing will add for a packet with one frame.
74  static size_t StreamFramePacketOverhead(
75      QuicVersion version,
76      QuicGuidLength guid_length,
77      bool include_version,
78      QuicSequenceNumberLength sequence_number_length,
79      InFecGroup is_in_fec_group);
80
81  bool HasRoomForStreamFrame(QuicStreamId id, QuicStreamOffset offset) const;
82
83  // Converts a raw payload to a frame which fits into the currently open
84  // packet if there is one.  Returns the number of bytes consumed from data.
85  // If data is empty and fin is true, the expected behavior is to consume the
86  // fin but return 0.
87  size_t CreateStreamFrame(QuicStreamId id,
88                           const IOVector& data,
89                           QuicStreamOffset offset,
90                           bool fin,
91                           QuicFrame* frame);
92
93  // As above, but keeps track of an QuicAckNotifier that should be called when
94  // the packet that contains this stream frame is ACKed.
95  // The |notifier| is not owned by the QuicPacketGenerator and must outlive the
96  // generated packet.
97  size_t CreateStreamFrameWithNotifier(QuicStreamId id,
98                                       const IOVector& data,
99                                       QuicStreamOffset offset,
100                                       bool fin,
101                                       QuicAckNotifier* notifier,
102                                       QuicFrame* frame);
103
104  // Serializes all frames into a single packet. All frames must fit into a
105  // single packet. Also, sets the entropy hash of the serialized packet to a
106  // random bool and returns that value as a member of SerializedPacket.
107  // Never returns a RetransmittableFrames in SerializedPacket.
108  SerializedPacket SerializeAllFrames(const QuicFrames& frames);
109
110  // Re-serializes frames with the original packet's sequence number length.
111  // Used for retransmitting packets to ensure they aren't too long.
112  SerializedPacket ReserializeAllFrames(
113      const QuicFrames& frames, QuicSequenceNumberLength original_length);
114
115  // Returns true if there are frames pending to be serialized.
116  bool HasPendingFrames();
117
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;
123
124  // Returns the number of bytes in the current packet, including the header,
125  // if serialized with the current frames.  Adding a frame to the packet
126  // may change the serialized length of existing frames, as per the comment
127  // in BytesFree.
128  size_t PacketSize() const;
129
130  // TODO(jri): AddSavedFrame calls AddFrame, which only saves the frame
131  // if it is a stream frame, not other types of frames. Fix this API;
132  // add a AddNonSavedFrame method.
133  // Adds |frame| to the packet creator's list of frames to be serialized.
134  // Returns false if the frame doesn't fit into the current packet.
135  bool AddSavedFrame(const QuicFrame& frame);
136
137  // Serializes all frames which have been added and adds any which should be
138  // retransmitted to |retransmittable_frames| if it's not NULL. All frames must
139  // fit into a single packet. Sets the entropy hash of the serialized
140  // packet to a random bool and returns that value as a member of
141  // SerializedPacket. Also, sets |serialized_frames| in the SerializedPacket
142  // to the corresponding RetransmittableFrames if any frames are to be
143  // retransmitted.
144  SerializedPacket SerializePacket();
145
146  // Packetize FEC data. All frames must fit into a single packet. Also, sets
147  // the entropy hash of the serialized packet to a random bool and returns
148  // that value as a member of SerializedPacket.
149  SerializedPacket SerializeFec();
150
151  // Creates a packet with connection close frame. Caller owns the created
152  // packet. Also, sets the entropy hash of the serialized packet to a random
153  // bool and returns that value as a member of SerializedPacket.
154  SerializedPacket SerializeConnectionClose(
155      QuicConnectionCloseFrame* close_frame);
156
157  // Creates a version negotiation packet which supports |supported_versions|.
158  // Caller owns the created  packet. Also, sets the entropy hash of the
159  // serialized packet to a random bool and returns that value as a member of
160  // SerializedPacket.
161  QuicEncryptedPacket* SerializeVersionNegotiationPacket(
162      const QuicVersionVector& supported_versions);
163
164  // Sequence number of the last created packet, or 0 if no packets have been
165  // created.
166  QuicPacketSequenceNumber sequence_number() const {
167    return sequence_number_;
168  }
169
170  void set_sequence_number(QuicPacketSequenceNumber s) {
171    sequence_number_ = s;
172  }
173
174  Options* options() {
175    return &options_;
176  }
177
178 private:
179  friend class test::QuicPacketCreatorPeer;
180
181  static bool ShouldRetransmit(const QuicFrame& frame);
182
183  // Starts a new FEC group with the next serialized packet, if FEC is enabled
184  // and there is not already an FEC group open.
185  void MaybeStartFEC();
186
187  void FillPacketHeader(QuicFecGroupNumber fec_group,
188                        bool fec_flag,
189                        bool fec_entropy_flag,
190                        QuicPacketHeader* header);
191
192  // Allows a frame to be added without creating retransmittable frames.
193  // Particularly useful for retransmits using SerializeAllFrames().
194  bool AddFrame(const QuicFrame& frame, bool save_retransmittable_frames);
195
196  // Adds a padding frame to the current packet only if the current packet
197  // contains a handshake message, and there is sufficient room to fit a
198  // padding frame.
199  void MaybeAddPadding();
200
201  Options options_;
202  QuicGuid guid_;
203  QuicFramer* framer_;
204  scoped_ptr<QuicRandomBoolSource> random_bool_source_;
205  QuicPacketSequenceNumber sequence_number_;
206  QuicFecGroupNumber fec_group_number_;
207  scoped_ptr<QuicFecGroup> fec_group_;
208  // bool to keep track if this packet creator is being used the server.
209  bool is_server_;
210  // Controls whether protocol version should be included while serializing the
211  // packet.
212  bool send_version_in_packet_;
213  // The sequence number length for the current packet and the current FEC group
214  // if FEC is enabled.
215  // Mutable so PacketSize() can adjust it when the packet is empty.
216  mutable QuicSequenceNumberLength sequence_number_length_;
217  // packet_size_ is mutable because it's just a cache of the current size.
218  // packet_size should never be read directly, use PacketSize() instead.
219  mutable size_t packet_size_;
220  QuicFrames queued_frames_;
221  scoped_ptr<RetransmittableFrames> queued_retransmittable_frames_;
222
223  DISALLOW_COPY_AND_ASSIGN(QuicPacketCreator);
224};
225
226}  // namespace net
227
228#endif  // NET_QUIC_QUIC_PACKET_CREATOR_H_
229