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