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