[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / net / quic / quic_protocol.h
blobe4ea75fc3e4fe7d2d86714ad088312cd995d87c8
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 <list>
11 #include <map>
12 #include <ostream>
13 #include <set>
14 #include <string>
15 #include <utility>
16 #include <vector>
18 #include "base/basictypes.h"
19 #include "base/containers/hash_tables.h"
20 #include "base/logging.h"
21 #include "base/strings/string_piece.h"
22 #include "net/base/int128.h"
23 #include "net/base/ip_endpoint.h"
24 #include "net/base/net_export.h"
25 #include "net/quic/iovector.h"
26 #include "net/quic/quic_bandwidth.h"
27 #include "net/quic/quic_time.h"
29 namespace net {
31 class QuicAckNotifier;
32 class QuicPacket;
33 struct QuicPacketHeader;
35 typedef uint64 QuicConnectionId;
36 typedef uint32 QuicStreamId;
37 typedef uint64 QuicStreamOffset;
38 typedef uint64 QuicPacketSequenceNumber;
39 typedef QuicPacketSequenceNumber QuicFecGroupNumber;
40 typedef uint64 QuicPublicResetNonceProof;
41 typedef uint8 QuicPacketEntropyHash;
42 typedef uint32 QuicHeaderId;
43 // QuicTag is the type of a tag in the wire protocol.
44 typedef uint32 QuicTag;
45 typedef std::vector<QuicTag> QuicTagVector;
46 typedef std::map<QuicTag, std::string> QuicTagValueMap;
47 // TODO(rtenneti): Didn't use SpdyPriority because SpdyPriority is uint8 and
48 // QuicPriority is uint32. Use SpdyPriority when we change the QUIC_VERSION.
49 typedef uint32 QuicPriority;
51 // Default and initial maximum size in bytes of a QUIC packet.
52 const QuicByteCount kDefaultMaxPacketSize = 1350;
53 // Default initial maximum size in bytes of a QUIC packet for servers.
54 const QuicByteCount kDefaultServerMaxPacketSize = 1000;
55 // The maximum packet size of any QUIC packet, based on ethernet's max size,
56 // minus the IP and UDP headers. IPv6 has a 40 byte header, UPD adds an
57 // additional 8 bytes. This is a total overhead of 48 bytes. Ethernet's
58 // max packet size is 1500 bytes, 1500 - 48 = 1452.
59 const QuicByteCount kMaxPacketSize = 1452;
60 // Default maximum packet size used in the Linux TCP implementation.
61 // Used in QUIC for congestion window computations in bytes.
62 const QuicByteCount kDefaultTCPMSS = 1460;
64 // We match SPDY's use of 32 when secure (since we'd compete with SPDY).
65 const QuicPacketCount kInitialCongestionWindowSecure = 32;
66 // Be conservative, and just use double a typical TCP ICWND for HTTP.
67 const QuicPacketCount kInitialCongestionWindowInsecure = 20;
69 // Minimum size of initial flow control window, for both stream and session.
70 const uint32 kMinimumFlowControlSendWindow = 16 * 1024; // 16 KB
72 // Minimum size of the CWND, in packets, when doing bandwidth resumption.
73 const QuicPacketCount kMinCongestionWindowForBandwidthResumption = 10;
75 // Maximum size of the CWND, in packets, for TCP congestion control algorithms.
76 const QuicPacketCount kMaxTcpCongestionWindow = 200;
78 // Default size of the socket receive buffer in bytes.
79 const QuicByteCount kDefaultSocketReceiveBuffer = 256 * 1024;
80 // Minimum size of the socket receive buffer in bytes.
81 // Smaller values are ignored.
82 const QuicByteCount kMinSocketReceiveBuffer = 16 * 1024;
84 // Don't allow a client to suggest an RTT shorter than 10ms.
85 const uint32 kMinInitialRoundTripTimeUs = 10 * kNumMicrosPerMilli;
87 // Don't allow a client to suggest an RTT longer than 15 seconds.
88 const uint32 kMaxInitialRoundTripTimeUs = 15 * kNumMicrosPerSecond;
90 // Maximum number of open streams per connection.
91 const size_t kDefaultMaxStreamsPerConnection = 100;
93 // Number of bytes reserved for public flags in the packet header.
94 const size_t kPublicFlagsSize = 1;
95 // Number of bytes reserved for version number in the packet header.
96 const size_t kQuicVersionSize = 4;
97 // Number of bytes reserved for private flags in the packet header.
98 const size_t kPrivateFlagsSize = 1;
99 // Number of bytes reserved for FEC group in the packet header.
100 const size_t kFecGroupSize = 1;
102 // Signifies that the QuicPacket will contain version of the protocol.
103 const bool kIncludeVersion = true;
105 // Index of the first byte in a QUIC packet which is used in hash calculation.
106 const size_t kStartOfHashData = 0;
108 // Limit on the delta between stream IDs.
109 const QuicStreamId kMaxStreamIdDelta = 200;
111 // Reserved ID for the crypto stream.
112 const QuicStreamId kCryptoStreamId = 1;
114 // Reserved ID for the headers stream.
115 const QuicStreamId kHeadersStreamId = 3;
117 // Maximum delayed ack time, in ms.
118 const int64 kMaxDelayedAckTimeMs = 25;
120 // The timeout before the handshake succeeds.
121 const int64 kInitialIdleTimeoutSecs = 5;
122 // The default idle timeout.
123 const int64 kDefaultIdleTimeoutSecs = 30;
124 // The maximum idle timeout that can be negotiated.
125 const int64 kMaximumIdleTimeoutSecs = 60 * 10; // 10 minutes.
126 // The default timeout for a connection until the crypto handshake succeeds.
127 const int64 kMaxTimeForCryptoHandshakeSecs = 10; // 10 secs.
129 // Default limit on the number of undecryptable packets the connection buffers
130 // before the CHLO/SHLO arrive.
131 const size_t kDefaultMaxUndecryptablePackets = 10;
133 // Default ping timeout.
134 const int64 kPingTimeoutSecs = 15; // 15 secs.
136 // Minimum number of RTTs between Server Config Updates (SCUP) sent to client.
137 const int kMinIntervalBetweenServerConfigUpdatesRTTs = 10;
139 // Minimum time between Server Config Updates (SCUP) sent to client.
140 const int kMinIntervalBetweenServerConfigUpdatesMs = 1000;
142 // Minimum number of packets between Server Config Updates (SCUP).
143 const int kMinPacketsBetweenServerConfigUpdates = 100;
145 // The number of open streams that a server will accept is set to be slightly
146 // larger than the negotiated limit. Immediately closing the connection if the
147 // client opens slightly too many streams is not ideal: the client may have sent
148 // a FIN that was lost, and simultaneously opened a new stream. The number of
149 // streams a server accepts is a fixed increment over the negotiated limit, or a
150 // percentage increase, whichever is larger.
151 const float kMaxStreamsMultiplier = 1.1f;
152 const int kMaxStreamsMinimumIncrement = 10;
154 // We define an unsigned 16-bit floating point value, inspired by IEEE floats
155 // (http://en.wikipedia.org/wiki/Half_precision_floating-point_format),
156 // with 5-bit exponent (bias 1), 11-bit mantissa (effective 12 with hidden
157 // bit) and denormals, but without signs, transfinites or fractions. Wire format
158 // 16 bits (little-endian byte order) are split into exponent (high 5) and
159 // mantissa (low 11) and decoded as:
160 // uint64 value;
161 // if (exponent == 0) value = mantissa;
162 // else value = (mantissa | 1 << 11) << (exponent - 1)
163 const int kUFloat16ExponentBits = 5;
164 const int kUFloat16MaxExponent = (1 << kUFloat16ExponentBits) - 2; // 30
165 const int kUFloat16MantissaBits = 16 - kUFloat16ExponentBits; // 11
166 const int kUFloat16MantissaEffectiveBits = kUFloat16MantissaBits + 1; // 12
167 const uint64 kUFloat16MaxValue = // 0x3FFC0000000
168 ((GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits) - 1) <<
169 kUFloat16MaxExponent;
171 enum TransmissionType {
172 NOT_RETRANSMISSION,
173 FIRST_TRANSMISSION_TYPE = NOT_RETRANSMISSION,
174 HANDSHAKE_RETRANSMISSION, // Retransmits due to handshake timeouts.
175 ALL_UNACKED_RETRANSMISSION, // Retransmits all unacked packets.
176 ALL_INITIAL_RETRANSMISSION, // Retransmits all initially encrypted packets.
177 LOSS_RETRANSMISSION, // Retransmits due to loss detection.
178 RTO_RETRANSMISSION, // Retransmits due to retransmit time out.
179 TLP_RETRANSMISSION, // Tail loss probes.
180 LAST_TRANSMISSION_TYPE = TLP_RETRANSMISSION,
183 enum HasRetransmittableData {
184 NO_RETRANSMITTABLE_DATA,
185 HAS_RETRANSMITTABLE_DATA,
188 enum IsHandshake {
189 NOT_HANDSHAKE,
190 IS_HANDSHAKE
193 enum class Perspective { IS_SERVER, IS_CLIENT };
195 NET_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
196 const Perspective& s);
198 // Indicates FEC protection level for data being written.
199 enum FecProtection {
200 MUST_FEC_PROTECT, // Callee must FEC protect this data.
201 MAY_FEC_PROTECT // Callee does not have to but may FEC protect this data.
204 // Indicates FEC policy.
205 enum FecPolicy {
206 FEC_PROTECT_ALWAYS, // All data in the stream should be FEC protected.
207 FEC_PROTECT_OPTIONAL // Data in the stream does not need FEC protection.
210 enum QuicFrameType {
211 // Regular frame types. The values set here cannot change without the
212 // introduction of a new QUIC version.
213 PADDING_FRAME = 0,
214 RST_STREAM_FRAME = 1,
215 CONNECTION_CLOSE_FRAME = 2,
216 GOAWAY_FRAME = 3,
217 WINDOW_UPDATE_FRAME = 4,
218 BLOCKED_FRAME = 5,
219 STOP_WAITING_FRAME = 6,
220 PING_FRAME = 7,
222 // STREAM and ACK frames are special frames. They are encoded differently on
223 // the wire and their values do not need to be stable.
224 STREAM_FRAME,
225 ACK_FRAME,
226 NUM_FRAME_TYPES
229 enum QuicConnectionIdLength {
230 PACKET_0BYTE_CONNECTION_ID = 0,
231 PACKET_1BYTE_CONNECTION_ID = 1,
232 PACKET_4BYTE_CONNECTION_ID = 4,
233 PACKET_8BYTE_CONNECTION_ID = 8
236 enum InFecGroup {
237 NOT_IN_FEC_GROUP,
238 IN_FEC_GROUP,
241 enum QuicSequenceNumberLength {
242 PACKET_1BYTE_SEQUENCE_NUMBER = 1,
243 PACKET_2BYTE_SEQUENCE_NUMBER = 2,
244 PACKET_4BYTE_SEQUENCE_NUMBER = 4,
245 PACKET_6BYTE_SEQUENCE_NUMBER = 6
248 // Used to indicate a QuicSequenceNumberLength using two flag bits.
249 enum QuicSequenceNumberLengthFlags {
250 PACKET_FLAGS_1BYTE_SEQUENCE = 0, // 00
251 PACKET_FLAGS_2BYTE_SEQUENCE = 1, // 01
252 PACKET_FLAGS_4BYTE_SEQUENCE = 1 << 1, // 10
253 PACKET_FLAGS_6BYTE_SEQUENCE = 1 << 1 | 1, // 11
256 // The public flags are specified in one byte.
257 enum QuicPacketPublicFlags {
258 PACKET_PUBLIC_FLAGS_NONE = 0,
260 // Bit 0: Does the packet header contains version info?
261 PACKET_PUBLIC_FLAGS_VERSION = 1 << 0,
263 // Bit 1: Is this packet a public reset packet?
264 PACKET_PUBLIC_FLAGS_RST = 1 << 1,
266 // Bits 2 and 3 specify the length of the ConnectionId as follows:
267 // ----00--: 0 bytes
268 // ----01--: 1 byte
269 // ----10--: 4 bytes
270 // ----11--: 8 bytes
271 PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID = 0,
272 PACKET_PUBLIC_FLAGS_1BYTE_CONNECTION_ID = 1 << 2,
273 PACKET_PUBLIC_FLAGS_4BYTE_CONNECTION_ID = 1 << 3,
274 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID = 1 << 3 | 1 << 2,
276 // Bits 4 and 5 describe the packet sequence number length as follows:
277 // --00----: 1 byte
278 // --01----: 2 bytes
279 // --10----: 4 bytes
280 // --11----: 6 bytes
281 PACKET_PUBLIC_FLAGS_1BYTE_SEQUENCE = PACKET_FLAGS_1BYTE_SEQUENCE << 4,
282 PACKET_PUBLIC_FLAGS_2BYTE_SEQUENCE = PACKET_FLAGS_2BYTE_SEQUENCE << 4,
283 PACKET_PUBLIC_FLAGS_4BYTE_SEQUENCE = PACKET_FLAGS_4BYTE_SEQUENCE << 4,
284 PACKET_PUBLIC_FLAGS_6BYTE_SEQUENCE = PACKET_FLAGS_6BYTE_SEQUENCE << 4,
286 // All bits set (bits 6 and 7 are not currently used): 00111111
287 PACKET_PUBLIC_FLAGS_MAX = (1 << 6) - 1
290 // The private flags are specified in one byte.
291 enum QuicPacketPrivateFlags {
292 PACKET_PRIVATE_FLAGS_NONE = 0,
294 // Bit 0: Does this packet contain an entropy bit?
295 PACKET_PRIVATE_FLAGS_ENTROPY = 1 << 0,
297 // Bit 1: Payload is part of an FEC group?
298 PACKET_PRIVATE_FLAGS_FEC_GROUP = 1 << 1,
300 // Bit 2: Payload is FEC as opposed to frames?
301 PACKET_PRIVATE_FLAGS_FEC = 1 << 2,
303 // All bits set (bits 3-7 are not currently used): 00000111
304 PACKET_PRIVATE_FLAGS_MAX = (1 << 3) - 1
307 // The available versions of QUIC. Guaranteed that the integer value of the enum
308 // will match the version number.
309 // When adding a new version to this enum you should add it to
310 // kSupportedQuicVersions (if appropriate), and also add a new case to the
311 // helper methods QuicVersionToQuicTag, QuicTagToQuicVersion, and
312 // QuicVersionToString.
313 enum QuicVersion {
314 // Special case to indicate unknown/unsupported QUIC version.
315 QUIC_VERSION_UNSUPPORTED = 0,
317 QUIC_VERSION_23 = 23, // Timestamp in the ack frame.
318 QUIC_VERSION_24 = 24, // SPDY/4 header compression.
321 // This vector contains QUIC versions which we currently support.
322 // This should be ordered such that the highest supported version is the first
323 // element, with subsequent elements in descending order (versions can be
324 // skipped as necessary).
326 // IMPORTANT: if you are adding to this list, follow the instructions at
327 // http://sites/quic/adding-and-removing-versions
328 static const QuicVersion kSupportedQuicVersions[] = {QUIC_VERSION_24,
329 QUIC_VERSION_23};
331 typedef std::vector<QuicVersion> QuicVersionVector;
333 // Returns a vector of QUIC versions in kSupportedQuicVersions.
334 NET_EXPORT_PRIVATE QuicVersionVector QuicSupportedVersions();
336 // QuicTag is written to and read from the wire, but we prefer to use
337 // the more readable QuicVersion at other levels.
338 // Helper function which translates from a QuicVersion to a QuicTag. Returns 0
339 // if QuicVersion is unsupported.
340 NET_EXPORT_PRIVATE QuicTag QuicVersionToQuicTag(const QuicVersion version);
342 // Returns appropriate QuicVersion from a QuicTag.
343 // Returns QUIC_VERSION_UNSUPPORTED if version_tag cannot be understood.
344 NET_EXPORT_PRIVATE QuicVersion QuicTagToQuicVersion(const QuicTag version_tag);
346 // Helper function which translates from a QuicVersion to a string.
347 // Returns strings corresponding to enum names (e.g. QUIC_VERSION_6).
348 NET_EXPORT_PRIVATE std::string QuicVersionToString(const QuicVersion version);
350 // Returns comma separated list of string representations of QuicVersion enum
351 // values in the supplied |versions| vector.
352 NET_EXPORT_PRIVATE std::string QuicVersionVectorToString(
353 const QuicVersionVector& versions);
355 // Version and Crypto tags are written to the wire with a big-endian
356 // representation of the name of the tag. For example
357 // the client hello tag (CHLO) will be written as the
358 // following 4 bytes: 'C' 'H' 'L' 'O'. Since it is
359 // stored in memory as a little endian uint32, we need
360 // to reverse the order of the bytes.
362 // MakeQuicTag returns a value given the four bytes. For example:
363 // MakeQuicTag('C', 'H', 'L', 'O');
364 NET_EXPORT_PRIVATE QuicTag MakeQuicTag(char a, char b, char c, char d);
366 // Returns true if the tag vector contains the specified tag.
367 NET_EXPORT_PRIVATE bool ContainsQuicTag(const QuicTagVector& tag_vector,
368 QuicTag tag);
370 // Size in bytes of the data or fec packet header.
371 NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(const QuicPacketHeader& header);
373 NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(
374 QuicConnectionIdLength connection_id_length,
375 bool include_version,
376 QuicSequenceNumberLength sequence_number_length,
377 InFecGroup is_in_fec_group);
379 // Index of the first byte in a QUIC packet of FEC protected data.
380 NET_EXPORT_PRIVATE size_t GetStartOfFecProtectedData(
381 QuicConnectionIdLength connection_id_length,
382 bool include_version,
383 QuicSequenceNumberLength sequence_number_length);
384 // Index of the first byte in a QUIC packet of encrypted data.
385 NET_EXPORT_PRIVATE size_t GetStartOfEncryptedData(
386 QuicConnectionIdLength connection_id_length,
387 bool include_version,
388 QuicSequenceNumberLength sequence_number_length);
390 enum QuicRstStreamErrorCode {
391 QUIC_STREAM_NO_ERROR = 0,
393 // There was some error which halted stream processing.
394 QUIC_ERROR_PROCESSING_STREAM,
395 // We got two fin or reset offsets which did not match.
396 QUIC_MULTIPLE_TERMINATION_OFFSETS,
397 // We got bad payload and can not respond to it at the protocol level.
398 QUIC_BAD_APPLICATION_PAYLOAD,
399 // Stream closed due to connection error. No reset frame is sent when this
400 // happens.
401 QUIC_STREAM_CONNECTION_ERROR,
402 // GoAway frame sent. No more stream can be created.
403 QUIC_STREAM_PEER_GOING_AWAY,
404 // The stream has been cancelled.
405 QUIC_STREAM_CANCELLED,
406 // Closing stream locally, sending a RST to allow for proper flow control
407 // accounting. Sent in response to a RST from the peer.
408 QUIC_RST_ACKNOWLEDGEMENT,
410 // No error. Used as bound while iterating.
411 QUIC_STREAM_LAST_ERROR,
414 // Because receiving an unknown QuicRstStreamErrorCode results in connection
415 // teardown, we use this to make sure any errors predating a given version are
416 // downgraded to the most appropriate existing error.
417 NET_EXPORT_PRIVATE QuicRstStreamErrorCode AdjustErrorForVersion(
418 QuicRstStreamErrorCode error_code,
419 QuicVersion version);
421 // These values must remain stable as they are uploaded to UMA histograms.
422 // To add a new error code, use the current value of QUIC_LAST_ERROR and
423 // increment QUIC_LAST_ERROR.
424 enum QuicErrorCode {
425 QUIC_NO_ERROR = 0,
427 // Connection has reached an invalid state.
428 QUIC_INTERNAL_ERROR = 1,
429 // There were data frames after the a fin or reset.
430 QUIC_STREAM_DATA_AFTER_TERMINATION = 2,
431 // Control frame is malformed.
432 QUIC_INVALID_PACKET_HEADER = 3,
433 // Frame data is malformed.
434 QUIC_INVALID_FRAME_DATA = 4,
435 // The packet contained no payload.
436 QUIC_MISSING_PAYLOAD = 48,
437 // FEC data is malformed.
438 QUIC_INVALID_FEC_DATA = 5,
439 // STREAM frame data is malformed.
440 QUIC_INVALID_STREAM_DATA = 46,
441 // STREAM frame data is not encrypted.
442 QUIC_UNENCRYPTED_STREAM_DATA = 61,
443 // RST_STREAM frame data is malformed.
444 QUIC_INVALID_RST_STREAM_DATA = 6,
445 // CONNECTION_CLOSE frame data is malformed.
446 QUIC_INVALID_CONNECTION_CLOSE_DATA = 7,
447 // GOAWAY frame data is malformed.
448 QUIC_INVALID_GOAWAY_DATA = 8,
449 // WINDOW_UPDATE frame data is malformed.
450 QUIC_INVALID_WINDOW_UPDATE_DATA = 57,
451 // BLOCKED frame data is malformed.
452 QUIC_INVALID_BLOCKED_DATA = 58,
453 // STOP_WAITING frame data is malformed.
454 QUIC_INVALID_STOP_WAITING_DATA = 60,
455 // ACK frame data is malformed.
456 QUIC_INVALID_ACK_DATA = 9,
458 // deprecated: QUIC_INVALID_CONGESTION_FEEDBACK_DATA = 47,
460 // Version negotiation packet is malformed.
461 QUIC_INVALID_VERSION_NEGOTIATION_PACKET = 10,
462 // Public RST packet is malformed.
463 QUIC_INVALID_PUBLIC_RST_PACKET = 11,
464 // There was an error decrypting.
465 QUIC_DECRYPTION_FAILURE = 12,
466 // There was an error encrypting.
467 QUIC_ENCRYPTION_FAILURE = 13,
468 // The packet exceeded kMaxPacketSize.
469 QUIC_PACKET_TOO_LARGE = 14,
470 // Data was sent for a stream which did not exist.
471 QUIC_PACKET_FOR_NONEXISTENT_STREAM = 15,
472 // The peer is going away. May be a client or server.
473 QUIC_PEER_GOING_AWAY = 16,
474 // A stream ID was invalid.
475 QUIC_INVALID_STREAM_ID = 17,
476 // A priority was invalid.
477 QUIC_INVALID_PRIORITY = 49,
478 // Too many streams already open.
479 QUIC_TOO_MANY_OPEN_STREAMS = 18,
480 // The peer must send a FIN/RST for each stream, and has not been doing so.
481 QUIC_TOO_MANY_UNFINISHED_STREAMS = 66,
482 // Received public reset for this connection.
483 QUIC_PUBLIC_RESET = 19,
484 // Invalid protocol version.
485 QUIC_INVALID_VERSION = 20,
487 // deprecated: QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED = 21
489 // The Header ID for a stream was too far from the previous.
490 QUIC_INVALID_HEADER_ID = 22,
491 // Negotiable parameter received during handshake had invalid value.
492 QUIC_INVALID_NEGOTIATED_VALUE = 23,
493 // There was an error decompressing data.
494 QUIC_DECOMPRESSION_FAILURE = 24,
495 // We hit our prenegotiated (or default) timeout
496 QUIC_CONNECTION_TIMED_OUT = 25,
497 // We hit our overall connection timeout
498 QUIC_CONNECTION_OVERALL_TIMED_OUT = 67,
499 // There was an error encountered migrating addresses
500 QUIC_ERROR_MIGRATING_ADDRESS = 26,
501 // There was an error while writing to the socket.
502 QUIC_PACKET_WRITE_ERROR = 27,
503 // There was an error while reading from the socket.
504 QUIC_PACKET_READ_ERROR = 51,
505 // We received a STREAM_FRAME with no data and no fin flag set.
506 QUIC_INVALID_STREAM_FRAME = 50,
507 // We received invalid data on the headers stream.
508 QUIC_INVALID_HEADERS_STREAM_DATA = 56,
509 // The peer received too much data, violating flow control.
510 QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA = 59,
511 // The peer sent too much data, violating flow control.
512 QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA = 63,
513 // The peer received an invalid flow control window.
514 QUIC_FLOW_CONTROL_INVALID_WINDOW = 64,
515 // The connection has been IP pooled into an existing connection.
516 QUIC_CONNECTION_IP_POOLED = 62,
517 // The connection has too many outstanding sent packets.
518 QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS = 68,
519 // The connection has too many outstanding received packets.
520 QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS = 69,
521 // The quic connection job to load server config is cancelled.
522 QUIC_CONNECTION_CANCELLED = 70,
524 // Crypto errors.
526 // Hanshake failed.
527 QUIC_HANDSHAKE_FAILED = 28,
528 // Handshake message contained out of order tags.
529 QUIC_CRYPTO_TAGS_OUT_OF_ORDER = 29,
530 // Handshake message contained too many entries.
531 QUIC_CRYPTO_TOO_MANY_ENTRIES = 30,
532 // Handshake message contained an invalid value length.
533 QUIC_CRYPTO_INVALID_VALUE_LENGTH = 31,
534 // A crypto message was received after the handshake was complete.
535 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE = 32,
536 // A crypto message was received with an illegal message tag.
537 QUIC_INVALID_CRYPTO_MESSAGE_TYPE = 33,
538 // A crypto message was received with an illegal parameter.
539 QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER = 34,
540 // An invalid channel id signature was supplied.
541 QUIC_INVALID_CHANNEL_ID_SIGNATURE = 52,
542 // A crypto message was received with a mandatory parameter missing.
543 QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND = 35,
544 // A crypto message was received with a parameter that has no overlap
545 // with the local parameter.
546 QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP = 36,
547 // A crypto message was received that contained a parameter with too few
548 // values.
549 QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND = 37,
550 // An internal error occured in crypto processing.
551 QUIC_CRYPTO_INTERNAL_ERROR = 38,
552 // A crypto handshake message specified an unsupported version.
553 QUIC_CRYPTO_VERSION_NOT_SUPPORTED = 39,
554 // There was no intersection between the crypto primitives supported by the
555 // peer and ourselves.
556 QUIC_CRYPTO_NO_SUPPORT = 40,
557 // The server rejected our client hello messages too many times.
558 QUIC_CRYPTO_TOO_MANY_REJECTS = 41,
559 // The client rejected the server's certificate chain or signature.
560 QUIC_PROOF_INVALID = 42,
561 // A crypto message was received with a duplicate tag.
562 QUIC_CRYPTO_DUPLICATE_TAG = 43,
563 // A crypto message was received with the wrong encryption level (i.e. it
564 // should have been encrypted but was not.)
565 QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT = 44,
566 // The server config for a server has expired.
567 QUIC_CRYPTO_SERVER_CONFIG_EXPIRED = 45,
568 // We failed to setup the symmetric keys for a connection.
569 QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED = 53,
570 // A handshake message arrived, but we are still validating the
571 // previous handshake message.
572 QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO = 54,
573 // A server config update arrived before the handshake is complete.
574 QUIC_CRYPTO_UPDATE_BEFORE_HANDSHAKE_COMPLETE = 65,
575 // This connection involved a version negotiation which appears to have been
576 // tampered with.
577 QUIC_VERSION_NEGOTIATION_MISMATCH = 55,
579 // No error. Used as bound while iterating.
580 QUIC_LAST_ERROR = 71,
583 struct NET_EXPORT_PRIVATE QuicPacketPublicHeader {
584 QuicPacketPublicHeader();
585 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader& other);
586 ~QuicPacketPublicHeader();
588 // Universal header. All QuicPacket headers will have a connection_id and
589 // public flags.
590 QuicConnectionId connection_id;
591 QuicConnectionIdLength connection_id_length;
592 bool reset_flag;
593 bool version_flag;
594 QuicSequenceNumberLength sequence_number_length;
595 QuicVersionVector versions;
598 // An integer which cannot be a packet sequence number.
599 const QuicPacketSequenceNumber kInvalidPacketSequenceNumber = 0;
601 // Header for Data or FEC packets.
602 struct NET_EXPORT_PRIVATE QuicPacketHeader {
603 QuicPacketHeader();
604 explicit QuicPacketHeader(const QuicPacketPublicHeader& header);
606 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
607 std::ostream& os, const QuicPacketHeader& s);
609 QuicPacketPublicHeader public_header;
610 bool fec_flag;
611 bool entropy_flag;
612 QuicPacketEntropyHash entropy_hash;
613 QuicPacketSequenceNumber packet_sequence_number;
614 InFecGroup is_in_fec_group;
615 QuicFecGroupNumber fec_group;
618 struct NET_EXPORT_PRIVATE QuicPublicResetPacket {
619 QuicPublicResetPacket();
620 explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header);
622 QuicPacketPublicHeader public_header;
623 QuicPublicResetNonceProof nonce_proof;
624 QuicPacketSequenceNumber rejected_sequence_number;
625 IPEndPoint client_address;
628 enum QuicVersionNegotiationState {
629 START_NEGOTIATION = 0,
630 // Server-side this implies we've sent a version negotiation packet and are
631 // waiting on the client to select a compatible version. Client-side this
632 // implies we've gotten a version negotiation packet, are retransmitting the
633 // initial packets with a supported version and are waiting for our first
634 // packet from the server.
635 NEGOTIATION_IN_PROGRESS,
636 // This indicates this endpoint has received a packet from the peer with a
637 // version this endpoint supports. Version negotiation is complete, and the
638 // version number will no longer be sent with future packets.
639 NEGOTIATED_VERSION
642 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket;
644 // A padding frame contains no payload.
645 struct NET_EXPORT_PRIVATE QuicPaddingFrame {
648 // A ping frame contains no payload, though it is retransmittable,
649 // and ACK'd just like other normal frames.
650 struct NET_EXPORT_PRIVATE QuicPingFrame {
653 struct NET_EXPORT_PRIVATE QuicStreamFrame {
654 QuicStreamFrame();
655 QuicStreamFrame(const QuicStreamFrame& frame);
656 QuicStreamFrame(QuicStreamId stream_id,
657 bool fin,
658 QuicStreamOffset offset,
659 IOVector data);
661 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
662 std::ostream& os, const QuicStreamFrame& s);
664 // Returns a copy of the IOVector |data| as a heap-allocated string.
665 // Caller must take ownership of the returned string.
666 std::string* GetDataAsString() const;
668 QuicStreamId stream_id;
669 bool fin;
670 QuicStreamOffset offset; // Location of this data in the stream.
671 IOVector data;
674 // TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing
675 // is finalized.
676 typedef std::set<QuicPacketSequenceNumber> SequenceNumberSet;
677 typedef std::list<QuicPacketSequenceNumber> SequenceNumberList;
679 typedef std::list<
680 std::pair<QuicPacketSequenceNumber, QuicTime> > PacketTimeList;
682 struct NET_EXPORT_PRIVATE QuicStopWaitingFrame {
683 QuicStopWaitingFrame();
684 ~QuicStopWaitingFrame();
686 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
687 std::ostream& os, const QuicStopWaitingFrame& s);
688 // Entropy hash of all packets up to, but not including, the least unacked
689 // packet.
690 QuicPacketEntropyHash entropy_hash;
691 // The lowest packet we've sent which is unacked, and we expect an ack for.
692 QuicPacketSequenceNumber least_unacked;
695 struct NET_EXPORT_PRIVATE QuicAckFrame {
696 QuicAckFrame();
697 ~QuicAckFrame();
699 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
700 std::ostream& os, const QuicAckFrame& s);
702 // Entropy hash of all packets up to largest observed not including missing
703 // packets.
704 QuicPacketEntropyHash entropy_hash;
706 // The highest packet sequence number we've observed from the peer.
708 // In general, this should be the largest packet number we've received. In
709 // the case of truncated acks, we may have to advertise a lower "upper bound"
710 // than largest received, to avoid implicitly acking missing packets that
711 // don't fit in the missing packet list due to size limitations. In this
712 // case, largest_observed may be a packet which is also in the missing packets
713 // list.
714 QuicPacketSequenceNumber largest_observed;
716 // Time elapsed since largest_observed was received until this Ack frame was
717 // sent.
718 QuicTime::Delta delta_time_largest_observed;
720 // TODO(satyamshekhar): Can be optimized using an interval set like data
721 // structure.
722 // The set of packets which we're expecting and have not received.
723 SequenceNumberSet missing_packets;
725 // Whether the ack had to be truncated when sent.
726 bool is_truncated;
728 // Packets which have been revived via FEC.
729 // All of these must also be in missing_packets.
730 SequenceNumberSet revived_packets;
732 // List of <sequence_number, time> for when packets arrived.
733 PacketTimeList received_packet_times;
736 // True if the sequence number is greater than largest_observed or is listed
737 // as missing.
738 // Always returns false for sequence numbers less than least_unacked.
739 bool NET_EXPORT_PRIVATE IsAwaitingPacket(
740 const QuicAckFrame& ack_frame,
741 QuicPacketSequenceNumber sequence_number);
743 // Inserts missing packets between [lower, higher).
744 void NET_EXPORT_PRIVATE InsertMissingPacketsBetween(
745 QuicAckFrame* ack_frame,
746 QuicPacketSequenceNumber lower,
747 QuicPacketSequenceNumber higher);
749 // Defines for all types of congestion control algorithms that can be used in
750 // QUIC. Note that this is separate from the congestion feedback type -
751 // some congestion control algorithms may use the same feedback type
752 // (Reno and Cubic are the classic example for that).
753 enum CongestionControlType {
754 kCubic,
755 kCubicBytes,
756 kReno,
757 kRenoBytes,
758 kBBR,
761 enum LossDetectionType {
762 kNack, // Used to mimic TCP's loss detection.
763 kTime, // Time based loss detection.
766 struct NET_EXPORT_PRIVATE QuicRstStreamFrame {
767 QuicRstStreamFrame();
768 QuicRstStreamFrame(QuicStreamId stream_id,
769 QuicRstStreamErrorCode error_code,
770 QuicStreamOffset bytes_written);
772 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
773 std::ostream& os, const QuicRstStreamFrame& r);
775 QuicStreamId stream_id;
776 QuicRstStreamErrorCode error_code;
777 std::string error_details;
779 // Used to update flow control windows. On termination of a stream, both
780 // endpoints must inform the peer of the number of bytes they have sent on
781 // that stream. This can be done through normal termination (data packet with
782 // FIN) or through a RST.
783 QuicStreamOffset byte_offset;
786 struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame {
787 QuicConnectionCloseFrame();
789 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
790 std::ostream& os, const QuicConnectionCloseFrame& c);
792 QuicErrorCode error_code;
793 std::string error_details;
796 struct NET_EXPORT_PRIVATE QuicGoAwayFrame {
797 QuicGoAwayFrame();
798 QuicGoAwayFrame(QuicErrorCode error_code,
799 QuicStreamId last_good_stream_id,
800 const std::string& reason);
802 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
803 std::ostream& os, const QuicGoAwayFrame& g);
805 QuicErrorCode error_code;
806 QuicStreamId last_good_stream_id;
807 std::string reason_phrase;
810 // Flow control updates per-stream and at the connection levoel.
811 // Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather
812 // than a window delta.
813 // TODO(rjshade): A possible future optimization is to make stream_id and
814 // byte_offset variable length, similar to stream frames.
815 struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame {
816 QuicWindowUpdateFrame() {}
817 QuicWindowUpdateFrame(QuicStreamId stream_id, QuicStreamOffset byte_offset);
819 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
820 std::ostream& os, const QuicWindowUpdateFrame& w);
822 // The stream this frame applies to. 0 is a special case meaning the overall
823 // connection rather than a specific stream.
824 QuicStreamId stream_id;
826 // Byte offset in the stream or connection. The receiver of this frame must
827 // not send data which would result in this offset being exceeded.
828 QuicStreamOffset byte_offset;
831 // The BLOCKED frame is used to indicate to the remote endpoint that this
832 // endpoint believes itself to be flow-control blocked but otherwise ready to
833 // send data. The BLOCKED frame is purely advisory and optional.
834 // Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28).
835 struct NET_EXPORT_PRIVATE QuicBlockedFrame {
836 QuicBlockedFrame() {}
837 explicit QuicBlockedFrame(QuicStreamId stream_id);
839 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
840 std::ostream& os, const QuicBlockedFrame& b);
842 // The stream this frame applies to. 0 is a special case meaning the overall
843 // connection rather than a specific stream.
844 QuicStreamId stream_id;
847 // EncryptionLevel enumerates the stages of encryption that a QUIC connection
848 // progresses through. When retransmitting a packet, the encryption level needs
849 // to be specified so that it is retransmitted at a level which the peer can
850 // understand.
851 enum EncryptionLevel {
852 ENCRYPTION_NONE = 0,
853 ENCRYPTION_INITIAL = 1,
854 ENCRYPTION_FORWARD_SECURE = 2,
856 NUM_ENCRYPTION_LEVELS,
859 struct NET_EXPORT_PRIVATE QuicFrame {
860 QuicFrame();
861 explicit QuicFrame(QuicPaddingFrame* padding_frame);
862 explicit QuicFrame(QuicStreamFrame* stream_frame);
863 explicit QuicFrame(QuicAckFrame* frame);
865 explicit QuicFrame(QuicRstStreamFrame* frame);
866 explicit QuicFrame(QuicConnectionCloseFrame* frame);
867 explicit QuicFrame(QuicStopWaitingFrame* frame);
868 explicit QuicFrame(QuicPingFrame* frame);
869 explicit QuicFrame(QuicGoAwayFrame* frame);
870 explicit QuicFrame(QuicWindowUpdateFrame* frame);
871 explicit QuicFrame(QuicBlockedFrame* frame);
873 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
874 std::ostream& os, const QuicFrame& frame);
876 QuicFrameType type;
877 union {
878 QuicPaddingFrame* padding_frame;
879 QuicStreamFrame* stream_frame;
880 QuicAckFrame* ack_frame;
882 QuicStopWaitingFrame* stop_waiting_frame;
884 QuicPingFrame* ping_frame;
885 QuicRstStreamFrame* rst_stream_frame;
886 QuicConnectionCloseFrame* connection_close_frame;
887 QuicGoAwayFrame* goaway_frame;
888 QuicWindowUpdateFrame* window_update_frame;
889 QuicBlockedFrame* blocked_frame;
893 typedef std::vector<QuicFrame> QuicFrames;
895 struct NET_EXPORT_PRIVATE QuicFecData {
896 QuicFecData();
898 // The FEC group number is also the sequence number of the first
899 // FEC protected packet. The last protected packet's sequence number will
900 // be one less than the sequence number of the FEC packet.
901 QuicFecGroupNumber fec_group;
902 base::StringPiece redundancy;
905 class NET_EXPORT_PRIVATE QuicData {
906 public:
907 QuicData(const char* buffer, size_t length);
908 QuicData(char* buffer, size_t length, bool owns_buffer);
909 virtual ~QuicData();
911 base::StringPiece AsStringPiece() const {
912 return base::StringPiece(data(), length());
915 const char* data() const { return buffer_; }
916 size_t length() const { return length_; }
918 private:
919 const char* buffer_;
920 size_t length_;
921 bool owns_buffer_;
923 DISALLOW_COPY_AND_ASSIGN(QuicData);
926 class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
927 public:
928 QuicPacket(char* buffer,
929 size_t length,
930 bool owns_buffer,
931 QuicConnectionIdLength connection_id_length,
932 bool includes_version,
933 QuicSequenceNumberLength sequence_number_length);
935 base::StringPiece FecProtectedData() const;
936 base::StringPiece AssociatedData() const;
937 base::StringPiece BeforePlaintext() const;
938 base::StringPiece Plaintext() const;
940 char* mutable_data() { return buffer_; }
942 private:
943 char* buffer_;
944 const QuicConnectionIdLength connection_id_length_;
945 const bool includes_version_;
946 const QuicSequenceNumberLength sequence_number_length_;
948 DISALLOW_COPY_AND_ASSIGN(QuicPacket);
951 class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
952 public:
953 QuicEncryptedPacket(const char* buffer, size_t length);
954 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer);
956 // Clones the packet into a new packet which owns the buffer.
957 QuicEncryptedPacket* Clone() const;
959 // By default, gtest prints the raw bytes of an object. The bool data
960 // member (in the base class QuicData) causes this object to have padding
961 // bytes, which causes the default gtest object printer to read
962 // uninitialize memory. So we need to teach gtest how to print this object.
963 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
964 std::ostream& os, const QuicEncryptedPacket& s);
966 private:
967 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket);
970 class NET_EXPORT_PRIVATE RetransmittableFrames {
971 public:
972 explicit RetransmittableFrames(EncryptionLevel level);
973 ~RetransmittableFrames();
975 // Allocates a local copy of the referenced StringPiece has QuicStreamFrame
976 // use it.
977 // Takes ownership of |stream_frame|.
978 const QuicFrame& AddStreamFrame(QuicStreamFrame* stream_frame);
979 // Takes ownership of the frame inside |frame|.
980 const QuicFrame& AddNonStreamFrame(const QuicFrame& frame);
981 // Removes all stream frames associated with |stream_id|.
982 void RemoveFramesForStream(QuicStreamId stream_id);
984 const QuicFrames& frames() const { return frames_; }
986 IsHandshake HasCryptoHandshake() const {
987 return has_crypto_handshake_;
990 EncryptionLevel encryption_level() const {
991 return encryption_level_;
994 private:
995 QuicFrames frames_;
996 const EncryptionLevel encryption_level_;
997 IsHandshake has_crypto_handshake_;
998 // Data referenced by the StringPiece of a QuicStreamFrame.
999 std::vector<std::string*> stream_data_;
1001 DISALLOW_COPY_AND_ASSIGN(RetransmittableFrames);
1004 struct NET_EXPORT_PRIVATE SerializedPacket {
1005 SerializedPacket(QuicPacketSequenceNumber sequence_number,
1006 QuicSequenceNumberLength sequence_number_length,
1007 QuicEncryptedPacket* packet,
1008 QuicPacketEntropyHash entropy_hash,
1009 RetransmittableFrames* retransmittable_frames);
1010 ~SerializedPacket();
1012 QuicPacketSequenceNumber sequence_number;
1013 QuicSequenceNumberLength sequence_number_length;
1014 QuicEncryptedPacket* packet;
1015 QuicPacketEntropyHash entropy_hash;
1016 RetransmittableFrames* retransmittable_frames;
1017 bool is_fec_packet;
1019 // Optional notifiers which will be informed when this packet has been ACKed.
1020 std::list<QuicAckNotifier*> notifiers;
1023 struct NET_EXPORT_PRIVATE TransmissionInfo {
1024 // Used by STL when assigning into a map.
1025 TransmissionInfo();
1027 // Constructs a Transmission with a new all_tranmissions set
1028 // containing |sequence_number|.
1029 TransmissionInfo(RetransmittableFrames* retransmittable_frames,
1030 QuicSequenceNumberLength sequence_number_length,
1031 TransmissionType transmission_type,
1032 QuicTime sent_time,
1033 QuicByteCount bytes_sent,
1034 bool is_fec_packet);
1036 RetransmittableFrames* retransmittable_frames;
1037 QuicSequenceNumberLength sequence_number_length;
1038 QuicTime sent_time;
1039 QuicByteCount bytes_sent;
1040 QuicPacketCount nack_count;
1041 // Reason why this packet was transmitted.
1042 TransmissionType transmission_type;
1043 // Stores the sequence numbers of all transmissions of this packet.
1044 // Must always be nullptr or have multiple elements.
1045 SequenceNumberList* all_transmissions;
1046 // In flight packets have not been abandoned or lost.
1047 bool in_flight;
1048 // True if the packet can never be acked, so it can be removed.
1049 bool is_unackable;
1050 // True if the packet is an FEC packet.
1051 bool is_fec_packet;
1054 } // namespace net
1056 #endif // NET_QUIC_QUIC_PROTOCOL_H_