best-route-strategy.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
4  * 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.
10  *
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 
26 #include "best-route-strategy.hpp"
27 #include "algorithm.hpp"
28 #include "common/logger.hpp"
29 
30 namespace nfd::fw {
31 
32 NFD_LOG_INIT(BestRouteStrategy);
34 
35 BestRouteStrategy::BestRouteStrategy(Forwarder& forwarder, const Name& name)
36  : Strategy(forwarder)
37  , ProcessNackTraits(this)
38 {
40  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
41  NDN_THROW(std::invalid_argument(
42  "BestRouteStrategy does not support version " + to_string(*parsed.version)));
43  }
44 
46  m_retxSuppression = RetxSuppressionExponential::construct(params);
47 
49 
50  NDN_LOG_DEBUG(*m_retxSuppression);
51 }
52 
53 const Name&
55 {
56  static const auto strategyName = Name("/localhost/nfd/strategy/best-route").appendVersion(5);
57  return strategyName;
58 }
59 
60 void
61 BestRouteStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
62  const shared_ptr<pit::Entry>& pitEntry)
63 {
64  RetxSuppressionResult suppression = m_retxSuppression->decidePerPitEntry(*pitEntry);
65  if (suppression == RetxSuppressionResult::SUPPRESS) {
66  NFD_LOG_DEBUG(interest << " from=" << ingress << " suppressed");
67  return;
68  }
69 
70  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
71  const fib::NextHopList& nexthops = fibEntry.getNextHops();
72  auto it = nexthops.end();
73 
74  if (suppression == RetxSuppressionResult::NEW) {
75  // forward to nexthop with lowest cost except downstream
76  it = std::find_if(nexthops.begin(), nexthops.end(), [&] (const auto& nexthop) {
77  return isNextHopEligible(ingress.face, interest, nexthop, pitEntry);
78  });
79 
80  if (it == nexthops.end()) {
81  NFD_LOG_DEBUG(interest << " from=" << ingress << " noNextHop");
82 
83  lp::NackHeader nackHeader;
84  nackHeader.setReason(lp::NackReason::NO_ROUTE);
85  this->sendNack(nackHeader, ingress.face, pitEntry);
86  this->rejectPendingInterest(pitEntry);
87  return;
88  }
89 
90  Face& outFace = it->getFace();
91  NFD_LOG_DEBUG(interest << " from=" << ingress << " newPitEntry-to=" << outFace.getId());
92  this->sendInterest(interest, outFace, pitEntry);
93  return;
94  }
95 
96  // find an unused upstream with lowest cost except downstream
97  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  });
101 
102  if (it != nexthops.end()) {
103  Face& outFace = it->getFace();
104  this->sendInterest(interest, outFace, pitEntry);
105  NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-unused-to=" << outFace.getId());
106  return;
107  }
108 
109  // find an eligible upstream that is used earliest
110  it = findEligibleNextHopWithEarliestOutRecord(ingress.face, interest, nexthops, pitEntry);
111  if (it == nexthops.end()) {
112  NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmitNoNextHop");
113  }
114  else {
115  Face& outFace = it->getFace();
116  this->sendInterest(interest, outFace, pitEntry);
117  NFD_LOG_DEBUG(interest << " from=" << ingress << " retransmit-retry-to=" << outFace.getId());
118  }
119 }
120 
121 void
122 BestRouteStrategy::afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
123  const shared_ptr<pit::Entry>& pitEntry)
124 {
125  this->processNack(nack, ingress.face, pitEntry);
126 }
127 
128 } // namespace nfd::fw
This file contains common algorithms used by forwarding strategies.
Represents a face-endpoint pair in the forwarder.
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:54
Generalization of a network interface.
Definition: face.hpp:56
FaceId getId() const noexcept
Returns the face ID.
Definition: face.hpp:121
Represents an entry in the FIB.
Definition: fib-entry.hpp:54
const NextHopList & getNextHops() const
Definition: fib-entry.hpp:66
"Best route" forwarding strategy.
BestRouteStrategy(Forwarder &forwarder, const Name &name=getStrategyName())
void afterReceiveNack(const lp::Nack &nack, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry) override
Trigger after a Nack is received.
void afterReceiveInterest(const Interest &interest, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry) override
Trigger after an Interest is received.
static const Name & getStrategyName()
void processNack(const lp::Nack &nack, const Face &inFace, const shared_ptr< pit::Entry > &pitEntry)
static std::unique_ptr< RetxSuppressionExponential > construct(const StrategyParameters &params)
Base class of all forwarding strategies.
Definition: strategy.hpp:42
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
Schedule the PIT entry for immediate deletion.
Definition: strategy.hpp:317
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
Performs a FIB lookup, considering Link object if present.
Definition: strategy.cpp:304
bool sendNack(const lp::NackHeader &header, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Nack packet.
Definition: strategy.hpp:333
pit::OutRecord * sendInterest(const Interest &interest, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send an Interest packet.
Definition: strategy.cpp:226
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
void setInstanceName(const Name &name) noexcept
Set strategy instance name.
Definition: strategy.hpp:415
static StrategyParameters parseParameters(const PartialName &params)
Parse strategy parameters encoded in a strategy instance name.
Definition: strategy.cpp:144
static Name makeInstanceName(const Name &input, const Name &strategyName)
Construct a strategy instance name.
Definition: strategy.cpp:134
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
std::vector< NextHop > NextHopList
Definition: fib-entry.hpp:47
@ SUPPRESS
Interest is retransmission and should be suppressed.
@ NEW
Interest is new (not a retransmission)
fib::NextHopList::const_iterator findEligibleNextHopWithEarliestOutRecord(const Face &inFace, const Interest &interest, const fib::NextHopList &nexthops, const shared_ptr< pit::Entry > &pitEntry)
Pick an eligible NextHop with earliest out-record.
Definition: algorithm.cpp:108
NFD_REGISTER_STRATEGY(SelfLearningStrategy)
std::optional< uint64_t > version
The strategy version number, if present.
Definition: strategy.hpp:387
PartialName parameters
Parameter components, may be empty.
Definition: strategy.hpp:388