Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "ncc-strategy.hpp" |
| 8 | |
| 9 | namespace nfd { |
| 10 | namespace fw { |
| 11 | |
| 12 | NccStrategy::NccStrategy(Forwarder& forwarder) |
| 13 | : Strategy(forwarder) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | NccStrategy::~NccStrategy() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | const time::Duration NccStrategy::DEFER_FIRST_WITHOUT_BEST_FACE = time::microseconds(4000); |
| 22 | const time::Duration NccStrategy::DEFER_RANGE_WITHOUT_BEST_FACE = time::microseconds(75000); |
| 23 | const time::Duration NccStrategy::MEASUREMENTS_LIFETIME = time::seconds(16); |
| 24 | |
| 25 | void |
| 26 | NccStrategy::afterReceiveInterest(const Face& inFace, |
| 27 | const Interest& interest, |
| 28 | shared_ptr<fib::Entry> fibEntry, |
| 29 | shared_ptr<pit::Entry> pitEntry) |
| 30 | { |
| 31 | const fib::NextHopList& nexthops = fibEntry->getNextHops(); |
| 32 | if (nexthops.size() == 0) { |
| 33 | this->rejectPendingInterest(pitEntry); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | shared_ptr<PitEntryInfo> pitEntryInfo = |
| 38 | pitEntry->getOrCreateStrategyInfo<PitEntryInfo>(); |
| 39 | bool isNewInterest = pitEntryInfo->m_isNewInterest; |
| 40 | if (!isNewInterest) { |
| 41 | return; |
| 42 | } |
| 43 | pitEntryInfo->m_isNewInterest = false; |
| 44 | |
| 45 | shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo = |
| 46 | this->getMeasurementsEntryInfo(pitEntry); |
| 47 | |
| 48 | time::Duration deferFirst = DEFER_FIRST_WITHOUT_BEST_FACE; |
| 49 | time::Duration deferRange = DEFER_RANGE_WITHOUT_BEST_FACE; |
| 50 | size_t nUpstreams = nexthops.size(); |
| 51 | |
| 52 | shared_ptr<Face> bestFace = measurementsEntryInfo->getBestFace(); |
| 53 | if (static_cast<bool>(bestFace) && fibEntry->hasNextHop(bestFace) && |
| 54 | pitEntry->canForwardTo(bestFace)) { |
| 55 | // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ? |
| 56 | deferFirst = measurementsEntryInfo->m_prediction; |
| 57 | deferRange = static_cast<time::Duration>((deferFirst + time::microseconds(1)) / 2); |
| 58 | --nUpstreams; |
| 59 | this->sendInterest(pitEntry, bestFace); |
| 60 | pitEntryInfo->m_bestFaceTimeout = scheduler::schedule( |
| 61 | measurementsEntryInfo->m_prediction, |
| 62 | bind(&NccStrategy::timeoutOnBestFace, this, weak_ptr<pit::Entry>(pitEntry))); |
| 63 | } |
| 64 | else { |
| 65 | // use first nexthop |
| 66 | this->sendInterest(pitEntry, nexthops.begin()->getFace()); |
| 67 | // TODO avoid sending to inFace |
| 68 | } |
| 69 | |
| 70 | shared_ptr<Face> previousFace = measurementsEntryInfo->m_previousFace.lock(); |
| 71 | if (static_cast<bool>(previousFace) && fibEntry->hasNextHop(previousFace) && |
| 72 | pitEntry->canForwardTo(previousFace)) { |
| 73 | --nUpstreams; |
| 74 | } |
| 75 | |
| 76 | pitEntryInfo->m_maxInterval = std::max(time::microseconds(1), |
| 77 | static_cast<time::Duration>((2 * deferRange + nUpstreams - 1) / nUpstreams)); |
| 78 | pitEntryInfo->m_propagateTimer = scheduler::schedule(deferFirst, |
| 79 | bind(&NccStrategy::doPropagate, this, |
| 80 | weak_ptr<pit::Entry>(pitEntry), weak_ptr<fib::Entry>(fibEntry))); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | NccStrategy::doPropagate(weak_ptr<pit::Entry> pitEntryWeak, weak_ptr<fib::Entry> fibEntryWeak) |
| 85 | { |
| 86 | shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock(); |
| 87 | if (!static_cast<bool>(pitEntry)) { |
| 88 | return; |
| 89 | } |
| 90 | shared_ptr<fib::Entry> fibEntry = fibEntryWeak.lock(); |
| 91 | if (!static_cast<bool>(fibEntry)) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | shared_ptr<PitEntryInfo> pitEntryInfo = |
| 96 | pitEntry->getStrategyInfo<PitEntryInfo>(); |
| 97 | BOOST_ASSERT(static_cast<bool>(pitEntryInfo)); |
| 98 | |
| 99 | shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo = |
| 100 | this->getMeasurementsEntryInfo(pitEntry); |
| 101 | |
| 102 | shared_ptr<Face> previousFace = measurementsEntryInfo->m_previousFace.lock(); |
| 103 | if (static_cast<bool>(previousFace) && fibEntry->hasNextHop(previousFace) && |
| 104 | pitEntry->canForwardTo(previousFace)) { |
| 105 | this->sendInterest(pitEntry, previousFace); |
| 106 | } |
| 107 | |
| 108 | const fib::NextHopList& nexthops = fibEntry->getNextHops(); |
| 109 | bool isForwarded = false; |
| 110 | for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) { |
| 111 | shared_ptr<Face> face = it->getFace(); |
| 112 | if (pitEntry->canForwardTo(face)) { |
| 113 | isForwarded = true; |
| 114 | this->sendInterest(pitEntry, face); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (isForwarded) { |
| 120 | static unsigned short seed[3]; |
| 121 | time::Duration deferNext = static_cast<time::Duration>( |
| 122 | nrand48(seed) % pitEntryInfo->m_maxInterval); |
| 123 | pitEntryInfo->m_propagateTimer = scheduler::schedule(deferNext, |
| 124 | bind(&NccStrategy::doPropagate, this, |
| 125 | weak_ptr<pit::Entry>(pitEntry), weak_ptr<fib::Entry>(fibEntry))); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | NccStrategy::timeoutOnBestFace(weak_ptr<pit::Entry> pitEntryWeak) |
| 131 | { |
| 132 | shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock(); |
| 133 | if (!static_cast<bool>(pitEntry)) { |
| 134 | return; |
| 135 | } |
| 136 | shared_ptr<measurements::Entry> measurementsEntry = this->getMeasurements().get(*pitEntry); |
| 137 | |
| 138 | for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) { |
| 139 | if (!static_cast<bool>(measurementsEntry)) { |
| 140 | // going out of this strategy's namespace |
| 141 | return; |
| 142 | } |
| 143 | this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME); |
| 144 | |
| 145 | shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo = |
| 146 | this->getMeasurementsEntryInfo(measurementsEntry); |
| 147 | measurementsEntryInfo->adjustPredictUp(); |
| 148 | |
| 149 | measurementsEntry = this->getMeasurements().getParent(measurementsEntry); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | NccStrategy::beforeSatisfyPendingInterest(shared_ptr<pit::Entry> pitEntry, |
| 155 | const Face& inFace, const Data& data) |
| 156 | { |
| 157 | shared_ptr<measurements::Entry> measurementsEntry = this->getMeasurements().get(*pitEntry); |
| 158 | |
| 159 | for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) { |
| 160 | if (!static_cast<bool>(measurementsEntry)) { |
| 161 | // going out of this strategy's namespace |
| 162 | return; |
| 163 | } |
| 164 | this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME); |
| 165 | |
| 166 | shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo = |
| 167 | this->getMeasurementsEntryInfo(measurementsEntry); |
| 168 | measurementsEntryInfo->updateBestFace(inFace); |
| 169 | |
| 170 | measurementsEntry = this->getMeasurements().getParent(measurementsEntry); |
| 171 | } |
| 172 | |
| 173 | shared_ptr<PitEntryInfo> pitEntryInfo = |
| 174 | pitEntry->getStrategyInfo<PitEntryInfo>(); |
| 175 | scheduler::cancel(pitEntryInfo->m_propagateTimer); |
| 176 | } |
| 177 | |
| 178 | shared_ptr<NccStrategy::MeasurementsEntryInfo> |
| 179 | NccStrategy::getMeasurementsEntryInfo(shared_ptr<pit::Entry> entry) |
| 180 | { |
| 181 | shared_ptr<measurements::Entry> measurementsEntry = this->getMeasurements().get(*entry); |
| 182 | return this->getMeasurementsEntryInfo(measurementsEntry); |
| 183 | } |
| 184 | |
| 185 | shared_ptr<NccStrategy::MeasurementsEntryInfo> |
| 186 | NccStrategy::getMeasurementsEntryInfo(shared_ptr<measurements::Entry> entry) |
| 187 | { |
| 188 | shared_ptr<MeasurementsEntryInfo> info = entry->getStrategyInfo<MeasurementsEntryInfo>(); |
| 189 | if (static_cast<bool>(info)) { |
| 190 | return info; |
| 191 | } |
| 192 | |
| 193 | info = make_shared<MeasurementsEntryInfo>(); |
| 194 | entry->setStrategyInfo(info); |
| 195 | |
| 196 | shared_ptr<measurements::Entry> parentEntry = this->getMeasurements().getParent(entry); |
| 197 | if (static_cast<bool>(parentEntry)) { |
| 198 | shared_ptr<MeasurementsEntryInfo> parentInfo = this->getMeasurementsEntryInfo(parentEntry); |
| 199 | BOOST_ASSERT(static_cast<bool>(parentInfo)); |
| 200 | info->inheritFrom(*parentInfo); |
| 201 | } |
| 202 | |
| 203 | return info; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | const time::Duration NccStrategy::MeasurementsEntryInfo::INITIAL_PREDICTION = |
| 208 | time::microseconds(8192); |
| 209 | const time::Duration NccStrategy::MeasurementsEntryInfo::MIN_PREDICTION = |
| 210 | time::microseconds(127); |
| 211 | const time::Duration NccStrategy::MeasurementsEntryInfo::MAX_PREDICTION = |
| 212 | time::microseconds(160000); |
| 213 | |
| 214 | NccStrategy::MeasurementsEntryInfo::MeasurementsEntryInfo() |
| 215 | : m_prediction(INITIAL_PREDICTION) |
| 216 | { |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | NccStrategy::MeasurementsEntryInfo::inheritFrom(const MeasurementsEntryInfo& other) |
| 221 | { |
| 222 | this->operator=(other); |
| 223 | } |
| 224 | |
| 225 | shared_ptr<Face> |
| 226 | NccStrategy::MeasurementsEntryInfo::getBestFace(void) { |
| 227 | shared_ptr<Face> best = m_bestFace.lock(); |
| 228 | if (static_cast<bool>(best)) { |
| 229 | return best; |
| 230 | } |
| 231 | m_bestFace = best = m_previousFace.lock(); |
| 232 | return best; |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | NccStrategy::MeasurementsEntryInfo::updateBestFace(const Face& face) { |
| 237 | if (m_bestFace.expired()) { |
| 238 | m_bestFace = const_cast<Face&>(face).shared_from_this(); |
| 239 | return; |
| 240 | } |
| 241 | shared_ptr<Face> bestFace = m_bestFace.lock(); |
| 242 | if (bestFace.get() == &face) { |
| 243 | this->adjustPredictDown(); |
| 244 | } |
| 245 | else { |
| 246 | m_previousFace = m_bestFace; |
| 247 | m_bestFace = const_cast<Face&>(face).shared_from_this(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | NccStrategy::MeasurementsEntryInfo::adjustPredictDown() { |
| 253 | m_prediction = std::max(MIN_PREDICTION, |
| 254 | static_cast<time::Duration>(m_prediction - (m_prediction >> ADJUST_PREDICT_DOWN_SHIFT))); |
| 255 | } |
| 256 | |
| 257 | void |
| 258 | NccStrategy::MeasurementsEntryInfo::adjustPredictUp() { |
| 259 | m_prediction = std::min(MAX_PREDICTION, |
| 260 | static_cast<time::Duration>(m_prediction + (m_prediction >> ADJUST_PREDICT_UP_SHIFT))); |
| 261 | } |
| 262 | |
| 263 | void |
| 264 | NccStrategy::MeasurementsEntryInfo::ageBestFace() { |
| 265 | m_previousFace = m_bestFace; |
| 266 | m_bestFace.reset(); |
| 267 | } |
| 268 | |
| 269 | NccStrategy::PitEntryInfo::PitEntryInfo() |
| 270 | : m_isNewInterest(true) |
| 271 | { |
| 272 | } |
| 273 | |
| 274 | NccStrategy::PitEntryInfo::~PitEntryInfo() |
| 275 | { |
| 276 | scheduler::cancel(m_bestFaceTimeout); |
| 277 | scheduler::cancel(m_propagateTimer); |
| 278 | } |
| 279 | |
| 280 | } // namespace fw |
| 281 | } // namespace nfd |