NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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; -*- */
26 #include "best-route-strategy2.hpp"
27 #include "algorithm.hpp"
28 #include "core/logger.hpp"
29 
30 namespace nfd {
31 namespace fw {
32 
33 NFD_LOG_INIT("BestRouteStrategy2");
34 
35 const Name BestRouteStrategy2::STRATEGY_NAME("ndn:/localhost/nfd/strategy/best-route/%FD%04");
37 
38 const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_INITIAL(10);
39 const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_MAX(250);
40 
42  : Strategy(forwarder, name)
43  , m_retxSuppression(RETX_SUPPRESSION_INITIAL,
44  RetxSuppressionExponential::DEFAULT_MULTIPLIER,
45  RETX_SUPPRESSION_MAX)
46 {
47 }
48 
57 static inline bool
58 isNextHopEligible(const Face& inFace, const Interest& interest,
59  const fib::NextHop& nexthop,
60  const shared_ptr<pit::Entry>& pitEntry,
61  bool wantUnused = false,
62  time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
63 {
64  const Face& outFace = nexthop.getFace();
65 
66  // do not forward back to the same face
67  if (&outFace == &inFace)
68  return false;
69 
70  // forwarding would violate scope
71  if (wouldViolateScope(inFace, interest, outFace))
72  return false;
73 
74  if (wantUnused) {
75  // nexthop must not have unexpired out-record
76  pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(outFace);
77  if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
78  return false;
79  }
80  }
81 
82  return true;
83 }
84 
88 static inline fib::NextHopList::const_iterator
90  const fib::NextHopList& nexthops,
91  const shared_ptr<pit::Entry>& pitEntry)
92 {
93  fib::NextHopList::const_iterator found = nexthops.end();
94  time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max();
95  for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
96  if (!isNextHopEligible(inFace, interest, *it, pitEntry))
97  continue;
98  pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(it->getFace());
99  BOOST_ASSERT(outRecord != pitEntry->out_end());
100  if (outRecord->getLastRenewed() < earliestRenewed) {
101  found = it;
102  earliestRenewed = outRecord->getLastRenewed();
103  }
104  }
105  return found;
106 }
107 
108 void
110  const shared_ptr<pit::Entry>& pitEntry)
111 {
112  RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry);
113  if (suppression == RetxSuppression::SUPPRESS) {
114  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
115  << " suppressed");
116  return;
117  }
118 
119  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
120  const fib::NextHopList& nexthops = fibEntry.getNextHops();
121  fib::NextHopList::const_iterator it = nexthops.end();
122 
123  if (suppression == RetxSuppression::NEW) {
124  // forward to nexthop with lowest cost except downstream
125  it = std::find_if(nexthops.begin(), nexthops.end(),
126  bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
127  false, time::steady_clock::TimePoint::min()));
128 
129  if (it == nexthops.end()) {
130  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
131 
132  lp::NackHeader nackHeader;
133  nackHeader.setReason(lp::NackReason::NO_ROUTE);
134  this->sendNack(pitEntry, inFace, nackHeader);
135 
136  this->rejectPendingInterest(pitEntry);
137  return;
138  }
139 
140  Face& outFace = it->getFace();
141  this->sendInterest(pitEntry, outFace, interest);
142  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
143  << " newPitEntry-to=" << outFace.getId());
144  return;
145  }
146 
147  // find an unused upstream with lowest cost except downstream
148  it = std::find_if(nexthops.begin(), nexthops.end(),
149  bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
150  true, time::steady_clock::now()));
151  if (it != nexthops.end()) {
152  Face& outFace = it->getFace();
153  this->sendInterest(pitEntry, outFace, interest);
154  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
155  << " retransmit-unused-to=" << outFace.getId());
156  return;
157  }
158 
159  // find an eligible upstream that is used earliest
160  it = findEligibleNextHopWithEarliestOutRecord(inFace, interest, nexthops, pitEntry);
161  if (it == nexthops.end()) {
162  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
163  }
164  else {
165  Face& outFace = it->getFace();
166  this->sendInterest(pitEntry, outFace, interest);
167  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
168  << " retransmit-retry-to=" << outFace.getId());
169  }
170 }
171 
176 inline lp::NackReason
178 {
179  if (x == lp::NackReason::NONE) {
180  return y;
181  }
182  if (y == lp::NackReason::NONE) {
183  return x;
184  }
185  return static_cast<lp::NackReason>(std::min(static_cast<int>(x), static_cast<int>(y)));
186 }
187 
188 void
190  const shared_ptr<pit::Entry>& pitEntry)
191 {
192  int nOutRecordsNotNacked = 0;
193  Face* lastFaceNotNacked = nullptr;
194  lp::NackReason leastSevereReason = lp::NackReason::NONE;
195  for (const pit::OutRecord& outR : pitEntry->getOutRecords()) {
196  const lp::NackHeader* inNack = outR.getIncomingNack();
197  if (inNack == nullptr) {
198  ++nOutRecordsNotNacked;
199  lastFaceNotNacked = &outR.getFace();
200  continue;
201  }
202 
203  leastSevereReason = compareLessSevere(leastSevereReason, inNack->getReason());
204  }
205 
206  lp::NackHeader outNack;
207  outNack.setReason(leastSevereReason);
208 
209  if (nOutRecordsNotNacked == 1) {
210  BOOST_ASSERT(lastFaceNotNacked != nullptr);
211  pit::InRecordCollection::iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
212  if (inR != pitEntry->in_end()) {
213  // one out-record not Nacked, which is also a downstream
214  NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
215  " nack=" << nack.getReason() <<
216  " nack-to(bidirectional)=" << lastFaceNotNacked->getId() <<
217  " out-nack=" << outNack.getReason());
218  this->sendNack(pitEntry, *lastFaceNotNacked, outNack);
219  return;
220  }
221  }
222 
223  if (nOutRecordsNotNacked > 0) {
224  NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
225  " nack=" << nack.getReason() <<
226  " waiting=" << nOutRecordsNotNacked);
227  // continue waiting
228  return;
229  }
230 
231  NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
232  " nack=" << nack.getReason() <<
233  " nack-to=all out-nack=" << outNack.getReason());
234  this->sendNacks(pitEntry, outNack);
235 }
236 
237 } // namespace fw
238 } // namespace nfd
time_point TimePoint
Definition: time.hpp:120
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
virtual 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:79
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const Face &outFace, const lp::NackHeader &header)
send Nack to outFace
Definition: strategy.hpp:175
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
Interest is retransmission and should be suppressed.
main class of NFD
Definition: forwarder.hpp:54
represents an Interest packet
Definition: interest.hpp:42
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:55
NackReason
indicates the reason type of a network NACK
Definition: nack-header.hpp:39
Interest is new (not a retransmission)
NackReason getReason() const
represents a Network Nack
Definition: nack.hpp:40
NackReason getReason() const
Definition: nack.hpp:92
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
lp::NackReason compareLessSevere(lp::NackReason x, lp::NackReason y)
FaceId getId() const
Definition: face.hpp:229
NFD_REGISTER_STRATEGY(AccessStrategy)
std::vector< fib::NextHop > NextHopList
Definition: fib-entry.hpp:47
virtual 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)
decide that a pending Interest cannot be forwarded
Definition: strategy.hpp:162
virtual Result decide(const Face &inFace, const Interest &interest, pit::Entry &pitEntry) const override
determines whether Interest is a retransmission, and if so, whether it shall be forwarded or suppress...
Name abstraction to represent an absolute name.
Definition: name.hpp:46
represents a forwarding strategy
Definition: strategy.hpp:38
void sendNacks(const shared_ptr< pit::Entry > &pitEntry, const lp::NackHeader &header, std::initializer_list< const Face *> exceptFaces=std::initializer_list< const Face *>())
send Nack to every face that has an in-record, except those in exceptFaces
Definition: strategy.cpp:107
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:129
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
contains information about an Interest toward an outgoing face
BestRouteStrategy2(Forwarder &forwarder, const Name &name=STRATEGY_NAME)
const NextHopList & getNextHops() const
Definition: fib-entry.hpp:64
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
a retransmission suppression decision algorithm that suppresses retransmissions using exponential bac...
represents a Network NACK header
Definition: nack-header.hpp:52
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &outFace, const Interest &interest)
send Interest to outFace
Definition: strategy.hpp:137
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:37
const Interest & getInterest() const
Definition: nack.hpp:53
Face & getFace() const
Definition: fib-nexthop.hpp:45
represents a nexthop record in FIB entry
Definition: fib-nexthop.hpp:38