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