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