[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_QUIC_QUIC_PROTOCOL_H_ |
| 6 | #define NET_QUIC_QUIC_PROTOCOL_H_ |
| 7 | |
[email protected] | e537f74 | 2012-12-07 15:33:53 | [diff] [blame] | 8 | #include <stddef.h> |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 9 | #include <limits> |
[email protected] | a674b4c | 2012-12-05 03:44:30 | [diff] [blame] | 10 | #include <map> |
[email protected] | 5640d0a | 2012-10-22 18:17:02 | [diff] [blame] | 11 | #include <ostream> |
[email protected] | e537f74 | 2012-12-07 15:33:53 | [diff] [blame] | 12 | #include <set> |
| 13 | #include <string> |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 14 | #include <utility> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "base/basictypes.h" |
| 18 | #include "base/hash_tables.h" |
| 19 | #include "base/logging.h" |
| 20 | #include "base/string_piece.h" |
[email protected] | 165e075 | 2012-11-16 07:49:44 | [diff] [blame] | 21 | #include "net/base/int128.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 22 | #include "net/base/net_export.h" |
[email protected] | 2a960e0 | 2012-11-11 14:48:10 | [diff] [blame] | 23 | #include "net/quic/quic_time.h" |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 24 | |
| 25 | namespace net { |
| 26 | |
[email protected] | f1e97e9 | 2012-12-16 04:53:25 | [diff] [blame] | 27 | using ::operator<<; |
| 28 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 29 | class QuicPacket; |
| 30 | |
| 31 | typedef uint64 QuicGuid; |
| 32 | typedef uint32 QuicStreamId; |
| 33 | typedef uint64 QuicStreamOffset; |
| 34 | typedef uint64 QuicPacketSequenceNumber; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 35 | typedef QuicPacketSequenceNumber QuicFecGroupNumber; |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 36 | typedef uint64 QuicPublicResetNonceProof; |
[email protected] | cff7b7b | 2013-01-11 08:49:07 | [diff] [blame] | 37 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 38 | // TODO(rch): Consider Quic specific names for these constants. |
| 39 | const size_t kMaxPacketSize = 1200; // Maximum size in bytes of a QUIC packet. |
| 40 | |
| 41 | // Maximum number of open streams per connection. |
| 42 | const size_t kDefaultMaxStreamsPerConnection = 100; |
| 43 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 44 | // Number of bytes reserved for guid in the packet header. |
| 45 | const size_t kQuicGuidSize = 8; |
| 46 | // Number of bytes reserved for public flags in the packet header. |
| 47 | const size_t kPublicFlagsSize = 1; |
| 48 | // Number of bytes reserved for sequence number in the packet header. |
| 49 | const size_t kSequenceNumberSize = 6; |
| 50 | // Number of bytes reserved for private flags in the packet header. |
| 51 | const size_t kPrivateFlagsSize = 1; |
| 52 | // Number of bytes reserved for FEC group in the packet header. |
| 53 | const size_t kFecGroupSize = 1; |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 54 | // Number of bytes reserved for the nonce proof in public reset packet. |
| 55 | const size_t kPublicResetNonceSize = 8; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 56 | |
| 57 | // Size in bytes of the data or fec packet header. |
| 58 | const size_t kPacketHeaderSize = kQuicGuidSize + kPublicFlagsSize + |
| 59 | kPrivateFlagsSize + kSequenceNumberSize + kFecGroupSize; |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 60 | // Size in bytes of the public reset packet. |
| 61 | const size_t kPublicResetPacketSize = kQuicGuidSize + kPublicFlagsSize + |
| 62 | kPublicResetNonceSize + kSequenceNumberSize; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 63 | |
| 64 | // Index into the guid offset in the header. |
| 65 | const size_t kGuidOffset = 0; |
| 66 | // Index into the flags offset in the header. |
| 67 | const size_t kPublicFlagsOffset = kQuicGuidSize; |
| 68 | |
| 69 | // Index into the sequence number offset in the header. |
| 70 | const size_t kSequenceNumberOffset = kPublicFlagsOffset + kPublicFlagsSize; |
| 71 | // Index into the private flags offset in the data packet header. |
| 72 | const size_t kPrivateFlagsOffset = kSequenceNumberOffset + kSequenceNumberSize; |
| 73 | // Index into the fec group offset in the header. |
| 74 | const size_t kFecGroupOffset = kPrivateFlagsOffset + kPrivateFlagsSize; |
| 75 | |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 76 | // Index into the nonce proof of the public reset packet. |
| 77 | const size_t kPublicResetPacketNonceProofOffset = kPublicFlagsOffset + |
| 78 | kPublicFlagsSize; |
| 79 | // Index into the rejected sequence number of the public reset packet. |
| 80 | const size_t kPublicResetPacketRejectedSequenceNumberOffset = |
| 81 | kPublicResetPacketNonceProofOffset + kPublicResetNonceSize; |
| 82 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 83 | // Index of the first byte in a QUIC packet of FEC protected data. |
| 84 | const size_t kStartOfFecProtectedData = kPacketHeaderSize; |
| 85 | // Index of the first byte in a QUIC packet of encrypted data. |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 86 | const size_t kStartOfEncryptedData = kPacketHeaderSize - kPrivateFlagsSize - |
| 87 | kFecGroupSize; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 88 | // Index of the first byte in a QUIC packet which is hashed. |
| 89 | const size_t kStartOfHashData = 0; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 90 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 91 | // Size in bytes of all stream frame fields. |
| 92 | const size_t kMinStreamFrameLength = 15; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 93 | |
| 94 | // Limit on the delta between stream IDs. |
| 95 | const QuicStreamId kMaxStreamIdDelta = 100; |
| 96 | |
| 97 | // Reserved ID for the crypto stream. |
| 98 | // TODO(rch): ensure that this is not usable by any other streams. |
| 99 | const QuicStreamId kCryptoStreamId = 1; |
| 100 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 101 | // Value which indicates this packet is not FEC protected. |
| 102 | const uint8 kNoFecOffset = 0xFF; |
| 103 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 104 | typedef std::pair<QuicPacketSequenceNumber, QuicPacket*> PacketPair; |
| 105 | |
[email protected] | 2a960e0 | 2012-11-11 14:48:10 | [diff] [blame] | 106 | const int64 kDefaultTimeoutUs = 600000000; // 10 minutes. |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 107 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 108 | enum QuicFrameType { |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 109 | PADDING_FRAME = 0, |
| 110 | STREAM_FRAME, |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 111 | ACK_FRAME, |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 112 | CONGESTION_FEEDBACK_FRAME, |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 113 | RST_STREAM_FRAME, |
| 114 | CONNECTION_CLOSE_FRAME, |
| 115 | NUM_FRAME_TYPES |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 116 | }; |
| 117 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 118 | enum QuicPacketPublicFlags { |
| 119 | PACKET_PUBLIC_FLAGS_NONE = 0, |
| 120 | PACKET_PUBLIC_FLAGS_VERSION = 1, |
| 121 | PACKET_PUBLIC_FLAGS_RST = 2, // Packet is a public reset packet. |
| 122 | PACKET_PUBLIC_FLAGS_MAX = 3 // Both bit set. |
| 123 | }; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 124 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 125 | enum QuicPacketPrivateFlags { |
| 126 | PACKET_PRIVATE_FLAGS_NONE = 0, |
| 127 | PACKET_PRIVATE_FLAGS_FEC = 1, // Payload is FEC as opposed to frames. |
| 128 | PACKET_PRIVATE_FLAGS_MAX = PACKET_PRIVATE_FLAGS_FEC |
| 129 | }; |
| 130 | |
| 131 | enum QuicVersion { |
| 132 | QUIC_VERSION_1 = 0 |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | enum QuicErrorCode { |
| 136 | // Stream errors. |
| 137 | QUIC_NO_ERROR = 0, |
| 138 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 139 | // There were data frames after the a fin or reset. |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 140 | QUIC_STREAM_DATA_AFTER_TERMINATION, |
| 141 | // There was some server error which halted stream processing. |
| 142 | QUIC_SERVER_ERROR_PROCESSING_STREAM, |
| 143 | // We got two fin or reset offsets which did not match. |
| 144 | QUIC_MULTIPLE_TERMINATION_OFFSETS, |
| 145 | // We got bad payload and can not respond to it at the protocol level. |
| 146 | QUIC_BAD_APPLICATION_PAYLOAD, |
| 147 | |
| 148 | // Connection errors. |
| 149 | |
| 150 | // Control frame is malformed. |
| 151 | QUIC_INVALID_PACKET_HEADER, |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 152 | // Frame data is malformed. |
| 153 | QUIC_INVALID_FRAME_DATA, |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 154 | // FEC data is malformed. |
| 155 | QUIC_INVALID_FEC_DATA, |
| 156 | // Stream rst data is malformed |
| 157 | QUIC_INVALID_RST_STREAM_DATA, |
| 158 | // Connection close data is malformed. |
| 159 | QUIC_INVALID_CONNECTION_CLOSE_DATA, |
| 160 | // Ack data is malformed. |
| 161 | QUIC_INVALID_ACK_DATA, |
| 162 | // There was an error decrypting. |
| 163 | QUIC_DECRYPTION_FAILURE, |
| 164 | // There was an error encrypting. |
| 165 | QUIC_ENCRYPTION_FAILURE, |
| 166 | // The packet exceeded kMaxPacketSize. |
| 167 | QUIC_PACKET_TOO_LARGE, |
| 168 | // Data was sent for a stream which did not exist. |
| 169 | QUIC_PACKET_FOR_NONEXISTENT_STREAM, |
| 170 | // The client is going away (browser close, etc.) |
| 171 | QUIC_CLIENT_GOING_AWAY, |
| 172 | // The server is going away (restart etc.) |
| 173 | QUIC_SERVER_GOING_AWAY, |
| 174 | // A stream ID was invalid. |
| 175 | QUIC_INVALID_STREAM_ID, |
| 176 | // Too many streams already open. |
| 177 | QUIC_TOO_MANY_OPEN_STREAMS, |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 178 | // Received public reset for this connection. |
| 179 | QUIC_PUBLIC_RESET, |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 180 | |
| 181 | // We hit our prenegotiated (or default) timeout |
| 182 | QUIC_CONNECTION_TIMED_OUT, |
| 183 | |
| 184 | // Crypto errors. |
| 185 | |
| 186 | // Handshake message contained out of order tags. |
| 187 | QUIC_CRYPTO_TAGS_OUT_OF_ORDER, |
[email protected] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 188 | // Handshake message contained too many entries. |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 189 | QUIC_CRYPTO_TOO_MANY_ENTRIES, |
| 190 | // Handshake message contained an invalid value length. |
| 191 | QUIC_CRYPTO_INVALID_VALUE_LENGTH, |
| 192 | // A crypto message was received after the handshake was complete. |
| 193 | QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE, |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame^] | 194 | // A crypto message was received with an illegal message tag. |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 195 | QUIC_INVALID_CRYPTO_MESSAGE_TYPE, |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame^] | 196 | // A crypto message was received with an illegal parameter. |
| 197 | QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER, |
| 198 | // A crypto message was received with a mandatory parameter missing. |
| 199 | QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND, |
| 200 | // A crypto message was received with a parameter that has no overlap |
| 201 | // with the local parameter. |
| 202 | QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP, |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 203 | }; |
| 204 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 205 | struct NET_EXPORT_PRIVATE QuicPacketPublicHeader { |
| 206 | // Universal header. All QuicPacket headers will have a guid and public flags. |
| 207 | // TODO(satyamshekhar): Support versioning as per Protocol Negotiation Doc. |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 208 | QuicGuid guid; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 209 | QuicPacketPublicFlags flags; |
| 210 | }; |
| 211 | |
| 212 | // Header for Data or FEC packets. |
| 213 | struct QuicPacketHeader { |
| 214 | QuicPacketHeader() {} |
| 215 | explicit QuicPacketHeader(const QuicPacketPublicHeader& header) |
| 216 | : public_header(header) {} |
| 217 | QuicPacketPublicHeader public_header; |
| 218 | QuicPacketPrivateFlags private_flags; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 219 | QuicPacketSequenceNumber packet_sequence_number; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 220 | QuicFecGroupNumber fec_group; |
| 221 | }; |
| 222 | |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 223 | struct QuicPublicResetPacket { |
| 224 | QuicPublicResetPacket() {} |
| 225 | explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header) |
| 226 | : public_header(header) {} |
| 227 | QuicPacketPublicHeader public_header; |
| 228 | QuicPacketSequenceNumber rejected_sequence_number; |
| 229 | QuicPublicResetNonceProof nonce_proof; |
| 230 | }; |
| 231 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 232 | // A padding frame contains no payload. |
| 233 | struct NET_EXPORT_PRIVATE QuicPaddingFrame { |
| 234 | }; |
| 235 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 236 | struct NET_EXPORT_PRIVATE QuicStreamFrame { |
| 237 | QuicStreamFrame(); |
| 238 | QuicStreamFrame(QuicStreamId stream_id, |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 239 | bool fin, |
[email protected] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 240 | QuicStreamOffset offset, |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 241 | base::StringPiece data); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 242 | |
| 243 | QuicStreamId stream_id; |
| 244 | bool fin; |
[email protected] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 245 | QuicStreamOffset offset; // Location of this data in the stream. |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 246 | base::StringPiece data; |
| 247 | }; |
| 248 | |
[email protected] | e537f74 | 2012-12-07 15:33:53 | [diff] [blame] | 249 | // TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing |
| 250 | // is finalized. |
| 251 | typedef std::set<QuicPacketSequenceNumber> SequenceSet; |
[email protected] | 3c5f4a73 | 2013-01-12 16:45:34 | [diff] [blame] | 252 | // TODO(pwestin): Add a way to enforce the max size of this map. |
[email protected] | e537f74 | 2012-12-07 15:33:53 | [diff] [blame] | 253 | typedef std::map<QuicPacketSequenceNumber, QuicTime> TimeMap; |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 254 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 255 | struct NET_EXPORT_PRIVATE ReceivedPacketInfo { |
| 256 | ReceivedPacketInfo(); |
| 257 | ~ReceivedPacketInfo(); |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 258 | NET_EXPORT_PRIVATE friend std::ostream& operator<<( |
| 259 | std::ostream& os, const ReceivedPacketInfo& s); |
[email protected] | a674b4c | 2012-12-05 03:44:30 | [diff] [blame] | 260 | |
[email protected] | e537f74 | 2012-12-07 15:33:53 | [diff] [blame] | 261 | // Records a packet receipt. |
[email protected] | 7884ecad | 2012-12-14 22:55:15 | [diff] [blame] | 262 | void RecordReceived(QuicPacketSequenceNumber sequence_number); |
[email protected] | e537f74 | 2012-12-07 15:33:53 | [diff] [blame] | 263 | |
[email protected] | 48697d8a | 2013-01-15 19:42:24 | [diff] [blame] | 264 | // True if the sequence number is greater than largest_observed or is listed |
[email protected] | e537f74 | 2012-12-07 15:33:53 | [diff] [blame] | 265 | // as missing. |
| 266 | // Always returns false for sequence numbers less than least_unacked. |
| 267 | bool IsAwaitingPacket(QuicPacketSequenceNumber sequence_number) const; |
| 268 | |
[email protected] | 7884ecad | 2012-12-14 22:55:15 | [diff] [blame] | 269 | // Clears all missing packets less than |least_unacked|. |
| 270 | void ClearMissingBefore(QuicPacketSequenceNumber least_unacked); |
[email protected] | e537f74 | 2012-12-07 15:33:53 | [diff] [blame] | 271 | |
[email protected] | 48697d8a | 2013-01-15 19:42:24 | [diff] [blame] | 272 | // The highest packet sequence number we've observed from the peer. |
| 273 | // |
| 274 | // In general, this should be the largest packet number we've received. In |
| 275 | // the case of truncated acks, we may have to advertise a lower "upper bound" |
| 276 | // than largest received, to avoid implicitly acking missing packets that |
| 277 | // don't fit in the missing packet list due to size limitations. In this |
| 278 | // case, largest_observed may be a packet which is also in the missing packets |
| 279 | // list. |
| 280 | QuicPacketSequenceNumber largest_observed; |
[email protected] | a674b4c | 2012-12-05 03:44:30 | [diff] [blame] | 281 | |
[email protected] | e537f74 | 2012-12-07 15:33:53 | [diff] [blame] | 282 | // The set of packets which we're expecting and have not received. |
| 283 | SequenceSet missing_packets; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | struct NET_EXPORT_PRIVATE SentPacketInfo { |
| 287 | SentPacketInfo(); |
| 288 | ~SentPacketInfo(); |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 289 | NET_EXPORT_PRIVATE friend std::ostream& operator<<( |
| 290 | std::ostream& os, const SentPacketInfo& s); |
| 291 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 292 | // The lowest packet we've sent which is unacked, and we expect an ack for. |
| 293 | QuicPacketSequenceNumber least_unacked; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 294 | }; |
| 295 | |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 296 | struct NET_EXPORT_PRIVATE QuicAckFrame { |
| 297 | QuicAckFrame() {} |
| 298 | // Testing convenience method to construct a QuicAckFrame with all packets |
[email protected] | 48697d8a | 2013-01-15 19:42:24 | [diff] [blame] | 299 | // from least_unacked to largest_observed acked. |
| 300 | QuicAckFrame(QuicPacketSequenceNumber largest_observed, |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 301 | QuicPacketSequenceNumber least_unacked); |
| 302 | |
| 303 | NET_EXPORT_PRIVATE friend std::ostream& operator<<( |
| 304 | std::ostream& os, const QuicAckFrame& s); |
| 305 | |
| 306 | SentPacketInfo sent_info; |
| 307 | ReceivedPacketInfo received_info; |
| 308 | }; |
| 309 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 310 | // Defines for all types of congestion feedback that will be negotiated in QUIC, |
| 311 | // kTCP MUST be supported by all QUIC implementations to guarentee 100% |
| 312 | // compatibility. |
| 313 | enum CongestionFeedbackType { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 314 | kTCP, // Used to mimic TCP. |
| 315 | kInterArrival, // Use additional inter arrival information. |
| 316 | kFixRate, // Provided for testing. |
| 317 | }; |
| 318 | |
| 319 | struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP { |
| 320 | uint16 accumulated_number_of_lost_packets; |
| 321 | uint16 receive_window; // Number of bytes >> 4. |
| 322 | }; |
| 323 | |
| 324 | struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival { |
[email protected] | 7884ecad | 2012-12-14 22:55:15 | [diff] [blame] | 325 | CongestionFeedbackMessageInterArrival(); |
| 326 | ~CongestionFeedbackMessageInterArrival(); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 327 | uint16 accumulated_number_of_lost_packets; |
[email protected] | 7884ecad | 2012-12-14 22:55:15 | [diff] [blame] | 328 | // The set of received packets since the last feedback was sent, along with |
| 329 | // their arrival times. |
| 330 | TimeMap received_packet_times; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 331 | }; |
| 332 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 333 | struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate { |
| 334 | uint32 bitrate_in_bytes_per_second; |
| 335 | }; |
| 336 | |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 337 | struct NET_EXPORT_PRIVATE QuicCongestionFeedbackFrame { |
[email protected] | 7884ecad | 2012-12-14 22:55:15 | [diff] [blame] | 338 | QuicCongestionFeedbackFrame(); |
| 339 | ~QuicCongestionFeedbackFrame(); |
| 340 | |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 341 | NET_EXPORT_PRIVATE friend std::ostream& operator<<( |
| 342 | std::ostream& os, const QuicCongestionFeedbackFrame& c); |
| 343 | |
[email protected] | 7884ecad | 2012-12-14 22:55:15 | [diff] [blame] | 344 | CongestionFeedbackType type; |
| 345 | // This should really be a union, but since the inter arrival struct |
| 346 | // is non-trivial, C++ prohibits it. |
| 347 | CongestionFeedbackMessageTCP tcp; |
| 348 | CongestionFeedbackMessageInterArrival inter_arrival; |
| 349 | CongestionFeedbackMessageFixRate fix_rate; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 350 | }; |
| 351 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 352 | struct NET_EXPORT_PRIVATE QuicRstStreamFrame { |
| 353 | QuicRstStreamFrame() {} |
| 354 | QuicRstStreamFrame(QuicStreamId stream_id, uint64 offset, |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 355 | QuicErrorCode error_code) |
[email protected] | 431bb4fd | 2012-10-19 17:46:09 | [diff] [blame] | 356 | : stream_id(stream_id), offset(offset), error_code(error_code) { |
| 357 | DCHECK_LE(error_code, std::numeric_limits<uint8>::max()); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | QuicStreamId stream_id; |
| 361 | uint64 offset; |
[email protected] | 431bb4fd | 2012-10-19 17:46:09 | [diff] [blame] | 362 | QuicErrorCode error_code; |
| 363 | std::string error_details; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 364 | }; |
| 365 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 366 | struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame { |
[email protected] | 431bb4fd | 2012-10-19 17:46:09 | [diff] [blame] | 367 | QuicErrorCode error_code; |
[email protected] | 431bb4fd | 2012-10-19 17:46:09 | [diff] [blame] | 368 | std::string error_details; |
[email protected] | d3d15bf | 2013-01-30 02:51:54 | [diff] [blame^] | 369 | QuicAckFrame ack_frame; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 370 | }; |
| 371 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 372 | struct NET_EXPORT_PRIVATE QuicFrame { |
| 373 | QuicFrame() {} |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 374 | explicit QuicFrame(QuicPaddingFrame* padding_frame) |
| 375 | : type(PADDING_FRAME), |
| 376 | padding_frame(padding_frame) { |
| 377 | } |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 378 | explicit QuicFrame(QuicStreamFrame* stream_frame) |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 379 | : type(STREAM_FRAME), |
| 380 | stream_frame(stream_frame) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 381 | } |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 382 | explicit QuicFrame(QuicAckFrame* frame) |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 383 | : type(ACK_FRAME), |
| 384 | ack_frame(frame) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 385 | } |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 386 | explicit QuicFrame(QuicCongestionFeedbackFrame* frame) |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 387 | : type(CONGESTION_FEEDBACK_FRAME), |
| 388 | congestion_feedback_frame(frame) { |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 389 | } |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 390 | explicit QuicFrame(QuicRstStreamFrame* frame) |
| 391 | : type(RST_STREAM_FRAME), |
| 392 | rst_stream_frame(frame) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 393 | } |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 394 | explicit QuicFrame(QuicConnectionCloseFrame* frame) |
| 395 | : type(CONNECTION_CLOSE_FRAME), |
| 396 | connection_close_frame(frame) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 397 | } |
| 398 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 399 | QuicFrameType type; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 400 | union { |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 401 | QuicPaddingFrame* padding_frame; |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 402 | QuicStreamFrame* stream_frame; |
| 403 | QuicAckFrame* ack_frame; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 404 | QuicCongestionFeedbackFrame* congestion_feedback_frame; |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 405 | QuicRstStreamFrame* rst_stream_frame; |
| 406 | QuicConnectionCloseFrame* connection_close_frame; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 407 | }; |
| 408 | }; |
| 409 | |
[email protected] | be24ab2 | 2012-10-22 03:01:52 | [diff] [blame] | 410 | typedef std::vector<QuicFrame> QuicFrames; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 411 | |
| 412 | struct NET_EXPORT_PRIVATE QuicFecData { |
| 413 | QuicFecData(); |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 414 | |
| 415 | bool operator==(const QuicFecData& other) const; |
| 416 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 417 | // The FEC group number is also the sequence number of the first |
| 418 | // FEC protected packet. The last protected packet's sequence number will |
| 419 | // be one less than the sequence number of the FEC packet. |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 420 | QuicFecGroupNumber fec_group; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 421 | QuicPacketSequenceNumber min_protected_packet_sequence_number; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 422 | // The last protected packet's sequence number will be one |
| 423 | // less than the sequence number of the FEC packet. |
| 424 | base::StringPiece redundancy; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 425 | }; |
| 426 | |
| 427 | struct NET_EXPORT_PRIVATE QuicPacketData { |
| 428 | std::string data; |
| 429 | }; |
| 430 | |
| 431 | class NET_EXPORT_PRIVATE QuicData { |
| 432 | public: |
| 433 | QuicData(const char* buffer, size_t length) |
| 434 | : buffer_(buffer), |
| 435 | length_(length), |
| 436 | owns_buffer_(false) { } |
| 437 | |
| 438 | QuicData(char* buffer, size_t length, bool owns_buffer) |
| 439 | : buffer_(buffer), |
| 440 | length_(length), |
| 441 | owns_buffer_(owns_buffer) { } |
| 442 | |
| 443 | virtual ~QuicData(); |
| 444 | |
| 445 | base::StringPiece AsStringPiece() const { |
| 446 | return base::StringPiece(data(), length()); |
| 447 | } |
| 448 | |
| 449 | const char* data() const { return buffer_; } |
| 450 | size_t length() const { return length_; } |
| 451 | |
| 452 | private: |
| 453 | const char* buffer_; |
| 454 | size_t length_; |
| 455 | bool owns_buffer_; |
| 456 | |
| 457 | DISALLOW_COPY_AND_ASSIGN(QuicData); |
| 458 | }; |
| 459 | |
| 460 | class NET_EXPORT_PRIVATE QuicPacket : public QuicData { |
| 461 | public: |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 462 | static QuicPacket* NewDataPacket(char* buffer, |
| 463 | size_t length, |
| 464 | bool owns_buffer) { |
| 465 | return new QuicPacket(buffer, length, owns_buffer, false); |
| 466 | } |
| 467 | |
| 468 | static QuicPacket* NewFecPacket(char* buffer, |
| 469 | size_t length, |
| 470 | bool owns_buffer) { |
| 471 | return new QuicPacket(buffer, length, owns_buffer, true); |
| 472 | } |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 473 | |
| 474 | base::StringPiece FecProtectedData() const { |
| 475 | return base::StringPiece(data() + kStartOfFecProtectedData, |
| 476 | length() - kStartOfFecProtectedData); |
| 477 | } |
| 478 | |
| 479 | base::StringPiece AssociatedData() const { |
| 480 | return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData); |
| 481 | } |
| 482 | |
| 483 | base::StringPiece Plaintext() const { |
| 484 | return base::StringPiece(data() + kStartOfEncryptedData, |
| 485 | length() - kStartOfEncryptedData); |
| 486 | } |
[email protected] | 082b65b | 2012-11-10 19:11:31 | [diff] [blame] | 487 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 488 | bool is_fec_packet() const { return is_fec_packet_; } |
[email protected] | 082b65b | 2012-11-10 19:11:31 | [diff] [blame] | 489 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 490 | char* mutable_data() { return buffer_; } |
| 491 | |
| 492 | private: |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 493 | QuicPacket(char* buffer, size_t length, bool owns_buffer, bool is_fec_packet) |
| 494 | : QuicData(buffer, length, owns_buffer), |
| 495 | buffer_(buffer), |
| 496 | is_fec_packet_(is_fec_packet) { } |
| 497 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 498 | char* buffer_; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 499 | const bool is_fec_packet_; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 500 | |
[email protected] | 2e740db | 2012-10-20 19:35:19 | [diff] [blame] | 501 | DISALLOW_COPY_AND_ASSIGN(QuicPacket); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData { |
| 505 | public: |
| 506 | QuicEncryptedPacket(const char* buffer, size_t length) |
| 507 | : QuicData(buffer, length) { } |
| 508 | |
| 509 | QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer) |
| 510 | : QuicData(buffer, length, owns_buffer) { } |
| 511 | |
[email protected] | c1b32c6 | 2013-01-20 02:49:10 | [diff] [blame] | 512 | // By default, gtest prints the raw bytes of an object. The bool data |
| 513 | // member (in the base class QuicData) causes this object to have padding |
| 514 | // bytes, which causes the default gtest object printer to read |
| 515 | // uninitialize memory. So we need to teach gtest how to print this object. |
| 516 | NET_EXPORT_PRIVATE friend std::ostream& operator<<( |
| 517 | std::ostream& os, const QuicEncryptedPacket& s); |
| 518 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 519 | base::StringPiece AssociatedData() const { |
| 520 | return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData); |
| 521 | } |
| 522 | |
[email protected] | 2e740db | 2012-10-20 19:35:19 | [diff] [blame] | 523 | private: |
| 524 | DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 525 | }; |
| 526 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 527 | // A struct for functions which consume data payloads and fins. |
| 528 | // The first member of the pair indicates bytes consumed. |
| 529 | // The second member of the pair indicates if an incoming fin was consumed. |
| 530 | struct QuicConsumedData { |
| 531 | QuicConsumedData(size_t bytes_consumed, bool fin_consumed) |
| 532 | : bytes_consumed(bytes_consumed), |
| 533 | fin_consumed(fin_consumed) { |
| 534 | } |
[email protected] | c1b32c6 | 2013-01-20 02:49:10 | [diff] [blame] | 535 | |
| 536 | // By default, gtest prints the raw bytes of an object. The bool data |
| 537 | // member causes this object to have padding bytes, which causes the |
| 538 | // default gtest object printer to read uninitialize memory. So we need |
| 539 | // to teach gtest how to print this object. |
| 540 | NET_EXPORT_PRIVATE friend std::ostream& operator<<( |
| 541 | std::ostream& os, const QuicConsumedData& s); |
| 542 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 543 | size_t bytes_consumed; |
| 544 | bool fin_consumed; |
| 545 | }; |
| 546 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 547 | } // namespace net |
| 548 | |
| 549 | #endif // NET_QUIC_QUIC_PROTOCOL_H_ |