[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [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 NET_QUIC_QUIC_PROTOCOL_H_ |
| 6 | #define NET_QUIC_QUIC_PROTOCOL_H_ |
| 7 | |
| 8 | #include <limits> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/basictypes.h" |
| 13 | #include "base/hash_tables.h" |
| 14 | #include "base/logging.h" |
| 15 | #include "base/string_piece.h" |
| 16 | #include "net/base/net_export.h" |
| 17 | #include "net/quic/uint128.h" |
| 18 | |
| 19 | namespace net { |
| 20 | |
| 21 | class QuicPacket; |
| 22 | |
| 23 | typedef uint64 QuicGuid; |
| 24 | typedef uint32 QuicStreamId; |
| 25 | typedef uint64 QuicStreamOffset; |
| 26 | typedef uint64 QuicPacketSequenceNumber; |
| 27 | typedef uint64 QuicTransmissionTime; |
| 28 | typedef uint8 QuicFecGroupNumber; |
| 29 | |
| 30 | // TODO(rch): Consider Quic specific names for these constants. |
| 31 | const size_t kMaxPacketSize = 1200; // Maximum size in bytes of a QUIC packet. |
| 32 | |
| 33 | // Maximum number of open streams per connection. |
| 34 | const size_t kDefaultMaxStreamsPerConnection = 100; |
| 35 | |
| 36 | // Size in bytes of the packet header common across all packets. |
| 37 | const size_t kPacketHeaderSize = 25; |
| 38 | // Index of the first byte in a QUIC packet of FEC protected data. |
| 39 | const size_t kStartOfFecProtectedData = kPacketHeaderSize; |
| 40 | // Index of the first byte in a QUIC packet of encrypted data. |
| 41 | const size_t kStartOfEncryptedData = kPacketHeaderSize - 1; |
| 42 | // Index of the first byte in a QUIC packet which is hashed. |
| 43 | const size_t kStartOfHashData = 0; |
| 44 | // Index into the retransmission offset in the header. |
| 45 | // (After GUID and sequence number.) |
| 46 | const int kRetransmissionOffset = 14; |
| 47 | // Index into the transmission time offset in the header. |
| 48 | const int kTransmissionTimeOffset = 15; |
| 49 | |
| 50 | // Size in bytes of all stream fragment fields. |
| 51 | const size_t kMinStreamFragmentLength = 15; |
| 52 | |
| 53 | // Limit on the delta between stream IDs. |
| 54 | const QuicStreamId kMaxStreamIdDelta = 100; |
| 55 | |
| 56 | // Reserved ID for the crypto stream. |
| 57 | // TODO(rch): ensure that this is not usable by any other streams. |
| 58 | const QuicStreamId kCryptoStreamId = 1; |
| 59 | |
| 60 | typedef std::pair<QuicPacketSequenceNumber, QuicPacket*> PacketPair; |
| 61 | |
| 62 | const int64 kDefaultTimeout = 600000000; // 10 minutes |
| 63 | |
| 64 | enum QuicFragmentType { |
| 65 | STREAM_FRAGMENT = 0, |
| 66 | PDU_FRAGMENT, |
| 67 | ACK_FRAGMENT, |
| 68 | RST_STREAM_FRAGMENT, |
| 69 | CONNECTION_CLOSE_FRAGMENT, |
| 70 | NUM_FRAGMENT_TYPES |
| 71 | }; |
| 72 | |
| 73 | enum QuicPacketFlags { |
| 74 | PACKET_FLAGS_NONE = 0, |
| 75 | PACKET_FLAGS_FEC = 1, // Payload is FEC as opposed to fragments. |
| 76 | |
| 77 | PACKET_FLAGS_MAX = PACKET_FLAGS_FEC |
| 78 | }; |
| 79 | |
| 80 | enum QuicErrorCode { |
| 81 | // Stream errors. |
| 82 | QUIC_NO_ERROR = 0, |
| 83 | |
| 84 | // There were data fragments after the a fin or reset. |
| 85 | QUIC_STREAM_DATA_AFTER_TERMINATION, |
| 86 | // There was some server error which halted stream processing. |
| 87 | QUIC_SERVER_ERROR_PROCESSING_STREAM, |
| 88 | // We got two fin or reset offsets which did not match. |
| 89 | QUIC_MULTIPLE_TERMINATION_OFFSETS, |
| 90 | // We got bad payload and can not respond to it at the protocol level. |
| 91 | QUIC_BAD_APPLICATION_PAYLOAD, |
| 92 | |
| 93 | // Connection errors. |
| 94 | |
| 95 | // Control frame is malformed. |
| 96 | QUIC_INVALID_PACKET_HEADER, |
| 97 | // Fragment data is malformed. |
| 98 | QUIC_INVALID_FRAGMENT_DATA, |
| 99 | // FEC data is malformed. |
| 100 | QUIC_INVALID_FEC_DATA, |
| 101 | // Stream rst data is malformed |
| 102 | QUIC_INVALID_RST_STREAM_DATA, |
| 103 | // Connection close data is malformed. |
| 104 | QUIC_INVALID_CONNECTION_CLOSE_DATA, |
| 105 | // Ack data is malformed. |
| 106 | QUIC_INVALID_ACK_DATA, |
| 107 | // There was an error decrypting. |
| 108 | QUIC_DECRYPTION_FAILURE, |
| 109 | // There was an error encrypting. |
| 110 | QUIC_ENCRYPTION_FAILURE, |
| 111 | // The packet exceeded kMaxPacketSize. |
| 112 | QUIC_PACKET_TOO_LARGE, |
| 113 | // Data was sent for a stream which did not exist. |
| 114 | QUIC_PACKET_FOR_NONEXISTENT_STREAM, |
| 115 | // The client is going away (browser close, etc.) |
| 116 | QUIC_CLIENT_GOING_AWAY, |
| 117 | // The server is going away (restart etc.) |
| 118 | QUIC_SERVER_GOING_AWAY, |
| 119 | // A stream ID was invalid. |
| 120 | QUIC_INVALID_STREAM_ID, |
| 121 | // Too many streams already open. |
| 122 | QUIC_TOO_MANY_OPEN_STREAMS, |
| 123 | |
| 124 | // We hit our prenegotiated (or default) timeout |
| 125 | QUIC_CONNECTION_TIMED_OUT, |
| 126 | |
| 127 | // Crypto errors. |
| 128 | |
| 129 | // Handshake message contained out of order tags. |
| 130 | QUIC_CRYPTO_TAGS_OUT_OF_ORDER, |
| 131 | // Handshake message contained too many entires. |
| 132 | QUIC_CRYPTO_TOO_MANY_ENTRIES, |
| 133 | // Handshake message contained an invalid value length. |
| 134 | QUIC_CRYPTO_INVALID_VALUE_LENGTH, |
| 135 | // A crypto message was received after the handshake was complete. |
| 136 | QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE, |
| 137 | // A crypto message was receieved with an illegal message tag. |
| 138 | QUIC_INVALID_CRYPTO_MESSAGE_TYPE, |
| 139 | |
| 140 | }; |
| 141 | |
| 142 | struct NET_EXPORT_PRIVATE QuicPacketHeader { |
| 143 | // Includes the ConnectionHeader and CongestionMonitoredHeader |
| 144 | // from the design docs, as well as some elements of DecryptedData. |
| 145 | QuicGuid guid; |
| 146 | QuicPacketSequenceNumber packet_sequence_number; |
| 147 | uint8 retransmission_count; |
| 148 | QuicTransmissionTime transmission_time; |
| 149 | QuicPacketFlags flags; |
| 150 | QuicFecGroupNumber fec_group; |
| 151 | }; |
| 152 | |
| 153 | struct NET_EXPORT_PRIVATE QuicStreamFragment { |
| 154 | QuicStreamFragment(); |
| 155 | QuicStreamFragment(QuicStreamId stream_id, |
| 156 | bool fin, |
| 157 | uint64 offset, |
| 158 | base::StringPiece data); |
| 159 | |
| 160 | QuicStreamId stream_id; |
| 161 | bool fin; |
| 162 | uint64 offset; |
| 163 | base::StringPiece data; |
| 164 | }; |
| 165 | |
| 166 | struct NET_EXPORT_PRIVATE ReceivedPacketInfo { |
| 167 | ReceivedPacketInfo(); |
| 168 | ~ReceivedPacketInfo(); |
| 169 | // The highest packet sequence number we've received from the peer. |
| 170 | QuicPacketSequenceNumber largest_received; |
| 171 | // The time at which we received the above packet. |
| 172 | QuicTransmissionTime time_received; |
| 173 | // The set of packets which we're expecting and have not received. |
| 174 | // This includes any packets between the lowest and largest_received |
| 175 | // which we have neither seen nor been informed are non-retransmitting. |
| 176 | base::hash_set<QuicPacketSequenceNumber> missing_packets; |
| 177 | }; |
| 178 | |
| 179 | struct NET_EXPORT_PRIVATE SentPacketInfo { |
| 180 | SentPacketInfo(); |
| 181 | ~SentPacketInfo(); |
| 182 | // The lowest packet we've sent which is unacked, and we expect an ack for. |
| 183 | QuicPacketSequenceNumber least_unacked; |
| 184 | // The set of packets between least_unacked and the last packet we have sent |
| 185 | // which we will not resend. |
| 186 | base::hash_set<QuicPacketSequenceNumber> non_retransmiting; |
| 187 | }; |
| 188 | |
| 189 | // Defines for all types of congestion feedback that will be negotiated in QUIC, |
| 190 | // kTCP MUST be supported by all QUIC implementations to guarentee 100% |
| 191 | // compatibility. |
| 192 | enum CongestionFeedbackType { |
| 193 | kNone = 0, // No feedback provided |
| 194 | kTCP, // Used to mimic TCP. |
| 195 | kInterArrival, // Use additional inter arrival information. |
| 196 | kFixRate, // Provided for testing. |
| 197 | }; |
| 198 | |
| 199 | struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP { |
| 200 | uint16 accumulated_number_of_lost_packets; |
| 201 | uint16 receive_window; // Number of bytes >> 4. |
| 202 | }; |
| 203 | |
| 204 | struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival { |
| 205 | uint16 accumulated_number_of_lost_packets; |
| 206 | int16 offset_time; |
| 207 | uint16 delta_time; // delta time is described below. |
| 208 | }; |
| 209 | |
| 210 | /* |
| 211 | * Description of delta time. |
| 212 | * |
| 213 | * 0 1 |
| 214 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 215 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 216 | * |D|S| offset_time | |
| 217 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 218 | * |
| 219 | * Where: |
| 220 | * D is the time domain. If set time domain is in milliseconds, else in |
| 221 | * microseconds. |
| 222 | * S is the sign bit. |
| 223 | * offset_time is the time offset where the relative packet size is equal to |
| 224 | * 0. |
| 225 | */ |
| 226 | |
| 227 | struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate { |
| 228 | uint32 bitrate_in_bytes_per_second; |
| 229 | }; |
| 230 | |
| 231 | struct NET_EXPORT_PRIVATE CongestionInfo { |
| 232 | CongestionFeedbackType type; |
| 233 | union { |
| 234 | CongestionFeedbackMessageTCP tcp; |
| 235 | CongestionFeedbackMessageInterArrival inter_arrival; |
| 236 | CongestionFeedbackMessageFixRate fix_rate; |
| 237 | }; |
| 238 | }; |
| 239 | |
| 240 | struct NET_EXPORT_PRIVATE QuicAckFragment { |
| 241 | QuicAckFragment() {} |
| 242 | QuicAckFragment(QuicPacketSequenceNumber largest_received, |
| 243 | QuicTransmissionTime time_received, |
| 244 | QuicPacketSequenceNumber least_unacked) { |
| 245 | received_info.largest_received = largest_received; |
| 246 | received_info.time_received = time_received; |
| 247 | sent_info.least_unacked = least_unacked; |
| 248 | congestion_info.type = kNone; |
| 249 | } |
| 250 | |
| 251 | SentPacketInfo sent_info; |
| 252 | ReceivedPacketInfo received_info; |
| 253 | CongestionInfo congestion_info; |
| 254 | |
| 255 | friend std::ostream& operator<<(std::ostream& os, const QuicAckFragment& s) { |
| 256 | os << "largest_received: " << s.received_info.largest_received |
| 257 | << " time: " << s.received_info.time_received |
| 258 | << " missing: "; |
| 259 | for (base::hash_set<QuicPacketSequenceNumber>::const_iterator it = |
| 260 | s.received_info.missing_packets.begin(); |
| 261 | it != s.received_info.missing_packets.end(); ++it) { |
| 262 | os << *it << " "; |
| 263 | } |
| 264 | |
| 265 | os << " least_waiting: " << s.sent_info.least_unacked |
| 266 | << " no_retransmit: "; |
| 267 | for (base::hash_set<QuicPacketSequenceNumber>::const_iterator it = |
| 268 | s.sent_info.non_retransmiting.begin(); |
| 269 | it != s.sent_info.non_retransmiting.end(); ++it) { |
| 270 | os << *it << " "; |
| 271 | } |
| 272 | os << "\n"; |
| 273 | return os; |
| 274 | } |
| 275 | }; |
| 276 | |
| 277 | struct NET_EXPORT_PRIVATE QuicRstStreamFragment { |
| 278 | QuicRstStreamFragment() {} |
| 279 | QuicRstStreamFragment(QuicStreamId stream_id, uint64 offset, |
| 280 | QuicErrorCode details) |
| 281 | : stream_id(stream_id), offset(offset), details(details) { |
| 282 | DCHECK_LE(details, std::numeric_limits<uint8>::max()); |
| 283 | } |
| 284 | |
| 285 | QuicStreamId stream_id; |
| 286 | uint64 offset; |
| 287 | QuicErrorCode details; |
| 288 | }; |
| 289 | |
| 290 | struct NET_EXPORT_PRIVATE QuicConnectionCloseFragment { |
| 291 | QuicErrorCode details; |
| 292 | QuicAckFragment ack_fragment; |
| 293 | }; |
| 294 | |
| 295 | struct NET_EXPORT_PRIVATE QuicFragment { |
| 296 | QuicFragment() {} |
| 297 | explicit QuicFragment(QuicStreamFragment* stream_fragment) |
| 298 | : type(STREAM_FRAGMENT), stream_fragment(stream_fragment) { |
| 299 | } |
| 300 | explicit QuicFragment(QuicAckFragment* fragment) |
| 301 | : type(ACK_FRAGMENT), ack_fragment(fragment) { |
| 302 | } |
| 303 | explicit QuicFragment(QuicRstStreamFragment* fragment) |
| 304 | : type(RST_STREAM_FRAGMENT), |
| 305 | rst_stream_fragment(fragment) { |
| 306 | } |
| 307 | explicit QuicFragment(QuicConnectionCloseFragment* fragment) |
| 308 | : type(CONNECTION_CLOSE_FRAGMENT), |
| 309 | connection_close_fragment(fragment) { |
| 310 | } |
| 311 | |
| 312 | QuicFragmentType type; |
| 313 | union { |
| 314 | QuicStreamFragment* stream_fragment; |
| 315 | QuicAckFragment* ack_fragment; |
| 316 | QuicRstStreamFragment* rst_stream_fragment; |
| 317 | QuicConnectionCloseFragment* connection_close_fragment; |
| 318 | }; |
| 319 | }; |
| 320 | |
| 321 | typedef std::vector<QuicFragment> QuicFragments; |
| 322 | |
| 323 | struct NET_EXPORT_PRIVATE QuicFecData { |
| 324 | QuicFecData(); |
| 325 | QuicFecGroupNumber fec_group; |
| 326 | QuicPacketSequenceNumber first_protected_packet_sequence_number; |
| 327 | // The last protected packet's sequence number will be one |
| 328 | // less than the sequence number of the FEC packet. |
| 329 | base::StringPiece redundancy; |
| 330 | bool operator==(const QuicFecData& other) const { |
| 331 | if (fec_group != other.fec_group) { |
| 332 | return false; |
| 333 | } |
| 334 | if (first_protected_packet_sequence_number != |
| 335 | other.first_protected_packet_sequence_number) { |
| 336 | return false; |
| 337 | } |
| 338 | if (redundancy != other.redundancy) { |
| 339 | return false; |
| 340 | } |
| 341 | return true; |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | struct NET_EXPORT_PRIVATE QuicPacketData { |
| 346 | std::string data; |
| 347 | }; |
| 348 | |
| 349 | class NET_EXPORT_PRIVATE QuicData { |
| 350 | public: |
| 351 | QuicData(const char* buffer, size_t length) |
| 352 | : buffer_(buffer), |
| 353 | length_(length), |
| 354 | owns_buffer_(false) { } |
| 355 | |
| 356 | QuicData(char* buffer, size_t length, bool owns_buffer) |
| 357 | : buffer_(buffer), |
| 358 | length_(length), |
| 359 | owns_buffer_(owns_buffer) { } |
| 360 | |
| 361 | virtual ~QuicData(); |
| 362 | |
| 363 | base::StringPiece AsStringPiece() const { |
| 364 | return base::StringPiece(data(), length()); |
| 365 | } |
| 366 | |
| 367 | const char* data() const { return buffer_; } |
| 368 | size_t length() const { return length_; } |
| 369 | |
| 370 | private: |
| 371 | const char* buffer_; |
| 372 | size_t length_; |
| 373 | bool owns_buffer_; |
| 374 | |
| 375 | DISALLOW_COPY_AND_ASSIGN(QuicData); |
| 376 | }; |
| 377 | |
| 378 | class NET_EXPORT_PRIVATE QuicPacket : public QuicData { |
| 379 | public: |
| 380 | QuicPacket(char* buffer, size_t length, bool owns_buffer) |
| 381 | : QuicData(buffer, length, owns_buffer), |
| 382 | buffer_(buffer) { } |
| 383 | |
| 384 | base::StringPiece FecProtectedData() const { |
| 385 | return base::StringPiece(data() + kStartOfFecProtectedData, |
| 386 | length() - kStartOfFecProtectedData); |
| 387 | } |
| 388 | |
| 389 | base::StringPiece AssociatedData() const { |
| 390 | return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData); |
| 391 | } |
| 392 | |
| 393 | base::StringPiece Plaintext() const { |
| 394 | return base::StringPiece(data() + kStartOfEncryptedData, |
| 395 | length() - kStartOfEncryptedData); |
| 396 | } |
| 397 | char* mutable_data() { return buffer_; } |
| 398 | |
| 399 | private: |
| 400 | char* buffer_; |
| 401 | |
| 402 | // TODO(rch): DISALLOW_COPY_AND_ASSIGN |
| 403 | }; |
| 404 | |
| 405 | class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData { |
| 406 | public: |
| 407 | QuicEncryptedPacket(const char* buffer, size_t length) |
| 408 | : QuicData(buffer, length) { } |
| 409 | |
| 410 | QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer) |
| 411 | : QuicData(buffer, length, owns_buffer) { } |
| 412 | |
| 413 | base::StringPiece AssociatedData() const { |
| 414 | return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData); |
| 415 | } |
| 416 | |
| 417 | // TODO(rch): DISALLOW_COPY_AND_ASSIGN |
| 418 | }; |
| 419 | |
| 420 | } // namespace net |
| 421 | |
| 422 | #endif // NET_QUIC_QUIC_PROTOCOL_H_ |