blob: 04fbd9142191989f7588d6b1604ccbedf2a8a6d9 [file] [log] [blame]
[email protected]8b37a092012-10-18 21:53:491// 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]e537f742012-12-07 15:33:538#include <stddef.h>
[email protected]8b37a092012-10-18 21:53:499#include <limits>
[email protected]a674b4c2012-12-05 03:44:3010#include <map>
[email protected]5640d0a2012-10-22 18:17:0211#include <ostream>
[email protected]e537f742012-12-07 15:33:5312#include <set>
13#include <string>
[email protected]8b37a092012-10-18 21:53:4914#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]165e0752012-11-16 07:49:4421#include "net/base/int128.h"
[email protected]8b37a092012-10-18 21:53:4922#include "net/base/net_export.h"
[email protected]2a960e02012-11-11 14:48:1023#include "net/quic/quic_time.h"
[email protected]8b37a092012-10-18 21:53:4924
25namespace net {
26
[email protected]f1e97e92012-12-16 04:53:2527using ::operator<<;
28
[email protected]8b37a092012-10-18 21:53:4929class QuicPacket;
30
31typedef uint64 QuicGuid;
32typedef uint32 QuicStreamId;
33typedef uint64 QuicStreamOffset;
34typedef uint64 QuicPacketSequenceNumber;
[email protected]8b37a092012-10-18 21:53:4935typedef uint8 QuicFecGroupNumber;
36
[email protected]cff7b7b2013-01-11 08:49:0737// A struct for functions which consume data payloads and fins.
38// The first member of the pair indicates bytes consumed.
39// The second member of the pair indicates if an incoming fin was consumed.
40struct QuicConsumedData {
41 QuicConsumedData(size_t bytes_consumed, bool fin_consumed)
42 : bytes_consumed(bytes_consumed),
43 fin_consumed(fin_consumed) {
44 }
45 size_t bytes_consumed;
46 bool fin_consumed;
47};
48
49
[email protected]8b37a092012-10-18 21:53:4950// TODO(rch): Consider Quic specific names for these constants.
51const size_t kMaxPacketSize = 1200; // Maximum size in bytes of a QUIC packet.
52
53// Maximum number of open streams per connection.
54const size_t kDefaultMaxStreamsPerConnection = 100;
55
56// Size in bytes of the packet header common across all packets.
[email protected]05e845d2012-11-11 16:39:2457const size_t kPacketHeaderSize = 16;
[email protected]8b37a092012-10-18 21:53:4958// Index of the first byte in a QUIC packet of FEC protected data.
59const size_t kStartOfFecProtectedData = kPacketHeaderSize;
60// Index of the first byte in a QUIC packet of encrypted data.
61const size_t kStartOfEncryptedData = kPacketHeaderSize - 1;
62// Index of the first byte in a QUIC packet which is hashed.
63const size_t kStartOfHashData = 0;
[email protected]082b65b2012-11-10 19:11:3164// Index into the sequence number offset in the header.
65const int kSequenceNumberOffset = 8;
[email protected]082b65b2012-11-10 19:11:3166// Index into the flags offset in the header.
[email protected]05e845d2012-11-11 16:39:2467const int kFlagsOffset = 14;
[email protected]082b65b2012-11-10 19:11:3168// Index into the fec group offset in the header.
[email protected]05e845d2012-11-11 16:39:2469const int kFecGroupOffset = 15;
[email protected]8b37a092012-10-18 21:53:4970
[email protected]be24ab22012-10-22 03:01:5271// Size in bytes of all stream frame fields.
72const size_t kMinStreamFrameLength = 15;
[email protected]8b37a092012-10-18 21:53:4973
74// Limit on the delta between stream IDs.
75const QuicStreamId kMaxStreamIdDelta = 100;
76
77// Reserved ID for the crypto stream.
78// TODO(rch): ensure that this is not usable by any other streams.
79const QuicStreamId kCryptoStreamId = 1;
80
81typedef std::pair<QuicPacketSequenceNumber, QuicPacket*> PacketPair;
82
[email protected]2a960e02012-11-11 14:48:1083const int64 kDefaultTimeoutUs = 600000000; // 10 minutes.
[email protected]8b37a092012-10-18 21:53:4984
[email protected]be24ab22012-10-22 03:01:5285enum QuicFrameType {
86 STREAM_FRAME = 0,
87 PDU_FRAME,
88 ACK_FRAME,
[email protected]26f3f8e2012-12-13 21:07:1989 CONGESTION_FEEDBACK_FRAME,
[email protected]be24ab22012-10-22 03:01:5290 RST_STREAM_FRAME,
91 CONNECTION_CLOSE_FRAME,
92 NUM_FRAME_TYPES
[email protected]8b37a092012-10-18 21:53:4993};
94
95enum QuicPacketFlags {
96 PACKET_FLAGS_NONE = 0,
[email protected]be24ab22012-10-22 03:01:5297 PACKET_FLAGS_FEC = 1, // Payload is FEC as opposed to frames.
[email protected]8b37a092012-10-18 21:53:4998
99 PACKET_FLAGS_MAX = PACKET_FLAGS_FEC
100};
101
102enum QuicErrorCode {
103 // Stream errors.
104 QUIC_NO_ERROR = 0,
105
[email protected]be24ab22012-10-22 03:01:52106 // There were data frames after the a fin or reset.
[email protected]8b37a092012-10-18 21:53:49107 QUIC_STREAM_DATA_AFTER_TERMINATION,
108 // There was some server error which halted stream processing.
109 QUIC_SERVER_ERROR_PROCESSING_STREAM,
110 // We got two fin or reset offsets which did not match.
111 QUIC_MULTIPLE_TERMINATION_OFFSETS,
112 // We got bad payload and can not respond to it at the protocol level.
113 QUIC_BAD_APPLICATION_PAYLOAD,
114
115 // Connection errors.
116
117 // Control frame is malformed.
118 QUIC_INVALID_PACKET_HEADER,
[email protected]be24ab22012-10-22 03:01:52119 // Frame data is malformed.
120 QUIC_INVALID_FRAME_DATA,
[email protected]8b37a092012-10-18 21:53:49121 // FEC data is malformed.
122 QUIC_INVALID_FEC_DATA,
123 // Stream rst data is malformed
124 QUIC_INVALID_RST_STREAM_DATA,
125 // Connection close data is malformed.
126 QUIC_INVALID_CONNECTION_CLOSE_DATA,
127 // Ack data is malformed.
128 QUIC_INVALID_ACK_DATA,
129 // There was an error decrypting.
130 QUIC_DECRYPTION_FAILURE,
131 // There was an error encrypting.
132 QUIC_ENCRYPTION_FAILURE,
133 // The packet exceeded kMaxPacketSize.
134 QUIC_PACKET_TOO_LARGE,
135 // Data was sent for a stream which did not exist.
136 QUIC_PACKET_FOR_NONEXISTENT_STREAM,
137 // The client is going away (browser close, etc.)
138 QUIC_CLIENT_GOING_AWAY,
139 // The server is going away (restart etc.)
140 QUIC_SERVER_GOING_AWAY,
141 // A stream ID was invalid.
142 QUIC_INVALID_STREAM_ID,
143 // Too many streams already open.
144 QUIC_TOO_MANY_OPEN_STREAMS,
145
146 // We hit our prenegotiated (or default) timeout
147 QUIC_CONNECTION_TIMED_OUT,
148
149 // Crypto errors.
150
151 // Handshake message contained out of order tags.
152 QUIC_CRYPTO_TAGS_OUT_OF_ORDER,
153 // Handshake message contained too many entires.
154 QUIC_CRYPTO_TOO_MANY_ENTRIES,
155 // Handshake message contained an invalid value length.
156 QUIC_CRYPTO_INVALID_VALUE_LENGTH,
157 // A crypto message was received after the handshake was complete.
158 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE,
159 // A crypto message was receieved with an illegal message tag.
160 QUIC_INVALID_CRYPTO_MESSAGE_TYPE,
161
162};
163
164struct NET_EXPORT_PRIVATE QuicPacketHeader {
165 // Includes the ConnectionHeader and CongestionMonitoredHeader
166 // from the design docs, as well as some elements of DecryptedData.
167 QuicGuid guid;
168 QuicPacketSequenceNumber packet_sequence_number;
[email protected]8b37a092012-10-18 21:53:49169 QuicPacketFlags flags;
170 QuicFecGroupNumber fec_group;
171};
172
[email protected]be24ab22012-10-22 03:01:52173struct NET_EXPORT_PRIVATE QuicStreamFrame {
174 QuicStreamFrame();
175 QuicStreamFrame(QuicStreamId stream_id,
[email protected]a5061242012-10-23 23:29:37176 bool fin,
177 uint64 offset,
178 base::StringPiece data);
[email protected]8b37a092012-10-18 21:53:49179
180 QuicStreamId stream_id;
181 bool fin;
182 uint64 offset;
183 base::StringPiece data;
184};
185
[email protected]e537f742012-12-07 15:33:53186// TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing
187// is finalized.
188typedef std::set<QuicPacketSequenceNumber> SequenceSet;
[email protected]3c5f4a732013-01-12 16:45:34189// TODO(pwestin): Add a way to enforce the max size of this map.
[email protected]e537f742012-12-07 15:33:53190typedef std::map<QuicPacketSequenceNumber, QuicTime> TimeMap;
[email protected]044ac2b2012-11-13 21:41:06191
[email protected]8b37a092012-10-18 21:53:49192struct NET_EXPORT_PRIVATE ReceivedPacketInfo {
193 ReceivedPacketInfo();
194 ~ReceivedPacketInfo();
[email protected]26f3f8e2012-12-13 21:07:19195 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
196 std::ostream& os, const ReceivedPacketInfo& s);
[email protected]a674b4c2012-12-05 03:44:30197
[email protected]e537f742012-12-07 15:33:53198 // Records a packet receipt.
[email protected]7884ecad2012-12-14 22:55:15199 void RecordReceived(QuicPacketSequenceNumber sequence_number);
[email protected]e537f742012-12-07 15:33:53200
201 // True if the sequence number is greater than largest_received or is listed
202 // as missing.
203 // Always returns false for sequence numbers less than least_unacked.
204 bool IsAwaitingPacket(QuicPacketSequenceNumber sequence_number) const;
205
[email protected]7884ecad2012-12-14 22:55:15206 // Clears all missing packets less than |least_unacked|.
207 void ClearMissingBefore(QuicPacketSequenceNumber least_unacked);
[email protected]e537f742012-12-07 15:33:53208
[email protected]8b37a092012-10-18 21:53:49209 // The highest packet sequence number we've received from the peer.
210 QuicPacketSequenceNumber largest_received;
[email protected]a674b4c2012-12-05 03:44:30211
[email protected]e537f742012-12-07 15:33:53212 // The set of packets which we're expecting and have not received.
213 SequenceSet missing_packets;
[email protected]8b37a092012-10-18 21:53:49214};
215
216struct NET_EXPORT_PRIVATE SentPacketInfo {
217 SentPacketInfo();
218 ~SentPacketInfo();
[email protected]26f3f8e2012-12-13 21:07:19219 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
220 std::ostream& os, const SentPacketInfo& s);
221
[email protected]8b37a092012-10-18 21:53:49222 // The lowest packet we've sent which is unacked, and we expect an ack for.
223 QuicPacketSequenceNumber least_unacked;
[email protected]8b37a092012-10-18 21:53:49224};
225
[email protected]26f3f8e2012-12-13 21:07:19226struct NET_EXPORT_PRIVATE QuicAckFrame {
227 QuicAckFrame() {}
228 // Testing convenience method to construct a QuicAckFrame with all packets
[email protected]7884ecad2012-12-14 22:55:15229 // from least_unacked to largest_received acked.
[email protected]26f3f8e2012-12-13 21:07:19230 QuicAckFrame(QuicPacketSequenceNumber largest_received,
[email protected]26f3f8e2012-12-13 21:07:19231 QuicPacketSequenceNumber least_unacked);
232
233 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
234 std::ostream& os, const QuicAckFrame& s);
235
236 SentPacketInfo sent_info;
237 ReceivedPacketInfo received_info;
238};
239
[email protected]8b37a092012-10-18 21:53:49240// Defines for all types of congestion feedback that will be negotiated in QUIC,
241// kTCP MUST be supported by all QUIC implementations to guarentee 100%
242// compatibility.
243enum CongestionFeedbackType {
[email protected]8b37a092012-10-18 21:53:49244 kTCP, // Used to mimic TCP.
245 kInterArrival, // Use additional inter arrival information.
246 kFixRate, // Provided for testing.
247};
248
249struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP {
250 uint16 accumulated_number_of_lost_packets;
251 uint16 receive_window; // Number of bytes >> 4.
252};
253
254struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival {
[email protected]7884ecad2012-12-14 22:55:15255 CongestionFeedbackMessageInterArrival();
256 ~CongestionFeedbackMessageInterArrival();
[email protected]8b37a092012-10-18 21:53:49257 uint16 accumulated_number_of_lost_packets;
[email protected]7884ecad2012-12-14 22:55:15258 // The set of received packets since the last feedback was sent, along with
259 // their arrival times.
260 TimeMap received_packet_times;
[email protected]8b37a092012-10-18 21:53:49261};
262
[email protected]8b37a092012-10-18 21:53:49263struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate {
264 uint32 bitrate_in_bytes_per_second;
265};
266
[email protected]26f3f8e2012-12-13 21:07:19267struct NET_EXPORT_PRIVATE QuicCongestionFeedbackFrame {
[email protected]7884ecad2012-12-14 22:55:15268 QuicCongestionFeedbackFrame();
269 ~QuicCongestionFeedbackFrame();
270
[email protected]26f3f8e2012-12-13 21:07:19271 NET_EXPORT_PRIVATE friend std::ostream& operator<<(
272 std::ostream& os, const QuicCongestionFeedbackFrame& c);
273
[email protected]7884ecad2012-12-14 22:55:15274 CongestionFeedbackType type;
275 // This should really be a union, but since the inter arrival struct
276 // is non-trivial, C++ prohibits it.
277 CongestionFeedbackMessageTCP tcp;
278 CongestionFeedbackMessageInterArrival inter_arrival;
279 CongestionFeedbackMessageFixRate fix_rate;
[email protected]8b37a092012-10-18 21:53:49280};
281
[email protected]be24ab22012-10-22 03:01:52282struct NET_EXPORT_PRIVATE QuicRstStreamFrame {
283 QuicRstStreamFrame() {}
284 QuicRstStreamFrame(QuicStreamId stream_id, uint64 offset,
[email protected]f702d572012-12-04 15:56:20285 QuicErrorCode error_code)
[email protected]431bb4fd2012-10-19 17:46:09286 : stream_id(stream_id), offset(offset), error_code(error_code) {
287 DCHECK_LE(error_code, std::numeric_limits<uint8>::max());
[email protected]8b37a092012-10-18 21:53:49288 }
289
290 QuicStreamId stream_id;
291 uint64 offset;
[email protected]431bb4fd2012-10-19 17:46:09292 QuicErrorCode error_code;
293 std::string error_details;
[email protected]8b37a092012-10-18 21:53:49294};
295
[email protected]be24ab22012-10-22 03:01:52296struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame {
[email protected]431bb4fd2012-10-19 17:46:09297 QuicErrorCode error_code;
[email protected]be24ab22012-10-22 03:01:52298 QuicAckFrame ack_frame;
[email protected]431bb4fd2012-10-19 17:46:09299 std::string error_details;
[email protected]8b37a092012-10-18 21:53:49300};
301
[email protected]be24ab22012-10-22 03:01:52302struct NET_EXPORT_PRIVATE QuicFrame {
303 QuicFrame() {}
304 explicit QuicFrame(QuicStreamFrame* stream_frame)
[email protected]610a7e942012-12-18 00:21:39305 : type(STREAM_FRAME),
306 stream_frame(stream_frame) {
[email protected]8b37a092012-10-18 21:53:49307 }
[email protected]be24ab22012-10-22 03:01:52308 explicit QuicFrame(QuicAckFrame* frame)
[email protected]610a7e942012-12-18 00:21:39309 : type(ACK_FRAME),
310 ack_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49311 }
[email protected]26f3f8e2012-12-13 21:07:19312 explicit QuicFrame(QuicCongestionFeedbackFrame* frame)
[email protected]610a7e942012-12-18 00:21:39313 : type(CONGESTION_FEEDBACK_FRAME),
314 congestion_feedback_frame(frame) {
[email protected]26f3f8e2012-12-13 21:07:19315 }
[email protected]be24ab22012-10-22 03:01:52316 explicit QuicFrame(QuicRstStreamFrame* frame)
317 : type(RST_STREAM_FRAME),
318 rst_stream_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49319 }
[email protected]be24ab22012-10-22 03:01:52320 explicit QuicFrame(QuicConnectionCloseFrame* frame)
321 : type(CONNECTION_CLOSE_FRAME),
322 connection_close_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49323 }
324
[email protected]be24ab22012-10-22 03:01:52325 QuicFrameType type;
[email protected]8b37a092012-10-18 21:53:49326 union {
[email protected]be24ab22012-10-22 03:01:52327 QuicStreamFrame* stream_frame;
328 QuicAckFrame* ack_frame;
[email protected]26f3f8e2012-12-13 21:07:19329 QuicCongestionFeedbackFrame* congestion_feedback_frame;
[email protected]be24ab22012-10-22 03:01:52330 QuicRstStreamFrame* rst_stream_frame;
331 QuicConnectionCloseFrame* connection_close_frame;
[email protected]8b37a092012-10-18 21:53:49332 };
333};
334
[email protected]be24ab22012-10-22 03:01:52335typedef std::vector<QuicFrame> QuicFrames;
[email protected]8b37a092012-10-18 21:53:49336
337struct NET_EXPORT_PRIVATE QuicFecData {
338 QuicFecData();
[email protected]a5061242012-10-23 23:29:37339
340 bool operator==(const QuicFecData& other) const;
341
[email protected]8b37a092012-10-18 21:53:49342 QuicFecGroupNumber fec_group;
[email protected]a5061242012-10-23 23:29:37343 QuicPacketSequenceNumber min_protected_packet_sequence_number;
[email protected]8b37a092012-10-18 21:53:49344 // The last protected packet's sequence number will be one
345 // less than the sequence number of the FEC packet.
346 base::StringPiece redundancy;
[email protected]8b37a092012-10-18 21:53:49347};
348
349struct NET_EXPORT_PRIVATE QuicPacketData {
350 std::string data;
351};
352
353class NET_EXPORT_PRIVATE QuicData {
354 public:
355 QuicData(const char* buffer, size_t length)
356 : buffer_(buffer),
357 length_(length),
358 owns_buffer_(false) { }
359
360 QuicData(char* buffer, size_t length, bool owns_buffer)
361 : buffer_(buffer),
362 length_(length),
363 owns_buffer_(owns_buffer) { }
364
365 virtual ~QuicData();
366
367 base::StringPiece AsStringPiece() const {
368 return base::StringPiece(data(), length());
369 }
370
371 const char* data() const { return buffer_; }
372 size_t length() const { return length_; }
373
374 private:
375 const char* buffer_;
376 size_t length_;
377 bool owns_buffer_;
378
379 DISALLOW_COPY_AND_ASSIGN(QuicData);
380};
381
382class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
383 public:
[email protected]082b65b2012-11-10 19:11:31384 QuicPacket(
385 char* buffer, size_t length, bool owns_buffer, QuicPacketFlags flags)
[email protected]8b37a092012-10-18 21:53:49386 : QuicData(buffer, length, owns_buffer),
[email protected]082b65b2012-11-10 19:11:31387 buffer_(buffer),
388 flags_(flags) { }
[email protected]8b37a092012-10-18 21:53:49389
390 base::StringPiece FecProtectedData() const {
391 return base::StringPiece(data() + kStartOfFecProtectedData,
392 length() - kStartOfFecProtectedData);
393 }
394
395 base::StringPiece AssociatedData() const {
396 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
397 }
398
399 base::StringPiece Plaintext() const {
400 return base::StringPiece(data() + kStartOfEncryptedData,
401 length() - kStartOfEncryptedData);
402 }
[email protected]082b65b2012-11-10 19:11:31403
404 bool IsFecPacket() const {
405 return flags_ == PACKET_FLAGS_FEC;
406 }
407
[email protected]8b37a092012-10-18 21:53:49408 char* mutable_data() { return buffer_; }
409
410 private:
411 char* buffer_;
[email protected]082b65b2012-11-10 19:11:31412 const QuicPacketFlags flags_;
[email protected]8b37a092012-10-18 21:53:49413
[email protected]2e740db2012-10-20 19:35:19414 DISALLOW_COPY_AND_ASSIGN(QuicPacket);
[email protected]8b37a092012-10-18 21:53:49415};
416
417class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
418 public:
419 QuicEncryptedPacket(const char* buffer, size_t length)
420 : QuicData(buffer, length) { }
421
422 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer)
423 : QuicData(buffer, length, owns_buffer) { }
424
425 base::StringPiece AssociatedData() const {
426 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
427 }
428
[email protected]2e740db2012-10-20 19:35:19429 private:
430 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket);
[email protected]8b37a092012-10-18 21:53:49431};
432
433} // namespace net
434
435#endif // NET_QUIC_QUIC_PROTOCOL_H_