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 | #ifndef QUICHE_QUIC_CORE_QUIC_STREAM_SEQUENCER_H_ |
| 6 | #define QUICHE_QUIC_CORE_QUIC_STREAM_SEQUENCER_H_ |
| 7 | |
| 8 | #include <cstddef> |
| 9 | #include <map> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 10 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 12 | #include "quic/core/quic_packets.h" |
| 13 | #include "quic/core/quic_stream_sequencer_buffer.h" |
| 14 | #include "quic/core/quic_types.h" |
| 15 | #include "quic/platform/api/quic_export.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | |
| 17 | namespace quic { |
| 18 | |
| 19 | namespace test { |
| 20 | class QuicStreamSequencerPeer; |
| 21 | } // namespace test |
| 22 | |
| 23 | // Buffers frames until we have something which can be passed |
| 24 | // up to the next layer. |
| 25 | class QUIC_EXPORT_PRIVATE QuicStreamSequencer { |
| 26 | public: |
| 27 | // Interface that thie Sequencer uses to communicate with the Stream. |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 28 | class QUIC_EXPORT_PRIVATE StreamInterface { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 29 | public: |
| 30 | virtual ~StreamInterface() = default; |
| 31 | |
| 32 | // Called when new data is available to be read from the sequencer. |
| 33 | virtual void OnDataAvailable() = 0; |
| 34 | // Called when the end of the stream has been read. |
| 35 | virtual void OnFinRead() = 0; |
| 36 | // Called when bytes have been consumed from the sequencer. |
| 37 | virtual void AddBytesConsumed(QuicByteCount bytes) = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 38 | // Called when an error has occurred which should result in the stream |
| 39 | // being reset. |
| 40 | virtual void Reset(QuicRstStreamErrorCode error) = 0; |
| 41 | // Called when an error has occurred which should result in the connection |
| 42 | // being closed. |
renjietang | 87df0d0 | 2020-02-13 11:53:52 -0800 | [diff] [blame] | 43 | virtual void OnUnrecoverableError(QuicErrorCode error, |
| 44 | const std::string& details) = 0; |
mattm | 55006b0 | 2021-01-14 15:09:54 -0800 | [diff] [blame] | 45 | // Called when an error has occurred which should result in the connection |
| 46 | // being closed, specifying the wire error code |ietf_error| explicitly. |
| 47 | virtual void OnUnrecoverableError(QuicErrorCode error, |
| 48 | QuicIetfTransportErrorCodes ietf_error, |
| 49 | const std::string& details) = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 50 | // Returns the stream id of this stream. |
| 51 | virtual QuicStreamId id() const = 0; |
fayang | 528c36b | 2020-12-23 07:02:09 -0800 | [diff] [blame] | 52 | |
| 53 | // Returns the QUIC version being used by this stream. |
| 54 | virtual ParsedQuicVersion version() const = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | explicit QuicStreamSequencer(StreamInterface* quic_stream); |
| 58 | QuicStreamSequencer(const QuicStreamSequencer&) = delete; |
| 59 | QuicStreamSequencer(QuicStreamSequencer&&) = default; |
| 60 | QuicStreamSequencer& operator=(const QuicStreamSequencer&) = delete; |
dschinazi | 969837b | 2020-04-20 12:59:40 -0700 | [diff] [blame] | 61 | QuicStreamSequencer& operator=(QuicStreamSequencer&&) = default; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 62 | virtual ~QuicStreamSequencer(); |
| 63 | |
| 64 | // If the frame is the next one we need in order to process in-order data, |
| 65 | // ProcessData will be immediately called on the stream until all buffered |
| 66 | // data is processed or the stream fails to consume data. Any unconsumed |
| 67 | // data will be buffered. If the frame is not the next in line, it will be |
| 68 | // buffered. |
| 69 | void OnStreamFrame(const QuicStreamFrame& frame); |
| 70 | |
| 71 | // If the frame is the next one we need in order to process in-order data, |
| 72 | // ProcessData will be immediately called on the crypto stream until all |
| 73 | // buffered data is processed or the crypto stream fails to consume data. Any |
| 74 | // unconsumed data will be buffered. If the frame is not the next in line, it |
| 75 | // will be buffered. |
| 76 | void OnCryptoFrame(const QuicCryptoFrame& frame); |
| 77 | |
| 78 | // Once data is buffered, it's up to the stream to read it when the stream |
| 79 | // can handle more data. The following three functions make that possible. |
| 80 | |
| 81 | // Fills in up to iov_len iovecs with the next readable regions. Returns the |
| 82 | // number of iovs used. Non-destructive of the underlying data. |
| 83 | int GetReadableRegions(iovec* iov, size_t iov_len) const; |
| 84 | |
| 85 | // Fills in one iovec with the next readable region. Returns false if there |
| 86 | // is no readable region available. |
| 87 | bool GetReadableRegion(iovec* iov) const; |
| 88 | |
bnc | 7b3e0a9 | 2019-06-24 16:06:45 -0700 | [diff] [blame] | 89 | // Fills in one iovec with the region starting at |offset| and returns true. |
| 90 | // Returns false if no readable region is available, either because data has |
| 91 | // not been received yet or has already been consumed. |
| 92 | bool PeekRegion(QuicStreamOffset offset, iovec* iov) const; |
| 93 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 94 | // Copies the data into the iov_len buffers provided. Returns the number of |
| 95 | // bytes read. Any buffered data no longer in use will be released. |
| 96 | // TODO(rch): remove this method and instead implement it as a helper method |
| 97 | // based on GetReadableRegions and MarkConsumed. |
renjietang | 8be8b74 | 2020-01-14 12:41:09 -0800 | [diff] [blame] | 98 | size_t Readv(const struct iovec* iov, size_t iov_len); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 99 | |
| 100 | // Consumes |num_bytes| data. Used in conjunction with |GetReadableRegions| |
| 101 | // to do zero-copy reads. |
| 102 | void MarkConsumed(size_t num_bytes); |
| 103 | |
| 104 | // Appends all of the readable data to |buffer| and marks all of the appended |
| 105 | // data as consumed. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 106 | void Read(std::string* buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 107 | |
| 108 | // Returns true if the sequncer has bytes available for reading. |
| 109 | bool HasBytesToRead() const; |
| 110 | |
| 111 | // Number of bytes available to read. |
| 112 | size_t ReadableBytes() const; |
| 113 | |
| 114 | // Returns true if the sequencer has delivered the fin. |
| 115 | bool IsClosed() const; |
| 116 | |
| 117 | // Calls |OnDataAvailable| on |stream_| if there is buffered data that can |
| 118 | // be processed, and causes |OnDataAvailable| to be called as new data |
| 119 | // arrives. |
| 120 | void SetUnblocked(); |
| 121 | |
| 122 | // Blocks processing of frames until |SetUnblocked| is called. |
| 123 | void SetBlockedUntilFlush(); |
| 124 | |
| 125 | // Sets the sequencer to discard all incoming data itself and not call |
| 126 | // |stream_->OnDataAvailable()|. |stream_->OnFinRead()| will be called |
| 127 | // automatically when the FIN is consumed (which may be immediately). |
| 128 | void StopReading(); |
| 129 | |
| 130 | // Free the memory of underlying buffer. |
| 131 | void ReleaseBuffer(); |
| 132 | |
| 133 | // Free the memory of underlying buffer when no bytes remain in it. |
| 134 | void ReleaseBufferIfEmpty(); |
| 135 | |
| 136 | // Number of bytes in the buffer right now. |
| 137 | size_t NumBytesBuffered() const; |
| 138 | |
| 139 | // Number of bytes has been consumed. |
| 140 | QuicStreamOffset NumBytesConsumed() const; |
| 141 | |
| 142 | QuicStreamOffset close_offset() const { return close_offset_; } |
| 143 | |
| 144 | int num_frames_received() const { return num_frames_received_; } |
| 145 | |
| 146 | int num_duplicate_frames_received() const { |
| 147 | return num_duplicate_frames_received_; |
| 148 | } |
| 149 | |
| 150 | bool ignore_read_data() const { return ignore_read_data_; } |
| 151 | |
| 152 | void set_level_triggered(bool level_triggered) { |
| 153 | level_triggered_ = level_triggered; |
| 154 | } |
| 155 | |
| 156 | bool level_triggered() const { return level_triggered_; } |
| 157 | |
| 158 | void set_stream(StreamInterface* stream) { stream_ = stream; } |
| 159 | |
| 160 | // Returns string describing internal state. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 161 | const std::string DebugString() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 162 | |
| 163 | private: |
| 164 | friend class test::QuicStreamSequencerPeer; |
| 165 | |
| 166 | // Deletes and records as consumed any buffered data that is now in-sequence. |
| 167 | // (To be called only after StopReading has been called.) |
| 168 | void FlushBufferedFrames(); |
| 169 | |
| 170 | // Wait until we've seen 'offset' bytes, and then terminate the stream. |
renjietang | c152cc5 | 2019-08-13 11:54:57 -0700 | [diff] [blame] | 171 | // Returns true if |stream_| is still available to receive data, and false if |
| 172 | // |stream_| is reset. |
| 173 | bool CloseStreamAtOffset(QuicStreamOffset offset); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 174 | |
| 175 | // If we've received a FIN and have processed all remaining data, then inform |
| 176 | // the stream of FIN, and clear buffers. |
bnc | f152d8c | 2019-08-06 06:03:08 -0700 | [diff] [blame] | 177 | void MaybeCloseStream(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 178 | |
| 179 | // Shared implementation between OnStreamFrame and OnCryptoFrame. |
| 180 | void OnFrameData(QuicStreamOffset byte_offset, |
| 181 | size_t data_len, |
| 182 | const char* data_buffer); |
| 183 | |
| 184 | // The stream which owns this sequencer. |
| 185 | StreamInterface* stream_; |
| 186 | |
| 187 | // Stores received data in offset order. |
| 188 | QuicStreamSequencerBuffer buffered_frames_; |
| 189 | |
renjietang | 0398785 | 2019-10-22 12:06:37 -0700 | [diff] [blame] | 190 | // The highest offset that is received so far. |
| 191 | QuicStreamOffset highest_offset_; |
| 192 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 193 | // The offset, if any, we got a stream termination for. When this many bytes |
| 194 | // have been processed, the sequencer will be closed. |
| 195 | QuicStreamOffset close_offset_; |
| 196 | |
| 197 | // If true, the sequencer is blocked from passing data to the stream and will |
| 198 | // buffer all new incoming data until FlushBufferedFrames is called. |
| 199 | bool blocked_; |
| 200 | |
| 201 | // Count of the number of frames received. |
| 202 | int num_frames_received_; |
| 203 | |
| 204 | // Count of the number of duplicate frames received. |
| 205 | int num_duplicate_frames_received_; |
| 206 | |
| 207 | // If true, all incoming data will be discarded. |
| 208 | bool ignore_read_data_; |
| 209 | |
| 210 | // If false, only call OnDataAvailable() when it becomes newly unblocked. |
| 211 | // Otherwise, call OnDataAvailable() when number of readable bytes changes. |
| 212 | bool level_triggered_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | } // namespace quic |
| 216 | |
| 217 | #endif // QUICHE_QUIC_CORE_QUIC_STREAM_SEQUENCER_H_ |