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