[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / quic / quic_protocol.cc
blobdf482a8f26ccbf99d1665e7d13f22ccdec6a9c88
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.
5 #include "net/quic/quic_protocol.h"
7 #include "base/stl_util.h"
8 #include "net/quic/quic_utils.h"
10 using base::StringPiece;
11 using std::map;
12 using std::numeric_limits;
13 using std::ostream;
14 using std::string;
16 namespace net {
18 size_t GetPacketHeaderSize(const QuicPacketHeader& header) {
19 return GetPacketHeaderSize(header.public_header.connection_id_length,
20 header.public_header.version_flag,
21 header.public_header.sequence_number_length,
22 header.is_in_fec_group);
25 size_t GetPacketHeaderSize(QuicConnectionIdLength connection_id_length,
26 bool include_version,
27 QuicSequenceNumberLength sequence_number_length,
28 InFecGroup is_in_fec_group) {
29 return kPublicFlagsSize + connection_id_length +
30 (include_version ? kQuicVersionSize : 0) + sequence_number_length +
31 kPrivateFlagsSize + (is_in_fec_group == IN_FEC_GROUP ? kFecGroupSize : 0);
34 size_t GetStartOfFecProtectedData(
35 QuicConnectionIdLength connection_id_length,
36 bool include_version,
37 QuicSequenceNumberLength sequence_number_length) {
38 return GetPacketHeaderSize(connection_id_length,
39 include_version,
40 sequence_number_length,
41 IN_FEC_GROUP);
44 size_t GetStartOfEncryptedData(
45 QuicConnectionIdLength connection_id_length,
46 bool include_version,
47 QuicSequenceNumberLength sequence_number_length) {
48 // Don't include the fec size, since encryption starts before private flags.
49 return GetPacketHeaderSize(connection_id_length,
50 include_version,
51 sequence_number_length,
52 NOT_IN_FEC_GROUP) - kPrivateFlagsSize;
55 QuicPacketPublicHeader::QuicPacketPublicHeader()
56 : connection_id(0),
57 connection_id_length(PACKET_8BYTE_CONNECTION_ID),
58 reset_flag(false),
59 version_flag(false),
60 sequence_number_length(PACKET_6BYTE_SEQUENCE_NUMBER) {
63 QuicPacketPublicHeader::QuicPacketPublicHeader(
64 const QuicPacketPublicHeader& other)
65 : connection_id(other.connection_id),
66 connection_id_length(other.connection_id_length),
67 reset_flag(other.reset_flag),
68 version_flag(other.version_flag),
69 sequence_number_length(other.sequence_number_length),
70 versions(other.versions) {
73 QuicPacketPublicHeader::~QuicPacketPublicHeader() {}
75 QuicPacketHeader::QuicPacketHeader()
76 : packet_sequence_number(0),
77 fec_flag(false),
78 entropy_flag(false),
79 entropy_hash(0),
80 is_in_fec_group(NOT_IN_FEC_GROUP),
81 fec_group(0) {
84 QuicPacketHeader::QuicPacketHeader(const QuicPacketPublicHeader& header)
85 : public_header(header),
86 packet_sequence_number(0),
87 fec_flag(false),
88 entropy_flag(false),
89 entropy_hash(0),
90 is_in_fec_group(NOT_IN_FEC_GROUP),
91 fec_group(0) {
94 QuicPublicResetPacket::QuicPublicResetPacket()
95 : nonce_proof(0),
96 rejected_sequence_number(0) {}
98 QuicPublicResetPacket::QuicPublicResetPacket(
99 const QuicPacketPublicHeader& header)
100 : public_header(header),
101 nonce_proof(0),
102 rejected_sequence_number(0) {}
104 QuicStreamFrame::QuicStreamFrame() : stream_id(0), fin(false), offset(0) {
107 QuicStreamFrame::QuicStreamFrame(const QuicStreamFrame& frame)
108 : stream_id(frame.stream_id),
109 fin(frame.fin),
110 offset(frame.offset),
111 data(frame.data) {
114 QuicStreamFrame::QuicStreamFrame(QuicStreamId stream_id,
115 bool fin,
116 QuicStreamOffset offset,
117 StringPiece data)
118 : stream_id(stream_id), fin(fin), offset(offset), data(data) {
121 uint32 MakeQuicTag(char a, char b, char c, char d) {
122 return static_cast<uint32>(a) |
123 static_cast<uint32>(b) << 8 |
124 static_cast<uint32>(c) << 16 |
125 static_cast<uint32>(d) << 24;
128 bool ContainsQuicTag(const QuicTagVector& tag_vector, QuicTag tag) {
129 return std::find(tag_vector.begin(), tag_vector.end(), tag)
130 != tag_vector.end();
133 QuicVersionVector QuicSupportedVersions() {
134 QuicVersionVector supported_versions;
135 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
136 supported_versions.push_back(kSupportedQuicVersions[i]);
138 return supported_versions;
141 QuicTag QuicVersionToQuicTag(const QuicVersion version) {
142 switch (version) {
143 case QUIC_VERSION_24:
144 return MakeQuicTag('Q', '0', '2', '4');
145 case QUIC_VERSION_25:
146 return MakeQuicTag('Q', '0', '2', '5');
147 default:
148 // This shold be an ERROR because we should never attempt to convert an
149 // invalid QuicVersion to be written to the wire.
150 LOG(ERROR) << "Unsupported QuicVersion: " << version;
151 return 0;
155 QuicVersion QuicTagToQuicVersion(const QuicTag version_tag) {
156 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
157 if (version_tag == QuicVersionToQuicTag(kSupportedQuicVersions[i])) {
158 return kSupportedQuicVersions[i];
161 // Reading from the client so this should not be considered an ERROR.
162 DVLOG(1) << "Unsupported QuicTag version: "
163 << QuicUtils::TagToString(version_tag);
164 return QUIC_VERSION_UNSUPPORTED;
167 #define RETURN_STRING_LITERAL(x) \
168 case x: \
169 return #x
171 string QuicVersionToString(const QuicVersion version) {
172 switch (version) {
173 RETURN_STRING_LITERAL(QUIC_VERSION_24);
174 RETURN_STRING_LITERAL(QUIC_VERSION_25);
175 default:
176 return "QUIC_VERSION_UNSUPPORTED";
180 string QuicVersionVectorToString(const QuicVersionVector& versions) {
181 string result = "";
182 for (size_t i = 0; i < versions.size(); ++i) {
183 if (i != 0) {
184 result.append(",");
186 result.append(QuicVersionToString(versions[i]));
188 return result;
191 ostream& operator<<(ostream& os, const Perspective& s) {
192 if (s == Perspective::IS_SERVER) {
193 os << "IS_SERVER";
194 } else {
195 os << "IS_CLIENT";
197 return os;
200 ostream& operator<<(ostream& os, const QuicPacketHeader& header) {
201 os << "{ connection_id: " << header.public_header.connection_id
202 << ", connection_id_length:" << header.public_header.connection_id_length
203 << ", sequence_number_length:"
204 << header.public_header.sequence_number_length
205 << ", reset_flag: " << header.public_header.reset_flag
206 << ", version_flag: " << header.public_header.version_flag;
207 if (header.public_header.version_flag) {
208 os << " version: ";
209 for (size_t i = 0; i < header.public_header.versions.size(); ++i) {
210 os << header.public_header.versions[i] << " ";
213 os << ", fec_flag: " << header.fec_flag
214 << ", entropy_flag: " << header.entropy_flag
215 << ", entropy hash: " << static_cast<int>(header.entropy_hash)
216 << ", sequence_number: " << header.packet_sequence_number
217 << ", is_in_fec_group:" << header.is_in_fec_group
218 << ", fec_group: " << header.fec_group<< "}\n";
219 return os;
222 bool IsAwaitingPacket(const QuicAckFrame& ack_frame,
223 QuicPacketSequenceNumber sequence_number) {
224 return sequence_number > ack_frame.largest_observed ||
225 ContainsKey(ack_frame.missing_packets, sequence_number);
228 void InsertMissingPacketsBetween(QuicAckFrame* ack_frame,
229 QuicPacketSequenceNumber lower,
230 QuicPacketSequenceNumber higher) {
231 for (QuicPacketSequenceNumber i = lower; i < higher; ++i) {
232 ack_frame->missing_packets.insert(i);
236 QuicStopWaitingFrame::QuicStopWaitingFrame()
237 : entropy_hash(0),
238 least_unacked(0) {
241 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
243 QuicAckFrame::QuicAckFrame()
244 : entropy_hash(0),
245 largest_observed(0),
246 delta_time_largest_observed(QuicTime::Delta::Infinite()),
247 is_truncated(false) {}
249 QuicAckFrame::~QuicAckFrame() {}
251 QuicRstStreamErrorCode AdjustErrorForVersion(
252 QuicRstStreamErrorCode error_code,
253 QuicVersion version) {
254 return error_code;
257 QuicRstStreamFrame::QuicRstStreamFrame()
258 : stream_id(0),
259 error_code(QUIC_STREAM_NO_ERROR) {
262 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id,
263 QuicRstStreamErrorCode error_code,
264 QuicStreamOffset bytes_written)
265 : stream_id(stream_id),
266 error_code(error_code),
267 byte_offset(bytes_written) {
268 DCHECK_LE(error_code, numeric_limits<uint8>::max());
271 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
272 : error_code(QUIC_NO_ERROR) {
275 QuicFrame::QuicFrame() {}
277 QuicFrame::QuicFrame(QuicPaddingFrame* padding_frame)
278 : type(PADDING_FRAME),
279 padding_frame(padding_frame) {
282 QuicFrame::QuicFrame(QuicStreamFrame* stream_frame)
283 : type(STREAM_FRAME),
284 stream_frame(stream_frame) {
287 QuicFrame::QuicFrame(QuicAckFrame* frame)
288 : type(ACK_FRAME),
289 ack_frame(frame) {
292 QuicFrame::QuicFrame(QuicStopWaitingFrame* frame)
293 : type(STOP_WAITING_FRAME),
294 stop_waiting_frame(frame) {
297 QuicFrame::QuicFrame(QuicPingFrame* frame)
298 : type(PING_FRAME),
299 ping_frame(frame) {
302 QuicFrame::QuicFrame(QuicRstStreamFrame* frame)
303 : type(RST_STREAM_FRAME),
304 rst_stream_frame(frame) {
307 QuicFrame::QuicFrame(QuicConnectionCloseFrame* frame)
308 : type(CONNECTION_CLOSE_FRAME),
309 connection_close_frame(frame) {
312 QuicFrame::QuicFrame(QuicGoAwayFrame* frame)
313 : type(GOAWAY_FRAME),
314 goaway_frame(frame) {
317 QuicFrame::QuicFrame(QuicWindowUpdateFrame* frame)
318 : type(WINDOW_UPDATE_FRAME),
319 window_update_frame(frame) {
322 QuicFrame::QuicFrame(QuicBlockedFrame* frame)
323 : type(BLOCKED_FRAME),
324 blocked_frame(frame) {
327 QuicFecData::QuicFecData() : fec_group(0) {}
329 ostream& operator<<(ostream& os, const QuicStopWaitingFrame& sent_info) {
330 os << "entropy_hash: " << static_cast<int>(sent_info.entropy_hash)
331 << " least_unacked: " << sent_info.least_unacked;
332 return os;
335 ostream& operator<<(ostream& os, const QuicAckFrame& ack_frame) {
336 os << "entropy_hash: " << static_cast<int>(ack_frame.entropy_hash)
337 << " largest_observed: " << ack_frame.largest_observed
338 << " delta_time_largest_observed: "
339 << ack_frame.delta_time_largest_observed.ToMicroseconds()
340 << " missing_packets: [ ";
341 for (SequenceNumberSet::const_iterator it = ack_frame.missing_packets.begin();
342 it != ack_frame.missing_packets.end(); ++it) {
343 os << *it << " ";
345 os << " ] is_truncated: " << ack_frame.is_truncated;
346 os << " revived_packets: [ ";
347 for (SequenceNumberSet::const_iterator it = ack_frame.revived_packets.begin();
348 it != ack_frame.revived_packets.end(); ++it) {
349 os << *it << " ";
351 os << " ] received_packets: [ ";
352 for (PacketTimeList::const_iterator it =
353 ack_frame.received_packet_times.begin();
354 it != ack_frame.received_packet_times.end(); ++it) {
355 os << it->first << " at " << it->second.ToDebuggingValue() << " ";
357 os << " ]";
358 return os;
361 ostream& operator<<(ostream& os, const QuicFrame& frame) {
362 switch (frame.type) {
363 case PADDING_FRAME: {
364 os << "type { PADDING_FRAME } ";
365 break;
367 case RST_STREAM_FRAME: {
368 os << "type { RST_STREAM_FRAME } " << *(frame.rst_stream_frame);
369 break;
371 case CONNECTION_CLOSE_FRAME: {
372 os << "type { CONNECTION_CLOSE_FRAME } "
373 << *(frame.connection_close_frame);
374 break;
376 case GOAWAY_FRAME: {
377 os << "type { GOAWAY_FRAME } " << *(frame.goaway_frame);
378 break;
380 case WINDOW_UPDATE_FRAME: {
381 os << "type { WINDOW_UPDATE_FRAME } " << *(frame.window_update_frame);
382 break;
384 case BLOCKED_FRAME: {
385 os << "type { BLOCKED_FRAME } " << *(frame.blocked_frame);
386 break;
388 case STREAM_FRAME: {
389 os << "type { STREAM_FRAME } " << *(frame.stream_frame);
390 break;
392 case ACK_FRAME: {
393 os << "type { ACK_FRAME } " << *(frame.ack_frame);
394 break;
396 case STOP_WAITING_FRAME: {
397 os << "type { STOP_WAITING_FRAME } " << *(frame.stop_waiting_frame);
398 break;
400 case PING_FRAME: {
401 os << "type { PING_FRAME } ";
402 break;
404 default: {
405 LOG(ERROR) << "Unknown frame type: " << frame.type;
406 break;
409 return os;
412 ostream& operator<<(ostream& os, const QuicRstStreamFrame& rst_frame) {
413 os << "stream_id { " << rst_frame.stream_id << " } "
414 << "error_code { " << rst_frame.error_code << " } "
415 << "error_details { " << rst_frame.error_details << " }\n";
416 return os;
419 ostream& operator<<(ostream& os,
420 const QuicConnectionCloseFrame& connection_close_frame) {
421 os << "error_code { " << connection_close_frame.error_code << " } "
422 << "error_details { " << connection_close_frame.error_details << " }\n";
423 return os;
426 ostream& operator<<(ostream& os, const QuicGoAwayFrame& goaway_frame) {
427 os << "error_code { " << goaway_frame.error_code << " } "
428 << "last_good_stream_id { " << goaway_frame.last_good_stream_id << " } "
429 << "reason_phrase { " << goaway_frame.reason_phrase << " }\n";
430 return os;
433 ostream& operator<<(ostream& os,
434 const QuicWindowUpdateFrame& window_update_frame) {
435 os << "stream_id { " << window_update_frame.stream_id << " } "
436 << "byte_offset { " << window_update_frame.byte_offset << " }\n";
437 return os;
440 ostream& operator<<(ostream& os, const QuicBlockedFrame& blocked_frame) {
441 os << "stream_id { " << blocked_frame.stream_id << " }\n";
442 return os;
445 ostream& operator<<(ostream& os, const QuicStreamFrame& stream_frame) {
446 os << "stream_id { " << stream_frame.stream_id << " } "
447 << "fin { " << stream_frame.fin << " } "
448 << "offset { " << stream_frame.offset << " } "
449 << "data { " << QuicUtils::StringToHexASCIIDump(stream_frame.data)
450 << " }\n";
451 return os;
454 QuicGoAwayFrame::QuicGoAwayFrame()
455 : error_code(QUIC_NO_ERROR),
456 last_good_stream_id(0) {
459 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code,
460 QuicStreamId last_good_stream_id,
461 const string& reason)
462 : error_code(error_code),
463 last_good_stream_id(last_good_stream_id),
464 reason_phrase(reason) {
465 DCHECK_LE(error_code, numeric_limits<uint8>::max());
468 QuicData::QuicData(const char* buffer,
469 size_t length)
470 : buffer_(buffer),
471 length_(length),
472 owns_buffer_(false) {
475 QuicData::QuicData(char* buffer,
476 size_t length,
477 bool owns_buffer)
478 : buffer_(buffer),
479 length_(length),
480 owns_buffer_(owns_buffer) {
483 QuicData::~QuicData() {
484 if (owns_buffer_) {
485 delete [] const_cast<char*>(buffer_);
489 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id,
490 QuicStreamOffset byte_offset)
491 : stream_id(stream_id),
492 byte_offset(byte_offset) {}
494 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id)
495 : stream_id(stream_id) {}
497 QuicPacket::QuicPacket(char* buffer,
498 size_t length,
499 bool owns_buffer,
500 QuicConnectionIdLength connection_id_length,
501 bool includes_version,
502 QuicSequenceNumberLength sequence_number_length)
503 : QuicData(buffer, length, owns_buffer),
504 buffer_(buffer),
505 connection_id_length_(connection_id_length),
506 includes_version_(includes_version),
507 sequence_number_length_(sequence_number_length) {
510 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer,
511 size_t length)
512 : QuicData(buffer, length) {
515 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer,
516 size_t length,
517 bool owns_buffer)
518 : QuicData(buffer, length, owns_buffer) {
521 StringPiece QuicPacket::FecProtectedData() const {
522 const size_t start_of_fec = GetStartOfFecProtectedData(
523 connection_id_length_, includes_version_, sequence_number_length_);
524 return StringPiece(data() + start_of_fec, length() - start_of_fec);
527 StringPiece QuicPacket::AssociatedData() const {
528 return StringPiece(
529 data() + kStartOfHashData,
530 GetStartOfEncryptedData(
531 connection_id_length_, includes_version_, sequence_number_length_) -
532 kStartOfHashData);
535 StringPiece QuicPacket::BeforePlaintext() const {
536 return StringPiece(data(), GetStartOfEncryptedData(connection_id_length_,
537 includes_version_,
538 sequence_number_length_));
541 StringPiece QuicPacket::Plaintext() const {
542 const size_t start_of_encrypted_data =
543 GetStartOfEncryptedData(
544 connection_id_length_, includes_version_, sequence_number_length_);
545 return StringPiece(data() + start_of_encrypted_data,
546 length() - start_of_encrypted_data);
549 RetransmittableFrames::RetransmittableFrames(EncryptionLevel level)
550 : encryption_level_(level), has_crypto_handshake_(NOT_HANDSHAKE) {
553 RetransmittableFrames::~RetransmittableFrames() {
554 for (QuicFrames::iterator it = frames_.begin(); it != frames_.end(); ++it) {
555 switch (it->type) {
556 case PADDING_FRAME:
557 delete it->padding_frame;
558 break;
559 case STREAM_FRAME:
560 delete it->stream_frame;
561 break;
562 case ACK_FRAME:
563 delete it->ack_frame;
564 break;
565 case STOP_WAITING_FRAME:
566 delete it->stop_waiting_frame;
567 break;
568 case PING_FRAME:
569 delete it->ping_frame;
570 break;
571 case RST_STREAM_FRAME:
572 delete it->rst_stream_frame;
573 break;
574 case CONNECTION_CLOSE_FRAME:
575 delete it->connection_close_frame;
576 break;
577 case GOAWAY_FRAME:
578 delete it->goaway_frame;
579 break;
580 case WINDOW_UPDATE_FRAME:
581 delete it->window_update_frame;
582 break;
583 case BLOCKED_FRAME:
584 delete it->blocked_frame;
585 break;
586 case NUM_FRAME_TYPES:
587 DCHECK(false) << "Cannot delete type: " << it->type;
590 for (const char* buffer : stream_data_) {
591 delete[] buffer;
595 const QuicFrame& RetransmittableFrames::AddFrame(const QuicFrame& frame) {
596 return AddFrame(frame, nullptr);
599 const QuicFrame& RetransmittableFrames::AddFrame(const QuicFrame& frame,
600 char* buffer) {
601 if (frame.type == STREAM_FRAME &&
602 frame.stream_frame->stream_id == kCryptoStreamId) {
603 has_crypto_handshake_ = IS_HANDSHAKE;
605 if (buffer != nullptr) {
606 stream_data_.push_back(buffer);
608 frames_.push_back(frame);
609 return frames_.back();
612 void RetransmittableFrames::RemoveFramesForStream(QuicStreamId stream_id) {
613 QuicFrames::iterator it = frames_.begin();
614 while (it != frames_.end()) {
615 if (it->type != STREAM_FRAME || it->stream_frame->stream_id != stream_id) {
616 ++it;
617 continue;
619 delete it->stream_frame;
620 it = frames_.erase(it);
624 SerializedPacket::SerializedPacket(
625 QuicPacketSequenceNumber sequence_number,
626 QuicSequenceNumberLength sequence_number_length,
627 QuicEncryptedPacket* packet,
628 QuicPacketEntropyHash entropy_hash,
629 RetransmittableFrames* retransmittable_frames)
630 : packet(packet),
631 retransmittable_frames(retransmittable_frames),
632 sequence_number(sequence_number),
633 sequence_number_length(sequence_number_length),
634 entropy_hash(entropy_hash),
635 is_fec_packet(false) {
638 SerializedPacket::~SerializedPacket() {}
640 QuicEncryptedPacket* QuicEncryptedPacket::Clone() const {
641 char* buffer = new char[this->length()];
642 memcpy(buffer, this->data(), this->length());
643 return new QuicEncryptedPacket(buffer, this->length(), true);
646 ostream& operator<<(ostream& os, const QuicEncryptedPacket& s) {
647 os << s.length() << "-byte data";
648 return os;
651 TransmissionInfo::TransmissionInfo()
652 : retransmittable_frames(nullptr),
653 sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER),
654 sent_time(QuicTime::Zero()),
655 bytes_sent(0),
656 nack_count(0),
657 transmission_type(NOT_RETRANSMISSION),
658 all_transmissions(nullptr),
659 in_flight(false),
660 is_unackable(false),
661 is_fec_packet(false) {
664 TransmissionInfo::TransmissionInfo(
665 RetransmittableFrames* retransmittable_frames,
666 QuicSequenceNumberLength sequence_number_length,
667 TransmissionType transmission_type,
668 QuicTime sent_time,
669 QuicByteCount bytes_sent,
670 bool is_fec_packet)
671 : retransmittable_frames(retransmittable_frames),
672 sequence_number_length(sequence_number_length),
673 sent_time(sent_time),
674 bytes_sent(bytes_sent),
675 nack_count(0),
676 transmission_type(transmission_type),
677 all_transmissions(nullptr),
678 in_flight(false),
679 is_unackable(false),
680 is_fec_packet(is_fec_packet) {
683 } // namespace net