1 // Copyright 2020 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_CONNECTIVITY_MONITOR_H_ 6 #define NET_QUIC_QUIC_CONNECTIVITY_MONITOR_H_ 7 8 #include <set> 9 10 #include "base/memory/raw_ptr.h" 11 #include "base/numerics/clamped_math.h" 12 #include "net/base/network_handle.h" 13 #include "net/quic/quic_chromium_client_session.h" 14 15 namespace net { 16 17 // Responsible for monitoring path degrading detection/recovery events on the 18 // default network interface. 19 // Reset all raw observations (reported by sessions) when the default network 20 // is changed, which happens either: 21 // - via OnDefaultNetworkUpdated if handles::NetworkHandle is supported on the 22 // platform; 23 // - via OnIPAddressChanged otherwise. 24 class NET_EXPORT_PRIVATE QuicConnectivityMonitor 25 : public QuicChromiumClientSession::ConnectivityObserver { 26 public: 27 explicit QuicConnectivityMonitor(handles::NetworkHandle default_network); 28 29 QuicConnectivityMonitor(const QuicConnectivityMonitor&) = delete; 30 QuicConnectivityMonitor& operator=(const QuicConnectivityMonitor&) = delete; 31 32 ~QuicConnectivityMonitor() override; 33 34 // Records connectivity related stats to histograms. 35 void RecordConnectivityStatsToHistograms( 36 const std::string& platform_notification, 37 handles::NetworkHandle affected_network) const; 38 39 // Returns the number of sessions that are currently degrading on the default 40 // network interface. 41 size_t GetNumDegradingSessions() const; 42 43 // Returns the number of reports received for |write_error_code| on 44 // |default_network|. 45 size_t GetCountForWriteErrorCode(int write_error_code) const; 46 47 // Called to set up the initial default network, which happens when the 48 // default network tracking is lost upon |this| creation. 49 void SetInitialDefaultNetwork(handles::NetworkHandle default_network); 50 51 // Called when handles::NetworkHandle is supported and the default network 52 // interface used by the platform is updated. 53 void OnDefaultNetworkUpdated(handles::NetworkHandle default_network); 54 55 // Called when handles::NetworkHandle is NOT supported and the IP address of 56 // the primary interface changes. This includes when the primary interface 57 // itself changes. 58 void OnIPAddressChanged(); 59 60 // Called when |session| is marked as going away due to IP address change. 61 void OnSessionGoingAwayOnIPAddressChange(QuicChromiumClientSession* session); 62 63 // QuicChromiumClientSession::ConnectivityObserver implementation. 64 void OnSessionPathDegrading(QuicChromiumClientSession* session, 65 handles::NetworkHandle network) override; 66 67 void OnSessionResumedPostPathDegrading( 68 QuicChromiumClientSession* session, 69 handles::NetworkHandle network) override; 70 71 void OnSessionEncounteringWriteError(QuicChromiumClientSession* session, 72 handles::NetworkHandle network, 73 int error_code) override; 74 75 void OnSessionClosedAfterHandshake(QuicChromiumClientSession* session, 76 handles::NetworkHandle network, 77 quic::ConnectionCloseSource source, 78 quic::QuicErrorCode error_code) override; 79 80 void OnSessionRegistered(QuicChromiumClientSession* session, 81 handles::NetworkHandle network) override; 82 83 void OnSessionRemoved(QuicChromiumClientSession* session) override; 84 85 private: 86 // Size chosen per net.QuicSession.WriteError histogram. 87 using WriteErrorMap = base::flat_map<int, size_t>; 88 // The most common QuicErrorCode cared by this monitor is: 89 // QUIC_PUBLIC_RESET by the peer, or 90 // QUIC_PACKET_WRITE_ERROR/QUIC_TOO_MANY_RTOS by self. 91 using QuicErrorCodeMap = base::flat_map<quic::QuicErrorCode, size_t>; 92 93 // If handles::NetworkHandle is not supported, always set to 94 // handles::kInvalidNetworkHandle. 95 handles::NetworkHandle default_network_; 96 // Sessions that are currently degrading on the |default_network_|. 97 std::set<raw_ptr<QuicChromiumClientSession, SetExperimental>> 98 degrading_sessions_; 99 // Sessions that are currently active on the |default_network_|. 100 std::set<raw_ptr<QuicChromiumClientSession, SetExperimental>> 101 active_sessions_; 102 103 // Number of sessions that have been active or created during the period of 104 // a speculative connectivity failure. 105 // The period of a speculative connectivity failure 106 // - starts by the earliest detection of path degradation or a connectivity 107 // related packet write error, 108 // - ends immediately by the detection of path recovery or a network change. 109 // Use clamped math to cap number of sessions at INT_MAX. 110 std::optional<base::ClampedNumeric<int>> 111 num_sessions_active_during_current_speculative_connectivity_failure_; 112 // Total number of sessions that has been degraded before any recovery, 113 // including no longer active sessions. 114 // Use clamped math to cap number of sessions at INT_MAX. 115 base::ClampedNumeric<int> num_all_degraded_sessions_{0}; 116 117 // Map from the write error code to the corresponding number of reports. 118 WriteErrorMap write_error_map_; 119 QuicErrorCodeMap quic_error_map_; 120 121 base::WeakPtrFactory<QuicConnectivityMonitor> weak_factory_{this}; 122 }; 123 124 } // namespace net 125 126 #endif // NET_QUIC_QUIC_CONNECTIVITY_MONITOR_H_ 127