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 #include "quiche/quic/core/quic_idle_network_detector.h"
6
7 #include "quiche/quic/core/quic_constants.h"
8 #include "quiche/quic/core/quic_time.h"
9 #include "quiche/quic/platform/api/quic_flag_utils.h"
10 #include "quiche/quic/platform/api/quic_flags.h"
11 #include "quiche/common/platform/api/quiche_logging.h"
12
13 namespace quic {
14
15 namespace {
16
17 class AlarmDelegate : public QuicAlarm::DelegateWithContext {
18 public:
AlarmDelegate(QuicIdleNetworkDetector * detector,QuicConnectionContext * context)19 explicit AlarmDelegate(QuicIdleNetworkDetector* detector,
20 QuicConnectionContext* context)
21 : QuicAlarm::DelegateWithContext(context), detector_(detector) {}
22 AlarmDelegate(const AlarmDelegate&) = delete;
23 AlarmDelegate& operator=(const AlarmDelegate&) = delete;
24
OnAlarm()25 void OnAlarm() override { detector_->OnAlarm(); }
26
27 private:
28 QuicIdleNetworkDetector* detector_;
29 };
30
31 } // namespace
32
QuicIdleNetworkDetector(Delegate * delegate,QuicTime now,QuicConnectionArena * arena,QuicAlarmFactory * alarm_factory,QuicConnectionContext * context)33 QuicIdleNetworkDetector::QuicIdleNetworkDetector(
34 Delegate* delegate, QuicTime now, QuicConnectionArena* arena,
35 QuicAlarmFactory* alarm_factory, QuicConnectionContext* context)
36 : delegate_(delegate),
37 start_time_(now),
38 handshake_timeout_(QuicTime::Delta::Infinite()),
39 time_of_last_received_packet_(now),
40 time_of_first_packet_sent_after_receiving_(QuicTime::Zero()),
41 idle_network_timeout_(QuicTime::Delta::Infinite()),
42 alarm_(alarm_factory->CreateAlarm(
43 arena->New<AlarmDelegate>(this, context), arena)) {}
44
OnAlarm()45 void QuicIdleNetworkDetector::OnAlarm() {
46 if (handshake_timeout_.IsInfinite()) {
47 delegate_->OnIdleNetworkDetected();
48 return;
49 }
50 if (idle_network_timeout_.IsInfinite()) {
51 delegate_->OnHandshakeTimeout();
52 return;
53 }
54 if (last_network_activity_time() + idle_network_timeout_ >
55 start_time_ + handshake_timeout_) {
56 delegate_->OnHandshakeTimeout();
57 return;
58 }
59 delegate_->OnIdleNetworkDetected();
60 }
61
SetTimeouts(QuicTime::Delta handshake_timeout,QuicTime::Delta idle_network_timeout)62 void QuicIdleNetworkDetector::SetTimeouts(
63 QuicTime::Delta handshake_timeout, QuicTime::Delta idle_network_timeout) {
64 handshake_timeout_ = handshake_timeout;
65 idle_network_timeout_ = idle_network_timeout;
66
67 SetAlarm();
68 }
69
StopDetection()70 void QuicIdleNetworkDetector::StopDetection() {
71 alarm_->PermanentCancel();
72 handshake_timeout_ = QuicTime::Delta::Infinite();
73 idle_network_timeout_ = QuicTime::Delta::Infinite();
74 handshake_timeout_ = QuicTime::Delta::Infinite();
75 stopped_ = true;
76 }
77
OnPacketSent(QuicTime now,QuicTime::Delta pto_delay)78 void QuicIdleNetworkDetector::OnPacketSent(QuicTime now,
79 QuicTime::Delta pto_delay) {
80 if (time_of_first_packet_sent_after_receiving_ >
81 time_of_last_received_packet_) {
82 return;
83 }
84 time_of_first_packet_sent_after_receiving_ =
85 std::max(time_of_first_packet_sent_after_receiving_, now);
86 if (shorter_idle_timeout_on_sent_packet_) {
87 MaybeSetAlarmOnSentPacket(pto_delay);
88 return;
89 }
90
91 SetAlarm();
92 }
93
OnPacketReceived(QuicTime now)94 void QuicIdleNetworkDetector::OnPacketReceived(QuicTime now) {
95 time_of_last_received_packet_ = std::max(time_of_last_received_packet_, now);
96
97 SetAlarm();
98 }
99
SetAlarm()100 void QuicIdleNetworkDetector::SetAlarm() {
101 if (stopped_) {
102 // TODO(wub): If this QUIC_BUG fires, it indicates a problem in the
103 // QuicConnection, which somehow called this function while disconnected.
104 // That problem needs to be fixed.
105 QUIC_BUG(quic_idle_detector_set_alarm_after_stopped)
106 << "SetAlarm called after stopped";
107 return;
108 }
109 // Set alarm to the nearer deadline.
110 QuicTime new_deadline = QuicTime::Zero();
111 if (!handshake_timeout_.IsInfinite()) {
112 new_deadline = start_time_ + handshake_timeout_;
113 }
114 if (!idle_network_timeout_.IsInfinite()) {
115 const QuicTime idle_network_deadline = GetIdleNetworkDeadline();
116 if (new_deadline.IsInitialized()) {
117 new_deadline = std::min(new_deadline, idle_network_deadline);
118 } else {
119 new_deadline = idle_network_deadline;
120 }
121 }
122 alarm_->Update(new_deadline, kAlarmGranularity);
123 }
124
MaybeSetAlarmOnSentPacket(QuicTime::Delta pto_delay)125 void QuicIdleNetworkDetector::MaybeSetAlarmOnSentPacket(
126 QuicTime::Delta pto_delay) {
127 QUICHE_DCHECK(shorter_idle_timeout_on_sent_packet_);
128 if (!handshake_timeout_.IsInfinite() || !alarm_->IsSet()) {
129 SetAlarm();
130 return;
131 }
132 // Make sure connection will be alive for another PTO.
133 const QuicTime deadline = alarm_->deadline();
134 const QuicTime min_deadline = last_network_activity_time() + pto_delay;
135 if (deadline > min_deadline) {
136 return;
137 }
138 alarm_->Update(min_deadline, kAlarmGranularity);
139 }
140
GetIdleNetworkDeadline() const141 QuicTime QuicIdleNetworkDetector::GetIdleNetworkDeadline() const {
142 if (idle_network_timeout_.IsInfinite()) {
143 return QuicTime::Zero();
144 }
145 return last_network_activity_time() + idle_network_timeout_;
146 }
147
148 } // namespace quic
149