Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / net / quic / quic_protocol.h
index aff081c..8bb89fa 100644 (file)
@@ -19,7 +19,9 @@
 #include "base/logging.h"
 #include "base/strings/string_piece.h"
 #include "net/base/int128.h"
+#include "net/base/ip_endpoint.h"
 #include "net/base/net_export.h"
+#include "net/quic/iovector.h"
 #include "net/quic/quic_bandwidth.h"
 #include "net/quic/quic_time.h"
 
@@ -42,21 +44,32 @@ typedef uint32 QuicHeaderId;
 // QuicTag is the type of a tag in the wire protocol.
 typedef uint32 QuicTag;
 typedef std::vector<QuicTag> QuicTagVector;
+typedef std::map<QuicTag, std::string> QuicTagValueMap;
+// TODO(rtenneti): Didn't use SpdyPriority because SpdyPriority is uint8 and
+// QuicPriority is uint32. Use SpdyPriority when we change the QUIC_VERSION.
 typedef uint32 QuicPriority;
 
 // TODO(rch): Consider Quic specific names for these constants.
 // Default and initial maximum size in bytes of a QUIC packet.
 const QuicByteCount kDefaultMaxPacketSize = 1200;
 // The maximum packet size of any QUIC packet, based on ethernet's max size,
-// minus the IP and UDP headers.
-const QuicByteCount kMaxPacketSize = 1472;
+// minus the IP and UDP headers. IPv6 has a 40 byte header, UPD adds an
+// additional 8 bytes.  This is a total overhead of 48 bytes.  Ethernet's
+// max packet size is 1500 bytes,  1500 - 48 = 1452.
+const QuicByteCount kMaxPacketSize = 1452;
 
 // Maximum size of the initial congestion window in packets.
 const size_t kDefaultInitialWindow = 10;
-const size_t kMaxInitialWindow = 100;
+// TODO(ianswett): Temporarily changed to 10 due to a large number of clients
+// mistakenly negotiating 100 initially and suffering the consequences.
+const size_t kMaxInitialWindow = 10;
+
+// Maximum size of the congestion window, in packets, for TCP congestion control
+// algorithms.
+const size_t kMaxTcpCongestionWindow = 200;
 
 // Don't allow a client to suggest an RTT longer than 15 seconds.
-const uint32 kMaxInitialRoundTripTimeUs = 15 * kNumMicrosPerSecond;
+const size_t kMaxInitialRoundTripTimeUs = 15 * kNumMicrosPerSecond;
 
 // Maximum number of open streams per connection.
 const size_t kDefaultMaxStreamsPerConnection = 100;
@@ -69,6 +82,7 @@ const size_t kQuicVersionSize = 4;
 const size_t kPrivateFlagsSize = 1;
 // Number of bytes reserved for FEC group in the packet header.
 const size_t kFecGroupSize = 1;
+// TODO(wtc): remove this when we drop support for QUIC_VERSION_13.
 // Number of bytes reserved for the nonce proof in public reset packet.
 const size_t kPublicResetNonceSize = 8;
 
@@ -84,9 +98,11 @@ const QuicStreamId kMaxStreamIdDelta = 100;
 const QuicHeaderId kMaxHeaderIdDelta = 100;
 
 // Reserved ID for the crypto stream.
-// TODO(rch): ensure that this is not usable by any other streams.
 const QuicStreamId kCryptoStreamId = 1;
 
+// Reserved ID for the headers stream.
+const QuicStreamId kHeadersStreamId = 3;
+
 // This is the default network timeout a for connection till the crypto
 // handshake succeeds and the negotiated timeout from the handshake is received.
 const int64 kDefaultInitialTimeoutSecs = 120;  // 2 mins.
