blob: eecc3b53598dcb9013d95990641247de8d341124 [file] [log] [blame]
Junxiao Shi986b8492014-08-20 12:07:14 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande90015992017-07-11 17:25:48 -05002/*
Davide Pesavento7922d122021-03-05 22:41:23 -05003 * Copyright (c) 2014-2021, Regents of the University of California,
Junxiao Shi192af1f2015-01-13 23:19:39 -07004 * 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 Shi986b8492014-08-20 12:07:14 -070010 *
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 Pesavento7922d122021-03-05 22:41:23 -050026#include "best-route-strategy.hpp"
Junxiao Shi00dc9142016-11-21 14:23:12 +000027#include "algorithm.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/logger.hpp"
Junxiao Shi986b8492014-08-20 12:07:14 -070029
30namespace nfd {
31namespace fw {
32
Davide Pesavento7922d122021-03-05 22:41:23 -050033NFD_LOG_INIT(BestRouteStrategy);
34NFD_REGISTER_STRATEGY(BestRouteStrategy);
Junxiao Shi986b8492014-08-20 12:07:14 -070035
Davide Pesavento7922d122021-03-05 22:41:23 -050036const time::milliseconds BestRouteStrategy::RETX_SUPPRESSION_INITIAL(10);
37const time::milliseconds BestRouteStrategy::RETX_SUPPRESSION_MAX(250);
Junxiao Shi52e85402015-11-11 06:06:58 -070038
Davide Pesavento7922d122021-03-05 22:41:23 -050039BestRouteStrategy::BestRouteStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi18739c42016-12-22 08:03:00 +000040 : Strategy(forwarder)
Junxiao Shi99540072017-01-27 19:57:33 +000041 , ProcessNackTraits(this)
Junxiao Shi52e85402015-11-11 06:06:58 -070042 , m_retxSuppression(RETX_SUPPRESSION_INITIAL,
43 RetxSuppressionExponential::DEFAULT_MULTIPLIER,
44 RETX_SUPPRESSION_MAX)
Junxiao Shi986b8492014-08-20 12:07:14 -070045{
Junxiao Shi18739c42016-12-22 08:03:00 +000046 ParsedInstanceName parsed = parseInstanceName(name);
47 if (!parsed.parameters.empty()) {
Davide Pesavento7922d122021-03-05 22:41:23 -050048 NDN_THROW(std::invalid_argument("BestRouteStrategy does not accept parameters"));
Junxiao Shi18739c42016-12-22 08:03:00 +000049 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000050 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050051 NDN_THROW(std::invalid_argument(
Davide Pesavento7922d122021-03-05 22:41:23 -050052 "BestRouteStrategy does not support version " + to_string(*parsed.version)));
Junxiao Shi91f6ee02016-12-29 21:44:44 +000053 }
Junxiao Shi18739c42016-12-22 08:03:00 +000054 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Junxiao Shi986b8492014-08-20 12:07:14 -070055}
56
Junxiao Shi037f4ab2016-12-13 04:27:06 +000057const Name&
Davide Pesavento7922d122021-03-05 22:41:23 -050058BestRouteStrategy::getStrategyName()
Junxiao Shi037f4ab2016-12-13 04:27:06 +000059{
Eric Newberry358414d2021-03-21 20:56:42 -070060 static const auto strategyName = Name("/localhost/nfd/strategy/best-route").appendVersion(5);
Junxiao Shi037f4ab2016-12-13 04:27:06 +000061 return strategyName;
62}
63
Junxiao Shi986b8492014-08-20 12:07:14 -070064void
Davide Pesavento0498ce82021-06-14 02:02:21 -040065BestRouteStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Davide Pesavento7922d122021-03-05 22:41:23 -050066 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -070067{
Ashlesh Gawande90015992017-07-11 17:25:48 -050068 RetxSuppressionResult suppression = m_retxSuppression.decidePerPitEntry(*pitEntry);
69 if (suppression == RetxSuppressionResult::SUPPRESS) {
ashiqopuc7079482019-02-20 05:34:37 +000070 NFD_LOG_DEBUG(interest << " from=" << ingress << " suppressed");
Junxiao Shi8d843142016-07-11 22:42:42 +000071 return;
72 }
73
74 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
75 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Davide Pesaventoe4b22382018-06-10 14:37:24 -040076 auto it = nexthops.end();
Junxiao Shi986b8492014-08-20 12:07:14 -070077
Ashlesh Gawande90015992017-07-11 17:25:48 -050078 if (suppression == RetxSuppressionResult::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -070079 // forward to nexthop with lowest cost except downstream
Davide Pesaventoe4b22382018-06-10 14:37:24 -040080 it = std::find_if(nexthops.begin(), nexthops.end(), [&] (const auto& nexthop) {
ashiqopuc7079482019-02-20 05:34:37 +000081 return isNextHopEligible(ingress.face, interest, nexthop, pitEntry);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040082 });
Junxiao Shi986b8492014-08-20 12:07:14 -070083
84 if (it == nexthops.end()) {
ashiqopuc7079482019-02-20 05:34:37 +000085 NFD_LOG_DEBUG(interest << " from=" << ingress << " noNextHop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -070086
87 lp::NackHeader nackHeader;
88 nackHeader.setReason(lp::NackReason::NO_ROUTE);
Davide Pesavento0498ce82021-06-14 02:02:21 -040089 this->sendNack(nackHeader, ingress.face, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -070090 this->rejectPendingInterest(pitEntry);
91 return;
92 }
93
Teng Liangebc20f62020-06-23 16:55:20 -070094 Face& outFace = it->getFace();
95 NFD_LOG_DEBUG(interest << " from=" << ingress << " newPitEntry-to=" << outFace.getId());
Davide Pesavento0498ce82021-06-14 02:02:21 -040096 this->sendInterest(interest, outFace, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -070097 return;
98 }
99
Junxiao Shi986b8492014-08-20 12:07:14 -0700100 // find an unused upstream with lowest cost except downstream
Teng Liangebc20f62020-06-23 16:55:20 -0700101 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 Pesaventoe4b22382018-06-10 14:37:24 -0400105
Junxiao Shi986b8492014-08-20 12:07:14 -0700106 if (it != nexthops.end()) {
Teng Liangebc20f62020-06-23 16:55:20 -0700107 Face& outFace = it->getFace();
Davide Pesavento0498ce82021-06-14 02:02:21 -0400108 this->sendInterest(interest, outFace, pitEntry);
Teng Liangebc20f62020-06-23 16:55:20 -0700109 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-unused-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700110 return;
111 }
112
113 // find an eligible upstream that is used earliest
ashiqopuc7079482019-02-20 05:34:37 +0000114 it = findEligibleNextHopWithEarliestOutRecord(ingress.face, interest, nexthops, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -0700115 if (it == nexthops.end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000116 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmitNoNextHop");
Junxiao Shi986b8492014-08-20 12:07:14 -0700117 }
118 else {
Teng Liangebc20f62020-06-23 16:55:20 -0700119 Face& outFace = it->getFace();
Davide Pesavento0498ce82021-06-14 02:02:21 -0400120 this->sendInterest(interest, outFace, pitEntry);
Teng Liangebc20f62020-06-23 16:55:20 -0700121 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-retry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700122 }
123}
124
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700125void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400126BestRouteStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
Davide Pesavento7922d122021-03-05 22:41:23 -0500127 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700128{
Davide Pesavento0498ce82021-06-14 02:02:21 -0400129 this->processNack(nack, ingress.face, pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700130}
131
Junxiao Shi986b8492014-08-20 12:07:14 -0700132} // namespace fw
133} // namespace nfd