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 | |
| 5 | #include "net/third_party/quiche/src/quic/core/quic_packet_creator.h" |
| 6 | |
| 7 | #include <algorithm> |
renjietang | 4c704c8 | 2019-10-07 16:39:11 -0700 | [diff] [blame] | 8 | #include <cstddef> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #include <cstdint> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 10 | #include <string> |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 11 | #include <utility> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h" |
renjietang | dbe9834 | 2019-10-18 11:00:57 -0700 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/core/frames/quic_frame.h" |
renjietang | 4c704c8 | 2019-10-07 16:39:11 -0700 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/quic/core/frames/quic_path_challenge_frame.h" |
renjietang | dbe9834 | 2019-10-18 11:00:57 -0700 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quic/core/frames/quic_stream_frame.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 18 | #include "net/third_party/quiche/src/quic/core/quic_constants.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 19 | #include "net/third_party/quiche/src/quic/core/quic_data_writer.h" |
| 20 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 21 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 22 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | #include "net/third_party/quiche/src/quic/platform/api/quic_aligned.h" |
| 24 | #include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h" |
| 25 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 26 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
| 27 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 28 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 29 | #include "net/third_party/quiche/src/quic/platform/api/quic_server_stats.h" |
bnc | 2c2444e | 2019-05-03 08:18:23 -0700 | [diff] [blame] | 30 | #include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 31 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
| 32 | #include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h" |
| 33 | |
| 34 | namespace quic { |
| 35 | namespace { |
| 36 | |
| 37 | QuicLongHeaderType EncryptionlevelToLongHeaderType(EncryptionLevel level) { |
| 38 | switch (level) { |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 39 | case ENCRYPTION_INITIAL: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | return INITIAL; |
QUICHE team | 88ea008 | 2019-03-15 10:05:26 -0700 | [diff] [blame] | 41 | case ENCRYPTION_HANDSHAKE: |
| 42 | return HANDSHAKE; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 43 | case ENCRYPTION_ZERO_RTT: |
| 44 | return ZERO_RTT_PROTECTED; |
| 45 | case ENCRYPTION_FORWARD_SECURE: |
| 46 | QUIC_BUG |
| 47 | << "Try to derive long header type for packet with encryption level: " |
dschinazi | ef79a5f | 2019-10-04 10:32:54 -0700 | [diff] [blame] | 48 | << EncryptionLevelToString(level); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 49 | return INVALID_PACKET_TYPE; |
| 50 | default: |
dschinazi | ef79a5f | 2019-10-04 10:32:54 -0700 | [diff] [blame] | 51 | QUIC_BUG << EncryptionLevelToString(level); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 52 | return INVALID_PACKET_TYPE; |
| 53 | } |
| 54 | } |
| 55 | |
fayang | 0875083 | 2019-10-24 11:25:34 -0700 | [diff] [blame] | 56 | // ScopedPacketContextSwitcher saves |packet|'s states and change states |
| 57 | // during its construction. When the switcher goes out of scope, it restores |
| 58 | // saved states. |
| 59 | class ScopedPacketContextSwitcher { |
| 60 | public: |
| 61 | ScopedPacketContextSwitcher(QuicPacketNumber packet_number, |
| 62 | QuicPacketNumberLength packet_number_length, |
| 63 | EncryptionLevel encryption_level, |
| 64 | SerializedPacket* packet) |
| 65 | |
| 66 | : saved_packet_number_(packet->packet_number), |
| 67 | saved_packet_number_length_(packet->packet_number_length), |
| 68 | saved_encryption_level_(packet->encryption_level), |
| 69 | packet_(packet) { |
| 70 | packet_->packet_number = packet_number, |
| 71 | packet_->packet_number_length = packet_number_length; |
| 72 | packet_->encryption_level = encryption_level; |
| 73 | } |
| 74 | |
| 75 | ~ScopedPacketContextSwitcher() { |
| 76 | packet_->packet_number = saved_packet_number_; |
| 77 | packet_->packet_number_length = saved_packet_number_length_; |
| 78 | packet_->encryption_level = saved_encryption_level_; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | const QuicPacketNumber saved_packet_number_; |
| 83 | const QuicPacketNumberLength saved_packet_number_length_; |
| 84 | const EncryptionLevel saved_encryption_level_; |
| 85 | SerializedPacket* packet_; |
| 86 | }; |
| 87 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 88 | } // namespace |
| 89 | |
| 90 | #define ENDPOINT \ |
| 91 | (framer_->perspective() == Perspective::IS_SERVER ? "Server: " : "Client: ") |
| 92 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 93 | QuicPacketCreator::QuicPacketCreator(QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 94 | QuicFramer* framer, |
| 95 | DelegateInterface* delegate) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 96 | : QuicPacketCreator(server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 97 | framer, |
| 98 | QuicRandom::GetInstance(), |
| 99 | delegate) {} |
| 100 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 101 | QuicPacketCreator::QuicPacketCreator(QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 102 | QuicFramer* framer, |
| 103 | QuicRandom* random, |
| 104 | DelegateInterface* delegate) |
| 105 | : delegate_(delegate), |
| 106 | debug_delegate_(nullptr), |
| 107 | framer_(framer), |
| 108 | random_(random), |
| 109 | send_version_in_packet_(framer->perspective() == Perspective::IS_CLIENT), |
| 110 | have_diversification_nonce_(false), |
| 111 | max_packet_length_(0), |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 112 | server_connection_id_included_(CONNECTION_ID_PRESENT), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 113 | packet_size_(0), |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 114 | server_connection_id_(server_connection_id), |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 115 | client_connection_id_(EmptyQuicConnectionId()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 116 | packet_(QuicPacketNumber(), |
| 117 | PACKET_1BYTE_PACKET_NUMBER, |
| 118 | nullptr, |
| 119 | 0, |
| 120 | false, |
| 121 | false), |
| 122 | pending_padding_bytes_(0), |
| 123 | needs_full_padding_(false), |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 124 | next_transmission_type_(NOT_RETRANSMISSION), |
| 125 | flusher_attached_(false), |
| 126 | fully_pad_crypto_handshake_packets_(true), |
fayang | 02a2874 | 2019-12-04 07:09:38 -0800 | [diff] [blame^] | 127 | latched_hard_max_packet_length_(0) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 128 | SetMaxPacketLength(kDefaultMaxPacketSize); |
| 129 | } |
| 130 | |
| 131 | QuicPacketCreator::~QuicPacketCreator() { |
| 132 | DeleteFrames(&packet_.retransmittable_frames); |
| 133 | } |
| 134 | |
| 135 | void QuicPacketCreator::SetEncrypter(EncryptionLevel level, |
| 136 | std::unique_ptr<QuicEncrypter> encrypter) { |
| 137 | framer_->SetEncrypter(level, std::move(encrypter)); |
| 138 | max_plaintext_size_ = framer_->GetMaxPlaintextSize(max_packet_length_); |
| 139 | } |
| 140 | |
| 141 | bool QuicPacketCreator::CanSetMaxPacketLength() const { |
| 142 | // |max_packet_length_| should not be changed mid-packet. |
| 143 | return queued_frames_.empty(); |
| 144 | } |
| 145 | |
| 146 | void QuicPacketCreator::SetMaxPacketLength(QuicByteCount length) { |
| 147 | DCHECK(CanSetMaxPacketLength()); |
| 148 | |
| 149 | // Avoid recomputing |max_plaintext_size_| if the length does not actually |
| 150 | // change. |
| 151 | if (length == max_packet_length_) { |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | max_packet_length_ = length; |
| 156 | max_plaintext_size_ = framer_->GetMaxPlaintextSize(max_packet_length_); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 157 | QUIC_BUG_IF(max_plaintext_size_ - PacketHeaderSize() < |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 158 | MinPlaintextPacketSize(framer_->version())) |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 159 | << "Attempted to set max packet length too small"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 160 | } |
| 161 | |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 162 | void QuicPacketCreator::SetSoftMaxPacketLength(QuicByteCount length) { |
| 163 | DCHECK(CanSetMaxPacketLength()); |
| 164 | if (length > max_packet_length_) { |
| 165 | QUIC_BUG << ENDPOINT |
| 166 | << "Try to increase max_packet_length_ in " |
| 167 | "SetSoftMaxPacketLength, use SetMaxPacketLength instead."; |
| 168 | return; |
| 169 | } |
| 170 | if (framer_->GetMaxPlaintextSize(length) < |
| 171 | PacketHeaderSize() + MinPlaintextPacketSize(framer_->version())) { |
| 172 | QUIC_DLOG(INFO) << length << " is too small to fit packet header"; |
| 173 | return; |
| 174 | } |
| 175 | QUIC_DVLOG(1) << "Setting soft max packet length to: " << length; |
| 176 | latched_hard_max_packet_length_ = max_packet_length_; |
| 177 | max_packet_length_ = length; |
| 178 | max_plaintext_size_ = framer_->GetMaxPlaintextSize(length); |
| 179 | } |
| 180 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 181 | // Stops serializing version of the protocol in packets sent after this call. |
| 182 | // A packet that is already open might send kQuicVersionSize bytes less than the |
| 183 | // maximum packet size if we stop sending version before it is serialized. |
| 184 | void QuicPacketCreator::StopSendingVersion() { |
| 185 | DCHECK(send_version_in_packet_); |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 186 | DCHECK(!VersionHasIetfInvariantHeader(framer_->transport_version())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 187 | send_version_in_packet_ = false; |
| 188 | if (packet_size_ > 0) { |
| 189 | DCHECK_LT(kQuicVersionSize, packet_size_); |
| 190 | packet_size_ -= kQuicVersionSize; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void QuicPacketCreator::SetDiversificationNonce( |
| 195 | const DiversificationNonce& nonce) { |
| 196 | DCHECK(!have_diversification_nonce_); |
| 197 | have_diversification_nonce_ = true; |
| 198 | diversification_nonce_ = nonce; |
| 199 | } |
| 200 | |
| 201 | void QuicPacketCreator::UpdatePacketNumberLength( |
| 202 | QuicPacketNumber least_packet_awaited_by_peer, |
| 203 | QuicPacketCount max_packets_in_flight) { |
| 204 | if (!queued_frames_.empty()) { |
| 205 | // Don't change creator state if there are frames queued. |
| 206 | QUIC_BUG << "Called UpdatePacketNumberLength with " << queued_frames_.size() |
| 207 | << " queued_frames. First frame type:" |
| 208 | << queued_frames_.front().type |
| 209 | << " last frame type:" << queued_frames_.back().type; |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | DCHECK_LE(least_packet_awaited_by_peer, packet_.packet_number + 1); |
| 214 | const uint64_t current_delta = |
| 215 | packet_.packet_number + 1 - least_packet_awaited_by_peer; |
| 216 | const uint64_t delta = std::max(current_delta, max_packets_in_flight); |
| 217 | packet_.packet_number_length = QuicFramer::GetMinPacketNumberLength( |
| 218 | framer_->transport_version(), QuicPacketNumber(delta * 4)); |
| 219 | } |
| 220 | |
fayang | 4c1c236 | 2019-09-13 07:20:01 -0700 | [diff] [blame] | 221 | void QuicPacketCreator::SkipNPacketNumbers( |
| 222 | QuicPacketCount count, |
| 223 | QuicPacketNumber least_packet_awaited_by_peer, |
| 224 | QuicPacketCount max_packets_in_flight) { |
| 225 | if (!queued_frames_.empty()) { |
| 226 | // Don't change creator state if there are frames queued. |
| 227 | QUIC_BUG << "Called SkipNPacketNumbers with " << queued_frames_.size() |
| 228 | << " queued_frames. First frame type:" |
| 229 | << queued_frames_.front().type |
| 230 | << " last frame type:" << queued_frames_.back().type; |
| 231 | return; |
| 232 | } |
| 233 | if (packet_.packet_number > packet_.packet_number + count) { |
| 234 | // Skipping count packet numbers causes packet number wrapping around, |
| 235 | // reject it. |
| 236 | QUIC_LOG(WARNING) << "Skipping " << count |
| 237 | << " packet numbers causes packet number wrapping " |
| 238 | "around, least_packet_awaited_by_peer: " |
| 239 | << least_packet_awaited_by_peer |
| 240 | << " packet_number:" << packet_.packet_number; |
| 241 | return; |
| 242 | } |
| 243 | packet_.packet_number += count; |
| 244 | // Packet number changes, update packet number length if necessary. |
| 245 | UpdatePacketNumberLength(least_packet_awaited_by_peer, max_packets_in_flight); |
| 246 | } |
| 247 | |
fayang | 62b637b | 2019-09-16 08:40:49 -0700 | [diff] [blame] | 248 | bool QuicPacketCreator::ConsumeCryptoDataToFillCurrentPacket( |
| 249 | EncryptionLevel level, |
| 250 | size_t write_length, |
| 251 | QuicStreamOffset offset, |
| 252 | bool needs_full_padding, |
| 253 | TransmissionType transmission_type, |
| 254 | QuicFrame* frame) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 255 | if (!CreateCryptoFrame(level, write_length, offset, frame)) { |
| 256 | return false; |
| 257 | } |
| 258 | // When crypto data was sent in stream frames, ConsumeData is called with |
| 259 | // |needs_full_padding = true|. Keep the same behavior here when sending |
| 260 | // crypto frames. |
| 261 | // |
| 262 | // TODO(nharper): Check what the IETF drafts say about padding out initial |
| 263 | // messages and change this as appropriate. |
nharper | 51961cf | 2019-05-13 13:23:24 -0700 | [diff] [blame] | 264 | if (needs_full_padding) { |
| 265 | needs_full_padding_ = true; |
| 266 | } |
fayang | 347ab75 | 2019-10-22 11:17:43 -0700 | [diff] [blame] | 267 | return AddFrame(*frame, transmission_type); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 268 | } |
| 269 | |
fayang | 62b637b | 2019-09-16 08:40:49 -0700 | [diff] [blame] | 270 | bool QuicPacketCreator::ConsumeDataToFillCurrentPacket( |
| 271 | QuicStreamId id, |
| 272 | size_t data_size, |
| 273 | QuicStreamOffset offset, |
| 274 | bool fin, |
| 275 | bool needs_full_padding, |
| 276 | TransmissionType transmission_type, |
| 277 | QuicFrame* frame) { |
QUICHE team | f08778a | 2019-03-14 08:10:26 -0700 | [diff] [blame] | 278 | if (!HasRoomForStreamFrame(id, offset, data_size)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 279 | return false; |
| 280 | } |
QUICHE team | f08778a | 2019-03-14 08:10:26 -0700 | [diff] [blame] | 281 | CreateStreamFrame(id, data_size, offset, fin, frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 282 | // Explicitly disallow multi-packet CHLOs. |
danzh | 88e3e05 | 2019-06-13 11:47:18 -0700 | [diff] [blame] | 283 | if (GetQuicFlag(FLAGS_quic_enforce_single_packet_chlo) && |
ianswett | e28f022 | 2019-04-04 13:31:22 -0700 | [diff] [blame] | 284 | StreamFrameIsClientHello(frame->stream_frame) && |
| 285 | frame->stream_frame.data_length < data_size) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 286 | const std::string error_details = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 287 | "Client hello won't fit in a single packet."; |
| 288 | QUIC_BUG << error_details << " Constructed stream frame length: " |
| 289 | << frame->stream_frame.data_length |
ianswett | e28f022 | 2019-04-04 13:31:22 -0700 | [diff] [blame] | 290 | << " CHLO length: " << data_size; |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 291 | delegate_->OnUnrecoverableError(QUIC_CRYPTO_CHLO_TOO_LARGE, error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 292 | return false; |
| 293 | } |
fayang | 347ab75 | 2019-10-22 11:17:43 -0700 | [diff] [blame] | 294 | if (!AddFrame(*frame, transmission_type)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 295 | // Fails if we try to write unencrypted stream data. |
| 296 | return false; |
| 297 | } |
| 298 | if (needs_full_padding) { |
| 299 | needs_full_padding_ = true; |
| 300 | } |
| 301 | |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, |
| 306 | QuicStreamOffset offset, |
| 307 | size_t data_size) { |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 308 | const size_t min_stream_frame_size = QuicFramer::GetMinStreamFrameSize( |
| 309 | framer_->transport_version(), id, offset, /*last_frame_in_packet=*/true, |
| 310 | data_size); |
| 311 | if (BytesFree() > min_stream_frame_size) { |
| 312 | return true; |
| 313 | } |
| 314 | if (!RemoveSoftMaxPacketLength()) { |
| 315 | return false; |
| 316 | } |
| 317 | return BytesFree() > min_stream_frame_size; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | bool QuicPacketCreator::HasRoomForMessageFrame(QuicByteCount length) { |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 321 | const size_t message_frame_size = QuicFramer::GetMessageFrameSize( |
| 322 | framer_->transport_version(), /*last_frame_in_packet=*/true, length); |
| 323 | if (BytesFree() >= message_frame_size) { |
| 324 | return true; |
| 325 | } |
| 326 | if (!RemoveSoftMaxPacketLength()) { |
| 327 | return false; |
| 328 | } |
| 329 | return BytesFree() >= message_frame_size; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // TODO(fkastenholz): this method should not use constant values for |
| 333 | // the last-frame-in-packet and data-length parameters to |
| 334 | // GetMinStreamFrameSize. Proper values should be plumbed in from |
| 335 | // higher up. This was left this way for now for a few reasons. First, |
| 336 | // higher up calls to StreamFramePacketOverhead() do not always know |
| 337 | // this information, leading to a cascade of changes and B) the |
| 338 | // higher-up software does not always loop, calling |
| 339 | // StreamFramePacketOverhead() once for every packet -- eg there is |
| 340 | // a test in quic_connection_test that calls it once and assumes that |
| 341 | // the value is the same for all packets. |
| 342 | |
| 343 | // static |
| 344 | size_t QuicPacketCreator::StreamFramePacketOverhead( |
| 345 | QuicTransportVersion version, |
| 346 | QuicConnectionIdLength destination_connection_id_length, |
| 347 | QuicConnectionIdLength source_connection_id_length, |
| 348 | bool include_version, |
| 349 | bool include_diversification_nonce, |
| 350 | QuicPacketNumberLength packet_number_length, |
| 351 | QuicVariableLengthIntegerLength retry_token_length_length, |
| 352 | QuicVariableLengthIntegerLength length_length, |
| 353 | QuicStreamOffset offset) { |
| 354 | return GetPacketHeaderSize(version, destination_connection_id_length, |
| 355 | source_connection_id_length, include_version, |
| 356 | include_diversification_nonce, |
| 357 | packet_number_length, retry_token_length_length, 0, |
| 358 | length_length) + |
| 359 | |
| 360 | // Assumes this is a packet with a single stream frame in it. Since |
| 361 | // last_frame_in_packet is set true, the size of the length field is |
| 362 | // not included in the calculation. This is OK because in other places |
| 363 | // in the code, the logic adds back 2 (the size of the Google QUIC |
| 364 | // length) when a frame is not the last frame of the packet. This is |
| 365 | // also acceptable for IETF Quic; even though the length field could be |
| 366 | // 8 bytes long, in practice it will not be longer than 2 bytes (enough |
| 367 | // to encode 16K). A length that would be encoded in 2 bytes (0xfff) |
| 368 | // is passed just for cleanliness. |
| 369 | // |
| 370 | // TODO(fkastenholz): This is very hacky and feels brittle. Ideally we |
| 371 | // would calculate the correct lengths at the correct time, based on |
| 372 | // the state at that time/place. |
| 373 | QuicFramer::GetMinStreamFrameSize(version, 1u, offset, true, |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 374 | kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void QuicPacketCreator::CreateStreamFrame(QuicStreamId id, |
QUICHE team | f08778a | 2019-03-14 08:10:26 -0700 | [diff] [blame] | 378 | size_t data_size, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 379 | QuicStreamOffset offset, |
| 380 | bool fin, |
| 381 | QuicFrame* frame) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 382 | DCHECK_GT( |
| 383 | max_packet_length_, |
| 384 | StreamFramePacketOverhead( |
| 385 | framer_->transport_version(), GetDestinationConnectionIdLength(), |
| 386 | GetSourceConnectionIdLength(), kIncludeVersion, |
| 387 | IncludeNonceInPublicHeader(), PACKET_6BYTE_PACKET_NUMBER, |
| 388 | GetRetryTokenLengthLength(), GetLengthLength(), offset)); |
| 389 | |
| 390 | QUIC_BUG_IF(!HasRoomForStreamFrame(id, offset, data_size)) |
| 391 | << "No room for Stream frame, BytesFree: " << BytesFree() |
| 392 | << " MinStreamFrameSize: " |
| 393 | << QuicFramer::GetMinStreamFrameSize(framer_->transport_version(), id, |
| 394 | offset, true, data_size); |
| 395 | |
QUICHE team | f08778a | 2019-03-14 08:10:26 -0700 | [diff] [blame] | 396 | QUIC_BUG_IF(data_size == 0 && !fin) |
| 397 | << "Creating a stream frame for stream ID:" << id |
| 398 | << " with no data or fin."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 399 | size_t min_frame_size = QuicFramer::GetMinStreamFrameSize( |
| 400 | framer_->transport_version(), id, offset, |
| 401 | /* last_frame_in_packet= */ true, data_size); |
| 402 | size_t bytes_consumed = |
| 403 | std::min<size_t>(BytesFree() - min_frame_size, data_size); |
| 404 | |
| 405 | bool set_fin = fin && bytes_consumed == data_size; // Last frame. |
| 406 | *frame = QuicFrame(QuicStreamFrame(id, set_fin, offset, bytes_consumed)); |
| 407 | } |
| 408 | |
| 409 | bool QuicPacketCreator::CreateCryptoFrame(EncryptionLevel level, |
| 410 | size_t write_length, |
| 411 | QuicStreamOffset offset, |
| 412 | QuicFrame* frame) { |
| 413 | size_t min_frame_size = |
| 414 | QuicFramer::GetMinCryptoFrameSize(write_length, offset); |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 415 | if (BytesFree() <= min_frame_size && |
| 416 | (!RemoveSoftMaxPacketLength() || BytesFree() <= min_frame_size)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 417 | return false; |
| 418 | } |
| 419 | size_t max_write_length = BytesFree() - min_frame_size; |
| 420 | size_t bytes_consumed = std::min<size_t>(max_write_length, write_length); |
| 421 | *frame = QuicFrame(new QuicCryptoFrame(level, offset, bytes_consumed)); |
| 422 | return true; |
| 423 | } |
| 424 | |
fayang | 62b637b | 2019-09-16 08:40:49 -0700 | [diff] [blame] | 425 | void QuicPacketCreator::FlushCurrentPacket() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 426 | if (!HasPendingFrames() && pending_padding_bytes_ == 0) { |
| 427 | return; |
| 428 | } |
| 429 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 430 | QUIC_CACHELINE_ALIGNED char stack_buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 431 | char* serialized_packet_buffer = delegate_->GetPacketBuffer(); |
| 432 | if (serialized_packet_buffer == nullptr) { |
| 433 | serialized_packet_buffer = stack_buffer; |
| 434 | } |
| 435 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 436 | SerializePacket(serialized_packet_buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 437 | OnSerializedPacket(); |
| 438 | } |
| 439 | |
| 440 | void QuicPacketCreator::OnSerializedPacket() { |
| 441 | if (packet_.encrypted_buffer == nullptr) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 442 | const std::string error_details = "Failed to SerializePacket."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 443 | QUIC_BUG << error_details; |
| 444 | delegate_->OnUnrecoverableError(QUIC_FAILED_TO_SERIALIZE_PACKET, |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 445 | error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | |
| 449 | SerializedPacket packet(std::move(packet_)); |
| 450 | ClearPacket(); |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 451 | RemoveSoftMaxPacketLength(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 452 | delegate_->OnSerializedPacket(&packet); |
| 453 | } |
| 454 | |
| 455 | void QuicPacketCreator::ClearPacket() { |
| 456 | packet_.has_ack = false; |
| 457 | packet_.has_stop_waiting = false; |
| 458 | packet_.has_crypto_handshake = NOT_HANDSHAKE; |
| 459 | packet_.num_padding_bytes = 0; |
wub | 98669f5 | 2019-04-18 10:49:18 -0700 | [diff] [blame] | 460 | packet_.transmission_type = NOT_RETRANSMISSION; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 461 | packet_.encrypted_buffer = nullptr; |
| 462 | packet_.encrypted_length = 0; |
| 463 | DCHECK(packet_.retransmittable_frames.empty()); |
fayang | 51152fd | 2019-10-21 06:48:09 -0700 | [diff] [blame] | 464 | DCHECK(packet_.nonretransmittable_frames.empty()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 465 | packet_.largest_acked.Clear(); |
| 466 | needs_full_padding_ = false; |
| 467 | } |
| 468 | |
fayang | 0875083 | 2019-10-24 11:25:34 -0700 | [diff] [blame] | 469 | size_t QuicPacketCreator::ReserializeInitialPacketInCoalescedPacket( |
| 470 | const SerializedPacket& packet, |
| 471 | size_t padding_size, |
| 472 | char* buffer, |
| 473 | size_t buffer_len) { |
| 474 | QUIC_BUG_IF(packet.encryption_level != ENCRYPTION_INITIAL); |
| 475 | QUIC_BUG_IF(packet.nonretransmittable_frames.empty() && |
| 476 | packet.retransmittable_frames.empty()) |
| 477 | << "Attempt to serialize empty ENCRYPTION_INITIAL packet in coalesced " |
| 478 | "packet"; |
| 479 | ScopedPacketContextSwitcher switcher( |
| 480 | packet.packet_number - |
| 481 | 1, // -1 because serialize packet increase packet number. |
| 482 | packet.packet_number_length, packet.encryption_level, &packet_); |
| 483 | for (const QuicFrame& frame : packet.nonretransmittable_frames) { |
| 484 | if (!AddFrame(frame, packet.transmission_type)) { |
| 485 | QUIC_BUG << "Failed to serialize frame: " << frame; |
| 486 | return 0; |
| 487 | } |
| 488 | } |
| 489 | for (const QuicFrame& frame : packet.retransmittable_frames) { |
| 490 | if (!AddFrame(frame, packet.transmission_type)) { |
| 491 | QUIC_BUG << "Failed to serialize frame: " << frame; |
| 492 | return 0; |
| 493 | } |
| 494 | } |
| 495 | // Add necessary padding. |
| 496 | if (padding_size > 0) { |
| 497 | QUIC_DVLOG(2) << ENDPOINT << "Add padding of size: " << padding_size; |
| 498 | if (!AddFrame(QuicFrame(QuicPaddingFrame(padding_size)), |
| 499 | packet.transmission_type)) { |
| 500 | QUIC_BUG << "Failed to add padding of size " << padding_size |
| 501 | << " when serializing ENCRYPTION_INITIAL " |
| 502 | "packet in coalesced packet"; |
| 503 | return 0; |
| 504 | } |
| 505 | } |
| 506 | SerializePacket(buffer, buffer_len); |
| 507 | const size_t encrypted_length = packet_.encrypted_length; |
| 508 | // Clear frames in packet_. No need to DeleteFrames since frames are owned by |
| 509 | // initial_packet. |
| 510 | packet_.retransmittable_frames.clear(); |
| 511 | packet_.nonretransmittable_frames.clear(); |
| 512 | ClearPacket(); |
| 513 | return encrypted_length; |
| 514 | } |
| 515 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 516 | void QuicPacketCreator::CreateAndSerializeStreamFrame( |
| 517 | QuicStreamId id, |
| 518 | size_t write_length, |
| 519 | QuicStreamOffset iov_offset, |
| 520 | QuicStreamOffset stream_offset, |
| 521 | bool fin, |
| 522 | TransmissionType transmission_type, |
| 523 | size_t* num_bytes_consumed) { |
| 524 | DCHECK(queued_frames_.empty()); |
| 525 | // Write out the packet header |
| 526 | QuicPacketHeader header; |
| 527 | FillPacketHeader(&header); |
| 528 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 529 | QUIC_CACHELINE_ALIGNED char stack_buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 530 | char* encrypted_buffer = delegate_->GetPacketBuffer(); |
| 531 | if (encrypted_buffer == nullptr) { |
| 532 | encrypted_buffer = stack_buffer; |
| 533 | } |
| 534 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 535 | QuicDataWriter writer(kMaxOutgoingPacketSize, encrypted_buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 536 | size_t length_field_offset = 0; |
| 537 | if (!framer_->AppendPacketHeader(header, &writer, &length_field_offset)) { |
| 538 | QUIC_BUG << "AppendPacketHeader failed"; |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | // Create a Stream frame with the remaining space. |
| 543 | QUIC_BUG_IF(iov_offset == write_length && !fin) |
| 544 | << "Creating a stream frame with no data or fin."; |
| 545 | const size_t remaining_data_size = write_length - iov_offset; |
nharper | ebabffd | 2019-06-03 17:34:45 -0700 | [diff] [blame] | 546 | size_t min_frame_size = QuicFramer::GetMinStreamFrameSize( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 547 | framer_->transport_version(), id, stream_offset, |
| 548 | /* last_frame_in_packet= */ true, remaining_data_size); |
nharper | ebabffd | 2019-06-03 17:34:45 -0700 | [diff] [blame] | 549 | size_t available_size = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 550 | max_plaintext_size_ - writer.length() - min_frame_size; |
nharper | ebabffd | 2019-06-03 17:34:45 -0700 | [diff] [blame] | 551 | size_t bytes_consumed = std::min<size_t>(available_size, remaining_data_size); |
| 552 | size_t plaintext_bytes_written = min_frame_size + bytes_consumed; |
| 553 | bool needs_padding = false; |
| 554 | if (plaintext_bytes_written < MinPlaintextPacketSize(framer_->version())) { |
| 555 | needs_padding = true; |
| 556 | // Recalculate sizes with the stream frame not being marked as the last |
| 557 | // frame in the packet. |
| 558 | min_frame_size = QuicFramer::GetMinStreamFrameSize( |
| 559 | framer_->transport_version(), id, stream_offset, |
| 560 | /* last_frame_in_packet= */ false, remaining_data_size); |
| 561 | available_size = max_plaintext_size_ - writer.length() - min_frame_size; |
| 562 | bytes_consumed = std::min<size_t>(available_size, remaining_data_size); |
| 563 | plaintext_bytes_written = min_frame_size + bytes_consumed; |
| 564 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 565 | |
| 566 | const bool set_fin = fin && (bytes_consumed == remaining_data_size); |
| 567 | QuicStreamFrame frame(id, set_fin, stream_offset, bytes_consumed); |
rch | c76cd74 | 2019-03-26 16:00:03 -0700 | [diff] [blame] | 568 | if (debug_delegate_ != nullptr) { |
| 569 | debug_delegate_->OnFrameAddedToPacket(QuicFrame(frame)); |
| 570 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 571 | QUIC_DVLOG(1) << ENDPOINT << "Adding frame: " << frame; |
| 572 | |
dschinazi | 118934b | 2019-06-13 18:09:08 -0700 | [diff] [blame] | 573 | QUIC_DVLOG(2) << ENDPOINT << "Serializing stream packet " << header << frame; |
| 574 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 575 | // TODO(ianswett): AppendTypeByte and AppendStreamFrame could be optimized |
| 576 | // into one method that takes a QuicStreamFrame, if warranted. |
nharper | ebabffd | 2019-06-03 17:34:45 -0700 | [diff] [blame] | 577 | bool omit_frame_length = !needs_padding; |
| 578 | if (!framer_->AppendTypeByte(QuicFrame(frame), omit_frame_length, &writer)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 579 | QUIC_BUG << "AppendTypeByte failed"; |
| 580 | return; |
| 581 | } |
nharper | ebabffd | 2019-06-03 17:34:45 -0700 | [diff] [blame] | 582 | if (!framer_->AppendStreamFrame(frame, omit_frame_length, &writer)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 583 | QUIC_BUG << "AppendStreamFrame failed"; |
| 584 | return; |
| 585 | } |
nharper | ebabffd | 2019-06-03 17:34:45 -0700 | [diff] [blame] | 586 | if (needs_padding && |
| 587 | plaintext_bytes_written < MinPlaintextPacketSize(framer_->version()) && |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 588 | !writer.WritePaddingBytes(MinPlaintextPacketSize(framer_->version()) - |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 589 | plaintext_bytes_written)) { |
| 590 | QUIC_BUG << "Unable to add padding bytes"; |
| 591 | return; |
| 592 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 593 | |
| 594 | if (!framer_->WriteIetfLongHeaderLength(header, &writer, length_field_offset, |
| 595 | packet_.encryption_level)) { |
| 596 | return; |
| 597 | } |
| 598 | |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 599 | packet_.transmission_type = transmission_type; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 600 | |
| 601 | size_t encrypted_length = framer_->EncryptInPlace( |
| 602 | packet_.encryption_level, packet_.packet_number, |
| 603 | GetStartOfEncryptedData(framer_->transport_version(), header), |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 604 | writer.length(), kMaxOutgoingPacketSize, encrypted_buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 605 | if (encrypted_length == 0) { |
| 606 | QUIC_BUG << "Failed to encrypt packet number " << header.packet_number; |
| 607 | return; |
| 608 | } |
| 609 | // TODO(ianswett): Optimize the storage so RetransmitableFrames can be |
| 610 | // unioned with a QuicStreamFrame and a UniqueStreamBuffer. |
| 611 | *num_bytes_consumed = bytes_consumed; |
| 612 | packet_size_ = 0; |
| 613 | packet_.encrypted_buffer = encrypted_buffer; |
| 614 | packet_.encrypted_length = encrypted_length; |
| 615 | packet_.retransmittable_frames.push_back(QuicFrame(frame)); |
| 616 | OnSerializedPacket(); |
| 617 | } |
| 618 | |
| 619 | bool QuicPacketCreator::HasPendingFrames() const { |
| 620 | return !queued_frames_.empty(); |
| 621 | } |
| 622 | |
| 623 | bool QuicPacketCreator::HasPendingRetransmittableFrames() const { |
| 624 | return !packet_.retransmittable_frames.empty(); |
| 625 | } |
| 626 | |
| 627 | bool QuicPacketCreator::HasPendingStreamFramesOfStream(QuicStreamId id) const { |
| 628 | for (const auto& frame : packet_.retransmittable_frames) { |
| 629 | if (frame.type == STREAM_FRAME && frame.stream_frame.stream_id == id) { |
| 630 | return true; |
| 631 | } |
| 632 | } |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | size_t QuicPacketCreator::ExpansionOnNewFrame() const { |
| 637 | // If the last frame in the packet is a message frame, then it will expand to |
| 638 | // include the varint message length when a new frame is added. |
| 639 | const bool has_trailing_message_frame = |
| 640 | !queued_frames_.empty() && queued_frames_.back().type == MESSAGE_FRAME; |
| 641 | if (has_trailing_message_frame) { |
| 642 | return QuicDataWriter::GetVarInt62Len( |
| 643 | queued_frames_.back().message_frame->message_length); |
| 644 | } |
| 645 | // If the last frame in the packet is a stream frame, then it will expand to |
| 646 | // include the stream_length field when a new frame is added. |
| 647 | const bool has_trailing_stream_frame = |
| 648 | !queued_frames_.empty() && queued_frames_.back().type == STREAM_FRAME; |
| 649 | if (!has_trailing_stream_frame) { |
| 650 | return 0; |
| 651 | } |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 652 | if (VersionHasIetfQuicFrames(framer_->transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 653 | return QuicDataWriter::GetVarInt62Len( |
| 654 | queued_frames_.back().stream_frame.data_length); |
| 655 | } |
| 656 | return kQuicStreamPayloadLengthSize; |
| 657 | } |
| 658 | |
| 659 | size_t QuicPacketCreator::BytesFree() { |
| 660 | DCHECK_GE(max_plaintext_size_, PacketSize()); |
| 661 | return max_plaintext_size_ - |
| 662 | std::min(max_plaintext_size_, PacketSize() + ExpansionOnNewFrame()); |
| 663 | } |
| 664 | |
| 665 | size_t QuicPacketCreator::PacketSize() { |
| 666 | if (!queued_frames_.empty()) { |
| 667 | return packet_size_; |
| 668 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 669 | packet_size_ = PacketHeaderSize(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 670 | return packet_size_; |
| 671 | } |
| 672 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 673 | bool QuicPacketCreator::AddPaddedSavedFrame( |
| 674 | const QuicFrame& frame, |
| 675 | TransmissionType transmission_type) { |
fayang | 347ab75 | 2019-10-22 11:17:43 -0700 | [diff] [blame] | 676 | if (AddFrame(frame, transmission_type)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 677 | needs_full_padding_ = true; |
| 678 | return true; |
| 679 | } |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | void QuicPacketCreator::SerializePacket(char* encrypted_buffer, |
| 684 | size_t encrypted_buffer_len) { |
| 685 | DCHECK_LT(0u, encrypted_buffer_len); |
| 686 | QUIC_BUG_IF(queued_frames_.empty() && pending_padding_bytes_ == 0) |
| 687 | << "Attempt to serialize empty packet"; |
| 688 | QuicPacketHeader header; |
| 689 | // FillPacketHeader increments packet_number_. |
| 690 | FillPacketHeader(&header); |
| 691 | |
| 692 | MaybeAddPadding(); |
| 693 | |
dschinazi | 118934b | 2019-06-13 18:09:08 -0700 | [diff] [blame] | 694 | QUIC_DVLOG(2) << ENDPOINT << "Serializing packet " << header |
wub | 031d47c | 2019-11-21 08:04:07 -0800 | [diff] [blame] | 695 | << QuicFramesToString(queued_frames_) << " at encryption_level " |
| 696 | << EncryptionLevelToString(packet_.encryption_level); |
dschinazi | 118934b | 2019-06-13 18:09:08 -0700 | [diff] [blame] | 697 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 698 | DCHECK_GE(max_plaintext_size_, packet_size_); |
| 699 | // Use the packet_size_ instead of the buffer size to ensure smaller |
| 700 | // packet sizes are properly used. |
| 701 | size_t length = |
| 702 | framer_->BuildDataPacket(header, queued_frames_, encrypted_buffer, |
| 703 | packet_size_, packet_.encryption_level); |
| 704 | if (length == 0) { |
| 705 | QUIC_BUG << "Failed to serialize " << queued_frames_.size() << " frames."; |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | // ACK Frames will be truncated due to length only if they're the only frame |
| 710 | // in the packet, and if packet_size_ was set to max_plaintext_size_. If |
| 711 | // truncation due to length occurred, then GetSerializedFrameLength will have |
| 712 | // returned all bytes free. |
| 713 | bool possibly_truncated_by_length = packet_size_ == max_plaintext_size_ && |
| 714 | queued_frames_.size() == 1 && |
| 715 | queued_frames_.back().type == ACK_FRAME; |
| 716 | // Because of possible truncation, we can't be confident that our |
| 717 | // packet size calculation worked correctly. |
| 718 | if (!possibly_truncated_by_length) { |
| 719 | DCHECK_EQ(packet_size_, length); |
| 720 | } |
| 721 | const size_t encrypted_length = framer_->EncryptInPlace( |
| 722 | packet_.encryption_level, packet_.packet_number, |
| 723 | GetStartOfEncryptedData(framer_->transport_version(), header), length, |
| 724 | encrypted_buffer_len, encrypted_buffer); |
| 725 | if (encrypted_length == 0) { |
| 726 | QUIC_BUG << "Failed to encrypt packet number " << packet_.packet_number; |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | packet_size_ = 0; |
| 731 | queued_frames_.clear(); |
| 732 | packet_.encrypted_buffer = encrypted_buffer; |
| 733 | packet_.encrypted_length = encrypted_length; |
| 734 | } |
| 735 | |
| 736 | std::unique_ptr<QuicEncryptedPacket> |
| 737 | QuicPacketCreator::SerializeVersionNegotiationPacket( |
| 738 | bool ietf_quic, |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 739 | bool use_length_prefix, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 740 | const ParsedQuicVersionVector& supported_versions) { |
| 741 | DCHECK_EQ(Perspective::IS_SERVER, framer_->perspective()); |
| 742 | std::unique_ptr<QuicEncryptedPacket> encrypted = |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 743 | QuicFramer::BuildVersionNegotiationPacket( |
| 744 | server_connection_id_, client_connection_id_, ietf_quic, |
| 745 | use_length_prefix, supported_versions); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 746 | DCHECK(encrypted); |
| 747 | DCHECK_GE(max_packet_length_, encrypted->length()); |
| 748 | return encrypted; |
| 749 | } |
| 750 | |
| 751 | OwningSerializedPacketPointer |
| 752 | QuicPacketCreator::SerializeConnectivityProbingPacket() { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 753 | QUIC_BUG_IF(VersionHasIetfQuicFrames(framer_->transport_version())) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 754 | << "Must not be version 99 to serialize padded ping connectivity probe"; |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 755 | RemoveSoftMaxPacketLength(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 756 | QuicPacketHeader header; |
| 757 | // FillPacketHeader increments packet_number_. |
| 758 | FillPacketHeader(&header); |
| 759 | |
dschinazi | 118934b | 2019-06-13 18:09:08 -0700 | [diff] [blame] | 760 | QUIC_DVLOG(2) << ENDPOINT << "Serializing connectivity probing packet " |
| 761 | << header; |
| 762 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 763 | std::unique_ptr<char[]> buffer(new char[kMaxOutgoingPacketSize]); |
renjietang | 4c704c8 | 2019-10-07 16:39:11 -0700 | [diff] [blame] | 764 | size_t length = BuildConnectivityProbingPacket( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 765 | header, buffer.get(), max_plaintext_size_, packet_.encryption_level); |
| 766 | DCHECK(length); |
| 767 | |
| 768 | const size_t encrypted_length = framer_->EncryptInPlace( |
| 769 | packet_.encryption_level, packet_.packet_number, |
| 770 | GetStartOfEncryptedData(framer_->transport_version(), header), length, |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 771 | kMaxOutgoingPacketSize, buffer.get()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 772 | DCHECK(encrypted_length); |
| 773 | |
| 774 | OwningSerializedPacketPointer serialize_packet(new SerializedPacket( |
| 775 | header.packet_number, header.packet_number_length, buffer.release(), |
| 776 | encrypted_length, /*has_ack=*/false, /*has_stop_waiting=*/false)); |
| 777 | |
| 778 | serialize_packet->encryption_level = packet_.encryption_level; |
| 779 | serialize_packet->transmission_type = NOT_RETRANSMISSION; |
| 780 | |
| 781 | return serialize_packet; |
| 782 | } |
| 783 | |
| 784 | OwningSerializedPacketPointer |
| 785 | QuicPacketCreator::SerializePathChallengeConnectivityProbingPacket( |
| 786 | QuicPathFrameBuffer* payload) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 787 | QUIC_BUG_IF(!VersionHasIetfQuicFrames(framer_->transport_version())) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 788 | << "Must be version 99 to serialize path challenge connectivity probe, " |
| 789 | "is version " |
| 790 | << framer_->transport_version(); |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 791 | RemoveSoftMaxPacketLength(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 792 | QuicPacketHeader header; |
| 793 | // FillPacketHeader increments packet_number_. |
| 794 | FillPacketHeader(&header); |
| 795 | |
dschinazi | 118934b | 2019-06-13 18:09:08 -0700 | [diff] [blame] | 796 | QUIC_DVLOG(2) << ENDPOINT << "Serializing path challenge packet " << header; |
| 797 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 798 | std::unique_ptr<char[]> buffer(new char[kMaxOutgoingPacketSize]); |
renjietang | 4c704c8 | 2019-10-07 16:39:11 -0700 | [diff] [blame] | 799 | size_t length = BuildPaddedPathChallengePacket( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 800 | header, buffer.get(), max_plaintext_size_, payload, random_, |
| 801 | packet_.encryption_level); |
| 802 | DCHECK(length); |
| 803 | |
| 804 | const size_t encrypted_length = framer_->EncryptInPlace( |
| 805 | packet_.encryption_level, packet_.packet_number, |
| 806 | GetStartOfEncryptedData(framer_->transport_version(), header), length, |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 807 | kMaxOutgoingPacketSize, buffer.get()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 808 | DCHECK(encrypted_length); |
| 809 | |
| 810 | OwningSerializedPacketPointer serialize_packet(new SerializedPacket( |
| 811 | header.packet_number, header.packet_number_length, buffer.release(), |
| 812 | encrypted_length, /*has_ack=*/false, /*has_stop_waiting=*/false)); |
| 813 | |
| 814 | serialize_packet->encryption_level = packet_.encryption_level; |
| 815 | serialize_packet->transmission_type = NOT_RETRANSMISSION; |
| 816 | |
| 817 | return serialize_packet; |
| 818 | } |
| 819 | |
| 820 | OwningSerializedPacketPointer |
| 821 | QuicPacketCreator::SerializePathResponseConnectivityProbingPacket( |
| 822 | const QuicDeque<QuicPathFrameBuffer>& payloads, |
| 823 | const bool is_padded) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 824 | QUIC_BUG_IF(!VersionHasIetfQuicFrames(framer_->transport_version())) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 825 | << "Must be version 99 to serialize path response connectivity probe, is " |
| 826 | "version " |
| 827 | << framer_->transport_version(); |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 828 | RemoveSoftMaxPacketLength(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 829 | QuicPacketHeader header; |
| 830 | // FillPacketHeader increments packet_number_. |
| 831 | FillPacketHeader(&header); |
| 832 | |
dschinazi | 118934b | 2019-06-13 18:09:08 -0700 | [diff] [blame] | 833 | QUIC_DVLOG(2) << ENDPOINT << "Serializing path response packet " << header; |
| 834 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 835 | std::unique_ptr<char[]> buffer(new char[kMaxOutgoingPacketSize]); |
renjietang | 4c704c8 | 2019-10-07 16:39:11 -0700 | [diff] [blame] | 836 | size_t length = |
| 837 | BuildPathResponsePacket(header, buffer.get(), max_plaintext_size_, |
| 838 | payloads, is_padded, packet_.encryption_level); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 839 | DCHECK(length); |
| 840 | |
| 841 | const size_t encrypted_length = framer_->EncryptInPlace( |
| 842 | packet_.encryption_level, packet_.packet_number, |
| 843 | GetStartOfEncryptedData(framer_->transport_version(), header), length, |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 844 | kMaxOutgoingPacketSize, buffer.get()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 845 | DCHECK(encrypted_length); |
| 846 | |
| 847 | OwningSerializedPacketPointer serialize_packet(new SerializedPacket( |
| 848 | header.packet_number, header.packet_number_length, buffer.release(), |
| 849 | encrypted_length, /*has_ack=*/false, /*has_stop_waiting=*/false)); |
| 850 | |
| 851 | serialize_packet->encryption_level = packet_.encryption_level; |
| 852 | serialize_packet->transmission_type = NOT_RETRANSMISSION; |
| 853 | |
| 854 | return serialize_packet; |
| 855 | } |
| 856 | |
renjietang | 4c704c8 | 2019-10-07 16:39:11 -0700 | [diff] [blame] | 857 | size_t QuicPacketCreator::BuildPaddedPathChallengePacket( |
| 858 | const QuicPacketHeader& header, |
| 859 | char* buffer, |
| 860 | size_t packet_length, |
| 861 | QuicPathFrameBuffer* payload, |
| 862 | QuicRandom* randomizer, |
| 863 | EncryptionLevel level) { |
| 864 | DCHECK(VersionHasIetfQuicFrames(framer_->transport_version())); |
| 865 | QuicFrames frames; |
| 866 | |
| 867 | // Write a PATH_CHALLENGE frame, which has a random 8-byte payload |
| 868 | randomizer->RandBytes(payload->data(), payload->size()); |
| 869 | QuicPathChallengeFrame path_challenge_frame(0, *payload); |
| 870 | frames.push_back(QuicFrame(&path_challenge_frame)); |
| 871 | |
| 872 | if (debug_delegate_ != nullptr) { |
| 873 | debug_delegate_->OnFrameAddedToPacket(QuicFrame(&path_challenge_frame)); |
| 874 | } |
| 875 | |
| 876 | // Add padding to the rest of the packet in order to assess Path MTU |
| 877 | // characteristics. |
| 878 | QuicPaddingFrame padding_frame; |
| 879 | frames.push_back(QuicFrame(padding_frame)); |
| 880 | |
| 881 | return framer_->BuildDataPacket(header, frames, buffer, packet_length, level); |
| 882 | } |
| 883 | |
| 884 | size_t QuicPacketCreator::BuildPathResponsePacket( |
| 885 | const QuicPacketHeader& header, |
| 886 | char* buffer, |
| 887 | size_t packet_length, |
| 888 | const QuicDeque<QuicPathFrameBuffer>& payloads, |
| 889 | const bool is_padded, |
| 890 | EncryptionLevel level) { |
| 891 | if (payloads.empty()) { |
| 892 | QUIC_BUG |
| 893 | << "Attempt to generate connectivity response with no request payloads"; |
| 894 | return 0; |
| 895 | } |
| 896 | DCHECK(VersionHasIetfQuicFrames(framer_->transport_version())); |
| 897 | |
| 898 | std::vector<std::unique_ptr<QuicPathResponseFrame>> path_response_frames; |
| 899 | for (const QuicPathFrameBuffer& payload : payloads) { |
| 900 | // Note that the control frame ID can be 0 since this is not retransmitted. |
| 901 | path_response_frames.push_back( |
| 902 | std::make_unique<QuicPathResponseFrame>(0, payload)); |
| 903 | } |
| 904 | |
| 905 | QuicFrames frames; |
| 906 | for (const std::unique_ptr<QuicPathResponseFrame>& path_response_frame : |
| 907 | path_response_frames) { |
| 908 | frames.push_back(QuicFrame(path_response_frame.get())); |
| 909 | if (debug_delegate_ != nullptr) { |
| 910 | debug_delegate_->OnFrameAddedToPacket( |
| 911 | QuicFrame(path_response_frame.get())); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | if (is_padded) { |
| 916 | // Add padding to the rest of the packet in order to assess Path MTU |
| 917 | // characteristics. |
| 918 | QuicPaddingFrame padding_frame; |
| 919 | frames.push_back(QuicFrame(padding_frame)); |
| 920 | } |
| 921 | |
| 922 | return framer_->BuildDataPacket(header, frames, buffer, packet_length, level); |
| 923 | } |
| 924 | |
| 925 | size_t QuicPacketCreator::BuildConnectivityProbingPacket( |
| 926 | const QuicPacketHeader& header, |
| 927 | char* buffer, |
| 928 | size_t packet_length, |
| 929 | EncryptionLevel level) { |
| 930 | QuicFrames frames; |
| 931 | |
| 932 | // Write a PING frame, which has no data payload. |
| 933 | QuicPingFrame ping_frame; |
| 934 | frames.push_back(QuicFrame(ping_frame)); |
| 935 | |
| 936 | // Add padding to the rest of the packet. |
| 937 | QuicPaddingFrame padding_frame; |
| 938 | frames.push_back(QuicFrame(padding_frame)); |
| 939 | |
| 940 | return framer_->BuildDataPacket(header, frames, buffer, packet_length, level); |
| 941 | } |
| 942 | |
fayang | 0875083 | 2019-10-24 11:25:34 -0700 | [diff] [blame] | 943 | size_t QuicPacketCreator::SerializeCoalescedPacket( |
| 944 | const QuicCoalescedPacket& coalesced, |
| 945 | char* buffer, |
| 946 | size_t buffer_len) { |
| 947 | QUIC_BUG_IF(packet_.num_padding_bytes != 0); |
| 948 | if (HasPendingFrames()) { |
| 949 | QUIC_BUG << "Try to serialize coalesced packet with pending frames"; |
| 950 | return 0; |
| 951 | } |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 952 | RemoveSoftMaxPacketLength(); |
fayang | 0875083 | 2019-10-24 11:25:34 -0700 | [diff] [blame] | 953 | QUIC_BUG_IF(coalesced.length() == 0) |
| 954 | << "Attempt to serialize empty coalesced packet"; |
| 955 | size_t packet_length = 0; |
| 956 | if (coalesced.initial_packet() != nullptr) { |
| 957 | size_t initial_length = ReserializeInitialPacketInCoalescedPacket( |
| 958 | *coalesced.initial_packet(), |
| 959 | /*padding_size=*/coalesced.max_packet_length() - coalesced.length(), |
| 960 | buffer, buffer_len); |
| 961 | if (initial_length == 0) { |
| 962 | QUIC_BUG << "Failed to reserialize ENCRYPTION_INITIAL packet in " |
| 963 | "coalesced packet"; |
| 964 | return 0; |
| 965 | } |
| 966 | buffer += initial_length; |
| 967 | buffer_len -= initial_length; |
| 968 | packet_length += initial_length; |
| 969 | } |
| 970 | size_t length_copied = 0; |
| 971 | if (!coalesced.CopyEncryptedBuffers(buffer, buffer_len, &length_copied)) { |
| 972 | return 0; |
| 973 | } |
| 974 | packet_length += length_copied; |
| 975 | QUIC_DVLOG(1) << ENDPOINT |
| 976 | << "Successfully serialized coalesced packet of length: " |
| 977 | << packet_length; |
| 978 | return packet_length; |
| 979 | } |
| 980 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 981 | // TODO(b/74062209): Make this a public method of framer? |
| 982 | SerializedPacket QuicPacketCreator::NoPacket() { |
| 983 | return SerializedPacket(QuicPacketNumber(), PACKET_1BYTE_PACKET_NUMBER, |
| 984 | nullptr, 0, false, false); |
| 985 | } |
| 986 | |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 987 | QuicConnectionId QuicPacketCreator::GetDestinationConnectionId() const { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 988 | if (framer_->perspective() == Perspective::IS_SERVER) { |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 989 | return client_connection_id_; |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 990 | } |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 991 | return server_connection_id_; |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | QuicConnectionId QuicPacketCreator::GetSourceConnectionId() const { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 995 | if (framer_->perspective() == Perspective::IS_CLIENT) { |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 996 | return client_connection_id_; |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 997 | } |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 998 | return server_connection_id_; |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 999 | } |
| 1000 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1001 | QuicConnectionIdIncluded QuicPacketCreator::GetDestinationConnectionIdIncluded() |
| 1002 | const { |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 1003 | // In versions that do not support client connection IDs, the destination |
| 1004 | // connection ID is only sent from client to server. |
| 1005 | return (framer_->perspective() == Perspective::IS_CLIENT || |
| 1006 | framer_->version().SupportsClientConnectionIds()) |
| 1007 | ? CONNECTION_ID_PRESENT |
| 1008 | : CONNECTION_ID_ABSENT; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | QuicConnectionIdIncluded QuicPacketCreator::GetSourceConnectionIdIncluded() |
| 1012 | const { |
| 1013 | // Long header packets sent by server include source connection ID. |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 1014 | // Ones sent by the client only include source connection ID if the version |
| 1015 | // supports client connection IDs. |
| 1016 | if (HasIetfLongHeader() && |
| 1017 | (framer_->perspective() == Perspective::IS_SERVER || |
| 1018 | framer_->version().SupportsClientConnectionIds())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1019 | return CONNECTION_ID_PRESENT; |
| 1020 | } |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 1021 | if (framer_->perspective() == Perspective::IS_SERVER) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 1022 | return server_connection_id_included_; |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1023 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1024 | return CONNECTION_ID_ABSENT; |
| 1025 | } |
| 1026 | |
| 1027 | QuicConnectionIdLength QuicPacketCreator::GetDestinationConnectionIdLength() |
| 1028 | const { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 1029 | DCHECK(QuicUtils::IsConnectionIdValidForVersion(server_connection_id_, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1030 | transport_version())); |
| 1031 | return GetDestinationConnectionIdIncluded() == CONNECTION_ID_PRESENT |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1032 | ? static_cast<QuicConnectionIdLength>( |
| 1033 | GetDestinationConnectionId().length()) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1034 | : PACKET_0BYTE_CONNECTION_ID; |
| 1035 | } |
| 1036 | |
| 1037 | QuicConnectionIdLength QuicPacketCreator::GetSourceConnectionIdLength() const { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 1038 | DCHECK(QuicUtils::IsConnectionIdValidForVersion(server_connection_id_, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1039 | transport_version())); |
| 1040 | return GetSourceConnectionIdIncluded() == CONNECTION_ID_PRESENT |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1041 | ? static_cast<QuicConnectionIdLength>( |
| 1042 | GetSourceConnectionId().length()) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1043 | : PACKET_0BYTE_CONNECTION_ID; |
| 1044 | } |
| 1045 | |
| 1046 | QuicPacketNumberLength QuicPacketCreator::GetPacketNumberLength() const { |
fayang | 3ac15c1 | 2019-06-14 14:04:51 -0700 | [diff] [blame] | 1047 | if (HasIetfLongHeader() && |
| 1048 | !framer_->version().SendsVariableLengthPacketNumberInLongHeader()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1049 | return PACKET_4BYTE_PACKET_NUMBER; |
| 1050 | } |
| 1051 | return packet_.packet_number_length; |
| 1052 | } |
| 1053 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1054 | size_t QuicPacketCreator::PacketHeaderSize() const { |
| 1055 | return GetPacketHeaderSize( |
| 1056 | framer_->transport_version(), GetDestinationConnectionIdLength(), |
| 1057 | GetSourceConnectionIdLength(), IncludeVersionInHeader(), |
| 1058 | IncludeNonceInPublicHeader(), GetPacketNumberLength(), |
| 1059 | GetRetryTokenLengthLength(), GetRetryToken().length(), GetLengthLength()); |
| 1060 | } |
| 1061 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1062 | QuicVariableLengthIntegerLength QuicPacketCreator::GetRetryTokenLengthLength() |
| 1063 | const { |
| 1064 | if (QuicVersionHasLongHeaderLengths(framer_->transport_version()) && |
| 1065 | HasIetfLongHeader() && |
| 1066 | EncryptionlevelToLongHeaderType(packet_.encryption_level) == INITIAL) { |
| 1067 | return QuicDataWriter::GetVarInt62Len(GetRetryToken().length()); |
| 1068 | } |
| 1069 | return VARIABLE_LENGTH_INTEGER_LENGTH_0; |
| 1070 | } |
| 1071 | |
| 1072 | QuicStringPiece QuicPacketCreator::GetRetryToken() const { |
dschinazi | 0db8709 | 2019-05-20 07:59:15 -0700 | [diff] [blame] | 1073 | if (QuicVersionHasLongHeaderLengths(framer_->transport_version()) && |
| 1074 | HasIetfLongHeader() && |
| 1075 | EncryptionlevelToLongHeaderType(packet_.encryption_level) == INITIAL) { |
| 1076 | return retry_token_; |
| 1077 | } |
| 1078 | return QuicStringPiece(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | void QuicPacketCreator::SetRetryToken(QuicStringPiece retry_token) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1082 | retry_token_ = std::string(retry_token); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1083 | } |
| 1084 | |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1085 | bool QuicPacketCreator::ConsumeRetransmittableControlFrame( |
| 1086 | const QuicFrame& frame) { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1087 | QUIC_BUG_IF(IsControlFrame(frame.type) && !GetControlFrameId(frame)) |
| 1088 | << "Adding a control frame with no control frame id: " << frame; |
| 1089 | DCHECK(QuicUtils::IsRetransmittableFrame(frame.type)) << frame; |
| 1090 | MaybeBundleAckOpportunistically(); |
| 1091 | if (HasPendingFrames()) { |
renjietang | b63005e | 2019-11-19 23:08:53 -0800 | [diff] [blame] | 1092 | if (AddFrame(frame, next_transmission_type_)) { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1093 | // There is pending frames and current frame fits. |
| 1094 | return true; |
| 1095 | } |
| 1096 | } |
| 1097 | DCHECK(!HasPendingFrames()); |
| 1098 | if (frame.type != PING_FRAME && frame.type != CONNECTION_CLOSE_FRAME && |
| 1099 | !delegate_->ShouldGeneratePacket(HAS_RETRANSMITTABLE_DATA, |
| 1100 | NOT_HANDSHAKE)) { |
| 1101 | // Do not check congestion window for ping or connection close frames. |
| 1102 | return false; |
| 1103 | } |
renjietang | b63005e | 2019-11-19 23:08:53 -0800 | [diff] [blame] | 1104 | const bool success = AddFrame(frame, next_transmission_type_); |
ianswett | bd78ea1 | 2019-09-26 06:32:37 -0700 | [diff] [blame] | 1105 | QUIC_BUG_IF(!success) << "Failed to add frame:" << frame |
| 1106 | << " transmission_type:" << next_transmission_type_; |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1107 | return success; |
| 1108 | } |
| 1109 | |
| 1110 | QuicConsumedData QuicPacketCreator::ConsumeData(QuicStreamId id, |
| 1111 | size_t write_length, |
| 1112 | QuicStreamOffset offset, |
| 1113 | StreamSendingState state) { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1114 | QUIC_BUG_IF(!flusher_attached_) << "Packet flusher is not attached when " |
| 1115 | "generator tries to write stream data."; |
| 1116 | bool has_handshake = QuicUtils::IsCryptoStreamId(transport_version(), id); |
| 1117 | MaybeBundleAckOpportunistically(); |
| 1118 | bool fin = state != NO_FIN; |
| 1119 | QUIC_BUG_IF(has_handshake && fin) |
| 1120 | << "Handshake packets should never send a fin"; |
| 1121 | // To make reasoning about crypto frames easier, we don't combine them with |
| 1122 | // other retransmittable frames in a single packet. |
| 1123 | if (has_handshake && HasPendingRetransmittableFrames()) { |
| 1124 | FlushCurrentPacket(); |
| 1125 | } |
| 1126 | |
| 1127 | size_t total_bytes_consumed = 0; |
| 1128 | bool fin_consumed = false; |
| 1129 | |
| 1130 | if (!HasRoomForStreamFrame(id, offset, write_length)) { |
| 1131 | FlushCurrentPacket(); |
| 1132 | } |
| 1133 | |
| 1134 | if (!fin && (write_length == 0)) { |
| 1135 | QUIC_BUG << "Attempt to consume empty data without FIN."; |
| 1136 | return QuicConsumedData(0, false); |
| 1137 | } |
| 1138 | // We determine if we can enter the fast path before executing |
| 1139 | // the slow path loop. |
| 1140 | bool run_fast_path = |
| 1141 | !has_handshake && state != FIN_AND_PADDING && !HasPendingFrames() && |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 1142 | write_length - total_bytes_consumed > kMaxOutgoingPacketSize && |
| 1143 | latched_hard_max_packet_length_ == 0; |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1144 | |
| 1145 | while (!run_fast_path && delegate_->ShouldGeneratePacket( |
| 1146 | HAS_RETRANSMITTABLE_DATA, |
| 1147 | has_handshake ? IS_HANDSHAKE : NOT_HANDSHAKE)) { |
| 1148 | QuicFrame frame; |
| 1149 | bool needs_full_padding = |
| 1150 | has_handshake && fully_pad_crypto_handshake_packets_; |
| 1151 | |
| 1152 | if (!ConsumeDataToFillCurrentPacket(id, write_length - total_bytes_consumed, |
| 1153 | offset + total_bytes_consumed, fin, |
| 1154 | needs_full_padding, |
| 1155 | next_transmission_type_, &frame)) { |
| 1156 | // The creator is always flushed if there's not enough room for a new |
| 1157 | // stream frame before ConsumeData, so ConsumeData should always succeed. |
| 1158 | QUIC_BUG << "Failed to ConsumeData, stream:" << id; |
| 1159 | return QuicConsumedData(0, false); |
| 1160 | } |
| 1161 | |
| 1162 | // A stream frame is created and added. |
| 1163 | size_t bytes_consumed = frame.stream_frame.data_length; |
| 1164 | total_bytes_consumed += bytes_consumed; |
| 1165 | fin_consumed = fin && total_bytes_consumed == write_length; |
| 1166 | if (fin_consumed && state == FIN_AND_PADDING) { |
| 1167 | AddRandomPadding(); |
| 1168 | } |
| 1169 | DCHECK(total_bytes_consumed == write_length || |
| 1170 | (bytes_consumed > 0 && HasPendingFrames())); |
| 1171 | |
| 1172 | if (total_bytes_consumed == write_length) { |
| 1173 | // We're done writing the data. Exit the loop. |
| 1174 | // We don't make this a precondition because we could have 0 bytes of data |
| 1175 | // if we're simply writing a fin. |
| 1176 | break; |
| 1177 | } |
| 1178 | FlushCurrentPacket(); |
| 1179 | |
| 1180 | run_fast_path = |
| 1181 | !has_handshake && state != FIN_AND_PADDING && !HasPendingFrames() && |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 1182 | write_length - total_bytes_consumed > kMaxOutgoingPacketSize && |
| 1183 | latched_hard_max_packet_length_ == 0; |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | if (run_fast_path) { |
| 1187 | return ConsumeDataFastPath(id, write_length, offset, state != NO_FIN, |
| 1188 | total_bytes_consumed); |
| 1189 | } |
| 1190 | |
| 1191 | // Don't allow the handshake to be bundled with other retransmittable frames. |
| 1192 | if (has_handshake) { |
| 1193 | FlushCurrentPacket(); |
| 1194 | } |
| 1195 | |
| 1196 | return QuicConsumedData(total_bytes_consumed, fin_consumed); |
| 1197 | } |
| 1198 | |
| 1199 | QuicConsumedData QuicPacketCreator::ConsumeDataFastPath( |
| 1200 | QuicStreamId id, |
| 1201 | size_t write_length, |
| 1202 | QuicStreamOffset offset, |
| 1203 | bool fin, |
| 1204 | size_t total_bytes_consumed) { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1205 | DCHECK(!QuicUtils::IsCryptoStreamId(transport_version(), id)); |
| 1206 | |
| 1207 | while (total_bytes_consumed < write_length && |
| 1208 | delegate_->ShouldGeneratePacket(HAS_RETRANSMITTABLE_DATA, |
| 1209 | NOT_HANDSHAKE)) { |
| 1210 | // Serialize and encrypt the packet. |
| 1211 | size_t bytes_consumed = 0; |
| 1212 | CreateAndSerializeStreamFrame(id, write_length, total_bytes_consumed, |
| 1213 | offset + total_bytes_consumed, fin, |
| 1214 | next_transmission_type_, &bytes_consumed); |
fayang | d99c2c1 | 2019-11-08 13:22:51 -0800 | [diff] [blame] | 1215 | if (GetQuicReloadableFlag( |
| 1216 | quic_close_connection_on_failed_consume_data_fast_path)) { |
| 1217 | QUIC_RELOADABLE_FLAG_COUNT( |
| 1218 | quic_close_connection_on_failed_consume_data_fast_path); |
| 1219 | if (bytes_consumed == 0) { |
| 1220 | const std::string error_details = |
| 1221 | "Failed in CreateAndSerializeStreamFrame."; |
| 1222 | QUIC_BUG << error_details; |
| 1223 | delegate_->OnUnrecoverableError(QUIC_FAILED_TO_SERIALIZE_PACKET, |
| 1224 | error_details); |
| 1225 | break; |
| 1226 | } |
| 1227 | } |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1228 | total_bytes_consumed += bytes_consumed; |
| 1229 | } |
| 1230 | |
| 1231 | return QuicConsumedData(total_bytes_consumed, |
| 1232 | fin && (total_bytes_consumed == write_length)); |
| 1233 | } |
| 1234 | |
| 1235 | size_t QuicPacketCreator::ConsumeCryptoData(EncryptionLevel level, |
| 1236 | size_t write_length, |
| 1237 | QuicStreamOffset offset) { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1238 | QUIC_BUG_IF(!flusher_attached_) << "Packet flusher is not attached when " |
| 1239 | "generator tries to write crypto data."; |
| 1240 | MaybeBundleAckOpportunistically(); |
| 1241 | // To make reasoning about crypto frames easier, we don't combine them with |
| 1242 | // other retransmittable frames in a single packet. |
| 1243 | // TODO(nharper): Once we have separate packet number spaces, everything |
| 1244 | // should be driven by encryption level, and we should stop flushing in this |
| 1245 | // spot. |
| 1246 | if (HasPendingRetransmittableFrames()) { |
| 1247 | FlushCurrentPacket(); |
| 1248 | } |
| 1249 | |
| 1250 | size_t total_bytes_consumed = 0; |
| 1251 | |
| 1252 | while (total_bytes_consumed < write_length) { |
| 1253 | QuicFrame frame; |
| 1254 | if (!ConsumeCryptoDataToFillCurrentPacket( |
| 1255 | level, write_length - total_bytes_consumed, |
| 1256 | offset + total_bytes_consumed, fully_pad_crypto_handshake_packets_, |
| 1257 | next_transmission_type_, &frame)) { |
| 1258 | // The only pending data in the packet is non-retransmittable frames. I'm |
| 1259 | // assuming here that they won't occupy so much of the packet that a |
| 1260 | // CRYPTO frame won't fit. |
| 1261 | QUIC_BUG << "Failed to ConsumeCryptoData at level " << level; |
| 1262 | return 0; |
| 1263 | } |
| 1264 | total_bytes_consumed += frame.crypto_frame->data_length; |
| 1265 | FlushCurrentPacket(); |
| 1266 | } |
| 1267 | |
| 1268 | // Don't allow the handshake to be bundled with other retransmittable frames. |
| 1269 | FlushCurrentPacket(); |
| 1270 | |
| 1271 | return total_bytes_consumed; |
| 1272 | } |
| 1273 | |
| 1274 | void QuicPacketCreator::GenerateMtuDiscoveryPacket(QuicByteCount target_mtu) { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1275 | // MTU discovery frames must be sent by themselves. |
| 1276 | if (!CanSetMaxPacketLength()) { |
| 1277 | QUIC_BUG << "MTU discovery packets should only be sent when no other " |
| 1278 | << "frames needs to be sent."; |
| 1279 | return; |
| 1280 | } |
| 1281 | const QuicByteCount current_mtu = max_packet_length(); |
| 1282 | |
| 1283 | // The MTU discovery frame is allocated on the stack, since it is going to be |
| 1284 | // serialized within this function. |
| 1285 | QuicMtuDiscoveryFrame mtu_discovery_frame; |
| 1286 | QuicFrame frame(mtu_discovery_frame); |
| 1287 | |
| 1288 | // Send the probe packet with the new length. |
| 1289 | SetMaxPacketLength(target_mtu); |
| 1290 | const bool success = AddPaddedSavedFrame(frame, next_transmission_type_); |
| 1291 | FlushCurrentPacket(); |
| 1292 | // The only reason AddFrame can fail is that the packet is too full to fit in |
| 1293 | // a ping. This is not possible for any sane MTU. |
ianswett | bd78ea1 | 2019-09-26 06:32:37 -0700 | [diff] [blame] | 1294 | QUIC_BUG_IF(!success) << "Failed to send path MTU target_mtu:" << target_mtu |
| 1295 | << " transmission_type:" << next_transmission_type_; |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1296 | |
| 1297 | // Reset the packet length back. |
| 1298 | SetMaxPacketLength(current_mtu); |
| 1299 | } |
| 1300 | |
| 1301 | void QuicPacketCreator::MaybeBundleAckOpportunistically() { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1302 | if (has_ack()) { |
| 1303 | // Ack already queued, nothing to do. |
| 1304 | return; |
| 1305 | } |
| 1306 | if (!delegate_->ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, |
| 1307 | NOT_HANDSHAKE)) { |
| 1308 | return; |
| 1309 | } |
| 1310 | const bool flushed = |
| 1311 | FlushAckFrame(delegate_->MaybeBundleAckOpportunistically()); |
ianswett | bd78ea1 | 2019-09-26 06:32:37 -0700 | [diff] [blame] | 1312 | QUIC_BUG_IF(!flushed) << "Failed to flush ACK frame. encryption_level:" |
| 1313 | << packet_.encryption_level; |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | bool QuicPacketCreator::FlushAckFrame(const QuicFrames& frames) { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1317 | QUIC_BUG_IF(!flusher_attached_) << "Packet flusher is not attached when " |
| 1318 | "generator tries to send ACK frame."; |
| 1319 | for (const auto& frame : frames) { |
| 1320 | DCHECK(frame.type == ACK_FRAME || frame.type == STOP_WAITING_FRAME); |
| 1321 | if (HasPendingFrames()) { |
renjietang | b63005e | 2019-11-19 23:08:53 -0800 | [diff] [blame] | 1322 | if (AddFrame(frame, next_transmission_type_)) { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1323 | // There is pending frames and current frame fits. |
| 1324 | continue; |
| 1325 | } |
| 1326 | } |
| 1327 | DCHECK(!HasPendingFrames()); |
| 1328 | // There is no pending frames, consult the delegate whether a packet can be |
| 1329 | // generated. |
| 1330 | if (!delegate_->ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, |
| 1331 | NOT_HANDSHAKE)) { |
| 1332 | return false; |
| 1333 | } |
renjietang | b63005e | 2019-11-19 23:08:53 -0800 | [diff] [blame] | 1334 | const bool success = AddFrame(frame, next_transmission_type_); |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1335 | QUIC_BUG_IF(!success) << "Failed to flush " << frame; |
| 1336 | } |
| 1337 | return true; |
| 1338 | } |
| 1339 | |
| 1340 | void QuicPacketCreator::AddRandomPadding() { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1341 | AddPendingPadding(random_->RandUint64() % kMaxNumRandomPaddingBytes + 1); |
| 1342 | } |
| 1343 | |
| 1344 | void QuicPacketCreator::AttachPacketFlusher() { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1345 | flusher_attached_ = true; |
| 1346 | if (!write_start_packet_number_.IsInitialized()) { |
| 1347 | write_start_packet_number_ = NextSendingPacketNumber(); |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | void QuicPacketCreator::Flush() { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1352 | FlushCurrentPacket(); |
| 1353 | SendRemainingPendingPadding(); |
| 1354 | flusher_attached_ = false; |
| 1355 | if (GetQuicFlag(FLAGS_quic_export_server_num_packets_per_write_histogram)) { |
| 1356 | if (!write_start_packet_number_.IsInitialized()) { |
| 1357 | QUIC_BUG << "write_start_packet_number is not initialized"; |
| 1358 | return; |
| 1359 | } |
| 1360 | QUIC_SERVER_HISTOGRAM_COUNTS( |
| 1361 | "quic_server_num_written_packets_per_write", |
| 1362 | NextSendingPacketNumber() - write_start_packet_number_, 1, 200, 50, |
| 1363 | "Number of QUIC packets written per write operation"); |
| 1364 | } |
| 1365 | write_start_packet_number_.Clear(); |
| 1366 | } |
| 1367 | |
| 1368 | void QuicPacketCreator::SendRemainingPendingPadding() { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1369 | while ( |
| 1370 | pending_padding_bytes() > 0 && !HasPendingFrames() && |
| 1371 | delegate_->ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, NOT_HANDSHAKE)) { |
| 1372 | FlushCurrentPacket(); |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | void QuicPacketCreator::SetServerConnectionIdLength(uint32_t length) { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1377 | if (length == 0) { |
| 1378 | SetServerConnectionIdIncluded(CONNECTION_ID_ABSENT); |
| 1379 | } else { |
| 1380 | SetServerConnectionIdIncluded(CONNECTION_ID_PRESENT); |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | void QuicPacketCreator::SetTransmissionType(TransmissionType type) { |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 1385 | next_transmission_type_ = type; |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | MessageStatus QuicPacketCreator::AddMessageFrame(QuicMessageId message_id, |
| 1389 | QuicMemSliceSpan message) { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1390 | QUIC_BUG_IF(!flusher_attached_) << "Packet flusher is not attached when " |
| 1391 | "generator tries to add message frame."; |
| 1392 | MaybeBundleAckOpportunistically(); |
| 1393 | const QuicByteCount message_length = message.total_length(); |
| 1394 | if (message_length > GetCurrentLargestMessagePayload()) { |
| 1395 | return MESSAGE_STATUS_TOO_LARGE; |
| 1396 | } |
| 1397 | if (!HasRoomForMessageFrame(message_length)) { |
| 1398 | FlushCurrentPacket(); |
| 1399 | } |
| 1400 | QuicMessageFrame* frame = new QuicMessageFrame(message_id, message); |
renjietang | b63005e | 2019-11-19 23:08:53 -0800 | [diff] [blame] | 1401 | const bool success = AddFrame(QuicFrame(frame), next_transmission_type_); |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1402 | if (!success) { |
| 1403 | QUIC_BUG << "Failed to send message " << message_id; |
| 1404 | delete frame; |
| 1405 | return MESSAGE_STATUS_INTERNAL_ERROR; |
| 1406 | } |
| 1407 | return MESSAGE_STATUS_SUCCESS; |
| 1408 | } |
| 1409 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1410 | QuicVariableLengthIntegerLength QuicPacketCreator::GetLengthLength() const { |
| 1411 | if (QuicVersionHasLongHeaderLengths(framer_->transport_version()) && |
| 1412 | HasIetfLongHeader()) { |
| 1413 | QuicLongHeaderType long_header_type = |
| 1414 | EncryptionlevelToLongHeaderType(packet_.encryption_level); |
| 1415 | if (long_header_type == INITIAL || long_header_type == ZERO_RTT_PROTECTED || |
| 1416 | long_header_type == HANDSHAKE) { |
| 1417 | return VARIABLE_LENGTH_INTEGER_LENGTH_2; |
| 1418 | } |
| 1419 | } |
| 1420 | return VARIABLE_LENGTH_INTEGER_LENGTH_0; |
| 1421 | } |
| 1422 | |
| 1423 | void QuicPacketCreator::FillPacketHeader(QuicPacketHeader* header) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1424 | header->destination_connection_id = GetDestinationConnectionId(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1425 | header->destination_connection_id_included = |
| 1426 | GetDestinationConnectionIdIncluded(); |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1427 | header->source_connection_id = GetSourceConnectionId(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1428 | header->source_connection_id_included = GetSourceConnectionIdIncluded(); |
| 1429 | header->reset_flag = false; |
| 1430 | header->version_flag = IncludeVersionInHeader(); |
| 1431 | if (IncludeNonceInPublicHeader()) { |
| 1432 | DCHECK_EQ(Perspective::IS_SERVER, framer_->perspective()); |
| 1433 | header->nonce = &diversification_nonce_; |
| 1434 | } else { |
| 1435 | header->nonce = nullptr; |
| 1436 | } |
fayang | 354c942 | 2019-05-21 08:10:35 -0700 | [diff] [blame] | 1437 | packet_.packet_number = NextSendingPacketNumber(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1438 | header->packet_number = packet_.packet_number; |
| 1439 | header->packet_number_length = GetPacketNumberLength(); |
| 1440 | header->retry_token_length_length = GetRetryTokenLengthLength(); |
| 1441 | header->retry_token = GetRetryToken(); |
| 1442 | header->length_length = GetLengthLength(); |
| 1443 | header->remaining_packet_length = 0; |
| 1444 | if (!HasIetfLongHeader()) { |
| 1445 | return; |
| 1446 | } |
| 1447 | header->long_packet_type = |
| 1448 | EncryptionlevelToLongHeaderType(packet_.encryption_level); |
| 1449 | } |
| 1450 | |
| 1451 | bool QuicPacketCreator::AddFrame(const QuicFrame& frame, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1452 | TransmissionType transmission_type) { |
| 1453 | QUIC_DVLOG(1) << ENDPOINT << "Adding frame with transmission type " |
dschinazi | ef79a5f | 2019-10-04 10:32:54 -0700 | [diff] [blame] | 1454 | << TransmissionTypeToString(transmission_type) << ": " << frame; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1455 | if (frame.type == STREAM_FRAME && |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1456 | !QuicUtils::IsCryptoStreamId(framer_->transport_version(), |
| 1457 | frame.stream_frame.stream_id) && |
fayang | 4952323 | 2019-05-03 06:28:22 -0700 | [diff] [blame] | 1458 | (packet_.encryption_level == ENCRYPTION_INITIAL || |
| 1459 | packet_.encryption_level == ENCRYPTION_HANDSHAKE)) { |
dschinazi | ef79a5f | 2019-10-04 10:32:54 -0700 | [diff] [blame] | 1460 | const std::string error_details = |
| 1461 | QuicStrCat("Cannot send stream data with level: ", |
| 1462 | EncryptionLevelToString(packet_.encryption_level)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1463 | QUIC_BUG << error_details; |
| 1464 | delegate_->OnUnrecoverableError( |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 1465 | QUIC_ATTEMPT_TO_SEND_UNENCRYPTED_STREAM_DATA, error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1466 | return false; |
| 1467 | } |
renjietang | dbe9834 | 2019-10-18 11:00:57 -0700 | [diff] [blame] | 1468 | |
renjietang | a66e415 | 2019-11-11 13:23:33 -0800 | [diff] [blame] | 1469 | if (GetQuicRestartFlag(quic_coalesce_stream_frames_2) && |
renjietang | dbe9834 | 2019-10-18 11:00:57 -0700 | [diff] [blame] | 1470 | frame.type == STREAM_FRAME && |
| 1471 | MaybeCoalesceStreamFrame(frame.stream_frame)) { |
renjietang | a66e415 | 2019-11-11 13:23:33 -0800 | [diff] [blame] | 1472 | QUIC_RESTART_FLAG_COUNT_N(quic_coalesce_stream_frames_2, 1, 3); |
renjietang | dbe9834 | 2019-10-18 11:00:57 -0700 | [diff] [blame] | 1473 | return true; |
| 1474 | } |
| 1475 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1476 | size_t frame_len = framer_->GetSerializedFrameLength( |
| 1477 | frame, BytesFree(), queued_frames_.empty(), |
| 1478 | /* last_frame_in_packet= */ true, GetPacketNumberLength()); |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 1479 | if (frame_len == 0 && RemoveSoftMaxPacketLength()) { |
| 1480 | // Remove soft max_packet_length and retry. |
| 1481 | frame_len = framer_->GetSerializedFrameLength( |
| 1482 | frame, BytesFree(), queued_frames_.empty(), |
| 1483 | /* last_frame_in_packet= */ true, GetPacketNumberLength()); |
| 1484 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1485 | if (frame_len == 0) { |
| 1486 | // Current open packet is full. |
fayang | 62b637b | 2019-09-16 08:40:49 -0700 | [diff] [blame] | 1487 | FlushCurrentPacket(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1488 | return false; |
| 1489 | } |
| 1490 | DCHECK_LT(0u, packet_size_); |
| 1491 | |
| 1492 | packet_size_ += ExpansionOnNewFrame() + frame_len; |
| 1493 | |
fayang | 347ab75 | 2019-10-22 11:17:43 -0700 | [diff] [blame] | 1494 | if (QuicUtils::IsRetransmittableFrame(frame.type)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1495 | packet_.retransmittable_frames.push_back(frame); |
| 1496 | queued_frames_.push_back(frame); |
| 1497 | if (QuicUtils::IsHandshakeFrame(frame, framer_->transport_version())) { |
| 1498 | packet_.has_crypto_handshake = IS_HANDSHAKE; |
| 1499 | } |
| 1500 | } else { |
fayang | 02a2874 | 2019-12-04 07:09:38 -0800 | [diff] [blame^] | 1501 | if (frame.type == PADDING_FRAME && |
| 1502 | frame.padding_frame.num_padding_bytes == -1) { |
| 1503 | // Populate the actual length of full padding frame, such that one can |
| 1504 | // know how much padding is actually added. |
| 1505 | packet_.nonretransmittable_frames.push_back( |
| 1506 | QuicFrame(QuicPaddingFrame(frame_len))); |
| 1507 | } else { |
| 1508 | packet_.nonretransmittable_frames.push_back(frame); |
fayang | 51152fd | 2019-10-21 06:48:09 -0700 | [diff] [blame] | 1509 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1510 | queued_frames_.push_back(frame); |
| 1511 | } |
| 1512 | |
| 1513 | if (frame.type == ACK_FRAME) { |
| 1514 | packet_.has_ack = true; |
| 1515 | packet_.largest_acked = LargestAcked(*frame.ack_frame); |
| 1516 | } |
| 1517 | if (frame.type == STOP_WAITING_FRAME) { |
| 1518 | packet_.has_stop_waiting = true; |
| 1519 | } |
| 1520 | if (debug_delegate_ != nullptr) { |
| 1521 | debug_delegate_->OnFrameAddedToPacket(frame); |
| 1522 | } |
| 1523 | |
| 1524 | // Packet transmission type is determined by the last added retransmittable |
| 1525 | // frame. |
fayang | cff885a | 2019-10-22 07:39:04 -0700 | [diff] [blame] | 1526 | if (QuicUtils::IsRetransmittableFrame(frame.type)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1527 | packet_.transmission_type = transmission_type; |
| 1528 | } |
| 1529 | return true; |
| 1530 | } |
| 1531 | |
renjietang | dbe9834 | 2019-10-18 11:00:57 -0700 | [diff] [blame] | 1532 | bool QuicPacketCreator::MaybeCoalesceStreamFrame(const QuicStreamFrame& frame) { |
| 1533 | if (queued_frames_.empty() || queued_frames_.back().type != STREAM_FRAME) { |
| 1534 | return false; |
| 1535 | } |
| 1536 | QuicStreamFrame* candidate = &queued_frames_.back().stream_frame; |
| 1537 | if (candidate->stream_id != frame.stream_id || |
| 1538 | candidate->offset + candidate->data_length != frame.offset || |
| 1539 | frame.data_length > BytesFree()) { |
| 1540 | return false; |
| 1541 | } |
| 1542 | candidate->data_length += frame.data_length; |
| 1543 | candidate->fin = frame.fin; |
| 1544 | |
| 1545 | // The back of retransmittable frames must be the same as the original |
| 1546 | // queued frames' back. |
| 1547 | DCHECK_EQ(packet_.retransmittable_frames.back().type, STREAM_FRAME); |
| 1548 | QuicStreamFrame* retransmittable = |
| 1549 | &packet_.retransmittable_frames.back().stream_frame; |
| 1550 | DCHECK_EQ(retransmittable->stream_id, frame.stream_id); |
| 1551 | DCHECK_EQ(retransmittable->offset + retransmittable->data_length, |
| 1552 | frame.offset); |
| 1553 | retransmittable->data_length = candidate->data_length; |
| 1554 | retransmittable->fin = candidate->fin; |
| 1555 | packet_size_ += frame.data_length; |
| 1556 | if (debug_delegate_ != nullptr) { |
| 1557 | debug_delegate_->OnStreamFrameCoalesced(*candidate); |
| 1558 | } |
| 1559 | return true; |
| 1560 | } |
| 1561 | |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 1562 | bool QuicPacketCreator::RemoveSoftMaxPacketLength() { |
| 1563 | if (latched_hard_max_packet_length_ == 0) { |
| 1564 | return false; |
| 1565 | } |
| 1566 | if (!CanSetMaxPacketLength()) { |
| 1567 | return false; |
| 1568 | } |
| 1569 | QUIC_DVLOG(1) << "Restoring max packet length to: " |
| 1570 | << latched_hard_max_packet_length_; |
| 1571 | SetMaxPacketLength(latched_hard_max_packet_length_); |
| 1572 | // Reset latched_max_packet_length_. |
| 1573 | latched_hard_max_packet_length_ = 0; |
| 1574 | return true; |
| 1575 | } |
| 1576 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1577 | void QuicPacketCreator::MaybeAddPadding() { |
| 1578 | // The current packet should have no padding bytes because padding is only |
| 1579 | // added when this method is called just before the packet is serialized. |
| 1580 | DCHECK_EQ(0, packet_.num_padding_bytes); |
| 1581 | if (BytesFree() == 0) { |
| 1582 | // Don't pad full packets. |
| 1583 | return; |
| 1584 | } |
| 1585 | |
| 1586 | if (packet_.transmission_type == PROBING_RETRANSMISSION) { |
| 1587 | needs_full_padding_ = true; |
| 1588 | } |
| 1589 | |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 1590 | // Packet coalescer pads INITIAL packets, so the creator should not. |
| 1591 | if (framer_->version().CanSendCoalescedPackets() && |
| 1592 | (packet_.encryption_level == ENCRYPTION_INITIAL || |
| 1593 | packet_.encryption_level == ENCRYPTION_HANDSHAKE)) { |
| 1594 | // TODO(fayang): MTU discovery packets should not ever be sent as |
| 1595 | // ENCRYPTION_INITIAL or ENCRYPTION_HANDSHAKE. |
| 1596 | bool is_mtu_discovery = false; |
| 1597 | for (const auto& frame : packet_.nonretransmittable_frames) { |
| 1598 | if (frame.type == MTU_DISCOVERY_FRAME) { |
| 1599 | is_mtu_discovery = true; |
| 1600 | break; |
| 1601 | } |
| 1602 | } |
| 1603 | if (!is_mtu_discovery) { |
| 1604 | // Do not add full padding if connection tries to coalesce packet. |
| 1605 | needs_full_padding_ = false; |
| 1606 | } |
| 1607 | } |
| 1608 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1609 | // Header protection requires a minimum plaintext packet size. |
| 1610 | size_t extra_padding_bytes = 0; |
| 1611 | if (framer_->version().HasHeaderProtection()) { |
| 1612 | size_t frame_bytes = PacketSize() - PacketHeaderSize(); |
| 1613 | |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1614 | if (frame_bytes + pending_padding_bytes_ < |
| 1615 | MinPlaintextPacketSize(framer_->version()) && |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1616 | !needs_full_padding_) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1617 | extra_padding_bytes = |
| 1618 | MinPlaintextPacketSize(framer_->version()) - frame_bytes; |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | if (!needs_full_padding_ && pending_padding_bytes_ == 0 && |
| 1623 | extra_padding_bytes == 0) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1624 | // Do not need padding. |
| 1625 | return; |
| 1626 | } |
| 1627 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1628 | int padding_bytes = -1; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1629 | if (needs_full_padding_) { |
| 1630 | // Full padding does not consume pending padding bytes. |
| 1631 | packet_.num_padding_bytes = -1; |
| 1632 | } else { |
| 1633 | packet_.num_padding_bytes = |
| 1634 | std::min<int16_t>(pending_padding_bytes_, BytesFree()); |
| 1635 | pending_padding_bytes_ -= packet_.num_padding_bytes; |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1636 | padding_bytes = |
| 1637 | std::max<int16_t>(packet_.num_padding_bytes, extra_padding_bytes); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1638 | } |
| 1639 | |
fayang | 347ab75 | 2019-10-22 11:17:43 -0700 | [diff] [blame] | 1640 | bool success = AddFrame(QuicFrame(QuicPaddingFrame(padding_bytes)), |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1641 | packet_.transmission_type); |
dschinazi | ef79a5f | 2019-10-04 10:32:54 -0700 | [diff] [blame] | 1642 | QUIC_BUG_IF(!success) << "Failed to add padding_bytes: " << padding_bytes |
| 1643 | << " transmission_type: " |
| 1644 | << TransmissionTypeToString(packet_.transmission_type); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | bool QuicPacketCreator::IncludeNonceInPublicHeader() const { |
| 1648 | return have_diversification_nonce_ && |
| 1649 | packet_.encryption_level == ENCRYPTION_ZERO_RTT; |
| 1650 | } |
| 1651 | |
| 1652 | bool QuicPacketCreator::IncludeVersionInHeader() const { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1653 | if (VersionHasIetfInvariantHeader(framer_->transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1654 | return packet_.encryption_level < ENCRYPTION_FORWARD_SECURE; |
| 1655 | } |
| 1656 | return send_version_in_packet_; |
| 1657 | } |
| 1658 | |
| 1659 | void QuicPacketCreator::AddPendingPadding(QuicByteCount size) { |
| 1660 | pending_padding_bytes_ += size; |
| 1661 | } |
| 1662 | |
ianswett | e28f022 | 2019-04-04 13:31:22 -0700 | [diff] [blame] | 1663 | bool QuicPacketCreator::StreamFrameIsClientHello( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1664 | const QuicStreamFrame& frame) const { |
| 1665 | if (framer_->perspective() == Perspective::IS_SERVER || |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1666 | !QuicUtils::IsCryptoStreamId(framer_->transport_version(), |
| 1667 | frame.stream_id)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1668 | return false; |
| 1669 | } |
ianswett | e28f022 | 2019-04-04 13:31:22 -0700 | [diff] [blame] | 1670 | // The ClientHello is always sent with INITIAL encryption. |
| 1671 | return packet_.encryption_level == ENCRYPTION_INITIAL; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1672 | } |
| 1673 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 1674 | void QuicPacketCreator::SetServerConnectionIdIncluded( |
| 1675 | QuicConnectionIdIncluded server_connection_id_included) { |
| 1676 | DCHECK(server_connection_id_included == CONNECTION_ID_PRESENT || |
| 1677 | server_connection_id_included == CONNECTION_ID_ABSENT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1678 | DCHECK(framer_->perspective() == Perspective::IS_SERVER || |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 1679 | server_connection_id_included != CONNECTION_ID_ABSENT); |
| 1680 | server_connection_id_included_ = server_connection_id_included; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1681 | } |
| 1682 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 1683 | void QuicPacketCreator::SetServerConnectionId( |
| 1684 | QuicConnectionId server_connection_id) { |
| 1685 | server_connection_id_ = server_connection_id; |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 1688 | void QuicPacketCreator::SetClientConnectionId( |
| 1689 | QuicConnectionId client_connection_id) { |
| 1690 | DCHECK(client_connection_id.IsEmpty() || |
| 1691 | framer_->version().SupportsClientConnectionIds()); |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 1692 | client_connection_id_ = client_connection_id; |
| 1693 | } |
| 1694 | |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1695 | QuicPacketLength QuicPacketCreator::GetCurrentLargestMessagePayload() const { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1696 | if (!VersionSupportsMessageFrames(framer_->transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1697 | return 0; |
| 1698 | } |
| 1699 | const size_t packet_header_size = GetPacketHeaderSize( |
| 1700 | framer_->transport_version(), GetDestinationConnectionIdLength(), |
| 1701 | GetSourceConnectionIdLength(), IncludeVersionInHeader(), |
| 1702 | IncludeNonceInPublicHeader(), GetPacketNumberLength(), |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1703 | // No Retry token on packets containing application data. |
| 1704 | VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, GetLengthLength()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1705 | // This is the largest possible message payload when the length field is |
| 1706 | // omitted. |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 1707 | size_t max_plaintext_size = |
| 1708 | latched_hard_max_packet_length_ == 0 |
| 1709 | ? max_plaintext_size_ |
| 1710 | : framer_->GetMaxPlaintextSize(latched_hard_max_packet_length_); |
| 1711 | return max_plaintext_size - |
| 1712 | std::min(max_plaintext_size, packet_header_size + kQuicFrameTypeSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1713 | } |
| 1714 | |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1715 | QuicPacketLength QuicPacketCreator::GetGuaranteedLargestMessagePayload() const { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1716 | if (!VersionSupportsMessageFrames(framer_->transport_version())) { |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1717 | return 0; |
| 1718 | } |
| 1719 | // QUIC Crypto server packets may include a diversification nonce. |
| 1720 | const bool may_include_nonce = |
| 1721 | framer_->version().handshake_protocol == PROTOCOL_QUIC_CRYPTO && |
| 1722 | framer_->perspective() == Perspective::IS_SERVER; |
| 1723 | // IETF QUIC long headers include a length on client 0RTT packets. |
| 1724 | QuicVariableLengthIntegerLength length_length = |
nharper | d43f1d6 | 2019-07-01 15:18:20 -0700 | [diff] [blame] | 1725 | VARIABLE_LENGTH_INTEGER_LENGTH_0; |
| 1726 | if (framer_->perspective() == Perspective::IS_CLIENT) { |
| 1727 | length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2; |
| 1728 | } |
nharper | 405f719 | 2019-09-11 08:28:06 -0700 | [diff] [blame] | 1729 | if (!QuicVersionHasLongHeaderLengths(framer_->transport_version())) { |
nharper | d43f1d6 | 2019-07-01 15:18:20 -0700 | [diff] [blame] | 1730 | length_length = VARIABLE_LENGTH_INTEGER_LENGTH_0; |
| 1731 | } |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1732 | const size_t packet_header_size = GetPacketHeaderSize( |
| 1733 | framer_->transport_version(), GetDestinationConnectionIdLength(), |
| 1734 | // Assume CID lengths don't change, but version may be present. |
| 1735 | GetSourceConnectionIdLength(), kIncludeVersion, may_include_nonce, |
| 1736 | PACKET_4BYTE_PACKET_NUMBER, |
| 1737 | // No Retry token on packets containing application data. |
| 1738 | VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, length_length); |
| 1739 | // This is the largest possible message payload when the length field is |
| 1740 | // omitted. |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 1741 | size_t max_plaintext_size = |
| 1742 | latched_hard_max_packet_length_ == 0 |
| 1743 | ? max_plaintext_size_ |
| 1744 | : framer_->GetMaxPlaintextSize(latched_hard_max_packet_length_); |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1745 | const QuicPacketLength largest_payload = |
fayang | 2ab1e85 | 2019-11-04 11:24:36 -0800 | [diff] [blame] | 1746 | max_plaintext_size - |
| 1747 | std::min(max_plaintext_size, packet_header_size + kQuicFrameTypeSize); |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1748 | // This must always be less than or equal to GetCurrentLargestMessagePayload. |
| 1749 | DCHECK_LE(largest_payload, GetCurrentLargestMessagePayload()); |
| 1750 | return largest_payload; |
| 1751 | } |
| 1752 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1753 | bool QuicPacketCreator::HasIetfLongHeader() const { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1754 | return VersionHasIetfInvariantHeader(framer_->transport_version()) && |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1755 | packet_.encryption_level < ENCRYPTION_FORWARD_SECURE; |
| 1756 | } |
| 1757 | |
nharper | 6f8a761 | 2019-07-08 12:31:20 -0700 | [diff] [blame] | 1758 | // static |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1759 | size_t QuicPacketCreator::MinPlaintextPacketSize( |
| 1760 | const ParsedQuicVersion& version) { |
| 1761 | if (!version.HasHeaderProtection()) { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1762 | return 0; |
| 1763 | } |
| 1764 | // Header protection samples 16 bytes of ciphertext starting 4 bytes after the |
| 1765 | // packet number. In IETF QUIC, all AEAD algorithms have a 16-byte auth tag |
| 1766 | // (i.e. the ciphertext is 16 bytes larger than the plaintext). Since packet |
| 1767 | // numbers could be as small as 1 byte, but the sample starts 4 bytes after |
| 1768 | // the packet number, at least 3 bytes of plaintext are needed to make sure |
| 1769 | // that there is enough ciphertext to sample. |
| 1770 | // |
| 1771 | // Google QUIC crypto uses different AEAD algorithms - in particular the auth |
| 1772 | // tags are only 12 bytes instead of 16 bytes. Since the auth tag is 4 bytes |
| 1773 | // shorter, 4 more bytes of plaintext are needed to guarantee there is enough |
| 1774 | // ciphertext to sample. |
| 1775 | // |
| 1776 | // This method could check for PROTOCOL_TLS1_3 vs PROTOCOL_QUIC_CRYPTO and |
| 1777 | // return 3 when TLS 1.3 is in use (the use of IETF vs Google QUIC crypters is |
| 1778 | // determined based on the handshake protocol used). However, even when TLS |
| 1779 | // 1.3 is used, unittests still use NullEncrypter/NullDecrypter (and other |
| 1780 | // test crypters) which also only use 12 byte tags. |
| 1781 | // |
| 1782 | // TODO(nharper): Set this based on the handshake protocol in use. |
| 1783 | return 7; |
| 1784 | } |
| 1785 | |
fayang | 354c942 | 2019-05-21 08:10:35 -0700 | [diff] [blame] | 1786 | QuicPacketNumber QuicPacketCreator::NextSendingPacketNumber() const { |
| 1787 | if (!packet_number().IsInitialized()) { |
| 1788 | return framer_->first_sending_packet_number(); |
| 1789 | } |
| 1790 | return packet_number() + 1; |
| 1791 | } |
| 1792 | |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1793 | bool QuicPacketCreator::PacketFlusherAttached() const { |
fayang | 18be79a | 2019-09-16 15:17:12 -0700 | [diff] [blame] | 1794 | return flusher_attached_; |
| 1795 | } |
| 1796 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1797 | #undef ENDPOINT // undef for jumbo builds |
| 1798 | } // namespace quic |