Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -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 | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Junxiao Shi | 192af1f | 2015-01-13 23:19:39 -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. |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 26 | #include "best-route-strategy.hpp" |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 27 | #include "algorithm.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/logger.hpp" |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
| 31 | namespace fw { |
| 32 | |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 33 | NFD_LOG_INIT(BestRouteStrategy); |
| 34 | NFD_REGISTER_STRATEGY(BestRouteStrategy); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 36 | const time::milliseconds BestRouteStrategy::RETX_SUPPRESSION_INITIAL(10); |
| 37 | const time::milliseconds BestRouteStrategy::RETX_SUPPRESSION_MAX(250); |
Junxiao Shi | 52e8540 | 2015-11-11 06:06:58 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 39 | BestRouteStrategy::BestRouteStrategy(Forwarder& forwarder, const Name& name) |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 40 | : Strategy(forwarder) |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 41 | , ProcessNackTraits(this) |
Junxiao Shi | 52e8540 | 2015-11-11 06:06:58 -0700 | [diff] [blame] | 42 | , m_retxSuppression(RETX_SUPPRESSION_INITIAL, |
| 43 | RetxSuppressionExponential::DEFAULT_MULTIPLIER, |
| 44 | RETX_SUPPRESSION_MAX) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 45 | { |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 46 | ParsedInstanceName parsed = parseInstanceName(name); |
| 47 | if (!parsed.parameters.empty()) { |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 48 | NDN_THROW(std::invalid_argument("BestRouteStrategy does not accept parameters")); |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 49 | } |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 50 | if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 51 | NDN_THROW(std::invalid_argument( |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 52 | "BestRouteStrategy does not support version " + to_string(*parsed.version))); |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 53 | } |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 54 | this->setInstanceName(makeInstanceName(name, getStrategyName())); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 57 | const Name& |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 58 | BestRouteStrategy::getStrategyName() |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 59 | { |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 60 | static const auto strategyName = Name("/localhost/nfd/strategy/best-route").appendVersion(5); |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 61 | return strategyName; |
| 62 | } |
| 63 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 64 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 65 | BestRouteStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress, |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 66 | const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 67 | { |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 68 | RetxSuppressionResult suppression = m_retxSuppression.decidePerPitEntry(*pitEntry); |
| 69 | if (suppression == RetxSuppressionResult::SUPPRESS) { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 70 | NFD_LOG_DEBUG(interest << " from=" << ingress << " suppressed"); |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
| 74 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
| 75 | const fib::NextHopList& nexthops = fibEntry.getNextHops(); |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 76 | auto it = nexthops.end(); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 77 | |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 78 | if (suppression == RetxSuppressionResult::NEW) { |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 79 | // forward to nexthop with lowest cost except downstream |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 80 | it = std::find_if(nexthops.begin(), nexthops.end(), [&] (const auto& nexthop) { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 81 | return isNextHopEligible(ingress.face, interest, nexthop, pitEntry); |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 82 | }); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 83 | |
| 84 | if (it == nexthops.end()) { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 85 | NFD_LOG_DEBUG(interest << " from=" << ingress << " noNextHop"); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 86 | |
| 87 | lp::NackHeader nackHeader; |
| 88 | nackHeader.setReason(lp::NackReason::NO_ROUTE); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 89 | this->sendNack(nackHeader, ingress.face, pitEntry); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 90 | this->rejectPendingInterest(pitEntry); |
| 91 | return; |
| 92 | } |
| 93 | |
Teng Liang | ebc20f6 | 2020-06-23 16:55:20 -0700 | [diff] [blame] | 94 | Face& outFace = it->getFace(); |
| 95 | NFD_LOG_DEBUG(interest << " from=" << ingress << " newPitEntry-to=" << outFace.getId()); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 96 | this->sendInterest(interest, outFace, pitEntry); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 100 | // find an unused upstream with lowest cost except downstream |
Teng Liang | ebc20f6 | 2020-06-23 16:55:20 -0700 | [diff] [blame] | 101 | it = std::find_if(nexthops.begin(), nexthops.end(), |
| 102 | [&, now = time::steady_clock::now()] (const auto& nexthop) { |
| 103 | return isNextHopEligible(ingress.face, interest, nexthop, pitEntry, true, now); |
| 104 | }); |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 105 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 106 | if (it != nexthops.end()) { |
Teng Liang | ebc20f6 | 2020-06-23 16:55:20 -0700 | [diff] [blame] | 107 | Face& outFace = it->getFace(); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 108 | this->sendInterest(interest, outFace, pitEntry); |
Teng Liang | ebc20f6 | 2020-06-23 16:55:20 -0700 | [diff] [blame] | 109 | NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-unused-to=" << outFace.getId()); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
| 113 | // find an eligible upstream that is used earliest |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 114 | it = findEligibleNextHopWithEarliestOutRecord(ingress.face, interest, nexthops, pitEntry); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 115 | if (it == nexthops.end()) { |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 116 | NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmitNoNextHop"); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 117 | } |
| 118 | else { |
Teng Liang | ebc20f6 | 2020-06-23 16:55:20 -0700 | [diff] [blame] | 119 | Face& outFace = it->getFace(); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 120 | this->sendInterest(interest, outFace, pitEntry); |
Teng Liang | ebc20f6 | 2020-06-23 16:55:20 -0700 | [diff] [blame] | 121 | NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-retry-to=" << outFace.getId()); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 125 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 126 | BestRouteStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress, |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 127 | const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 128 | { |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 129 | this->processNack(nack, ingress.face, pitEntry); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 132 | } // namespace fw |
| 133 | } // namespace nfd |