Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | b124b8e | 2024-02-16 16:54:26 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "retx-suppression-exponential.hpp" |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 27 | #include "algorithm.hpp" |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 29 | namespace nfd::fw { |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 30 | |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | class PitInfo final : public StrategyInfo |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 34 | { |
| 35 | public: |
| 36 | static constexpr int |
| 37 | getTypeId() |
| 38 | { |
| 39 | return 1020; |
| 40 | } |
| 41 | |
| 42 | explicit |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 43 | PitInfo(const RetxSuppressionExponential::Duration& initialInterval) |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 44 | : suppressionInterval(initialInterval) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | public: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 49 | // If the last transmission occurred within this interval, retx will be suppressed |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 50 | RetxSuppressionExponential::Duration suppressionInterval; |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 53 | } // namespace |
| 54 | |
| 55 | RetxSuppressionExponential::RetxSuppressionExponential(Duration initialInterval, |
| 56 | Duration maxInterval, |
| 57 | float multiplier) |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 58 | : m_initialInterval(initialInterval) |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 59 | , m_maxInterval(maxInterval) |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 60 | , m_multiplier(multiplier) |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 61 | { |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 62 | if (m_initialInterval <= 0_ns) { |
| 63 | NDN_THROW(std::invalid_argument("Retx suppression initial interval must be > 0")); |
| 64 | } |
| 65 | if (m_maxInterval < m_initialInterval) { |
| 66 | NDN_THROW(std::invalid_argument("Retx suppression max interval must be >= initial interval")); |
| 67 | } |
| 68 | if (m_multiplier < 1.0f) { |
| 69 | NDN_THROW(std::invalid_argument("Retx suppression multiplier must be >= 1")); |
| 70 | } |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 73 | RetxSuppressionResult |
| 74 | RetxSuppressionExponential::decidePerPitEntry(pit::Entry& pitEntry) |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 75 | { |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 76 | bool isNewPitEntry = !hasPendingOutRecords(pitEntry); |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 77 | if (isNewPitEntry) { |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 78 | return RetxSuppressionResult::NEW; |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 81 | auto lastOutgoing = getLastOutgoing(pitEntry); |
| 82 | auto now = time::steady_clock::now(); |
| 83 | auto sinceLastOutgoing = now - lastOutgoing; |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 84 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 85 | PitInfo* pi = pitEntry.insertStrategyInfo<PitInfo>(m_initialInterval).first; |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 86 | bool shouldSuppress = sinceLastOutgoing < pi->suppressionInterval; |
| 87 | |
| 88 | if (shouldSuppress) { |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 89 | return RetxSuppressionResult::SUPPRESS; |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | pi->suppressionInterval = std::min(m_maxInterval, |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 93 | time::duration_cast<Duration>(pi->suppressionInterval * m_multiplier)); |
| 94 | |
| 95 | return RetxSuppressionResult::FORWARD; |
| 96 | } |
| 97 | |
| 98 | RetxSuppressionResult |
| 99 | RetxSuppressionExponential::decidePerUpstream(pit::Entry& pitEntry, Face& outFace) |
| 100 | { |
| 101 | // NEW if outRecord for the face does not exist |
Davide Pesavento | b124b8e | 2024-02-16 16:54:26 -0500 | [diff] [blame] | 102 | auto outRecord = pitEntry.findOutRecord(outFace); |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 103 | if (outRecord == pitEntry.out_end()) { |
| 104 | return RetxSuppressionResult::NEW; |
| 105 | } |
| 106 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 107 | auto lastOutgoing = outRecord->getLastRenewed(); |
| 108 | auto now = time::steady_clock::now(); |
| 109 | auto sinceLastOutgoing = now - lastOutgoing; |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 110 | |
| 111 | // insertStrategyInfo does not insert m_initialInterval again if it already exists |
| 112 | PitInfo* pi = outRecord->insertStrategyInfo<PitInfo>(m_initialInterval).first; |
| 113 | bool shouldSuppress = sinceLastOutgoing < pi->suppressionInterval; |
| 114 | |
| 115 | if (shouldSuppress) { |
| 116 | return RetxSuppressionResult::SUPPRESS; |
| 117 | } |
| 118 | |
| 119 | return RetxSuppressionResult::FORWARD; |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | RetxSuppressionExponential::incrementIntervalForOutRecord(pit::OutRecord& outRecord) |
| 124 | { |
| 125 | PitInfo* pi = outRecord.insertStrategyInfo<PitInfo>(m_initialInterval).first; |
| 126 | pi->suppressionInterval = std::min(m_maxInterval, |
| 127 | time::duration_cast<Duration>(pi->suppressionInterval * m_multiplier)); |
Junxiao Shi | 73b909a | 2015-02-08 21:31:42 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Ashlesh Gawande | 1ef93d0 | 2022-04-08 00:25:06 -0400 | [diff] [blame] | 130 | std::unique_ptr<RetxSuppressionExponential> |
| 131 | RetxSuppressionExponential::construct(const StrategyParameters& params) |
| 132 | { |
| 133 | auto init = params.getOrDefault<Duration::rep>("retx-suppression-initial", |
| 134 | RetxSuppressionExponential::DEFAULT_INITIAL_INTERVAL.count()); |
| 135 | auto max = params.getOrDefault<Duration::rep>("retx-suppression-max", |
| 136 | RetxSuppressionExponential::DEFAULT_MAX_INTERVAL.count()); |
| 137 | auto mult = params.getOrDefault<float>("retx-suppression-multiplier", |
| 138 | RetxSuppressionExponential::DEFAULT_MULTIPLIER); |
| 139 | |
| 140 | return make_unique<RetxSuppressionExponential>(Duration(init), Duration(max), mult); |
| 141 | } |
| 142 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 143 | } // namespace nfd::fw |