| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2016 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 NET_QUIC_CORE_QUIC_FRAMES_H_ | |
| 6 #define NET_QUIC_CORE_QUIC_FRAMES_H_ | |
| 7 | |
| 8 #include <cstdint> | |
| 9 #include <limits> | |
| 10 #include <memory> | |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/logging.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/strings/string_piece.h" | |
| 17 #include "net/quic/core/interval_set.h" | |
| 18 #include "net/quic/core/quic_buffer_allocator.h" | |
| 19 #include "net/quic/core/quic_constants.h" | |
| 20 #include "net/quic/core/quic_error_codes.h" | |
| 21 #include "net/quic/core/quic_time.h" | |
| 22 #include "net/quic/core/quic_types.h" | |
| 23 #include "net/quic/core/quic_versions.h" | |
| 24 | |
| 25 namespace net { | |
| 26 | |
| 27 // A padding frame contains no payload. | |
| 28 struct NET_EXPORT_PRIVATE QuicPaddingFrame { | |
| 29 QuicPaddingFrame() : num_padding_bytes(-1) {} | |
| 30 explicit QuicPaddingFrame(int num_padding_bytes) | |
| 31 : num_padding_bytes(num_padding_bytes) {} | |
| 32 | |
| 33 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
| 34 const QuicPaddingFrame& s); | |
| 35 | |
| 36 // -1: full padding to the end of a max-sized packet | |
| 37 // otherwise: only pad up to num_padding_bytes bytes | |
| 38 int num_padding_bytes; | |
| 39 }; | |
| 40 | |
| 41 // A ping frame contains no payload, though it is retransmittable, | |
| 42 // and ACK'd just like other normal frames. | |
| 43 struct NET_EXPORT_PRIVATE QuicPingFrame {}; | |
| 44 | |
| 45 // A path MTU discovery frame contains no payload and is serialized as a ping | |
| 46 // frame. | |
| 47 struct NET_EXPORT_PRIVATE QuicMtuDiscoveryFrame {}; | |
| 48 | |
| 49 // Deleter for stream buffers. Copyable to support platforms where the deleter | |
| 50 // of a unique_ptr must be copyable. Otherwise it would be nice for this to be | |
| 51 // move-only. | |
| 52 class NET_EXPORT_PRIVATE StreamBufferDeleter { | |
| 53 public: | |
| 54 StreamBufferDeleter() : allocator_(nullptr) {} | |
| 55 explicit StreamBufferDeleter(QuicBufferAllocator* allocator) | |
| 56 : allocator_(allocator) {} | |
| 57 | |
| 58 // Deletes |buffer| using |allocator_|. | |
| 59 void operator()(char* buffer) const; | |
| 60 | |
| 61 private: | |
| 62 // Not owned; must be valid so long as the buffer stored in the unique_ptr | |
| 63 // that owns |this| is valid. | |
| 64 QuicBufferAllocator* allocator_; | |
| 65 }; | |
| 66 | |
| 67 using UniqueStreamBuffer = std::unique_ptr<char[], StreamBufferDeleter>; | |
| 68 | |
| 69 // Allocates memory of size |size| using |allocator| for a QUIC stream buffer. | |
| 70 NET_EXPORT_PRIVATE UniqueStreamBuffer | |
| 71 NewStreamBuffer(QuicBufferAllocator* allocator, size_t size); | |
| 72 | |
| 73 struct NET_EXPORT_PRIVATE QuicStreamFrame { | |
| 74 QuicStreamFrame(); | |
| 75 QuicStreamFrame(QuicStreamId stream_id, | |
| 76 bool fin, | |
| 77 QuicStreamOffset offset, | |
| 78 base::StringPiece data); | |
| 79 QuicStreamFrame(QuicStreamId stream_id, | |
| 80 bool fin, | |
| 81 QuicStreamOffset offset, | |
| 82 QuicPacketLength data_length, | |
| 83 UniqueStreamBuffer buffer); | |
| 84 ~QuicStreamFrame(); | |
| 85 | |
| 86 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
| 87 const QuicStreamFrame& s); | |
| 88 | |
| 89 QuicStreamId stream_id; | |
| 90 bool fin; | |
| 91 QuicPacketLength data_length; | |
| 92 const char* data_buffer; | |
| 93 QuicStreamOffset offset; // Location of this data in the stream. | |
| 94 // nullptr when the QuicStreamFrame is received, and non-null when sent. | |
| 95 UniqueStreamBuffer buffer; | |
| 96 | |
| 97 private: | |
| 98 QuicStreamFrame(QuicStreamId stream_id, | |
| 99 bool fin, | |
| 100 QuicStreamOffset offset, | |
| 101 const char* data_buffer, | |
| 102 QuicPacketLength data_length, | |
| 103 UniqueStreamBuffer buffer); | |
| 104 | |
| 105 DISALLOW_COPY_AND_ASSIGN(QuicStreamFrame); | |
| 106 }; | |
| 107 static_assert(sizeof(QuicStreamFrame) <= 64, | |
| 108 "Keep the QuicStreamFrame size to a cacheline."); | |
| 109 | |
| 110 typedef std::vector<std::pair<QuicPacketNumber, QuicTime>> PacketTimeVector; | |
| 111 | |
| 112 struct NET_EXPORT_PRIVATE QuicStopWaitingFrame { | |
| 113 QuicStopWaitingFrame(); | |
| 114 ~QuicStopWaitingFrame(); | |
| 115 | |
| 116 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
| 117 std::ostream& os, | |
| 118 const QuicStopWaitingFrame& s); | |
| 119 // Path which this stop waiting frame belongs to. | |
| 120 QuicPathId path_id; | |
| 121 // The lowest packet we've sent which is unacked, and we expect an ack for. | |
| 122 QuicPacketNumber least_unacked; | |
| 123 }; | |
| 124 | |
| 125 // A sequence of packet numbers where each number is unique. Intended to be used | |
| 126 // in a sliding window fashion, where smaller old packet numbers are removed and | |
| 127 // larger new packet numbers are added, with the occasional random access. | |
| 128 class NET_EXPORT_PRIVATE PacketNumberQueue { | |
| 129 public: | |
| 130 using const_iterator = IntervalSet<QuicPacketNumber>::const_iterator; | |
| 131 using const_reverse_iterator = | |
| 132 IntervalSet<QuicPacketNumber>::const_reverse_iterator; | |
| 133 | |
| 134 PacketNumberQueue(); | |
| 135 PacketNumberQueue(const PacketNumberQueue& other); | |
| 136 PacketNumberQueue(PacketNumberQueue&& other); | |
| 137 ~PacketNumberQueue(); | |
| 138 | |
| 139 PacketNumberQueue& operator=(const PacketNumberQueue& other); | |
| 140 PacketNumberQueue& operator=(PacketNumberQueue&& other); | |
| 141 | |
| 142 // Adds |packet_number| to the set of packets in the queue. | |
| 143 void Add(QuicPacketNumber packet_number); | |
| 144 | |
| 145 // Adds packets between [lower, higher) to the set of packets in the queue. It | |
| 146 // is undefined behavior to call this with |higher| < |lower|. | |
| 147 void Add(QuicPacketNumber lower, QuicPacketNumber higher); | |
| 148 | |
| 149 // Removes |packet_number| from the set of packets in the queue. | |
| 150 void Remove(QuicPacketNumber packet_number); | |
| 151 | |
| 152 // Removes packets numbers between [lower, higher) to the set of packets in | |
| 153 // the queue. It is undefined behavior to call this with |higher| < |lower|. | |
| 154 void Remove(QuicPacketNumber lower, QuicPacketNumber higher); | |
| 155 | |
| 156 // Removes packets with values less than |higher| from the set of packets in | |
| 157 // the queue. Returns true if packets were removed. | |
| 158 bool RemoveUpTo(QuicPacketNumber higher); | |
| 159 | |
| 160 // Mutates packet number set so that it contains only those packet numbers | |
| 161 // from minimum to maximum packet number not currently in the set. Do nothing | |
| 162 // if packet number set is empty. | |
| 163 void Complement(); | |
| 164 | |
| 165 // Returns true if the queue contains |packet_number|. | |
| 166 bool Contains(QuicPacketNumber packet_number) const; | |
| 167 | |
| 168 // Returns true if the queue is empty. | |
| 169 bool Empty() const; | |
| 170 | |
| 171 // Returns the minimum packet number stored in the queue. It is undefined | |
| 172 // behavior to call this if the queue is empty. | |
| 173 QuicPacketNumber Min() const; | |
| 174 | |
| 175 // Returns the maximum packet number stored in the queue. It is undefined | |
| 176 // behavior to call this if the queue is empty. | |
| 177 QuicPacketNumber Max() const; | |
| 178 | |
| 179 // Returns the number of unique packets stored in the queue. Inefficient; only | |
| 180 // exposed for testing. | |
| 181 size_t NumPacketsSlow() const; | |
| 182 | |
| 183 // Returns the number of disjoint packet number intervals contained in the | |
| 184 // queue. | |
| 185 size_t NumIntervals() const; | |
| 186 | |
| 187 // Returns the length of last interval. | |
| 188 QuicPacketNumber LastIntervalLength() const; | |
| 189 | |
| 190 // Returns iterators over the packet number intervals. | |
| 191 const_iterator begin() const; | |
| 192 const_iterator end() const; | |
| 193 const_reverse_iterator rbegin() const; | |
| 194 const_reverse_iterator rend() const; | |
| 195 const_iterator lower_bound(QuicPacketNumber packet_number) const; | |
| 196 | |
| 197 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
| 198 std::ostream& os, | |
| 199 const PacketNumberQueue& q); | |
| 200 | |
| 201 private: | |
| 202 IntervalSet<QuicPacketNumber> packet_number_intervals_; | |
| 203 }; | |
| 204 | |
| 205 struct NET_EXPORT_PRIVATE QuicAckFrame { | |
| 206 QuicAckFrame(); | |
| 207 QuicAckFrame(const QuicAckFrame& other); | |
| 208 ~QuicAckFrame(); | |
| 209 | |
| 210 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
| 211 const QuicAckFrame& s); | |
| 212 | |
| 213 // The highest packet number we've observed from the peer. | |
| 214 QuicPacketNumber largest_observed; | |
| 215 | |
| 216 // Time elapsed since largest_observed was received until this Ack frame was | |
| 217 // sent. | |
| 218 QuicTime::Delta ack_delay_time; | |
| 219 | |
| 220 // Vector of <packet_number, time> for when packets arrived. | |
| 221 PacketTimeVector received_packet_times; | |
| 222 | |
| 223 // Set of packets. | |
| 224 PacketNumberQueue packets; | |
| 225 | |
| 226 // Path which this ack belongs to. | |
| 227 QuicPathId path_id; | |
| 228 }; | |
| 229 | |
| 230 // True if the packet number is greater than largest_observed or is listed | |
| 231 // as missing. | |
| 232 // Always returns false for packet numbers less than least_unacked. | |
| 233 NET_EXPORT_PRIVATE bool IsAwaitingPacket( | |
| 234 const QuicAckFrame& ack_frame, | |
| 235 QuicPacketNumber packet_number, | |
| 236 QuicPacketNumber peer_least_packet_awaiting_ack); | |
| 237 | |
| 238 struct NET_EXPORT_PRIVATE QuicRstStreamFrame { | |
| 239 QuicRstStreamFrame(); | |
| 240 QuicRstStreamFrame(QuicStreamId stream_id, | |
| 241 QuicRstStreamErrorCode error_code, | |
| 242 QuicStreamOffset bytes_written); | |
| 243 | |
| 244 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
| 245 std::ostream& os, | |
| 246 const QuicRstStreamFrame& r); | |
| 247 | |
| 248 QuicStreamId stream_id; | |
| 249 QuicRstStreamErrorCode error_code; | |
| 250 | |
| 251 // Used to update flow control windows. On termination of a stream, both | |
| 252 // endpoints must inform the peer of the number of bytes they have sent on | |
| 253 // that stream. This can be done through normal termination (data packet with | |
| 254 // FIN) or through a RST. | |
| 255 QuicStreamOffset byte_offset; | |
| 256 }; | |
| 257 | |
| 258 struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame { | |
| 259 QuicConnectionCloseFrame(); | |
| 260 | |
| 261 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
| 262 std::ostream& os, | |
| 263 const QuicConnectionCloseFrame& c); | |
| 264 | |
| 265 QuicErrorCode error_code; | |
| 266 std::string error_details; | |
| 267 }; | |
| 268 | |
| 269 struct NET_EXPORT_PRIVATE QuicGoAwayFrame { | |
| 270 QuicGoAwayFrame(); | |
| 271 QuicGoAwayFrame(QuicErrorCode error_code, | |
| 272 QuicStreamId last_good_stream_id, | |
| 273 const std::string& reason); | |
| 274 | |
| 275 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
| 276 const QuicGoAwayFrame& g); | |
| 277 | |
| 278 QuicErrorCode error_code; | |
| 279 QuicStreamId last_good_stream_id; | |
| 280 std::string reason_phrase; | |
| 281 }; | |
| 282 | |
| 283 // Flow control updates per-stream and at the connection levoel. | |
| 284 // Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather | |
| 285 // than a window delta. | |
| 286 // TODO(rjshade): A possible future optimization is to make stream_id and | |
| 287 // byte_offset variable length, similar to stream frames. | |
| 288 struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame { | |
| 289 QuicWindowUpdateFrame() {} | |
| 290 QuicWindowUpdateFrame(QuicStreamId stream_id, QuicStreamOffset byte_offset); | |
| 291 | |
| 292 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
| 293 std::ostream& os, | |
| 294 const QuicWindowUpdateFrame& w); | |
| 295 | |
| 296 // The stream this frame applies to. 0 is a special case meaning the overall | |
| 297 // connection rather than a specific stream. | |
| 298 QuicStreamId stream_id; | |
| 299 | |
| 300 // Byte offset in the stream or connection. The receiver of this frame must | |
| 301 // not send data which would result in this offset being exceeded. | |
| 302 QuicStreamOffset byte_offset; | |
| 303 }; | |
| 304 | |
| 305 // The BLOCKED frame is used to indicate to the remote endpoint that this | |
| 306 // endpoint believes itself to be flow-control blocked but otherwise ready to | |
| 307 // send data. The BLOCKED frame is purely advisory and optional. | |
| 308 // Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28). | |
| 309 struct NET_EXPORT_PRIVATE QuicBlockedFrame { | |
| 310 QuicBlockedFrame() {} | |
| 311 explicit QuicBlockedFrame(QuicStreamId stream_id); | |
| 312 | |
| 313 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
| 314 const QuicBlockedFrame& b); | |
| 315 | |
| 316 // The stream this frame applies to. 0 is a special case meaning the overall | |
| 317 // connection rather than a specific stream. | |
| 318 QuicStreamId stream_id; | |
| 319 }; | |
| 320 | |
| 321 // The PATH_CLOSE frame is used to explicitly close a path. Both endpoints can | |
| 322 // send a PATH_CLOSE frame to initiate a path termination. A path is considered | |
| 323 // to be closed either a PATH_CLOSE frame is sent or received. An endpoint drops | |
| 324 // receive side of a closed path, and packets with retransmittable frames on a | |
| 325 // closed path are marked as retransmissions which will be transmitted on other | |
| 326 // paths. | |
| 327 struct NET_EXPORT_PRIVATE QuicPathCloseFrame { | |
| 328 QuicPathCloseFrame() {} | |
| 329 explicit QuicPathCloseFrame(QuicPathId path_id); | |
| 330 | |
| 331 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
| 332 std::ostream& os, | |
| 333 const QuicPathCloseFrame& p); | |
| 334 | |
| 335 QuicPathId path_id; | |
| 336 }; | |
| 337 | |
| 338 struct NET_EXPORT_PRIVATE QuicFrame { | |
| 339 QuicFrame(); | |
| 340 explicit QuicFrame(QuicPaddingFrame padding_frame); | |
| 341 explicit QuicFrame(QuicMtuDiscoveryFrame frame); | |
| 342 explicit QuicFrame(QuicPingFrame frame); | |
| 343 | |
| 344 explicit QuicFrame(QuicStreamFrame* stream_frame); | |
| 345 explicit QuicFrame(QuicAckFrame* frame); | |
| 346 explicit QuicFrame(QuicRstStreamFrame* frame); | |
| 347 explicit QuicFrame(QuicConnectionCloseFrame* frame); | |
| 348 explicit QuicFrame(QuicStopWaitingFrame* frame); | |
| 349 explicit QuicFrame(QuicGoAwayFrame* frame); | |
| 350 explicit QuicFrame(QuicWindowUpdateFrame* frame); | |
| 351 explicit QuicFrame(QuicBlockedFrame* frame); | |
| 352 explicit QuicFrame(QuicPathCloseFrame* frame); | |
| 353 | |
| 354 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
| 355 const QuicFrame& frame); | |
| 356 | |
| 357 QuicFrameType type; | |
| 358 union { | |
| 359 // Frames smaller than a pointer are inline. | |
| 360 QuicPaddingFrame padding_frame; | |
| 361 QuicMtuDiscoveryFrame mtu_discovery_frame; | |
| 362 QuicPingFrame ping_frame; | |
| 363 | |
| 364 // Frames larger than a pointer. | |
| 365 QuicStreamFrame* stream_frame; | |
| 366 QuicAckFrame* ack_frame; | |
| 367 QuicStopWaitingFrame* stop_waiting_frame; | |
| 368 QuicRstStreamFrame* rst_stream_frame; | |
| 369 QuicConnectionCloseFrame* connection_close_frame; | |
| 370 QuicGoAwayFrame* goaway_frame; | |
| 371 QuicWindowUpdateFrame* window_update_frame; | |
| 372 QuicBlockedFrame* blocked_frame; | |
| 373 QuicPathCloseFrame* path_close_frame; | |
| 374 }; | |
| 375 }; | |
| 376 // QuicFrameType consumes 8 bytes with padding. | |
| 377 static_assert(sizeof(QuicFrame) <= 16, | |
| 378 "Frames larger than 8 bytes should be referenced by pointer."); | |
| 379 | |
| 380 typedef std::vector<QuicFrame> QuicFrames; | |
| 381 | |
| 382 // Deletes all the sub-frames contained in |frames|. | |
| 383 NET_EXPORT_PRIVATE void DeleteFrames(QuicFrames* frames); | |
| 384 | |
| 385 // Deletes all the QuicStreamFrames for the specified |stream_id|. | |
| 386 NET_EXPORT_PRIVATE void RemoveFramesForStream(QuicFrames* frames, | |
| 387 QuicStreamId stream_id); | |
| 388 | |
| 389 } // namespace net | |
| 390 | |
| 391 #endif // NET_QUIC_CORE_QUIC_FRAMES_H_ | |
| OLD | NEW |