quic_protocol.h revision 010d83a9304c5a91596085d917d248abff47903a
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 <stddef.h> 9#include <limits> 10#include <map> 11#include <ostream> 12#include <set> 13#include <string> 14#include <utility> 15#include <vector> 16 17#include "base/basictypes.h" 18#include "base/containers/hash_tables.h" 19#include "base/logging.h" 20#include "base/strings/string_piece.h" 21#include "net/base/int128.h" 22#include "net/base/ip_endpoint.h" 23#include "net/base/net_export.h" 24#include "net/quic/iovector.h" 25#include "net/quic/quic_bandwidth.h" 26#include "net/quic/quic_time.h" 27 28namespace net { 29 30using ::operator<<; 31 32class QuicAckNotifier; 33class QuicPacket; 34struct QuicPacketHeader; 35 36typedef uint64 QuicConnectionId; 37typedef uint32 QuicStreamId; 38typedef uint64 QuicStreamOffset; 39typedef uint64 QuicPacketSequenceNumber; 40typedef QuicPacketSequenceNumber QuicFecGroupNumber; 41typedef uint64 QuicPublicResetNonceProof; 42typedef uint8 QuicPacketEntropyHash; 43typedef uint32 QuicHeaderId; 44// QuicTag is the type of a tag in the wire protocol. 45typedef uint32 QuicTag; 46typedef std::vector<QuicTag> QuicTagVector; 47typedef std::map<QuicTag, std::string> QuicTagValueMap; 48// TODO(rtenneti): Didn't use SpdyPriority because SpdyPriority is uint8 and 49// QuicPriority is uint32. Use SpdyPriority when we change the QUIC_VERSION. 50typedef uint32 QuicPriority; 51 52// TODO(rch): Consider Quic specific names for these constants. 53// Default and initial maximum size in bytes of a QUIC packet. 54const QuicByteCount kDefaultMaxPacketSize = 1200; 55// The maximum packet size of any QUIC packet, based on ethernet's max size, 56// minus the IP and UDP headers. IPv6 has a 40 byte header, UPD adds an 57// additional 8 bytes. This is a total overhead of 48 bytes. Ethernet's 58// max packet size is 1500 bytes, 1500 - 48 = 1452. 59const QuicByteCount kMaxPacketSize = 1452; 60 61// Maximum size of the initial congestion window in packets. 62const size_t kDefaultInitialWindow = 10; 63const uint32 kMaxInitialWindow = 100; 64 65// Default size of initial flow control window. 66const uint32 kDefaultFlowControlSendWindow = 16 * 1024; // 16 KB 67 68// Maximum size of the congestion window, in packets, for TCP congestion control 69// algorithms. 70const size_t kMaxTcpCongestionWindow = 200; 71 72// Don't allow a client to suggest an RTT longer than 15 seconds. 73const uint32 kMaxInitialRoundTripTimeUs = 15 * kNumMicrosPerSecond; 74 75// Maximum number of open streams per connection. 76const size_t kDefaultMaxStreamsPerConnection = 100; 77 78// Number of bytes reserved for public flags in the packet header. 79const size_t kPublicFlagsSize = 1; 80// Number of bytes reserved for version number in the packet header. 81const size_t kQuicVersionSize = 4; 82// Number of bytes reserved for private flags in the packet header. 83const size_t kPrivateFlagsSize = 1; 84// Number of bytes reserved for FEC group in the packet header. 85const size_t kFecGroupSize = 1; 86 87// Signifies that the QuicPacket will contain version of the protocol. 88const bool kIncludeVersion = true; 89 90// Index of the first byte in a QUIC packet which is used in hash calculation. 91const size_t kStartOfHashData = 0; 92 93// Limit on the delta between stream IDs. 94const QuicStreamId kMaxStreamIdDelta = 200; 95// Limit on the delta between header IDs. 96const QuicHeaderId kMaxHeaderIdDelta = 200; 97 98// Reserved ID for the crypto stream. 99const QuicStreamId kCryptoStreamId = 1; 100 101// Reserved ID for the headers stream. 102const QuicStreamId kHeadersStreamId = 3; 103 104// This is the default network timeout a for connection till the crypto 105// handshake succeeds and the negotiated timeout from the handshake is received. 106const int64 kDefaultInitialTimeoutSecs = 120; // 2 mins. 107const int64 kDefaultTimeoutSecs = 60 * 10; // 10 minutes. 108const int64 kDefaultMaxTimeForCryptoHandshakeSecs = 5; // 5 secs. 109 110// Default ping timeout. 111const int64 kPingTimeoutSecs = 15; // 15 secs. 112 113// We define an unsigned 16-bit floating point value, inspired by IEEE floats 114// (http://en.wikipedia.org/wiki/Half_precision_floating-point_format), 115// with 5-bit exponent (bias 1), 11-bit mantissa (effective 12 with hidden 116// bit) and denormals, but without signs, transfinites or fractions. Wire format 117// 16 bits (little-endian byte order) are split into exponent (high 5) and 118// mantissa (low 11) and decoded as: 119// uint64 value; 120// if (exponent == 0) value = mantissa; 121// else value = (mantissa | 1 << 11) << (exponent - 1) 122const int kUFloat16ExponentBits = 5; 123const int kUFloat16MaxExponent = (1 << kUFloat16ExponentBits) - 2; // 30 124const int kUFloat16MantissaBits = 16 - kUFloat16ExponentBits; // 11 125const int kUFloat16MantissaEffectiveBits = kUFloat16MantissaBits + 1; // 12 126const uint64 kUFloat16MaxValue = // 0x3FFC0000000 127 ((GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits) - 1) << 128 kUFloat16MaxExponent; 129 130enum TransmissionType { 131 NOT_RETRANSMISSION, 132 FIRST_TRANSMISSION_TYPE = NOT_RETRANSMISSION, 133 HANDSHAKE_RETRANSMISSION, // Retransmits due to handshake timeouts. 134 ALL_UNACKED_RETRANSMISSION, // Retransmits of all unacked packets. 135 LOSS_RETRANSMISSION, // Retransmits due to loss detection. 136 RTO_RETRANSMISSION, // Retransmits due to retransmit time out. 137 TLP_RETRANSMISSION, // Tail loss probes. 138 LAST_TRANSMISSION_TYPE = TLP_RETRANSMISSION, 139}; 140 141enum RetransmissionType { 142 INITIAL_ENCRYPTION_ONLY, 143 ALL_PACKETS 144}; 145 146enum HasRetransmittableData { 147 NO_RETRANSMITTABLE_DATA, 148 HAS_RETRANSMITTABLE_DATA, 149}; 150 151enum IsHandshake { 152 NOT_HANDSHAKE, 153 IS_HANDSHAKE 154}; 155 156enum QuicFrameType { 157 // Regular frame types. The values set here cannot change without the 158 // introduction of a new QUIC version. 159 PADDING_FRAME = 0, 160 RST_STREAM_FRAME = 1, 161 CONNECTION_CLOSE_FRAME = 2, 162 GOAWAY_FRAME = 3, 163 WINDOW_UPDATE_FRAME = 4, 164 BLOCKED_FRAME = 5, 165 STOP_WAITING_FRAME = 6, 166 PING_FRAME = 7, 167 168 // STREAM, ACK, and CONGESTION_FEEDBACK frames are special frames. They are 169 // encoded differently on the wire and their values do not need to be stable. 170 STREAM_FRAME, 171 ACK_FRAME, 172 CONGESTION_FEEDBACK_FRAME, 173 NUM_FRAME_TYPES 174}; 175 176enum QuicConnectionIdLength { 177 PACKET_0BYTE_CONNECTION_ID = 0, 178 PACKET_1BYTE_CONNECTION_ID = 1, 179 PACKET_4BYTE_CONNECTION_ID = 4, 180 PACKET_8BYTE_CONNECTION_ID = 8 181}; 182 183enum InFecGroup { 184 NOT_IN_FEC_GROUP, 185 IN_FEC_GROUP, 186}; 187 188enum QuicSequenceNumberLength { 189 PACKET_1BYTE_SEQUENCE_NUMBER = 1, 190 PACKET_2BYTE_SEQUENCE_NUMBER = 2, 191 PACKET_4BYTE_SEQUENCE_NUMBER = 4, 192 PACKET_6BYTE_SEQUENCE_NUMBER = 6 193}; 194 195// Used to indicate a QuicSequenceNumberLength using two flag bits. 196enum QuicSequenceNumberLengthFlags { 197 PACKET_FLAGS_1BYTE_SEQUENCE = 0, // 00 198 PACKET_FLAGS_2BYTE_SEQUENCE = 1, // 01 199 PACKET_FLAGS_4BYTE_SEQUENCE = 1 << 1, // 10 200 PACKET_FLAGS_6BYTE_SEQUENCE = 1 << 1 | 1, // 11 201}; 202 203// The public flags are specified in one byte. 204enum QuicPacketPublicFlags { 205 PACKET_PUBLIC_FLAGS_NONE = 0, 206 207 // Bit 0: Does the packet header contains version info? 208 PACKET_PUBLIC_FLAGS_VERSION = 1 << 0, 209 210 // Bit 1: Is this packet a public reset packet? 211 PACKET_PUBLIC_FLAGS_RST = 1 << 1, 212 213 // Bits 2 and 3 specify the length of the ConnectionId as follows: 214 // ----00--: 0 bytes 215 // ----01--: 1 byte 216 // ----10--: 4 bytes 217 // ----11--: 8 bytes 218 PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID = 0, 219 PACKET_PUBLIC_FLAGS_1BYTE_CONNECTION_ID = 1 << 2, 220 PACKET_PUBLIC_FLAGS_4BYTE_CONNECTION_ID = 1 << 3, 221 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID = 1 << 3 | 1 << 2, 222 223 // Bits 4 and 5 describe the packet sequence number length as follows: 224 // --00----: 1 byte 225 // --01----: 2 bytes 226 // --10----: 4 bytes 227 // --11----: 6 bytes 228 PACKET_PUBLIC_FLAGS_1BYTE_SEQUENCE = PACKET_FLAGS_1BYTE_SEQUENCE << 4, 229 PACKET_PUBLIC_FLAGS_2BYTE_SEQUENCE = PACKET_FLAGS_2BYTE_SEQUENCE << 4, 230 PACKET_PUBLIC_FLAGS_4BYTE_SEQUENCE = PACKET_FLAGS_4BYTE_SEQUENCE << 4, 231 PACKET_PUBLIC_FLAGS_6BYTE_SEQUENCE = PACKET_FLAGS_6BYTE_SEQUENCE << 4, 232 233 // All bits set (bits 6 and 7 are not currently used): 00111111 234 PACKET_PUBLIC_FLAGS_MAX = (1 << 6) - 1 235}; 236 237// The private flags are specified in one byte. 238enum QuicPacketPrivateFlags { 239 PACKET_PRIVATE_FLAGS_NONE = 0, 240 241 // Bit 0: Does this packet contain an entropy bit? 242 PACKET_PRIVATE_FLAGS_ENTROPY = 1 << 0, 243 244 // Bit 1: Payload is part of an FEC group? 245 PACKET_PRIVATE_FLAGS_FEC_GROUP = 1 << 1, 246 247 // Bit 2: Payload is FEC as opposed to frames? 248 PACKET_PRIVATE_FLAGS_FEC = 1 << 2, 249 250 // All bits set (bits 3-7 are not currently used): 00000111 251 PACKET_PRIVATE_FLAGS_MAX = (1 << 3) - 1 252}; 253 254// The available versions of QUIC. Guaranteed that the integer value of the enum 255// will match the version number. 256// When adding a new version to this enum you should add it to 257// kSupportedQuicVersions (if appropriate), and also add a new case to the 258// helper methods QuicVersionToQuicTag, QuicTagToQuicVersion, and 259// QuicVersionToString. 260enum QuicVersion { 261 // Special case to indicate unknown/unsupported QUIC version. 262 QUIC_VERSION_UNSUPPORTED = 0, 263 264 QUIC_VERSION_15 = 15, 265 QUIC_VERSION_16 = 16, 266 QUIC_VERSION_17 = 17, 267 QUIC_VERSION_18 = 18, 268 QUIC_VERSION_19 = 19, // Current version. 269}; 270 271// This vector contains QUIC versions which we currently support. 272// This should be ordered such that the highest supported version is the first 273// element, with subsequent elements in descending order (versions can be 274// skipped as necessary). 275// 276// IMPORTANT: if you are addding to this list, follow the instructions at 277// http://sites/quic/adding-and-removing-versions 278static const QuicVersion kSupportedQuicVersions[] = {QUIC_VERSION_18, 279 QUIC_VERSION_17, 280 QUIC_VERSION_16, 281 QUIC_VERSION_15}; 282 283typedef std::vector<QuicVersion> QuicVersionVector; 284 285// Returns a vector of QUIC versions in kSupportedQuicVersions. 286NET_EXPORT_PRIVATE QuicVersionVector QuicSupportedVersions(); 287 288// QuicTag is written to and read from the wire, but we prefer to use 289// the more readable QuicVersion at other levels. 290// Helper function which translates from a QuicVersion to a QuicTag. Returns 0 291// if QuicVersion is unsupported. 292NET_EXPORT_PRIVATE QuicTag QuicVersionToQuicTag(const QuicVersion version); 293 294// Returns appropriate QuicVersion from a QuicTag. 295// Returns QUIC_VERSION_UNSUPPORTED if version_tag cannot be understood. 296NET_EXPORT_PRIVATE QuicVersion QuicTagToQuicVersion(const QuicTag version_tag); 297 298// Helper function which translates from a QuicVersion to a string. 299// Returns strings corresponding to enum names (e.g. QUIC_VERSION_6). 300NET_EXPORT_PRIVATE std::string QuicVersionToString(const QuicVersion version); 301 302// Returns comma separated list of string representations of QuicVersion enum 303// values in the supplied |versions| vector. 304NET_EXPORT_PRIVATE std::string QuicVersionVectorToString( 305 const QuicVersionVector& versions); 306 307// Version and Crypto tags are written to the wire with a big-endian 308// representation of the name of the tag. For example 309// the client hello tag (CHLO) will be written as the 310// following 4 bytes: 'C' 'H' 'L' 'O'. Since it is 311// stored in memory as a little endian uint32, we need 312// to reverse the order of the bytes. 313 314// MakeQuicTag returns a value given the four bytes. For example: 315// MakeQuicTag('C', 'H', 'L', 'O'); 316NET_EXPORT_PRIVATE QuicTag MakeQuicTag(char a, char b, char c, char d); 317 318// Size in bytes of the data or fec packet header. 319NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(const QuicPacketHeader& header); 320 321NET_EXPORT_PRIVATE size_t GetPacketHeaderSize( 322 QuicConnectionIdLength connection_id_length, 323 bool include_version, 324 QuicSequenceNumberLength sequence_number_length, 325 InFecGroup is_in_fec_group); 326 327// Index of the first byte in a QUIC packet of FEC protected data. 328NET_EXPORT_PRIVATE size_t GetStartOfFecProtectedData( 329 QuicConnectionIdLength connection_id_length, 330 bool include_version, 331 QuicSequenceNumberLength sequence_number_length); 332// Index of the first byte in a QUIC packet of encrypted data. 333NET_EXPORT_PRIVATE size_t GetStartOfEncryptedData( 334 QuicConnectionIdLength connection_id_length, 335 bool include_version, 336 QuicSequenceNumberLength sequence_number_length); 337 338enum QuicRstStreamErrorCode { 339 QUIC_STREAM_NO_ERROR = 0, 340 341 // There was some error which halted stream processing. 342 QUIC_ERROR_PROCESSING_STREAM, 343 // We got two fin or reset offsets which did not match. 344 QUIC_MULTIPLE_TERMINATION_OFFSETS, 345 // We got bad payload and can not respond to it at the protocol level. 346 QUIC_BAD_APPLICATION_PAYLOAD, 347 // Stream closed due to connection error. No reset frame is sent when this 348 // happens. 349 QUIC_STREAM_CONNECTION_ERROR, 350 // GoAway frame sent. No more stream can be created. 351 QUIC_STREAM_PEER_GOING_AWAY, 352 // The stream has been cancelled. 353 QUIC_STREAM_CANCELLED, 354 // Sending a RST to allow for proper flow control accounting. 355 QUIC_RST_FLOW_CONTROL_ACCOUNTING, 356 357 // No error. Used as bound while iterating. 358 QUIC_STREAM_LAST_ERROR, 359}; 360 361// Because receiving an unknown QuicRstStreamErrorCode results in connection 362// teardown, we use this to make sure any errors predating a given version are 363// downgraded to the most appropriate existing error. 364NET_EXPORT_PRIVATE QuicRstStreamErrorCode AdjustErrorForVersion( 365 QuicRstStreamErrorCode error_code, 366 QuicVersion version); 367 368// These values must remain stable as they are uploaded to UMA histograms. 369// To add a new error code, use the current value of QUIC_LAST_ERROR and 370// increment QUIC_LAST_ERROR. 371enum QuicErrorCode { 372 QUIC_NO_ERROR = 0, 373 374 // Connection has reached an invalid state. 375 QUIC_INTERNAL_ERROR = 1, 376 // There were data frames after the a fin or reset. 377 QUIC_STREAM_DATA_AFTER_TERMINATION = 2, 378 // Control frame is malformed. 379 QUIC_INVALID_PACKET_HEADER = 3, 380 // Frame data is malformed. 381 QUIC_INVALID_FRAME_DATA = 4, 382 // The packet contained no payload. 383 QUIC_MISSING_PAYLOAD = 48, 384 // FEC data is malformed. 385 QUIC_INVALID_FEC_DATA = 5, 386 // STREAM frame data is malformed. 387 QUIC_INVALID_STREAM_DATA = 46, 388 // STREAM frame data is not encrypted. 389 QUIC_UNENCRYPTED_STREAM_DATA = 61, 390 // RST_STREAM frame data is malformed. 391 QUIC_INVALID_RST_STREAM_DATA = 6, 392 // CONNECTION_CLOSE frame data is malformed. 393 QUIC_INVALID_CONNECTION_CLOSE_DATA = 7, 394 // GOAWAY frame data is malformed. 395 QUIC_INVALID_GOAWAY_DATA = 8, 396 // WINDOW_UPDATE frame data is malformed. 397 QUIC_INVALID_WINDOW_UPDATE_DATA = 57, 398 // BLOCKED frame data is malformed. 399 QUIC_INVALID_BLOCKED_DATA = 58, 400 // STOP_WAITING frame data is malformed. 401 QUIC_INVALID_STOP_WAITING_DATA = 60, 402 // ACK frame data is malformed. 403 QUIC_INVALID_ACK_DATA = 9, 404 // CONGESTION_FEEDBACK frame data is malformed. 405 QUIC_INVALID_CONGESTION_FEEDBACK_DATA = 47, 406 // Version negotiation packet is malformed. 407 QUIC_INVALID_VERSION_NEGOTIATION_PACKET = 10, 408 // Public RST packet is malformed. 409 QUIC_INVALID_PUBLIC_RST_PACKET = 11, 410 // There was an error decrypting. 411 QUIC_DECRYPTION_FAILURE = 12, 412 // There was an error encrypting. 413 QUIC_ENCRYPTION_FAILURE = 13, 414 // The packet exceeded kMaxPacketSize. 415 QUIC_PACKET_TOO_LARGE = 14, 416 // Data was sent for a stream which did not exist. 417 QUIC_PACKET_FOR_NONEXISTENT_STREAM = 15, 418 // The peer is going away. May be a client or server. 419 QUIC_PEER_GOING_AWAY = 16, 420 // A stream ID was invalid. 421 QUIC_INVALID_STREAM_ID = 17, 422 // A priority was invalid. 423 QUIC_INVALID_PRIORITY = 49, 424 // Too many streams already open. 425 QUIC_TOO_MANY_OPEN_STREAMS = 18, 426 // Received public reset for this connection. 427 QUIC_PUBLIC_RESET = 19, 428 // Invalid protocol version. 429 QUIC_INVALID_VERSION = 20, 430 431 // deprecated: QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED = 21 432 433 // The Header ID for a stream was too far from the previous. 434 QUIC_INVALID_HEADER_ID = 22, 435 // Negotiable parameter received during handshake had invalid value. 436 QUIC_INVALID_NEGOTIATED_VALUE = 23, 437 // There was an error decompressing data. 438 QUIC_DECOMPRESSION_FAILURE = 24, 439 // We hit our prenegotiated (or default) timeout 440 QUIC_CONNECTION_TIMED_OUT = 25, 441 // There was an error encountered migrating addresses 442 QUIC_ERROR_MIGRATING_ADDRESS = 26, 443 // There was an error while writing to the socket. 444 QUIC_PACKET_WRITE_ERROR = 27, 445 // There was an error while reading from the socket. 446 QUIC_PACKET_READ_ERROR = 51, 447 // We received a STREAM_FRAME with no data and no fin flag set. 448 QUIC_INVALID_STREAM_FRAME = 50, 449 // We received invalid data on the headers stream. 450 QUIC_INVALID_HEADERS_STREAM_DATA = 56, 451 // The peer violated the flow control protocol. 452 QUIC_FLOW_CONTROL_ERROR = 59, 453 454 // Crypto errors. 455 456 // Hanshake failed. 457 QUIC_HANDSHAKE_FAILED = 28, 458 // Handshake message contained out of order tags. 459 QUIC_CRYPTO_TAGS_OUT_OF_ORDER = 29, 460 // Handshake message contained too many entries. 461 QUIC_CRYPTO_TOO_MANY_ENTRIES = 30, 462 // Handshake message contained an invalid value length. 463 QUIC_CRYPTO_INVALID_VALUE_LENGTH = 31, 464 // A crypto message was received after the handshake was complete. 465 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE = 32, 466 // A crypto message was received with an illegal message tag. 467 QUIC_INVALID_CRYPTO_MESSAGE_TYPE = 33, 468 // A crypto message was received with an illegal parameter. 469 QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER = 34, 470 // An invalid channel id signature was supplied. 471 QUIC_INVALID_CHANNEL_ID_SIGNATURE = 52, 472 // A crypto message was received with a mandatory parameter missing. 473 QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND = 35, 474 // A crypto message was received with a parameter that has no overlap 475 // with the local parameter. 476 QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP = 36, 477 // A crypto message was received that contained a parameter with too few 478 // values. 479 QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND = 37, 480 // An internal error occured in crypto processing. 481 QUIC_CRYPTO_INTERNAL_ERROR = 38, 482 // A crypto handshake message specified an unsupported version. 483 QUIC_CRYPTO_VERSION_NOT_SUPPORTED = 39, 484 // There was no intersection between the crypto primitives supported by the 485 // peer and ourselves. 486 QUIC_CRYPTO_NO_SUPPORT = 40, 487 // The server rejected our client hello messages too many times. 488 QUIC_CRYPTO_TOO_MANY_REJECTS = 41, 489 // The client rejected the server's certificate chain or signature. 490 QUIC_PROOF_INVALID = 42, 491 // A crypto message was received with a duplicate tag. 492 QUIC_CRYPTO_DUPLICATE_TAG = 43, 493 // A crypto message was received with the wrong encryption level (i.e. it 494 // should have been encrypted but was not.) 495 QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT = 44, 496 // The server config for a server has expired. 497 QUIC_CRYPTO_SERVER_CONFIG_EXPIRED = 45, 498 // We failed to setup the symmetric keys for a connection. 499 QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED = 53, 500 // A handshake message arrived, but we are still validating the 501 // previous handshake message. 502 QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO = 54, 503 // This connection involved a version negotiation which appears to have been 504 // tampered with. 505 QUIC_VERSION_NEGOTIATION_MISMATCH = 55, 506 507 // No error. Used as bound while iterating. 508 QUIC_LAST_ERROR = 62, 509}; 510 511struct NET_EXPORT_PRIVATE QuicPacketPublicHeader { 512 QuicPacketPublicHeader(); 513 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader& other); 514 ~QuicPacketPublicHeader(); 515 516 // Universal header. All QuicPacket headers will have a connection_id and 517 // public flags. 518 QuicConnectionId connection_id; 519 QuicConnectionIdLength connection_id_length; 520 bool reset_flag; 521 bool version_flag; 522 QuicSequenceNumberLength sequence_number_length; 523 QuicVersionVector versions; 524}; 525 526// Header for Data or FEC packets. 527struct NET_EXPORT_PRIVATE QuicPacketHeader { 528 QuicPacketHeader(); 529 explicit QuicPacketHeader(const QuicPacketPublicHeader& header); 530 531 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 532 std::ostream& os, const QuicPacketHeader& s); 533 534 QuicPacketPublicHeader public_header; 535 bool fec_flag; 536 bool entropy_flag; 537 QuicPacketEntropyHash entropy_hash; 538 QuicPacketSequenceNumber packet_sequence_number; 539 InFecGroup is_in_fec_group; 540 QuicFecGroupNumber fec_group; 541}; 542 543struct NET_EXPORT_PRIVATE QuicPublicResetPacket { 544 QuicPublicResetPacket(); 545 explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header); 546 547 QuicPacketPublicHeader public_header; 548 QuicPublicResetNonceProof nonce_proof; 549 QuicPacketSequenceNumber rejected_sequence_number; 550 IPEndPoint client_address; 551}; 552 553enum QuicVersionNegotiationState { 554 START_NEGOTIATION = 0, 555 // Server-side this implies we've sent a version negotiation packet and are 556 // waiting on the client to select a compatible version. Client-side this 557 // implies we've gotten a version negotiation packet, are retransmitting the 558 // initial packets with a supported version and are waiting for our first 559 // packet from the server. 560 NEGOTIATION_IN_PROGRESS, 561 // This indicates this endpoint has received a packet from the peer with a 562 // version this endpoint supports. Version negotiation is complete, and the 563 // version number will no longer be sent with future packets. 564 NEGOTIATED_VERSION 565}; 566 567typedef QuicPacketPublicHeader QuicVersionNegotiationPacket; 568 569// A padding frame contains no payload. 570struct NET_EXPORT_PRIVATE QuicPaddingFrame { 571}; 572 573// A ping frame contains no payload, though it is retransmittable, 574// and ACK'd just like other normal frames. 575struct NET_EXPORT_PRIVATE QuicPingFrame { 576}; 577 578struct NET_EXPORT_PRIVATE QuicStreamFrame { 579 QuicStreamFrame(); 580 QuicStreamFrame(const QuicStreamFrame& frame); 581 QuicStreamFrame(QuicStreamId stream_id, 582 bool fin, 583 QuicStreamOffset offset, 584 IOVector data); 585 586 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 587 std::ostream& os, const QuicStreamFrame& s); 588 589 // Returns a copy of the IOVector |data| as a heap-allocated string. 590 // Caller must take ownership of the returned string. 591 std::string* GetDataAsString() const; 592 593 QuicStreamId stream_id; 594 bool fin; 595 QuicStreamOffset offset; // Location of this data in the stream. 596 IOVector data; 597 598 // If this is set, then when this packet is ACKed the AckNotifier will be 599 // informed. 600 QuicAckNotifier* notifier; 601}; 602 603// TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing 604// is finalized. 605typedef std::set<QuicPacketSequenceNumber> SequenceNumberSet; 606// TODO(pwestin): Add a way to enforce the max size of this map. 607typedef std::map<QuicPacketSequenceNumber, QuicTime> TimeMap; 608 609struct NET_EXPORT_PRIVATE ReceivedPacketInfo { 610 ReceivedPacketInfo(); 611 ~ReceivedPacketInfo(); 612 613 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 614 std::ostream& os, const ReceivedPacketInfo& s); 615 616 // Entropy hash of all packets up to largest observed not including missing 617 // packets. 618 QuicPacketEntropyHash entropy_hash; 619 620 // The highest packet sequence number we've observed from the peer. 621 // 622 // In general, this should be the largest packet number we've received. In 623 // the case of truncated acks, we may have to advertise a lower "upper bound" 624 // than largest received, to avoid implicitly acking missing packets that 625 // don't fit in the missing packet list due to size limitations. In this 626 // case, largest_observed may be a packet which is also in the missing packets 627 // list. 628 QuicPacketSequenceNumber largest_observed; 629 630 // Time elapsed since largest_observed was received until this Ack frame was 631 // sent. 632 QuicTime::Delta delta_time_largest_observed; 633 634 // TODO(satyamshekhar): Can be optimized using an interval set like data 635 // structure. 636 // The set of packets which we're expecting and have not received. 637 SequenceNumberSet missing_packets; 638 639 // Whether the ack had to be truncated when sent. 640 bool is_truncated; 641 642 // Packets which have been revived via FEC. 643 // All of these must also be in missing_packets. 644 SequenceNumberSet revived_packets; 645}; 646 647// True if the sequence number is greater than largest_observed or is listed 648// as missing. 649// Always returns false for sequence numbers less than least_unacked. 650bool NET_EXPORT_PRIVATE IsAwaitingPacket( 651 const ReceivedPacketInfo& received_info, 652 QuicPacketSequenceNumber sequence_number); 653 654// Inserts missing packets between [lower, higher). 655void NET_EXPORT_PRIVATE InsertMissingPacketsBetween( 656 ReceivedPacketInfo* received_info, 657 QuicPacketSequenceNumber lower, 658 QuicPacketSequenceNumber higher); 659 660struct NET_EXPORT_PRIVATE QuicStopWaitingFrame { 661 QuicStopWaitingFrame(); 662 ~QuicStopWaitingFrame(); 663 664 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 665 std::ostream& os, const QuicStopWaitingFrame& s); 666 667 // Entropy hash of all packets up to, but not including, the least unacked 668 // packet. 669 QuicPacketEntropyHash entropy_hash; 670 // The lowest packet we've sent which is unacked, and we expect an ack for. 671 QuicPacketSequenceNumber least_unacked; 672}; 673 674struct NET_EXPORT_PRIVATE QuicAckFrame { 675 QuicAckFrame(); 676 677 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 678 std::ostream& os, const QuicAckFrame& s); 679 680 QuicStopWaitingFrame sent_info; 681 ReceivedPacketInfo received_info; 682}; 683 684// Defines for all types of congestion feedback that will be negotiated in QUIC, 685// kTCP MUST be supported by all QUIC implementations to guarantee 100% 686// compatibility. 687enum CongestionFeedbackType { 688 kTCP, // Used to mimic TCP. 689 kInterArrival, // Use additional inter arrival information. 690 kFixRate, // Provided for testing. 691}; 692 693enum LossDetectionType { 694 kNack, // Used to mimic TCP's loss detection. 695 kTime, // Time based loss detection. 696}; 697 698struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP { 699 CongestionFeedbackMessageTCP(); 700 701 QuicByteCount receive_window; 702}; 703 704struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival { 705 CongestionFeedbackMessageInterArrival(); 706 ~CongestionFeedbackMessageInterArrival(); 707 708 // The set of received packets since the last feedback was sent, along with 709 // their arrival times. 710 TimeMap received_packet_times; 711}; 712 713struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate { 714 CongestionFeedbackMessageFixRate(); 715 QuicBandwidth bitrate; 716}; 717 718struct NET_EXPORT_PRIVATE QuicCongestionFeedbackFrame { 719 QuicCongestionFeedbackFrame(); 720 ~QuicCongestionFeedbackFrame(); 721 722 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 723 std::ostream& os, const QuicCongestionFeedbackFrame& c); 724 725 CongestionFeedbackType type; 726 // This should really be a union, but since the inter arrival struct 727 // is non-trivial, C++ prohibits it. 728 CongestionFeedbackMessageTCP tcp; 729 CongestionFeedbackMessageInterArrival inter_arrival; 730 CongestionFeedbackMessageFixRate fix_rate; 731}; 732 733struct NET_EXPORT_PRIVATE QuicRstStreamFrame { 734 QuicRstStreamFrame(); 735 QuicRstStreamFrame(QuicStreamId stream_id, 736 QuicRstStreamErrorCode error_code, 737 QuicStreamOffset bytes_written); 738 739 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 740 std::ostream& os, const QuicRstStreamFrame& r); 741 742 QuicStreamId stream_id; 743 QuicRstStreamErrorCode error_code; 744 std::string error_details; 745 746 // Used to update flow control windows. On termination of a stream, both 747 // endpoints must inform the peer of the number of bytes they have sent on 748 // that stream. This can be done through normal termination (data packet with 749 // FIN) or through a RST. 750 QuicStreamOffset byte_offset; 751}; 752 753struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame { 754 QuicConnectionCloseFrame(); 755 756 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 757 std::ostream& os, const QuicConnectionCloseFrame& c); 758 759 QuicErrorCode error_code; 760 std::string error_details; 761}; 762 763struct NET_EXPORT_PRIVATE QuicGoAwayFrame { 764 QuicGoAwayFrame(); 765 QuicGoAwayFrame(QuicErrorCode error_code, 766 QuicStreamId last_good_stream_id, 767 const std::string& reason); 768 769 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 770 std::ostream& os, const QuicGoAwayFrame& g); 771 772 QuicErrorCode error_code; 773 QuicStreamId last_good_stream_id; 774 std::string reason_phrase; 775}; 776 777// Flow control updates per-stream and at the connection levoel. 778// Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather 779// than a window delta. 780// TODO(rjshade): A possible future optimization is to make stream_id and 781// byte_offset variable length, similar to stream frames. 782struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame { 783 QuicWindowUpdateFrame() {} 784 QuicWindowUpdateFrame(QuicStreamId stream_id, QuicStreamOffset byte_offset); 785 786 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 787 std::ostream& os, const QuicWindowUpdateFrame& w); 788 789 // The stream this frame applies to. 0 is a special case meaning the overall 790 // connection rather than a specific stream. 791 QuicStreamId stream_id; 792 793 // Byte offset in the stream or connection. The receiver of this frame must 794 // not send data which would result in this offset being exceeded. 795 QuicStreamOffset byte_offset; 796}; 797 798// The BLOCKED frame is used to indicate to the remote endpoint that this 799// endpoint believes itself to be flow-control blocked but otherwise ready to 800// send data. The BLOCKED frame is purely advisory and optional. 801// Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28). 802struct NET_EXPORT_PRIVATE QuicBlockedFrame { 803 QuicBlockedFrame() {} 804 explicit QuicBlockedFrame(QuicStreamId stream_id); 805 806 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 807 std::ostream& os, const QuicBlockedFrame& b); 808 809 // The stream this frame applies to. 0 is a special case meaning the overall 810 // connection rather than a specific stream. 811 QuicStreamId stream_id; 812}; 813 814// EncryptionLevel enumerates the stages of encryption that a QUIC connection 815// progresses through. When retransmitting a packet, the encryption level needs 816// to be specified so that it is retransmitted at a level which the peer can 817// understand. 818enum EncryptionLevel { 819 ENCRYPTION_NONE = 0, 820 ENCRYPTION_INITIAL = 1, 821 ENCRYPTION_FORWARD_SECURE = 2, 822 823 NUM_ENCRYPTION_LEVELS, 824}; 825 826struct NET_EXPORT_PRIVATE QuicFrame { 827 QuicFrame(); 828 explicit QuicFrame(QuicPaddingFrame* padding_frame); 829 explicit QuicFrame(QuicStreamFrame* stream_frame); 830 explicit QuicFrame(QuicAckFrame* frame); 831 explicit QuicFrame(QuicCongestionFeedbackFrame* frame); 832 explicit QuicFrame(QuicRstStreamFrame* frame); 833 explicit QuicFrame(QuicConnectionCloseFrame* frame); 834 explicit QuicFrame(QuicStopWaitingFrame* frame); 835 explicit QuicFrame(QuicPingFrame* frame); 836 explicit QuicFrame(QuicGoAwayFrame* frame); 837 explicit QuicFrame(QuicWindowUpdateFrame* frame); 838 explicit QuicFrame(QuicBlockedFrame* frame); 839 840 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 841 std::ostream& os, const QuicFrame& frame); 842 843 QuicFrameType type; 844 union { 845 QuicPaddingFrame* padding_frame; 846 QuicStreamFrame* stream_frame; 847 QuicAckFrame* ack_frame; 848 QuicCongestionFeedbackFrame* congestion_feedback_frame; 849 QuicStopWaitingFrame* stop_waiting_frame; 850 QuicPingFrame* ping_frame; 851 QuicRstStreamFrame* rst_stream_frame; 852 QuicConnectionCloseFrame* connection_close_frame; 853 QuicGoAwayFrame* goaway_frame; 854 QuicWindowUpdateFrame* window_update_frame; 855 QuicBlockedFrame* blocked_frame; 856 }; 857}; 858 859typedef std::vector<QuicFrame> QuicFrames; 860 861struct NET_EXPORT_PRIVATE QuicFecData { 862 QuicFecData(); 863 864 // The FEC group number is also the sequence number of the first 865 // FEC protected packet. The last protected packet's sequence number will 866 // be one less than the sequence number of the FEC packet. 867 QuicFecGroupNumber fec_group; 868 base::StringPiece redundancy; 869}; 870 871class NET_EXPORT_PRIVATE QuicData { 872 public: 873 QuicData(const char* buffer, size_t length); 874 QuicData(char* buffer, size_t length, bool owns_buffer); 875 virtual ~QuicData(); 876 877 base::StringPiece AsStringPiece() const { 878 return base::StringPiece(data(), length()); 879 } 880 881 const char* data() const { return buffer_; } 882 size_t length() const { return length_; } 883 884 private: 885 const char* buffer_; 886 size_t length_; 887 bool owns_buffer_; 888 889 DISALLOW_COPY_AND_ASSIGN(QuicData); 890}; 891 892class NET_EXPORT_PRIVATE QuicPacket : public QuicData { 893 public: 894 static QuicPacket* NewDataPacket( 895 char* buffer, 896 size_t length, 897 bool owns_buffer, 898 QuicConnectionIdLength connection_id_length, 899 bool includes_version, 900 QuicSequenceNumberLength sequence_number_length) { 901 return new QuicPacket(buffer, length, owns_buffer, connection_id_length, 902 includes_version, sequence_number_length, false); 903 } 904 905 static QuicPacket* NewFecPacket( 906 char* buffer, 907 size_t length, 908 bool owns_buffer, 909 QuicConnectionIdLength connection_id_length, 910 bool includes_version, 911 QuicSequenceNumberLength sequence_number_length) { 912 return new QuicPacket(buffer, length, owns_buffer, connection_id_length, 913 includes_version, sequence_number_length, true); 914 } 915 916 base::StringPiece FecProtectedData() const; 917 base::StringPiece AssociatedData() const; 918 base::StringPiece BeforePlaintext() const; 919 base::StringPiece Plaintext() const; 920 921 bool is_fec_packet() const { return is_fec_packet_; } 922 923 char* mutable_data() { return buffer_; } 924 925 private: 926 QuicPacket(char* buffer, 927 size_t length, 928 bool owns_buffer, 929 QuicConnectionIdLength connection_id_length, 930 bool includes_version, 931 QuicSequenceNumberLength sequence_number_length, 932 bool is_fec_packet); 933 934 char* buffer_; 935 const bool is_fec_packet_; 936 const QuicConnectionIdLength connection_id_length_; 937 const bool includes_version_; 938 const QuicSequenceNumberLength sequence_number_length_; 939 940 DISALLOW_COPY_AND_ASSIGN(QuicPacket); 941}; 942 943class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData { 944 public: 945 QuicEncryptedPacket(const char* buffer, size_t length); 946 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer); 947 948 // Clones the packet into a new packet which owns the buffer. 949 QuicEncryptedPacket* Clone() const; 950 951 // By default, gtest prints the raw bytes of an object. The bool data 952 // member (in the base class QuicData) causes this object to have padding 953 // bytes, which causes the default gtest object printer to read 954 // uninitialize memory. So we need to teach gtest how to print this object. 955 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 956 std::ostream& os, const QuicEncryptedPacket& s); 957 958 private: 959 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket); 960}; 961 962class NET_EXPORT_PRIVATE RetransmittableFrames { 963 public: 964 RetransmittableFrames(); 965 ~RetransmittableFrames(); 966 967 // Allocates a local copy of the referenced StringPiece has QuicStreamFrame 968 // use it. 969 // Takes ownership of |stream_frame|. 970 const QuicFrame& AddStreamFrame(QuicStreamFrame* stream_frame); 971 // Takes ownership of the frame inside |frame|. 972 const QuicFrame& AddNonStreamFrame(const QuicFrame& frame); 973 const QuicFrames& frames() const { return frames_; } 974 975 IsHandshake HasCryptoHandshake() const; 976 977 void set_encryption_level(EncryptionLevel level); 978 EncryptionLevel encryption_level() const { 979 return encryption_level_; 980 } 981 982 private: 983 QuicFrames frames_; 984 EncryptionLevel encryption_level_; 985 // Data referenced by the StringPiece of a QuicStreamFrame. 986 std::vector<std::string*> stream_data_; 987 988 DISALLOW_COPY_AND_ASSIGN(RetransmittableFrames); 989}; 990 991struct NET_EXPORT_PRIVATE SerializedPacket { 992 SerializedPacket(QuicPacketSequenceNumber sequence_number, 993 QuicSequenceNumberLength sequence_number_length, 994 QuicPacket* packet, 995 QuicPacketEntropyHash entropy_hash, 996 RetransmittableFrames* retransmittable_frames); 997 ~SerializedPacket(); 998 999 QuicPacketSequenceNumber sequence_number; 1000 QuicSequenceNumberLength sequence_number_length; 1001 QuicPacket* packet; 1002 QuicPacketEntropyHash entropy_hash; 1003 RetransmittableFrames* retransmittable_frames; 1004 1005 // If set, these will be called when this packet is ACKed by the peer. 1006 std::set<QuicAckNotifier*> notifiers; 1007}; 1008 1009struct NET_EXPORT_PRIVATE TransmissionInfo { 1010 // Used by STL when assigning into a map. 1011 TransmissionInfo(); 1012 1013 // Constructs a Transmission with a new all_tranmissions set 1014 // containing |sequence_number|. 1015 TransmissionInfo(RetransmittableFrames* retransmittable_frames, 1016 QuicPacketSequenceNumber sequence_number, 1017 QuicSequenceNumberLength sequence_number_length); 1018 1019 // Constructs a Transmission with the specified |all_tranmissions| set 1020 // and inserts |sequence_number| into it. 1021 TransmissionInfo(RetransmittableFrames* retransmittable_frames, 1022 QuicPacketSequenceNumber sequence_number, 1023 QuicSequenceNumberLength sequence_number_length, 1024 SequenceNumberSet* all_transmissions); 1025 1026 RetransmittableFrames* retransmittable_frames; 1027 QuicSequenceNumberLength sequence_number_length; 1028 // Zero when the packet is serialized, non-zero once it's sent. 1029 QuicTime sent_time; 1030 // Zero when the packet is serialized, non-zero once it's sent. 1031 QuicByteCount bytes_sent; 1032 size_t nack_count; 1033 // Stores the sequence numbers of all transmissions of this packet. 1034 // Can never be null. 1035 SequenceNumberSet* all_transmissions; 1036 // Pending packets have not been abandoned or lost. 1037 bool pending; 1038}; 1039 1040// A struct for functions which consume data payloads and fins. 1041struct NET_EXPORT_PRIVATE QuicConsumedData { 1042 QuicConsumedData(size_t bytes_consumed, bool fin_consumed); 1043 1044 // By default, gtest prints the raw bytes of an object. The bool data 1045 // member causes this object to have padding bytes, which causes the 1046 // default gtest object printer to read uninitialize memory. So we need 1047 // to teach gtest how to print this object. 1048 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 1049 std::ostream& os, const QuicConsumedData& s); 1050 1051 // How many bytes were consumed. 1052 size_t bytes_consumed; 1053 1054 // True if an incoming fin was consumed. 1055 bool fin_consumed; 1056}; 1057 1058enum WriteStatus { 1059 WRITE_STATUS_OK, 1060 WRITE_STATUS_BLOCKED, 1061 WRITE_STATUS_ERROR, 1062}; 1063 1064// A struct used to return the result of write calls including either the number 1065// of bytes written or the error code, depending upon the status. 1066struct NET_EXPORT_PRIVATE WriteResult { 1067 WriteResult(WriteStatus status, int bytes_written_or_error_code); 1068 WriteResult(); 1069 1070 WriteStatus status; 1071 union { 1072 int bytes_written; // only valid when status is OK 1073 int error_code; // only valid when status is ERROR 1074 }; 1075}; 1076 1077} // namespace net 1078 1079#endif // NET_QUIC_QUIC_PROTOCOL_H_ 1080