asf-strategy.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 "asf-strategy.hpp"
27 #include "algorithm.hpp"
28 #include "core/logger.hpp"
29 
30 namespace nfd {
31 namespace fw {
32 namespace asf {
33 
34 NFD_LOG_INIT(AsfStrategy);
35 NFD_REGISTER_STRATEGY(AsfStrategy);
36 
37 const time::milliseconds AsfStrategy::RETX_SUPPRESSION_INITIAL(10);
38 const time::milliseconds AsfStrategy::RETX_SUPPRESSION_MAX(250);
39 
40 AsfStrategy::AsfStrategy(Forwarder& forwarder, const Name& name)
41  : Strategy(forwarder)
42  , m_measurements(getMeasurements())
43  , m_probing(m_measurements)
44  , m_maxSilentTimeouts(0)
45  , m_retxSuppression(RETX_SUPPRESSION_INITIAL,
46  RetxSuppressionExponential::DEFAULT_MULTIPLIER,
47  RETX_SUPPRESSION_MAX)
48 {
50  if (!parsed.parameters.empty()) {
51  processParams(parsed.parameters);
52  }
53 
54  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
55  BOOST_THROW_EXCEPTION(std::invalid_argument(
56  "AsfStrategy does not support version " + to_string(*parsed.version)));
57  }
59 
60  NFD_LOG_DEBUG("Probing interval=" << m_probing.getProbingInterval()
61  << ", Num silent timeouts=" << m_maxSilentTimeouts);
62 }
63 
64 const Name&
66 {
67  static Name strategyName("/localhost/nfd/strategy/asf/%FD%03");
68  return strategyName;
69 }
70 
71 void
72 AsfStrategy::processParams(const PartialName& parsed)
73 {
74  for (const auto& component : parsed) {
75  std::string parsedStr(reinterpret_cast<const char*>(component.value()), component.value_size());
76  auto n = parsedStr.find("~");
77  if (n == std::string::npos) {
78  BOOST_THROW_EXCEPTION(std::invalid_argument("Format is <parameter>~<value>"));
79  }
80 
81  auto f = parsedStr.substr(0, n);
82  auto s = parsedStr.substr(n + 1);
83  if (f == "probing-interval") {
84  m_probing.setProbingInterval(getParamValue(f, s));
85  }
86  else if (f == "n-silent-timeouts") {
87  m_maxSilentTimeouts = getParamValue(f, s);
88  }
89  else {
90  BOOST_THROW_EXCEPTION(std::invalid_argument("Parameter should be probing-interval or n-silent-timeouts"));
91  }
92  }
93 }
94 
95 uint64_t
96 AsfStrategy::getParamValue(const std::string& param, const std::string& value)
97 {
98  try {
99  if (!value.empty() && value[0] == '-')
100  BOOST_THROW_EXCEPTION(boost::bad_lexical_cast());
101 
102  return boost::lexical_cast<uint64_t>(value);
103  }
104  catch (const boost::bad_lexical_cast&) {
105  BOOST_THROW_EXCEPTION(std::invalid_argument("Value of " + param + " must be a non-negative integer"));
106  }
107 }
108 
109 void
110 AsfStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
111  const shared_ptr<pit::Entry>& pitEntry)
112 {
113  // Should the Interest be suppressed?
114  RetxSuppressionResult suppressResult = m_retxSuppression.decidePerPitEntry(*pitEntry);
115 
116  switch (suppressResult) {
119  break;
121  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " suppressed");
122  return;
123  }
124 
125  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
126  const fib::NextHopList& nexthops = fibEntry.getNextHops();
127 
128  if (nexthops.size() == 0) {
129  sendNoRouteNack(inFace, interest, pitEntry);
130  this->rejectPendingInterest(pitEntry);
131  return;
132  }
133 
134  Face* faceToUse = getBestFaceForForwarding(fibEntry, interest, inFace);
135 
136  if (faceToUse == nullptr) {
137  sendNoRouteNack(inFace, interest, pitEntry);
138  this->rejectPendingInterest(pitEntry);
139  return;
140  }
141 
142  NFD_LOG_TRACE("Forwarding interest to face: " << faceToUse->getId());
143 
144  forwardInterest(interest, fibEntry, pitEntry, *faceToUse);
145 
146  // If necessary, send probe
147  if (m_probing.isProbingNeeded(fibEntry, interest)) {
148  Face* faceToProbe = m_probing.getFaceToProbe(inFace, interest, fibEntry, *faceToUse);
149 
150  if (faceToProbe != nullptr) {
151  bool wantNewNonce = true;
152  forwardInterest(interest, fibEntry, pitEntry, *faceToProbe, wantNewNonce);
153  m_probing.afterForwardingProbe(fibEntry, interest);
154  }
155  }
156 }
157 
158 void
159 AsfStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
160  const Face& inFace, const Data& data)
161 {
162  NamespaceInfo* namespaceInfo = m_measurements.getNamespaceInfo(pitEntry->getName());
163 
164  if (namespaceInfo == nullptr) {
165  NFD_LOG_TRACE("Could not find measurements entry for " << pitEntry->getName());
166  return;
167  }
168 
169  // Record the RTT between the Interest out to Data in
170  FaceInfo* faceInfo = namespaceInfo->get(inFace.getId());
171  if (faceInfo == nullptr) {
172  return;
173  }
174  faceInfo->recordRtt(pitEntry, inFace);
175 
176  // Extend lifetime for measurements associated with Face
177  namespaceInfo->extendFaceInfoLifetime(*faceInfo, inFace.getId());
178 
179  if (faceInfo->isTimeoutScheduled()) {
180  faceInfo->cancelTimeoutEvent(data.getName());
181  }
182 }
183 
184 void
185 AsfStrategy::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
186  const shared_ptr<pit::Entry>& pitEntry)
187 {
188  NFD_LOG_DEBUG("Nack for " << nack.getInterest() << " from=" << inFace.getId() << ": " << nack.getReason());
189  onTimeout(pitEntry->getName(), inFace.getId());
190 }
191 
194 
195 void
196 AsfStrategy::forwardInterest(const Interest& interest,
197  const fib::Entry& fibEntry,
198  const shared_ptr<pit::Entry>& pitEntry,
199  Face& outFace,
200  bool wantNewNonce)
201 {
202  if (wantNewNonce) {
203  //Send probe: interest with new Nonce
204  Interest probeInterest(interest);
205  probeInterest.refreshNonce();
206  NFD_LOG_TRACE("Sending probe for " << probeInterest << probeInterest.getNonce()
207  << " to FaceId: " << outFace.getId());
208  this->sendInterest(pitEntry, outFace, probeInterest);
209  }
210  else {
211  this->sendInterest(pitEntry, outFace, interest);
212  }
213 
214  FaceInfo& faceInfo = m_measurements.getOrCreateFaceInfo(fibEntry, interest, outFace.getId());
215 
216  // Refresh measurements since Face is being used for forwarding
217  NamespaceInfo& namespaceInfo = m_measurements.getOrCreateNamespaceInfo(fibEntry, interest);
218  namespaceInfo.extendFaceInfoLifetime(faceInfo, outFace.getId());
219 
220  if (!faceInfo.isTimeoutScheduled()) {
221  // Estimate and schedule timeout
222  RttEstimator::Duration timeout = faceInfo.computeRto();
223 
224  NFD_LOG_TRACE("Scheduling timeout for " << fibEntry.getPrefix()
225  << " FaceId: " << outFace.getId()
226  << " in " << time::duration_cast<time::milliseconds>(timeout) << " ms");
227 
228  scheduler::EventId id = scheduler::schedule(timeout,
229  bind(&AsfStrategy::onTimeout, this, interest.getName(), outFace.getId()));
230 
231  faceInfo.setTimeoutEvent(id, interest.getName());
232  }
233 }
234 
235 struct FaceStats
236 {
237  Face* face;
238  RttStats::Rtt rtt;
239  RttStats::Rtt srtt;
240  uint64_t cost;
241 };
242 
243 double
244 getValueForSorting(const FaceStats& stats)
245 {
246  // These values allow faces with no measurements to be ranked better than timeouts
247  // srtt < RTT_NO_MEASUREMENT < RTT_TIMEOUT
248  static const RttStats::Rtt SORTING_RTT_TIMEOUT = time::microseconds::max();
249  static const RttStats::Rtt SORTING_RTT_NO_MEASUREMENT = SORTING_RTT_TIMEOUT / 2;
250 
251  if (stats.rtt == RttStats::RTT_TIMEOUT) {
252  return SORTING_RTT_TIMEOUT.count();
253  }
254  else if (stats.rtt == RttStats::RTT_NO_MEASUREMENT) {
255  return SORTING_RTT_NO_MEASUREMENT.count();
256  }
257  else {
258  return stats.srtt.count();
259  }
260 }
261 
262 Face*
263 AsfStrategy::getBestFaceForForwarding(const fib::Entry& fibEntry, const Interest& interest,
264  const Face& inFace)
265 {
266  NFD_LOG_TRACE("Looking for best face for " << fibEntry.getPrefix());
267 
268  typedef std::function<bool(const FaceStats&, const FaceStats&)> FaceStatsPredicate;
269  typedef std::set<FaceStats, FaceStatsPredicate> FaceStatsSet;
270 
271  FaceStatsSet rankedFaces(
272  [] (const FaceStats& lhs, const FaceStats& rhs) -> bool {
273  // Sort by RTT and then by cost
274  double lhsValue = getValueForSorting(lhs);
275  double rhsValue = getValueForSorting(rhs);
276 
277  if (lhsValue < rhsValue) {
278  return true;
279  }
280  else if (lhsValue == rhsValue) {
281  return lhs.cost < rhs.cost;
282  }
283  else {
284  return false;
285  }
286  });
287 
288  for (const fib::NextHop& hop : fibEntry.getNextHops()) {
289  Face& hopFace = hop.getFace();
290 
291  if ((hopFace.getId() == inFace.getId() && hopFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) ||
292  wouldViolateScope(inFace, interest, hopFace)) {
293  continue;
294  }
295 
296  FaceInfo* info = m_measurements.getFaceInfo(fibEntry, interest, hopFace.getId());
297 
298  if (info == nullptr) {
299  FaceStats stats = {&hopFace,
302  hop.getCost()};
303 
304  rankedFaces.insert(stats);
305  }
306  else {
307  FaceStats stats = {&hopFace, info->getRtt(), info->getSrtt(), hop.getCost()};
308  rankedFaces.insert(stats);
309  }
310  }
311 
312  FaceStatsSet::iterator it = rankedFaces.begin();
313 
314  if (it != rankedFaces.end()) {
315  return it->face;
316  }
317  else {
318  return nullptr;
319  }
320 }
321 
322 void
323 AsfStrategy::onTimeout(const Name& interestName, const face::FaceId faceId)
324 {
325  NamespaceInfo* namespaceInfo = m_measurements.getNamespaceInfo(interestName);
326 
327  if (namespaceInfo == nullptr) {
328  NFD_LOG_TRACE("FibEntry for " << interestName << " has been removed since timeout scheduling");
329  return;
330  }
331 
332  FaceInfoTable::iterator it = namespaceInfo->find(faceId);
333 
334  if (it == namespaceInfo->end()) {
335  it = namespaceInfo->insert(faceId);
336  }
337 
338  FaceInfo& faceInfo = it->second;
339 
340  faceInfo.setNSilentTimeouts(faceInfo.getNSilentTimeouts() + 1);
341 
342  if (faceInfo.getNSilentTimeouts() <= m_maxSilentTimeouts) {
343  NFD_LOG_TRACE("FaceId " << faceId << " for " << interestName << " has timed-out "
344  << faceInfo.getNSilentTimeouts() << " time(s), ignoring");
345  // Extend lifetime for measurements associated with Face
346  namespaceInfo->extendFaceInfoLifetime(faceInfo, faceId);
347 
348  if (faceInfo.isTimeoutScheduled()) {
349  faceInfo.cancelTimeoutEvent(interestName);
350  }
351  }
352  else {
353  NFD_LOG_TRACE("FaceId " << faceId << " for " << interestName << " has timed-out");
354  faceInfo.recordTimeout(interestName);
355  }
356 }
357 
358 void
359 AsfStrategy::sendNoRouteNack(const Face& inFace, const Interest& interest,
360  const shared_ptr<pit::Entry>& pitEntry)
361 {
362  NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
363 
364  lp::NackHeader nackHeader;
365  nackHeader.setReason(lp::NackReason::NO_ROUTE);
366  this->sendNack(pitEntry, inFace, nackHeader);
367 }
368 
369 } // namespace asf
370 } // namespace fw
371 } // namespace nfd
main class of NFD
Definition: forwarder.hpp:52
Interest is retransmission and should be forwarded.
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:367
void afterForwardingProbe(const fib::Entry &fibEntry, const Interest &interest)
void extendFaceInfoLifetime(FaceInfo &info, FaceId faceId)
RttStats::Rtt getRtt() const
static const Name & getStrategyName()
NamespaceInfo * getNamespaceInfo(const Name &prefix)
represents a FIB entry
Definition: fib-entry.hpp:51
void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data) override
trigger before PIT entry is satisfied
NFD_REGISTER_STRATEGY(AsfStrategy)
#define NFD_LOG_TRACE
Definition: logger.hpp:37
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &outFace, const Interest &interest)
send Interest to outFace
Definition: strategy.hpp:241
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const Face &outFace, const lp::NackHeader &header)
send Nack to outFace
Definition: strategy.hpp:286
FaceInfo & getOrCreateFaceInfo(const fib::Entry &fibEntry, const Interest &interest, FaceId faceId)
Interest is retransmission and should be suppressed.
FaceInfo * get(FaceId faceId)
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
Definition: strategy.cpp:133
const FaceInfoTable::iterator insert(FaceId faceId)
RttStats::Rtt getSrtt() const
time::duration< double, boost::micro > Rtt
FaceInfoTable::iterator end()
time::microseconds Duration
Table::const_iterator iterator
Definition: cs-internal.hpp:41
void cancelTimeoutEvent(const Name &prefix)
FaceInfoTable::iterator find(FaceId faceId)
const Name & getPrefix() const
Definition: fib-entry.hpp:58
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
Interest is new (not a retransmission)
a retransmission suppression decision algorithm that suppresses retransmissions using exponential bac...
NamespaceInfo & getOrCreateNamespaceInfo(const fib::Entry &fibEntry, const Interest &interest)
bool isProbingNeeded(const fib::Entry &fibEntry, const Interest &interest)
Represents a collection of nexthops.
PartialName parameters
parameter components
Definition: strategy.hpp:340
bool isTimeoutScheduled() const
void recordRtt(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace)
time::milliseconds getProbingInterval() const
stores stategy information about each face in this namespace
RetxSuppressionResult decidePerPitEntry(pit::Entry &pitEntry)
determines whether Interest is a retransmission per pit entry and if so, whether it shall be forwarde...
represents a forwarding strategy
Definition: strategy.hpp:37
void afterReceiveNack(const Face &inFace, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Nack is received
void recordTimeout(const Name &interestName)
This file contains common algorithms used by forwarding strategies.
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
void setTimeoutEvent(const scheduler::EventId &id, const Name &interestName)
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
static const Rtt RTT_TIMEOUT
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:122
EventId schedule(time::nanoseconds after, const EventCallback &event)
Schedule an event.
Definition: scheduler.cpp:47
void setProbingInterval(size_t probingInterval)
void afterReceiveInterest(const Face &inFace, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Interest is received
size_t getNSilentTimeouts() const
double getValueForSorting(const FaceStats &stats)
Face * getFaceToProbe(const Face &inFace, const Interest &interest, const fib::Entry &fibEntry, const Face &faceUsed)
uint64_t FaceId
identifies a face
Definition: face.hpp:39
RttEstimator::Duration computeRto() const
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:32
const NextHopList & getNextHops() const
Definition: fib-entry.hpp:64
void setNSilentTimeouts(size_t nSilentTimeouts)
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
schedule the PIT entry for immediate deletion
Definition: strategy.hpp:273
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:253
Strategy information for each face in a namespace.
AsfStrategy(Forwarder &forwarder, const Name &name=getStrategyName())
static const Rtt RTT_NO_MEASUREMENT
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:339