blob: 94f2e2973362d6b670b33e6eb13ec5cb21cf98a8 [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
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 -050036BestRouteStrategy::BestRouteStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi18739c42016-12-22 08:03:00 +000037 : Strategy(forwarder)
Junxiao Shi99540072017-01-27 19:57:33 +000038 , ProcessNackTraits(this)
Junxiao Shi986b8492014-08-20 12:07:14 -070039{
Junxiao Shi18739c42016-12-22 08:03:00 +000040 ParsedInstanceName parsed = parseInstanceName(name);
Junxiao Shi91f6ee02016-12-29 21:44:44 +000041 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050042 NDN_THROW(std::invalid_argument(
Davide Pesavento7922d122021-03-05 22:41:23 -050043 "BestRouteStrategy does not support version " + to_string(*parsed.version)));
Junxiao Shi91f6ee02016-12-29 21:44:44 +000044 }
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -040045
46 StrategyParameters params = parseParameters(parsed.parameters);
47 m_retxSuppression = RetxSuppressionExponential::construct(params);
48
Junxiao Shi18739c42016-12-22 08:03:00 +000049 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -040050
51 NDN_LOG_DEBUG(*m_retxSuppression);
Junxiao Shi986b8492014-08-20 12:07:14 -070052}
53
Junxiao Shi037f4ab2016-12-13 04:27:06 +000054const Name&
Davide Pesavento7922d122021-03-05 22:41:23 -050055BestRouteStrategy::getStrategyName()
Junxiao Shi037f4ab2016-12-13 04:27:06 +000056{
Eric Newberry358414d2021-03-21 20:56:42 -070057 static const auto strategyName = Name("/localhost/nfd/strategy/best-route").appendVersion(5);
Junxiao Shi037f4ab2016-12-13 04:27:06 +000058 return strategyName;
59}
60
Junxiao Shi986b8492014-08-20 12:07:14 -070061void
Davide Pesavento0498ce82021-06-14 02:02:21 -040062BestRouteStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Davide Pesavento7922d122021-03-05 22:41:23 -050063 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -070064{
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -040065 RetxSuppressionResult suppression = m_retxSuppression->decidePerPitEntry(*pitEntry);
Ashlesh Gawande90015992017-07-11 17:25:48 -050066 if (suppression == RetxSuppressionResult::SUPPRESS) {
ashiqopuc7079482019-02-20 05:34:37 +000067 NFD_LOG_DEBUG(interest << " from=" << ingress << " suppressed");
Junxiao Shi8d843142016-07-11 22:42:42 +000068 return;
69 }
70
71 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
72 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Davide Pesaventoe4b22382018-06-10 14:37:24 -040073 auto it = nexthops.end();
Junxiao Shi986b8492014-08-20 12:07:14 -070074
Ashlesh Gawande90015992017-07-11 17:25:48 -050075 if (suppression == RetxSuppressionResult::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -070076 // forward to nexthop with lowest cost except downstream
Davide Pesaventoe4b22382018-06-10 14:37:24 -040077 it = std::find_if(nexthops.begin(), nexthops.end(), [&] (const auto& nexthop) {
ashiqopuc7079482019-02-20 05:34:37 +000078 return isNextHopEligible(ingress.face, interest, nexthop, pitEntry);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040079 });
Junxiao Shi986b8492014-08-20 12:07:14 -070080
81 if (it == nexthops.end()) {
ashiqopuc7079482019-02-20 05:34:37 +000082 NFD_LOG_DEBUG(interest << " from=" << ingress << " noNextHop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -070083
84 lp::NackHeader nackHeader;
85 nackHeader.setReason(lp::NackReason::NO_ROUTE);
Davide Pesavento0498ce82021-06-14 02:02:21 -040086 this->sendNack(nackHeader, ingress.face, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -070087 this->rejectPendingInterest(pitEntry);
88 return;
89 }
90
Teng Liangebc20f62020-06-23 16:55:20 -070091 Face& outFace = it->getFace();
92 NFD_LOG_DEBUG(interest << " from=" << ingress << " newPitEntry-to=" << outFace.getId());
Davide Pesavento0498ce82021-06-14 02:02:21 -040093 this->sendInterest(interest, outFace, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -070094 return;
95 }
96
Junxiao Shi986b8492014-08-20 12:07:14 -070097 // find an unused upstream with lowest cost except downstream
Teng Liangebc20f62020-06-23 16:55:20 -070098 it = std::find_if(nexthops.begin(), nexthops.end(),
99 [&, now = time::steady_clock::now()] (const auto& nexthop) {
100 return isNextHopEligible(ingress.face, interest, nexthop, pitEntry, true, now);
101 });
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400102
Junxiao Shi986b8492014-08-20 12:07:14 -0700103 if (it != nexthops.end()) {
Teng Liangebc20f62020-06-23 16:55:20 -0700104 Face& outFace = it->getFace();
Davide Pesavento0498ce82021-06-14 02:02:21 -0400105 this->sendInterest(interest, outFace, pitEntry);
Teng Liangebc20f62020-06-23 16:55:20 -0700106 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-unused-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700107 return;
108 }
109
110 // find an eligible upstream that is used earliest
ashiqopuc7079482019-02-20 05:34:37 +0000111 it = findEligibleNextHopWithEarliestOutRecord(ingress.face, interest, nexthops, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -0700112 if (it == nexthops.end()) {
ashiqopuc7079482019-02-20 05:34:37 +0000113 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmitNoNextHop");
Junxiao Shi986b8492014-08-20 12:07:14 -0700114 }
115 else {
Teng Liangebc20f62020-06-23 16:55:20 -0700116 Face& outFace = it->getFace();
Davide Pesavento0498ce82021-06-14 02:02:21 -0400117 this->sendInterest(interest, outFace, pitEntry);
Teng Liangebc20f62020-06-23 16:55:20 -0700118 NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-retry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700119 }
120}
121
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700122void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400123BestRouteStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
Davide Pesavento7922d122021-03-05 22:41:23 -0500124 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700125{
Davide Pesavento0498ce82021-06-14 02:02:21 -0400126 this->processNack(nack, ingress.face, pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700127}
128
Junxiao Shi986b8492014-08-20 12:07:14 -0700129} // namespace fw
130} // namespace nfd