Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -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 | 80ee7cb | 2014-12-14 10:53:05 -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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "access-strategy.hpp" |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 27 | #include "pit-algorithm.hpp" |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 28 | #include "core/logger.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace fw { |
| 32 | |
| 33 | NFD_LOG_INIT("AccessStrategy"); |
| 34 | |
| 35 | const Name AccessStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/access/%FD%01"); |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 36 | NFD_REGISTER_STRATEGY(AccessStrategy); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 37 | |
| 38 | AccessStrategy::AccessStrategy(Forwarder& forwarder, const Name& name) |
| 39 | : Strategy(forwarder, name) |
| 40 | , m_removeFaceInfoConn(this->beforeRemoveFace.connect( |
| 41 | bind(&AccessStrategy::removeFaceInfo, this, _1))) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | AccessStrategy::~AccessStrategy() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | AccessStrategy::afterReceiveInterest(const Face& inFace, |
| 51 | const Interest& interest, |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 52 | shared_ptr<pit::Entry> pitEntry) |
| 53 | { |
Junxiao Shi | a788e25 | 2015-01-28 17:06:25 -0700 | [diff] [blame] | 54 | RetxSuppression::Result suppressResult = m_retxSuppression.decide(inFace, interest, *pitEntry); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 55 | switch (suppressResult) { |
Junxiao Shi | a788e25 | 2015-01-28 17:06:25 -0700 | [diff] [blame] | 56 | case RetxSuppression::NEW: |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 57 | this->afterReceiveNewInterest(inFace, interest, pitEntry); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 58 | break; |
Junxiao Shi | a788e25 | 2015-01-28 17:06:25 -0700 | [diff] [blame] | 59 | case RetxSuppression::FORWARD: |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 60 | this->afterReceiveRetxInterest(inFace, interest, pitEntry); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 61 | break; |
Junxiao Shi | a788e25 | 2015-01-28 17:06:25 -0700 | [diff] [blame] | 62 | case RetxSuppression::SUPPRESS: |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 63 | NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-suppress"); |
| 64 | break; |
| 65 | default: |
| 66 | BOOST_ASSERT(false); |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | AccessStrategy::afterReceiveNewInterest(const Face& inFace, |
| 73 | const Interest& interest, |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 74 | shared_ptr<pit::Entry> pitEntry) |
| 75 | { |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 76 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 77 | Name miName; |
| 78 | shared_ptr<MtInfo> mi; |
| 79 | std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry); |
| 80 | |
| 81 | // has measurements for Interest Name? |
| 82 | if (mi != nullptr) { |
| 83 | NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << |
| 84 | " new-interest mi=" << miName); |
| 85 | |
| 86 | // send to last working nexthop |
| 87 | bool isSentToLastNexthop = this->sendToLastNexthop(inFace, pitEntry, *mi, fibEntry); |
| 88 | |
| 89 | if (isSentToLastNexthop) { |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | else { |
| 94 | NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << |
| 95 | " new-interest no-mi"); |
| 96 | } |
| 97 | |
| 98 | // no measurements, or last working nexthop unavailable |
| 99 | |
Junxiao Shi | 965d3a4 | 2015-06-01 06:55:23 -0700 | [diff] [blame] | 100 | // multicast to all nexthops except incoming face |
| 101 | this->multicast(pitEntry, fibEntry, {inFace.getId()}); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
| 105 | AccessStrategy::afterReceiveRetxInterest(const Face& inFace, |
| 106 | const Interest& interest, |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 107 | shared_ptr<pit::Entry> pitEntry) |
| 108 | { |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 109 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 110 | NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-forward"); |
Junxiao Shi | 965d3a4 | 2015-06-01 06:55:23 -0700 | [diff] [blame] | 111 | this->multicast(pitEntry, fibEntry, {inFace.getId()}); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | bool |
| 115 | AccessStrategy::sendToLastNexthop(const Face& inFace, shared_ptr<pit::Entry> pitEntry, MtInfo& mi, |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 116 | const fib::Entry& fibEntry) |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 117 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 118 | if (mi.lastNexthop == face::INVALID_FACEID) { |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 119 | NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop"); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | if (mi.lastNexthop == inFace.getId()) { |
| 124 | NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream"); |
| 125 | return false; |
| 126 | } |
| 127 | |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 128 | Face* face = this->getFace(mi.lastNexthop); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 129 | if (face == nullptr || !fibEntry.hasNextHop(*face)) { |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 130 | NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone"); |
| 131 | return false; |
| 132 | } |
| 133 | |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 134 | if (violatesScope(*pitEntry, *face)) { |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 135 | NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope"); |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | RttEstimator::Duration rto = mi.rtt.computeRto(); |
| 140 | NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop << |
| 141 | " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count()); |
| 142 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 143 | this->sendInterest(pitEntry, *face); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 144 | |
| 145 | // schedule RTO timeout |
| 146 | shared_ptr<PitInfo> pi = pitEntry->getOrCreateStrategyInfo<PitInfo>(); |
| 147 | pi->rtoTimer = scheduler::schedule(rto, |
| 148 | bind(&AccessStrategy::afterRtoTimeout, this, weak_ptr<pit::Entry>(pitEntry), |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 149 | inFace.getId(), mi.lastNexthop)); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 150 | |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | void |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 155 | AccessStrategy::afterRtoTimeout(weak_ptr<pit::Entry> pitWeak, |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 156 | FaceId inFace, FaceId firstOutFace) |
| 157 | { |
| 158 | shared_ptr<pit::Entry> pitEntry = pitWeak.lock(); |
| 159 | BOOST_ASSERT(pitEntry != nullptr); |
| 160 | // pitEntry can't become nullptr, because RTO timer should be cancelled upon pitEntry destruction |
| 161 | |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 162 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 163 | |
| 164 | NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFace << |
| 165 | " multicast-except " << inFace << ',' << firstOutFace); |
Junxiao Shi | 965d3a4 | 2015-06-01 06:55:23 -0700 | [diff] [blame] | 166 | this->multicast(pitEntry, fibEntry, {inFace, firstOutFace}); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 170 | AccessStrategy::multicast(shared_ptr<pit::Entry> pitEntry, const fib::Entry& fibEntry, |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 171 | std::unordered_set<FaceId> exceptFaces) |
| 172 | { |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 173 | for (const fib::NextHop& nexthop : fibEntry.getNextHops()) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 174 | Face& face = nexthop.getFace(); |
| 175 | if (exceptFaces.count(face.getId()) > 0) { |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 176 | continue; |
| 177 | } |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 178 | NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << face.getId() << |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 179 | " multicast"); |
| 180 | this->sendInterest(pitEntry, face); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | AccessStrategy::beforeSatisfyInterest(shared_ptr<pit::Entry> pitEntry, |
| 186 | const Face& inFace, const Data& data) |
| 187 | { |
| 188 | shared_ptr<PitInfo> pi = pitEntry->getStrategyInfo<PitInfo>(); |
| 189 | if (pi != nullptr) { |
| 190 | pi->rtoTimer.cancel(); |
| 191 | } |
| 192 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 193 | if (!pitEntry->hasInRecords()) { // already satisfied by another upstream |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 194 | NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() << |
| 195 | " not-fastest"); |
| 196 | return; |
| 197 | } |
| 198 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 199 | pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace); |
| 200 | if (outRecord == pitEntry->out_end()) { // no out-record |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 201 | NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() << |
| 202 | " no-out-record"); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | time::steady_clock::Duration rtt = time::steady_clock::now() - outRecord->getLastRenewed(); |
| 207 | NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() << |
| 208 | " rtt=" << time::duration_cast<time::microseconds>(rtt).count()); |
| 209 | this->updateMeasurements(inFace, data, time::duration_cast<RttEstimator::Duration>(rtt)); |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | AccessStrategy::updateMeasurements(const Face& inFace, const Data& data, |
| 214 | const RttEstimator::Duration& rtt) |
| 215 | { |
| 216 | FaceInfo& fi = m_fit[inFace.getId()]; |
| 217 | fi.rtt.addMeasurement(rtt); |
| 218 | |
| 219 | shared_ptr<MtInfo> mi = this->addPrefixMeasurements(data); |
| 220 | if (mi->lastNexthop != inFace.getId()) { |
| 221 | mi->lastNexthop = inFace.getId(); |
| 222 | mi->rtt = fi.rtt; |
| 223 | } |
| 224 | else { |
| 225 | mi->rtt.addMeasurement(rtt); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | AccessStrategy::MtInfo::MtInfo() |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 230 | : lastNexthop(face::INVALID_FACEID) |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 231 | , rtt(1, time::milliseconds(1), 0.1) |
| 232 | { |
| 233 | } |
| 234 | |
| 235 | std::tuple<Name, shared_ptr<AccessStrategy::MtInfo>> |
| 236 | AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry) |
| 237 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 238 | measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 239 | if (me == nullptr) { |
| 240 | return std::forward_as_tuple(Name(), nullptr); |
| 241 | } |
| 242 | |
| 243 | shared_ptr<MtInfo> mi = me->getStrategyInfo<MtInfo>(); |
| 244 | BOOST_ASSERT(mi != nullptr); |
| 245 | // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist; |
| 246 | // this case needs another longest prefix match until mi is found |
| 247 | return std::forward_as_tuple(me->getName(), mi); |
| 248 | } |
| 249 | |
| 250 | shared_ptr<AccessStrategy::MtInfo> |
| 251 | AccessStrategy::addPrefixMeasurements(const Data& data) |
| 252 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 253 | measurements::Entry* me = nullptr; |
| 254 | if (!data.getName().empty()) { |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 255 | me = this->getMeasurements().get(data.getName().getPrefix(-1)); |
| 256 | } |
| 257 | if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty |
| 258 | me = this->getMeasurements().get(data.getName()); |
| 259 | // Data Name must be in this strategy |
| 260 | BOOST_ASSERT(me != nullptr); |
| 261 | } |
| 262 | |
Junxiao Shi | 7b0f4d1 | 2015-05-10 21:40:29 -0700 | [diff] [blame] | 263 | static const time::nanoseconds ME_LIFETIME = time::seconds(8); |
| 264 | this->getMeasurements().extendLifetime(*me, ME_LIFETIME); |
| 265 | |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 266 | return me->getOrCreateStrategyInfo<MtInfo>(); |
| 267 | } |
| 268 | |
| 269 | AccessStrategy::FaceInfo::FaceInfo() |
| 270 | : rtt(1, time::milliseconds(1), 0.1) |
| 271 | { |
| 272 | } |
| 273 | |
| 274 | void |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 275 | AccessStrategy::removeFaceInfo(const Face& face) |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 276 | { |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 277 | m_fit.erase(face.getId()); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | } // namespace fw |
| 281 | } // namespace nfd |