blob: 04cd091150c4d2486df7b023ec5d6250d432c92e [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]8b37a092012-10-18 21:53:49157 bool fin,
158 uint64 offset,
159 base::StringPiece data);
160
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]8b37a092012-10-18 21:53:49244 QuicTransmissionTime time_received,
245 QuicPacketSequenceNumber least_unacked) {
246 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();
310 QuicFecGroupNumber fec_group;
311 QuicPacketSequenceNumber first_protected_packet_sequence_number;
312 // The last protected packet's sequence number will be one
313 // less than the sequence number of the FEC packet.
314 base::StringPiece redundancy;
[email protected]5640d0a2012-10-22 18:17:02315 bool operator==(const QuicFecData& other) const;
[email protected]8b37a092012-10-18 21:53:49316};
317
318struct NET_EXPORT_PRIVATE QuicPacketData {
319 std::string data;
320};
321
322class NET_EXPORT_PRIVATE QuicData {
323 public:
324 QuicData(const char* buffer, size_t length)
325 : buffer_(buffer),
326 length_(length),
327 owns_buffer_(false) { }
328
329 QuicData(char* buffer, size_t length, bool owns_buffer)
330 : buffer_(buffer),
331 length_(length),
332 owns_buffer_(owns_buffer) { }
333
334 virtual ~QuicData();
335
336 base::StringPiece AsStringPiece() const {
337 return base::StringPiece(data(), length());
338 }
339
340 const char* data() const { return buffer_; }
341 size_t length() const { return length_; }
342
343 private:
344 const char* buffer_;
345 size_t length_;
346 bool owns_buffer_;
347
348 DISALLOW_COPY_AND_ASSIGN(QuicData);
349};
350
351class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
352 public:
353 QuicPacket(char* buffer, size_t length, bool owns_buffer)
354 : QuicData(buffer, length, owns_buffer),
355 buffer_(buffer) { }
356
357 base::StringPiece FecProtectedData() const {
358 return base::StringPiece(data() + kStartOfFecProtectedData,
359 length() - kStartOfFecProtectedData);
360 }
361
362 base::StringPiece AssociatedData() const {
363 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
364 }
365
366 base::StringPiece Plaintext() const {
367 return base::StringPiece(data() + kStartOfEncryptedData,
368 length() - kStartOfEncryptedData);
369 }
370 char* mutable_data() { return buffer_; }
371
372 private:
373 char* buffer_;
374
[email protected]2e740db2012-10-20 19:35:19375 DISALLOW_COPY_AND_ASSIGN(QuicPacket);
[email protected]8b37a092012-10-18 21:53:49376};
377
378class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
379 public:
380 QuicEncryptedPacket(const char* buffer, size_t length)
381 : QuicData(buffer, length) { }
382
383 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer)
384 : QuicData(buffer, length, owns_buffer) { }
385
386 base::StringPiece AssociatedData() const {
387 return base::StringPiece(data() + kStartOfHashData, kStartOfEncryptedData);
388 }
389
[email protected]2e740db2012-10-20 19:35:19390 private:
391 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket);
[email protected]8b37a092012-10-18 21:53:49392};
393
394} // namespace net
395
396#endif // NET_QUIC_QUIC_PROTOCOL_H_