blob: 11ae600dad487fa488a808d3907ab8a1a6f16563 [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{
Teng Liangf995f382017-04-04 22:09:39 +000060 static Name strategyName("/localhost/nfd/strategy/best-route/%FD%05");
Junxiao Shi037f4ab2016-12-13 04:27:06 +000061 return strategyName;
62}
63
Junxiao Shi986b8492014-08-20 12:07:14 -070064void
Davide Pesavento7922d122021-03-05 22:41:23 -050065BestRouteStrategy::afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
66 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);
Teng Liangebc20f62020-06-23 16:55:20 -070089 this->sendNack(pitEntry, ingress.face, nackHeader);
Junxiao Shi5e5e4452015-09-24 16:56:52 -070090
Junxiao Shi986b8492014-08-20 12:07:14 -070091 this->rejectPendingInterest(pitEntry);
92 return;
93 }
94
Teng Liangebc20f62020-06-23 16:55:20 -070095 Face& outFace = it->getFace();
96 NFD_LOG_DEBUG(interest << " from=" << ingress << " newPitEntry-to=" << outFace.getId());
97 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -070098 return;
99 }
100
Junxiao Shi986b8492014-08-20 12:07:14 -0700101 // find an unused upstream with lowest cost except downstream
Teng Liangebc20f62020-06-23 16:55:20 -0700102 it = std::find_if(nexthops.begin(), nexthops.end(),
103 [&, now = time::steady_clock::now()] (const auto& nexthop) {
104 return isNextHopEligible(ingress.face, interest, nexthop, pitEntry, true, now);
105 });
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400106
Junxiao Shi986b8492014-08-20 12:07:14 -0700107 if (it != nexthops.end()) {
Teng Liangebc20f62020-06-23 16:55:20 -0700108 Face& outFace = it->getFace();
109 this->sendInterest(pitEntry, outFace, interest);
110 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-unused-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700111 return;
112 }
113
114 // find an eligible upstream that is used earliest
ashiqopuc7079482019-02-20 05:34:37 +0000115 it = findEligibleNextHopWithEarliestOutRecord(ingress.face, interest, nexthops, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -0700116 if (it == nexthops.end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000117 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmitNoNextHop");
Junxiao Shi986b8492014-08-20 12:07:14 -0700118 }
119 else {
Teng Liangebc20f62020-06-23 16:55:20 -0700120 Face& outFace = it->getFace();
121 this->sendInterest(pitEntry, outFace, interest);
122 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-retry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700123 }
124}
125
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700126void
Davide Pesavento7922d122021-03-05 22:41:23 -0500127BestRouteStrategy::afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack& nack,
128 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700129{
ashiqopuc7079482019-02-20 05:34:37 +0000130 this->processNack(ingress.face, nack, pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700131}
132
Junxiao Shi986b8492014-08-20 12:07:14 -0700133} // namespace fw
134} // namespace nfd