Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: net/quic/quic_protocol.h

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

Powered by Google App Engine
This is Rietveld 408576698