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