blob: 9990facc65fd9b1692fb3fbab115d14ec6d8f2ab [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
8#include <limits>
[email protected]5640d0a2012-10-22 18:17:029#include <ostream>
[email protected]8b37a092012-10-18 21:53:4910#include <utility>
11#include <vector>
12
13#include "base/basictypes.h"
14#include "base/hash_tables.h"
15#include "base/logging.h"
16#include "base/string_piece.h"
17#include "net/base/net_export.h"
18#include "net/quic/uint128.h"
19
20namespace net {
21
22class QuicPacket;
23
24typedef uint64 QuicGuid;
25typedef uint32 QuicStreamId;
26typedef uint64 QuicStreamOffset;
27typedef uint64 QuicPacketSequenceNumber;
28typedef uint64 QuicTransmissionTime;
29typedef uint8 QuicFecGroupNumber;
30
31// TODO(rch): Consider Quic specific names for these constants.
32const size_t kMaxPacketSize = 1200; // Maximum size in bytes of a QUIC packet.
33
34// Maximum number of open streams per connection.
35const size_t kDefaultMaxStreamsPerConnection = 100;
36
37// Size in bytes of the packet header common across all packets.
38const size_t kPacketHeaderSize = 25;
39// Index of the first byte in a QUIC packet of FEC protected data.
40const size_t kStartOfFecProtectedData = kPacketHeaderSize;
41// Index of the first byte in a QUIC packet of encrypted data.
42const size_t kStartOfEncryptedData = kPacketHeaderSize - 1;
43// Index of the first byte in a QUIC packet which is hashed.
44const size_t kStartOfHashData = 0;
45// Index into the retransmission offset in the header.
46// (After GUID and sequence number.)
47const int kRetransmissionOffset = 14;
48// Index into the transmission time offset in the header.
49const int kTransmissionTimeOffset = 15;
50
[email protected]be24ab22012-10-22 03:01:5251// Size in bytes of all stream frame fields.
52const size_t kMinStreamFrameLength = 15;
[email protected]8b37a092012-10-18 21:53:4953
54// Limit on the delta between stream IDs.
55const QuicStreamId kMaxStreamIdDelta = 100;
56
57// Reserved ID for the crypto stream.
58// TODO(rch): ensure that this is not usable by any other streams.
59const QuicStreamId kCryptoStreamId = 1;
60
61typedef std::pair<QuicPacketSequenceNumber, QuicPacket*> PacketPair;
62
63const int64 kDefaultTimeout = 600000000; // 10 minutes
64
[email protected]be24ab22012-10-22 03:01:5265enum QuicFrameType {
66 STREAM_FRAME = 0,
67 PDU_FRAME,
68 ACK_FRAME,
69 RST_STREAM_FRAME,
70 CONNECTION_CLOSE_FRAME,
71 NUM_FRAME_TYPES
[email protected]8b37a092012-10-18 21:53:4972};
73
74enum QuicPacketFlags {
75 PACKET_FLAGS_NONE = 0,
[email protected]be24ab22012-10-22 03:01:5276 PACKET_FLAGS_FEC = 1, // Payload is FEC as opposed to frames.
[email protected]8b37a092012-10-18 21:53:4977
78 PACKET_FLAGS_MAX = PACKET_FLAGS_FEC
79};
80
81enum QuicErrorCode {
82 // Stream errors.
83 QUIC_NO_ERROR = 0,
84
[email protected]be24ab22012-10-22 03:01:5285 // There were data frames after the a fin or reset.
[email protected]8b37a092012-10-18 21:53:4986 QUIC_STREAM_DATA_AFTER_TERMINATION,
87 // There was some server error which halted stream processing.
88 QUIC_SERVER_ERROR_PROCESSING_STREAM,
89 // We got two fin or reset offsets which did not match.
90 QUIC_MULTIPLE_TERMINATION_OFFSETS,
91 // We got bad payload and can not respond to it at the protocol level.
92 QUIC_BAD_APPLICATION_PAYLOAD,
93
94 // Connection errors.
95
96 // Control frame is malformed.
97 QUIC_INVALID_PACKET_HEADER,
[email protected]be24ab22012-10-22 03:01:5298 // Frame data is malformed.
99 QUIC_INVALID_FRAME_DATA,
[email protected]8b37a092012-10-18 21:53:49100 // FEC data is malformed.
101 QUIC_INVALID_FEC_DATA,
102 // Stream rst data is malformed
103 QUIC_INVALID_RST_STREAM_DATA,
104 // Connection close data is malformed.
105 QUIC_INVALID_CONNECTION_CLOSE_DATA,
106 // Ack data is malformed.
107 QUIC_INVALID_ACK_DATA,
108 // There was an error decrypting.
109 QUIC_DECRYPTION_FAILURE,
110 // There was an error encrypting.
111 QUIC_ENCRYPTION_FAILURE,
112 // The packet exceeded kMaxPacketSize.
113 QUIC_PACKET_TOO_LARGE,
114 // Data was sent for a stream which did not exist.
115 QUIC_PACKET_FOR_NONEXISTENT_STREAM,
116 // The client is going away (browser close, etc.)
117 QUIC_CLIENT_GOING_AWAY,
118 // The server is going away (restart etc.)
119 QUIC_SERVER_GOING_AWAY,
120 // A stream ID was invalid.
121 QUIC_INVALID_STREAM_ID,
122 // Too many streams already open.
123 QUIC_TOO_MANY_OPEN_STREAMS,
124
125 // We hit our prenegotiated (or default) timeout
126 QUIC_CONNECTION_TIMED_OUT,
127
128 // Crypto errors.
129
130 // Handshake message contained out of order tags.
131 QUIC_CRYPTO_TAGS_OUT_OF_ORDER,
132 // Handshake message contained too many entires.
133 QUIC_CRYPTO_TOO_MANY_ENTRIES,
134 // Handshake message contained an invalid value length.
135 QUIC_CRYPTO_INVALID_VALUE_LENGTH,
136 // A crypto message was received after the handshake was complete.
137 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE,
138 // A crypto message was receieved with an illegal message tag.
139 QUIC_INVALID_CRYPTO_MESSAGE_TYPE,
140
141};
142
143struct NET_EXPORT_PRIVATE QuicPacketHeader {
144 // Includes the ConnectionHeader and CongestionMonitoredHeader
145 // from the design docs, as well as some elements of DecryptedData.
146 QuicGuid guid;
147 QuicPacketSequenceNumber packet_sequence_number;
148 uint8 retransmission_count;
149 QuicTransmissionTime transmission_time;
150 QuicPacketFlags flags;
151 QuicFecGroupNumber fec_group;
152};
153
[email protected]be24ab22012-10-22 03:01:52154struct NET_EXPORT_PRIVATE QuicStreamFrame {
155 QuicStreamFrame();
156 QuicStreamFrame(QuicStreamId stream_id,
[email protected]a5061242012-10-23 23:29:37157 bool fin,
158 uint64 offset,
159 base::StringPiece data);
[email protected]8b37a092012-10-18 21:53:49160
161 QuicStreamId stream_id;
162 bool fin;
163 uint64 offset;
164 base::StringPiece data;
165};
166
167struct NET_EXPORT_PRIVATE ReceivedPacketInfo {
168 ReceivedPacketInfo();
169 ~ReceivedPacketInfo();
170 // The highest packet sequence number we've received from the peer.
171 QuicPacketSequenceNumber largest_received;
172 // The time at which we received the above packet.
173 QuicTransmissionTime time_received;
174 // The set of packets which we're expecting and have not received.
175 // This includes any packets between the lowest and largest_received
176 // which we have neither seen nor been informed are non-retransmitting.
177 base::hash_set<QuicPacketSequenceNumber> missing_packets;
178};
179
180struct NET_EXPORT_PRIVATE SentPacketInfo {
181 SentPacketInfo();
182 ~SentPacketInfo();
183 // The lowest packet we've sent which is unacked, and we expect an ack for.
184 QuicPacketSequenceNumber least_unacked;
185 // The set of packets between least_unacked and the last packet we have sent
186 // which we will not resend.
187 base::hash_set<QuicPacketSequenceNumber> non_retransmiting;
188};
189
190// Defines for all types of congestion feedback that will be negotiated in QUIC,
191// kTCP MUST be supported by all QUIC implementations to guarentee 100%
192// compatibility.
193enum CongestionFeedbackType {
194 kNone = 0, // No feedback provided
195 kTCP, // Used to mimic TCP.
196 kInterArrival, // Use additional inter arrival information.
197 kFixRate, // Provided for testing.
198};
199
200struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP {
201 uint16 accumulated_number_of_lost_packets;
202 uint16 receive_window; // Number of bytes >> 4.
203};
204
205struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival {
206 uint16 accumulated_number_of_lost_packets;
207 int16 offset_time;
208 uint16 delta_time; // delta time is described below.
209};
210
211/*
212 * Description of delta time.
213 *
214 * 0 1
215 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
216 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217 * |D|S| offset_time |
218 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219 *
220 * Where:
221 * D is the time domain. If set time domain is in milliseconds, else in
222 * microseconds.
223 * S is the sign bit.
224 * offset_time is the time offset where the relative packet size is equal to
225 * 0.
226 */
227
228struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate {
229 uint32 bitrate_in_bytes_per_second;
230};
231
232struct NET_EXPORT_PRIVATE CongestionInfo {
233 CongestionFeedbackType type;
234 union {
235 CongestionFeedbackMessageTCP tcp;
236 CongestionFeedbackMessageInterArrival inter_arrival;
237 CongestionFeedbackMessageFixRate fix_rate;
238 };
239};
240
[email protected]be24ab22012-10-22 03:01:52241struct NET_EXPORT_PRIVATE QuicAckFrame {
242 QuicAckFrame() {}
243 QuicAckFrame(QuicPacketSequenceNumber largest_received,
[email protected]a5061242012-10-23 23:29:37244 QuicTransmissionTime time_received,
245 QuicPacketSequenceNumber least_unacked) {
[email protected]8b37a092012-10-18 21:53:49246 received_info.largest_received = largest_received;
247 received_info.time_received = time_received;
248 sent_info.least_unacked = least_unacked;
249 congestion_info.type = kNone;
250 }
251
[email protected]5640d0a2012-10-22 18:17:02252 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os,
253 const QuicAckFrame& s);
254
[email protected]8b37a092012-10-18 21:53:49255 SentPacketInfo sent_info;
256 ReceivedPacketInfo received_info;
257 CongestionInfo congestion_info;
[email protected]8b37a092012-10-18 21:53:49258};
259
[email protected]be24ab22012-10-22 03:01:52260struct NET_EXPORT_PRIVATE QuicRstStreamFrame {
261 QuicRstStreamFrame() {}
262 QuicRstStreamFrame(QuicStreamId stream_id, uint64 offset,
[email protected]431bb4fd2012-10-19 17:46:09263 QuicErrorCode error_code)
264 : stream_id(stream_id), offset(offset), error_code(error_code) {
265 DCHECK_LE(error_code, std::numeric_limits<uint8>::max());
[email protected]8b37a092012-10-18 21:53:49266 }
267
268 QuicStreamId stream_id;
269 uint64 offset;
[email protected]431bb4fd2012-10-19 17:46:09270 QuicErrorCode error_code;
271 std::string error_details;
[email protected]8b37a092012-10-18 21:53:49272};
273
[email protected]be24ab22012-10-22 03:01:52274struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame {
[email protected]431bb4fd2012-10-19 17:46:09275 QuicErrorCode error_code;
[email protected]be24ab22012-10-22 03:01:52276 QuicAckFrame ack_frame;
[email protected]431bb4fd2012-10-19 17:46:09277 std::string error_details;
[email protected]8b37a092012-10-18 21:53:49278};
279
[email protected]be24ab22012-10-22 03:01:52280struct NET_EXPORT_PRIVATE QuicFrame {
281 QuicFrame() {}
282 explicit QuicFrame(QuicStreamFrame* stream_frame)
283 : type(STREAM_FRAME), stream_frame(stream_frame) {
[email protected]8b37a092012-10-18 21:53:49284 }
[email protected]be24ab22012-10-22 03:01:52285 explicit QuicFrame(QuicAckFrame* frame)
286 : type(ACK_FRAME), ack_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49287 }
[email protected]be24ab22012-10-22 03:01:52288 explicit QuicFrame(QuicRstStreamFrame* frame)
289 : type(RST_STREAM_FRAME),
290 rst_stream_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49291 }
[email protected]be24ab22012-10-22 03:01:52292 explicit QuicFrame(QuicConnectionCloseFrame* frame)
293 : type(CONNECTION_CLOSE_FRAME),
294 connection_close_frame(frame) {
[email protected]8b37a092012-10-18 21:53:49295 }
296
[email protected]be24ab22012-10-22 03:01:52297 QuicFrameType type;
[email protected]8b37a092012-10-18 21:53:49298 union {
[email protected]be24ab22012-10-22 03:01:52299 QuicStreamFrame* stream_frame;
300 QuicAckFrame* ack_frame;
301 QuicRstStreamFrame* rst_stream_frame;
302 QuicConnectionCloseFrame* connection_close_frame;
[email protected]8b37a092012-10-18 21:53:49303 };
304};
305
[email protected]be24ab22012-10-22 03:01:52306typedef std::vector<QuicFrame> QuicFrames;
[email protected]8b37a092012-10-18 21:53:49307
308struct NET_EXPORT_PRIVATE QuicFecData {
309 QuicFecData();
[email protected]a5061242012-10-23 23:29:37310
311 bool operator==(const QuicFecData& other) const;
312
[email protected]8b37a092012-10-18 21:53:49313 QuicFecGroupNumber fec_group;
[email protected]a5061242012-10-23 23:29:37314 QuicPacketSequenceNumber min_protected_packet_sequence_number;
[email protected]8b37a092012-10-18 21:53:49315 // The last protected packet's sequence number will be one
316 // less than the sequence number of the FEC packet.
317 base::StringPiece redundancy;
[email protected]8b37a092012-10-18 21:53:49318};
319
320struct NET_EXPORT_PRIVATE QuicPacketData {
321 std::string data;
322};
323
324class NET_EXPORT_PRIVATE QuicData {
325 public:
326 QuicData(const char* buffer, size_t length)
327 : buffer_(buffer),
328 length_(length),
329 owns_buffer_(false) { }
330
331 QuicData(char* buffer, size_t length, bool owns_buffer)
332 : buffer_(buffer),
333 length_(length),
334 owns_buffer_(owns_buffer) { }
335
336 virtual ~QuicData();
337
338 base::StringPiece AsStringPiece() const {
339 return base::StringPiece(data(), length());
340 }
341
342 const char* data() const { return buffer_; }
343 size_t length() const { return length_; }
344
345 private:
346 const char* buffer_;
347 size_t length_;
348 bool owns_buffer_;
349
350 DISALLOW_COPY_AND_ASSIGN(QuicData);
351};
352
353class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
354 public:
355 QuicPacket(char* buffer, size_t length, bool owns_buffer)
356 : QuicData(buffer, length, owns_buffer),
357 buffer_(buffer) { }
358
359 base::StringPiece FecProtectedData() const {
360 return base::StringPiece(data() + kStartOfFecProtectedData,
361 length() - kStartOfFecProtectedData);
362 }
363
364 base::StringPiece AssociatedData() const {
365 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
366 }
367
368 base::StringPiece Plaintext() const {
369 return base::StringPiece(data() + kStartOfEncryptedData,
370 length() - kStartOfEncryptedData);
371 }
372 char* mutable_data() { return buffer_; }
373
374 private:
375 char* buffer_;
376
[email protected]2e740db2012-10-20 19:35:19377 DISALLOW_COPY_AND_ASSIGN(QuicPacket);
[email protected]8b37a092012-10-18 21:53:49378};
379
380class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
381 public:
382 QuicEncryptedPacket(const char* buffer, size_t length)
383 : QuicData(buffer, length) { }
384
385 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer)
386 : QuicData(buffer, length, owns_buffer) { }
387
388 base::StringPiece AssociatedData() const {
389 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
390 }
391
[email protected]2e740db2012-10-20 19:35:19392 private:
393 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket);
[email protected]8b37a092012-10-18 21:53:49394};
395
396} // namespace net
397
398#endif // NET_QUIC_QUIC_PROTOCOL_H_