summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/net/dcsctp/tx/retransmission_timeout.cc
blob: 7d8fb9761ced374faf9938aa0eccb760acde15d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include "net/dcsctp/tx/retransmission_timeout.h"

#include <algorithm>
#include <cstdint>

#include "net/dcsctp/public/dcsctp_options.h"

namespace dcsctp {

RetransmissionTimeout::RetransmissionTimeout(const DcSctpOptions& options)
    : min_rto_(*options.rto_min),
      max_rto_(*options.rto_max),
      max_rtt_(*options.rtt_max),
      min_rtt_variance_(*options.min_rtt_variance),
      scaled_srtt_(*options.rto_initial << kRttShift),
      rto_(*options.rto_initial) {}

void RetransmissionTimeout::ObserveRTT(DurationMs measured_rtt) {
  const int32_t rtt = *measured_rtt;

  // Unrealistic values will be skipped. If a wrongly measured (or otherwise
  // corrupt) value was processed, it could change the state in a way that would
  // take a very long time to recover.
  if (rtt < 0 || rtt > max_rtt_) {
    return;
  }

  // From https://tools.ietf.org/html/rfc4960#section-6.3.1, but avoiding
  // floating point math by implementing algorithm from "V. Jacobson: Congestion
  // avoidance and control", but adapted for SCTP.
  if (first_measurement_) {
    scaled_srtt_ = rtt << kRttShift;
    scaled_rtt_var_ = (rtt / 2) << kRttVarShift;
    first_measurement_ = false;
  } else {
    int32_t rtt_diff = rtt - (scaled_srtt_ >> kRttShift);
    scaled_srtt_ += rtt_diff;
    if (rtt_diff < 0) {
      rtt_diff = -rtt_diff;
    }
    rtt_diff -= (scaled_rtt_var_ >> kRttVarShift);
    scaled_rtt_var_ += rtt_diff;
  }

  if (scaled_rtt_var_ < min_rtt_variance_) {
    scaled_rtt_var_ = min_rtt_variance_;
  }

  rto_ = (scaled_srtt_ >> kRttShift) + scaled_rtt_var_;

  // Clamp RTO between min and max.
  rto_ = std::min(std::max(rto_, min_rto_), max_rto_);
}
}  // namespace dcsctp