@@ -114,6 +130,12 @@ enum TransmissionType {
   NOT_RETRANSMISSION,
   NACK_RETRANSMISSION,
   RTO_RETRANSMISSION,
+  TLP_RETRANSMISSION,
+};
+
+enum RetransmissionType {
+  INITIAL_ENCRYPTION_ONLY,
+  ALL_PACKETS
 };
 
 enum HasRetransmittableData {
@@ -127,10 +149,17 @@ enum IsHandshake {
 };
 
 enum QuicFrameType {
+  // Regular frame types. The values set here cannot change without the
+  // introduction of a new QUIC version.
   PADDING_FRAME = 0,
-  RST_STREAM_FRAME,
-  CONNECTION_CLOSE_FRAME,
-  GOAWAY_FRAME,
+  RST_STREAM_FRAME = 1,
+  CONNECTION_CLOSE_FRAME = 2,
+  GOAWAY_FRAME = 3,
+  WINDOW_UPDATE_FRAME = 4,
+  BLOCKED_FRAME = 5,
+
+  // STREAM, ACK, and CONGESTION_FEEDBACK frames are special frames. They are
+  // encoded differently on the wire and their values do not need to be stable.
   STREAM_FRAME,
   ACK_FRAME,
   CONGESTION_FEEDBACK_FRAME,
@@ -225,17 +254,23 @@ enum QuicVersion {
   // Special case to indicate unknown/unsupported QUIC version.
   QUIC_VERSION_UNSUPPORTED = 0,
 
-  QUIC_VERSION_10 = 10,
-  QUIC_VERSION_11 = 11,
-  QUIC_VERSION_12 = 12,  // Current version.
+  QUIC_VERSION_12 = 12,
+  QUIC_VERSION_13 = 13,
+  QUIC_VERSION_14 = 14,
+  QUIC_VERSION_15 = 15,  // Current version.
 };
 
 // This vector contains QUIC versions which we currently support.
 // This should be ordered such that the highest supported version is the first
 // element, with subsequent elements in descending order (versions can be
 // skipped as necessary).
-static const QuicVersion kSupportedQuicVersions[] =
-    {QUIC_VERSION_11};
+//
+// IMPORTANT: if you are addding to this list, follow the instructions at
+// http://sites/quic/adding-and-removing-versions
+static const QuicVersion kSupportedQuicVersions[] = {QUIC_VERSION_15,
+                                                     QUIC_VERSION_14,
+                                                     QUIC_VERSION_13,
+                                                     QUIC_VERSION_12};
 
 typedef std::vector<QuicVersion> QuicVersionVector;
 
@@ -273,7 +308,7 @@ NET_EXPORT_PRIVATE std::string QuicVersionVectorToString(
 NET_EXPORT_PRIVATE QuicTag MakeQuicTag(char a, char b, char c, char d);
 
 // Size in bytes of the data or fec packet header.
-NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(QuicPacketHeader header);
+NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(const QuicPacketHeader& header);
 
 NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(
     QuicGuidLength guid_length,
@@ -281,9 +316,6 @@ NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(
     QuicSequenceNumberLength sequence_number_length,
     InFecGroup is_in_fec_group);
 
-// Size in bytes of the public reset packet.
-NET_EXPORT_PRIVATE size_t GetPublicResetPacketSize();
-
 // Index of the first byte in a QUIC packet of FEC protected data.
 NET_EXPORT_PRIVATE size_t GetStartOfFecProtectedData(
     QuicGuidLength guid_length,
@@ -342,6 +374,10 @@ enum QuicErrorCode {
   QUIC_INVALID_CONNECTION_CLOSE_DATA = 7,
   // GOAWAY frame data is malformed.
   QUIC_INVALID_GOAWAY_DATA = 8,
+  // WINDOW_UPDATE frame data is malformed.
+  QUIC_INVALID_WINDOW_UPDATE_DATA = 57,
+  // BLOCKED frame data is malformed.
+  QUIC_INVALID_BLOCKED_DATA = 58,
   // ACK frame data is malformed.
   QUIC_INVALID_ACK_DATA = 9,
   // CONGESTION_FEEDBACK frame data is malformed.
@@ -388,7 +424,10 @@ enum QuicErrorCode {
   QUIC_PACKET_READ_ERROR = 51,
   // We received a STREAM_FRAME with no data and no fin flag set.
   QUIC_INVALID_STREAM_FRAME = 50,
-
+  // We received invalid data on the headers stream.
+  QUIC_INVALID_HEADERS_STREAM_DATA = 56,
+  // The peer violated the flow control protocol.
+  QUIC_FLOW_CONTROL_ERROR = 59,
 
   // Crypto errors.
 
@@ -436,9 +475,15 @@ enum QuicErrorCode {
   QUIC_CRYPTO_SERVER_CONFIG_EXPIRED = 45,
   // We failed to setup the symmetric keys for a connection.
   QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED = 53,
+  // A handshake message arrived, but we are still validating the
+  // previous handshake message.
+  QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO = 54,
+  // This connection involved a version negotiation which appears to have been
+  // tampered with.
+  QUIC_VERSION_NEGOTIATION_MISMATCH = 55,
 
   // No error. Used as bound while iterating.
-  QUIC_LAST_ERROR = 54,
+  QUIC_LAST_ERROR = 60,
 };
 
 struct NET_EXPORT_PRIVATE QuicPacketPublicHeader {
@@ -473,12 +518,13 @@ struct NET_EXPORT_PRIVATE QuicPacketHeader {
 };
 
 struct NET_EXPORT_PRIVATE QuicPublicResetPacket {
-  QuicPublicResetPacket() {}
-  explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header)
-      : public_header(header) {}
+  QuicPublicResetPacket();
+  explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header);
+
   QuicPacketPublicHeader public_header;
-  QuicPacketSequenceNumber rejected_sequence_number;
   QuicPublicResetNonceProof nonce_proof;
+  QuicPacketSequenceNumber rejected_sequence_number;
+  IPEndPoint client_address;
 };
 
 enum QuicVersionNegotiationState {
@@ -503,15 +549,23 @@ struct NET_EXPORT_PRIVATE QuicPaddingFrame {
 
 struct NET_EXPORT_PRIVATE QuicStreamFrame {
   QuicStreamFrame();
+  QuicStreamFrame(const QuicStreamFrame& frame);
   QuicStreamFrame(QuicStreamId stream_id,
                   bool fin,
                   QuicStreamOffset offset,
-                  base::StringPiece data);
+                  IOVector data);
+
+  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
+      std::ostream& os, const QuicStreamFrame& s);
+
+  // Returns a copy of the IOVector |data| as a heap-allocated string.
+  // Caller must take ownership of the returned string.
+  std::string* GetDataAsString() const;
 
   QuicStreamId stream_id;
   bool fin;
   QuicStreamOffset offset;  // Location of this data in the stream.
-  base::StringPiece data;
+  IOVector data;
 
   // If this is set, then when this packet is ACKed the AckNotifier will be
   // informed.
@@ -527,6 +581,7 @@ typedef std::map<QuicPacketSequenceNumber, QuicTime> TimeMap;
 struct NET_EXPORT_PRIVATE ReceivedPacketInfo {
   ReceivedPacketInfo();
   ~ReceivedPacketInfo();
+
   NET_EXPORT_PRIVATE friend std::ostream& operator<<(
       std::ostream& os, const ReceivedPacketInfo& s);
 
@@ -555,6 +610,10 @@ struct NET_EXPORT_PRIVATE ReceivedPacketInfo {
 
   // Whether the ack had to be truncated when sent.
   bool is_truncated;
+
+  // Packets which have been revived via FEC.
+  // All of these must also be in missing_packets.
+  SequenceNumberSet revived_packets;
 };
 
 // True if the sequence number is greater than largest_observed or is listed
@@ -573,9 +632,9 @@ void NET_EXPORT_PRIVATE InsertMissingPacketsBetween(
 struct NET_EXPORT_PRIVATE SentPacketInfo {
   SentPacketInfo();
   ~SentPacketInfo();
+
   NET_EXPORT_PRIVATE friend std::ostream& operator<<(
       std::ostream& os, const SentPacketInfo& s);
-
   // Entropy hash of all packets up to, but not including, the least unacked
   // packet.
   QuicPacketEntropyHash entropy_hash;
@@ -584,7 +643,7 @@ struct NET_EXPORT_PRIVATE SentPacketInfo {
 };
 
 struct NET_EXPORT_PRIVATE QuicAckFrame {
-  QuicAckFrame() {}
+  QuicAckFrame();
   // Testing convenience method to construct a QuicAckFrame with all packets
   // from least_unacked to largest_observed acked.
   QuicAckFrame(QuicPacketSequenceNumber largest_observed,
@@ -608,14 +667,15 @@ enum CongestionFeedbackType {
 };
 
 struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP {
-  uint16 accumulated_number_of_lost_packets;
+  CongestionFeedbackMessageTCP();
+
   QuicByteCount receive_window;
 };
 
 struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival {
   CongestionFeedbackMessageInterArrival();
   ~CongestionFeedbackMessageInterArrival();
-  uint16 accumulated_number_of_lost_packets;
+
   // The set of received packets since the last feedback was sent, along with
   // their arrival times.
   TimeMap received_packet_times;
@@ -642,35 +702,86 @@ struct NET_EXPORT_PRIVATE QuicCongestionFeedbackFrame {
 };
 
 struct NET_EXPORT_PRIVATE QuicRstStreamFrame {
-  QuicRstStreamFrame() {}
-  QuicRstStreamFrame(QuicStreamId stream_id, QuicRstStreamErrorCode error_code)
-      : stream_id(stream_id), error_code(error_code) {
-    DCHECK_LE(error_code, std::numeric_limits<uint8>::max());
-  }
+  QuicRstStreamFrame();
+  QuicRstStreamFrame(QuicStreamId stream_id,
+                     QuicRstStreamErrorCode error_code,
+                     QuicStreamOffset bytes_written);
+
+  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
+      std::ostream& os, const QuicRstStreamFrame& r);
 
   QuicStreamId stream_id;
   QuicRstStreamErrorCode error_code;
   std::string error_details;
+
+  // Used to update flow control windows. On termination of a stream, both
+  // endpoints must inform the peer of the number of bytes they have sent on
+  // that stream. This can be done through normal termination (data packet with
+  // FIN) or through a RST.
+  QuicStreamOffset byte_offset;
 };
 
 struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame {
+  QuicConnectionCloseFrame();
+
+  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
+      std::ostream& os, const QuicConnectionCloseFrame& c);
+
   QuicErrorCode error_code;
   std::string error_details;
-  // TODO(ianswett): Remove this once QUIC_VERSION_11 is removed.
-  QuicAckFrame ack_frame;
 };
 
 struct NET_EXPORT_PRIVATE QuicGoAwayFrame {
-  QuicGoAwayFrame() {}
+  QuicGoAwayFrame();
   QuicGoAwayFrame(QuicErrorCode error_code,
                   QuicStreamId last_good_stream_id,
                   const std::string& reason);
 
+  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
+      std::ostream& os, const QuicGoAwayFrame& g);
+
   QuicErrorCode error_code;
   QuicStreamId last_good_stream_id;
   std::string reason_phrase;
 };
 
+// Flow control updates per-stream and at the connection levoel.
+// Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather
+// than a window delta.
+// TODO(rjshade): A possible future optimization is to make stream_id and
+//                byte_offset variable length, similar to stream frames.
+struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame {
+  QuicWindowUpdateFrame() {}
+  QuicWindowUpdateFrame(QuicStreamId stream_id, QuicStreamOffset byte_offset);
+
+  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
+      std::ostream& os, const QuicWindowUpdateFrame& w);
+
+  // The stream this frame applies to.  0 is a special case meaning the overall
+  // connection rather than a specific stream.
+  QuicStreamId stream_id;
+
+  // Byte offset in the stream or connection. The receiver of this frame must
+  // not send data which would result in this offset being exceeded.
+  QuicStreamOffset byte_offset;
+};
+
+// The BLOCKED frame is used to indicate to the remote endpoint that this
+// endpoint believes itself to be flow-control blocked but otherwise ready to
+// send data. The BLOCKED frame is purely advisory and optional.
+// Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28).
+struct NET_EXPORT_PRIVATE QuicBlockedFrame {
+  QuicBlockedFrame() {}
+  explicit QuicBlockedFrame(QuicStreamId stream_id);
+
+  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
+      std::ostream& os, const QuicBlockedFrame& b);
+
+  // The stream this frame applies to.  0 is a special case meaning the overall
+  // connection rather than a specific stream.
+  QuicStreamId stream_id;
+};
+
 // EncryptionLevel enumerates the stages of encryption that a QUIC connection
 // progresses through. When retransmitting a packet, the encryption level needs
 // to be specified so that it is retransmitted at a level which the peer can
@@ -684,35 +795,19 @@ enum EncryptionLevel {
 };
 
 struct NET_EXPORT_PRIVATE QuicFrame {
-  QuicFrame() {}
-  explicit QuicFrame(QuicPaddingFrame* padding_frame)
-      : type(PADDING_FRAME),
-        padding_frame(padding_frame) {
-  }
-  explicit QuicFrame(QuicStreamFrame* stream_frame)
-      : type(STREAM_FRAME),
-        stream_frame(stream_frame) {
-  }
-  explicit QuicFrame(QuicAckFrame* frame)
-      : type(ACK_FRAME),
-        ack_frame(frame) {
-  }
-  explicit QuicFrame(QuicCongestionFeedbackFrame* frame)
-      : type(CONGESTION_FEEDBACK_FRAME),
-        congestion_feedback_frame(frame) {
-  }
-  explicit QuicFrame(QuicRstStreamFrame* frame)
-      : type(RST_STREAM_FRAME),
-        rst_stream_frame(frame) {
-  }
-  explicit QuicFrame(QuicConnectionCloseFrame* frame)
-      : type(CONNECTION_CLOSE_FRAME),
-        connection_close_frame(frame) {
-  }
-  explicit QuicFrame(QuicGoAwayFrame* frame)
-      : type(GOAWAY_FRAME),
-        goaway_frame(frame) {
-  }
+  QuicFrame();
+  explicit QuicFrame(QuicPaddingFrame* padding_frame);
+  explicit QuicFrame(QuicStreamFrame* stream_frame);
+  explicit QuicFrame(QuicAckFrame* frame);
+  explicit QuicFrame(QuicCongestionFeedbackFrame* frame);
+  explicit QuicFrame(QuicRstStreamFrame* frame);
+  explicit QuicFrame(QuicConnectionCloseFrame* frame);
+  explicit QuicFrame(QuicGoAwayFrame* frame);
+  explicit QuicFrame(QuicWindowUpdateFrame* frame);
+  explicit QuicFrame(QuicBlockedFrame* frame);
+
+  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
+      std::ostream& os, const QuicFrame& frame);
 
   QuicFrameType type;
   union {
@@ -723,6 +818,8 @@ struct NET_EXPORT_PRIVATE QuicFrame {
     QuicRstStreamFrame* rst_stream_frame;
     QuicConnectionCloseFrame* connection_close_frame;
     QuicGoAwayFrame* goaway_frame;
+    QuicWindowUpdateFrame* window_update_frame;
+    QuicBlockedFrame* blocked_frame;
   };
 };
 
@@ -740,16 +837,8 @@ struct NET_EXPORT_PRIVATE QuicFecData {
 
 class NET_EXPORT_PRIVATE QuicData {
  public:
-  QuicData(const char* buffer, size_t length)
-      : buffer_(buffer),
-        length_(length),
-        owns_buffer_(false) {}
-
-  QuicData(char* buffer, size_t length, bool owns_buffer)
-      : buffer_(buffer),
-        length_(length),
-        owns_buffer_(owns_buffer) {}
-
+  QuicData(const char* buffer, size_t length);
+  QuicData(char* buffer, size_t length, bool owns_buffer);
   virtual ~QuicData();
 
   base::StringPiece AsStringPiece() const {
@@ -807,13 +896,7 @@ class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
              QuicGuidLength guid_length,
              bool includes_version,
              QuicSequenceNumberLength sequence_number_length,
-             bool is_fec_packet)
-      : QuicData(buffer, length, owns_buffer),
-        buffer_(buffer),
-        is_fec_packet_(is_fec_packet),
-        guid_length_(guid_length),
-        includes_version_(includes_version),
-        sequence_number_length_(sequence_number_length) {}
+             bool is_fec_packet);
 
   char* buffer_;
   const bool is_fec_packet_;
@@ -826,11 +909,11 @@ class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
 
 class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
  public:
-  QuicEncryptedPacket(const char* buffer, size_t length)
-      : QuicData(buffer, length) {}
+  QuicEncryptedPacket(const char* buffer, size_t length);
+  QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer);
 
-  QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer)
-      : QuicData(buffer, length, owns_buffer) {}
+  // Clones the packet into a new packet which owns the buffer.
+  QuicEncryptedPacket* Clone() const;
 
   // By default, gtest prints the raw bytes of an object. The bool data
   // member (in the base class QuicData) causes this object to have padding
@@ -856,6 +939,8 @@ class NET_EXPORT_PRIVATE RetransmittableFrames {
   const QuicFrame& AddNonStreamFrame(const QuicFrame& frame);
   const QuicFrames& frames() const { return frames_; }
 
+  IsHandshake HasCryptoHandshake() const;
+
   void set_encryption_level(EncryptionLevel level);
   EncryptionLevel encryption_level() const {
     return encryption_level_;
@@ -889,10 +974,9 @@ struct NET_EXPORT_PRIVATE SerializedPacket {
 };
 
 // A struct for functions which consume data payloads and fins.
-struct QuicConsumedData {
-  QuicConsumedData(size_t bytes_consumed, bool fin_consumed)
-      : bytes_consumed(bytes_consumed),
-        fin_consumed(fin_consumed) {}
+struct NET_EXPORT_PRIVATE QuicConsumedData {
+  QuicConsumedData(size_t bytes_consumed, bool fin_consumed);
+
   // By default, gtest prints the raw bytes of an object. The bool data
   // member causes this object to have padding bytes, which causes the
   // default gtest object printer to read uninitialize memory. So we need
@@ -916,9 +1000,7 @@ enum WriteStatus {
 // A struct used to return the result of write calls including either the number
 // of bytes written or the error code, depending upon the status.
 struct NET_EXPORT_PRIVATE WriteResult {
-  WriteResult(WriteStatus status, int bytes_written_or_error_code) :
-    status(status), bytes_written(bytes_written_or_error_code) {
-  }
+  WriteResult(WriteStatus status, int bytes_written_or_error_code);
 
   WriteStatus status;
   union {