blob: c56eef42d1977a4f008e71ed5343b7e5bf1856d0 [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shifef73e42016-03-29 14:15:05 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi63162202015-01-14 22:27:33 -07004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shiaf6569a2014-06-14 00:01:34 -070024 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070025
26#include "ncc-strategy.hpp"
Junxiao Shifef73e42016-03-29 14:15:05 -070027#include "pit-algorithm.hpp"
Junxiao Shiaf6569a2014-06-14 00:01:34 -070028#include "core/random.hpp"
29#include <boost/random/uniform_int_distribution.hpp>
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070030
31namespace nfd {
32namespace fw {
33
Junxiao Shie93d6a32014-09-07 16:13:22 -070034const Name NccStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/ncc/%FD%01");
Junxiao Shifaf3eb02015-02-16 10:50:36 -070035NFD_REGISTER_STRATEGY(NccStrategy);
Junxiao Shif3c07812014-03-11 21:48:49 -070036
37NccStrategy::NccStrategy(Forwarder& forwarder, const Name& name)
38 : Strategy(forwarder, name)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070039{
40}
41
Junxiao Shi1391d612014-03-27 22:27:20 -070042const time::microseconds NccStrategy::DEFER_FIRST_WITHOUT_BEST_FACE = time::microseconds(4000);
43const time::microseconds NccStrategy::DEFER_RANGE_WITHOUT_BEST_FACE = time::microseconds(75000);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070044const time::nanoseconds NccStrategy::MEASUREMENTS_LIFETIME = time::seconds(16);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070045
46void
Junxiao Shi15e98b02016-08-12 11:21:44 +000047NccStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
48 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070049{
Junxiao Shi8d843142016-07-11 22:42:42 +000050 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
51 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070052 if (nexthops.size() == 0) {
53 this->rejectPendingInterest(pitEntry);
54 return;
55 }
56
Junxiao Shi5b3feb62016-08-19 01:51:41 +000057 shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->insertStrategyInfo<PitEntryInfo>();
Junxiao Shifef73e42016-03-29 14:15:05 -070058 bool isNewPitEntry = !hasPendingOutRecords(*pitEntry);
Junxiao Shi8bfd56d2014-10-08 10:06:00 -070059 if (!isNewPitEntry) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070060 return;
61 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070062
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000063 MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070064
Junxiao Shi1391d612014-03-27 22:27:20 -070065 time::microseconds deferFirst = DEFER_FIRST_WITHOUT_BEST_FACE;
66 time::microseconds deferRange = DEFER_RANGE_WITHOUT_BEST_FACE;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070067 size_t nUpstreams = nexthops.size();
68
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000069 shared_ptr<Face> bestFace = meInfo.getBestFace();
Junxiao Shia6de4292016-07-12 02:08:10 +000070 if (bestFace != nullptr && fibEntry.hasNextHop(*bestFace) &&
Junxiao Shifef73e42016-03-29 14:15:05 -070071 canForwardToLegacy(*pitEntry, *bestFace)) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070072 // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ?
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000073 deferFirst = meInfo.prediction;
Junxiao Shi1391d612014-03-27 22:27:20 -070074 deferRange = time::microseconds((deferFirst.count() + 1) / 2);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070075 --nUpstreams;
Junxiao Shia6de4292016-07-12 02:08:10 +000076 this->sendInterest(pitEntry, *bestFace);
Junxiao Shi1391d612014-03-27 22:27:20 -070077 pitEntryInfo->bestFaceTimeout = scheduler::schedule(
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000078 meInfo.prediction,
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070079 bind(&NccStrategy::timeoutOnBestFace, this, weak_ptr<pit::Entry>(pitEntry)));
80 }
81 else {
Junxiao Shi63162202015-01-14 22:27:33 -070082 // use first eligible nexthop
83 auto firstEligibleNexthop = std::find_if(nexthops.begin(), nexthops.end(),
84 [&pitEntry] (const fib::NextHop& nexthop) {
Junxiao Shia6de4292016-07-12 02:08:10 +000085 return canForwardToLegacy(*pitEntry, nexthop.getFace());
Junxiao Shi63162202015-01-14 22:27:33 -070086 });
87 if (firstEligibleNexthop != nexthops.end()) {
88 this->sendInterest(pitEntry, firstEligibleNexthop->getFace());
89 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070090 }
91
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000092 shared_ptr<Face> previousFace = meInfo.previousFace.lock();
Junxiao Shia6de4292016-07-12 02:08:10 +000093 if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
Junxiao Shifef73e42016-03-29 14:15:05 -070094 canForwardToLegacy(*pitEntry, *previousFace)) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070095 --nUpstreams;
96 }
97
Junxiao Shi1391d612014-03-27 22:27:20 -070098 if (nUpstreams > 0) {
99 pitEntryInfo->maxInterval = std::max(time::microseconds(1),
100 time::microseconds((2 * deferRange.count() + nUpstreams - 1) / nUpstreams));
101 }
Junxiao Shicbb490a2014-08-13 12:24:24 -0700102 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 }
Junxiao Shi1391d612014-03-27 22:27:20 -0700108 pitEntryInfo->propagateTimer = scheduler::schedule(deferFirst,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000109 bind(&NccStrategy::doPropagate, this, weak_ptr<pit::Entry>(pitEntry)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700110}
111
112void
Junxiao Shi8d843142016-07-11 22:42:42 +0000113NccStrategy::doPropagate(weak_ptr<pit::Entry> pitEntryWeak)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700114{
115 shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock();
Junxiao Shi8d843142016-07-11 22:42:42 +0000116 if (pitEntry == nullptr) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700117 return;
118 }
Junxiao Shi8d843142016-07-11 22:42:42 +0000119 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700120
Junxiao Shi1391d612014-03-27 22:27:20 -0700121 shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
122 // pitEntryInfo is guaranteed to exist here, because doPropagate is triggered
123 // from a timer set by NccStrategy.
Junxiao Shi8d843142016-07-11 22:42:42 +0000124 BOOST_ASSERT(pitEntryInfo != nullptr);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700125
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000126 MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700127
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000128 shared_ptr<Face> previousFace = meInfo.previousFace.lock();
Junxiao Shia6de4292016-07-12 02:08:10 +0000129 if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
Junxiao Shifef73e42016-03-29 14:15:05 -0700130 canForwardToLegacy(*pitEntry, *previousFace)) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000131 this->sendInterest(pitEntry, *previousFace);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700132 }
133
Junxiao Shi8d843142016-07-11 22:42:42 +0000134 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700135 bool isForwarded = false;
136 for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000137 Face& face = it->getFace();
138 if (canForwardToLegacy(*pitEntry, face)) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700139 isForwarded = true;
140 this->sendInterest(pitEntry, face);
141 break;
142 }
143 }
144
145 if (isForwarded) {
Junxiao Shicbb490a2014-08-13 12:24:24 -0700146 boost::random::uniform_int_distribution<time::nanoseconds::rep> dist(0,
147 pitEntryInfo->maxInterval.count() - 1);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700148 time::nanoseconds deferNext = time::nanoseconds(dist(getGlobalRng()));
Junxiao Shi1391d612014-03-27 22:27:20 -0700149 pitEntryInfo->propagateTimer = scheduler::schedule(deferNext,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000150 bind(&NccStrategy::doPropagate, this, weak_ptr<pit::Entry>(pitEntry)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700151 }
152}
153
154void
155NccStrategy::timeoutOnBestFace(weak_ptr<pit::Entry> pitEntryWeak)
156{
157 shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock();
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000158 if (pitEntry == nullptr) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700159 return;
160 }
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000161 measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700162
163 for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) {
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000164 if (measurementsEntry == nullptr) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700165 // going out of this strategy's namespace
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000166 break;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700167 }
Junxiao Shie368d992014-12-02 23:44:31 -0700168 this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700169
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000170 MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry);
171 meInfo.adjustPredictUp();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700172
Junxiao Shie368d992014-12-02 23:44:31 -0700173 measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700174 }
175}
176
177void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000178NccStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi82e7f582014-09-07 15:15:40 -0700179 const Face& inFace, const Data& data)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700180{
Junxiao Shi4846f372016-04-05 13:39:30 -0700181 if (!pitEntry->hasInRecords()) {
Junxiao Shi82e7f582014-09-07 15:15:40 -0700182 // 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
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000187 measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700188
189 for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) {
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000190 if (measurementsEntry == nullptr) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700191 // going out of this strategy's namespace
192 return;
193 }
Junxiao Shie368d992014-12-02 23:44:31 -0700194 this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700195
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000196 MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry);
197 meInfo.updateBestFace(inFace);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700198
Junxiao Shie368d992014-12-02 23:44:31 -0700199 measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700200 }
201
Junxiao Shi1391d612014-03-27 22:27:20 -0700202 shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
Hila Ben Abraham39a79be2016-03-30 22:00:55 -0700203 if (pitEntryInfo != nullptr) {
Junxiao Shi1391d612014-03-27 22:27:20 -0700204 scheduler::cancel(pitEntryInfo->propagateTimer);
Hila Ben Abraham39a79be2016-03-30 22:00:55 -0700205
206 // Verify that the best face satisfied the interest before canceling the timeout call
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000207 MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
208 shared_ptr<Face> bestFace = meInfo.getBestFace();
Hila Ben Abraham39a79be2016-03-30 22:00:55 -0700209
210 if (bestFace.get() == &inFace)
211 scheduler::cancel(pitEntryInfo->bestFaceTimeout);
Junxiao Shi1391d612014-03-27 22:27:20 -0700212 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700213}
214
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000215NccStrategy::MeasurementsEntryInfo&
Junxiao Shi15e98b02016-08-12 11:21:44 +0000216NccStrategy::getMeasurementsEntryInfo(const shared_ptr<pit::Entry>& entry)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700217{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000218 measurements::Entry* measurementsEntry = this->getMeasurements().get(*entry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700219 return this->getMeasurementsEntryInfo(measurementsEntry);
220}
221
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000222NccStrategy::MeasurementsEntryInfo&
223NccStrategy::getMeasurementsEntryInfo(measurements::Entry* entry)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700224{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000225 BOOST_ASSERT(entry != nullptr);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700226 shared_ptr<MeasurementsEntryInfo> info = entry->getStrategyInfo<MeasurementsEntryInfo>();
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000227 if (info != nullptr) {
228 return *info;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700229 }
230
231 info = make_shared<MeasurementsEntryInfo>();
232 entry->setStrategyInfo(info);
233
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000234 measurements::Entry* parentEntry = this->getMeasurements().getParent(*entry);
235 if (parentEntry != nullptr) {
236 MeasurementsEntryInfo& parentInfo = this->getMeasurementsEntryInfo(parentEntry);
237 info->inheritFrom(parentInfo);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700238 }
239
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000240 return *info;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700241}
242
243
Junxiao Shi1391d612014-03-27 22:27:20 -0700244const time::microseconds NccStrategy::MeasurementsEntryInfo::INITIAL_PREDICTION =
245 time::microseconds(8192);
246const time::microseconds NccStrategy::MeasurementsEntryInfo::MIN_PREDICTION =
247 time::microseconds(127);
248const time::microseconds NccStrategy::MeasurementsEntryInfo::MAX_PREDICTION =
249 time::microseconds(160000);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700250
251NccStrategy::MeasurementsEntryInfo::MeasurementsEntryInfo()
Junxiao Shi1391d612014-03-27 22:27:20 -0700252 : prediction(INITIAL_PREDICTION)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700253{
254}
255
256void
257NccStrategy::MeasurementsEntryInfo::inheritFrom(const MeasurementsEntryInfo& other)
258{
259 this->operator=(other);
260}
261
262shared_ptr<Face>
263NccStrategy::MeasurementsEntryInfo::getBestFace(void) {
Junxiao Shi1391d612014-03-27 22:27:20 -0700264 shared_ptr<Face> best = this->bestFace.lock();
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000265 if (best != nullptr) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700266 return best;
267 }
Junxiao Shi1391d612014-03-27 22:27:20 -0700268 this->bestFace = best = this->previousFace.lock();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700269 return best;
270}
271
272void
273NccStrategy::MeasurementsEntryInfo::updateBestFace(const Face& face) {
Junxiao Shi1391d612014-03-27 22:27:20 -0700274 if (this->bestFace.expired()) {
275 this->bestFace = const_cast<Face&>(face).shared_from_this();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700276 return;
277 }
Junxiao Shi1391d612014-03-27 22:27:20 -0700278 shared_ptr<Face> bestFace = this->bestFace.lock();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700279 if (bestFace.get() == &face) {
280 this->adjustPredictDown();
281 }
282 else {
Junxiao Shi1391d612014-03-27 22:27:20 -0700283 this->previousFace = this->bestFace;
284 this->bestFace = const_cast<Face&>(face).shared_from_this();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700285 }
286}
287
288void
289NccStrategy::MeasurementsEntryInfo::adjustPredictDown() {
Junxiao Shi1391d612014-03-27 22:27:20 -0700290 prediction = std::max(MIN_PREDICTION,
291 time::microseconds(prediction.count() - (prediction.count() >> ADJUST_PREDICT_DOWN_SHIFT)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700292}
293
294void
295NccStrategy::MeasurementsEntryInfo::adjustPredictUp() {
Junxiao Shi1391d612014-03-27 22:27:20 -0700296 prediction = std::min(MAX_PREDICTION,
297 time::microseconds(prediction.count() + (prediction.count() >> ADJUST_PREDICT_UP_SHIFT)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700298}
299
300void
301NccStrategy::MeasurementsEntryInfo::ageBestFace() {
Junxiao Shi1391d612014-03-27 22:27:20 -0700302 this->previousFace = this->bestFace;
303 this->bestFace.reset();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700304}
305
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700306NccStrategy::PitEntryInfo::~PitEntryInfo()
307{
Junxiao Shi1391d612014-03-27 22:27:20 -0700308 scheduler::cancel(this->bestFaceTimeout);
309 scheduler::cancel(this->propagateTimer);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700310}
311
312} // namespace fw
313} // namespace nfd