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