xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/quic_network_blackhole_detector.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2020 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 QUICHE_QUIC_CORE_QUIC_NETWORK_BLACKHOLE_DETECTOR_H_
6 #define QUICHE_QUIC_CORE_QUIC_NETWORK_BLACKHOLE_DETECTOR_H_
7 
8 #include "quiche/quic/core/quic_alarm.h"
9 #include "quiche/quic/core/quic_alarm_factory.h"
10 #include "quiche/quic/core/quic_one_block_arena.h"
11 #include "quiche/quic/core/quic_time.h"
12 #include "quiche/quic/platform/api/quic_export.h"
13 #include "quiche/quic/platform/api/quic_flags.h"
14 
15 namespace quic {
16 
17 namespace test {
18 class QuicConnectionPeer;
19 class QuicNetworkBlackholeDetectorPeer;
20 }  // namespace test
21 
22 // QuicNetworkBlackholeDetector can detect path degrading and/or network
23 // blackhole. If both detections are in progress, detector will be in path
24 // degrading detection mode. After reporting path degrading detected, detector
25 // switches to blackhole detection mode. So blackhole detection deadline must
26 // be later than path degrading deadline.
27 class QUICHE_EXPORT QuicNetworkBlackholeDetector {
28  public:
29   class QUICHE_EXPORT Delegate {
30    public:
~Delegate()31     virtual ~Delegate() {}
32 
33     // Called when the path degrading alarm fires.
34     virtual void OnPathDegradingDetected() = 0;
35 
36     // Called when the path blackhole alarm fires.
37     virtual void OnBlackholeDetected() = 0;
38 
39     // Called when the path mtu reduction alarm fires.
40     virtual void OnPathMtuReductionDetected() = 0;
41   };
42 
43   QuicNetworkBlackholeDetector(Delegate* delegate, QuicConnectionArena* arena,
44                                QuicAlarmFactory* alarm_factory,
45                                QuicConnectionContext* context);
46 
47   // Called to stop all detections. If |permanent|, the alarm will be cancelled
48   // permanently and future calls to RestartDetection will be no-op.
49   void StopDetection(bool permanent);
50 
51   // Called to restart path degrading, path mtu reduction and blackhole
52   // detections. Please note, if |blackhole_deadline| is set, it must be the
53   // furthest in the future of all deadlines.
54   void RestartDetection(QuicTime path_degrading_deadline,
55                         QuicTime blackhole_deadline,
56                         QuicTime path_mtu_reduction_deadline);
57 
58   // Called when |alarm_| fires.
59   void OnAlarm();
60 
61   // Returns true if |alarm_| is set.
62   bool IsDetectionInProgress() const;
63 
64  private:
65   friend class test::QuicConnectionPeer;
66   friend class test::QuicNetworkBlackholeDetectorPeer;
67 
68   QuicTime GetEarliestDeadline() const;
69   QuicTime GetLastDeadline() const;
70 
71   // Update alarm to the next deadline.
72   void UpdateAlarm() const;
73 
74   Delegate* delegate_;  // Not owned.
75 
76   // Time that Delegate::OnPathDegrading will be called. 0 means no path
77   // degrading detection is in progress.
78   QuicTime path_degrading_deadline_ = QuicTime::Zero();
79   // Time that Delegate::OnBlackholeDetected will be called. 0 means no
80   // blackhole detection is in progress.
81   QuicTime blackhole_deadline_ = QuicTime::Zero();
82   // Time that Delegate::OnPathMtuReductionDetected will be called. 0 means no
83   // path mtu reduction detection is in progress.
84   QuicTime path_mtu_reduction_deadline_ = QuicTime::Zero();
85 
86   QuicArenaScopedPtr<QuicAlarm> alarm_;
87 };
88 
89 }  // namespace quic
90 
91 #endif  // QUICHE_QUIC_CORE_QUIC_NETWORK_BLACKHOLE_DETECTOR_H_
92