[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [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/quic/reliable_quic_stream.h" |
| 6 | |
[email protected] | 497bfb2 | 2014-01-08 01:28:03 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 8 | #include "net/quic/quic_session.h" |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 9 | #include "net/quic/quic_write_blocked_list.h" |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 10 | |
| 11 | using base::StringPiece; |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 12 | using std::min; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 13 | |
| 14 | namespace net { |
| 15 | |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 16 | #define ENDPOINT (is_server_ ? "Server: " : " Client: ") |
| 17 | |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 18 | namespace { |
| 19 | |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 20 | struct iovec MakeIovec(StringPiece data) { |
| 21 | struct iovec iov = {const_cast<char*>(data.data()), |
| 22 | static_cast<size_t>(data.size())}; |
| 23 | return iov; |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | } // namespace |
| 27 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 28 | ReliableQuicStream::ReliableQuicStream(QuicStreamId id, |
| 29 | QuicSession* session) |
| 30 | : sequencer_(this), |
| 31 | id_(id), |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 32 | session_(session), |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 33 | stream_bytes_read_(0), |
| 34 | stream_bytes_written_(0), |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 35 | stream_error_(QUIC_STREAM_NO_ERROR), |
| 36 | connection_error_(QUIC_NO_ERROR), |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 37 | read_side_closed_(false), |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 38 | write_side_closed_(false), |
| 39 | fin_buffered_(false), |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 40 | fin_sent_(false), |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 41 | rst_sent_(false), |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 42 | is_server_(session_->is_server()) { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ReliableQuicStream::~ReliableQuicStream() { |
| 46 | } |
| 47 | |
| 48 | bool ReliableQuicStream::WillAcceptStreamFrame( |
| 49 | const QuicStreamFrame& frame) const { |
| 50 | if (read_side_closed_) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 51 | return true; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 52 | } |
| 53 | if (frame.stream_id != id_) { |
| 54 | LOG(ERROR) << "Error!"; |
| 55 | return false; |
| 56 | } |
| 57 | return sequencer_.WillAcceptStreamFrame(frame); |
| 58 | } |
| 59 | |
| 60 | bool ReliableQuicStream::OnStreamFrame(const QuicStreamFrame& frame) { |
| 61 | DCHECK_EQ(frame.stream_id, id_); |
| 62 | if (read_side_closed_) { |
[email protected] | 3e5fed1 | 2013-11-22 22:21:41 | [diff] [blame] | 63 | DVLOG(1) << ENDPOINT << "Ignoring frame " << frame.stream_id; |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 64 | // We don't want to be reading: blackhole the data. |
| 65 | return true; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 66 | } |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 67 | // Note: This count include duplicate data received. |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 68 | stream_bytes_read_ += frame.data.TotalBufferSize(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 69 | |
| 70 | bool accepted = sequencer_.OnStreamFrame(frame); |
| 71 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 72 | return accepted; |
| 73 | } |
| 74 | |
[email protected] | bdf2d43 | 2014-02-09 10:45:41 | [diff] [blame] | 75 | void ReliableQuicStream::OnStreamReset(const QuicRstStreamFrame& frame) { |
| 76 | stream_error_ = frame.error_code; |
[email protected] | 2adef90e | 2013-12-02 20:26:57 | [diff] [blame] | 77 | CloseWriteSide(); |
| 78 | CloseReadSide(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 81 | void ReliableQuicStream::OnConnectionClosed(QuicErrorCode error, |
| 82 | bool from_peer) { |
[email protected] | 4e49b6a | 2013-06-18 16:39:28 | [diff] [blame] | 83 | if (read_side_closed_ && write_side_closed_) { |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 84 | return; |
| 85 | } |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 86 | if (error != QUIC_NO_ERROR) { |
| 87 | stream_error_ = QUIC_STREAM_CONNECTION_ERROR; |
| 88 | connection_error_ = error; |
| 89 | } |
| 90 | |
[email protected] | 2adef90e | 2013-12-02 20:26:57 | [diff] [blame] | 91 | CloseWriteSide(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 92 | CloseReadSide(); |
| 93 | } |
| 94 | |
[email protected] | 2adef90e | 2013-12-02 20:26:57 | [diff] [blame] | 95 | void ReliableQuicStream::OnFinRead() { |
| 96 | DCHECK(sequencer_.IsClosed()); |
| 97 | CloseReadSide(); |
| 98 | } |
| 99 | |
| 100 | void ReliableQuicStream::Reset(QuicRstStreamErrorCode error) { |
| 101 | DCHECK_NE(QUIC_STREAM_NO_ERROR, error); |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 102 | stream_error_ = error; |
[email protected] | 2adef90e | 2013-12-02 20:26:57 | [diff] [blame] | 103 | // Sending a RstStream results in calling CloseStream. |
[email protected] | 6ae6e34 | 2014-02-06 02:21:42 | [diff] [blame] | 104 | session()->SendRstStream(id(), error, stream_bytes_written_); |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 105 | rst_sent_ = true; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 108 | void ReliableQuicStream::CloseConnection(QuicErrorCode error) { |
| 109 | session()->connection()->SendConnectionClose(error); |
| 110 | } |
| 111 | |
| 112 | void ReliableQuicStream::CloseConnectionWithDetails(QuicErrorCode error, |
| 113 | const string& details) { |
| 114 | session()->connection()->SendConnectionCloseWithDetails(error, details); |
| 115 | } |
| 116 | |
[email protected] | cb23a92 | 2014-02-20 17:42:38 | [diff] [blame] | 117 | QuicVersion ReliableQuicStream::version() const { |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 118 | return session()->connection()->version(); |
| 119 | } |
| 120 | |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 121 | void ReliableQuicStream::WriteOrBufferData(StringPiece data, bool fin) { |
[email protected] | b55bf4f | 2014-01-24 21:30:26 | [diff] [blame] | 122 | if (data.empty() && !fin) { |
| 123 | LOG(DFATAL) << "data.empty() && !fin"; |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | if (fin_buffered_) { |
| 128 | LOG(DFATAL) << "Fin already buffered"; |
| 129 | return; |
| 130 | } |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 131 | |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 132 | QuicConsumedData consumed_data(0, false); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 133 | fin_buffered_ = fin; |
| 134 | |
| 135 | if (queued_data_.empty()) { |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 136 | struct iovec iov(MakeIovec(data)); |
| 137 | consumed_data = WritevData(&iov, 1, fin, NULL); |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 138 | DCHECK_LE(consumed_data.bytes_consumed, data.length()); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 139 | } |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 140 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 141 | // If there's unconsumed data or an unconsumed fin, queue it. |
| 142 | if (consumed_data.bytes_consumed < data.length() || |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 143 | (fin && !consumed_data.fin_consumed)) { |
| 144 | queued_data_.push_back( |
| 145 | string(data.data() + consumed_data.bytes_consumed, |
| 146 | data.length() - consumed_data.bytes_consumed)); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 147 | } |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void ReliableQuicStream::OnCanWrite() { |
| 151 | bool fin = false; |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 152 | while (!queued_data_.empty()) { |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 153 | const string& data = queued_data_.front(); |
| 154 | if (queued_data_.size() == 1 && fin_buffered_) { |
| 155 | fin = true; |
| 156 | } |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 157 | struct iovec iov(MakeIovec(data)); |
| 158 | QuicConsumedData consumed_data = WritevData(&iov, 1, fin, NULL); |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 159 | if (consumed_data.bytes_consumed == data.size() && |
| 160 | fin == consumed_data.fin_consumed) { |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 161 | queued_data_.pop_front(); |
| 162 | } else { |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 163 | queued_data_.front().erase(0, consumed_data.bytes_consumed); |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 164 | break; |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 169 | QuicConsumedData ReliableQuicStream::WritevData( |
[email protected] | 0ac0c5b | 2013-11-20 05:55:58 | [diff] [blame] | 170 | const struct iovec* iov, |
| 171 | int iov_count, |
| 172 | bool fin, |
| 173 | QuicAckNotifier::DelegateInterface* ack_notifier_delegate) { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 174 | if (write_side_closed_) { |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 175 | DLOG(ERROR) << ENDPOINT << "Attempt to write when the write side is closed"; |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 176 | return QuicConsumedData(0, false); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 179 | size_t write_length = 0u; |
[email protected] | 4d1789c3 | 2013-09-18 23:54:36 | [diff] [blame] | 180 | for (int i = 0; i < iov_count; ++i) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 181 | write_length += iov[i].iov_len; |
[email protected] | cb23a92 | 2014-02-20 17:42:38 | [diff] [blame] | 182 | // TODO(rjshade): Maybe block write based on available flow control window. |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 183 | } |
[email protected] | 93dd91f | 2014-02-27 00:09:03 | [diff] [blame] | 184 | |
| 185 | // Fill an IOVector with bytes from the iovec. |
| 186 | IOVector data; |
| 187 | data.AppendIovecAtMostBytes(iov, iov_count, write_length); |
| 188 | |
[email protected] | 0ac0c5b | 2013-11-20 05:55:58 | [diff] [blame] | 189 | QuicConsumedData consumed_data = session()->WritevData( |
[email protected] | 93dd91f | 2014-02-27 00:09:03 | [diff] [blame] | 190 | id(), data, stream_bytes_written_, fin, ack_notifier_delegate); |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 191 | stream_bytes_written_ += consumed_data.bytes_consumed; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 192 | if (consumed_data.bytes_consumed == write_length) { |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 193 | if (fin && consumed_data.fin_consumed) { |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 194 | fin_sent_ = true; |
| 195 | CloseWriteSide(); |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 196 | } else if (fin && !consumed_data.fin_consumed) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 197 | session_->MarkWriteBlocked(id(), EffectivePriority()); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 198 | } |
| 199 | } else { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 200 | session_->MarkWriteBlocked(id(), EffectivePriority()); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 201 | } |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 202 | return consumed_data; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void ReliableQuicStream::CloseReadSide() { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 206 | if (read_side_closed_) { |
| 207 | return; |
| 208 | } |
[email protected] | 3e5fed1 | 2013-11-22 22:21:41 | [diff] [blame] | 209 | DVLOG(1) << ENDPOINT << "Done reading from stream " << id(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 210 | |
| 211 | read_side_closed_ = true; |
| 212 | if (write_side_closed_) { |
[email protected] | 3e5fed1 | 2013-11-22 22:21:41 | [diff] [blame] | 213 | DVLOG(1) << ENDPOINT << "Closing stream: " << id(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 214 | session_->CloseStream(id()); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void ReliableQuicStream::CloseWriteSide() { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 219 | if (write_side_closed_) { |
| 220 | return; |
| 221 | } |
[email protected] | 3e5fed1 | 2013-11-22 22:21:41 | [diff] [blame] | 222 | DVLOG(1) << ENDPOINT << "Done writing to stream " << id(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 223 | |
| 224 | write_side_closed_ = true; |
| 225 | if (read_side_closed_) { |
[email protected] | 3e5fed1 | 2013-11-22 22:21:41 | [diff] [blame] | 226 | DVLOG(1) << ENDPOINT << "Closing stream: " << id(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 227 | session_->CloseStream(id()); |
| 228 | } |
| 229 | } |
| 230 | |
[email protected] | 6adeb92 | 2013-09-01 22:43:25 | [diff] [blame] | 231 | bool ReliableQuicStream::HasBufferedData() { |
| 232 | return !queued_data_.empty(); |
| 233 | } |
| 234 | |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 235 | void ReliableQuicStream::OnClose() { |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 236 | CloseReadSide(); |
| 237 | CloseWriteSide(); |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 238 | |
| 239 | if (version() > QUIC_VERSION_13 && |
| 240 | !fin_sent_ && !rst_sent_) { |
| 241 | // For flow control accounting, we must tell the peer how many bytes we have |
| 242 | // written on this stream before termination. Done here if needed, using a |
| 243 | // RST frame. |
| 244 | DVLOG(1) << ENDPOINT << "Sending RST in OnClose: " << id(); |
| 245 | session_->SendRstStream(id(), QUIC_STREAM_NO_ERROR, stream_bytes_written_); |
| 246 | rst_sent_ = true; |
| 247 | } |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 250 | } // namespace net |