OLD | NEW |
(Empty) | |
| 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 |
| 8 #include <limits> |
| 9 #include <utility> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/hash_tables.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/string_piece.h" |
| 16 #include "net/base/net_export.h" |
| 17 #include "net/quic/uint128.h" |
| 18 |
| 19 namespace net { |
| 20 |
| 21 class QuicPacket; |
| 22 |
| 23 typedef uint64 QuicGuid; |
| 24 typedef uint32 QuicStreamId; |
| 25 typedef uint64 QuicStreamOffset; |
| 26 typedef uint64 QuicPacketSequenceNumber; |
| 27 typedef uint64 QuicTransmissionTime; |
| 28 typedef uint8 QuicFecGroupNumber; |
| 29 |
| 30 const size_t kMaxPacketSize = 1200; // Maximum size in bytes of a QUIC packet. |
| 31 |
| 32 // Maximum number of open streams per connection. |
| 33 const size_t kDefaultMaxStreamsPerConnection = 100; |
| 34 |
| 35 // Size in bytes of the packet header common across all packets. |
| 36 const size_t kPacketHeaderSize = 25; |
| 37 // Index of the first byte in a QUIC packet of FEC protected data. |
| 38 const size_t kStartOfFecProtectedData = kPacketHeaderSize; |
| 39 // Index of the first byte in a QUIC packet of encrypted data. |
| 40 const size_t kStartOfEncryptedData = kPacketHeaderSize - 1; |
| 41 // Index of the first byte in a QUIC packet which is hashed. |
| 42 const size_t kStartOfHashData = 0; |
| 43 // Index into the retransmission offset in the header. |
| 44 // (After GUID and sequence number.) |
| 45 const int kRetransmissionOffset = 14; |
| 46 // Index into the transmission time offset in the header. |
| 47 const int kTransmissionTimeOffset = 15; |
| 48 |
| 49 // Size in bytes of all stream fragment fields. |
| 50 const size_t kMinStreamFragmentLength = 15; |
| 51 |
| 52 // Limit on the delta between stream IDs. |
| 53 const QuicStreamId kMaxStreamIdDelta = 100; |
| 54 |
| 55 // Reserved ID for the crypto stream. |
| 56 // TODO(rch): ensure that this is not usable by any other streams. |
| 57 const QuicStreamId kCryptoStreamId = 1; |
| 58 |
| 59 typedef std::pair<QuicPacketSequenceNumber, QuicPacket*> PacketPair; |
| 60 |
| 61 const int64 kDefaultTimeout = 600000000; // 10 minutes |
| 62 |
| 63 enum QuicFragmentType { |
| 64 STREAM_FRAGMENT = 0, |
| 65 PDU_FRAGMENT, |
| 66 ACK_FRAGMENT, |
| 67 RST_STREAM_FRAGMENT, |
| 68 CONNECTION_CLOSE_FRAGMENT, |
| 69 NUM_FRAGMENT_TYPES |
| 70 }; |
| 71 |
| 72 enum QuicPacketFlags { |
| 73 PACKET_FLAGS_NONE = 0, |
| 74 PACKET_FLAGS_FEC = 1, // Payload is FEC as opposed to fragments. |
| 75 |
| 76 PACKET_FLAGS_MAX = PACKET_FLAGS_FEC |
| 77 }; |
| 78 |
| 79 enum QuicErrorCode { |
| 80 // Stream errors. |
| 81 QUIC_NO_ERROR = 0, |
| 82 |
| 83 // There were data fragments after the a fin or reset. |
| 84 QUIC_STREAM_DATA_AFTER_TERMINATION, |
| 85 // There was some server error which halted stream processing. |
| 86 QUIC_SERVER_ERROR_PROCESSING_STREAM, |
| 87 // We got two fin or reset offsets which did not match. |
| 88 QUIC_MULTIPLE_TERMINATION_OFFSETS, |
| 89 // We got bad payload and can not respond to it at the protocol level. |
| 90 QUIC_BAD_APPLICATION_PAYLOAD, |
| 91 |
| 92 // Connection errors. |
| 93 |
| 94 // Control frame is malformed. |
| 95 QUIC_INVALID_PACKET_HEADER, |
| 96 // Fragment data is malformed. |
| 97 QUIC_INVALID_FRAGMENT_DATA, |
| 98 // FEC data is malformed. |
| 99 QUIC_INVALID_FEC_DATA, |
| 100 // Stream rst data is malformed |
| 101 QUIC_INVALID_RST_STREAM_DATA, |
| 102 // Connection close data is malformed. |
| 103 QUIC_INVALID_CONNECTION_CLOSE_DATA, |
| 104 // Ack data is malformed. |
| 105 QUIC_INVALID_ACK_DATA, |
| 106 // There was an error decrypting. |
| 107 QUIC_DECRYPTION_FAILURE, |
| 108 // There was an error encrypting. |
| 109 QUIC_ENCRYPTION_FAILURE, |
| 110 // The packet exceeded kMaxPacketSize. |
| 111 QUIC_PACKET_TOO_LARGE, |
| 112 // Data was sent for a stream which did not exist. |
| 113 QUIC_PACKET_FOR_NONEXISTENT_STREAM, |
| 114 // The client is going away (browser close, etc.) |
| 115 QUIC_CLIENT_GOING_AWAY, |
| 116 // The server is going away (restart etc.) |
| 117 QUIC_SERVER_GOING_AWAY, |
| 118 // A stream ID was invalid. |
| 119 QUIC_INVALID_STREAM_ID, |
| 120 // Too many streams already open. |
| 121 QUIC_TOO_MANY_OPEN_STREAMS, |
| 122 |
| 123 // We hit our prenegotiated (or default) timeout |
| 124 QUIC_CONNECTION_TIMED_OUT, |
| 125 |
| 126 // Crypto errors. |
| 127 |
| 128 // Handshake message contained out of order tags. |
| 129 QUIC_CRYPTO_TAGS_OUT_OF_ORDER, |
| 130 // Handshake message contained too many entires. |
| 131 QUIC_CRYPTO_TOO_MANY_ENTRIES, |
| 132 // Handshake message contained an invalid value length. |
| 133 QUIC_CRYPTO_INVALID_VALUE_LENGTH, |
| 134 // A crypto message was received after the handshake was complete. |
| 135 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE, |
| 136 // A crypto message was receieved with an illegal message tag. |
| 137 QUIC_INVALID_CRYPTO_MESSAGE_TYPE, |
| 138 |
| 139 }; |
| 140 |
| 141 struct NET_EXPORT_PRIVATE QuicPacketHeader { |
| 142 // Includes the ConnectionHeader and CongestionMonitoredHeader |
| 143 // from the design docs, as well as some elements of DecryptedData. |
| 144 QuicGuid guid; |
| 145 QuicPacketSequenceNumber packet_sequence_number; |
| 146 uint8 retransmission_count; |
| 147 QuicTransmissionTime transmission_time; |
| 148 QuicPacketFlags flags; |
| 149 QuicFecGroupNumber fec_group; |
| 150 }; |
| 151 |
| 152 struct NET_EXPORT_PRIVATE QuicStreamFragment { |
| 153 QuicStreamFragment(); |
| 154 QuicStreamFragment(QuicStreamId stream_id, |
| 155 bool fin, |
| 156 uint64 offset, |
| 157 base::StringPiece data); |
| 158 |
| 159 QuicStreamId stream_id; |
| 160 bool fin; |
| 161 uint64 offset; |
| 162 base::StringPiece data; |
| 163 }; |
| 164 |
| 165 struct NET_EXPORT_PRIVATE ReceivedPacketInfo { |
| 166 ReceivedPacketInfo(); |
| 167 ~ReceivedPacketInfo(); |
| 168 // The highest packet sequence number we've received from the peer. |
| 169 QuicPacketSequenceNumber largest_received; |
| 170 // The time at which we received the above packet. |
| 171 QuicTransmissionTime time_received; |
| 172 // The set of packets which we're expecting and have not received. |
| 173 // This includes any packets between the lowest and largest_received |
| 174 // which we have neither seen nor been informed are non-retransmitting. |
| 175 base::hash_set<QuicPacketSequenceNumber> missing_packets; |
| 176 }; |
| 177 |
| 178 struct NET_EXPORT_PRIVATE SentPacketInfo { |
| 179 SentPacketInfo(); |
| 180 ~SentPacketInfo(); |
| 181 // The lowest packet we've sent which is unacked, and we expect an ack for. |
| 182 QuicPacketSequenceNumber least_unacked; |
| 183 // The set of packets between least_unacked and the last packet we have sent |
| 184 // which we will not resend. |
| 185 base::hash_set<QuicPacketSequenceNumber> non_retransmiting; |
| 186 }; |
| 187 |
| 188 // Defines for all types of congestion feedback that will be negotiated in QUIC, |
| 189 // kTCP MUST be supported by all QUIC implementations to guarentee 100% |
| 190 // compatibility. |
| 191 enum CongestionFeedbackType { |
| 192 kNone = 0, // No feedback provided |
| 193 kTCP, // Used to mimic TCP. |
| 194 kInterArrival, // Use additional inter arrival information. |
| 195 kFixRate, // Provided for testing. |
| 196 }; |
| 197 |
| 198 struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP { |
| 199 uint16 accumulated_number_of_lost_packets; |
| 200 uint16 receive_window; // Number of bytes >> 4. |
| 201 }; |
| 202 |
| 203 struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival { |
| 204 uint16 accumulated_number_of_lost_packets; |
| 205 int16 offset_time; |
| 206 uint16 delta_time; // delta time is described below. |
| 207 }; |
| 208 |
| 209 /* |
| 210 * Description of delta time. |
| 211 * |
| 212 * 0 1 |
| 213 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 214 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 215 * |D|S| offset_time | |
| 216 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 217 * |
| 218 * Where: |
| 219 * D is the time domain. If set time domain is in milliseconds, else in |
| 220 * microseconds. |
| 221 * S is the sign bit. |
| 222 * offset_time is the time offset where the relative packet size is equal to |
| 223 * 0. |
| 224 */ |
| 225 |
| 226 struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate { |
| 227 uint32 bitrate_in_bytes_per_second; |
| 228 }; |
| 229 |
| 230 struct NET_EXPORT_PRIVATE CongestionInfo { |
| 231 CongestionFeedbackType type; |
| 232 union { |
| 233 CongestionFeedbackMessageTCP tcp; |
| 234 CongestionFeedbackMessageInterArrival inter_arrival; |
| 235 CongestionFeedbackMessageFixRate fix_rate; |
| 236 }; |
| 237 }; |
| 238 |
| 239 struct NET_EXPORT_PRIVATE QuicAckFragment { |
| 240 QuicAckFragment() {} |
| 241 QuicAckFragment(QuicPacketSequenceNumber largest_received, |
| 242 QuicTransmissionTime time_received, |
| 243 QuicPacketSequenceNumber least_unacked) { |
| 244 received_info.largest_received = largest_received; |
| 245 received_info.time_received = time_received; |
| 246 sent_info.least_unacked = least_unacked; |
| 247 congestion_info.type = kNone; |
| 248 } |
| 249 |
| 250 SentPacketInfo sent_info; |
| 251 ReceivedPacketInfo received_info; |
| 252 CongestionInfo congestion_info; |
| 253 |
| 254 friend std::ostream& operator<<(std::ostream& os, const QuicAckFragment& s) { |
| 255 os << "largest_received: " << s.received_info.largest_received |
| 256 << " time: " << s.received_info.time_received |
| 257 << " missing: "; |
| 258 for (base::hash_set<QuicPacketSequenceNumber>::const_iterator it = |
| 259 s.received_info.missing_packets.begin(); |
| 260 it != s.received_info.missing_packets.end(); ++it) { |
| 261 os << *it << " "; |
| 262 } |
| 263 |
| 264 os << " least_waiting: " << s.sent_info.least_unacked |
| 265 << " no_retransmit: "; |
| 266 for (base::hash_set<QuicPacketSequenceNumber>::const_iterator it = |
| 267 s.sent_info.non_retransmiting.begin(); |
| 268 it != s.sent_info.non_retransmiting.end(); ++it) { |
| 269 os << *it << " "; |
| 270 } |
| 271 os << "\n"; |
| 272 return os; |
| 273 } |
| 274 }; |
| 275 |
| 276 struct NET_EXPORT_PRIVATE QuicRstStreamFragment { |
| 277 QuicRstStreamFragment() {} |
| 278 QuicRstStreamFragment(QuicStreamId stream_id, uint64 offset, |
| 279 QuicErrorCode details) |
| 280 : stream_id(stream_id), offset(offset), details(details) { |
| 281 DCHECK_LE(details, std::numeric_limits<uint8>::max()); |
| 282 } |
| 283 |
| 284 QuicStreamId stream_id; |
| 285 uint64 offset; |
| 286 QuicErrorCode details; |
| 287 }; |
| 288 |
| 289 struct NET_EXPORT_PRIVATE QuicConnectionCloseFragment { |
| 290 QuicErrorCode details; |
| 291 QuicAckFragment ack_fragment; |
| 292 }; |
| 293 |
| 294 struct NET_EXPORT_PRIVATE QuicFragment { |
| 295 QuicFragment() {} |
| 296 explicit QuicFragment(QuicStreamFragment* stream_fragment) |
| 297 : type(STREAM_FRAGMENT), stream_fragment(stream_fragment) { |
| 298 } |
| 299 explicit QuicFragment(QuicAckFragment* fragment) |
| 300 : type(ACK_FRAGMENT), ack_fragment(fragment) { |
| 301 } |
| 302 explicit QuicFragment(QuicRstStreamFragment* fragment) |
| 303 : type(RST_STREAM_FRAGMENT), |
| 304 rst_stream_fragment(fragment) { |
| 305 } |
| 306 explicit QuicFragment(QuicConnectionCloseFragment* fragment) |
| 307 : type(CONNECTION_CLOSE_FRAGMENT), |
| 308 connection_close_fragment(fragment) { |
| 309 } |
| 310 |
| 311 QuicFragmentType type; |
| 312 union { |
| 313 QuicStreamFragment* stream_fragment; |
| 314 QuicAckFragment* ack_fragment; |
| 315 QuicRstStreamFragment* rst_stream_fragment; |
| 316 QuicConnectionCloseFragment* connection_close_fragment; |
| 317 }; |
| 318 }; |
| 319 |
| 320 typedef std::vector<QuicFragment> QuicFragments; |
| 321 |
| 322 struct NET_EXPORT_PRIVATE QuicFecData { |
| 323 QuicFecData(); |
| 324 QuicFecGroupNumber fec_group; |
| 325 QuicPacketSequenceNumber first_protected_packet_sequence_number; |
| 326 // The last protected packet's sequence number will be one |
| 327 // less than the sequence number of the FEC packet. |
| 328 base::StringPiece redundancy; |
| 329 bool operator==(const QuicFecData& other) const { |
| 330 if (fec_group != other.fec_group) { |
| 331 return false; |
| 332 } |
| 333 if (first_protected_packet_sequence_number != |
| 334 other.first_protected_packet_sequence_number) { |
| 335 return false; |
| 336 } |
| 337 if (redundancy != other.redundancy) { |
| 338 return false; |
| 339 } |
| 340 return true; |
| 341 } |
| 342 }; |
| 343 |
| 344 struct NET_EXPORT_PRIVATE QuicPacketData { |
| 345 std::string data; |
| 346 }; |
| 347 |
| 348 class NET_EXPORT_PRIVATE QuicData { |
| 349 public: |
| 350 QuicData(const char* buffer, size_t length) |
| 351 : buffer_(buffer), |
| 352 length_(length), |
| 353 owns_buffer_(false) { } |
| 354 |
| 355 QuicData(char* buffer, size_t length, bool owns_buffer) |
| 356 : buffer_(buffer), |
| 357 length_(length), |
| 358 owns_buffer_(owns_buffer) { } |
| 359 |
| 360 virtual ~QuicData(); |
| 361 |
| 362 base::StringPiece AsStringPiece() const { |
| 363 return base::StringPiece(data(), length()); |
| 364 } |
| 365 |
| 366 const char* data() const { return buffer_; } |
| 367 size_t length() const { return length_; } |
| 368 |
| 369 private: |
| 370 const char* buffer_; |
| 371 size_t length_; |
| 372 bool owns_buffer_; |
| 373 |
| 374 DISALLOW_COPY_AND_ASSIGN(QuicData); |
| 375 }; |
| 376 |
| 377 class NET_EXPORT_PRIVATE QuicPacket : public QuicData { |
| 378 public: |
| 379 QuicPacket(char* buffer, size_t length, bool owns_buffer) |
| 380 : QuicData(buffer, length, owns_buffer), |
| 381 buffer_(buffer) { } |
| 382 |
| 383 base::StringPiece FecProtectedData() const { |
| 384 return base::StringPiece(data() + kStartOfFecProtectedData, |
| 385 length() - kStartOfFecProtectedData); |
| 386 } |
| 387 |
| 388 base::StringPiece AssociatedData() const { |
| 389 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData); |
| 390 } |
| 391 |
| 392 base::StringPiece Plaintext() const { |
| 393 return base::StringPiece(data() + kStartOfEncryptedData, |
| 394 length() - kStartOfEncryptedData); |
| 395 } |
| 396 char* mutable_data() { return buffer_; } |
| 397 |
| 398 private: |
| 399 char* buffer_; |
| 400 |
| 401 // TODO(rch): DISALLOW_COPY_AND_ASSIGN |
| 402 }; |
| 403 |
| 404 class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData { |
| 405 public: |
| 406 QuicEncryptedPacket(const char* buffer, size_t length) |
| 407 : QuicData(buffer, length) { } |
| 408 |
| 409 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer) |
| 410 : QuicData(buffer, length, owns_buffer) { } |
| 411 |
| 412 base::StringPiece AssociatedData() const { |
| 413 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData); |
| 414 } |
| 415 |
| 416 // TODO(rch): DISALLOW_COPY_AND_ASSIGN |
| 417 }; |
| 418 |
| 419 } // namespace net |
| 420 |
| 421 #endif // NET_QUIC_QUIC_PROTOCOL_H_ |
OLD | NEW |