blob: a275b3ac771360eb47962e5e267a3d269aed92e3 [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/*
Alex Lane653eb072023-07-27 22:11:46 -04003 * Copyright (c) 2014-2023, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::fw {
Junxiao Shi986b8492014-08-20 12:07:14 -070031
Davide Pesavento7922d122021-03-05 22:41:23 -050032NFD_LOG_INIT(BestRouteStrategy);
33NFD_REGISTER_STRATEGY(BestRouteStrategy);
Junxiao Shi986b8492014-08-20 12:07:14 -070034
Davide Pesavento7922d122021-03-05 22:41:23 -050035BestRouteStrategy::BestRouteStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi18739c42016-12-22 08:03:00 +000036 : Strategy(forwarder)
Junxiao Shi99540072017-01-27 19:57:33 +000037 , ProcessNackTraits(this)
Junxiao Shi986b8492014-08-20 12:07:14 -070038{
Junxiao Shi18739c42016-12-22 08:03:00 +000039 ParsedInstanceName parsed = parseInstanceName(name);
Junxiao Shi91f6ee02016-12-29 21:44:44 +000040 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050041 NDN_THROW(std::invalid_argument(
Davide Pesavento7922d122021-03-05 22:41:23 -050042 "BestRouteStrategy does not support version " + to_string(*parsed.version)));
Junxiao Shi91f6ee02016-12-29 21:44:44 +000043 }
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -040044
45 StrategyParameters params = parseParameters(parsed.parameters);
46 m_retxSuppression = RetxSuppressionExponential::construct(params);
47
Junxiao Shi18739c42016-12-22 08:03:00 +000048 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -040049
50 NDN_LOG_DEBUG(*m_retxSuppression);
Junxiao Shi986b8492014-08-20 12:07:14 -070051}
52
Junxiao Shi037f4ab2016-12-13 04:27:06 +000053const Name&
Davide Pesavento7922d122021-03-05 22:41:23 -050054BestRouteStrategy::getStrategyName()
Junxiao Shi037f4ab2016-12-13 04:27:06 +000055{
Eric Newberry358414d2021-03-21 20:56:42 -070056 static const auto strategyName = Name("/localhost/nfd/strategy/best-route").appendVersion(5);
Junxiao Shi037f4ab2016-12-13 04:27:06 +000057 return strategyName;
58}
59
Junxiao Shi986b8492014-08-20 12:07:14 -070060void
Davide Pesavento0498ce82021-06-14 02:02:21 -040061BestRouteStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Davide Pesavento7922d122021-03-05 22:41:23 -050062 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -070063{
Alex Lane653eb072023-07-27 22:11:46 -040064 auto suppression = m_retxSuppression->decidePerPitEntry(*pitEntry);
Ashlesh Gawande90015992017-07-11 17:25:48 -050065 if (suppression == RetxSuppressionResult::SUPPRESS) {
Alex Lane653eb072023-07-27 22:11:46 -040066 NFD_LOG_INTEREST_FROM(interest, ingress, "suppressed");
Junxiao Shi8d843142016-07-11 22:42:42 +000067 return;
68 }
69
70 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
71 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Davide Pesaventoe4b22382018-06-10 14:37:24 -040072 auto it = nexthops.end();
Junxiao Shi986b8492014-08-20 12:07:14 -070073
Ashlesh Gawande90015992017-07-11 17:25:48 -050074 if (suppression == RetxSuppressionResult::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -070075 // forward to nexthop with lowest cost except downstream
Davide Pesaventoe4b22382018-06-10 14:37:24 -040076 it = std::find_if(nexthops.begin(), nexthops.end(), [&] (const auto& nexthop) {
ashiqopuc7079482019-02-20 05:34:37 +000077 return isNextHopEligible(ingress.face, interest, nexthop, pitEntry);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040078 });
Junxiao Shi986b8492014-08-20 12:07:14 -070079
80 if (it == nexthops.end()) {
Alex Lane653eb072023-07-27 22:11:46 -040081 NFD_LOG_INTEREST_FROM(interest, ingress, "new no-nexthop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -070082 lp::NackHeader nackHeader;
83 nackHeader.setReason(lp::NackReason::NO_ROUTE);
Davide Pesavento0498ce82021-06-14 02:02:21 -040084 this->sendNack(nackHeader, ingress.face, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -070085 this->rejectPendingInterest(pitEntry);
86 return;
87 }
88
Teng Liangebc20f62020-06-23 16:55:20 -070089 Face& outFace = it->getFace();
Alex Lane653eb072023-07-27 22:11:46 -040090 NFD_LOG_INTEREST_FROM(interest, ingress, "new to=" << outFace.getId());
Davide Pesavento0498ce82021-06-14 02:02:21 -040091 this->sendInterest(interest, outFace, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -070092 return;
93 }
94
Junxiao Shi986b8492014-08-20 12:07:14 -070095 // find an unused upstream with lowest cost except downstream
Teng Liangebc20f62020-06-23 16:55:20 -070096 it = std::find_if(nexthops.begin(), nexthops.end(),
97 [&, now = time::steady_clock::now()] (const auto& nexthop) {
98 return isNextHopEligible(ingress.face, interest, nexthop, pitEntry, true, now);
99 });
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400100
Junxiao Shi986b8492014-08-20 12:07:14 -0700101 if (it != nexthops.end()) {
Teng Liangebc20f62020-06-23 16:55:20 -0700102 Face& outFace = it->getFace();
Davide Pesavento0498ce82021-06-14 02:02:21 -0400103 this->sendInterest(interest, outFace, pitEntry);
Alex Lane653eb072023-07-27 22:11:46 -0400104 NFD_LOG_INTEREST_FROM(interest, ingress, "retx unused-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700105 return;
106 }
107
108 // find an eligible upstream that is used earliest
ashiqopuc7079482019-02-20 05:34:37 +0000109 it = findEligibleNextHopWithEarliestOutRecord(ingress.face, interest, nexthops, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -0700110 if (it == nexthops.end()) {
Alex Lane653eb072023-07-27 22:11:46 -0400111 NFD_LOG_INTEREST_FROM(interest, ingress, "retx no-nexthop");
Junxiao Shi986b8492014-08-20 12:07:14 -0700112 }
113 else {
Teng Liangebc20f62020-06-23 16:55:20 -0700114 Face& outFace = it->getFace();
Davide Pesavento0498ce82021-06-14 02:02:21 -0400115 this->sendInterest(interest, outFace, pitEntry);
Alex Lane653eb072023-07-27 22:11:46 -0400116 NFD_LOG_INTEREST_FROM(interest, ingress, "retx retry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700117 }
118}
119
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700120void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400121BestRouteStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
Davide Pesavento7922d122021-03-05 22:41:23 -0500122 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700123{
Davide Pesavento0498ce82021-06-14 02:02:21 -0400124 this->processNack(nack, ingress.face, pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700125}
126
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400127} // namespace nfd::fw