QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 | |
fayang | 4245c21 | 2019-11-05 13:33:46 -0800 | [diff] [blame] | 5 | // Responsible for creating packets on behalf of a QuicConnection. |
| 6 | // Packets are serialized just-in-time. Stream data and control frames will be |
| 7 | // requested from the Connection just-in-time. Frames are accumulated into |
| 8 | // "current" packet until no more frames can fit, then current packet gets |
| 9 | // serialized and passed to connection via OnSerializedPacket(). |
| 10 | // |
| 11 | // Whether a packet should be serialized is determined by whether delegate is |
| 12 | // writable. If the Delegate is not writable, then no operations will cause |
| 13 | // a packet to be serialized. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | |
| 15 | #ifndef QUICHE_QUIC_CORE_QUIC_PACKET_CREATOR_H_ |
| 16 | #define QUICHE_QUIC_CORE_QUIC_PACKET_CREATOR_H_ |
| 17 | |
| 18 | #include <cstddef> |
| 19 | #include <memory> |
| 20 | #include <utility> |
| 21 | #include <vector> |
| 22 | |
renjietang | dbe9834 | 2019-10-18 11:00:57 -0700 | [diff] [blame] | 23 | #include "net/third_party/quiche/src/quic/core/frames/quic_stream_frame.h" |
danzh | bf4836c | 2020-02-11 20:29:15 -0800 | [diff] [blame] | 24 | #include "net/third_party/quiche/src/quic/core/quic_circular_deque.h" |
fayang | 0875083 | 2019-10-24 11:25:34 -0700 | [diff] [blame] | 25 | #include "net/third_party/quiche/src/quic/core/quic_coalesced_packet.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 26 | #include "net/third_party/quiche/src/quic/core/quic_framer.h" |
| 27 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 28 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 29 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 30 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 31 | |
| 32 | namespace quic { |
| 33 | namespace test { |
| 34 | class QuicPacketCreatorPeer; |
| 35 | } |
| 36 | |
| 37 | class QUIC_EXPORT_PRIVATE QuicPacketCreator { |
| 38 | public: |
| 39 | // A delegate interface for further processing serialized packet. |
ianswett | b023c7b | 2019-05-06 12:38:10 -0700 | [diff] [blame] | 40 | class QUIC_EXPORT_PRIVATE DelegateInterface { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 41 | public: |
ianswett | b023c7b | 2019-05-06 12:38:10 -0700 | [diff] [blame] | 42 | virtual ~DelegateInterface() {} |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 43 | // Get a buffer of kMaxOutgoingPacketSize bytes to serialize the next |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 44 | // packet. If the return value's buffer is nullptr, QuicPacketCreator will |
| 45 | // serialize on a stack buffer. |
| 46 | virtual QuicPacketBuffer GetPacketBuffer() = 0; |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 47 | // Called when a packet is serialized. Delegate take the ownership of |
| 48 | // |serialized_packet|. |
| 49 | virtual void OnSerializedPacket(SerializedPacket serialized_packet) = 0; |
ianswett | b023c7b | 2019-05-06 12:38:10 -0700 | [diff] [blame] | 50 | |
| 51 | // Called when an unrecoverable error is encountered. |
| 52 | virtual void OnUnrecoverableError(QuicErrorCode error, |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 53 | const std::string& error_details) = 0; |
fayang | cad1179 | 2019-09-16 13:11:44 -0700 | [diff] [blame] | 54 | |
| 55 | // Consults delegate whether a packet should be generated. |
| 56 | virtual bool ShouldGeneratePacket(HasRetransmittableData retransmittable, |
| 57 | IsHandshake handshake) = 0; |
| 58 | // Called when there is data to be sent. Retrieves updated ACK frame from |
| 59 | // the delegate. |
| 60 | virtual const QuicFrames MaybeBundleAckOpportunistically() = 0; |
fayang | 1504296 | 2020-07-01 12:14:29 -0700 | [diff] [blame] | 61 | |
| 62 | // Returns the packet fate for serialized packets which will be handed over |
| 63 | // to delegate via OnSerializedPacket(). Called when a packet is about to be |
| 64 | // serialized. |
| 65 | virtual SerializedPacketFate GetSerializedPacketFate( |
| 66 | bool is_mtu_discovery, |
| 67 | EncryptionLevel encryption_level) = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | // Interface which gets callbacks from the QuicPacketCreator at interesting |
| 71 | // points. Implementations must not mutate the state of the creator |
| 72 | // as a result of these callbacks. |
| 73 | class QUIC_EXPORT_PRIVATE DebugDelegate { |
| 74 | public: |
| 75 | virtual ~DebugDelegate() {} |
| 76 | |
| 77 | // Called when a frame has been added to the current packet. |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 78 | virtual void OnFrameAddedToPacket(const QuicFrame& /*frame*/) {} |
renjietang | dbe9834 | 2019-10-18 11:00:57 -0700 | [diff] [blame] | 79 | |
| 80 | // Called when a stream frame is coalesced with an existing stream frame. |
| 81 | // |frame| is the new stream frame. |
| 82 | virtual void OnStreamFrameCoalesced(const QuicStreamFrame& /*frame*/) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 83 | }; |
| 84 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 85 | QuicPacketCreator(QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 86 | QuicFramer* framer, |
| 87 | DelegateInterface* delegate); |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 88 | QuicPacketCreator(QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 89 | QuicFramer* framer, |
| 90 | QuicRandom* random, |
| 91 | DelegateInterface* delegate); |
| 92 | QuicPacketCreator(const QuicPacketCreator&) = delete; |
| 93 | QuicPacketCreator& operator=(const QuicPacketCreator&) = delete; |
| 94 | |
| 95 | ~QuicPacketCreator(); |
| 96 | |
| 97 | // Makes the framer not serialize the protocol version in sent packets. |
| 98 | void StopSendingVersion(); |
| 99 | |
| 100 | // SetDiversificationNonce sets the nonce that will be sent in each public |
| 101 | // header of packets encrypted at the initial encryption level. Should only |
| 102 | // be called by servers. |
| 103 | void SetDiversificationNonce(const DiversificationNonce& nonce); |
| 104 | |
| 105 | // Update the packet number length to use in future packets as soon as it |
| 106 | // can be safely changed. |
| 107 | // TODO(fayang): Directly set packet number length instead of compute it in |
| 108 | // creator. |
| 109 | void UpdatePacketNumberLength(QuicPacketNumber least_packet_awaited_by_peer, |
| 110 | QuicPacketCount max_packets_in_flight); |
| 111 | |
fayang | 4c1c236 | 2019-09-13 07:20:01 -0700 | [diff] [blame] | 112 | // Skip |count| packet numbers. |
| 113 | void SkipNPacketNumbers(QuicPacketCount count, |
| 114 | QuicPacketNumber least_packet_awaited_by_peer, |
| 115 | QuicPacketCount max_packets_in_flight); |
| 116 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 117 | // The overhead the framing will add for a packet with one frame. |
| 118 | static size_t StreamFramePacketOverhead( |
| 119 | QuicTransportVersion version, |
| 120 | QuicConnectionIdLength destination_connection_id_length, |
| 121 | QuicConnectionIdLength source_connection_id_length, |
| 122 | bool include_version, |
| 123 | bool include_diversification_nonce, |
| 124 | QuicPacketNumberLength packet_number_length, |
| 125 | QuicVariableLengthIntegerLength retry_token_length_length, |
| 126 | QuicVariableLengthIntegerLength length_length, |
| 127 | QuicStreamOffset offset); |
| 128 | |
| 129 | // Returns false and flushes all pending frames if current open packet is |
| 130 | // full. |
| 131 | // If current packet is not full, creates a stream frame that fits into the |
| 132 | // open packet and adds it to the packet. |
fayang | 62b637b | 2019-09-16 08:40:49 -0700 | [diff] [blame] | 133 | bool ConsumeDataToFillCurrentPacket(QuicStreamId id, |
| 134 | size_t data_size, |
| 135 | QuicStreamOffset offset, |
| 136 | bool fin, |
| 137 | bool needs_full_padding, |
| 138 | TransmissionType transmission_type, |
| 139 | QuicFrame* frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 140 | |
| 141 | // Creates a CRYPTO frame that fits into the current packet (which must be |
| 142 | // empty) and adds it to the packet. |
fayang | 62b637b | 2019-09-16 08:40:49 -0700 | [diff] [blame] | 143 | bool ConsumeCryptoDataToFillCurrentPacket(EncryptionLevel level, |
| 144 | size_t write_length, |
| 145 | QuicStreamOffset offset, |
| 146 | bool needs_full_padding, |
| 147 | TransmissionType transmission_type, |
| 148 | QuicFrame* frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 149 | |
| 150 | // Returns true if current open packet can accommodate more stream frames of |
| 151 | // stream |id| at |offset| and data length |data_size|, false otherwise. |
fayang | 7a06f9b | 2020-06-24 10:23:19 -0700 | [diff] [blame] | 152 | // TODO(fayang): mark this const when deprecating quic_update_packet_size. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 153 | bool HasRoomForStreamFrame(QuicStreamId id, |
| 154 | QuicStreamOffset offset, |
| 155 | size_t data_size); |
| 156 | |
| 157 | // Returns true if current open packet can accommodate a message frame of |
| 158 | // |length|. |
fayang | 7a06f9b | 2020-06-24 10:23:19 -0700 | [diff] [blame] | 159 | // TODO(fayang): mark this const when deprecating quic_update_packet_size. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 160 | bool HasRoomForMessageFrame(QuicByteCount length); |
| 161 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 162 | // Serializes all added frames into a single packet and invokes the delegate_ |
| 163 | // to further process the SerializedPacket. |
fayang | 62b637b | 2019-09-16 08:40:49 -0700 | [diff] [blame] | 164 | void FlushCurrentPacket(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 165 | |
| 166 | // Optimized method to create a QuicStreamFrame and serialize it. Adds the |
| 167 | // QuicStreamFrame to the returned SerializedPacket. Sets |
| 168 | // |num_bytes_consumed| to the number of bytes consumed to create the |
| 169 | // QuicStreamFrame. |
| 170 | void CreateAndSerializeStreamFrame(QuicStreamId id, |
| 171 | size_t write_length, |
| 172 | QuicStreamOffset iov_offset, |
| 173 | QuicStreamOffset stream_offset, |
| 174 | bool fin, |
| 175 | TransmissionType transmission_type, |
| 176 | size_t* num_bytes_consumed); |
| 177 | |
| 178 | // Returns true if there are frames pending to be serialized. |
| 179 | bool HasPendingFrames() const; |
| 180 | |
| 181 | // Returns true if there are retransmittable frames pending to be serialized. |
| 182 | bool HasPendingRetransmittableFrames() const; |
| 183 | |
| 184 | // Returns true if there are stream frames for |id| pending to be serialized. |
| 185 | bool HasPendingStreamFramesOfStream(QuicStreamId id) const; |
| 186 | |
| 187 | // Returns the number of bytes which are available to be used by additional |
| 188 | // frames in the packet. Since stream frames are slightly smaller when they |
| 189 | // are the last frame in a packet, this method will return a different |
| 190 | // value than max_packet_size - PacketSize(), in this case. |
fayang | 7a06f9b | 2020-06-24 10:23:19 -0700 | [diff] [blame] | 191 | // TODO(fayang): mark this const when deprecating quic_update_packet_size. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 192 | size_t BytesFree(); |
| 193 | |
| 194 | // Returns the number of bytes that the packet will expand by if a new frame |
| 195 | // is added to the packet. If the last frame was a stream frame, it will |
| 196 | // expand slightly when a new frame is added, and this method returns the |
| 197 | // amount of expected expansion. |
| 198 | size_t ExpansionOnNewFrame() const; |
| 199 | |
fayang | 54a38e3 | 2020-06-26 07:01:15 -0700 | [diff] [blame] | 200 | // Returns the number of bytes that the packet will expand by when a new frame |
| 201 | // is going to be added. |last_frame| is the last frame of the packet. |
bnc | e5c11c0 | 2020-07-29 08:02:21 -0700 | [diff] [blame] | 202 | static size_t ExpansionOnNewFrameWithLastFrame(const QuicFrame& last_frame, |
| 203 | QuicTransportVersion version); |
fayang | 54a38e3 | 2020-06-26 07:01:15 -0700 | [diff] [blame] | 204 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 205 | // Returns the number of bytes in the current packet, including the header, |
| 206 | // if serialized with the current frames. Adding a frame to the packet |
| 207 | // may change the serialized length of existing frames, as per the comment |
| 208 | // in BytesFree. |
fayang | 7a06f9b | 2020-06-24 10:23:19 -0700 | [diff] [blame] | 209 | // TODO(fayang): mark this const when deprecating quic_update_packet_size. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 210 | size_t PacketSize(); |
| 211 | |
| 212 | // Tries to add |frame| to the packet creator's list of frames to be |
| 213 | // serialized. If the frame does not fit into the current packet, flushes the |
| 214 | // packet and returns false. |
renjietang | b63005e | 2019-11-19 23:08:53 -0800 | [diff] [blame] | 215 | bool AddFrame(const QuicFrame& frame, TransmissionType transmission_type); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 216 | |
| 217 | // Identical to AddSavedFrame, but allows the frame to be padded. |
| 218 | bool AddPaddedSavedFrame(const QuicFrame& frame, |
| 219 | TransmissionType transmission_type); |
| 220 | |
| 221 | // Creates a version negotiation packet which supports |supported_versions|. |
| 222 | std::unique_ptr<QuicEncryptedPacket> SerializeVersionNegotiationPacket( |
| 223 | bool ietf_quic, |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 224 | bool use_length_prefix, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 225 | const ParsedQuicVersionVector& supported_versions); |
| 226 | |
| 227 | // Creates a connectivity probing packet for versions prior to version 99. |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 228 | std::unique_ptr<SerializedPacket> SerializeConnectivityProbingPacket(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 229 | |
| 230 | // Create connectivity probing request and response packets using PATH |
| 231 | // CHALLENGE and PATH RESPONSE frames, respectively, for version 99/IETF QUIC. |
| 232 | // SerializePathChallengeConnectivityProbingPacket will pad the packet to be |
| 233 | // MTU bytes long. |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 234 | std::unique_ptr<SerializedPacket> |
| 235 | SerializePathChallengeConnectivityProbingPacket(QuicPathFrameBuffer* payload); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 236 | |
| 237 | // If |is_padded| is true then SerializePathResponseConnectivityProbingPacket |
| 238 | // will pad the packet to be MTU bytes long, else it will not pad the packet. |
| 239 | // |payloads| is cleared. |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 240 | std::unique_ptr<SerializedPacket> |
| 241 | SerializePathResponseConnectivityProbingPacket( |
wub | a750aab | 2020-02-10 06:43:15 -0800 | [diff] [blame] | 242 | const QuicCircularDeque<QuicPathFrameBuffer>& payloads, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 243 | const bool is_padded); |
| 244 | |
| 245 | // Returns a dummy packet that is valid but contains no useful information. |
| 246 | static SerializedPacket NoPacket(); |
| 247 | |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 248 | // Returns the destination connection ID to send over the wire. |
| 249 | QuicConnectionId GetDestinationConnectionId() const; |
| 250 | |
| 251 | // Returns the source connection ID to send over the wire. |
| 252 | QuicConnectionId GetSourceConnectionId() const; |
| 253 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 254 | // Returns length of destination connection ID to send over the wire. |
| 255 | QuicConnectionIdLength GetDestinationConnectionIdLength() const; |
| 256 | |
| 257 | // Returns length of source connection ID to send over the wire. |
| 258 | QuicConnectionIdLength GetSourceConnectionIdLength() const; |
| 259 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 260 | // Sets whether the server connection ID should be sent over the wire. |
| 261 | void SetServerConnectionIdIncluded( |
| 262 | QuicConnectionIdIncluded server_connection_id_included); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 263 | |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 264 | // Update the server connection ID used in outgoing packets. |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 265 | void SetServerConnectionId(QuicConnectionId server_connection_id); |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 266 | |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 267 | // Update the client connection ID used in outgoing packets. |
| 268 | void SetClientConnectionId(QuicConnectionId client_connection_id); |
| 269 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 270 | // Sets the encryption level that will be applied to new packets. |
| 271 | void set_encryption_level(EncryptionLevel level) { |
| 272 | packet_.encryption_level = level; |
| 273 | } |
dschinazi | 6458eb3 | 2020-06-23 12:38:41 -0700 | [diff] [blame] | 274 | EncryptionLevel encryption_level() { return packet_.encryption_level; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 275 | |
| 276 | // packet number of the last created packet, or 0 if no packets have been |
| 277 | // created. |
| 278 | QuicPacketNumber packet_number() const { return packet_.packet_number; } |
| 279 | |
| 280 | QuicByteCount max_packet_length() const { return max_packet_length_; } |
| 281 | |
| 282 | bool has_ack() const { return packet_.has_ack; } |
| 283 | |
| 284 | bool has_stop_waiting() const { return packet_.has_stop_waiting; } |
| 285 | |
| 286 | // Sets the encrypter to use for the encryption level and updates the max |
| 287 | // plaintext size. |
| 288 | void SetEncrypter(EncryptionLevel level, |
| 289 | std::unique_ptr<QuicEncrypter> encrypter); |
| 290 | |
| 291 | // Indicates whether the packet creator is in a state where it can change |
| 292 | // current maximum packet length. |
| 293 | bool CanSetMaxPacketLength() const; |
| 294 | |
| 295 | // Sets the maximum packet length. |
| 296 | void SetMaxPacketLength(QuicByteCount length); |
| 297 | |
dschinazi | ed459c0 | 2020-05-07 16:12:23 -0700 | [diff] [blame] | 298 | // Sets the maximum DATAGRAM/MESSAGE frame size we can send. |
| 299 | void SetMaxDatagramFrameSize(QuicByteCount max_datagram_frame_size); |
| 300 | |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 301 | // Set a soft maximum packet length in the creator. If a packet cannot be |
| 302 | // successfully created, creator will remove the soft limit and use the actual |
| 303 | // max packet length. |
| 304 | void SetSoftMaxPacketLength(QuicByteCount length); |
| 305 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 306 | // Increases pending_padding_bytes by |size|. Pending padding will be sent by |
| 307 | // MaybeAddPadding(). |
| 308 | void AddPendingPadding(QuicByteCount size); |
| 309 | |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 310 | // Sets the retry token to be sent over the wire in IETF Initial packets. |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 311 | void SetRetryToken(quiche::QuicheStringPiece retry_token); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 312 | |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 313 | // Consumes retransmittable control |frame|. Returns true if the frame is |
| 314 | // successfully consumed. Returns false otherwise. |
| 315 | bool ConsumeRetransmittableControlFrame(const QuicFrame& frame); |
| 316 | |
| 317 | // Given some data, may consume part or all of it and pass it to the |
| 318 | // packet creator to be serialized into packets. If not in batch |
| 319 | // mode, these packets will also be sent during this call. |
| 320 | // When |state| is FIN_AND_PADDING, random padding of size [1, 256] will be |
| 321 | // added after stream frames. If current constructed packet cannot |
| 322 | // accommodate, the padding will overflow to the next packet(s). |
| 323 | QuicConsumedData ConsumeData(QuicStreamId id, |
| 324 | size_t write_length, |
| 325 | QuicStreamOffset offset, |
| 326 | StreamSendingState state); |
| 327 | |
| 328 | // Sends as many data only packets as allowed by the send algorithm and the |
| 329 | // available iov. |
| 330 | // This path does not support padding, or bundling pending frames. |
| 331 | // In case we access this method from ConsumeData, total_bytes_consumed |
| 332 | // keeps track of how many bytes have already been consumed. |
| 333 | QuicConsumedData ConsumeDataFastPath(QuicStreamId id, |
| 334 | size_t write_length, |
| 335 | QuicStreamOffset offset, |
| 336 | bool fin, |
| 337 | size_t total_bytes_consumed); |
| 338 | |
| 339 | // Consumes data for CRYPTO frames sent at |level| starting at |offset| for a |
| 340 | // total of |write_length| bytes, and returns the number of bytes consumed. |
| 341 | // The data is passed into the packet creator and serialized into one or more |
| 342 | // packets. |
| 343 | size_t ConsumeCryptoData(EncryptionLevel level, |
| 344 | size_t write_length, |
| 345 | QuicStreamOffset offset); |
| 346 | |
| 347 | // Generates an MTU discovery packet of specified size. |
| 348 | void GenerateMtuDiscoveryPacket(QuicByteCount target_mtu); |
| 349 | |
| 350 | // Called when there is data to be sent, Retrieves updated ACK frame from |
| 351 | // delegate_ and flushes it. |
| 352 | void MaybeBundleAckOpportunistically(); |
| 353 | |
| 354 | // Called to flush ACK and STOP_WAITING frames, returns false if the flush |
| 355 | // fails. |
| 356 | bool FlushAckFrame(const QuicFrames& frames); |
| 357 | |
| 358 | // Adds a random amount of padding (between 1 to 256 bytes). |
| 359 | void AddRandomPadding(); |
| 360 | |
| 361 | // Attaches packet flusher. |
| 362 | void AttachPacketFlusher(); |
| 363 | |
| 364 | // Flushes everything, including current open packet and pending padding. |
| 365 | void Flush(); |
| 366 | |
| 367 | // Sends remaining pending padding. |
| 368 | // Pending paddings should only be sent when there is nothing else to send. |
| 369 | void SendRemainingPendingPadding(); |
| 370 | |
| 371 | // Set the minimum number of bytes for the server connection id length; |
| 372 | void SetServerConnectionIdLength(uint32_t length); |
| 373 | |
| 374 | // Set transmission type of next constructed packets. |
| 375 | void SetTransmissionType(TransmissionType type); |
| 376 | |
| 377 | // Tries to add a message frame containing |message| and returns the status. |
| 378 | MessageStatus AddMessageFrame(QuicMessageId message_id, |
| 379 | QuicMemSliceSpan message); |
| 380 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 381 | // Returns the largest payload that will fit into a single MESSAGE frame. |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 382 | QuicPacketLength GetCurrentLargestMessagePayload() const; |
| 383 | // Returns the largest payload that will fit into a single MESSAGE frame at |
| 384 | // any point during the connection. This assumes the version and |
| 385 | // connection ID lengths do not change. |
| 386 | QuicPacketLength GetGuaranteedLargestMessagePayload() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 387 | |
fayang | 354c942 | 2019-05-21 08:10:35 -0700 | [diff] [blame] | 388 | // Packet number of next created packet. |
| 389 | QuicPacketNumber NextSendingPacketNumber() const; |
| 390 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 391 | void set_debug_delegate(DebugDelegate* debug_delegate) { |
| 392 | debug_delegate_ = debug_delegate; |
| 393 | } |
| 394 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 395 | QuicByteCount pending_padding_bytes() const { return pending_padding_bytes_; } |
| 396 | |
dschinazi | 5c1d7d8 | 2020-07-29 16:42:50 -0700 | [diff] [blame] | 397 | ParsedQuicVersion version() const { return framer_->version(); } |
| 398 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 399 | QuicTransportVersion transport_version() const { |
| 400 | return framer_->transport_version(); |
| 401 | } |
| 402 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 403 | // Returns the minimum size that the plaintext of a packet must be. |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 404 | static size_t MinPlaintextPacketSize(const ParsedQuicVersion& version); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 405 | |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 406 | // Indicates whether packet flusher is currently attached. |
| 407 | bool PacketFlusherAttached() const; |
| 408 | |
nharper | 3907ac2 | 2019-09-25 15:32:28 -0700 | [diff] [blame] | 409 | void set_fully_pad_crypto_handshake_packets(bool new_value) { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 410 | fully_pad_crypto_handshake_packets_ = new_value; |
| 411 | } |
| 412 | |
| 413 | bool fully_pad_crypto_handshake_packets() const { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 414 | return fully_pad_crypto_handshake_packets_; |
| 415 | } |
| 416 | |
renjietang | 4c704c8 | 2019-10-07 16:39:11 -0700 | [diff] [blame] | 417 | // Serialize a probing packet that uses IETF QUIC's PATH CHALLENGE frame. Also |
| 418 | // fills the packet with padding. |
| 419 | size_t BuildPaddedPathChallengePacket(const QuicPacketHeader& header, |
| 420 | char* buffer, |
| 421 | size_t packet_length, |
| 422 | QuicPathFrameBuffer* payload, |
| 423 | QuicRandom* randomizer, |
| 424 | EncryptionLevel level); |
| 425 | |
| 426 | // Serialize a probing response packet that uses IETF QUIC's PATH RESPONSE |
| 427 | // frame. Also fills the packet with padding if |is_padded| is |
| 428 | // true. |payloads| is always emptied, even if the packet can not be |
| 429 | // successfully built. |
wub | a750aab | 2020-02-10 06:43:15 -0800 | [diff] [blame] | 430 | size_t BuildPathResponsePacket( |
| 431 | const QuicPacketHeader& header, |
| 432 | char* buffer, |
| 433 | size_t packet_length, |
| 434 | const QuicCircularDeque<QuicPathFrameBuffer>& payloads, |
| 435 | const bool is_padded, |
| 436 | EncryptionLevel level); |
renjietang | 4c704c8 | 2019-10-07 16:39:11 -0700 | [diff] [blame] | 437 | |
| 438 | // Serializes a probing packet, which is a padded PING packet. Returns the |
| 439 | // length of the packet. Returns 0 if it fails to serialize. |
| 440 | size_t BuildConnectivityProbingPacket(const QuicPacketHeader& header, |
| 441 | char* buffer, |
| 442 | size_t packet_length, |
| 443 | EncryptionLevel level); |
| 444 | |
fayang | 0875083 | 2019-10-24 11:25:34 -0700 | [diff] [blame] | 445 | // Serializes |coalesced| to provided |buffer|, returns coalesced packet |
| 446 | // length if serialization succeeds. Otherwise, returns 0. |
| 447 | size_t SerializeCoalescedPacket(const QuicCoalescedPacket& coalesced, |
| 448 | char* buffer, |
| 449 | size_t buffer_len); |
| 450 | |
fayang | fe963c5 | 2020-07-16 06:56:09 -0700 | [diff] [blame] | 451 | // Returns true if max_packet_length_ is currently a soft value. |
| 452 | bool HasSoftMaxPacketLength() const; |
| 453 | |
dschinazi | 6458eb3 | 2020-06-23 12:38:41 -0700 | [diff] [blame] | 454 | void set_disable_padding_override(bool should_disable_padding) { |
| 455 | disable_padding_override_ = should_disable_padding; |
| 456 | } |
| 457 | |
fayang | 1504296 | 2020-07-01 12:14:29 -0700 | [diff] [blame] | 458 | bool determine_serialized_packet_fate_early() const { |
| 459 | return determine_serialized_packet_fate_early_; |
| 460 | } |
| 461 | |
fayang | fe963c5 | 2020-07-16 06:56:09 -0700 | [diff] [blame] | 462 | bool coalesced_packet_of_higher_space() const { |
| 463 | return coalesced_packet_of_higher_space_; |
| 464 | } |
| 465 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 466 | private: |
| 467 | friend class test::QuicPacketCreatorPeer; |
| 468 | |
| 469 | // Creates a stream frame which fits into the current open packet. If |
QUICHE team | f08778a | 2019-03-14 08:10:26 -0700 | [diff] [blame] | 470 | // |data_size| is 0 and fin is true, the expected behavior is to consume |
| 471 | // the fin. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 472 | void CreateStreamFrame(QuicStreamId id, |
QUICHE team | f08778a | 2019-03-14 08:10:26 -0700 | [diff] [blame] | 473 | size_t data_size, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 474 | QuicStreamOffset offset, |
| 475 | bool fin, |
| 476 | QuicFrame* frame); |
| 477 | |
| 478 | // Creates a CRYPTO frame which fits into the current open packet. Returns |
| 479 | // false if there isn't enough room in the current open packet for a CRYPTO |
| 480 | // frame, and true if there is. |
| 481 | bool CreateCryptoFrame(EncryptionLevel level, |
| 482 | size_t write_length, |
| 483 | QuicStreamOffset offset, |
| 484 | QuicFrame* frame); |
| 485 | |
| 486 | void FillPacketHeader(QuicPacketHeader* header); |
| 487 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 488 | // Adds a padding frame to the current packet (if there is space) when (1) |
| 489 | // current packet needs full padding or (2) there are pending paddings. |
| 490 | void MaybeAddPadding(); |
| 491 | |
| 492 | // Serializes all frames which have been added and adds any which should be |
| 493 | // retransmitted to packet_.retransmittable_frames. All frames must fit into |
| 494 | // a single packet. |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 495 | // Fails if |encrypted_buffer_len| isn't long enough for the encrypted packet. |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 496 | void SerializePacket(QuicOwnedPacketBuffer encrypted_buffer, |
| 497 | size_t encrypted_buffer_len); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 498 | |
| 499 | // Called after a new SerialiedPacket is created to call the delegate's |
| 500 | // OnSerializedPacket and reset state. |
| 501 | void OnSerializedPacket(); |
| 502 | |
| 503 | // Clears all fields of packet_ that should be cleared between serializations. |
| 504 | void ClearPacket(); |
| 505 | |
fayang | 0875083 | 2019-10-24 11:25:34 -0700 | [diff] [blame] | 506 | // Re-serialzes frames of ENCRYPTION_INITIAL packet in coalesced packet with |
| 507 | // the original packet's packet number and packet number length. |
| 508 | // |padding_size| indicates the size of necessary padding. Returns 0 if |
| 509 | // serialization fails. |
| 510 | size_t ReserializeInitialPacketInCoalescedPacket( |
| 511 | const SerializedPacket& packet, |
| 512 | size_t padding_size, |
| 513 | char* buffer, |
| 514 | size_t buffer_len); |
| 515 | |
renjietang | dbe9834 | 2019-10-18 11:00:57 -0700 | [diff] [blame] | 516 | // Tries to coalesce |frame| with the back of |queued_frames_|. |
| 517 | // Returns true on success. |
| 518 | bool MaybeCoalesceStreamFrame(const QuicStreamFrame& frame); |
| 519 | |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 520 | // Called to remove the soft max_packet_length and restores |
| 521 | // latched_hard_max_packet_length_ if the packet cannot accommodate a single |
| 522 | // frame. Returns true if the soft limit is successfully removed. Returns |
| 523 | // false if either there is no current soft limit or there are queued frames |
| 524 | // (such that the packet length cannot be changed). |
| 525 | bool RemoveSoftMaxPacketLength(); |
| 526 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 527 | // Returns true if a diversification nonce should be included in the current |
| 528 | // packet's header. |
| 529 | bool IncludeNonceInPublicHeader() const; |
| 530 | |
| 531 | // Returns true if version should be included in current packet's header. |
| 532 | bool IncludeVersionInHeader() const; |
| 533 | |
| 534 | // Returns length of packet number to send over the wire. |
| 535 | // packet_.packet_number_length should never be read directly, use this |
| 536 | // function instead. |
| 537 | QuicPacketNumberLength GetPacketNumberLength() const; |
| 538 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 539 | // Returns the size in bytes of the packet header. |
| 540 | size_t PacketHeaderSize() const; |
| 541 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 542 | // Returns whether the destination connection ID is sent over the wire. |
| 543 | QuicConnectionIdIncluded GetDestinationConnectionIdIncluded() const; |
| 544 | |
| 545 | // Returns whether the source connection ID is sent over the wire. |
| 546 | QuicConnectionIdIncluded GetSourceConnectionIdIncluded() const; |
| 547 | |
| 548 | // Returns length of the retry token variable length integer to send over the |
| 549 | // wire. Is non-zero for v99 IETF Initial packets. |
| 550 | QuicVariableLengthIntegerLength GetRetryTokenLengthLength() const; |
| 551 | |
| 552 | // Returns the retry token to send over the wire, only sent in |
| 553 | // v99 IETF Initial packets. |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 554 | quiche::QuicheStringPiece GetRetryToken() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 555 | |
| 556 | // Returns length of the length variable length integer to send over the |
| 557 | // wire. Is non-zero for v99 IETF Initial, 0-RTT or Handshake packets. |
| 558 | QuicVariableLengthIntegerLength GetLengthLength() const; |
| 559 | |
ianswett | e28f022 | 2019-04-04 13:31:22 -0700 | [diff] [blame] | 560 | // Returns true if |frame| is a ClientHello. |
| 561 | bool StreamFrameIsClientHello(const QuicStreamFrame& frame) const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 562 | |
| 563 | // Returns true if packet under construction has IETF long header. |
| 564 | bool HasIetfLongHeader() const; |
| 565 | |
fayang | fce2f72 | 2020-06-26 10:27:32 -0700 | [diff] [blame] | 566 | // Get serialized frame length. Returns 0 if the frame does not fit into |
| 567 | // current packet. |
| 568 | size_t GetSerializedFrameLength(const QuicFrame& frame); |
| 569 | |
| 570 | // Add extra padding to pending_padding_bytes_ to meet minimum plaintext |
| 571 | // packet size required for header protection. |
| 572 | void MaybeAddExtraPaddingForHeaderProtection(); |
| 573 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 574 | // Does not own these delegates or the framer. |
| 575 | DelegateInterface* delegate_; |
| 576 | DebugDelegate* debug_delegate_; |
| 577 | QuicFramer* framer_; |
| 578 | QuicRandom* random_; |
| 579 | |
| 580 | // Controls whether version should be included while serializing the packet. |
| 581 | // send_version_in_packet_ should never be read directly, use |
| 582 | // IncludeVersionInHeader() instead. |
| 583 | bool send_version_in_packet_; |
| 584 | // If true, then |diversification_nonce_| will be included in the header of |
| 585 | // all packets created at the initial encryption level. |
| 586 | bool have_diversification_nonce_; |
| 587 | DiversificationNonce diversification_nonce_; |
| 588 | // Maximum length including headers and encryption (UDP payload length.) |
| 589 | QuicByteCount max_packet_length_; |
| 590 | size_t max_plaintext_size_; |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 591 | // Whether the server_connection_id is sent over the wire. |
| 592 | QuicConnectionIdIncluded server_connection_id_included_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 593 | |
| 594 | // Frames to be added to the next SerializedPacket |
| 595 | QuicFrames queued_frames_; |
| 596 | |
fayang | 7a06f9b | 2020-06-24 10:23:19 -0700 | [diff] [blame] | 597 | // Serialization size of header + frames. If there is no queued frames, |
| 598 | // packet_size_ is 0. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 599 | // TODO(ianswett): Move packet_size_ into SerializedPacket once |
| 600 | // QuicEncryptedPacket has been flattened into SerializedPacket. |
| 601 | size_t packet_size_; |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 602 | QuicConnectionId server_connection_id_; |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 603 | QuicConnectionId client_connection_id_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 604 | |
| 605 | // Packet used to invoke OnSerializedPacket. |
| 606 | SerializedPacket packet_; |
| 607 | |
| 608 | // Retry token to send over the wire in v99 IETF Initial packets. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 609 | std::string retry_token_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 610 | |
| 611 | // Pending padding bytes to send. Pending padding bytes will be sent in next |
| 612 | // packet(s) (after all other frames) if current constructed packet does not |
| 613 | // have room to send all of them. |
| 614 | QuicByteCount pending_padding_bytes_; |
| 615 | |
| 616 | // Indicates whether current constructed packet needs full padding to max |
| 617 | // packet size. Please note, full padding does not consume pending padding |
| 618 | // bytes. |
| 619 | bool needs_full_padding_; |
| 620 | |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 621 | // Transmission type of the next serialized packet. |
| 622 | TransmissionType next_transmission_type_; |
| 623 | |
| 624 | // True if packet flusher is currently attached. |
| 625 | bool flusher_attached_; |
| 626 | |
| 627 | // Whether crypto handshake packets should be fully padded. |
| 628 | bool fully_pad_crypto_handshake_packets_; |
| 629 | |
| 630 | // Packet number of the first packet of a write operation. This gets set |
| 631 | // when the out-most flusher attaches and gets cleared when the out-most |
| 632 | // flusher detaches. |
| 633 | QuicPacketNumber write_start_packet_number_; |
| 634 | |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 635 | // If not 0, this latches the actual max_packet_length when |
| 636 | // SetSoftMaxPacketLength is called and max_packet_length_ gets |
| 637 | // set to a soft value. |
| 638 | QuicByteCount latched_hard_max_packet_length_; |
dschinazi | ed459c0 | 2020-05-07 16:12:23 -0700 | [diff] [blame] | 639 | |
| 640 | // The maximum length of a MESSAGE/DATAGRAM frame that our peer is willing to |
| 641 | // accept. There is no limit for QUIC_CRYPTO connections, but QUIC+TLS |
| 642 | // negotiates this during the handshake. |
| 643 | QuicByteCount max_datagram_frame_size_; |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 644 | |
dschinazi | 6458eb3 | 2020-06-23 12:38:41 -0700 | [diff] [blame] | 645 | // When true, this will override the padding generation code to disable it. |
fayang | 1504296 | 2020-07-01 12:14:29 -0700 | [diff] [blame] | 646 | // TODO(fayang): remove this when deprecating |
| 647 | // quic_determine_serialized_packet_fate_early. |
dschinazi | 6458eb3 | 2020-06-23 12:38:41 -0700 | [diff] [blame] | 648 | bool disable_padding_override_ = false; |
fayang | 7a06f9b | 2020-06-24 10:23:19 -0700 | [diff] [blame] | 649 | |
fayang | fce2f72 | 2020-06-26 10:27:32 -0700 | [diff] [blame] | 650 | const bool update_packet_size_ = |
| 651 | GetQuicReloadableFlag(quic_update_packet_size); |
| 652 | |
| 653 | const bool fix_extra_padding_bytes_ = |
| 654 | GetQuicReloadableFlag(quic_fix_extra_padding_bytes); |
fayang | 1504296 | 2020-07-01 12:14:29 -0700 | [diff] [blame] | 655 | |
| 656 | const bool determine_serialized_packet_fate_early_ = |
| 657 | GetQuicReloadableFlag(quic_determine_serialized_packet_fate_early); |
fayang | fe963c5 | 2020-07-16 06:56:09 -0700 | [diff] [blame] | 658 | |
| 659 | const bool coalesced_packet_of_higher_space_ = |
fayang | dceb9a3 | 2020-07-29 10:57:04 -0700 | [diff] [blame] | 660 | GetQuicReloadableFlag(quic_coalesced_packet_of_higher_space2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 661 | }; |
| 662 | |
| 663 | } // namespace quic |
| 664 | |
| 665 | #endif // QUICHE_QUIC_CORE_QUIC_PACKET_CREATOR_H_ |