ncc-strategy.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "ncc-strategy.hpp"
27 #include "pit-algorithm.hpp"
28 #include "core/random.hpp"
29 #include <boost/random/uniform_int_distribution.hpp>
30 
31 namespace nfd {
32 namespace fw {
33 
34 const Name NccStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/ncc/%FD%01");
35 NFD_REGISTER_STRATEGY(NccStrategy);
36 
37 NccStrategy::NccStrategy(Forwarder& forwarder, const Name& name)
38  : Strategy(forwarder, name)
39 {
40 }
41 
42 const time::microseconds NccStrategy::DEFER_FIRST_WITHOUT_BEST_FACE = time::microseconds(4000);
43 const time::microseconds NccStrategy::DEFER_RANGE_WITHOUT_BEST_FACE = time::microseconds(75000);
44 const time::nanoseconds NccStrategy::MEASUREMENTS_LIFETIME = time::seconds(16);
45 
46 void
47 NccStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
48  const shared_ptr<pit::Entry>& pitEntry)
49 {
50  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
51  const fib::NextHopList& nexthops = fibEntry.getNextHops();
52  if (nexthops.size() == 0) {
53  this->rejectPendingInterest(pitEntry);
54  return;
55  }
56 
57  PitEntryInfo* pitEntryInfo = pitEntry->insertStrategyInfo<PitEntryInfo>().first;
58  bool isNewPitEntry = !hasPendingOutRecords(*pitEntry);
59  if (!isNewPitEntry) {
60  return;
61  }
62 
63  MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
64 
65  time::microseconds deferFirst = DEFER_FIRST_WITHOUT_BEST_FACE;
66  time::microseconds deferRange = DEFER_RANGE_WITHOUT_BEST_FACE;
67  size_t nUpstreams = nexthops.size();
68 
69  shared_ptr<Face> bestFace = meInfo.getBestFace();
70  if (bestFace != nullptr && fibEntry.hasNextHop(*bestFace) &&
71  canForwardToLegacy(*pitEntry, *bestFace)) {
72  // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ?
73  deferFirst = meInfo.prediction;
74  deferRange = time::microseconds((deferFirst.count() + 1) / 2);
75  --nUpstreams;
76  this->sendInterest(pitEntry, *bestFace);
77  pitEntryInfo->bestFaceTimeout = scheduler::schedule(
78  meInfo.prediction,
79  bind(&NccStrategy::timeoutOnBestFace, this, weak_ptr<pit::Entry>(pitEntry)));
80  }
81  else {
82  // use first eligible nexthop
83  auto firstEligibleNexthop = std::find_if(nexthops.begin(), nexthops.end(),
84  [&pitEntry] (const fib::NextHop& nexthop) {
85  return canForwardToLegacy(*pitEntry, nexthop.getFace());
86  });
87  if (firstEligibleNexthop != nexthops.end()) {
88  this->sendInterest(pitEntry, firstEligibleNexthop->getFace());
89  }
90  }
91 
92  shared_ptr<Face> previousFace = meInfo.previousFace.lock();
93  if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
94  canForwardToLegacy(*pitEntry, *previousFace)) {
95  --nUpstreams;
96  }
97 
98  if (nUpstreams > 0) {
99  pitEntryInfo->maxInterval = std::max(time::microseconds(1),
100  time::microseconds((2 * deferRange.count() + nUpstreams - 1) / nUpstreams));
101  }
102  else {
103  // Normally, maxInterval is unused if there aren't any face beyond best and previousBest.
104  // However, in case FIB entry gains a new nexthop before doPropagate executes (bug 1853),
105  // this maxInterval would be used to determine when the next doPropagate would happen.
106  pitEntryInfo->maxInterval = deferFirst;
107  }
108  pitEntryInfo->propagateTimer = scheduler::schedule(deferFirst,
109  bind(&NccStrategy::doPropagate, this, weak_ptr<pit::Entry>(pitEntry)));
110 }
111 
112 void
113 NccStrategy::doPropagate(weak_ptr<pit::Entry> pitEntryWeak)
114 {
115  shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock();
116  if (pitEntry == nullptr) {
117  return;
118  }
119  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
120 
121  PitEntryInfo* pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
122  // pitEntryInfo is guaranteed to exist here, because doPropagate is triggered
123  // from a timer set by NccStrategy.
124  BOOST_ASSERT(pitEntryInfo != nullptr);
125 
126  MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
127 
128  shared_ptr<Face> previousFace = meInfo.previousFace.lock();
129  if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
130  canForwardToLegacy(*pitEntry, *previousFace)) {
131  this->sendInterest(pitEntry, *previousFace);
132  }
133 
134  const fib::NextHopList& nexthops = fibEntry.getNextHops();
135  bool isForwarded = false;
136  for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
137  Face& face = it->getFace();
138  if (canForwardToLegacy(*pitEntry, face)) {
139  isForwarded = true;
140  this->sendInterest(pitEntry, face);
141  break;
142  }
143  }
144 
145  if (isForwarded) {
146  boost::random::uniform_int_distribution<time::nanoseconds::rep> dist(0,
147  pitEntryInfo->maxInterval.count() - 1);
148  time::nanoseconds deferNext = time::nanoseconds(dist(getGlobalRng()));
149  pitEntryInfo->propagateTimer = scheduler::schedule(deferNext,
150  bind(&NccStrategy::doPropagate, this, weak_ptr<pit::Entry>(pitEntry)));
151  }
152 }
153 
154 void
155 NccStrategy::timeoutOnBestFace(weak_ptr<pit::Entry> pitEntryWeak)
156 {
157  shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock();
158  if (pitEntry == nullptr) {
159  return;
160  }
161  measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry);
162 
163  for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) {
164  if (measurementsEntry == nullptr) {
165  // going out of this strategy's namespace
166  break;
167  }
168  this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
169 
170  MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry);
171  meInfo.adjustPredictUp();
172 
173  measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
174  }
175 }
176 
177 void
178 NccStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
179  const Face& inFace, const Data& data)
180 {
181  if (!pitEntry->hasInRecords()) {
182  // PIT entry has already been satisfied (and is now waiting for straggler timer to expire)
183  // NCC does not collect measurements for non-best face
184  return;
185  }
186 
187  measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry);
188 
189  for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) {
190  if (measurementsEntry == nullptr) {
191  // going out of this strategy's namespace
192  return;
193  }
194  this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
195 
196  MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry);
197  meInfo.updateBestFace(inFace);
198 
199  measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
200  }
201 
202  PitEntryInfo* pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
203  if (pitEntryInfo != nullptr) {
204  scheduler::cancel(pitEntryInfo->propagateTimer);
205 
206  // Verify that the best face satisfied the interest before canceling the timeout call
207  MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
208  shared_ptr<Face> bestFace = meInfo.getBestFace();
209 
210  if (bestFace.get() == &inFace)
211  scheduler::cancel(pitEntryInfo->bestFaceTimeout);
212  }
213 }
214 
216 NccStrategy::getMeasurementsEntryInfo(const shared_ptr<pit::Entry>& entry)
217 {
218  measurements::Entry* measurementsEntry = this->getMeasurements().get(*entry);
219  return this->getMeasurementsEntryInfo(measurementsEntry);
220 }
221 
224 {
225  BOOST_ASSERT(entry != nullptr);
226  MeasurementsEntryInfo* info = nullptr;
227  bool isNew = false;
228  std::tie(info, isNew) = entry->insertStrategyInfo<MeasurementsEntryInfo>();
229  if (!isNew) {
230  return *info;
231  }
232 
233  measurements::Entry* parentEntry = this->getMeasurements().getParent(*entry);
234  if (parentEntry != nullptr) {
235  MeasurementsEntryInfo& parentInfo = this->getMeasurementsEntryInfo(parentEntry);
236  info->inheritFrom(parentInfo);
237  }
238 
239  return *info;
240 }
241 
242 
244  time::microseconds(8192);
246  time::microseconds(127);
248  time::microseconds(160000);
249 
251  : prediction(INITIAL_PREDICTION)
252 {
253 }
254 
255 void
257 {
258  this->operator=(other);
259 }
260 
261 shared_ptr<Face>
263  shared_ptr<Face> best = this->bestFace.lock();
264  if (best != nullptr) {
265  return best;
266  }
267  this->bestFace = best = this->previousFace.lock();
268  return best;
269 }
270 
271 void
273  if (this->bestFace.expired()) {
274  this->bestFace = const_cast<Face&>(face).shared_from_this();
275  return;
276  }
277  shared_ptr<Face> bestFace = this->bestFace.lock();
278  if (bestFace.get() == &face) {
279  this->adjustPredictDown();
280  }
281  else {
282  this->previousFace = this->bestFace;
283  this->bestFace = const_cast<Face&>(face).shared_from_this();
284  }
285 }
286 
287 void
288 NccStrategy::MeasurementsEntryInfo::adjustPredictDown() {
289  prediction = std::max(MIN_PREDICTION,
290  time::microseconds(prediction.count() - (prediction.count() >> ADJUST_PREDICT_DOWN_SHIFT)));
291 }
292 
293 void
295  prediction = std::min(MAX_PREDICTION,
296  time::microseconds(prediction.count() + (prediction.count() >> ADJUST_PREDICT_UP_SHIFT)));
297 }
298 
299 void
300 NccStrategy::MeasurementsEntryInfo::ageBestFace() {
301  this->previousFace = this->bestFace;
302  this->bestFace.reset();
303 }
304 
306 {
307  scheduler::cancel(this->bestFaceTimeout);
308  scheduler::cancel(this->propagateTimer);
309 }
310 
311 } // namespace fw
312 } // namespace nfd
bool canForwardToLegacy(const pit::Entry &pitEntry, const Face &face)
decide whether Interest can be forwarded to face
#define NFD_REGISTER_STRATEGY(StrategyType)
registers a built-in strategy
NccStrategy(Forwarder &forwarder, const Name &name=STRATEGY_NAME)
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
time::microseconds maxInterval
maximum interval between forwarding to two nexthops except best and previous
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
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
static const time::nanoseconds MEASUREMENTS_LIFETIME
StrategyInfo on pit::Entry.
std::pair< T *, bool > insertStrategyInfo(A &&...args)
insert a StrategyInfo item
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:188
shared_ptr< Face > getBestFace()
static const Name STRATEGY_NAME
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
void doPropagate(weak_ptr< pit::Entry > pitEntryWeak)
propagate to another upstream
void updateBestFace(const Face &face)
std::vector< fib::NextHop > NextHopList
Definition: fib-entry.hpp:47
static const time::microseconds MIN_PREDICTION
MeasurementsEntryInfo()
boost::random::mt19937 & getGlobalRng()
Definition: random.cpp:34
bool hasPendingOutRecords(const pit::Entry &pitEntry)
determine whether pitEntry has any pending out-records
represents a forwarding strategy
Definition: strategy.hpp:38
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
Definition: scheduler.cpp:47
static const time::microseconds INITIAL_PREDICTION
StrategyInfo on measurements::Entry.
static const time::microseconds DEFER_FIRST_WITHOUT_BEST_FACE
virtual ~PitEntryInfo() override
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
static const time::microseconds MAX_PREDICTION
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:151
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:91
virtual void afterReceiveInterest(const Face &inFace, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Interest is received