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