blob: fc9cd20c2d9427b11f74ae2393816689991ea04e [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
69 shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo =
70 this->getMeasurementsEntryInfo(pitEntry);
71
Junxiao Shi1391d612014-03-27 22:27:20 -070072 time::microseconds deferFirst = DEFER_FIRST_WITHOUT_BEST_FACE;
73 time::microseconds deferRange = DEFER_RANGE_WITHOUT_BEST_FACE;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070074 size_t nUpstreams = nexthops.size();
75
76 shared_ptr<Face> bestFace = measurementsEntryInfo->getBestFace();
Junxiao Shia6de4292016-07-12 02:08:10 +000077 if (bestFace != nullptr && fibEntry.hasNextHop(*bestFace) &&
Junxiao Shifef73e42016-03-29 14:15:05 -070078 canForwardToLegacy(*pitEntry, *bestFace)) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070079 // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ?
Junxiao Shi1391d612014-03-27 22:27:20 -070080 deferFirst = measurementsEntryInfo->prediction;
81 deferRange = time::microseconds((deferFirst.count() + 1) / 2);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070082 --nUpstreams;
Junxiao Shia6de4292016-07-12 02:08:10 +000083 this->sendInterest(pitEntry, *bestFace);
Junxiao Shi1391d612014-03-27 22:27:20 -070084 pitEntryInfo->bestFaceTimeout = scheduler::schedule(
85 measurementsEntryInfo->prediction,
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070086 bind(&NccStrategy::timeoutOnBestFace, this, weak_ptr<pit::Entry>(pitEntry)));
87 }
88 else {
Junxiao Shi63162202015-01-14 22:27:33 -070089 // use first eligible nexthop
90 auto firstEligibleNexthop = std::find_if(nexthops.begin(), nexthops.end(),
91 [&pitEntry] (const fib::NextHop& nexthop) {
Junxiao Shia6de4292016-07-12 02:08:10 +000092 return canForwardToLegacy(*pitEntry, nexthop.getFace());
Junxiao Shi63162202015-01-14 22:27:33 -070093 });
94 if (firstEligibleNexthop != nexthops.end()) {
95 this->sendInterest(pitEntry, firstEligibleNexthop->getFace());
96 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070097 }
98
Junxiao Shi1391d612014-03-27 22:27:20 -070099 shared_ptr<Face> previousFace = measurementsEntryInfo->previousFace.lock();
Junxiao Shia6de4292016-07-12 02:08:10 +0000100 if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
Junxiao Shifef73e42016-03-29 14:15:05 -0700101 canForwardToLegacy(*pitEntry, *previousFace)) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700102 --nUpstreams;
103 }
104
Junxiao Shi1391d612014-03-27 22:27:20 -0700105 if (nUpstreams > 0) {
106 pitEntryInfo->maxInterval = std::max(time::microseconds(1),
107 time::microseconds((2 * deferRange.count() + nUpstreams - 1) / nUpstreams));
108 }
Junxiao Shicbb490a2014-08-13 12:24:24 -0700109 else {
110 // Normally, maxInterval is unused if there aren't any face beyond best and previousBest.
111 // However, in case FIB entry gains a new nexthop before doPropagate executes (bug 1853),
112 // this maxInterval would be used to determine when the next doPropagate would happen.
113 pitEntryInfo->maxInterval = deferFirst;
114 }
Junxiao Shi1391d612014-03-27 22:27:20 -0700115 pitEntryInfo->propagateTimer = scheduler::schedule(deferFirst,
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700116 bind(&NccStrategy::doPropagate, this,
Junxiao Shi8d843142016-07-11 22:42:42 +0000117 weak_ptr<pit::Entry>(pitEntry)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700118}
119
120void
Junxiao Shi8d843142016-07-11 22:42:42 +0000121NccStrategy::doPropagate(weak_ptr<pit::Entry> pitEntryWeak)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700122{
123 shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock();
Junxiao Shi8d843142016-07-11 22:42:42 +0000124 if (pitEntry == nullptr) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700125 return;
126 }
Junxiao Shi8d843142016-07-11 22:42:42 +0000127 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700128
Junxiao Shi1391d612014-03-27 22:27:20 -0700129 shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
130 // pitEntryInfo is guaranteed to exist here, because doPropagate is triggered
131 // from a timer set by NccStrategy.
Junxiao Shi8d843142016-07-11 22:42:42 +0000132 BOOST_ASSERT(pitEntryInfo != nullptr);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700133
134 shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo =
135 this->getMeasurementsEntryInfo(pitEntry);
136
Junxiao Shi1391d612014-03-27 22:27:20 -0700137 shared_ptr<Face> previousFace = measurementsEntryInfo->previousFace.lock();
Junxiao Shia6de4292016-07-12 02:08:10 +0000138 if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
Junxiao Shifef73e42016-03-29 14:15:05 -0700139 canForwardToLegacy(*pitEntry, *previousFace)) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000140 this->sendInterest(pitEntry, *previousFace);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700141 }
142
Junxiao Shi8d843142016-07-11 22:42:42 +0000143 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700144 bool isForwarded = false;
145 for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000146 Face& face = it->getFace();
147 if (canForwardToLegacy(*pitEntry, face)) {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700148 isForwarded = true;
149 this->sendInterest(pitEntry, face);
150 break;
151 }
152 }
153
154 if (isForwarded) {
Junxiao Shicbb490a2014-08-13 12:24:24 -0700155 boost::random::uniform_int_distribution<time::nanoseconds::rep> dist(0,
156 pitEntryInfo->maxInterval.count() - 1);
Junxiao Shiaf6569a2014-06-14 00:01:34 -0700157 time::nanoseconds deferNext = time::nanoseconds(dist(getGlobalRng()));
Junxiao Shi1391d612014-03-27 22:27:20 -0700158 pitEntryInfo->propagateTimer = scheduler::schedule(deferNext,
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700159 bind(&NccStrategy::doPropagate, this,
Junxiao Shi8d843142016-07-11 22:42:42 +0000160 weak_ptr<pit::Entry>(pitEntry)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700161 }
162}
163
164void
165NccStrategy::timeoutOnBestFace(weak_ptr<pit::Entry> pitEntryWeak)
166{
167 shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock();
168 if (!static_cast<bool>(pitEntry)) {
169 return;
170 }
171 shared_ptr<measurements::Entry> measurementsEntry = this->getMeasurements().get(*pitEntry);
172
173 for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) {
174 if (!static_cast<bool>(measurementsEntry)) {
175 // going out of this strategy's namespace
176 return;
177 }
Junxiao Shie368d992014-12-02 23:44:31 -0700178 this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700179
180 shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo =
181 this->getMeasurementsEntryInfo(measurementsEntry);
182 measurementsEntryInfo->adjustPredictUp();
183
Junxiao Shie368d992014-12-02 23:44:31 -0700184 measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700185 }
186}
187
188void
Junxiao Shi82e7f582014-09-07 15:15:40 -0700189NccStrategy::beforeSatisfyInterest(shared_ptr<pit::Entry> pitEntry,
190 const Face& inFace, const Data& data)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700191{
Junxiao Shi4846f372016-04-05 13:39:30 -0700192 if (!pitEntry->hasInRecords()) {
Junxiao Shi82e7f582014-09-07 15:15:40 -0700193 // PIT entry has already been satisfied (and is now waiting for straggler timer to expire)
194 // NCC does not collect measurements for non-best face
195 return;
196 }
197
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700198 shared_ptr<measurements::Entry> measurementsEntry = this->getMeasurements().get(*pitEntry);
199
200 for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) {
201 if (!static_cast<bool>(measurementsEntry)) {
202 // going out of this strategy's namespace
203 return;
204 }
Junxiao Shie368d992014-12-02 23:44:31 -0700205 this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700206
207 shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo =
208 this->getMeasurementsEntryInfo(measurementsEntry);
209 measurementsEntryInfo->updateBestFace(inFace);
210
Junxiao Shie368d992014-12-02 23:44:31 -0700211 measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700212 }
213
Junxiao Shi1391d612014-03-27 22:27:20 -0700214 shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
Hila Ben Abraham39a79be2016-03-30 22:00:55 -0700215 if (pitEntryInfo != nullptr) {
Junxiao Shi1391d612014-03-27 22:27:20 -0700216 scheduler::cancel(pitEntryInfo->propagateTimer);
Hila Ben Abraham39a79be2016-03-30 22:00:55 -0700217
218 // Verify that the best face satisfied the interest before canceling the timeout call
219 shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo =
220 this->getMeasurementsEntryInfo(pitEntry);
221 shared_ptr<Face> bestFace = measurementsEntryInfo->getBestFace();
222
223 if (bestFace.get() == &inFace)
224 scheduler::cancel(pitEntryInfo->bestFaceTimeout);
Junxiao Shi1391d612014-03-27 22:27:20 -0700225 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700226}
227
228shared_ptr<NccStrategy::MeasurementsEntryInfo>
229NccStrategy::getMeasurementsEntryInfo(shared_ptr<pit::Entry> entry)
230{
231 shared_ptr<measurements::Entry> measurementsEntry = this->getMeasurements().get(*entry);
232 return this->getMeasurementsEntryInfo(measurementsEntry);
233}
234
235shared_ptr<NccStrategy::MeasurementsEntryInfo>
236NccStrategy::getMeasurementsEntryInfo(shared_ptr<measurements::Entry> entry)
237{
238 shared_ptr<MeasurementsEntryInfo> info = entry->getStrategyInfo<MeasurementsEntryInfo>();
239 if (static_cast<bool>(info)) {
240 return info;
241 }
242
243 info = make_shared<MeasurementsEntryInfo>();
244 entry->setStrategyInfo(info);
245
Junxiao Shie368d992014-12-02 23:44:31 -0700246 shared_ptr<measurements::Entry> parentEntry = this->getMeasurements().getParent(*entry);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700247 if (static_cast<bool>(parentEntry)) {
248 shared_ptr<MeasurementsEntryInfo> parentInfo = this->getMeasurementsEntryInfo(parentEntry);
249 BOOST_ASSERT(static_cast<bool>(parentInfo));
250 info->inheritFrom(*parentInfo);
251 }
252
253 return info;
254}
255
256
Junxiao Shi1391d612014-03-27 22:27:20 -0700257const time::microseconds NccStrategy::MeasurementsEntryInfo::INITIAL_PREDICTION =
258 time::microseconds(8192);
259const time::microseconds NccStrategy::MeasurementsEntryInfo::MIN_PREDICTION =
260 time::microseconds(127);
261const time::microseconds NccStrategy::MeasurementsEntryInfo::MAX_PREDICTION =
262 time::microseconds(160000);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700263
264NccStrategy::MeasurementsEntryInfo::MeasurementsEntryInfo()
Junxiao Shi1391d612014-03-27 22:27:20 -0700265 : prediction(INITIAL_PREDICTION)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700266{
267}
268
269void
270NccStrategy::MeasurementsEntryInfo::inheritFrom(const MeasurementsEntryInfo& other)
271{
272 this->operator=(other);
273}
274
275shared_ptr<Face>
276NccStrategy::MeasurementsEntryInfo::getBestFace(void) {
Junxiao Shi1391d612014-03-27 22:27:20 -0700277 shared_ptr<Face> best = this->bestFace.lock();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700278 if (static_cast<bool>(best)) {
279 return best;
280 }
Junxiao Shi1391d612014-03-27 22:27:20 -0700281 this->bestFace = best = this->previousFace.lock();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700282 return best;
283}
284
285void
286NccStrategy::MeasurementsEntryInfo::updateBestFace(const Face& face) {
Junxiao Shi1391d612014-03-27 22:27:20 -0700287 if (this->bestFace.expired()) {
288 this->bestFace = const_cast<Face&>(face).shared_from_this();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700289 return;
290 }
Junxiao Shi1391d612014-03-27 22:27:20 -0700291 shared_ptr<Face> bestFace = this->bestFace.lock();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700292 if (bestFace.get() == &face) {
293 this->adjustPredictDown();
294 }
295 else {
Junxiao Shi1391d612014-03-27 22:27:20 -0700296 this->previousFace = this->bestFace;
297 this->bestFace = const_cast<Face&>(face).shared_from_this();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700298 }
299}
300
301void
302NccStrategy::MeasurementsEntryInfo::adjustPredictDown() {
Junxiao Shi1391d612014-03-27 22:27:20 -0700303 prediction = std::max(MIN_PREDICTION,
304 time::microseconds(prediction.count() - (prediction.count() >> ADJUST_PREDICT_DOWN_SHIFT)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700305}
306
307void
308NccStrategy::MeasurementsEntryInfo::adjustPredictUp() {
Junxiao Shi1391d612014-03-27 22:27:20 -0700309 prediction = std::min(MAX_PREDICTION,
310 time::microseconds(prediction.count() + (prediction.count() >> ADJUST_PREDICT_UP_SHIFT)));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700311}
312
313void
314NccStrategy::MeasurementsEntryInfo::ageBestFace() {
Junxiao Shi1391d612014-03-27 22:27:20 -0700315 this->previousFace = this->bestFace;
316 this->bestFace.reset();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700317}
318
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700319NccStrategy::PitEntryInfo::~PitEntryInfo()
320{
Junxiao Shi1391d612014-03-27 22:27:20 -0700321 scheduler::cancel(this->bestFaceTimeout);
322 scheduler::cancel(this->propagateTimer);
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700323}
324
325} // namespace fw
326} // namespace nfd