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