quic_connection_logger.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright (c) 2013 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. #ifndef NET_QUIC_QUIC_CONNECTION_LOGGER_H_
  5. #define NET_QUIC_QUIC_CONNECTION_LOGGER_H_
  6. #include <stddef.h>
  7. #include <bitset>
  8. #include "base/memory/raw_ptr.h"
  9. #include "net/base/ip_endpoint.h"
  10. #include "net/base/net_export.h"
  11. #include "net/base/network_change_notifier.h"
  12. #include "net/cert/cert_verify_result.h"
  13. #include "net/log/net_log_with_source.h"
  14. #include "net/quic/quic_event_logger.h"
  15. #include "net/socket/socket_performance_watcher.h"
  16. #include "net/third_party/quiche/src/quiche/quic/core/crypto/crypto_handshake_message.h"
  17. #include "net/third_party/quiche/src/quiche/quic/core/http/quic_spdy_session.h"
  18. #include "net/third_party/quiche/src/quiche/quic/core/quic_connection.h"
  19. #include "net/third_party/quiche/src/quiche/quic/core/quic_packets.h"
  20. namespace base {
  21. class HistogramBase;
  22. }
  23. namespace net {
  24. // Handles both NetLog support and UMA histograms for QUIC.
  25. class NET_EXPORT_PRIVATE QuicConnectionLogger
  26. : public quic::QuicConnectionDebugVisitor,
  27. public quic::QuicPacketCreator::DebugDelegate {
  28. public:
  29. QuicConnectionLogger(
  30. quic::QuicSession* session,
  31. const char* const connection_description,
  32. std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
  33. const NetLogWithSource& net_log);
  34. QuicConnectionLogger(const QuicConnectionLogger&) = delete;
  35. QuicConnectionLogger& operator=(const QuicConnectionLogger&) = delete;
  36. ~QuicConnectionLogger() override;
  37. // quic::QuicPacketCreator::DebugDelegateInterface
  38. void OnFrameAddedToPacket(const quic::QuicFrame& frame) override;
  39. void OnStreamFrameCoalesced(const quic::QuicStreamFrame& frame) override;
  40. // quic::QuicConnectionDebugVisitor Interface
  41. void OnPacketSent(quic::QuicPacketNumber packet_number,
  42. quic::QuicPacketLength packet_length,
  43. bool has_crypto_handshake,
  44. quic::TransmissionType transmission_type,
  45. quic::EncryptionLevel encryption_level,
  46. const quic::QuicFrames& retransmittable_frames,
  47. const quic::QuicFrames& nonretransmittable_frames,
  48. quic::QuicTime sent_time) override;
  49. void OnIncomingAck(quic::QuicPacketNumber ack_packet_number,
  50. quic::EncryptionLevel ack_decrypted_level,
  51. const quic::QuicAckFrame& frame,
  52. quic::QuicTime ack_receive_time,
  53. quic::QuicPacketNumber largest_observed,
  54. bool rtt_updated,
  55. quic::QuicPacketNumber least_unacked_sent_packet) override;
  56. void OnPacketLoss(quic::QuicPacketNumber lost_packet_number,
  57. quic::EncryptionLevel encryption_level,
  58. quic::TransmissionType transmission_type,
  59. quic::QuicTime detection_time) override;
  60. void OnPingSent() override;
  61. void OnPacketReceived(const quic::QuicSocketAddress& self_address,
  62. const quic::QuicSocketAddress& peer_address,
  63. const quic::QuicEncryptedPacket& packet) override;
  64. void OnUnauthenticatedHeader(const quic::QuicPacketHeader& header) override;
  65. void OnIncorrectConnectionId(quic::QuicConnectionId connection_id) override;
  66. void OnUndecryptablePacket(quic::EncryptionLevel decryption_level,
  67. bool dropped) override;
  68. void OnAttemptingToProcessUndecryptablePacket(
  69. quic::EncryptionLevel decryption_level) override;
  70. void OnDuplicatePacket(quic::QuicPacketNumber packet_number) override;
  71. void OnProtocolVersionMismatch(quic::ParsedQuicVersion version) override;
  72. void OnPacketHeader(const quic::QuicPacketHeader& header,
  73. quic::QuicTime receive_time,
  74. quic::EncryptionLevel level) override;
  75. void OnPathChallengeFrame(const quic::QuicPathChallengeFrame& frame) override;
  76. void OnPathResponseFrame(const quic::QuicPathResponseFrame& frame) override;
  77. void OnCryptoFrame(const quic::QuicCryptoFrame& frame) override;
  78. void OnStopSendingFrame(const quic::QuicStopSendingFrame& frame) override;
  79. void OnStreamsBlockedFrame(
  80. const quic::QuicStreamsBlockedFrame& frame) override;
  81. void OnMaxStreamsFrame(const quic::QuicMaxStreamsFrame& frame) override;
  82. void OnStreamFrame(const quic::QuicStreamFrame& frame) override;
  83. void OnStopWaitingFrame(const quic::QuicStopWaitingFrame& frame) override;
  84. void OnRstStreamFrame(const quic::QuicRstStreamFrame& frame) override;
  85. void OnConnectionCloseFrame(
  86. const quic::QuicConnectionCloseFrame& frame) override;
  87. void OnWindowUpdateFrame(const quic::QuicWindowUpdateFrame& frame,
  88. const quic::QuicTime& receive_time) override;
  89. void OnBlockedFrame(const quic::QuicBlockedFrame& frame) override;
  90. void OnGoAwayFrame(const quic::QuicGoAwayFrame& frame) override;
  91. void OnPingFrame(const quic::QuicPingFrame& frame,
  92. quic::QuicTime::Delta ping_received_delay) override;
  93. void OnPaddingFrame(const quic::QuicPaddingFrame& frame) override;
  94. void OnNewConnectionIdFrame(
  95. const quic::QuicNewConnectionIdFrame& frame) override;
  96. void OnNewTokenFrame(const quic::QuicNewTokenFrame& frame) override;
  97. void OnRetireConnectionIdFrame(
  98. const quic::QuicRetireConnectionIdFrame& frame) override;
  99. void OnMessageFrame(const quic::QuicMessageFrame& frame) override;
  100. void OnHandshakeDoneFrame(const quic::QuicHandshakeDoneFrame& frame) override;
  101. void OnCoalescedPacketSent(const quic::QuicCoalescedPacket& coalesced_packet,
  102. size_t length) override;
  103. void OnPublicResetPacket(const quic::QuicPublicResetPacket& packet) override;
  104. void OnVersionNegotiationPacket(
  105. const quic::QuicVersionNegotiationPacket& packet) override;
  106. void OnConnectionClosed(const quic::QuicConnectionCloseFrame& frame,
  107. quic::ConnectionCloseSource source) override;
  108. void OnSuccessfulVersionNegotiation(
  109. const quic::ParsedQuicVersion& version) override;
  110. void OnRttChanged(quic::QuicTime::Delta rtt) const override;
  111. void OnTransportParametersSent(
  112. const quic::TransportParameters& transport_parameters) override;
  113. void OnTransportParametersReceived(
  114. const quic::TransportParameters& transport_parameters) override;
  115. void OnTransportParametersResumed(
  116. const quic::TransportParameters& transport_parameters) override;
  117. void OnCryptoHandshakeMessageReceived(
  118. const quic::CryptoHandshakeMessage& message);
  119. void OnCryptoHandshakeMessageSent(
  120. const quic::CryptoHandshakeMessage& message);
  121. void UpdateReceivedFrameCounts(quic::QuicStreamId stream_id,
  122. int num_frames_received,
  123. int num_duplicate_frames_received);
  124. void OnCertificateVerified(const CertVerifyResult& result);
  125. // Returns connection's overall packet loss rate in fraction.
  126. float ReceivedPacketLossRate() const;
  127. void OnZeroRttRejected(int reason) override;
  128. private:
  129. // Do a factory get for a histogram to record a 6-packet loss-sequence as a
  130. // sample. The histogram will record the 64 distinct possible combinations.
  131. // |which_6| is used to adjust the name of the histogram to distinguish the
  132. // first 6 packets in a connection, vs. some later 6 packets.
  133. base::HistogramBase* Get6PacketHistogram(const char* which_6) const;
  134. // For connections longer than 21 received packets, this call will calculate
  135. // the overall packet loss rate, and record it into a histogram.
  136. void RecordAggregatePacketLossRate() const;
  137. raw_ptr<quic::QuicSession> session_; // Unowned.
  138. // The last packet number received.
  139. quic::QuicPacketNumber last_received_packet_number_;
  140. // The size of the most recently received packet.
  141. size_t last_received_packet_size_ = 0;
  142. // True if a PING frame has been sent and no packet has been received.
  143. bool no_packet_received_after_ping_ = false;
  144. // The size of the previously received packet.
  145. size_t previous_received_packet_size_ = 0;
  146. // The first received packet number. Used as the left edge of
  147. // received_packets_ and received_acks_. In the case where packets are
  148. // received out of order, packets with numbers smaller than
  149. // first_received_packet_number_ will not be logged.
  150. quic::QuicPacketNumber first_received_packet_number_;
  151. // The largest packet number received. In the case where a packet is
  152. // received late (out of order), this value will not be updated.
  153. quic::QuicPacketNumber largest_received_packet_number_;
  154. // Number of times that the current received packet number is
  155. // smaller than the last received packet number.
  156. size_t num_out_of_order_received_packets_ = 0;
  157. // Number of times that the current received packet number is
  158. // smaller than the last received packet number and where the
  159. // size of the current packet is larger than the size of the previous
  160. // packet.
  161. size_t num_out_of_order_large_received_packets_ = 0;
  162. // The number of times that OnPacketHeader was called.
  163. // If the network replicates packets, then this number may be slightly
  164. // different from the real number of distinct packets received.
  165. quic::QuicPacketCount num_packets_received_ = 0;
  166. // The quic::kCADR value provided by the server in ServerHello.
  167. IPEndPoint local_address_from_shlo_;
  168. // The first local address from which a packet was received.
  169. IPEndPoint local_address_from_self_;
  170. // Count of the number of frames received.
  171. int num_frames_received_ = 0;
  172. // Count of the number of duplicate frames received.
  173. int num_duplicate_frames_received_ = 0;
  174. // Count of the number of packets received with incorrect connection IDs.
  175. int num_incorrect_connection_ids_ = 0;
  176. // Count of the number of undecryptable packets received.
  177. int num_undecryptable_packets_ = 0;
  178. // Count of the number of duplicate packets received.
  179. int num_duplicate_packets_ = 0;
  180. // Count of the number of BLOCKED frames received.
  181. int num_blocked_frames_received_ = 0;
  182. // Count of the number of BLOCKED frames sent.
  183. int num_blocked_frames_sent_ = 0;
  184. // Vector of initial packets status' indexed by packet numbers, where
  185. // false means never received. We track 150 packets starting from
  186. // first_received_packet_number_.
  187. std::bitset<150> received_packets_;
  188. // Vector to indicate which of the initial 150 received packets turned out to
  189. // contain solo ACK frames. An element is true iff an ACK frame was in the
  190. // corresponding packet, and there was very little else.
  191. std::bitset<150> received_acks_;
  192. // The available type of connection (WiFi, 3G, etc.) when connection was first
  193. // used.
  194. const char* const connection_description_;
  195. // Receives notifications regarding the performance of the underlying socket
  196. // for the QUIC connection. May be null.
  197. const std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher_;
  198. QuicEventLogger event_logger_;
  199. };
  200. } // namespace net
  201. #endif // NET_QUIC_QUIC_CONNECTION_LOGGER_H_