Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 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 "asf-strategy.hpp" |
Ashlesh Gawande | 2a73f35 | 2016-12-01 15:37:03 +0000 | [diff] [blame] | 27 | #include "algorithm.hpp" |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 28 | |
| 29 | #include "core/logger.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace fw { |
| 33 | namespace asf { |
| 34 | |
| 35 | NFD_LOG_INIT("AsfStrategy"); |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 36 | NFD_REGISTER_STRATEGY(AsfStrategy); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 37 | |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 38 | const time::milliseconds AsfStrategy::RETX_SUPPRESSION_INITIAL(10); |
| 39 | const time::milliseconds AsfStrategy::RETX_SUPPRESSION_MAX(250); |
| 40 | |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 41 | AsfStrategy::AsfStrategy(Forwarder& forwarder, const Name& name) |
| 42 | : Strategy(forwarder, name) |
| 43 | , m_measurements(getMeasurements()) |
| 44 | , m_probing(m_measurements) |
| 45 | , m_retxSuppression(RETX_SUPPRESSION_INITIAL, |
| 46 | RetxSuppressionExponential::DEFAULT_MULTIPLIER, |
| 47 | RETX_SUPPRESSION_MAX) |
| 48 | { |
| 49 | } |
| 50 | |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 51 | const Name& |
| 52 | AsfStrategy::getStrategyName() |
| 53 | { |
| 54 | static Name strategyName("/localhost/nfd/strategy/asf/%FD%01"); |
| 55 | return strategyName; |
| 56 | } |
| 57 | |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 58 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 59 | AsfStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest, |
| 60 | const shared_ptr<pit::Entry>& pitEntry) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 61 | { |
| 62 | // Should the Interest be suppressed? |
| 63 | RetxSuppression::Result suppressResult = m_retxSuppression.decide(inFace, interest, *pitEntry); |
| 64 | |
| 65 | switch (suppressResult) { |
| 66 | case RetxSuppression::NEW: |
| 67 | case RetxSuppression::FORWARD: |
| 68 | break; |
| 69 | case RetxSuppression::SUPPRESS: |
| 70 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " suppressed"); |
| 71 | return; |
| 72 | } |
| 73 | |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 74 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
| 75 | const fib::NextHopList& nexthops = fibEntry.getNextHops(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 76 | |
| 77 | if (nexthops.size() == 0) { |
| 78 | sendNoRouteNack(inFace, interest, pitEntry); |
| 79 | this->rejectPendingInterest(pitEntry); |
| 80 | return; |
| 81 | } |
| 82 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 83 | Face* faceToUse = getBestFaceForForwarding(fibEntry, interest, inFace); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 84 | |
| 85 | if (faceToUse == nullptr) { |
| 86 | sendNoRouteNack(inFace, interest, pitEntry); |
| 87 | this->rejectPendingInterest(pitEntry); |
| 88 | return; |
| 89 | } |
| 90 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 91 | forwardInterest(interest, fibEntry, pitEntry, *faceToUse); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 92 | |
| 93 | // If necessary, send probe |
| 94 | if (m_probing.isProbingNeeded(fibEntry, interest)) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 95 | Face* faceToProbe = m_probing.getFaceToProbe(inFace, interest, fibEntry, *faceToUse); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 96 | |
| 97 | if (faceToProbe != nullptr) { |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 98 | bool wantNewNonce = true; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 99 | forwardInterest(interest, fibEntry, pitEntry, *faceToProbe, wantNewNonce); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 100 | m_probing.afterForwardingProbe(fibEntry, interest); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 106 | AsfStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry, |
| 107 | const Face& inFace, const Data& data) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 108 | { |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 109 | NamespaceInfo* namespaceInfo = m_measurements.getNamespaceInfo(pitEntry->getName()); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 110 | |
| 111 | if (namespaceInfo == nullptr) { |
| 112 | NFD_LOG_TRACE("Could not find measurements entry for " << pitEntry->getName()); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | // Record the RTT between the Interest out to Data in |
| 117 | FaceInfo& faceInfo = namespaceInfo->get(inFace.getId()); |
| 118 | faceInfo.recordRtt(pitEntry, inFace); |
| 119 | |
| 120 | // Extend lifetime for measurements associated with Face |
| 121 | namespaceInfo->extendFaceInfoLifetime(faceInfo, inFace); |
| 122 | |
| 123 | if (faceInfo.isTimeoutScheduled()) { |
| 124 | faceInfo.cancelTimeoutEvent(data.getName()); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | AsfStrategy::afterReceiveNack(const Face& inFace, const lp::Nack& nack, |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 130 | const shared_ptr<pit::Entry>& pitEntry) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 131 | { |
| 132 | NFD_LOG_DEBUG("Nack for " << nack.getInterest() << " from=" << inFace.getId() << ": " << nack.getReason()); |
| 133 | onTimeout(pitEntry->getName(), inFace.getId()); |
| 134 | } |
| 135 | |
| 136 | //////////////////////////////////////////////////////////////////////////////// |
| 137 | //////////////////////////////////////////////////////////////////////////////// |
| 138 | |
| 139 | void |
| 140 | AsfStrategy::forwardInterest(const Interest& interest, |
| 141 | const fib::Entry& fibEntry, |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 142 | const shared_ptr<pit::Entry>& pitEntry, |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 143 | Face& outFace, |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 144 | bool wantNewNonce) |
| 145 | { |
Ashlesh Gawande | 2a73f35 | 2016-12-01 15:37:03 +0000 | [diff] [blame] | 146 | if (wantNewNonce) { |
| 147 | //Send probe: interest with new Nonce |
| 148 | Interest probeInterest(interest); |
| 149 | probeInterest.refreshNonce(); |
| 150 | NFD_LOG_TRACE("Sending probe for " << probeInterest << probeInterest.getNonce() |
| 151 | << " to FaceId: " << outFace.getId()); |
| 152 | this->sendInterest(pitEntry, outFace, probeInterest); |
| 153 | } |
| 154 | else { |
| 155 | this->sendInterest(pitEntry, outFace, interest); |
| 156 | } |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 157 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 158 | FaceInfo& faceInfo = m_measurements.getOrCreateFaceInfo(fibEntry, interest, outFace); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 159 | |
| 160 | // Refresh measurements since Face is being used for forwarding |
| 161 | NamespaceInfo& namespaceInfo = m_measurements.getOrCreateNamespaceInfo(fibEntry, interest); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 162 | namespaceInfo.extendFaceInfoLifetime(faceInfo, outFace); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 163 | |
| 164 | if (!faceInfo.isTimeoutScheduled()) { |
| 165 | // Estimate and schedule timeout |
| 166 | RttEstimator::Duration timeout = faceInfo.computeRto(); |
| 167 | |
| 168 | NFD_LOG_TRACE("Scheduling timeout for " << fibEntry.getPrefix() |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 169 | << " FaceId: " << outFace.getId() |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 170 | << " in " << time::duration_cast<time::milliseconds>(timeout) << " ms"); |
| 171 | |
| 172 | scheduler::EventId id = scheduler::schedule(timeout, |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 173 | bind(&AsfStrategy::onTimeout, this, interest.getName(), outFace.getId())); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 174 | |
| 175 | faceInfo.setTimeoutEvent(id, interest.getName()); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | struct FaceStats |
| 180 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 181 | Face* face; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 182 | RttStats::Rtt rtt; |
| 183 | RttStats::Rtt srtt; |
| 184 | uint64_t cost; |
| 185 | }; |
| 186 | |
| 187 | double |
| 188 | getValueForSorting(const FaceStats& stats) |
| 189 | { |
| 190 | // These values allow faces with no measurements to be ranked better than timeouts |
| 191 | // srtt < RTT_NO_MEASUREMENT < RTT_TIMEOUT |
| 192 | static const RttStats::Rtt SORTING_RTT_TIMEOUT = time::microseconds::max(); |
| 193 | static const RttStats::Rtt SORTING_RTT_NO_MEASUREMENT = SORTING_RTT_TIMEOUT / 2; |
| 194 | |
| 195 | if (stats.rtt == RttStats::RTT_TIMEOUT) { |
| 196 | return SORTING_RTT_TIMEOUT.count(); |
| 197 | } |
| 198 | else if (stats.rtt == RttStats::RTT_NO_MEASUREMENT) { |
| 199 | return SORTING_RTT_NO_MEASUREMENT.count(); |
| 200 | } |
| 201 | else { |
| 202 | return stats.srtt.count(); |
| 203 | } |
| 204 | } |
| 205 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 206 | Face* |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 207 | AsfStrategy::getBestFaceForForwarding(const fib::Entry& fibEntry, const Interest& interest, |
| 208 | const Face& inFace) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 209 | { |
| 210 | NFD_LOG_TRACE("Looking for best face for " << fibEntry.getPrefix()); |
| 211 | |
| 212 | typedef std::function<bool(const FaceStats&, const FaceStats&)> FaceStatsPredicate; |
| 213 | typedef std::set<FaceStats, FaceStatsPredicate> FaceStatsSet; |
| 214 | |
| 215 | FaceStatsSet rankedFaces( |
| 216 | [] (const FaceStats& lhs, const FaceStats& rhs) -> bool { |
| 217 | // Sort by RTT and then by cost |
| 218 | double lhsValue = getValueForSorting(lhs); |
| 219 | double rhsValue = getValueForSorting(rhs); |
| 220 | |
| 221 | if (lhsValue < rhsValue) { |
| 222 | return true; |
| 223 | } |
| 224 | else if (lhsValue == rhsValue) { |
| 225 | return lhs.cost < rhs.cost; |
| 226 | } |
| 227 | else { |
| 228 | return false; |
| 229 | } |
| 230 | }); |
| 231 | |
| 232 | for (const fib::NextHop& hop : fibEntry.getNextHops()) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 233 | Face& hopFace = hop.getFace(); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 234 | |
Ashlesh Gawande | 2a73f35 | 2016-12-01 15:37:03 +0000 | [diff] [blame] | 235 | if (hopFace.getId() == inFace.getId() || wouldViolateScope(inFace, interest, hopFace)) { |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 236 | continue; |
| 237 | } |
| 238 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 239 | FaceInfo* info = m_measurements.getFaceInfo(fibEntry, interest, hopFace); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 240 | |
| 241 | if (info == nullptr) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 242 | FaceStats stats = {&hopFace, |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 243 | RttStats::RTT_NO_MEASUREMENT, |
| 244 | RttStats::RTT_NO_MEASUREMENT, |
| 245 | hop.getCost()}; |
| 246 | |
| 247 | rankedFaces.insert(stats); |
| 248 | } |
| 249 | else { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 250 | FaceStats stats = {&hopFace, info->getRtt(), info->getSrtt(), hop.getCost()}; |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 251 | rankedFaces.insert(stats); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | FaceStatsSet::iterator it = rankedFaces.begin(); |
| 256 | |
| 257 | if (it != rankedFaces.end()) { |
| 258 | return it->face; |
| 259 | } |
| 260 | else { |
| 261 | return nullptr; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 266 | AsfStrategy::onTimeout(const Name& interestName, face::FaceId faceId) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 267 | { |
| 268 | NFD_LOG_TRACE("FaceId: " << faceId << " for " << interestName << " has timed-out"); |
| 269 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 270 | NamespaceInfo* namespaceInfo = m_measurements.getNamespaceInfo(interestName); |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 271 | |
| 272 | if (namespaceInfo == nullptr) { |
| 273 | NFD_LOG_TRACE("FibEntry for " << interestName << " has been removed since timeout scheduling"); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | FaceInfoTable::iterator it = namespaceInfo->find(faceId); |
| 278 | |
| 279 | if (it == namespaceInfo->end()) { |
| 280 | it = namespaceInfo->insert(faceId); |
| 281 | } |
| 282 | |
| 283 | FaceInfo& faceInfo = it->second; |
| 284 | faceInfo.recordTimeout(interestName); |
| 285 | } |
| 286 | |
| 287 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 288 | AsfStrategy::sendNoRouteNack(const Face& inFace, const Interest& interest, |
| 289 | const shared_ptr<pit::Entry>& pitEntry) |
Vince Lehman | 8a4c29e | 2016-07-11 08:49:35 +0000 | [diff] [blame] | 290 | { |
| 291 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop"); |
| 292 | |
| 293 | lp::NackHeader nackHeader; |
| 294 | nackHeader.setReason(lp::NackReason::NO_ROUTE); |
| 295 | this->sendNack(pitEntry, inFace, nackHeader); |
| 296 | } |
| 297 | |
| 298 | } // namespace asf |
| 299 | } // namespace fw |
| 300 | } // namespace nfd |