blob: 0de6265fb1f6588e456495248bec7134ac0b981f [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/*
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -04003 * Copyright (c) 2014-2022, 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{
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -040064 RetxSuppressionResult suppression = m_retxSuppression->decidePerPitEntry(*pitEntry);
Ashlesh Gawande90015992017-07-11 17:25:48 -050065 if (suppression == RetxSuppressionResult::SUPPRESS) {
ashiqopuc7079482019-02-20 05:34:37 +000066 NFD_LOG_DEBUG(interest << " from=" << 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()) {
ashiqopuc7079482019-02-20 05:34:37 +000081 NFD_LOG_DEBUG(interest << " from=" << ingress << " noNextHop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -070082
83 lp::NackHeader nackHeader;
84 nackHeader.setReason(lp::NackReason::NO_ROUTE);
Davide Pesavento0498ce82021-06-14 02:02:21 -040085 this->sendNack(nackHeader, ingress.face, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -070086 this->rejectPendingInterest(pitEntry);
87 return;
88 }
89
Teng Liangebc20f62020-06-23 16:55:20 -070090 Face& outFace = it->getFace();
91 NFD_LOG_DEBUG(interest << " from=" << ingress << " newPitEntry-to=" << outFace.getId());
Davide Pesavento0498ce82021-06-14 02:02:21 -040092 this->sendInterest(interest, outFace, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -070093 return;
94 }
95
Junxiao Shi986b8492014-08-20 12:07:14 -070096 // find an unused upstream with lowest cost except downstream
Teng Liangebc20f62020-06-23 16:55:20 -070097 it = std::find_if(nexthops.begin(), nexthops.end(),
98 [&, now = time::steady_clock::now()] (const auto& nexthop) {
99 return isNextHopEligible(ingress.face, interest, nexthop, pitEntry, true, now);
100 });
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400101
Junxiao Shi986b8492014-08-20 12:07:14 -0700102 if (it != nexthops.end()) {
Teng Liangebc20f62020-06-23 16:55:20 -0700103 Face& outFace = it->getFace();
Davide Pesavento0498ce82021-06-14 02:02:21 -0400104 this->sendInterest(interest, outFace, pitEntry);
Teng Liangebc20f62020-06-23 16:55:20 -0700105 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-unused-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700106 return;
107 }
108
109 // find an eligible upstream that is used earliest
ashiqopuc7079482019-02-20 05:34:37 +0000110 it = findEligibleNextHopWithEarliestOutRecord(ingress.face, interest, nexthops, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -0700111 if (it == nexthops.end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000112 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmitNoNextHop");
Junxiao Shi986b8492014-08-20 12:07:14 -0700113 }
114 else {
Teng Liangebc20f62020-06-23 16:55:20 -0700115 Face& outFace = it->getFace();
Davide Pesavento0498ce82021-06-14 02:02:21 -0400116 this->sendInterest(interest, outFace, pitEntry);
Teng Liangebc20f62020-06-23 16:55:20 -0700117 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-retry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700118 }
119}
120
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700121void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400122BestRouteStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
Davide Pesavento7922d122021-03-05 22:41:23 -0500123 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700124{
Davide Pesavento0498ce82021-06-14 02:02:21 -0400125 this->processNack(nack, ingress.face, pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700126}
127
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400128} // namespace nfd::fw