ncc-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 "ncc-strategy.hpp"
27 #include "algorithm.hpp"
28 #include "core/random.hpp"
29 
30 namespace nfd {
31 namespace fw {
32 
33 NFD_REGISTER_STRATEGY(NccStrategy);
34 
35 const time::microseconds NccStrategy::DEFER_FIRST_WITHOUT_BEST_FACE = 4_ms;
36 const time::microseconds NccStrategy::DEFER_RANGE_WITHOUT_BEST_FACE = 75_ms;
37 const time::nanoseconds NccStrategy::MEASUREMENTS_LIFETIME = 16_s;
38 
39 NccStrategy::NccStrategy(Forwarder& forwarder, const Name& name)
40  : Strategy(forwarder)
41 {
43  if (!parsed.parameters.empty()) {
44  BOOST_THROW_EXCEPTION(std::invalid_argument("NccStrategy does not accept parameters"));
45  }
46  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
47  BOOST_THROW_EXCEPTION(std::invalid_argument(
48  "NccStrategy does not support version " + to_string(*parsed.version)));
49  }
51 }
52 
53 const Name&
55 {
56  static Name strategyName("/localhost/nfd/strategy/ncc/%FD%01");
57  return strategyName;
58 }
59 
60 void
61 NccStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
62  const shared_ptr<pit::Entry>& pitEntry)
63 {
64  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
65  const fib::NextHopList& nexthops = fibEntry.getNextHops();
66  if (nexthops.size() == 0) {
67  this->rejectPendingInterest(pitEntry);
68  return;
69  }
70 
71  PitEntryInfo* pitEntryInfo = pitEntry->insertStrategyInfo<PitEntryInfo>().first;
72  bool isNewPitEntry = !hasPendingOutRecords(*pitEntry);
73  if (!isNewPitEntry) {
74  return;
75  }
76 
77  MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
78 
79  time::microseconds deferFirst = DEFER_FIRST_WITHOUT_BEST_FACE;
80  time::microseconds deferRange = DEFER_RANGE_WITHOUT_BEST_FACE;
81  size_t nUpstreams = nexthops.size();
82 
83  shared_ptr<Face> bestFace = meInfo.getBestFace();
84  if (bestFace != nullptr && fibEntry.hasNextHop(*bestFace) &&
85  !wouldViolateScope(inFace, interest, *bestFace) &&
86  canForwardToLegacy(*pitEntry, *bestFace)) {
87  // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ?
88  deferFirst = meInfo.prediction;
89  deferRange = time::microseconds((deferFirst.count() + 1) / 2);
90  --nUpstreams;
91  this->sendInterest(pitEntry, *bestFace, interest);
92  pitEntryInfo->bestFaceTimeout = scheduler::schedule(
93  meInfo.prediction,
94  bind(&NccStrategy::timeoutOnBestFace, this, weak_ptr<pit::Entry>(pitEntry)));
95  }
96  else {
97  // use first eligible nexthop
98  auto firstEligibleNexthop = std::find_if(nexthops.begin(), nexthops.end(),
99  [&] (const fib::NextHop& nexthop) {
100  Face& outFace = nexthop.getFace();
101  return !wouldViolateScope(inFace, interest, outFace) &&
102  canForwardToLegacy(*pitEntry, outFace);
103  });
104  if (firstEligibleNexthop != nexthops.end()) {
105  this->sendInterest(pitEntry, firstEligibleNexthop->getFace(), interest);
106  }
107  else {
108  this->rejectPendingInterest(pitEntry);
109  return;
110  }
111  }
112 
113  shared_ptr<Face> previousFace = meInfo.previousFace.lock();
114  if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
115  !wouldViolateScope(inFace, interest, *previousFace) &&
116  canForwardToLegacy(*pitEntry, *previousFace)) {
117  --nUpstreams;
118  }
119 
120  if (nUpstreams > 0) {
121  pitEntryInfo->maxInterval = std::max(1_us,
122  time::microseconds((2 * deferRange.count() + nUpstreams - 1) / nUpstreams));
123  }
124  else {
125  // Normally, maxInterval is unused if there aren't any face beyond best and previousBest.
126  // However, in case FIB entry gains a new nexthop before doPropagate executes (bug 1853),
127  // this maxInterval would be used to determine when the next doPropagate would happen.
128  pitEntryInfo->maxInterval = deferFirst;
129  }
130  pitEntryInfo->propagateTimer = scheduler::schedule(deferFirst,
131  bind(&NccStrategy::doPropagate, this, inFace.getId(), weak_ptr<pit::Entry>(pitEntry)));
132 }
133 
134 void
135 NccStrategy::doPropagate(FaceId inFaceId, weak_ptr<pit::Entry> pitEntryWeak)
136 {
137  Face* inFace = this->getFace(inFaceId);
138  if (inFace == nullptr) {
139  return;
140  }
141  shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock();
142  if (pitEntry == nullptr) {
143  return;
144  }
145  auto inRecord = pitEntry->getInRecord(*inFace);
146  if (inRecord == pitEntry->in_end()) {
147  return;
148  }
149  const Interest& interest = inRecord->getInterest();
150  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
151 
152  PitEntryInfo* pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
153  // pitEntryInfo is guaranteed to exist here, because doPropagate is triggered
154  // from a timer set by NccStrategy.
155  BOOST_ASSERT(pitEntryInfo != nullptr);
156 
157  MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
158 
159  shared_ptr<Face> previousFace = meInfo.previousFace.lock();
160  if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
161  !wouldViolateScope(*inFace, interest, *previousFace) &&
162  canForwardToLegacy(*pitEntry, *previousFace)) {
163  this->sendInterest(pitEntry, *previousFace, interest);
164  }
165 
166  bool isForwarded = false;
167  for (const auto& nexthop : fibEntry.getNextHops()) {
168  Face& face = nexthop.getFace();
169  if (!wouldViolateScope(*inFace, interest, face) &&
170  canForwardToLegacy(*pitEntry, face)) {
171  isForwarded = true;
172  this->sendInterest(pitEntry, face, interest);
173  break;
174  }
175  }
176 
177  if (isForwarded) {
178  std::uniform_int_distribution<time::nanoseconds::rep> dist(0, pitEntryInfo->maxInterval.count() - 1);
179  time::nanoseconds deferNext = time::nanoseconds(dist(getGlobalRng()));
180  pitEntryInfo->propagateTimer = scheduler::schedule(deferNext,
181  bind(&NccStrategy::doPropagate, this, inFaceId, weak_ptr<pit::Entry>(pitEntry)));
182  }
183 }
184 
185 void
186 NccStrategy::timeoutOnBestFace(weak_ptr<pit::Entry> pitEntryWeak)
187 {
188  shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock();
189  if (pitEntry == nullptr) {
190  return;
191  }
192  measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry);
193 
194  for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) {
195  if (measurementsEntry == nullptr) {
196  // going out of this strategy's namespace
197  break;
198  }
199  this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
200 
201  MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry);
202  meInfo.adjustPredictUp();
203 
204  measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
205  }
206 }
207 
208 void
209 NccStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
210  const Face& inFace, const Data& data)
211 {
212  if (!pitEntry->hasInRecords()) {
213  // PIT entry has already been satisfied (and is now waiting for straggler timer to expire)
214  // NCC does not collect measurements for non-best face
215  return;
216  }
217 
218  measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry);
219 
220  for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) {
221  if (measurementsEntry == nullptr) {
222  // going out of this strategy's namespace
223  return;
224  }
225  this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
226 
227  MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry);
228  meInfo.updateBestFace(inFace);
229 
230  measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
231  }
232 
233  PitEntryInfo* pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
234  if (pitEntryInfo != nullptr) {
235  scheduler::cancel(pitEntryInfo->propagateTimer);
236 
237  // Verify that the best face satisfied the interest before canceling the timeout call
238  MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
239  shared_ptr<Face> bestFace = meInfo.getBestFace();
240 
241  if (bestFace.get() == &inFace)
242  scheduler::cancel(pitEntryInfo->bestFaceTimeout);
243  }
244 }
245 
247 NccStrategy::getMeasurementsEntryInfo(const shared_ptr<pit::Entry>& entry)
248 {
249  measurements::Entry* measurementsEntry = this->getMeasurements().get(*entry);
250  return this->getMeasurementsEntryInfo(measurementsEntry);
251 }
252 
255 {
256  BOOST_ASSERT(entry != nullptr);
257  MeasurementsEntryInfo* info = nullptr;
258  bool isNew = false;
259  std::tie(info, isNew) = entry->insertStrategyInfo<MeasurementsEntryInfo>();
260  if (!isNew) {
261  return *info;
262  }
263 
264  measurements::Entry* parentEntry = this->getMeasurements().getParent(*entry);
265  if (parentEntry != nullptr) {
266  MeasurementsEntryInfo& parentInfo = this->getMeasurementsEntryInfo(parentEntry);
267  info->inheritFrom(parentInfo);
268  }
269 
270  return *info;
271 }
272 
273 const time::microseconds NccStrategy::MeasurementsEntryInfo::INITIAL_PREDICTION = 8192_us;
274 const time::microseconds NccStrategy::MeasurementsEntryInfo::MIN_PREDICTION = 127_us;
275 const time::microseconds NccStrategy::MeasurementsEntryInfo::MAX_PREDICTION = 160_ms;
276 
278  : prediction(INITIAL_PREDICTION)
279 {
280 }
281 
282 void
284 {
285  this->operator=(other);
286 }
287 
288 shared_ptr<Face>
290 {
291  shared_ptr<Face> best = this->bestFace.lock();
292  if (best != nullptr) {
293  return best;
294  }
295  this->bestFace = best = this->previousFace.lock();
296  return best;
297 }
298 
299 void
301 {
302  if (this->bestFace.expired()) {
303  this->bestFace = const_cast<Face&>(face).shared_from_this();
304  return;
305  }
306  shared_ptr<Face> bestFace = this->bestFace.lock();
307  if (bestFace.get() == &face) {
308  this->adjustPredictDown();
309  }
310  else {
311  this->previousFace = this->bestFace;
312  this->bestFace = const_cast<Face&>(face).shared_from_this();
313  }
314 }
315 
316 void
317 NccStrategy::MeasurementsEntryInfo::adjustPredictDown()
318 {
319  prediction = std::max(MIN_PREDICTION,
320  time::microseconds(prediction.count() - (prediction.count() >> ADJUST_PREDICT_DOWN_SHIFT)));
321 }
322 
323 void
325 {
326  prediction = std::min(MAX_PREDICTION,
327  time::microseconds(prediction.count() + (prediction.count() >> ADJUST_PREDICT_UP_SHIFT)));
328 }
329 
330 void
331 NccStrategy::MeasurementsEntryInfo::ageBestFace()
332 {
333  this->previousFace = this->bestFace;
334  this->bestFace.reset();
335 }
336 
338 {
339  scheduler::cancel(this->bestFaceTimeout);
340  scheduler::cancel(this->propagateTimer);
341 }
342 
343 } // namespace fw
344 } // namespace nfd
bool canForwardToLegacy(const pit::Entry &pitEntry, const Face &face)
decide whether Interest can be forwarded to face
Definition: algorithm.cpp:59
static const int ADJUST_PREDICT_UP_SHIFT
bool hasNextHop(const Face &face) const
Definition: fib-entry.cpp:47
main class of NFD
Definition: forwarder.hpp:52
void timeoutOnBestFace(weak_ptr< pit::Entry > pitEntryWeak)
best face did not reply within prediction
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:367
time::microseconds maxInterval
maximum interval between forwarding to two nexthops except best and previous
weak_ptr< Face > bestFace
weak_ptr< Face > previousFace
represents a Measurements entry
static const int UPDATE_MEASUREMENTS_N_LEVELS
void cancel(const EventId &eventId)
Cancel a scheduled event.
Definition: scheduler.cpp:53
time::microseconds prediction
virtual void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data) override
trigger before PIT entry is satisfied
represents a FIB entry
Definition: fib-entry.hpp:51
void adjustPredictUp()
scheduler::EventId bestFaceTimeout
timer that expires when best face does not respond within predicted time
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &outFace, const Interest &interest)
send Interest to outFace
Definition: strategy.hpp:241
static const time::nanoseconds MEASUREMENTS_LIFETIME
StrategyInfo on pit::Entry.
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
Definition: strategy.cpp:133
std::pair< T *, bool > insertStrategyInfo(A &&...args)
insert a StrategyInfo item
void doPropagate(FaceId inFaceId, weak_ptr< pit::Entry > pitEntryWeak)
propagate to another upstream
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:318
#define NFD_REGISTER_STRATEGY(S)
registers a strategy
Definition: strategy.hpp:405
shared_ptr< Face > getBestFace()
MeasurementsEntryInfo & getMeasurementsEntryInfo(measurements::Entry *entry)
scheduler::EventId propagateTimer
timer for propagating to another face
void inheritFrom(const MeasurementsEntryInfo &other)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
static const time::microseconds DEFER_RANGE_WITHOUT_BEST_FACE
NccStrategy(Forwarder &forwarder, const Name &name=getStrategyName())
void updateBestFace(const Face &face)
static const time::microseconds MIN_PREDICTION
MeasurementsEntryInfo()
Represents a collection of nexthops.
PartialName parameters
parameter components
Definition: strategy.hpp:340
bool hasPendingOutRecords(const pit::Entry &pitEntry)
determine whether pitEntry has any pending out-records
Definition: algorithm.cpp:113
std::mt19937 & getGlobalRng()
Definition: random.cpp:32
static const int ADJUST_PREDICT_DOWN_SHIFT
represents a forwarding strategy
Definition: strategy.hpp:37
This file contains common algorithms used by forwarding strategies.
Declares the global pseudorandom number generator (PRNG) for NFD.
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
static const Name & getStrategyName()
static const time::microseconds INITIAL_PREDICTION
StrategyInfo on measurements::Entry.
static const time::microseconds DEFER_FIRST_WITHOUT_BEST_FACE
virtual ~PitEntryInfo() override
uint64_t FaceId
identifies a face
Definition: face.hpp:39
represents a nexthop record in FIB entry
Definition: fib-nexthop.hpp:38
static const time::microseconds MAX_PREDICTION
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
Face * getFace(FaceId id) const
Definition: strategy.hpp:324
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
virtual void afterReceiveInterest(const Face &inFace, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Interest is received
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:339