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