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 NFD_REGISTER_STRATEGY(BestRouteStrategy2);
35 
36 const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_INITIAL(10);
37 const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_MAX(250);
38 
40  : Strategy(forwarder)
41  , m_retxSuppression(RETX_SUPPRESSION_INITIAL,
42  RetxSuppressionExponential::DEFAULT_MULTIPLIER,
43  RETX_SUPPRESSION_MAX)
44 {
46  if (!parsed.parameters.empty()) {
47  BOOST_THROW_EXCEPTION(std::invalid_argument("BestRouteStrategy2 does not accept parameters"));
48  }
49  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
50  BOOST_THROW_EXCEPTION(std::invalid_argument(
51  "BestRouteStrategy2 does not support version " + std::to_string(*parsed.version)));
52  }
54 }
55 
56 const Name&
58 {
59  static Name strategyName("/localhost/nfd/strategy/best-route/%FD%04");
60  return strategyName;
61 }
62 
71 static inline bool
72 isNextHopEligible(const Face& inFace, const Interest& interest,
73  const fib::NextHop& nexthop,
74  const shared_ptr<pit::Entry>& pitEntry,
75  bool wantUnused = false,
76  time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
77 {
78  const Face& outFace = nexthop.getFace();
79 
80  // do not forward back to the same face
81  if (&outFace == &inFace)
82  return false;
83 
84  // forwarding would violate scope
85  if (wouldViolateScope(inFace, interest, outFace))
86  return false;
87 
88  if (wantUnused) {
89  // nexthop must not have unexpired out-record
90  pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(outFace);
91  if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
92  return false;
93  }
94  }
95 
96  return true;
97 }
98 
102 static inline fib::NextHopList::const_iterator
103 findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest,
104  const fib::NextHopList& nexthops,
105  const shared_ptr<pit::Entry>& pitEntry)
106 {
107  fib::NextHopList::const_iterator found = nexthops.end();
108  time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max();
109  for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
110  if (!isNextHopEligible(inFace, interest, *it, pitEntry))
111  continue;
112  pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(it->getFace());
113  BOOST_ASSERT(outRecord != pitEntry->out_end());
114  if (outRecord->getLastRenewed() < earliestRenewed) {
115  found = it;
116  earliestRenewed = outRecord->getLastRenewed();
117  }
118  }
119  return found;
120 }
121 
122 void
123 BestRouteStrategy2::afterReceiveInterest(const Face& inFace, const Interest& interest,
124  const shared_ptr<pit::Entry>& pitEntry)
125 {
126  RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry);
127  if (suppression == RetxSuppression::SUPPRESS) {
128  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
129  << " suppressed");
130  return;
131  }
132 
133  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
134  const fib::NextHopList& nexthops = fibEntry.getNextHops();
135  fib::NextHopList::const_iterator it = nexthops.end();
136 
137  if (suppression == RetxSuppression::NEW) {
138  // forward to nexthop with lowest cost except downstream
139  it = std::find_if(nexthops.begin(), nexthops.end(),
140  bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
141  false, time::steady_clock::TimePoint::min()));
142 
143  if (it == nexthops.end()) {
144  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
145 
146  lp::NackHeader nackHeader;
147  nackHeader.setReason(lp::NackReason::NO_ROUTE);
148  this->sendNack(pitEntry, inFace, nackHeader);
149 
150  this->rejectPendingInterest(pitEntry);
151  return;
152  }
153 
154  Face& outFace = it->getFace();
155  this->sendInterest(pitEntry, outFace, interest);
156  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
157  << " newPitEntry-to=" << outFace.getId());
158  return;
159  }
160 
161  // find an unused upstream with lowest cost except downstream
162  it = std::find_if(nexthops.begin(), nexthops.end(),
163  bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
164  true, time::steady_clock::now()));
165  if (it != nexthops.end()) {
166  Face& outFace = it->getFace();
167  this->sendInterest(pitEntry, outFace, interest);
168  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
169  << " retransmit-unused-to=" << outFace.getId());
170  return;
171  }
172 
173  // find an eligible upstream that is used earliest
174  it = findEligibleNextHopWithEarliestOutRecord(inFace, interest, nexthops, pitEntry);
175  if (it == nexthops.end()) {
176  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
177  }
178  else {
179  Face& outFace = it->getFace();
180  this->sendInterest(pitEntry, outFace, interest);
181  NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
182  << " retransmit-retry-to=" << outFace.getId());
183  }
184 }
185 
190 inline lp::NackReason
191 compareLessSevere(lp::NackReason x, lp::NackReason y)
192 {
193  if (x == lp::NackReason::NONE) {
194  return y;
195  }
196  if (y == lp::NackReason::NONE) {
197  return x;
198  }
199  return static_cast<lp::NackReason>(std::min(static_cast<int>(x), static_cast<int>(y)));
200 }
201 
202 void
203 BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
204  const shared_ptr<pit::Entry>& pitEntry)
205 {
206  int nOutRecordsNotNacked = 0;
207  Face* lastFaceNotNacked = nullptr;
208  lp::NackReason leastSevereReason = lp::NackReason::NONE;
209  for (const pit::OutRecord& outR : pitEntry->getOutRecords()) {
210  const lp::NackHeader* inNack = outR.getIncomingNack();
211  if (inNack == nullptr) {
212  ++nOutRecordsNotNacked;
213  lastFaceNotNacked = &outR.getFace();
214  continue;
215  }
216 
217  leastSevereReason = compareLessSevere(leastSevereReason, inNack->getReason());
218  }
219 
220  lp::NackHeader outNack;
221  outNack.setReason(leastSevereReason);
222 
223  if (nOutRecordsNotNacked == 1) {
224  BOOST_ASSERT(lastFaceNotNacked != nullptr);
225  pit::InRecordCollection::iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
226  if (inR != pitEntry->in_end()) {
227  // one out-record not Nacked, which is also a downstream
228  NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
229  " nack=" << nack.getReason() <<
230  " nack-to(bidirectional)=" << lastFaceNotNacked->getId() <<
231  " out-nack=" << outNack.getReason());
232  this->sendNack(pitEntry, *lastFaceNotNacked, outNack);
233  return;
234  }
235  }
236 
237  if (nOutRecordsNotNacked > 0) {
238  NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
239  " nack=" << nack.getReason() <<
240  " waiting=" << nOutRecordsNotNacked);
241  // continue waiting
242  return;
243  }
244 
245  NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
246  " nack=" << nack.getReason() <<
247  " nack-to=all out-nack=" << outNack.getReason());
248  this->sendNacks(pitEntry, outNack);
249 }
250 
251 } // namespace fw
252 } // namespace nfd
main class of NFD
Definition: forwarder.hpp:52
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:290
#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:174
contains information about an Interest toward an outgoing face
represents a FIB entry
Definition: fib-entry.hpp:51
static const Name & getStrategyName()
Interest is retransmission and should be suppressed.
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &outFace, const Interest &interest)
send Interest to outFace
Definition: strategy.hpp:191
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const Face &outFace, const lp::NackHeader &header)
send Nack to outFace
Definition: strategy.hpp:217
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
BestRouteStrategy2(Forwarder &forwarder, const Name &name=getStrategyName())
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
Definition: strategy.cpp:132
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
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
#define NFD_REGISTER_STRATEGY(S)
registers a strategy
Definition: strategy.hpp:328
a retransmission suppression decision algorithm that suppresses retransmissions using exponential bac...
std::vector< fib::NextHop > NextHopList
Definition: fib-entry.hpp:47
virtual void afterReceiveNack(const Face &inFace, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Nack is received
PartialName parameters
parameter components
Definition: strategy.hpp:263
virtual void afterReceiveInterest(const Face &inFace, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Interest is received
ndn::optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:262
represents a forwarding strategy
Definition: strategy.hpp:37
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
Interest is new (not a retransmission)
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
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 ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:121
represents a nexthop record in FIB entry
Definition: fib-nexthop.hpp:38
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 NextHopList & getNextHops() const
Definition: fib-entry.hpp:64
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
decide that a pending Interest cannot be forwarded
Definition: strategy.hpp:204
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:196