| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/quic_stream_sequencer.h" | 5 #include "net/quic/quic_stream_sequencer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "net/quic/quic_bug_tracker.h" | 12 #include "net/quic/quic_bug_tracker.h" |
| 13 #include "net/quic/quic_clock.h" | 13 #include "net/quic/quic_clock.h" |
| 14 #include "net/quic/quic_flags.h" | 14 #include "net/quic/quic_flags.h" |
| 15 #include "net/quic/quic_frame_list.h" | |
| 16 #include "net/quic/quic_protocol.h" | 15 #include "net/quic/quic_protocol.h" |
| 17 #include "net/quic/reliable_quic_stream.h" | 16 #include "net/quic/reliable_quic_stream.h" |
| 18 #include "net/quic/stream_sequencer_buffer.h" | 17 #include "net/quic/stream_sequencer_buffer.h" |
| 19 | 18 |
| 20 using std::min; | 19 using std::min; |
| 21 using std::numeric_limits; | 20 using std::numeric_limits; |
| 22 using std::string; | 21 using std::string; |
| 23 | 22 |
| 24 namespace net { | 23 namespace net { |
| 25 | 24 |
| 26 QuicStreamSequencer::QuicStreamSequencer(ReliableQuicStream* quic_stream, | 25 QuicStreamSequencer::QuicStreamSequencer(ReliableQuicStream* quic_stream, |
| 27 const QuicClock* clock) | 26 const QuicClock* clock) |
| 28 : stream_(quic_stream), | 27 : stream_(quic_stream), |
| 28 buffered_frames_(kStreamReceiveWindowLimit), |
| 29 close_offset_(numeric_limits<QuicStreamOffset>::max()), | 29 close_offset_(numeric_limits<QuicStreamOffset>::max()), |
| 30 blocked_(false), | 30 blocked_(false), |
| 31 num_frames_received_(0), | 31 num_frames_received_(0), |
| 32 num_duplicate_frames_received_(0), | 32 num_duplicate_frames_received_(0), |
| 33 num_early_frames_received_(0), | 33 num_early_frames_received_(0), |
| 34 clock_(clock), | 34 clock_(clock), |
| 35 ignore_read_data_(false) { | 35 ignore_read_data_(false) {} |
| 36 if (FLAGS_quic_use_stream_sequencer_buffer) { | |
| 37 buffered_frames_.reset( | |
| 38 new StreamSequencerBuffer(kStreamReceiveWindowLimit)); | |
| 39 } else { | |
| 40 buffered_frames_.reset(new QuicFrameList()); | |
| 41 } | |
| 42 } | |
| 43 | 36 |
| 44 QuicStreamSequencer::~QuicStreamSequencer() {} | 37 QuicStreamSequencer::~QuicStreamSequencer() {} |
| 45 | 38 |
| 46 void QuicStreamSequencer::OnStreamFrame(const QuicStreamFrame& frame) { | 39 void QuicStreamSequencer::OnStreamFrame(const QuicStreamFrame& frame) { |
| 47 ++num_frames_received_; | 40 ++num_frames_received_; |
| 48 const QuicStreamOffset byte_offset = frame.offset; | 41 const QuicStreamOffset byte_offset = frame.offset; |
| 49 const size_t data_len = frame.frame_length; | 42 const size_t data_len = frame.frame_length; |
| 50 if (data_len == 0 && !frame.fin) { | 43 if (data_len == 0 && !frame.fin) { |
| 51 // Stream frames must have data or a fin flag. | 44 // Stream frames must have data or a fin flag. |
| 52 LOG(WARNING) << "QUIC_INVALID_STREAM_FRAM: Empty stream frame " | 45 LOG(WARNING) << "QUIC_INVALID_STREAM_FRAM: Empty stream frame " |
| 53 "without FIN set."; | 46 "without FIN set."; |
| 54 stream_->CloseConnectionWithDetails(QUIC_INVALID_STREAM_FRAME, | 47 stream_->CloseConnectionWithDetails(QUIC_INVALID_STREAM_FRAME, |
| 55 "Empty stream frame without FIN set."); | 48 "Empty stream frame without FIN set."); |
| 56 return; | 49 return; |
| 57 } | 50 } |
| 58 | 51 |
| 59 if (frame.fin) { | 52 if (frame.fin) { |
| 60 CloseStreamAtOffset(frame.offset + data_len); | 53 CloseStreamAtOffset(frame.offset + data_len); |
| 61 if (data_len == 0) { | 54 if (data_len == 0) { |
| 62 return; | 55 return; |
| 63 } | 56 } |
| 64 } | 57 } |
| 65 size_t bytes_written; | 58 size_t bytes_written; |
| 66 QuicErrorCode result = buffered_frames_->OnStreamData( | 59 QuicErrorCode result = buffered_frames_.OnStreamData( |
| 67 byte_offset, StringPiece(frame.frame_buffer, frame.frame_length), | 60 byte_offset, StringPiece(frame.frame_buffer, frame.frame_length), |
| 68 clock_->ApproximateNow(), &bytes_written); | 61 clock_->ApproximateNow(), &bytes_written); |
| 69 | 62 |
| 70 if (result == QUIC_INVALID_STREAM_DATA) { | 63 if (result == QUIC_INVALID_STREAM_DATA) { |
| 71 LOG(WARNING) << "QUIC_INVALID_STREAM_FRAME: Stream frame " | 64 LOG(WARNING) << "QUIC_INVALID_STREAM_FRAME: Stream frame " |
| 72 "overlaps with buffered data."; | 65 "overlaps with buffered data."; |
| 73 stream_->CloseConnectionWithDetails( | 66 stream_->CloseConnectionWithDetails( |
| 74 QUIC_INVALID_STREAM_FRAME, "Stream frame overlaps with buffered data."); | 67 QUIC_INVALID_STREAM_FRAME, "Stream frame overlaps with buffered data."); |
| 75 return; | 68 return; |
| 76 } | 69 } |
| 77 if (result == QUIC_NO_ERROR && bytes_written == 0) { | 70 if (result == QUIC_NO_ERROR && bytes_written == 0) { |
| 78 ++num_duplicate_frames_received_; | 71 ++num_duplicate_frames_received_; |
| 79 // Silently ignore duplicates. | 72 // Silently ignore duplicates. |
| 80 return; | 73 return; |
| 81 } | 74 } |
| 82 | 75 |
| 83 if (byte_offset > buffered_frames_->BytesConsumed()) { | 76 if (byte_offset > buffered_frames_.BytesConsumed()) { |
| 84 ++num_early_frames_received_; | 77 ++num_early_frames_received_; |
| 85 } | 78 } |
| 86 | 79 |
| 87 if (blocked_) { | 80 if (blocked_) { |
| 88 return; | 81 return; |
| 89 } | 82 } |
| 90 | 83 |
| 91 if (byte_offset == buffered_frames_->BytesConsumed()) { | 84 if (byte_offset == buffered_frames_.BytesConsumed()) { |
| 92 if (ignore_read_data_) { | 85 if (ignore_read_data_) { |
| 93 FlushBufferedFrames(); | 86 FlushBufferedFrames(); |
| 94 } else { | 87 } else { |
| 95 stream_->OnDataAvailable(); | 88 stream_->OnDataAvailable(); |
| 96 } | 89 } |
| 97 } | 90 } |
| 98 } | 91 } |
| 99 | 92 |
| 100 void QuicStreamSequencer::CloseStreamAtOffset(QuicStreamOffset offset) { | 93 void QuicStreamSequencer::CloseStreamAtOffset(QuicStreamOffset offset) { |
| 101 const QuicStreamOffset kMaxOffset = numeric_limits<QuicStreamOffset>::max(); | 94 const QuicStreamOffset kMaxOffset = numeric_limits<QuicStreamOffset>::max(); |
| 102 | 95 |
| 103 // If there is a scheduled close, the new offset should match it. | 96 // If there is a scheduled close, the new offset should match it. |
| 104 if (close_offset_ != kMaxOffset && offset != close_offset_) { | 97 if (close_offset_ != kMaxOffset && offset != close_offset_) { |
| 105 stream_->Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS); | 98 stream_->Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS); |
| 106 return; | 99 return; |
| 107 } | 100 } |
| 108 | 101 |
| 109 close_offset_ = offset; | 102 close_offset_ = offset; |
| 110 | 103 |
| 111 MaybeCloseStream(); | 104 MaybeCloseStream(); |
| 112 } | 105 } |
| 113 | 106 |
| 114 bool QuicStreamSequencer::MaybeCloseStream() { | 107 bool QuicStreamSequencer::MaybeCloseStream() { |
| 115 if (blocked_ || !IsClosed()) { | 108 if (blocked_ || !IsClosed()) { |
| 116 return false; | 109 return false; |
| 117 } | 110 } |
| 118 | 111 |
| 119 DVLOG(1) << "Passing up termination, as we've processed " | 112 DVLOG(1) << "Passing up termination, as we've processed " |
| 120 << buffered_frames_->BytesConsumed() << " of " << close_offset_ | 113 << buffered_frames_.BytesConsumed() << " of " << close_offset_ |
| 121 << " bytes."; | 114 << " bytes."; |
| 122 // This will cause the stream to consume the FIN. | 115 // This will cause the stream to consume the FIN. |
| 123 // Technically it's an error if |num_bytes_consumed| isn't exactly | 116 // Technically it's an error if |num_bytes_consumed| isn't exactly |
| 124 // equal to |close_offset|, but error handling seems silly at this point. | 117 // equal to |close_offset|, but error handling seems silly at this point. |
| 125 if (ignore_read_data_) { | 118 if (ignore_read_data_) { |
| 126 // The sequencer is discarding stream data and must notify the stream on | 119 // The sequencer is discarding stream data and must notify the stream on |
| 127 // receipt of a FIN because the consumer won't. | 120 // receipt of a FIN because the consumer won't. |
| 128 stream_->OnFinRead(); | 121 stream_->OnFinRead(); |
| 129 } else { | 122 } else { |
| 130 stream_->OnDataAvailable(); | 123 stream_->OnDataAvailable(); |
| 131 } | 124 } |
| 132 buffered_frames_->Clear(); | 125 buffered_frames_.Clear(); |
| 133 return true; | 126 return true; |
| 134 } | 127 } |
| 135 | 128 |
| 136 int QuicStreamSequencer::GetReadableRegions(iovec* iov, size_t iov_len) const { | 129 int QuicStreamSequencer::GetReadableRegions(iovec* iov, size_t iov_len) const { |
| 137 DCHECK(!blocked_); | 130 DCHECK(!blocked_); |
| 138 return buffered_frames_->GetReadableRegions(iov, iov_len); | 131 return buffered_frames_.GetReadableRegions(iov, iov_len); |
| 139 } | 132 } |
| 140 | 133 |
| 141 bool QuicStreamSequencer::GetReadableRegion(iovec* iov, | 134 bool QuicStreamSequencer::GetReadableRegion(iovec* iov, |
| 142 QuicTime* timestamp) const { | 135 QuicTime* timestamp) const { |
| 143 DCHECK(!blocked_); | 136 DCHECK(!blocked_); |
| 144 return buffered_frames_->GetReadableRegion(iov, timestamp); | 137 return buffered_frames_.GetReadableRegion(iov, timestamp); |
| 145 } | 138 } |
| 146 | 139 |
| 147 int QuicStreamSequencer::Readv(const struct iovec* iov, size_t iov_len) { | 140 int QuicStreamSequencer::Readv(const struct iovec* iov, size_t iov_len) { |
| 148 DCHECK(!blocked_); | 141 DCHECK(!blocked_); |
| 149 size_t bytes_read = buffered_frames_->Readv(iov, iov_len); | 142 size_t bytes_read = buffered_frames_.Readv(iov, iov_len); |
| 150 stream_->AddBytesConsumed(bytes_read); | 143 stream_->AddBytesConsumed(bytes_read); |
| 151 return static_cast<int>(bytes_read); | 144 return static_cast<int>(bytes_read); |
| 152 } | 145 } |
| 153 | 146 |
| 154 bool QuicStreamSequencer::HasBytesToRead() const { | 147 bool QuicStreamSequencer::HasBytesToRead() const { |
| 155 return buffered_frames_->HasBytesToRead(); | 148 return buffered_frames_.HasBytesToRead(); |
| 156 } | 149 } |
| 157 | 150 |
| 158 bool QuicStreamSequencer::IsClosed() const { | 151 bool QuicStreamSequencer::IsClosed() const { |
| 159 return buffered_frames_->BytesConsumed() >= close_offset_; | 152 return buffered_frames_.BytesConsumed() >= close_offset_; |
| 160 } | 153 } |
| 161 | 154 |
| 162 void QuicStreamSequencer::MarkConsumed(size_t num_bytes_consumed) { | 155 void QuicStreamSequencer::MarkConsumed(size_t num_bytes_consumed) { |
| 163 DCHECK(!blocked_); | 156 DCHECK(!blocked_); |
| 164 bool result = buffered_frames_->MarkConsumed(num_bytes_consumed); | 157 bool result = buffered_frames_.MarkConsumed(num_bytes_consumed); |
| 165 if (!result) { | 158 if (!result) { |
| 166 QUIC_BUG << "Invalid argument to MarkConsumed." | 159 QUIC_BUG << "Invalid argument to MarkConsumed." |
| 167 << " expect to consume: " << num_bytes_consumed | 160 << " expect to consume: " << num_bytes_consumed |
| 168 << ", but not enough bytes available."; | 161 << ", but not enough bytes available."; |
| 169 stream_->Reset(QUIC_ERROR_PROCESSING_STREAM); | 162 stream_->Reset(QUIC_ERROR_PROCESSING_STREAM); |
| 170 return; | 163 return; |
| 171 } | 164 } |
| 172 stream_->AddBytesConsumed(num_bytes_consumed); | 165 stream_->AddBytesConsumed(num_bytes_consumed); |
| 173 } | 166 } |
| 174 | 167 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 186 void QuicStreamSequencer::StopReading() { | 179 void QuicStreamSequencer::StopReading() { |
| 187 if (ignore_read_data_) { | 180 if (ignore_read_data_) { |
| 188 return; | 181 return; |
| 189 } | 182 } |
| 190 ignore_read_data_ = true; | 183 ignore_read_data_ = true; |
| 191 FlushBufferedFrames(); | 184 FlushBufferedFrames(); |
| 192 } | 185 } |
| 193 | 186 |
| 194 void QuicStreamSequencer::FlushBufferedFrames() { | 187 void QuicStreamSequencer::FlushBufferedFrames() { |
| 195 DCHECK(ignore_read_data_); | 188 DCHECK(ignore_read_data_); |
| 196 size_t bytes_flushed = buffered_frames_->FlushBufferedFrames(); | 189 size_t bytes_flushed = buffered_frames_.FlushBufferedFrames(); |
| 197 DVLOG(1) << "Flushing buffered data at offset " | 190 DVLOG(1) << "Flushing buffered data at offset " |
| 198 << buffered_frames_->BytesConsumed() << " length " << bytes_flushed | 191 << buffered_frames_.BytesConsumed() << " length " << bytes_flushed |
| 199 << " for stream " << stream_->id(); | 192 << " for stream " << stream_->id(); |
| 200 stream_->AddBytesConsumed(bytes_flushed); | 193 stream_->AddBytesConsumed(bytes_flushed); |
| 201 MaybeCloseStream(); | 194 MaybeCloseStream(); |
| 202 } | 195 } |
| 203 | 196 |
| 204 size_t QuicStreamSequencer::NumBytesBuffered() const { | 197 size_t QuicStreamSequencer::NumBytesBuffered() const { |
| 205 return buffered_frames_->BytesBuffered(); | 198 return buffered_frames_.BytesBuffered(); |
| 206 } | 199 } |
| 207 | 200 |
| 208 QuicStreamOffset QuicStreamSequencer::NumBytesConsumed() const { | 201 QuicStreamOffset QuicStreamSequencer::NumBytesConsumed() const { |
| 209 return buffered_frames_->BytesConsumed(); | 202 return buffered_frames_.BytesConsumed(); |
| 210 } | 203 } |
| 211 | 204 |
| 212 } // namespace net | 205 } // namespace net |
| OLD | NEW |