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
5#ifndef NET_QUIC_QUIC_CONNECTION_LOGGER_H_
6#define NET_QUIC_QUIC_CONNECTION_LOGGER_H_
7
8#include <bitset>
9
10#include "net/base/ip_endpoint.h"
11#include "net/base/net_log.h"
12#include "net/base/network_change_notifier.h"
13#include "net/quic/quic_connection.h"
14#include "net/quic/quic_protocol.h"
15
16namespace net {
17namespace test {
18class QuicConnectionLoggerPeer;
19}  // namespace test
20
21class CryptoHandshakeMessage;
22class CertVerifyResult;
23
24// This class is a debug visitor of a QuicConnection which logs
25// events to |net_log|.
26class NET_EXPORT_PRIVATE QuicConnectionLogger
27    : public QuicConnectionDebugVisitor {
28 public:
29  explicit QuicConnectionLogger(const BoundNetLog& net_log);
30
31  virtual ~QuicConnectionLogger();
32
33  // QuicPacketGenerator::DebugDelegateInterface
34  virtual void OnFrameAddedToPacket(const QuicFrame& frame) OVERRIDE;
35
36  // QuicConnectionDebugVisitorInterface
37  virtual void OnPacketSent(QuicPacketSequenceNumber sequence_number,
38                            EncryptionLevel level,
39                            TransmissionType transmission_type,
40                            const QuicEncryptedPacket& packet,
41                            WriteResult result) OVERRIDE;
42  virtual void OnPacketRetransmitted(
43      QuicPacketSequenceNumber old_sequence_number,
44      QuicPacketSequenceNumber new_sequence_number) OVERRIDE;
45  virtual void OnPacketReceived(const IPEndPoint& self_address,
46                                const IPEndPoint& peer_address,
47                                const QuicEncryptedPacket& packet) OVERRIDE;
48  virtual void OnIncorrectConnectionId(
49      QuicConnectionId connection_id) OVERRIDE;
50  virtual void OnUndecryptablePacket() OVERRIDE;
51  virtual void OnDuplicatePacket(QuicPacketSequenceNumber sequence_number)
52      OVERRIDE;
53  virtual void OnProtocolVersionMismatch(QuicVersion version) OVERRIDE;
54  virtual void OnPacketHeader(const QuicPacketHeader& header) OVERRIDE;
55  virtual void OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE;
56  virtual void OnAckFrame(const QuicAckFrame& frame) OVERRIDE;
57  virtual void OnCongestionFeedbackFrame(
58      const QuicCongestionFeedbackFrame& frame) OVERRIDE;
59  virtual void OnStopWaitingFrame(const QuicStopWaitingFrame& frame) OVERRIDE;
60  virtual void OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE;
61  virtual void OnConnectionCloseFrame(
62      const QuicConnectionCloseFrame& frame) OVERRIDE;
63  virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) OVERRIDE;
64  virtual void OnBlockedFrame(const QuicBlockedFrame& frame) OVERRIDE;
65  virtual void OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE;
66  virtual void OnPingFrame(const QuicPingFrame& frame) OVERRIDE;
67  virtual void OnPublicResetPacket(
68      const QuicPublicResetPacket& packet) OVERRIDE;
69  virtual void OnVersionNegotiationPacket(
70      const QuicVersionNegotiationPacket& packet) OVERRIDE;
71  virtual void OnRevivedPacket(const QuicPacketHeader& revived_header,
72                               base::StringPiece payload) OVERRIDE;
73  virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) OVERRIDE;
74  virtual void OnSuccessfulVersionNegotiation(
75      const QuicVersion& version) OVERRIDE;
76
77  void OnCryptoHandshakeMessageReceived(
78      const CryptoHandshakeMessage& message);
79  void OnCryptoHandshakeMessageSent(
80      const CryptoHandshakeMessage& message);
81  void UpdateReceivedFrameCounts(QuicStreamId stream_id,
82                                 int num_frames_received,
83                                 int num_duplicate_frames_received);
84  void OnCertificateVerified(const CertVerifyResult& result);
85
86 private:
87  friend class test::QuicConnectionLoggerPeer;
88
89  // Do a factory get for a histogram for recording data, about individual
90  // packet sequence numbers, that was gathered in the vectors
91  // received_packets_ and received_acks_. |statistic_name| identifies which
92  // element of data is recorded, and is used to form the histogram name.
93  base::HistogramBase* GetPacketSequenceNumberHistogram(
94      const char* statistic_name) const;
95  // Do a factory get for a histogram to record a 6-packet loss-sequence as a
96  // sample. The histogram will record the 64 distinct possible combinations.
97  // |which_6| is used to adjust the name of the histogram to distinguish the
98  // first 6 packets in a connection, vs. some later 6 packets.
99  base::HistogramBase* Get6PacketHistogram(const char* which_6) const;
100  // Do a factory get for a histogram to record cumulative stats across a 21
101  // packet sequence.  |which_21| is used to adjust the name of the histogram
102  // to distinguish the first 21 packets' loss data, vs. some later 21 packet
103  // sequences' loss data.
104  base::HistogramBase* Get21CumulativeHistogram(const char* which_21) const;
105  // Add samples associated with a |bit_mask_of_packets| to the given histogram
106  // that was provided by Get21CumulativeHistogram().  The LSB of that mask
107  // corresponds to the oldest packet sequence number in the series of packets,
108  // and the bit in the 2^20 position corresponds to the most recently received
109  // packet.  Of the maximum of 21 bits that are valid (correspond to packets),
110  // only the most significant |valid_bits_in_mask| are processed.
111  // A bit value of 0 indicates that a packet was never received, and a 1
112  // indicates the packet was received.
113  static void AddTo21CumulativeHistogram(base::HistogramBase* histogram,
114                                         int bit_mask_of_packets,
115                                         int valid_bits_in_mask);
116  // For connections longer than 21 received packets, this call will calculate
117  // the overall packet loss rate, and record it into a histogram.
118  void RecordAggregatePacketLossRate() const;
119  // At destruction time, this records results of |pacaket_received_| into
120  // histograms for specific connection types.
121  void RecordLossHistograms() const;
122
123  BoundNetLog net_log_;
124  // The last packet sequence number received.
125  QuicPacketSequenceNumber last_received_packet_sequence_number_;
126  // The size of the most recently received packet.
127  size_t last_received_packet_size_;
128  // The largest packet sequence number received.  In the case where a packet is
129  // received late (out of order), this value will not be updated.
130  QuicPacketSequenceNumber largest_received_packet_sequence_number_;
131  // The largest packet sequence number which the peer has failed to
132  // receive, according to the missing packet set in their ack frames.
133  QuicPacketSequenceNumber largest_received_missing_packet_sequence_number_;
134  // Number of times that the current received packet sequence number is
135  // smaller than the last received packet sequence number.
136  size_t num_out_of_order_received_packets_;
137  // The number of times that OnPacketHeader was called.
138  // If the network replicates packets, then this number may be slightly
139  // different from the real number of distinct packets received.
140  QuicPacketSequenceNumber num_packets_received_;
141  // Number of times a truncated ACK frame was sent.
142  size_t num_truncated_acks_sent_;
143  // Number of times a truncated ACK frame was received.
144  size_t num_truncated_acks_received_;
145  // The kCADR value provided by the server in ServerHello.
146  IPEndPoint local_address_from_shlo_;
147  // The first local address from which a packet was received.
148  IPEndPoint local_address_from_self_;
149  // Count of the number of frames received.
150  int num_frames_received_;
151  // Count of the number of duplicate frames received.
152  int num_duplicate_frames_received_;
153  // Count of the number of packets received with incorrect connection IDs.
154  int num_incorrect_connection_ids_;
155  // Count of the number of undecryptable packets received.
156  int num_undecryptable_packets_;
157  // Count of the number of duplicate packets received.
158  int num_duplicate_packets_;
159  // Vector of inital packets status' indexed by packet sequence numbers, where
160  // false means never received.  Zero is not a valid packet sequence number, so
161  // that offset is never used, and we'll track 150 packets.
162  std::bitset<151> received_packets_;
163  // Vector to indicate which of the initial 150 received packets turned out to
164  // contain solo ACK frames.  An element is true iff an ACK frame was in the
165  // corresponding packet, and there was very little else.
166  std::bitset<151> received_acks_;
167  // The available type of connection (WiFi, 3G, etc.) when connection was first
168  // used.
169  const char* const connection_description_;
170
171  DISALLOW_COPY_AND_ASSIGN(QuicConnectionLogger);
172};
173
174}  // namespace net
175
176#endif  // NET_QUIC_QUIC_CONNECTION_LOGGER_H_
177