NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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 "core/logger.hpp"
28 
29 namespace nfd {
30 namespace fw {
31 
32 NFD_LOG_INIT("BestRouteStrategy2");
33 
34 const Name BestRouteStrategy2::STRATEGY_NAME("ndn:/localhost/nfd/strategy/best-route/%FD%04");
36 
37 const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_INITIAL(10);
38 const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_MAX(250);
39 
41  : Strategy(forwarder, name)
42  , m_retxSuppression(RETX_SUPPRESSION_INITIAL,
43  RetxSuppressionExponential::DEFAULT_MULTIPLIER,
44  RETX_SUPPRESSION_MAX)
45 {
46 }
47 
55 static inline bool
56 predicate_NextHop_eligible(const shared_ptr<pit::Entry>& pitEntry,
57  const fib::NextHop& nexthop, FaceId currentDownstream,
58  bool wantUnused = false,
59  time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
60 {
61  shared_ptr<Face> upstream = nexthop.getFace();
62 
63  // upstream is current downstream
64  if (upstream->getId() == currentDownstream)
65  return false;
66 
67  // forwarding would violate scope
68  if (pitEntry->violatesScope(*upstream))
69  return false;
70 
71  if (wantUnused) {
72  // NextHop must not have unexpired OutRecord
73  pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*upstream);
74  if (outRecord != pitEntry->getOutRecords().end() &&
75  outRecord->getExpiry() > now) {
76  return false;
77  }
78  }
79 
80  return true;
81 }
82 
86 static inline fib::NextHopList::const_iterator
87 findEligibleNextHopWithEarliestOutRecord(const shared_ptr<pit::Entry>& pitEntry,
88  const fib::NextHopList& nexthops,
89  FaceId currentDownstream)
90 {
91  fib::NextHopList::const_iterator found = nexthops.end();
92  time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max();
93  for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
94  if (!predicate_NextHop_eligible(pitEntry, *it, currentDownstream))
95  continue;
96  pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*it->getFace());
97  BOOST_ASSERT(outRecord != pitEntry->getOutRecords().end());
98  if (outRecord->getLastRenewed() < earliestRenewed) {
99  found = it;
100  earliestRenewed = outRecord->getLastRenewed();
101  }
102  }
103  return found;
104 }
105 
106 void
108  const Interest& interest,
109  shared_ptr<fib::Entry> fibEntry,
110  shared_ptr<pit::Entry> pitEntry)
111 {
112  const fib::NextHopList& nexthops = fibEntry->getNextHops();
113  fib::NextHopList::const_iterator it = nexthops.end();
114 
115  RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry);
116  if (suppression == RetxSuppression::NEW) {
117  // forward to nexthop with lowest cost except downstream
118  it = std::find_if(nexthops.begin(), nexthops.end(),
119  bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
120  false, time::steady_clock::TimePoint::min()));
121 
122  if (it == nexthops.end()) {
123  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
124 
125  lp::NackHeader nackHeader;
126  nackHeader.setReason(lp::NackReason::NO_ROUTE);
127  this->sendNack(pitEntry, inFace, nackHeader);
128 
129  this->rejectPendingInterest(pitEntry);
130  return;
131  }
132 
133  shared_ptr<Face> outFace = it->getFace();
134  this->sendInterest(pitEntry, outFace);
135  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
136  << " newPitEntry-to=" << outFace->getId());
137  return;
138  }
139 
140  if (suppression == RetxSuppression::SUPPRESS) {
141  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
142  << " suppressed");
143  return;
144  }
145 
146  // find an unused upstream with lowest cost except downstream
147  it = std::find_if(nexthops.begin(), nexthops.end(),
148  bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
149  true, time::steady_clock::now()));
150  if (it != nexthops.end()) {
151  shared_ptr<Face> outFace = it->getFace();
152  this->sendInterest(pitEntry, outFace);
153  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
154  << " retransmit-unused-to=" << outFace->getId());
155  return;
156  }
157 
158  // find an eligible upstream that is used earliest
159  it = findEligibleNextHopWithEarliestOutRecord(pitEntry, nexthops, inFace.getId());
160  if (it == nexthops.end()) {
161  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
162  }
163  else {
164  shared_ptr<Face> outFace = it->getFace();
165  this->sendInterest(pitEntry, outFace);
166  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
167  << " retransmit-retry-to=" << outFace->getId());
168  }
169 }
170 
175 inline lp::NackReason
177 {
178  if (x == lp::NackReason::NONE) {
179  return y;
180  }
181  if (y == lp::NackReason::NONE) {
182  return x;
183  }
184  return static_cast<lp::NackReason>(std::min(static_cast<int>(x), static_cast<int>(y)));
185 }
186 
187 void
188 BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
189  shared_ptr<fib::Entry> fibEntry,
190  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().get();
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::const_iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
212  if (inR != pitEntry->getInRecords().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 
232  NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
233  " nack=" << nack.getReason() <<
234  " nack-to=all out-nack=" << outNack.getReason());
235  this->sendNacks(pitEntry, outNack);
236 }
237 
238 } // namespace fw
239 } // namespace nfd
time_point TimePoint
Definition: time.hpp:108
virtual void afterReceiveInterest(const Face &inFace, const Interest &interest, shared_ptr< fib::Entry > fibEntry, shared_ptr< pit::Entry > pitEntry) 1
trigger after Interest is received
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:55
NackHeader & setReason(NackReason reason)
set reason code
virtual void afterReceiveNack(const Face &inFace, const lp::Nack &nack, shared_ptr< fib::Entry > fibEntry, shared_ptr< pit::Entry > pitEntry) 1
trigger after Nack is received
static time_point now() noexcept
Definition: time.cpp:79
Best Route strategy version 4.
Interest is retransmission and should be suppressed.
main class of NFD
Definition: forwarder.hpp:55
represents an Interest packet
Definition: interest.hpp:45
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
void sendNacks(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:72
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
lp::NackReason compareLessSevere(lp::NackReason x, lp::NackReason y)
NFD_REGISTER_STRATEGY(AccessStrategy)
std::vector< fib::NextHop > NextHopList
Definition: fib-entry.hpp:48
Name abstraction to represent an absolute name.
Definition: name.hpp:46
const shared_ptr< Face > & getFace() const
Definition: fib-nexthop.cpp:38
void rejectPendingInterest(shared_ptr< pit::Entry > pitEntry)
decide that a pending Interest cannot be forwarded
Definition: strategy.hpp:216
represents a forwarding strategy
Definition: strategy.hpp:38
virtual Result decide(const Face &inFace, const Interest &interest, pit::Entry &pitEntry) const 1
determines whether Interest is a retransmission, and if so, whether it shall be forwarded or suppress...
static bool predicate_NextHop_eligible(const shared_ptr< pit::Entry > &pitEntry, const fib::NextHop &nexthop, FaceId currentDownstream, bool wantUnused=false, time::steady_clock::TimePoint now=time::steady_clock::TimePoint::min())
determines whether a NextHop is eligible
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
contains information about an Interest toward an outgoing face
void sendInterest(shared_ptr< pit::Entry > pitEntry, shared_ptr< Face > outFace, bool wantNewNonce=false)
send Interest to outFace
Definition: strategy.hpp:208
BestRouteStrategy2(Forwarder &forwarder, const Name &name=STRATEGY_NAME)
void sendNack(shared_ptr< pit::Entry > pitEntry, const Face &outFace, const lp::NackHeader &header)
send Nack to outFace
Definition: strategy.hpp:222
uint64_t FaceId
identifies a face
Definition: face.hpp:39
a retransmission suppression decision algorithm that suppresses retransmissions using exponential bac...
represents a Network NACK header
Definition: nack-header.hpp:52
static fib::NextHopList::const_iterator findEligibleNextHopWithEarliestOutRecord(const shared_ptr< pit::Entry > &pitEntry, const fib::NextHopList &nexthops, FaceId currentDownstream)
pick an eligible NextHop with earliest OutRecord
const Interest & getInterest() const
Definition: nack.hpp:53
represents a nexthop record in FIB entry
Definition: fib-nexthop.hpp:38