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_stream_sequencer.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <limits> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 9 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
renjietang | 0398785 | 2019-10-22 12:06:37 -0700 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/quic_error_codes.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/quic_stream.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/quic_stream_sequencer_buffer.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_clock.h" |
| 19 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
renjietang | c152cc5 | 2019-08-13 11:54:57 -0700 | [diff] [blame] | 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 21 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 22 | #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] | 23 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
| 24 | |
| 25 | namespace quic { |
| 26 | |
| 27 | QuicStreamSequencer::QuicStreamSequencer(StreamInterface* quic_stream) |
| 28 | : stream_(quic_stream), |
| 29 | buffered_frames_(kStreamReceiveWindowLimit), |
renjietang | 0398785 | 2019-10-22 12:06:37 -0700 | [diff] [blame] | 30 | highest_offset_(0), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 31 | close_offset_(std::numeric_limits<QuicStreamOffset>::max()), |
| 32 | blocked_(false), |
| 33 | num_frames_received_(0), |
| 34 | num_duplicate_frames_received_(0), |
| 35 | ignore_read_data_(false), |
| 36 | level_triggered_(false), |
| 37 | stop_reading_when_level_triggered_( |
renjietang | e2a64de | 2019-11-15 12:16:56 -0800 | [diff] [blame] | 38 | GetQuicReloadableFlag(quic_stop_reading_when_level_triggered)), |
| 39 | close_connection_and_discard_data_on_wrong_offset_(GetQuicReloadableFlag( |
| 40 | quic_close_connection_and_discard_data_on_wrong_offset)) { |
ianswett | 3ccd770 | 2019-10-24 04:03:48 -0700 | [diff] [blame] | 41 | if (stop_reading_when_level_triggered_) { |
| 42 | QUIC_RELOADABLE_FLAG_COUNT(quic_stop_reading_when_level_triggered); |
| 43 | } |
| 44 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 45 | |
| 46 | QuicStreamSequencer::~QuicStreamSequencer() {} |
| 47 | |
| 48 | void QuicStreamSequencer::OnStreamFrame(const QuicStreamFrame& frame) { |
renjietang | 963c2ec | 2019-09-12 11:46:50 -0700 | [diff] [blame] | 49 | DCHECK_LE(frame.offset + frame.data_length, close_offset_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 50 | ++num_frames_received_; |
| 51 | const QuicStreamOffset byte_offset = frame.offset; |
| 52 | const size_t data_len = frame.data_length; |
| 53 | |
| 54 | if (frame.fin) { |
renjietang | c152cc5 | 2019-08-13 11:54:57 -0700 | [diff] [blame] | 55 | bool should_process_data = CloseStreamAtOffset(frame.offset + data_len); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | if (data_len == 0) { |
| 57 | return; |
| 58 | } |
renjietang | e2a64de | 2019-11-15 12:16:56 -0800 | [diff] [blame] | 59 | if (close_connection_and_discard_data_on_wrong_offset_) { |
| 60 | QUIC_RELOADABLE_FLAG_COUNT_N( |
| 61 | quic_close_connection_and_discard_data_on_wrong_offset, 1, 3); |
renjietang | c152cc5 | 2019-08-13 11:54:57 -0700 | [diff] [blame] | 62 | if (!should_process_data) { |
| 63 | return; |
| 64 | } |
| 65 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 66 | } |
| 67 | OnFrameData(byte_offset, data_len, frame.data_buffer); |
| 68 | } |
| 69 | |
| 70 | void QuicStreamSequencer::OnCryptoFrame(const QuicCryptoFrame& frame) { |
| 71 | ++num_frames_received_; |
| 72 | OnFrameData(frame.offset, frame.data_length, frame.data_buffer); |
| 73 | } |
| 74 | |
| 75 | void QuicStreamSequencer::OnFrameData(QuicStreamOffset byte_offset, |
| 76 | size_t data_len, |
| 77 | const char* data_buffer) { |
renjietang | 0398785 | 2019-10-22 12:06:37 -0700 | [diff] [blame] | 78 | highest_offset_ = std::max(highest_offset_, byte_offset + data_len); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 79 | const size_t previous_readable_bytes = buffered_frames_.ReadableBytes(); |
| 80 | size_t bytes_written; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 81 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 82 | QuicErrorCode result = buffered_frames_.OnStreamData( |
| 83 | byte_offset, QuicStringPiece(data_buffer, data_len), &bytes_written, |
| 84 | &error_details); |
| 85 | if (result != QUIC_NO_ERROR) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 86 | std::string details = QuicStrCat( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 87 | "Stream ", stream_->id(), ": ", QuicErrorCodeToString(result), ": ", |
| 88 | error_details, |
| 89 | "\nPeer Address: ", stream_->PeerAddressOfLatestPacket().ToString()); |
| 90 | QUIC_LOG_FIRST_N(WARNING, 50) << QuicErrorCodeToString(result); |
| 91 | QUIC_LOG_FIRST_N(WARNING, 50) << details; |
| 92 | stream_->CloseConnectionWithDetails(result, details); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | if (bytes_written == 0) { |
| 97 | ++num_duplicate_frames_received_; |
| 98 | // Silently ignore duplicates. |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | if (blocked_) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | if (level_triggered_) { |
| 107 | if (buffered_frames_.ReadableBytes() > previous_readable_bytes) { |
| 108 | // Readable bytes has changed, let stream decide if to inform application |
| 109 | // or not. |
| 110 | if (stop_reading_when_level_triggered_ && ignore_read_data_) { |
| 111 | QUIC_RELOADABLE_FLAG_COUNT(quic_stop_reading_when_level_triggered); |
| 112 | FlushBufferedFrames(); |
| 113 | } else { |
| 114 | stream_->OnDataAvailable(); |
| 115 | } |
| 116 | } |
| 117 | return; |
| 118 | } |
| 119 | const bool stream_unblocked = |
| 120 | previous_readable_bytes == 0 && buffered_frames_.ReadableBytes() > 0; |
| 121 | if (stream_unblocked) { |
| 122 | if (ignore_read_data_) { |
| 123 | FlushBufferedFrames(); |
| 124 | } else { |
| 125 | stream_->OnDataAvailable(); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
renjietang | c152cc5 | 2019-08-13 11:54:57 -0700 | [diff] [blame] | 130 | bool QuicStreamSequencer::CloseStreamAtOffset(QuicStreamOffset offset) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 131 | const QuicStreamOffset kMaxOffset = |
| 132 | std::numeric_limits<QuicStreamOffset>::max(); |
| 133 | |
| 134 | // If there is a scheduled close, the new offset should match it. |
| 135 | if (close_offset_ != kMaxOffset && offset != close_offset_) { |
renjietang | e2a64de | 2019-11-15 12:16:56 -0800 | [diff] [blame] | 136 | if (!close_connection_and_discard_data_on_wrong_offset_) { |
renjietang | 0398785 | 2019-10-22 12:06:37 -0700 | [diff] [blame] | 137 | stream_->Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS); |
| 138 | return false; |
| 139 | } |
renjietang | e2a64de | 2019-11-15 12:16:56 -0800 | [diff] [blame] | 140 | QUIC_RELOADABLE_FLAG_COUNT_N( |
| 141 | quic_close_connection_and_discard_data_on_wrong_offset, 2, 3); |
renjietang | 0398785 | 2019-10-22 12:06:37 -0700 | [diff] [blame] | 142 | stream_->CloseConnectionWithDetails( |
| 143 | QUIC_STREAM_SEQUENCER_INVALID_STATE, |
| 144 | QuicStrCat("Stream ", stream_->id(), |
| 145 | " received new final offset: ", offset, |
| 146 | ", which is different from close offset: ", close_offset_)); |
renjietang | c152cc5 | 2019-08-13 11:54:57 -0700 | [diff] [blame] | 147 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 148 | } |
| 149 | |
renjietang | 0398785 | 2019-10-22 12:06:37 -0700 | [diff] [blame] | 150 | // The final offset should be no less than the highest offset that is |
| 151 | // received. |
renjietang | e2a64de | 2019-11-15 12:16:56 -0800 | [diff] [blame] | 152 | if (close_connection_and_discard_data_on_wrong_offset_ && |
| 153 | offset < highest_offset_) { |
| 154 | QUIC_RELOADABLE_FLAG_COUNT_N( |
| 155 | quic_close_connection_and_discard_data_on_wrong_offset, 3, 3); |
| 156 | stream_->CloseConnectionWithDetails( |
| 157 | QUIC_STREAM_SEQUENCER_INVALID_STATE, |
| 158 | QuicStrCat( |
| 159 | "Stream ", stream_->id(), " received fin with offset: ", offset, |
| 160 | ", which reduces current highest offset: ", highest_offset_)); |
| 161 | return false; |
renjietang | 0398785 | 2019-10-22 12:06:37 -0700 | [diff] [blame] | 162 | } |
| 163 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 164 | close_offset_ = offset; |
| 165 | |
| 166 | MaybeCloseStream(); |
renjietang | c152cc5 | 2019-08-13 11:54:57 -0700 | [diff] [blame] | 167 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 168 | } |
| 169 | |
bnc | f152d8c | 2019-08-06 06:03:08 -0700 | [diff] [blame] | 170 | void QuicStreamSequencer::MaybeCloseStream() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 171 | if (blocked_ || !IsClosed()) { |
bnc | f152d8c | 2019-08-06 06:03:08 -0700 | [diff] [blame] | 172 | return; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | QUIC_DVLOG(1) << "Passing up termination, as we've processed " |
| 176 | << buffered_frames_.BytesConsumed() << " of " << close_offset_ |
| 177 | << " bytes."; |
| 178 | // This will cause the stream to consume the FIN. |
| 179 | // Technically it's an error if |num_bytes_consumed| isn't exactly |
| 180 | // equal to |close_offset|, but error handling seems silly at this point. |
| 181 | if (ignore_read_data_) { |
| 182 | // The sequencer is discarding stream data and must notify the stream on |
| 183 | // receipt of a FIN because the consumer won't. |
| 184 | stream_->OnFinRead(); |
| 185 | } else { |
| 186 | stream_->OnDataAvailable(); |
| 187 | } |
| 188 | buffered_frames_.Clear(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | int QuicStreamSequencer::GetReadableRegions(iovec* iov, size_t iov_len) const { |
| 192 | DCHECK(!blocked_); |
| 193 | return buffered_frames_.GetReadableRegions(iov, iov_len); |
| 194 | } |
| 195 | |
| 196 | bool QuicStreamSequencer::GetReadableRegion(iovec* iov) const { |
| 197 | DCHECK(!blocked_); |
| 198 | return buffered_frames_.GetReadableRegion(iov); |
| 199 | } |
| 200 | |
bnc | 7b3e0a9 | 2019-06-24 16:06:45 -0700 | [diff] [blame] | 201 | bool QuicStreamSequencer::PeekRegion(QuicStreamOffset offset, |
| 202 | iovec* iov) const { |
| 203 | DCHECK(!blocked_); |
| 204 | return buffered_frames_.PeekRegion(offset, iov); |
| 205 | } |
| 206 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 207 | void QuicStreamSequencer::Read(std::string* buffer) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 208 | DCHECK(!blocked_); |
| 209 | buffer->resize(buffer->size() + ReadableBytes()); |
| 210 | iovec iov; |
| 211 | iov.iov_len = ReadableBytes(); |
| 212 | iov.iov_base = &(*buffer)[buffer->size() - iov.iov_len]; |
| 213 | Readv(&iov, 1); |
| 214 | } |
| 215 | |
| 216 | int QuicStreamSequencer::Readv(const struct iovec* iov, size_t iov_len) { |
| 217 | DCHECK(!blocked_); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 218 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 219 | size_t bytes_read; |
| 220 | QuicErrorCode read_error = |
| 221 | buffered_frames_.Readv(iov, iov_len, &bytes_read, &error_details); |
| 222 | if (read_error != QUIC_NO_ERROR) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 223 | std::string details = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 224 | QuicStrCat("Stream ", stream_->id(), ": ", error_details); |
| 225 | stream_->CloseConnectionWithDetails(read_error, details); |
| 226 | return static_cast<int>(bytes_read); |
| 227 | } |
| 228 | |
| 229 | stream_->AddBytesConsumed(bytes_read); |
| 230 | return static_cast<int>(bytes_read); |
| 231 | } |
| 232 | |
| 233 | bool QuicStreamSequencer::HasBytesToRead() const { |
| 234 | return buffered_frames_.HasBytesToRead(); |
| 235 | } |
| 236 | |
| 237 | size_t QuicStreamSequencer::ReadableBytes() const { |
| 238 | return buffered_frames_.ReadableBytes(); |
| 239 | } |
| 240 | |
| 241 | bool QuicStreamSequencer::IsClosed() const { |
| 242 | return buffered_frames_.BytesConsumed() >= close_offset_; |
| 243 | } |
| 244 | |
| 245 | void QuicStreamSequencer::MarkConsumed(size_t num_bytes_consumed) { |
| 246 | DCHECK(!blocked_); |
| 247 | bool result = buffered_frames_.MarkConsumed(num_bytes_consumed); |
| 248 | if (!result) { |
| 249 | QUIC_BUG << "Invalid argument to MarkConsumed." |
| 250 | << " expect to consume: " << num_bytes_consumed |
| 251 | << ", but not enough bytes available. " << DebugString(); |
| 252 | stream_->Reset(QUIC_ERROR_PROCESSING_STREAM); |
| 253 | return; |
| 254 | } |
| 255 | stream_->AddBytesConsumed(num_bytes_consumed); |
| 256 | } |
| 257 | |
| 258 | void QuicStreamSequencer::SetBlockedUntilFlush() { |
| 259 | blocked_ = true; |
| 260 | } |
| 261 | |
| 262 | void QuicStreamSequencer::SetUnblocked() { |
| 263 | blocked_ = false; |
| 264 | if (IsClosed() || HasBytesToRead()) { |
| 265 | stream_->OnDataAvailable(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void QuicStreamSequencer::StopReading() { |
| 270 | if (ignore_read_data_) { |
| 271 | return; |
| 272 | } |
| 273 | ignore_read_data_ = true; |
| 274 | FlushBufferedFrames(); |
| 275 | } |
| 276 | |
| 277 | void QuicStreamSequencer::ReleaseBuffer() { |
| 278 | buffered_frames_.ReleaseWholeBuffer(); |
| 279 | } |
| 280 | |
| 281 | void QuicStreamSequencer::ReleaseBufferIfEmpty() { |
| 282 | if (buffered_frames_.Empty()) { |
| 283 | buffered_frames_.ReleaseWholeBuffer(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void QuicStreamSequencer::FlushBufferedFrames() { |
| 288 | DCHECK(ignore_read_data_); |
| 289 | size_t bytes_flushed = buffered_frames_.FlushBufferedFrames(); |
| 290 | QUIC_DVLOG(1) << "Flushing buffered data at offset " |
| 291 | << buffered_frames_.BytesConsumed() << " length " |
| 292 | << bytes_flushed << " for stream " << stream_->id(); |
| 293 | stream_->AddBytesConsumed(bytes_flushed); |
| 294 | MaybeCloseStream(); |
| 295 | } |
| 296 | |
| 297 | size_t QuicStreamSequencer::NumBytesBuffered() const { |
| 298 | return buffered_frames_.BytesBuffered(); |
| 299 | } |
| 300 | |
| 301 | QuicStreamOffset QuicStreamSequencer::NumBytesConsumed() const { |
| 302 | return buffered_frames_.BytesConsumed(); |
| 303 | } |
| 304 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 305 | const std::string QuicStreamSequencer::DebugString() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 306 | // clang-format off |
| 307 | return QuicStrCat("QuicStreamSequencer:", |
| 308 | "\n bytes buffered: ", NumBytesBuffered(), |
| 309 | "\n bytes consumed: ", NumBytesConsumed(), |
| 310 | "\n has bytes to read: ", HasBytesToRead() ? "true" : "false", |
| 311 | "\n frames received: ", num_frames_received(), |
| 312 | "\n close offset bytes: ", close_offset_, |
| 313 | "\n is closed: ", IsClosed() ? "true" : "false"); |
| 314 | // clang-format on |
| 315 | } |
| 316 | |
| 317 | } // namespace quic |