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