Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | 6316220 | 2015-01-14 22:27:33 -0700 | [diff] [blame] | 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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 25 | |
| 26 | #include "ncc-strategy.hpp" |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 27 | #include "algorithm.hpp" |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 28 | #include "core/random.hpp" |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
| 31 | namespace fw { |
| 32 | |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 33 | NFD_REGISTER_STRATEGY(NccStrategy); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 34 | |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 35 | const time::microseconds NccStrategy::DEFER_FIRST_WITHOUT_BEST_FACE = time::microseconds(4000); |
| 36 | const time::microseconds NccStrategy::DEFER_RANGE_WITHOUT_BEST_FACE = time::microseconds(75000); |
| 37 | const time::nanoseconds NccStrategy::MEASUREMENTS_LIFETIME = time::seconds(16); |
| 38 | |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 39 | NccStrategy::NccStrategy(Forwarder& forwarder, const Name& name) |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame^] | 40 | : Strategy(forwarder) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 41 | { |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame^] | 42 | ParsedInstanceName parsed = parseInstanceName(name); |
| 43 | if (!parsed.parameters.empty()) { |
| 44 | BOOST_THROW_EXCEPTION(std::invalid_argument("NccStrategy does not accept parameters")); |
| 45 | } |
| 46 | this->setInstanceName(makeInstanceName(name, getStrategyName())); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 49 | const Name& |
| 50 | NccStrategy::getStrategyName() |
| 51 | { |
| 52 | static Name strategyName("/localhost/nfd/strategy/ncc/%FD%01"); |
| 53 | return strategyName; |
| 54 | } |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 55 | |
| 56 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 57 | NccStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest, |
| 58 | const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 59 | { |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 60 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
| 61 | const fib::NextHopList& nexthops = fibEntry.getNextHops(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 62 | if (nexthops.size() == 0) { |
| 63 | this->rejectPendingInterest(pitEntry); |
| 64 | return; |
| 65 | } |
| 66 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 67 | PitEntryInfo* pitEntryInfo = pitEntry->insertStrategyInfo<PitEntryInfo>().first; |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 68 | bool isNewPitEntry = !hasPendingOutRecords(*pitEntry); |
Junxiao Shi | 8bfd56d | 2014-10-08 10:06:00 -0700 | [diff] [blame] | 69 | if (!isNewPitEntry) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 70 | return; |
| 71 | } |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 72 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 73 | MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 74 | |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 75 | time::microseconds deferFirst = DEFER_FIRST_WITHOUT_BEST_FACE; |
| 76 | time::microseconds deferRange = DEFER_RANGE_WITHOUT_BEST_FACE; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 77 | size_t nUpstreams = nexthops.size(); |
| 78 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 79 | shared_ptr<Face> bestFace = meInfo.getBestFace(); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 80 | if (bestFace != nullptr && fibEntry.hasNextHop(*bestFace) && |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 81 | !wouldViolateScope(inFace, interest, *bestFace) && |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 82 | canForwardToLegacy(*pitEntry, *bestFace)) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 83 | // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ? |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 84 | deferFirst = meInfo.prediction; |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 85 | deferRange = time::microseconds((deferFirst.count() + 1) / 2); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 86 | --nUpstreams; |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 87 | this->sendInterest(pitEntry, *bestFace, interest); |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 88 | pitEntryInfo->bestFaceTimeout = scheduler::schedule( |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 89 | meInfo.prediction, |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 90 | bind(&NccStrategy::timeoutOnBestFace, this, weak_ptr<pit::Entry>(pitEntry))); |
| 91 | } |
| 92 | else { |
Junxiao Shi | 6316220 | 2015-01-14 22:27:33 -0700 | [diff] [blame] | 93 | // use first eligible nexthop |
| 94 | auto firstEligibleNexthop = std::find_if(nexthops.begin(), nexthops.end(), |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 95 | [&] (const fib::NextHop& nexthop) { |
| 96 | Face& outFace = nexthop.getFace(); |
| 97 | return !wouldViolateScope(inFace, interest, outFace) && |
| 98 | canForwardToLegacy(*pitEntry, outFace); |
Junxiao Shi | 6316220 | 2015-01-14 22:27:33 -0700 | [diff] [blame] | 99 | }); |
| 100 | if (firstEligibleNexthop != nexthops.end()) { |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 101 | this->sendInterest(pitEntry, firstEligibleNexthop->getFace(), interest); |
| 102 | } |
| 103 | else { |
| 104 | this->rejectPendingInterest(pitEntry); |
| 105 | return; |
Junxiao Shi | 6316220 | 2015-01-14 22:27:33 -0700 | [diff] [blame] | 106 | } |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 109 | shared_ptr<Face> previousFace = meInfo.previousFace.lock(); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 110 | if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) && |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 111 | !wouldViolateScope(inFace, interest, *previousFace) && |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 112 | canForwardToLegacy(*pitEntry, *previousFace)) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 113 | --nUpstreams; |
| 114 | } |
| 115 | |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 116 | if (nUpstreams > 0) { |
| 117 | pitEntryInfo->maxInterval = std::max(time::microseconds(1), |
| 118 | time::microseconds((2 * deferRange.count() + nUpstreams - 1) / nUpstreams)); |
| 119 | } |
Junxiao Shi | cbb490a | 2014-08-13 12:24:24 -0700 | [diff] [blame] | 120 | else { |
| 121 | // Normally, maxInterval is unused if there aren't any face beyond best and previousBest. |
| 122 | // However, in case FIB entry gains a new nexthop before doPropagate executes (bug 1853), |
| 123 | // this maxInterval would be used to determine when the next doPropagate would happen. |
| 124 | pitEntryInfo->maxInterval = deferFirst; |
| 125 | } |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 126 | pitEntryInfo->propagateTimer = scheduler::schedule(deferFirst, |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 127 | bind(&NccStrategy::doPropagate, this, inFace.getId(), weak_ptr<pit::Entry>(pitEntry))); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 131 | NccStrategy::doPropagate(FaceId inFaceId, weak_ptr<pit::Entry> pitEntryWeak) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 132 | { |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 133 | Face* inFace = this->getFace(inFaceId); |
| 134 | if (inFace == nullptr) { |
| 135 | return; |
| 136 | } |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 137 | shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock(); |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 138 | if (pitEntry == nullptr) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 139 | return; |
| 140 | } |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 141 | pit::InRecordCollection::const_iterator inRecord = pitEntry->getInRecord(*inFace); |
| 142 | if (inRecord == pitEntry->in_end()) { |
| 143 | return; |
| 144 | } |
| 145 | const Interest& interest = inRecord->getInterest(); |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 146 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 147 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 148 | PitEntryInfo* pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>(); |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 149 | // pitEntryInfo is guaranteed to exist here, because doPropagate is triggered |
| 150 | // from a timer set by NccStrategy. |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 151 | BOOST_ASSERT(pitEntryInfo != nullptr); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 152 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 153 | MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 154 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 155 | shared_ptr<Face> previousFace = meInfo.previousFace.lock(); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 156 | if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) && |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 157 | !wouldViolateScope(*inFace, interest, *previousFace) && |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 158 | canForwardToLegacy(*pitEntry, *previousFace)) { |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 159 | this->sendInterest(pitEntry, *previousFace, interest); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 162 | const fib::NextHopList& nexthops = fibEntry.getNextHops(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 163 | bool isForwarded = false; |
| 164 | for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 165 | Face& face = it->getFace(); |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 166 | if (!wouldViolateScope(*inFace, interest, face) && |
| 167 | canForwardToLegacy(*pitEntry, face)) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 168 | isForwarded = true; |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 169 | this->sendInterest(pitEntry, face, interest); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (isForwarded) { |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 175 | std::uniform_int_distribution<time::nanoseconds::rep> dist(0, pitEntryInfo->maxInterval.count() - 1); |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 176 | time::nanoseconds deferNext = time::nanoseconds(dist(getGlobalRng())); |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 177 | pitEntryInfo->propagateTimer = scheduler::schedule(deferNext, |
Junxiao Shi | e21b3f3 | 2016-11-24 14:13:46 +0000 | [diff] [blame] | 178 | bind(&NccStrategy::doPropagate, this, inFaceId, weak_ptr<pit::Entry>(pitEntry))); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | void |
| 183 | NccStrategy::timeoutOnBestFace(weak_ptr<pit::Entry> pitEntryWeak) |
| 184 | { |
| 185 | shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock(); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 186 | if (pitEntry == nullptr) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 189 | measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 190 | |
| 191 | for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 192 | if (measurementsEntry == nullptr) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 193 | // going out of this strategy's namespace |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 194 | break; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 195 | } |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 196 | this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 197 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 198 | MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry); |
| 199 | meInfo.adjustPredictUp(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 200 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 201 | measurementsEntry = this->getMeasurements().getParent(*measurementsEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 206 | NccStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry, |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 207 | const Face& inFace, const Data& data) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 208 | { |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 209 | if (!pitEntry->hasInRecords()) { |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 210 | // PIT entry has already been satisfied (and is now waiting for straggler timer to expire) |
| 211 | // NCC does not collect measurements for non-best face |
| 212 | return; |
| 213 | } |
| 214 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 215 | measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 216 | |
| 217 | for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 218 | if (measurementsEntry == nullptr) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 219 | // going out of this strategy's namespace |
| 220 | return; |
| 221 | } |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 222 | this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 223 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 224 | MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry); |
| 225 | meInfo.updateBestFace(inFace); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 226 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 227 | measurementsEntry = this->getMeasurements().getParent(*measurementsEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 230 | PitEntryInfo* pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>(); |
Hila Ben Abraham | 39a79be | 2016-03-30 22:00:55 -0700 | [diff] [blame] | 231 | if (pitEntryInfo != nullptr) { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 232 | scheduler::cancel(pitEntryInfo->propagateTimer); |
Hila Ben Abraham | 39a79be | 2016-03-30 22:00:55 -0700 | [diff] [blame] | 233 | |
| 234 | // Verify that the best face satisfied the interest before canceling the timeout call |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 235 | MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry); |
| 236 | shared_ptr<Face> bestFace = meInfo.getBestFace(); |
Hila Ben Abraham | 39a79be | 2016-03-30 22:00:55 -0700 | [diff] [blame] | 237 | |
| 238 | if (bestFace.get() == &inFace) |
| 239 | scheduler::cancel(pitEntryInfo->bestFaceTimeout); |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 240 | } |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 243 | NccStrategy::MeasurementsEntryInfo& |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 244 | NccStrategy::getMeasurementsEntryInfo(const shared_ptr<pit::Entry>& entry) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 245 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 246 | measurements::Entry* measurementsEntry = this->getMeasurements().get(*entry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 247 | return this->getMeasurementsEntryInfo(measurementsEntry); |
| 248 | } |
| 249 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 250 | NccStrategy::MeasurementsEntryInfo& |
| 251 | NccStrategy::getMeasurementsEntryInfo(measurements::Entry* entry) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 252 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 253 | BOOST_ASSERT(entry != nullptr); |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 254 | MeasurementsEntryInfo* info = nullptr; |
| 255 | bool isNew = false; |
| 256 | std::tie(info, isNew) = entry->insertStrategyInfo<MeasurementsEntryInfo>(); |
| 257 | if (!isNew) { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 258 | return *info; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 261 | measurements::Entry* parentEntry = this->getMeasurements().getParent(*entry); |
| 262 | if (parentEntry != nullptr) { |
| 263 | MeasurementsEntryInfo& parentInfo = this->getMeasurementsEntryInfo(parentEntry); |
| 264 | info->inheritFrom(parentInfo); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 267 | return *info; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 270 | const time::microseconds NccStrategy::MeasurementsEntryInfo::INITIAL_PREDICTION = time::microseconds(8192); |
| 271 | const time::microseconds NccStrategy::MeasurementsEntryInfo::MIN_PREDICTION = time::microseconds(127); |
| 272 | const time::microseconds NccStrategy::MeasurementsEntryInfo::MAX_PREDICTION = time::milliseconds(160); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 273 | |
| 274 | NccStrategy::MeasurementsEntryInfo::MeasurementsEntryInfo() |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 275 | : prediction(INITIAL_PREDICTION) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 276 | { |
| 277 | } |
| 278 | |
| 279 | void |
| 280 | NccStrategy::MeasurementsEntryInfo::inheritFrom(const MeasurementsEntryInfo& other) |
| 281 | { |
| 282 | this->operator=(other); |
| 283 | } |
| 284 | |
| 285 | shared_ptr<Face> |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 286 | NccStrategy::MeasurementsEntryInfo::getBestFace() |
| 287 | { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 288 | shared_ptr<Face> best = this->bestFace.lock(); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 289 | if (best != nullptr) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 290 | return best; |
| 291 | } |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 292 | this->bestFace = best = this->previousFace.lock(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 293 | return best; |
| 294 | } |
| 295 | |
| 296 | void |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 297 | NccStrategy::MeasurementsEntryInfo::updateBestFace(const Face& face) |
| 298 | { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 299 | if (this->bestFace.expired()) { |
| 300 | this->bestFace = const_cast<Face&>(face).shared_from_this(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 301 | return; |
| 302 | } |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 303 | shared_ptr<Face> bestFace = this->bestFace.lock(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 304 | if (bestFace.get() == &face) { |
| 305 | this->adjustPredictDown(); |
| 306 | } |
| 307 | else { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 308 | this->previousFace = this->bestFace; |
| 309 | this->bestFace = const_cast<Face&>(face).shared_from_this(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | void |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 314 | NccStrategy::MeasurementsEntryInfo::adjustPredictDown() |
| 315 | { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 316 | prediction = std::max(MIN_PREDICTION, |
| 317 | time::microseconds(prediction.count() - (prediction.count() >> ADJUST_PREDICT_DOWN_SHIFT))); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 321 | NccStrategy::MeasurementsEntryInfo::adjustPredictUp() |
| 322 | { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 323 | prediction = std::min(MAX_PREDICTION, |
| 324 | time::microseconds(prediction.count() + (prediction.count() >> ADJUST_PREDICT_UP_SHIFT))); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void |
Davide Pesavento | 5f47aa6 | 2016-10-07 22:09:09 +0200 | [diff] [blame] | 328 | NccStrategy::MeasurementsEntryInfo::ageBestFace() |
| 329 | { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 330 | this->previousFace = this->bestFace; |
| 331 | this->bestFace.reset(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 334 | NccStrategy::PitEntryInfo::~PitEntryInfo() |
| 335 | { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 336 | scheduler::cancel(this->bestFaceTimeout); |
| 337 | scheduler::cancel(this->propagateTimer); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | } // namespace fw |
| 341 | } // namespace nfd |