blob: 213ce59897ed67e25327a060e7a32ab050d35270 [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
42NccStrategy::~NccStrategy()
43{
44}
45
Junxiao Shi1391d612014-03-27 22:27:20 -070046const time::microseconds NccStrategy::DEFER_FIRST_WITHOUT_BEST_FACE = time::microseconds(4000);
47const time::microseconds NccStrategy::DEFER_RANGE_WITHOUT_BEST_FACE = time::microseconds(75000);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070048const time::nanoseconds NccStrategy::MEASUREMENTS_LIFETIME = time::seconds(16);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070049
50void
51NccStrategy::afterReceiveInterest(const Face& inFace,
52 const Interest& interest,
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070053 shared_ptr<pit::Entry> pitEntry)
54{
Junxiao Shi8d843142016-07-11 22:42:42 +000055 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
56 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070057 if (nexthops.size() == 0) {
58 this->rejectPendingInterest(pitEntry);
59 return;
60 }
61
62 shared_ptr<PitEntryInfo> pitEntryInfo =
63 pitEntry->getOrCreateStrategyInfo<PitEntryInfo>();
Junxiao Shifef73e42016-03-29 14:15:05 -070064 bool isNewPitEntry = !hasPendingOutRecords(*pitEntry);
Junxiao Shi8bfd56d2014-10-08 10:06:00 -070065 if (!isNewPitEntry) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070066 return;
67 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070068
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000069 MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070070
Junxiao Shi1391d612014-03-27 22:27:20 -070071 time::microseconds deferFirst = DEFER_FIRST_WITHOUT_BEST_FACE;
72 time::microseconds deferRange = DEFER_RANGE_WITHOUT_BEST_FACE;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070073 size_t nUpstreams = nexthops.size();
74
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000075 shared_ptr<Face> bestFace = meInfo.getBestFace();
Junxiao Shia6de4292016-07-12 02:08:10 +000076 if (bestFace != nullptr && fibEntry.hasNextHop(*bestFace) &&
Junxiao Shifef73e42016-03-29 14:15:05 -070077 canForwardToLegacy(*pitEntry, *bestFace)) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070078 // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ?
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000079 deferFirst = meInfo.prediction;
Junxiao Shi1391d612014-03-27 22:27:20 -070080 deferRange = time::microseconds((deferFirst.count() + 1) / 2);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070081 --nUpstreams;
Junxiao Shia6de4292016-07-12 02:08:10 +000082 this->sendInterest(pitEntry, *bestFace);
Junxiao Shi1391d612014-03-27 22:27:20 -070083 pitEntryInfo->bestFaceTimeout = scheduler::schedule(
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000084 meInfo.prediction,
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070085 bind(&NccStrategy::timeoutOnBestFace, this, weak_ptr<pit::Entry>(pitEntry)));
86 }
87 else {
Junxiao Shi63162202015-01-14 22:27:33 -070088 // use first eligible nexthop
89 auto firstEligibleNexthop = std::find_if(nexthops.begin(), nexthops.end(),
90 [&pitEntry] (const fib::NextHop& nexthop) {
Junxiao Shia6de4292016-07-12 02:08:10 +000091 return canForwardToLegacy(*pitEntry, nexthop.getFace());
Junxiao Shi63162202015-01-14 22:27:33 -070092 });
93 if (firstEligibleNexthop != nexthops.end()) {
94 this->sendInterest(pitEntry, firstEligibleNexthop->getFace());
95 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070096 }
97
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000098 shared_ptr<Face> previousFace = meInfo.previousFace.lock();
Junxiao Shia6de4292016-07-12 02:08:10 +000099 if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
Junxiao Shifef73e42016-03-29 14:15:05 -0700100 canForwardToLegacy(*pitEntry, *previousFace)) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700101 --nUpstreams;
102 }
103
Junxiao Shi1391d612014-03-27 22:27:20 -0700104 if (nUpstreams > 0) {
105 pitEntryInfo->maxInterval = std::max(time::microseconds(1),
106 time::microseconds((2 * deferRange.count() + nUpstreams - 1) / nUpstreams));
107 }
Junxiao Shicbb490a2014-08-13 12:24:24 -0700108 else {
109 // Normally, maxInterval is unused if there aren't any face beyond best and previousBest.
110 // However, in case FIB entry gains a new nexthop before doPropagate executes (bug 1853),
111 // this maxInterval would be used to determine when the next doPropagate would happen.
112 pitEntryInfo->maxInterval = deferFirst;
113 }
Junxiao Shi1391d612014-03-27 22:27:20 -0700114 pitEntryInfo->propagateTimer = scheduler::schedule(deferFirst,
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700115 bind(&NccStrategy::doPropagate, this,
Junxiao Shi8d843142016-07-11 22:42:42 +0000116 weak_ptr<pit::Entry>(pitEntry)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700117}
118
119void
Junxiao Shi8d843142016-07-11 22:42:42 +0000120NccStrategy::doPropagate(weak_ptr<pit::Entry> pitEntryWeak)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700121{
122 shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock();
Junxiao Shi8d843142016-07-11 22:42:42 +0000123 if (pitEntry == nullptr) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700124 return;
125 }
Junxiao Shi8d843142016-07-11 22:42:42 +0000126 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700127
Junxiao Shi1391d612014-03-27 22:27:20 -0700128 shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
129 // pitEntryInfo is guaranteed to exist here, because doPropagate is triggered
130 // from a timer set by NccStrategy.
Junxiao Shi8d843142016-07-11 22:42:42 +0000131 BOOST_ASSERT(pitEntryInfo != nullptr);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700132
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000133 MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700134
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000135 shared_ptr<Face> previousFace = meInfo.previousFace.lock();
Junxiao Shia6de4292016-07-12 02:08:10 +0000136 if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
Junxiao Shifef73e42016-03-29 14:15:05 -0700137 canForwardToLegacy(*pitEntry, *previousFace)) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000138 this->sendInterest(pitEntry, *previousFace);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700139 }
140
Junxiao Shi8d843142016-07-11 22:42:42 +0000141 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700142 bool isForwarded = false;
143 for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000144 Face& face = it->getFace();
145 if (canForwardToLegacy(*pitEntry, face)) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700146 isForwarded = true;
147 this->sendInterest(pitEntry, face);
148 break;
149 }
150 }
151
152 if (isForwarded) {
Junxiao Shicbb490a2014-08-13 12:24:24 -0700153 boost::random::uniform_int_distribution<time::nanoseconds::rep> dist(0,
154 pitEntryInfo->maxInterval.count() - 1);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700155 time::nanoseconds deferNext = time::nanoseconds(dist(getGlobalRng()));
Junxiao Shi1391d612014-03-27 22:27:20 -0700156 pitEntryInfo->propagateTimer = scheduler::schedule(deferNext,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000157 bind(&NccStrategy::doPropagate, this, weak_ptr<pit::Entry>(pitEntry)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700158 }
159}
160
161void
162NccStrategy::timeoutOnBestFace(weak_ptr<pit::Entry> pitEntryWeak)
163{
164 shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock();
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000165 if (pitEntry == nullptr) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700166 return;
167 }
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000168 measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700169
170 for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) {
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000171 if (measurementsEntry == nullptr) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700172 // going out of this strategy's namespace
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000173 break;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700174 }
Junxiao Shie368d992014-12-02 23:44:31 -0700175 this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700176
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000177 MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry);
178 meInfo.adjustPredictUp();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700179
Junxiao Shie368d992014-12-02 23:44:31 -0700180 measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700181 }
182}
183
184void
Junxiao Shi82e7f582014-09-07 15:15:40 -0700185NccStrategy::beforeSatisfyInterest(shared_ptr<pit::Entry> pitEntry,
186 const Face& inFace, const Data& data)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700187{
Junxiao Shi4846f372016-04-05 13:39:30 -0700188 if (!pitEntry->hasInRecords()) {
Junxiao Shi82e7f582014-09-07 15:15:40 -0700189 // PIT entry has already been satisfied (and is now waiting for straggler timer to expire)
190 // NCC does not collect measurements for non-best face
191 return;
192 }
193
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000194 measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700195
196 for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) {
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000197 if (measurementsEntry == nullptr) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700198 // going out of this strategy's namespace
199 return;
200 }
Junxiao Shie368d992014-12-02 23:44:31 -0700201 this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700202
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000203 MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry);
204 meInfo.updateBestFace(inFace);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700205
Junxiao Shie368d992014-12-02 23:44:31 -0700206 measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700207 }
208
Junxiao Shi1391d612014-03-27 22:27:20 -0700209 shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
Hila Ben Abraham39a79be2016-03-30 22:00:55 -0700210 if (pitEntryInfo != nullptr) {
Junxiao Shi1391d612014-03-27 22:27:20 -0700211 scheduler::cancel(pitEntryInfo->propagateTimer);
Hila Ben Abraham39a79be2016-03-30 22:00:55 -0700212
213 // Verify that the best face satisfied the interest before canceling the timeout call
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000214 MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
215 shared_ptr<Face> bestFace = meInfo.getBestFace();
Hila Ben Abraham39a79be2016-03-30 22:00:55 -0700216
217 if (bestFace.get() == &inFace)
218 scheduler::cancel(pitEntryInfo->bestFaceTimeout);
Junxiao Shi1391d612014-03-27 22:27:20 -0700219 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700220}
221
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000222NccStrategy::MeasurementsEntryInfo&
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700223NccStrategy::getMeasurementsEntryInfo(shared_ptr<pit::Entry> entry)
224{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000225 measurements::Entry* measurementsEntry = this->getMeasurements().get(*entry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700226 return this->getMeasurementsEntryInfo(measurementsEntry);
227}
228
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000229NccStrategy::MeasurementsEntryInfo&
230NccStrategy::getMeasurementsEntryInfo(measurements::Entry* entry)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700231{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000232 BOOST_ASSERT(entry != nullptr);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700233 shared_ptr<MeasurementsEntryInfo> info = entry->getStrategyInfo<MeasurementsEntryInfo>();
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000234 if (info != nullptr) {
235 return *info;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700236 }
237
238 info = make_shared<MeasurementsEntryInfo>();
239 entry->setStrategyInfo(info);
240
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000241 measurements::Entry* parentEntry = this->getMeasurements().getParent(*entry);
242 if (parentEntry != nullptr) {
243 MeasurementsEntryInfo& parentInfo = this->getMeasurementsEntryInfo(parentEntry);
244 info->inheritFrom(parentInfo);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700245 }
246
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000247 return *info;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700248}
249
250
Junxiao Shi1391d612014-03-27 22:27:20 -0700251const time::microseconds NccStrategy::MeasurementsEntryInfo::INITIAL_PREDICTION =
252 time::microseconds(8192);
253const time::microseconds NccStrategy::MeasurementsEntryInfo::MIN_PREDICTION =
254 time::microseconds(127);
255const time::microseconds NccStrategy::MeasurementsEntryInfo::MAX_PREDICTION =
256 time::microseconds(160000);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700257
258NccStrategy::MeasurementsEntryInfo::MeasurementsEntryInfo()
Junxiao Shi1391d612014-03-27 22:27:20 -0700259 : prediction(INITIAL_PREDICTION)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700260{
261}
262
263void
264NccStrategy::MeasurementsEntryInfo::inheritFrom(const MeasurementsEntryInfo& other)
265{
266 this->operator=(other);
267}
268
269shared_ptr<Face>
270NccStrategy::MeasurementsEntryInfo::getBestFace(void) {
Junxiao Shi1391d612014-03-27 22:27:20 -0700271 shared_ptr<Face> best = this->bestFace.lock();
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000272 if (best != nullptr) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700273 return best;
274 }
Junxiao Shi1391d612014-03-27 22:27:20 -0700275 this->bestFace = best = this->previousFace.lock();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700276 return best;
277}
278
279void
280NccStrategy::MeasurementsEntryInfo::updateBestFace(const Face& face) {
Junxiao Shi1391d612014-03-27 22:27:20 -0700281 if (this->bestFace.expired()) {
282 this->bestFace = const_cast<Face&>(face).shared_from_this();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700283 return;
284 }
Junxiao Shi1391d612014-03-27 22:27:20 -0700285 shared_ptr<Face> bestFace = this->bestFace.lock();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700286 if (bestFace.get() == &face) {
287 this->adjustPredictDown();
288 }
289 else {
Junxiao Shi1391d612014-03-27 22:27:20 -0700290 this->previousFace = this->bestFace;
291 this->bestFace = const_cast<Face&>(face).shared_from_this();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700292 }
293}
294
295void
296NccStrategy::MeasurementsEntryInfo::adjustPredictDown() {
Junxiao Shi1391d612014-03-27 22:27:20 -0700297 prediction = std::max(MIN_PREDICTION,
298 time::microseconds(prediction.count() - (prediction.count() >> ADJUST_PREDICT_DOWN_SHIFT)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700299}
300
301void
302NccStrategy::MeasurementsEntryInfo::adjustPredictUp() {
Junxiao Shi1391d612014-03-27 22:27:20 -0700303 prediction = std::min(MAX_PREDICTION,
304 time::microseconds(prediction.count() + (prediction.count() >> ADJUST_PREDICT_UP_SHIFT)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700305}
306
307void
308NccStrategy::MeasurementsEntryInfo::ageBestFace() {
Junxiao Shi1391d612014-03-27 22:27:20 -0700309 this->previousFace = this->bestFace;
310 this->bestFace.reset();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700311}
312
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700313NccStrategy::PitEntryInfo::~PitEntryInfo()
314{
Junxiao Shi1391d612014-03-27 22:27:20 -0700315 scheduler::cancel(this->bestFaceTimeout);
316 scheduler::cancel(this->propagateTimer);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700317}
318
319} // namespace fw
320} // namespace nfd