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