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_generator.h" |
| 6 | |
| 7 | #include <cstdint> |
| 8 | |
| 9 | #include "net/third_party/quiche/src/quic/core/crypto/quic_random.h" |
| 10 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
fayang | 354c942 | 2019-05-21 08:10:35 -0700 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_server_stats.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | |
| 19 | namespace quic { |
| 20 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 21 | QuicPacketGenerator::QuicPacketGenerator(QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 22 | QuicFramer* framer, |
| 23 | QuicRandom* random_generator, |
| 24 | DelegateInterface* delegate) |
| 25 | : delegate_(delegate), |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 26 | packet_creator_(server_connection_id, framer, random_generator, delegate), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 27 | next_transmission_type_(NOT_RETRANSMISSION), |
| 28 | flusher_attached_(false), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 29 | random_generator_(random_generator), |
| 30 | fully_pad_crypto_handshake_packets_(true), |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 31 | deprecate_queued_control_frames_( |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 32 | GetQuicReloadableFlag(quic_deprecate_queued_control_frames)) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 33 | |
| 34 | QuicPacketGenerator::~QuicPacketGenerator() { |
| 35 | DeleteFrames(&queued_control_frames_); |
| 36 | } |
| 37 | |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 38 | bool QuicPacketGenerator::ConsumeRetransmittableControlFrame( |
| 39 | const QuicFrame& frame) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | QUIC_BUG_IF(IsControlFrame(frame.type) && !GetControlFrameId(frame)) |
| 41 | << "Adding a control frame with no control frame id: " << frame; |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 42 | DCHECK(QuicUtils::IsRetransmittableFrame(frame.type)) << frame; |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 43 | MaybeBundleAckOpportunistically(); |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 44 | if (deprecate_queued_control_frames_) { |
| 45 | QUIC_RELOADABLE_FLAG_COUNT(quic_deprecate_queued_control_frames); |
| 46 | if (packet_creator_.HasPendingFrames()) { |
| 47 | if (packet_creator_.AddSavedFrame(frame, next_transmission_type_)) { |
| 48 | // There is pending frames and current frame fits. |
| 49 | return true; |
| 50 | } |
| 51 | } |
| 52 | DCHECK(!packet_creator_.HasPendingFrames()); |
| 53 | if (frame.type != PING_FRAME && frame.type != CONNECTION_CLOSE_FRAME && |
| 54 | !delegate_->ShouldGeneratePacket(HAS_RETRANSMITTABLE_DATA, |
| 55 | NOT_HANDSHAKE)) { |
| 56 | // Do not check congestion window for ping or connection close frames. |
| 57 | return false; |
| 58 | } |
| 59 | const bool success = |
| 60 | packet_creator_.AddSavedFrame(frame, next_transmission_type_); |
| 61 | DCHECK(success); |
| 62 | return success; |
| 63 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 64 | queued_control_frames_.push_back(frame); |
| 65 | SendQueuedFrames(/*flush=*/false); |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 66 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | size_t QuicPacketGenerator::ConsumeCryptoData(EncryptionLevel level, |
| 70 | size_t write_length, |
| 71 | QuicStreamOffset offset) { |
| 72 | QUIC_BUG_IF(!flusher_attached_) << "Packet flusher is not attached when " |
| 73 | "generator tries to write crypto data."; |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 74 | MaybeBundleAckOpportunistically(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 75 | // To make reasoning about crypto frames easier, we don't combine them with |
| 76 | // other retransmittable frames in a single packet. |
| 77 | // TODO(nharper): Once we have separate packet number spaces, everything |
| 78 | // should be driven by encryption level, and we should stop flushing in this |
| 79 | // spot. |
| 80 | const bool flush = packet_creator_.HasPendingRetransmittableFrames(); |
| 81 | SendQueuedFrames(flush); |
| 82 | |
| 83 | size_t total_bytes_consumed = 0; |
| 84 | |
| 85 | while (total_bytes_consumed < write_length) { |
| 86 | QuicFrame frame; |
| 87 | if (!packet_creator_.ConsumeCryptoData( |
| 88 | level, write_length - total_bytes_consumed, |
nharper | 51961cf | 2019-05-13 13:23:24 -0700 | [diff] [blame] | 89 | offset + total_bytes_consumed, fully_pad_crypto_handshake_packets_, |
| 90 | next_transmission_type_, &frame)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 91 | // The only pending data in the packet is non-retransmittable frames. I'm |
| 92 | // assuming here that they won't occupy so much of the packet that a |
| 93 | // CRYPTO frame won't fit. |
| 94 | QUIC_BUG << "Failed to ConsumeCryptoData at level " << level; |
| 95 | return 0; |
| 96 | } |
| 97 | total_bytes_consumed += frame.crypto_frame->data_length; |
| 98 | |
| 99 | // TODO(ianswett): Move to having the creator flush itself when it's full. |
| 100 | packet_creator_.Flush(); |
| 101 | } |
| 102 | |
| 103 | // Don't allow the handshake to be bundled with other retransmittable frames. |
| 104 | SendQueuedFrames(/*flush=*/true); |
| 105 | |
| 106 | return total_bytes_consumed; |
| 107 | } |
| 108 | |
| 109 | QuicConsumedData QuicPacketGenerator::ConsumeData(QuicStreamId id, |
| 110 | size_t write_length, |
| 111 | QuicStreamOffset offset, |
| 112 | StreamSendingState state) { |
| 113 | QUIC_BUG_IF(!flusher_attached_) << "Packet flusher is not attached when " |
| 114 | "generator tries to write stream data."; |
| 115 | bool has_handshake = |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 116 | QuicUtils::IsCryptoStreamId(packet_creator_.transport_version(), id); |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 117 | MaybeBundleAckOpportunistically(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 118 | bool fin = state != NO_FIN; |
| 119 | QUIC_BUG_IF(has_handshake && fin) |
| 120 | << "Handshake packets should never send a fin"; |
| 121 | // To make reasoning about crypto frames easier, we don't combine them with |
| 122 | // other retransmittable frames in a single packet. |
| 123 | const bool flush = |
| 124 | has_handshake && packet_creator_.HasPendingRetransmittableFrames(); |
| 125 | SendQueuedFrames(flush); |
| 126 | |
| 127 | size_t total_bytes_consumed = 0; |
| 128 | bool fin_consumed = false; |
| 129 | |
| 130 | if (!packet_creator_.HasRoomForStreamFrame(id, offset, write_length)) { |
| 131 | packet_creator_.Flush(); |
| 132 | } |
| 133 | |
| 134 | if (!fin && (write_length == 0)) { |
| 135 | QUIC_BUG << "Attempt to consume empty data without FIN."; |
| 136 | return QuicConsumedData(0, false); |
| 137 | } |
| 138 | // We determine if we can enter the fast path before executing |
| 139 | // the slow path loop. |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 140 | bool run_fast_path = |
| 141 | !has_handshake && state != FIN_AND_PADDING && !HasQueuedFrames() && |
| 142 | write_length - total_bytes_consumed > kMaxOutgoingPacketSize; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 143 | |
| 144 | while (!run_fast_path && delegate_->ShouldGeneratePacket( |
| 145 | HAS_RETRANSMITTABLE_DATA, |
| 146 | has_handshake ? IS_HANDSHAKE : NOT_HANDSHAKE)) { |
| 147 | QuicFrame frame; |
| 148 | bool needs_full_padding = |
| 149 | has_handshake && fully_pad_crypto_handshake_packets_; |
| 150 | |
ianswett | e28f022 | 2019-04-04 13:31:22 -0700 | [diff] [blame] | 151 | if (!packet_creator_.ConsumeData(id, write_length - total_bytes_consumed, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 152 | offset + total_bytes_consumed, fin, |
| 153 | needs_full_padding, |
| 154 | next_transmission_type_, &frame)) { |
| 155 | // The creator is always flushed if there's not enough room for a new |
| 156 | // stream frame before ConsumeData, so ConsumeData should always succeed. |
| 157 | QUIC_BUG << "Failed to ConsumeData, stream:" << id; |
| 158 | return QuicConsumedData(0, false); |
| 159 | } |
| 160 | |
| 161 | // A stream frame is created and added. |
| 162 | size_t bytes_consumed = frame.stream_frame.data_length; |
| 163 | total_bytes_consumed += bytes_consumed; |
| 164 | fin_consumed = fin && total_bytes_consumed == write_length; |
| 165 | if (fin_consumed && state == FIN_AND_PADDING) { |
| 166 | AddRandomPadding(); |
| 167 | } |
| 168 | DCHECK(total_bytes_consumed == write_length || |
| 169 | (bytes_consumed > 0 && packet_creator_.HasPendingFrames())); |
| 170 | |
| 171 | if (total_bytes_consumed == write_length) { |
| 172 | // We're done writing the data. Exit the loop. |
| 173 | // We don't make this a precondition because we could have 0 bytes of data |
| 174 | // if we're simply writing a fin. |
| 175 | break; |
| 176 | } |
| 177 | // TODO(ianswett): Move to having the creator flush itself when it's full. |
| 178 | packet_creator_.Flush(); |
| 179 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 180 | run_fast_path = |
| 181 | !has_handshake && state != FIN_AND_PADDING && !HasQueuedFrames() && |
| 182 | write_length - total_bytes_consumed > kMaxOutgoingPacketSize; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (run_fast_path) { |
| 186 | return ConsumeDataFastPath(id, write_length, offset, state != NO_FIN, |
| 187 | total_bytes_consumed); |
| 188 | } |
| 189 | |
| 190 | // Don't allow the handshake to be bundled with other retransmittable frames. |
| 191 | if (has_handshake) { |
| 192 | SendQueuedFrames(/*flush=*/true); |
| 193 | } |
| 194 | |
| 195 | return QuicConsumedData(total_bytes_consumed, fin_consumed); |
| 196 | } |
| 197 | |
| 198 | QuicConsumedData QuicPacketGenerator::ConsumeDataFastPath( |
| 199 | QuicStreamId id, |
| 200 | size_t write_length, |
| 201 | QuicStreamOffset offset, |
| 202 | bool fin, |
| 203 | size_t total_bytes_consumed) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 204 | DCHECK(!QuicUtils::IsCryptoStreamId(packet_creator_.transport_version(), id)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 205 | |
| 206 | while (total_bytes_consumed < write_length && |
| 207 | delegate_->ShouldGeneratePacket(HAS_RETRANSMITTABLE_DATA, |
| 208 | NOT_HANDSHAKE)) { |
| 209 | // Serialize and encrypt the packet. |
| 210 | size_t bytes_consumed = 0; |
| 211 | packet_creator_.CreateAndSerializeStreamFrame( |
| 212 | id, write_length, total_bytes_consumed, offset + total_bytes_consumed, |
| 213 | fin, next_transmission_type_, &bytes_consumed); |
| 214 | total_bytes_consumed += bytes_consumed; |
| 215 | } |
| 216 | |
| 217 | return QuicConsumedData(total_bytes_consumed, |
| 218 | fin && (total_bytes_consumed == write_length)); |
| 219 | } |
| 220 | |
| 221 | void QuicPacketGenerator::GenerateMtuDiscoveryPacket(QuicByteCount target_mtu) { |
| 222 | // MTU discovery frames must be sent by themselves. |
| 223 | if (!packet_creator_.CanSetMaxPacketLength()) { |
| 224 | QUIC_BUG << "MTU discovery packets should only be sent when no other " |
| 225 | << "frames needs to be sent."; |
| 226 | return; |
| 227 | } |
| 228 | const QuicByteCount current_mtu = GetCurrentMaxPacketLength(); |
| 229 | |
| 230 | // The MTU discovery frame is allocated on the stack, since it is going to be |
| 231 | // serialized within this function. |
| 232 | QuicMtuDiscoveryFrame mtu_discovery_frame; |
| 233 | QuicFrame frame(mtu_discovery_frame); |
| 234 | |
| 235 | // Send the probe packet with the new length. |
| 236 | SetMaxPacketLength(target_mtu); |
| 237 | const bool success = |
| 238 | packet_creator_.AddPaddedSavedFrame(frame, next_transmission_type_); |
| 239 | packet_creator_.Flush(); |
| 240 | // The only reason AddFrame can fail is that the packet is too full to fit in |
| 241 | // a ping. This is not possible for any sane MTU. |
| 242 | DCHECK(success); |
| 243 | |
| 244 | // Reset the packet length back. |
| 245 | SetMaxPacketLength(current_mtu); |
| 246 | } |
| 247 | |
| 248 | bool QuicPacketGenerator::CanSendWithNextPendingFrameAddition() const { |
| 249 | DCHECK(HasPendingFrames() || packet_creator_.pending_padding_bytes() > 0); |
| 250 | HasRetransmittableData retransmittable = |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 251 | packet_creator_.pending_padding_bytes() > 0 ? NO_RETRANSMITTABLE_DATA |
| 252 | : HAS_RETRANSMITTABLE_DATA; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 253 | if (retransmittable == HAS_RETRANSMITTABLE_DATA) { |
| 254 | DCHECK(!queued_control_frames_.empty()); // These are retransmittable. |
| 255 | } |
| 256 | return delegate_->ShouldGeneratePacket(retransmittable, NOT_HANDSHAKE); |
| 257 | } |
| 258 | |
| 259 | void QuicPacketGenerator::SendQueuedFrames(bool flush) { |
| 260 | // Only add pending frames if we are SURE we can then send the whole packet. |
| 261 | while (HasPendingFrames() && |
| 262 | (flush || CanSendWithNextPendingFrameAddition())) { |
| 263 | bool first_frame = packet_creator_.CanSetMaxPacketLength(); |
| 264 | if (!AddNextPendingFrame() && first_frame) { |
| 265 | // A single frame cannot fit into the packet, tear down the connection. |
| 266 | QUIC_BUG << "A single frame cannot fit into packet." |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 267 | << " number of queued_control_frames: " |
| 268 | << queued_control_frames_.size(); |
| 269 | if (!queued_control_frames_.empty()) { |
| 270 | QUIC_LOG(INFO) << queued_control_frames_[0]; |
| 271 | } |
| 272 | delegate_->OnUnrecoverableError(QUIC_FAILED_TO_SERIALIZE_PACKET, |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 273 | "Single frame cannot fit into a packet"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | } |
| 277 | if (flush) { |
| 278 | packet_creator_.Flush(); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | bool QuicPacketGenerator::PacketFlusherAttached() const { |
| 283 | return flusher_attached_; |
| 284 | } |
| 285 | |
| 286 | void QuicPacketGenerator::AttachPacketFlusher() { |
| 287 | flusher_attached_ = true; |
fayang | 354c942 | 2019-05-21 08:10:35 -0700 | [diff] [blame] | 288 | if (!write_start_packet_number_.IsInitialized()) { |
| 289 | write_start_packet_number_ = packet_creator_.NextSendingPacketNumber(); |
| 290 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void QuicPacketGenerator::Flush() { |
| 294 | SendQueuedFrames(/*flush=*/false); |
| 295 | packet_creator_.Flush(); |
| 296 | SendRemainingPendingPadding(); |
| 297 | flusher_attached_ = false; |
fayang | 354c942 | 2019-05-21 08:10:35 -0700 | [diff] [blame] | 298 | if (GetQuicFlag(FLAGS_quic_export_server_num_packets_per_write_histogram)) { |
| 299 | if (!write_start_packet_number_.IsInitialized()) { |
| 300 | QUIC_BUG << "write_start_packet_number is not initialized"; |
| 301 | return; |
| 302 | } |
| 303 | QUIC_SERVER_HISTOGRAM_COUNTS( |
| 304 | "quic_server_num_written_packets_per_write", |
| 305 | packet_creator_.NextSendingPacketNumber() - write_start_packet_number_, |
| 306 | 1, 200, 50, "Number of QUIC packets written per write operation"); |
| 307 | } |
| 308 | write_start_packet_number_.Clear(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void QuicPacketGenerator::FlushAllQueuedFrames() { |
| 312 | SendQueuedFrames(/*flush=*/true); |
| 313 | } |
| 314 | |
| 315 | bool QuicPacketGenerator::HasQueuedFrames() const { |
| 316 | return packet_creator_.HasPendingFrames() || HasPendingFrames(); |
| 317 | } |
| 318 | |
| 319 | bool QuicPacketGenerator::IsPendingPacketEmpty() const { |
| 320 | return !packet_creator_.HasPendingFrames(); |
| 321 | } |
| 322 | |
| 323 | bool QuicPacketGenerator::HasPendingFrames() const { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 324 | return !queued_control_frames_.empty(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | bool QuicPacketGenerator::AddNextPendingFrame() { |
| 328 | QUIC_BUG_IF(!flusher_attached_) << "Packet flusher is not attached when " |
| 329 | "generator tries to write control frames."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 330 | QUIC_BUG_IF(queued_control_frames_.empty()) |
| 331 | << "AddNextPendingFrame called with no queued control frames."; |
| 332 | |
| 333 | if (!packet_creator_.AddSavedFrame(queued_control_frames_.back(), |
| 334 | next_transmission_type_)) { |
| 335 | // Packet was full. |
| 336 | return false; |
| 337 | } |
| 338 | queued_control_frames_.pop_back(); |
| 339 | return true; |
| 340 | } |
| 341 | |
| 342 | void QuicPacketGenerator::StopSendingVersion() { |
| 343 | packet_creator_.StopSendingVersion(); |
| 344 | } |
| 345 | |
| 346 | void QuicPacketGenerator::SetDiversificationNonce( |
| 347 | const DiversificationNonce& nonce) { |
| 348 | packet_creator_.SetDiversificationNonce(nonce); |
| 349 | } |
| 350 | |
| 351 | QuicPacketNumber QuicPacketGenerator::packet_number() const { |
| 352 | return packet_creator_.packet_number(); |
| 353 | } |
| 354 | |
| 355 | QuicByteCount QuicPacketGenerator::GetCurrentMaxPacketLength() const { |
| 356 | return packet_creator_.max_packet_length(); |
| 357 | } |
| 358 | |
| 359 | void QuicPacketGenerator::SetMaxPacketLength(QuicByteCount length) { |
| 360 | DCHECK(packet_creator_.CanSetMaxPacketLength()); |
| 361 | packet_creator_.SetMaxPacketLength(length); |
| 362 | } |
| 363 | |
| 364 | std::unique_ptr<QuicEncryptedPacket> |
| 365 | QuicPacketGenerator::SerializeVersionNegotiationPacket( |
| 366 | bool ietf_quic, |
| 367 | const ParsedQuicVersionVector& supported_versions) { |
| 368 | return packet_creator_.SerializeVersionNegotiationPacket(ietf_quic, |
| 369 | supported_versions); |
| 370 | } |
| 371 | |
| 372 | OwningSerializedPacketPointer |
| 373 | QuicPacketGenerator::SerializeConnectivityProbingPacket() { |
| 374 | return packet_creator_.SerializeConnectivityProbingPacket(); |
| 375 | } |
| 376 | |
| 377 | OwningSerializedPacketPointer |
| 378 | QuicPacketGenerator::SerializePathChallengeConnectivityProbingPacket( |
| 379 | QuicPathFrameBuffer* payload) { |
| 380 | return packet_creator_.SerializePathChallengeConnectivityProbingPacket( |
| 381 | payload); |
| 382 | } |
| 383 | |
| 384 | OwningSerializedPacketPointer |
| 385 | QuicPacketGenerator::SerializePathResponseConnectivityProbingPacket( |
| 386 | const QuicDeque<QuicPathFrameBuffer>& payloads, |
| 387 | const bool is_padded) { |
| 388 | return packet_creator_.SerializePathResponseConnectivityProbingPacket( |
| 389 | payloads, is_padded); |
| 390 | } |
| 391 | |
| 392 | void QuicPacketGenerator::ReserializeAllFrames( |
| 393 | const QuicPendingRetransmission& retransmission, |
| 394 | char* buffer, |
| 395 | size_t buffer_len) { |
| 396 | packet_creator_.ReserializeAllFrames(retransmission, buffer, buffer_len); |
| 397 | } |
| 398 | |
| 399 | void QuicPacketGenerator::UpdatePacketNumberLength( |
| 400 | QuicPacketNumber least_packet_awaited_by_peer, |
| 401 | QuicPacketCount max_packets_in_flight) { |
| 402 | return packet_creator_.UpdatePacketNumberLength(least_packet_awaited_by_peer, |
| 403 | max_packets_in_flight); |
| 404 | } |
| 405 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 406 | void QuicPacketGenerator::SetServerConnectionIdLength(uint32_t length) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 407 | if (length == 0) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 408 | packet_creator_.SetServerConnectionIdIncluded(CONNECTION_ID_ABSENT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 409 | } else { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 410 | packet_creator_.SetServerConnectionIdIncluded(CONNECTION_ID_PRESENT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | void QuicPacketGenerator::set_encryption_level(EncryptionLevel level) { |
| 415 | packet_creator_.set_encryption_level(level); |
| 416 | } |
| 417 | |
| 418 | void QuicPacketGenerator::SetEncrypter( |
| 419 | EncryptionLevel level, |
| 420 | std::unique_ptr<QuicEncrypter> encrypter) { |
| 421 | packet_creator_.SetEncrypter(level, std::move(encrypter)); |
| 422 | } |
| 423 | |
| 424 | void QuicPacketGenerator::AddRandomPadding() { |
| 425 | packet_creator_.AddPendingPadding( |
| 426 | random_generator_->RandUint64() % kMaxNumRandomPaddingBytes + 1); |
| 427 | } |
| 428 | |
| 429 | void QuicPacketGenerator::SendRemainingPendingPadding() { |
| 430 | while (packet_creator_.pending_padding_bytes() > 0 && !HasQueuedFrames() && |
| 431 | CanSendWithNextPendingFrameAddition()) { |
| 432 | packet_creator_.Flush(); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | bool QuicPacketGenerator::HasRetransmittableFrames() const { |
| 437 | return !queued_control_frames_.empty() || |
| 438 | packet_creator_.HasPendingRetransmittableFrames(); |
| 439 | } |
| 440 | |
| 441 | bool QuicPacketGenerator::HasPendingStreamFramesOfStream( |
| 442 | QuicStreamId id) const { |
| 443 | return packet_creator_.HasPendingStreamFramesOfStream(id); |
| 444 | } |
| 445 | |
| 446 | void QuicPacketGenerator::SetTransmissionType(TransmissionType type) { |
| 447 | packet_creator_.SetTransmissionType(type); |
wub | 98669f5 | 2019-04-18 10:49:18 -0700 | [diff] [blame] | 448 | if (packet_creator_.can_set_transmission_type()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 449 | next_transmission_type_ = type; |
| 450 | } |
| 451 | } |
| 452 | |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 453 | void QuicPacketGenerator::SetRetryToken(QuicStringPiece retry_token) { |
| 454 | packet_creator_.SetRetryToken(retry_token); |
| 455 | } |
| 456 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 457 | void QuicPacketGenerator::SetCanSetTransmissionType( |
| 458 | bool can_set_transmission_type) { |
| 459 | packet_creator_.set_can_set_transmission_type(can_set_transmission_type); |
| 460 | } |
| 461 | |
| 462 | MessageStatus QuicPacketGenerator::AddMessageFrame(QuicMessageId message_id, |
| 463 | QuicMemSliceSpan message) { |
| 464 | QUIC_BUG_IF(!flusher_attached_) << "Packet flusher is not attached when " |
| 465 | "generator tries to add message frame."; |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 466 | MaybeBundleAckOpportunistically(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 467 | const QuicByteCount message_length = message.total_length(); |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 468 | if (message_length > GetCurrentLargestMessagePayload()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 469 | return MESSAGE_STATUS_TOO_LARGE; |
| 470 | } |
| 471 | SendQueuedFrames(/*flush=*/false); |
| 472 | if (!packet_creator_.HasRoomForMessageFrame(message_length)) { |
| 473 | packet_creator_.Flush(); |
| 474 | } |
wub | 553a966 | 2019-03-28 20:13:23 -0700 | [diff] [blame] | 475 | QuicMessageFrame* frame = new QuicMessageFrame(message_id, message); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 476 | const bool success = |
| 477 | packet_creator_.AddSavedFrame(QuicFrame(frame), next_transmission_type_); |
| 478 | if (!success) { |
| 479 | QUIC_BUG << "Failed to send message " << message_id; |
| 480 | delete frame; |
| 481 | return MESSAGE_STATUS_INTERNAL_ERROR; |
| 482 | } |
| 483 | return MESSAGE_STATUS_SUCCESS; |
| 484 | } |
| 485 | |
| 486 | void QuicPacketGenerator::MaybeBundleAckOpportunistically() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 487 | if (packet_creator_.has_ack()) { |
| 488 | // Ack already queued, nothing to do. |
| 489 | return; |
| 490 | } |
| 491 | if (!delegate_->ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, |
| 492 | NOT_HANDSHAKE)) { |
| 493 | return; |
| 494 | } |
| 495 | const bool flushed = |
| 496 | FlushAckFrame(delegate_->MaybeBundleAckOpportunistically()); |
| 497 | DCHECK(flushed); |
| 498 | } |
| 499 | |
| 500 | bool QuicPacketGenerator::FlushAckFrame(const QuicFrames& frames) { |
| 501 | QUIC_BUG_IF(!flusher_attached_) << "Packet flusher is not attached when " |
| 502 | "generator tries to send ACK frame."; |
| 503 | for (const auto& frame : frames) { |
| 504 | DCHECK(frame.type == ACK_FRAME || frame.type == STOP_WAITING_FRAME); |
| 505 | if (packet_creator_.HasPendingFrames()) { |
| 506 | if (packet_creator_.AddSavedFrame(frame, next_transmission_type_)) { |
| 507 | // There is pending frames and current frame fits. |
| 508 | continue; |
| 509 | } |
| 510 | } |
| 511 | DCHECK(!packet_creator_.HasPendingFrames()); |
| 512 | // There is no pending frames, consult the delegate whether a packet can be |
| 513 | // generated. |
| 514 | if (!delegate_->ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, |
| 515 | NOT_HANDSHAKE)) { |
| 516 | return false; |
| 517 | } |
| 518 | const bool success = |
| 519 | packet_creator_.AddSavedFrame(frame, next_transmission_type_); |
| 520 | QUIC_BUG_IF(!success) << "Failed to flush " << frame; |
| 521 | } |
| 522 | return true; |
| 523 | } |
| 524 | |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 525 | QuicPacketLength QuicPacketGenerator::GetCurrentLargestMessagePayload() const { |
| 526 | return packet_creator_.GetCurrentLargestMessagePayload(); |
| 527 | } |
| 528 | |
| 529 | QuicPacketLength QuicPacketGenerator::GetGuaranteedLargestMessagePayload() |
| 530 | const { |
| 531 | return packet_creator_.GetGuaranteedLargestMessagePayload(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 532 | } |
| 533 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 534 | void QuicPacketGenerator::SetServerConnectionId( |
| 535 | QuicConnectionId server_connection_id) { |
| 536 | packet_creator_.SetServerConnectionId(server_connection_id); |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 537 | } |
| 538 | |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 539 | void QuicPacketGenerator::SetClientConnectionId( |
| 540 | QuicConnectionId client_connection_id) { |
| 541 | packet_creator_.SetClientConnectionId(client_connection_id); |
| 542 | } |
| 543 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 544 | } // namespace quic |