NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
best-route-strategy2.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, 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-strategy2.hpp"
27 #include "algorithm.hpp"
28 #include "core/logger.hpp"
29 
30 namespace nfd {
31 namespace fw {
32 
35 
36 const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_INITIAL(10);
37 const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_MAX(250);
38 
40  : Strategy(forwarder)
41  , ProcessNackTraits(this)
42  , m_retxSuppression(RETX_SUPPRESSION_INITIAL,
43  RetxSuppressionExponential::DEFAULT_MULTIPLIER,
44  RETX_SUPPRESSION_MAX)
45 {
47  if (!parsed.parameters.empty()) {
48  BOOST_THROW_EXCEPTION(std::invalid_argument("BestRouteStrategy2 does not accept parameters"));
49  }
50  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
51  BOOST_THROW_EXCEPTION(std::invalid_argument(
52  "BestRouteStrategy2 does not support version " + to_string(*parsed.version)));
53  }
55 }
56 
57 const Name&
59 {
60  static Name strategyName("/localhost/nfd/strategy/best-route/%FD%05");
61  return strategyName;
62 }
63 
72 static bool
73 isNextHopEligible(const Face& inFace, const Interest& interest,
74  const fib::NextHop& nexthop,
75  const shared_ptr<pit::Entry>& pitEntry,
76  bool wantUnused = false,
77  time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
78 {
79  const Face& outFace = nexthop.getFace();
80 
81  // do not forward back to the same face, unless it is ad hoc
82  if (outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC)
83  return false;
84 
85  // forwarding would violate scope
86  if (wouldViolateScope(inFace, interest, outFace))
87  return false;
88 
89  if (wantUnused) {
90  // nexthop must not have unexpired out-record
91  auto outRecord = pitEntry->getOutRecord(outFace);
92  if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
93  return false;
94  }
95  }
96 
97  return true;
98 }
99 
103 static fib::NextHopList::const_iterator
105  const fib::NextHopList& nexthops,
106  const shared_ptr<pit::Entry>& pitEntry)
107 {
108  auto found = nexthops.end();
109  auto earliestRenewed = time::steady_clock::TimePoint::max();
110 
111  for (auto it = nexthops.begin(); it != nexthops.end(); ++it) {
112  if (!isNextHopEligible(inFace, interest, *it, pitEntry))
113  continue;
114 
115  auto outRecord = pitEntry->getOutRecord(it->getFace());
116  BOOST_ASSERT(outRecord != pitEntry->out_end());
117  if (outRecord->getLastRenewed() < earliestRenewed) {
118  found = it;
119  earliestRenewed = outRecord->getLastRenewed();
120  }
121  }
122 
123  return found;
124 }
125 
126 void
128  const shared_ptr<pit::Entry>& pitEntry)
129 {
130  RetxSuppressionResult suppression = m_retxSuppression.decidePerPitEntry(*pitEntry);
131  if (suppression == RetxSuppressionResult::SUPPRESS) {
132  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
133  << " suppressed");
134  return;
135  }
136 
137  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
138  const fib::NextHopList& nexthops = fibEntry.getNextHops();
139  auto it = nexthops.end();
140 
141  if (suppression == RetxSuppressionResult::NEW) {
142  // forward to nexthop with lowest cost except downstream
143  it = std::find_if(nexthops.begin(), nexthops.end(), [&] (const auto& nexthop) {
144  return isNextHopEligible(inFace, interest, nexthop, pitEntry);
145  });
146 
147  if (it == nexthops.end()) {
148  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
149 
150  lp::NackHeader nackHeader;
151  nackHeader.setReason(lp::NackReason::NO_ROUTE);
152  this->sendNack(pitEntry, inFace, nackHeader);
153 
154  this->rejectPendingInterest(pitEntry);
155  return;
156  }
157 
158  Face& outFace = it->getFace();
159  this->sendInterest(pitEntry, outFace, interest);
160  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
161  << " newPitEntry-to=" << outFace.getId());
162  return;
163  }
164 
165  // find an unused upstream with lowest cost except downstream
166  it = std::find_if(nexthops.begin(), nexthops.end(), [&] (const auto& nexthop) {
167  return isNextHopEligible(inFace, interest, nexthop, pitEntry, true, time::steady_clock::now());
168  });
169 
170  if (it != nexthops.end()) {
171  Face& outFace = it->getFace();
172  this->sendInterest(pitEntry, outFace, interest);
173  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
174  << " retransmit-unused-to=" << outFace.getId());
175  return;
176  }
177 
178  // find an eligible upstream that is used earliest
179  it = findEligibleNextHopWithEarliestOutRecord(inFace, interest, nexthops, pitEntry);
180  if (it == nexthops.end()) {
181  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
182  }
183  else {
184  Face& outFace = it->getFace();
185  this->sendInterest(pitEntry, outFace, interest);
186  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
187  << " retransmit-retry-to=" << outFace.getId());
188  }
189 }
190 
191 void
193  const shared_ptr<pit::Entry>& pitEntry)
194 {
195  this->processNack(inFace, nack, pitEntry);
196 }
197 
198 } // namespace fw
199 } // namespace nfd
time_point TimePoint
Definition: time.hpp:225
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:122
NackHeader & setReason(NackReason reason)
set reason code
generalization of a network interface
Definition: face.hpp:67
represents a FIB entry
Definition: fib-entry.hpp:51
PartialName parameters
parameter components
Definition: strategy.hpp:340
void afterReceiveNack(const Face &inFace, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Nack is received
static time_point now() noexcept
Definition: time.cpp:80
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const Face &outFace, const lp::NackHeader &header)
send Nack to outFace
Definition: strategy.hpp:286
Best Route strategy version 4.
static bool isNextHopEligible(const Face &inFace, const Interest &interest, const fib::NextHop &nexthop, const shared_ptr< pit::Entry > &pitEntry, bool wantUnused=false, time::steady_clock::TimePoint now=time::steady_clock::TimePoint::min())
determines whether a NextHop is eligible
main class of NFD
Definition: forwarder.hpp:54
Interest is retransmission and should be suppressed.
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:367
Represents an Interest packet.
Definition: interest.hpp:44
void processNack(const Face &inFace, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
BestRouteStrategy2(Forwarder &forwarder, const Name &name=getStrategyName())
Represents a collection of nexthops.
represents a Network Nack
Definition: nack.hpp:38
ndn::nfd::LinkType getLinkType() const
Definition: face.hpp:287
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
Interest is new (not a retransmission)
FaceId getId() const
Definition: face.hpp:233
NFD_REGISTER_STRATEGY(AccessStrategy)
void afterReceiveInterest(const Face &inFace, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Interest is received
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
schedule the PIT entry for immediate deletion
Definition: strategy.hpp:273
Represents an absolute name.
Definition: name.hpp:43
represents a forwarding strategy
Definition: strategy.hpp:37
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:339
static const Name & getStrategyName()
This file contains common algorithms used by forwarding strategies.
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:253
static 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
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
Definition: strategy.cpp:133
std::string to_string(const V &v)
Definition: backports.hpp:67
const NextHopList & getNextHops() const
Definition: fib-entry.hpp:64
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
a retransmission suppression decision algorithm that suppresses retransmissions using exponential bac...
represents a Network NACK header
Definition: nack-header.hpp:57
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &outFace, const Interest &interest)
send Interest to outFace
Definition: strategy.hpp:241
bool wouldViolateScope(const Face &inFace, const Interest &interest, const Face &outFace)
determine whether forwarding the Interest in pitEntry to outFace would violate scope
Definition: algorithm.cpp:32
RetxSuppressionResult decidePerPitEntry(pit::Entry &pitEntry)
determines whether Interest is a retransmission per pit entry and if so, whether it shall be forwarde...
Face & getFace() const
Definition: fib-nexthop.hpp:45
represents a nexthop record in FIB entry
Definition: fib-nexthop.hpp:38