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 "pit-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");
36 NFD_REGISTER_STRATEGY(BestRouteStrategy2);
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 
56 static inline bool
57 predicate_NextHop_eligible(const shared_ptr<pit::Entry>& pitEntry,
58  const fib::NextHop& nexthop, FaceId currentDownstream,
59  bool wantUnused = false,
60  time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
61 {
62  Face& upstream = nexthop.getFace();
63 
64  // upstream is current downstream
65  if (upstream.getId() == currentDownstream)
66  return false;
67 
68  // forwarding would violate scope
69  if (violatesScope(*pitEntry, upstream))
70  return false;
71 
72  if (wantUnused) {
73  // NextHop must not have unexpired out-record
74  pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(upstream);
75  if (outRecord != pitEntry->out_end() && 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::iterator outRecord = pitEntry->getOutRecord(it->getFace());
97  BOOST_ASSERT(outRecord != pitEntry->out_end());
98  if (outRecord->getLastRenewed() < earliestRenewed) {
99  found = it;
100  earliestRenewed = outRecord->getLastRenewed();
101  }
102  }
103  return found;
104 }
105 
106 void
107 BestRouteStrategy2::afterReceiveInterest(const Face& inFace, const Interest& interest,
108  const shared_ptr<pit::Entry>& pitEntry)
109 {
110  RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry);
111  if (suppression == RetxSuppression::SUPPRESS) {
112  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
113  << " suppressed");
114  return;
115  }
116 
117  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
118  const fib::NextHopList& nexthops = fibEntry.getNextHops();
119  fib::NextHopList::const_iterator it = nexthops.end();
120 
121  if (suppression == RetxSuppression::NEW) {
122  // forward to nexthop with lowest cost except downstream
123  it = std::find_if(nexthops.begin(), nexthops.end(),
124  bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
125  false, time::steady_clock::TimePoint::min()));
126 
127  if (it == nexthops.end()) {
128  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
129 
130  lp::NackHeader nackHeader;
131  nackHeader.setReason(lp::NackReason::NO_ROUTE);
132  this->sendNack(pitEntry, inFace, nackHeader);
133 
134  this->rejectPendingInterest(pitEntry);
135  return;
136  }
137 
138  Face& outFace = it->getFace();
139  this->sendInterest(pitEntry, outFace);
140  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
141  << " newPitEntry-to=" << outFace.getId());
142  return;
143  }
144 
145  // find an unused upstream with lowest cost except downstream
146  it = std::find_if(nexthops.begin(), nexthops.end(),
147  bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
148  true, time::steady_clock::now()));
149  if (it != nexthops.end()) {
150  Face& outFace = it->getFace();
151  this->sendInterest(pitEntry, outFace);
152  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
153  << " retransmit-unused-to=" << outFace.getId());
154  return;
155  }
156 
157  // find an eligible upstream that is used earliest
158  it = findEligibleNextHopWithEarliestOutRecord(pitEntry, nexthops, inFace.getId());
159  if (it == nexthops.end()) {
160  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
161  }
162  else {
163  Face& outFace = it->getFace();
164  this->sendInterest(pitEntry, outFace);
165  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
166  << " retransmit-retry-to=" << outFace.getId());
167  }
168 }
169 
174 inline lp::NackReason
175 compareLessSevere(lp::NackReason x, lp::NackReason y)
176 {
177  if (x == lp::NackReason::NONE) {
178  return y;
179  }
180  if (y == lp::NackReason::NONE) {
181  return x;
182  }
183  return static_cast<lp::NackReason>(std::min(static_cast<int>(x), static_cast<int>(y)));
184 }
185 
186 void
187 BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
188  const shared_ptr<pit::Entry>& pitEntry)
189 {
190  int nOutRecordsNotNacked = 0;
191  Face* lastFaceNotNacked = nullptr;
192  lp::NackReason leastSevereReason = lp::NackReason::NONE;
193  for (const pit::OutRecord& outR : pitEntry->getOutRecords()) {
194  const lp::NackHeader* inNack = outR.getIncomingNack();
195  if (inNack == nullptr) {
196  ++nOutRecordsNotNacked;
197  lastFaceNotNacked = &outR.getFace();
198  continue;
199  }
200 
201  leastSevereReason = compareLessSevere(leastSevereReason, inNack->getReason());
202  }
203 
204  lp::NackHeader outNack;
205  outNack.setReason(leastSevereReason);
206 
207  if (nOutRecordsNotNacked == 1) {
208  BOOST_ASSERT(lastFaceNotNacked != nullptr);
209  pit::InRecordCollection::iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
210  if (inR != pitEntry->in_end()) {
211  // one out-record not Nacked, which is also a downstream
212  NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
213  " nack=" << nack.getReason() <<
214  " nack-to(bidirectional)=" << lastFaceNotNacked->getId() <<
215  " out-nack=" << outNack.getReason());
216  this->sendNack(pitEntry, *lastFaceNotNacked, outNack);
217  return;
218  }
219  }
220 
221  if (nOutRecordsNotNacked > 0) {
222  NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
223  " nack=" << nack.getReason() <<
224  " waiting=" << nOutRecordsNotNacked);
225  // continue waiting
226  return;
227  }
228 
229 
230  NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
231  " nack=" << nack.getReason() <<
232  " nack-to=all out-nack=" << outNack.getReason());
233  this->sendNacks(pitEntry, outNack);
234 }
235 
236 } // namespace fw
237 } // namespace nfd
#define NFD_REGISTER_STRATEGY(StrategyType)
registers a built-in strategy
main class of NFD
Definition: forwarder.hpp:52
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:161
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:69
contains information about an Interest toward an outgoing face
represents a FIB entry
Definition: fib-entry.hpp:51
Interest is retransmission and should be suppressed.
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const Face &outFace, const lp::NackHeader &header)
send Nack to outFace
Definition: strategy.hpp:164
Table::const_iterator iterator
Definition: cs-internal.hpp:41
bool violatesScope(const pit::Entry &pitEntry, const Face &outFace)
determine whether forwarding the Interest in pitEntry to outFace would violate scope ...
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
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...
lp::NackReason compareLessSevere(lp::NackReason x, lp::NackReason y)
Face & getFace() const
Definition: fib-nexthop.hpp:45
a retransmission suppression decision algorithm that suppresses retransmissions using exponential bac...
std::vector< fib::NextHop > NextHopList
Definition: fib-entry.hpp:47
BestRouteStrategy2(Forwarder &forwarder, const Name &name=STRATEGY_NAME)
virtual void afterReceiveNack(const Face &inFace, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Nack is received
virtual void afterReceiveInterest(const Face &inFace, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Interest is received
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
represents a forwarding strategy
Definition: strategy.hpp:38
Interest is new (not a retransmission)
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
uint64_t FaceId
identifies a face
Definition: face.hpp:39
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &outFace, bool wantNewNonce=false)
send Interest to outFace
Definition: strategy.hpp:138
represents a nexthop record in FIB entry
Definition: fib-nexthop.hpp:38
const NextHopList & getNextHops() const
Definition: fib-entry.hpp:64
static fib::NextHopList::const_iterator findEligibleNextHopWithEarliestOutRecord(const shared_ptr< pit::Entry > &pitEntry, const fib::NextHopList &nexthops, FaceId currentDownstream)
pick an eligible NextHop with earliest out-record
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
decide that a pending Interest cannot be forwarded
Definition: strategy.hpp:151
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:91