Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / quic_protocol.h
blobb6aaee3d5661ebf8a1ff4bffa4b44ec91caf8dd2
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 <stdint.h>
10 #include <limits>
11 #include <list>
12 #include <map>
13 #include <ostream>
14 #include <set>
15 #include <string>
16 #include <utility>
17 #include <vector>
19 #include "base/basictypes.h"
20 #include "base/containers/hash_tables.h"
21 #include "base/logging.h"
22 #include "base/strings/string_piece.h"
23 #include "net/base/int128.h"
24 #include "net/base/iovec.h"
25 #include "net/base/ip_endpoint.h"
26 #include "net/base/net_export.h"
27 #include "net/quic/quic_bandwidth.h"
28 #include "net/quic/quic_time.h"
29 #include "net/quic/quic_types.h"
31 namespace net {
33 class QuicAckNotifier;
34 class QuicPacket;
35 struct QuicPacketHeader;
37 typedef uint64 QuicConnectionId;
38 typedef uint32 QuicStreamId;
39 typedef uint64 QuicStreamOffset;
40 typedef uint64 QuicPacketNumber;
41 typedef QuicPacketNumber QuicFecGroupNumber;
42 typedef uint64 QuicPublicResetNonceProof;
43 typedef uint8 QuicPacketEntropyHash;
44 typedef uint32 QuicHeaderId;
45 // QuicTag is the type of a tag in the wire protocol.
46 typedef uint32 QuicTag;
47 typedef std::vector<QuicTag> QuicTagVector;
48 typedef std::map<QuicTag, std::string> QuicTagValueMap;
49 // TODO(rtenneti): Didn't use SpdyPriority because SpdyPriority is uint8 and
50 // QuicPriority is uint32. Use SpdyPriority when we change the QUIC_VERSION.
51 typedef uint32 QuicPriority;
53 // Default initial maximum size in bytes of a QUIC packet.
54 const QuicByteCount kDefaultMaxPacketSize = 1350;
55 // Default initial maximum size in bytes of a QUIC packet for servers.
56 const QuicByteCount kDefaultServerMaxPacketSize = 1000;
57 // The maximum packet size of any QUIC packet, based on ethernet's max size,
58 // minus the IP and UDP headers. IPv6 has a 40 byte header, UPD adds an
59 // additional 8 bytes. This is a total overhead of 48 bytes. Ethernet's
60 // max packet size is 1500 bytes, 1500 - 48 = 1452.
61 const QuicByteCount kMaxPacketSize = 1452;
62 // Default maximum packet size used in the Linux TCP implementation.
63 // Used in QUIC for congestion window computations in bytes.
64 const QuicByteCount kDefaultTCPMSS = 1460;
66 // We match SPDY's use of 32 when secure (since we'd compete with SPDY).
67 const QuicPacketCount kInitialCongestionWindowSecure = 32;
68 // Be conservative, and just use double a typical TCP ICWND for HTTP.
69 const QuicPacketCount kInitialCongestionWindowInsecure = 20;
71 // Minimum size of initial flow control window, for both stream and session.
72 const uint32 kMinimumFlowControlSendWindow = 16 * 1024; // 16 KB
74 // Minimum size of the CWND, in packets, when doing bandwidth resumption.
75 const QuicPacketCount kMinCongestionWindowForBandwidthResumption = 10;
77 // Maximum size of the CWND, in packets.
78 const QuicPacketCount kMaxCongestionWindow = 200;
80 // Maximum number of tracked packets.
81 const QuicPacketCount kMaxTrackedPackets = 5000;
83 // Default size of the socket receive buffer in bytes.
84 const QuicByteCount kDefaultSocketReceiveBuffer = 256 * 1024;
85 // Minimum size of the socket receive buffer in bytes.
86 // Smaller values are ignored.
87 const QuicByteCount kMinSocketReceiveBuffer = 16 * 1024;
89 // Fraction of the receive buffer that can be used for encrypted bytes.
90 // Allows a 5% overhead for IP and UDP framing, as well as ack only packets.
91 static const float kUsableRecieveBufferFraction = 0.95f;
92 // Fraction of the receive buffer that can be used, based on conservative
93 // estimates and testing on Linux.
94 // An alternative to kUsableRecieveBufferFraction.
95 static const float kConservativeReceiveBufferFraction = 0.6f;
97 // Don't allow a client to suggest an RTT shorter than 10ms.
98 const uint32 kMinInitialRoundTripTimeUs = 10 * kNumMicrosPerMilli;
100 // Don't allow a client to suggest an RTT longer than 15 seconds.
101 const uint32 kMaxInitialRoundTripTimeUs = 15 * kNumMicrosPerSecond;
103 // Maximum number of open streams per connection.
104 const size_t kDefaultMaxStreamsPerConnection = 100;
106 // Number of bytes reserved for public flags in the packet header.
107 const size_t kPublicFlagsSize = 1;
108 // Number of bytes reserved for version number in the packet header.
109 const size_t kQuicVersionSize = 4;
110 // Number of bytes reserved for private flags in the packet header.
111 const size_t kPrivateFlagsSize = 1;
112 // Number of bytes reserved for FEC group in the packet header.
113 const size_t kFecGroupSize = 1;
115 // Signifies that the QuicPacket will contain version of the protocol.
116 const bool kIncludeVersion = true;
118 // Index of the first byte in a QUIC packet which is used in hash calculation.
119 const size_t kStartOfHashData = 0;
121 // Reserved ID for the crypto stream.
122 const QuicStreamId kCryptoStreamId = 1;
124 // Reserved ID for the headers stream.
125 const QuicStreamId kHeadersStreamId = 3;
127 // Maximum delayed ack time, in ms.
128 const int64 kMaxDelayedAckTimeMs = 25;
130 // The timeout before the handshake succeeds.
131 const int64 kInitialIdleTimeoutSecs = 5;
132 // The default idle timeout.
133 const int64 kDefaultIdleTimeoutSecs = 30;
134 // The maximum idle timeout that can be negotiated.
135 const int64 kMaximumIdleTimeoutSecs = 60 * 10; // 10 minutes.
136 // The default timeout for a connection until the crypto handshake succeeds.
137 const int64 kMaxTimeForCryptoHandshakeSecs = 10; // 10 secs.
139 // Default limit on the number of undecryptable packets the connection buffers
140 // before the CHLO/SHLO arrive.
141 const size_t kDefaultMaxUndecryptablePackets = 10;
143 // Default ping timeout.
144 const int64 kPingTimeoutSecs = 15; // 15 secs.
146 // Minimum number of RTTs between Server Config Updates (SCUP) sent to client.
147 const int kMinIntervalBetweenServerConfigUpdatesRTTs = 10;
149 // Minimum time between Server Config Updates (SCUP) sent to client.
150 const int kMinIntervalBetweenServerConfigUpdatesMs = 1000;
152 // Minimum number of packets between Server Config Updates (SCUP).
153 const int kMinPacketsBetweenServerConfigUpdates = 100;
155 // The number of open streams that a server will accept is set to be slightly
156 // larger than the negotiated limit. Immediately closing the connection if the
157 // client opens slightly too many streams is not ideal: the client may have sent
158 // a FIN that was lost, and simultaneously opened a new stream. The number of
159 // streams a server accepts is a fixed increment over the negotiated limit, or a
160 // percentage increase, whichever is larger.
161 const float kMaxStreamsMultiplier = 1.1f;
162 const int kMaxStreamsMinimumIncrement = 10;
164 // We define an unsigned 16-bit floating point value, inspired by IEEE floats
165 // (http://en.wikipedia.org/wiki/Half_precision_floating-point_format),
166 // with 5-bit exponent (bias 1), 11-bit mantissa (effective 12 with hidden
167 // bit) and denormals, but without signs, transfinites or fractions. Wire format
168 // 16 bits (little-endian byte order) are split into exponent (high 5) and
169 // mantissa (low 11) and decoded as:
170 // uint64 value;
171 // if (exponent == 0) value = mantissa;
172 // else value = (mantissa | 1 << 11) << (exponent - 1)
173 const int kUFloat16ExponentBits = 5;
174 const int kUFloat16MaxExponent = (1 << kUFloat16ExponentBits) - 2; // 30
175 const int kUFloat16MantissaBits = 16 - kUFloat16ExponentBits; // 11
176 const int kUFloat16MantissaEffectiveBits = kUFloat16MantissaBits + 1; // 12
177 const uint64 kUFloat16MaxValue = // 0x3FFC0000000
178 ((UINT64_C(1) << kUFloat16MantissaEffectiveBits) - 1) <<
179 kUFloat16MaxExponent;
181 enum TransmissionType {
182 NOT_RETRANSMISSION,
183 FIRST_TRANSMISSION_TYPE = NOT_RETRANSMISSION,
184 HANDSHAKE_RETRANSMISSION, // Retransmits due to handshake timeouts.
185 ALL_UNACKED_RETRANSMISSION, // Retransmits all unacked packets.
186 ALL_INITIAL_RETRANSMISSION, // Retransmits all initially encrypted packets.
187 LOSS_RETRANSMISSION, // Retransmits due to loss detection.
188 RTO_RETRANSMISSION, // Retransmits due to retransmit time out.
189 TLP_RETRANSMISSION, // Tail loss probes.
190 LAST_TRANSMISSION_TYPE = TLP_RETRANSMISSION,
193 enum HasRetransmittableData {
194 NO_RETRANSMITTABLE_DATA,
195 HAS_RETRANSMITTABLE_DATA,
198 enum IsHandshake {
199 NOT_HANDSHAKE,
200 IS_HANDSHAKE
203 enum class Perspective { IS_SERVER, IS_CLIENT };
205 NET_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
206 const Perspective& s);
208 // Indicates FEC protection level for data being written.
209 enum FecProtection {
210 MUST_FEC_PROTECT, // Callee must FEC protect this data.
211 MAY_FEC_PROTECT // Callee does not have to but may FEC protect this data.
214 // Indicates FEC policy.
215 enum FecPolicy {
216 FEC_PROTECT_ALWAYS, // All data in the stream should be FEC protected.
217 FEC_PROTECT_OPTIONAL // Data in the stream does not need FEC protection.
220 // Indicates FEC policy about when to send FEC packet.
221 enum FecSendPolicy {
222 // Send FEC packet when FEC group is full or when FEC alarm goes off.
223 FEC_ANY_TRIGGER,
224 // Send FEC packet only when FEC alarm goes off.
225 FEC_ALARM_TRIGGER
228 enum QuicFrameType {
229 // Regular frame types. The values set here cannot change without the
230 // introduction of a new QUIC version.
231 PADDING_FRAME = 0,
232 RST_STREAM_FRAME = 1,
233 CONNECTION_CLOSE_FRAME = 2,
234 GOAWAY_FRAME = 3,
235 WINDOW_UPDATE_FRAME = 4,
236 BLOCKED_FRAME = 5,
237 STOP_WAITING_FRAME = 6,
238 PING_FRAME = 7,
240 // STREAM and ACK frames are special frames. They are encoded differently on
241 // the wire and their values do not need to be stable.
242 STREAM_FRAME,
243 ACK_FRAME,
244 // The path MTU discovery frame is encoded as a PING frame on the wire.
245 MTU_DISCOVERY_FRAME,
246 NUM_FRAME_TYPES
249 enum QuicConnectionIdLength {
250 PACKET_0BYTE_CONNECTION_ID = 0,
251 PACKET_1BYTE_CONNECTION_ID = 1,
252 PACKET_4BYTE_CONNECTION_ID = 4,
253 PACKET_8BYTE_CONNECTION_ID = 8
256 enum InFecGroup {
257 NOT_IN_FEC_GROUP,
258 IN_FEC_GROUP,
261 enum QuicPacketNumberLength {
262 PACKET_1BYTE_PACKET_NUMBER = 1,
263 PACKET_2BYTE_PACKET_NUMBER = 2,
264 PACKET_4BYTE_PACKET_NUMBER = 4,
265 PACKET_6BYTE_PACKET_NUMBER = 6
268 // Used to indicate a QuicSequenceNumberLength using two flag bits.
269 enum QuicPacketNumberLengthFlags {
270 PACKET_FLAGS_1BYTE_PACKET = 0, // 00
271 PACKET_FLAGS_2BYTE_PACKET = 1, // 01
272 PACKET_FLAGS_4BYTE_PACKET = 1 << 1, // 10
273 PACKET_FLAGS_6BYTE_PACKET = 1 << 1 | 1, // 11
276 // The public flags are specified in one byte.
277 enum QuicPacketPublicFlags {
278 PACKET_PUBLIC_FLAGS_NONE = 0,
280 // Bit 0: Does the packet header contains version info?
281 PACKET_PUBLIC_FLAGS_VERSION = 1 << 0,
283 // Bit 1: Is this packet a public reset packet?
284 PACKET_PUBLIC_FLAGS_RST = 1 << 1,
286 // Bits 2 and 3 specify the length of the ConnectionId as follows:
287 // ----00--: 0 bytes
288 // ----01--: 1 byte
289 // ----10--: 4 bytes
290 // ----11--: 8 bytes
291 PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID = 0,
292 PACKET_PUBLIC_FLAGS_1BYTE_CONNECTION_ID = 1 << 2,
293 PACKET_PUBLIC_FLAGS_4BYTE_CONNECTION_ID = 1 << 3,
294 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID = 1 << 3 | 1 << 2,
296 // Bits 4 and 5 describe the packet number length as follows:
297 // --00----: 1 byte
298 // --01----: 2 bytes
299 // --10----: 4 bytes
300 // --11----: 6 bytes
301 PACKET_PUBLIC_FLAGS_1BYTE_PACKET = PACKET_FLAGS_1BYTE_PACKET << 4,
302 PACKET_PUBLIC_FLAGS_2BYTE_PACKET = PACKET_FLAGS_2BYTE_PACKET << 4,
303 PACKET_PUBLIC_FLAGS_4BYTE_PACKET = PACKET_FLAGS_4BYTE_PACKET << 4,
304 PACKET_PUBLIC_FLAGS_6BYTE_PACKET = PACKET_FLAGS_6BYTE_PACKET << 4,
306 // All bits set (bits 6 and 7 are not currently used): 00111111
307 PACKET_PUBLIC_FLAGS_MAX = (1 << 6) - 1
310 // The private flags are specified in one byte.
311 enum QuicPacketPrivateFlags {
312 PACKET_PRIVATE_FLAGS_NONE = 0,
314 // Bit 0: Does this packet contain an entropy bit?
315 PACKET_PRIVATE_FLAGS_ENTROPY = 1 << 0,
317 // Bit 1: Payload is part of an FEC group?
318 PACKET_PRIVATE_FLAGS_FEC_GROUP = 1 << 1,
320 // Bit 2: Payload is FEC as opposed to frames?
321 PACKET_PRIVATE_FLAGS_FEC = 1 << 2,
323 // All bits set (bits 3-7 are not currently used): 00000111
324 PACKET_PRIVATE_FLAGS_MAX = (1 << 3) - 1
327 // The available versions of QUIC. Guaranteed that the integer value of the enum
328 // will match the version number.
329 // When adding a new version to this enum you should add it to
330 // kSupportedQuicVersions (if appropriate), and also add a new case to the
331 // helper methods QuicVersionToQuicTag, QuicTagToQuicVersion, and
332 // QuicVersionToString.
333 enum QuicVersion {
334 // Special case to indicate unknown/unsupported QUIC version.
335 QUIC_VERSION_UNSUPPORTED = 0,
337 QUIC_VERSION_24 = 24, // SPDY/4 header compression.
338 QUIC_VERSION_25 = 25, // SPDY/4 header keys, and removal of error_details
339 // from QuicRstStreamFrame
342 // This vector contains QUIC versions which we currently support.
343 // This should be ordered such that the highest supported version is the first
344 // element, with subsequent elements in descending order (versions can be
345 // skipped as necessary).
347 // IMPORTANT: if you are adding to this list, follow the instructions at
348 // http://sites/quic/adding-and-removing-versions
349 static const QuicVersion kSupportedQuicVersions[] = {QUIC_VERSION_25,
350 QUIC_VERSION_24};
352 typedef std::vector<QuicVersion> QuicVersionVector;
354 // Returns a vector of QUIC versions in kSupportedQuicVersions.
355 NET_EXPORT_PRIVATE QuicVersionVector QuicSupportedVersions();
357 // QuicTag is written to and read from the wire, but we prefer to use
358 // the more readable QuicVersion at other levels.
359 // Helper function which translates from a QuicVersion to a QuicTag. Returns 0
360 // if QuicVersion is unsupported.
361 NET_EXPORT_PRIVATE QuicTag QuicVersionToQuicTag(const QuicVersion version);
363 // Returns appropriate QuicVersion from a QuicTag.
364 // Returns QUIC_VERSION_UNSUPPORTED if version_tag cannot be understood.
365 NET_EXPORT_PRIVATE QuicVersion QuicTagToQuicVersion(const QuicTag version_tag);
367 // Helper function which translates from a QuicVersion to a string.
368 // Returns strings corresponding to enum names (e.g. QUIC_VERSION_6).
369 NET_EXPORT_PRIVATE std::string QuicVersionToString(const QuicVersion version);
371 // Returns comma separated list of string representations of QuicVersion enum
372 // values in the supplied |versions| vector.
373 NET_EXPORT_PRIVATE std::string QuicVersionVectorToString(
374 const QuicVersionVector& versions);
376 // Version and Crypto tags are written to the wire with a big-endian
377 // representation of the name of the tag. For example
378 // the client hello tag (CHLO) will be written as the
379 // following 4 bytes: 'C' 'H' 'L' 'O'. Since it is
380 // stored in memory as a little endian uint32, we need
381 // to reverse the order of the bytes.
383 // MakeQuicTag returns a value given the four bytes. For example:
384 // MakeQuicTag('C', 'H', 'L', 'O');
385 NET_EXPORT_PRIVATE QuicTag MakeQuicTag(char a, char b, char c, char d);
387 // Returns true if the tag vector contains the specified tag.
388 NET_EXPORT_PRIVATE bool ContainsQuicTag(const QuicTagVector& tag_vector,
389 QuicTag tag);
391 // Size in bytes of the data or fec packet header.
392 NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(const QuicPacketHeader& header);
394 NET_EXPORT_PRIVATE size_t
395 GetPacketHeaderSize(QuicConnectionIdLength connection_id_length,
396 bool include_version,
397 QuicPacketNumberLength packet_number_length,
398 InFecGroup is_in_fec_group);
400 // Index of the first byte in a QUIC packet of FEC protected data.
401 NET_EXPORT_PRIVATE size_t
402 GetStartOfFecProtectedData(QuicConnectionIdLength connection_id_length,
403 bool include_version,
404 QuicPacketNumberLength packet_number_length);
405 // Index of the first byte in a QUIC packet of encrypted data.
406 NET_EXPORT_PRIVATE size_t
407 GetStartOfEncryptedData(QuicConnectionIdLength connection_id_length,
408 bool include_version,
409 QuicPacketNumberLength packet_number_length);
411 enum QuicRstStreamErrorCode {
412 QUIC_STREAM_NO_ERROR = 0,
414 // There was some error which halted stream processing.
415 QUIC_ERROR_PROCESSING_STREAM,
416 // We got two fin or reset offsets which did not match.
417 QUIC_MULTIPLE_TERMINATION_OFFSETS,
418 // We got bad payload and can not respond to it at the protocol level.
419 QUIC_BAD_APPLICATION_PAYLOAD,
420 // Stream closed due to connection error. No reset frame is sent when this
421 // happens.
422 QUIC_STREAM_CONNECTION_ERROR,
423 // GoAway frame sent. No more stream can be created.
424 QUIC_STREAM_PEER_GOING_AWAY,
425 // The stream has been cancelled.
426 QUIC_STREAM_CANCELLED,
427 // Closing stream locally, sending a RST to allow for proper flow control
428 // accounting. Sent in response to a RST from the peer.
429 QUIC_RST_ACKNOWLEDGEMENT,
431 // No error. Used as bound while iterating.
432 QUIC_STREAM_LAST_ERROR,
435 // Because receiving an unknown QuicRstStreamErrorCode results in connection
436 // teardown, we use this to make sure any errors predating a given version are
437 // downgraded to the most appropriate existing error.
438 NET_EXPORT_PRIVATE QuicRstStreamErrorCode AdjustErrorForVersion(
439 QuicRstStreamErrorCode error_code,
440 QuicVersion version);
442 // These values must remain stable as they are uploaded to UMA histograms.
443 // To add a new error code, use the current value of QUIC_LAST_ERROR and
444 // increment QUIC_LAST_ERROR.
445 enum QuicErrorCode {
446 QUIC_NO_ERROR = 0,
448 // Connection has reached an invalid state.
449 QUIC_INTERNAL_ERROR = 1,
450 // There were data frames after the a fin or reset.
451 QUIC_STREAM_DATA_AFTER_TERMINATION = 2,
452 // Control frame is malformed.
453 QUIC_INVALID_PACKET_HEADER = 3,
454 // Frame data is malformed.
455 QUIC_INVALID_FRAME_DATA = 4,
456 // The packet contained no payload.
457 QUIC_MISSING_PAYLOAD = 48,
458 // FEC data is malformed.
459 QUIC_INVALID_FEC_DATA = 5,
460 // STREAM frame data is malformed.
461 QUIC_INVALID_STREAM_DATA = 46,
462 // STREAM frame data is not encrypted.
463 QUIC_UNENCRYPTED_STREAM_DATA = 61,
464 // RST_STREAM frame data is malformed.
465 QUIC_INVALID_RST_STREAM_DATA = 6,
466 // CONNECTION_CLOSE frame data is malformed.
467 QUIC_INVALID_CONNECTION_CLOSE_DATA = 7,
468 // GOAWAY frame data is malformed.
469 QUIC_INVALID_GOAWAY_DATA = 8,
470 // WINDOW_UPDATE frame data is malformed.
471 QUIC_INVALID_WINDOW_UPDATE_DATA = 57,
472 // BLOCKED frame data is malformed.
473 QUIC_INVALID_BLOCKED_DATA = 58,
474 // STOP_WAITING frame data is malformed.
475 QUIC_INVALID_STOP_WAITING_DATA = 60,
476 // ACK frame data is malformed.
477 QUIC_INVALID_ACK_DATA = 9,
479 // deprecated: QUIC_INVALID_CONGESTION_FEEDBACK_DATA = 47,
481 // Version negotiation packet is malformed.
482 QUIC_INVALID_VERSION_NEGOTIATION_PACKET = 10,
483 // Public RST packet is malformed.
484 QUIC_INVALID_PUBLIC_RST_PACKET = 11,
485 // There was an error decrypting.
486 QUIC_DECRYPTION_FAILURE = 12,
487 // There was an error encrypting.
488 QUIC_ENCRYPTION_FAILURE = 13,
489 // The packet exceeded kMaxPacketSize.
490 QUIC_PACKET_TOO_LARGE = 14,
491 // Data was sent for a stream which did not exist.
492 QUIC_PACKET_FOR_NONEXISTENT_STREAM = 15,
493 // The peer is going away. May be a client or server.
494 QUIC_PEER_GOING_AWAY = 16,
495 // A stream ID was invalid.
496 QUIC_INVALID_STREAM_ID = 17,
497 // A priority was invalid.
498 QUIC_INVALID_PRIORITY = 49,
499 // Too many streams already open.
500 QUIC_TOO_MANY_OPEN_STREAMS = 18,
501 // The peer must send a FIN/RST for each stream, and has not been doing so.
502 QUIC_TOO_MANY_UNFINISHED_STREAMS = 66,
503 // Received public reset for this connection.
504 QUIC_PUBLIC_RESET = 19,
505 // Invalid protocol version.
506 QUIC_INVALID_VERSION = 20,
508 // deprecated: QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED = 21
510 // The Header ID for a stream was too far from the previous.
511 QUIC_INVALID_HEADER_ID = 22,
512 // Negotiable parameter received during handshake had invalid value.
513 QUIC_INVALID_NEGOTIATED_VALUE = 23,
514 // There was an error decompressing data.
515 QUIC_DECOMPRESSION_FAILURE = 24,
516 // We hit our prenegotiated (or default) timeout
517 QUIC_CONNECTION_TIMED_OUT = 25,
518 // We hit our overall connection timeout
519 QUIC_CONNECTION_OVERALL_TIMED_OUT = 67,
520 // There was an error encountered migrating addresses
521 QUIC_ERROR_MIGRATING_ADDRESS = 26,
522 // There was an error while writing to the socket.
523 QUIC_PACKET_WRITE_ERROR = 27,
524 // There was an error while reading from the socket.
525 QUIC_PACKET_READ_ERROR = 51,
526 // We received a STREAM_FRAME with no data and no fin flag set.
527 QUIC_INVALID_STREAM_FRAME = 50,
528 // We received invalid data on the headers stream.
529 QUIC_INVALID_HEADERS_STREAM_DATA = 56,
530 // The peer received too much data, violating flow control.
531 QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA = 59,
532 // The peer sent too much data, violating flow control.
533 QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA = 63,
534 // The peer received an invalid flow control window.
535 QUIC_FLOW_CONTROL_INVALID_WINDOW = 64,
536 // The connection has been IP pooled into an existing connection.
537 QUIC_CONNECTION_IP_POOLED = 62,
538 // The connection has too many outstanding sent packets.
539 QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS = 68,
540 // The connection has too many outstanding received packets.
541 QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS = 69,
542 // The quic connection job to load server config is cancelled.
543 QUIC_CONNECTION_CANCELLED = 70,
544 // Disabled QUIC because of high packet loss rate.
545 QUIC_BAD_PACKET_LOSS_RATE = 71,
546 // Disabled QUIC because of too many PUBLIC_RESETs post handshake.
547 QUIC_PUBLIC_RESETS_POST_HANDSHAKE = 73,
548 // Disabled QUIC because of too many timeouts with streams open.
549 QUIC_TIMEOUTS_WITH_OPEN_STREAMS = 74,
550 // Closed because we failed to serialize a packet.
551 QUIC_FAILED_TO_SERIALIZE_PACKET = 75,
553 // Crypto errors.
555 // Hanshake failed.
556 QUIC_HANDSHAKE_FAILED = 28,
557 // Handshake message contained out of order tags.
558 QUIC_CRYPTO_TAGS_OUT_OF_ORDER = 29,
559 // Handshake message contained too many entries.
560 QUIC_CRYPTO_TOO_MANY_ENTRIES = 30,
561 // Handshake message contained an invalid value length.
562 QUIC_CRYPTO_INVALID_VALUE_LENGTH = 31,
563 // A crypto message was received after the handshake was complete.
564 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE = 32,
565 // A crypto message was received with an illegal message tag.
566 QUIC_INVALID_CRYPTO_MESSAGE_TYPE = 33,
567 // A crypto message was received with an illegal parameter.
568 QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER = 34,
569 // An invalid channel id signature was supplied.
570 QUIC_INVALID_CHANNEL_ID_SIGNATURE = 52,
571 // A crypto message was received with a mandatory parameter missing.
572 QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND = 35,
573 // A crypto message was received with a parameter that has no overlap
574 // with the local parameter.
575 QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP = 36,
576 // A crypto message was received that contained a parameter with too few
577 // values.
578 QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND = 37,
579 // An internal error occured in crypto processing.
580 QUIC_CRYPTO_INTERNAL_ERROR = 38,
581 // A crypto handshake message specified an unsupported version.
582 QUIC_CRYPTO_VERSION_NOT_SUPPORTED = 39,
583 // A crypto handshake message resulted in a stateless reject.
584 QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT = 72,
585 // There was no intersection between the crypto primitives supported by the
586 // peer and ourselves.
587 QUIC_CRYPTO_NO_SUPPORT = 40,
588 // The server rejected our client hello messages too many times.
589 QUIC_CRYPTO_TOO_MANY_REJECTS = 41,
590 // The client rejected the server's certificate chain or signature.
591 QUIC_PROOF_INVALID = 42,
592 // A crypto message was received with a duplicate tag.
593 QUIC_CRYPTO_DUPLICATE_TAG = 43,
594 // A crypto message was received with the wrong encryption level (i.e. it
595 // should have been encrypted but was not.)
596 QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT = 44,
597 // The server config for a server has expired.
598 QUIC_CRYPTO_SERVER_CONFIG_EXPIRED = 45,
599 // We failed to setup the symmetric keys for a connection.
600 QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED = 53,
601 // A handshake message arrived, but we are still validating the
602 // previous handshake message.
603 QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO = 54,
604 // A server config update arrived before the handshake is complete.
605 QUIC_CRYPTO_UPDATE_BEFORE_HANDSHAKE_COMPLETE = 65,
606 // This connection involved a version negotiation which appears to have been
607 // tampered with.
608 QUIC_VERSION_NEGOTIATION_MISMATCH = 55,
610 // No error. Used as bound while iterating.
611 QUIC_LAST_ERROR = 76,
614 struct NET_EXPORT_PRIVATE QuicPacketPublicHeader {
615 QuicPacketPublicHeader();
616 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader& other);
617 ~QuicPacketPublicHeader();
619 // Universal header. All QuicPacket headers will have a connection_id and
620 // public flags.
621 QuicConnectionId connection_id;
622 QuicConnectionIdLength connection_id_length;
623 bool reset_flag;
624 bool version_flag;
625 QuicPacketNumberLength packet_number_length;
626 QuicVersionVector versions;
629 // An integer which cannot be a packet number.
630 const QuicPacketNumber kInvalidPacketNumber = 0;
632 // Header for Data or FEC packets.
633 struct NET_EXPORT_PRIVATE QuicPacketHeader {
634 QuicPacketHeader();
635 explicit QuicPacketHeader(const QuicPacketPublicHeader& header);
637 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
638 std::ostream& os, const QuicPacketHeader& s);
640 QuicPacketPublicHeader public_header;
641 QuicPacketNumber packet_packet_number;
642 bool fec_flag;
643 bool entropy_flag;
644 QuicPacketEntropyHash entropy_hash;
645 InFecGroup is_in_fec_group;
646 QuicFecGroupNumber fec_group;
649 struct NET_EXPORT_PRIVATE QuicPublicResetPacket {
650 QuicPublicResetPacket();
651 explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header);
653 QuicPacketPublicHeader public_header;
654 QuicPublicResetNonceProof nonce_proof;
655 QuicPacketNumber rejected_packet_number;
656 IPEndPoint client_address;
659 enum QuicVersionNegotiationState {
660 START_NEGOTIATION = 0,
661 // Server-side this implies we've sent a version negotiation packet and are
662 // waiting on the client to select a compatible version. Client-side this
663 // implies we've gotten a version negotiation packet, are retransmitting the
664 // initial packets with a supported version and are waiting for our first
665 // packet from the server.
666 NEGOTIATION_IN_PROGRESS,
667 // This indicates this endpoint has received a packet from the peer with a
668 // version this endpoint supports. Version negotiation is complete, and the
669 // version number will no longer be sent with future packets.
670 NEGOTIATED_VERSION
673 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket;
675 // A padding frame contains no payload.
676 struct NET_EXPORT_PRIVATE QuicPaddingFrame {
679 // A ping frame contains no payload, though it is retransmittable,
680 // and ACK'd just like other normal frames.
681 struct NET_EXPORT_PRIVATE QuicPingFrame {
684 // A path MTU discovery frame contains no payload and is serialized as a ping
685 // frame.
686 struct NET_EXPORT_PRIVATE QuicMtuDiscoveryFrame {};
688 struct NET_EXPORT_PRIVATE QuicStreamFrame {
689 QuicStreamFrame();
690 QuicStreamFrame(const QuicStreamFrame& frame);
691 QuicStreamFrame(QuicStreamId stream_id,
692 bool fin,
693 QuicStreamOffset offset,
694 base::StringPiece data);
696 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
697 std::ostream& os, const QuicStreamFrame& s);
699 QuicStreamId stream_id;
700 bool fin;
701 QuicStreamOffset offset; // Location of this data in the stream.
702 base::StringPiece data;
705 // TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing
706 // is finalized.
707 typedef std::set<QuicPacketNumber> PacketNumberSet;
708 typedef std::list<QuicPacketNumber> PacketNumberList;
710 typedef std::list<std::pair<QuicPacketNumber, QuicTime>> PacketTimeList;
712 struct NET_EXPORT_PRIVATE QuicStopWaitingFrame {
713 QuicStopWaitingFrame();
714 ~QuicStopWaitingFrame();
716 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
717 std::ostream& os, const QuicStopWaitingFrame& s);
718 // Entropy hash of all packets up to, but not including, the least unacked
719 // packet.
720 QuicPacketEntropyHash entropy_hash;
721 // The lowest packet we've sent which is unacked, and we expect an ack for.
722 QuicPacketNumber least_unacked;
725 struct NET_EXPORT_PRIVATE QuicAckFrame {
726 QuicAckFrame();
727 ~QuicAckFrame();
729 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
730 std::ostream& os, const QuicAckFrame& s);
732 // Entropy hash of all packets up to largest observed not including missing
733 // packets.
734 QuicPacketEntropyHash entropy_hash;
736 // The highest packet number we've observed from the peer.
738 // In general, this should be the largest packet number we've received. In
739 // the case of truncated acks, we may have to advertise a lower "upper bound"
740 // than largest received, to avoid implicitly acking missing packets that
741 // don't fit in the missing packet list due to size limitations. In this
742 // case, largest_observed may be a packet which is also in the missing packets
743 // list.
744 QuicPacketNumber largest_observed;
746 // Time elapsed since largest_observed was received until this Ack frame was
747 // sent.
748 QuicTime::Delta delta_time_largest_observed;
750 // TODO(satyamshekhar): Can be optimized using an interval set like data
751 // structure.
752 // The set of packets which we're expecting and have not received.
753 PacketNumberSet missing_packets;
755 // Whether the ack had to be truncated when sent.
756 bool is_truncated;
758 // Packets which have been revived via FEC.
759 // All of these must also be in missing_packets.
760 PacketNumberSet revived_packets;
762 // List of <packet_number, time> for when packets arrived.
763 PacketTimeList received_packet_times;
766 // True if the packet number is greater than largest_observed or is listed
767 // as missing.
768 // Always returns false for packet numbers less than least_unacked.
769 bool NET_EXPORT_PRIVATE IsAwaitingPacket(const QuicAckFrame& ack_frame,
770 QuicPacketNumber packet_number);
772 // Inserts missing packets between [lower, higher).
773 void NET_EXPORT_PRIVATE InsertMissingPacketsBetween(QuicAckFrame* ack_frame,
774 QuicPacketNumber lower,
775 QuicPacketNumber higher);
777 // Defines for all types of congestion control algorithms that can be used in
778 // QUIC. Note that this is separate from the congestion feedback type -
779 // some congestion control algorithms may use the same feedback type
780 // (Reno and Cubic are the classic example for that).
781 enum CongestionControlType {
782 kCubic,
783 kCubicBytes,
784 kReno,
785 kRenoBytes,
786 kBBR,
789 enum LossDetectionType {
790 kNack, // Used to mimic TCP's loss detection.
791 kTime, // Time based loss detection.
794 struct NET_EXPORT_PRIVATE QuicRstStreamFrame {
795 QuicRstStreamFrame();
796 QuicRstStreamFrame(QuicStreamId stream_id,
797 QuicRstStreamErrorCode error_code,
798 QuicStreamOffset bytes_written);
800 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
801 std::ostream& os, const QuicRstStreamFrame& r);
803 QuicStreamId stream_id;
804 QuicRstStreamErrorCode error_code;
805 // Only used in versions <= QUIC_VERSION_24.
806 std::string error_details;
808 // Used to update flow control windows. On termination of a stream, both
809 // endpoints must inform the peer of the number of bytes they have sent on
810 // that stream. This can be done through normal termination (data packet with
811 // FIN) or through a RST.
812 QuicStreamOffset byte_offset;
815 struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame {
816 QuicConnectionCloseFrame();
818 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
819 std::ostream& os, const QuicConnectionCloseFrame& c);
821 QuicErrorCode error_code;
822 std::string error_details;
825 struct NET_EXPORT_PRIVATE QuicGoAwayFrame {
826 QuicGoAwayFrame();
827 QuicGoAwayFrame(QuicErrorCode error_code,
828 QuicStreamId last_good_stream_id,
829 const std::string& reason);
831 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
832 std::ostream& os, const QuicGoAwayFrame& g);
834 QuicErrorCode error_code;
835 QuicStreamId last_good_stream_id;
836 std::string reason_phrase;
839 // Flow control updates per-stream and at the connection levoel.
840 // Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather
841 // than a window delta.
842 // TODO(rjshade): A possible future optimization is to make stream_id and
843 // byte_offset variable length, similar to stream frames.
844 struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame {
845 QuicWindowUpdateFrame() {}
846 QuicWindowUpdateFrame(QuicStreamId stream_id, QuicStreamOffset byte_offset);
848 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
849 std::ostream& os, const QuicWindowUpdateFrame& w);
851 // The stream this frame applies to. 0 is a special case meaning the overall
852 // connection rather than a specific stream.
853 QuicStreamId stream_id;
855 // Byte offset in the stream or connection. The receiver of this frame must
856 // not send data which would result in this offset being exceeded.
857 QuicStreamOffset byte_offset;
860 // The BLOCKED frame is used to indicate to the remote endpoint that this
861 // endpoint believes itself to be flow-control blocked but otherwise ready to
862 // send data. The BLOCKED frame is purely advisory and optional.
863 // Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28).
864 struct NET_EXPORT_PRIVATE QuicBlockedFrame {
865 QuicBlockedFrame() {}
866 explicit QuicBlockedFrame(QuicStreamId stream_id);
868 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
869 std::ostream& os, const QuicBlockedFrame& b);
871 // The stream this frame applies to. 0 is a special case meaning the overall
872 // connection rather than a specific stream.
873 QuicStreamId stream_id;
876 // EncryptionLevel enumerates the stages of encryption that a QUIC connection
877 // progresses through. When retransmitting a packet, the encryption level needs
878 // to be specified so that it is retransmitted at a level which the peer can
879 // understand.
880 enum EncryptionLevel {
881 ENCRYPTION_NONE = 0,
882 ENCRYPTION_INITIAL = 1,
883 ENCRYPTION_FORWARD_SECURE = 2,
885 NUM_ENCRYPTION_LEVELS,
888 struct NET_EXPORT_PRIVATE QuicFrame {
889 QuicFrame();
890 explicit QuicFrame(QuicPaddingFrame* padding_frame);
891 explicit QuicFrame(QuicStreamFrame* stream_frame);
892 explicit QuicFrame(QuicAckFrame* frame);
893 explicit QuicFrame(QuicMtuDiscoveryFrame* frame);
895 explicit QuicFrame(QuicRstStreamFrame* frame);
896 explicit QuicFrame(QuicConnectionCloseFrame* frame);
897 explicit QuicFrame(QuicStopWaitingFrame* frame);
898 explicit QuicFrame(QuicPingFrame* frame);
899 explicit QuicFrame(QuicGoAwayFrame* frame);
900 explicit QuicFrame(QuicWindowUpdateFrame* frame);
901 explicit QuicFrame(QuicBlockedFrame* frame);
903 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
904 std::ostream& os, const QuicFrame& frame);
906 QuicFrameType type;
907 union {
908 QuicPaddingFrame* padding_frame;
909 QuicStreamFrame* stream_frame;
910 QuicAckFrame* ack_frame;
911 QuicMtuDiscoveryFrame* mtu_discovery_frame;
913 QuicStopWaitingFrame* stop_waiting_frame;
915 QuicPingFrame* ping_frame;
916 QuicRstStreamFrame* rst_stream_frame;
917 QuicConnectionCloseFrame* connection_close_frame;
918 QuicGoAwayFrame* goaway_frame;
919 QuicWindowUpdateFrame* window_update_frame;
920 QuicBlockedFrame* blocked_frame;
924 typedef std::vector<QuicFrame> QuicFrames;
926 struct NET_EXPORT_PRIVATE QuicFecData {
927 QuicFecData();
929 // The FEC group number is also the packet number of the first
930 // FEC protected packet. The last protected packet's packet number will
931 // be one less than the packet number of the FEC packet.
932 QuicFecGroupNumber fec_group;
933 base::StringPiece redundancy;
936 class NET_EXPORT_PRIVATE QuicData {
937 public:
938 QuicData(const char* buffer, size_t length);
939 QuicData(char* buffer, size_t length, bool owns_buffer);
940 virtual ~QuicData();
942 base::StringPiece AsStringPiece() const {
943 return base::StringPiece(data(), length());
946 const char* data() const { return buffer_; }
947 size_t length() const { return length_; }
948 bool owns_buffer() const { return owns_buffer_; }
950 private:
951 const char* buffer_;
952 size_t length_;
953 bool owns_buffer_;
955 DISALLOW_COPY_AND_ASSIGN(QuicData);
958 class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
959 public:
960 QuicPacket(char* buffer,
961 size_t length,
962 bool owns_buffer,
963 QuicConnectionIdLength connection_id_length,
964 bool includes_version,
965 QuicPacketNumberLength packet_number_length);
967 base::StringPiece FecProtectedData() const;
968 base::StringPiece AssociatedData() const;
969 base::StringPiece BeforePlaintext() const;
970 base::StringPiece Plaintext() const;
972 char* mutable_data() { return buffer_; }
974 private:
975 char* buffer_;
976 const QuicConnectionIdLength connection_id_length_;
977 const bool includes_version_;
978 const QuicPacketNumberLength packet_number_length_;
980 DISALLOW_COPY_AND_ASSIGN(QuicPacket);
983 class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
984 public:
985 QuicEncryptedPacket(const char* buffer, size_t length);
986 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer);
988 // Clones the packet into a new packet which owns the buffer.
989 QuicEncryptedPacket* Clone() const;
991 // By default, gtest prints the raw bytes of an object. The bool data
992 // member (in the base class QuicData) causes this object to have padding
993 // bytes, which causes the default gtest object printer to read
994 // uninitialize memory. So we need to teach gtest how to print this object.
995 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
996 std::ostream& os, const QuicEncryptedPacket& s);
998 private:
999 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket);
1002 class NET_EXPORT_PRIVATE RetransmittableFrames {
1003 public:
1004 explicit RetransmittableFrames(EncryptionLevel level);
1005 ~RetransmittableFrames();
1007 // Takes ownership of the frame inside |frame|.
1008 const QuicFrame& AddFrame(const QuicFrame& frame);
1009 // Takes ownership of the frame inside |frame| and |buffer|.
1010 const QuicFrame& AddFrame(const QuicFrame& frame, char* buffer);
1011 // Removes all stream frames associated with |stream_id|.
1012 void RemoveFramesForStream(QuicStreamId stream_id);
1014 const QuicFrames& frames() const { return frames_; }
1016 IsHandshake HasCryptoHandshake() const {
1017 return has_crypto_handshake_;
1020 EncryptionLevel encryption_level() const {
1021 return encryption_level_;
1024 bool needs_padding() const { return needs_padding_; }
1026 void set_needs_padding(bool needs_padding) { needs_padding_ = needs_padding; }
1028 private:
1029 QuicFrames frames_;
1030 const EncryptionLevel encryption_level_;
1031 IsHandshake has_crypto_handshake_;
1032 bool needs_padding_;
1033 // Data referenced by the StringPiece of a QuicStreamFrame.
1034 std::vector<const char*> stream_data_;
1036 DISALLOW_COPY_AND_ASSIGN(RetransmittableFrames);
1039 struct NET_EXPORT_PRIVATE SerializedPacket {
1040 SerializedPacket(QuicPacketNumber packet_number,
1041 QuicPacketNumberLength packet_number_length,
1042 QuicEncryptedPacket* packet,
1043 QuicPacketEntropyHash entropy_hash,
1044 RetransmittableFrames* retransmittable_frames,
1045 bool has_ack,
1046 bool has_stop_waiting);
1047 ~SerializedPacket();
1049 QuicEncryptedPacket* packet;
1050 RetransmittableFrames* retransmittable_frames;
1051 QuicPacketNumber packet_number;
1052 QuicPacketNumberLength packet_number_length;
1053 QuicPacketEntropyHash entropy_hash;
1054 bool is_fec_packet;
1055 bool has_ack;
1056 bool has_stop_waiting;
1058 // Optional notifiers which will be informed when this packet has been ACKed.
1059 std::list<QuicAckNotifier*> notifiers;
1062 struct NET_EXPORT_PRIVATE TransmissionInfo {
1063 // Used by STL when assigning into a map.
1064 TransmissionInfo();
1066 // Constructs a Transmission with a new all_tranmissions set
1067 // containing |packet_number|.
1068 TransmissionInfo(RetransmittableFrames* retransmittable_frames,
1069 QuicPacketNumberLength packet_number_length,
1070 TransmissionType transmission_type,
1071 QuicTime sent_time,
1072 QuicByteCount bytes_sent,
1073 bool is_fec_packet);
1075 RetransmittableFrames* retransmittable_frames;
1076 QuicPacketNumberLength packet_number_length;
1077 QuicTime sent_time;
1078 QuicByteCount bytes_sent;
1079 QuicPacketCount nack_count;
1080 // Reason why this packet was transmitted.
1081 TransmissionType transmission_type;
1082 // Stores the packet numbers of all transmissions of this packet.
1083 // Must always be nullptr or have multiple elements.
1084 PacketNumberList* all_transmissions;
1085 // In flight packets have not been abandoned or lost.
1086 bool in_flight;
1087 // True if the packet can never be acked, so it can be removed.
1088 bool is_unackable;
1089 // True if the packet is an FEC packet.
1090 bool is_fec_packet;
1093 // Convenience wrapper to wrap an iovec array and the total length, which must
1094 // be less than or equal to the actual total length of the iovecs.
1095 struct NET_EXPORT_PRIVATE QuicIOVector {
1096 QuicIOVector(const struct iovec* iov, int iov_count, size_t total_length)
1097 : iov(iov), iov_count(iov_count), total_length(total_length) {}
1099 const struct iovec* iov;
1100 const int iov_count;
1101 const size_t total_length;
1104 } // namespace net
1106 #endif // NET_QUIC_QUIC_PROTOCOL_H_