Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Junxiao Shi | 192af1f | 2015-01-13 23:19:39 -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. |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 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 "best-route-strategy2.hpp" |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 27 | #include "algorithm.hpp" |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 28 | #include "core/logger.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace fw { |
| 32 | |
| 33 | NFD_LOG_INIT("BestRouteStrategy2"); |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 34 | NFD_REGISTER_STRATEGY(BestRouteStrategy2); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 35 | |
Junxiao Shi | 52e8540 | 2015-11-11 06:06:58 -0700 | [diff] [blame] | 36 | const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_INITIAL(10); |
| 37 | const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_MAX(250); |
| 38 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 39 | BestRouteStrategy2::BestRouteStrategy2(Forwarder& forwarder, const Name& name) |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 40 | : Strategy(forwarder) |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 41 | , ProcessNackTraits(this) |
Junxiao Shi | 52e8540 | 2015-11-11 06:06:58 -0700 | [diff] [blame] | 42 | , m_retxSuppression(RETX_SUPPRESSION_INITIAL, |
| 43 | RetxSuppressionExponential::DEFAULT_MULTIPLIER, |
| 44 | RETX_SUPPRESSION_MAX) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 45 | { |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 46 | ParsedInstanceName parsed = parseInstanceName(name); |
| 47 | if (!parsed.parameters.empty()) { |
| 48 | BOOST_THROW_EXCEPTION(std::invalid_argument("BestRouteStrategy2 does not accept parameters")); |
| 49 | } |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 50 | if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) { |
| 51 | BOOST_THROW_EXCEPTION(std::invalid_argument( |
| 52 | "BestRouteStrategy2 does not support version " + std::to_string(*parsed.version))); |
| 53 | } |
Junxiao Shi | 18739c4 | 2016-12-22 08:03:00 +0000 | [diff] [blame] | 54 | this->setInstanceName(makeInstanceName(name, getStrategyName())); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 57 | const Name& |
| 58 | BestRouteStrategy2::getStrategyName() |
| 59 | { |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 60 | static Name strategyName("/localhost/nfd/strategy/best-route/%FD%05"); |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame] | 61 | return strategyName; |
| 62 | } |
| 63 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 64 | /** \brief determines whether a NextHop is eligible |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 65 | * \param inFace incoming face of current Interest |
| 66 | * \param interest incoming Interest |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 67 | * \param nexthop next hop |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 68 | * \param pitEntry PIT entry |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 69 | * \param wantUnused if true, NextHop must not have unexpired out-record |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 70 | * \param now time::steady_clock::now(), ignored if !wantUnused |
| 71 | */ |
| 72 | static inline bool |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 73 | isNextHopEligible(const Face& inFace, const Interest& interest, |
| 74 | const fib::NextHop& nexthop, |
| 75 | const shared_ptr<pit::Entry>& pitEntry, |
| 76 | bool wantUnused = false, |
| 77 | time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min()) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 78 | { |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 79 | const Face& outFace = nexthop.getFace(); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 80 | |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 81 | // do not forward back to the same face, unless it is ad hoc |
| 82 | if (outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 83 | return false; |
| 84 | |
| 85 | // forwarding would violate scope |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 86 | if (wouldViolateScope(inFace, interest, outFace)) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 87 | return false; |
| 88 | |
| 89 | if (wantUnused) { |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 90 | // nexthop must not have unexpired out-record |
| 91 | pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(outFace); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 92 | if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) { |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 100 | /** \brief pick an eligible NextHop with earliest out-record |
| 101 | * \note It is assumed that every nexthop has an out-record. |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 102 | */ |
| 103 | static inline fib::NextHopList::const_iterator |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 104 | findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest, |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 105 | const fib::NextHopList& nexthops, |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 106 | const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 107 | { |
| 108 | fib::NextHopList::const_iterator found = nexthops.end(); |
| 109 | time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max(); |
| 110 | for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) { |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 111 | if (!isNextHopEligible(inFace, interest, *it, pitEntry)) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 112 | continue; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 113 | pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(it->getFace()); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 114 | BOOST_ASSERT(outRecord != pitEntry->out_end()); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 115 | if (outRecord->getLastRenewed() < earliestRenewed) { |
| 116 | found = it; |
| 117 | earliestRenewed = outRecord->getLastRenewed(); |
| 118 | } |
| 119 | } |
| 120 | return found; |
| 121 | } |
| 122 | |
| 123 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 124 | BestRouteStrategy2::afterReceiveInterest(const Face& inFace, const Interest& interest, |
| 125 | const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 126 | { |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 127 | RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry); |
| 128 | if (suppression == RetxSuppression::SUPPRESS) { |
| 129 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() |
| 130 | << " suppressed"); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
| 135 | const fib::NextHopList& nexthops = fibEntry.getNextHops(); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 136 | fib::NextHopList::const_iterator it = nexthops.end(); |
| 137 | |
Junxiao Shi | a788e25 | 2015-01-28 17:06:25 -0700 | [diff] [blame] | 138 | if (suppression == RetxSuppression::NEW) { |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 139 | // forward to nexthop with lowest cost except downstream |
| 140 | it = std::find_if(nexthops.begin(), nexthops.end(), |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 141 | bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry, |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 142 | false, time::steady_clock::TimePoint::min())); |
| 143 | |
| 144 | if (it == nexthops.end()) { |
| 145 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop"); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 146 | |
| 147 | lp::NackHeader nackHeader; |
| 148 | nackHeader.setReason(lp::NackReason::NO_ROUTE); |
| 149 | this->sendNack(pitEntry, inFace, nackHeader); |
| 150 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 151 | this->rejectPendingInterest(pitEntry); |
| 152 | return; |
| 153 | } |
| 154 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 155 | Face& outFace = it->getFace(); |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 156 | this->sendInterest(pitEntry, outFace, interest); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 157 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 158 | << " newPitEntry-to=" << outFace.getId()); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 162 | // find an unused upstream with lowest cost except downstream |
| 163 | it = std::find_if(nexthops.begin(), nexthops.end(), |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 164 | bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry, |
Junxiao Shi | 192af1f | 2015-01-13 23:19:39 -0700 | [diff] [blame] | 165 | true, time::steady_clock::now())); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 166 | if (it != nexthops.end()) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 167 | Face& outFace = it->getFace(); |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 168 | this->sendInterest(pitEntry, outFace, interest); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 169 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 170 | << " retransmit-unused-to=" << outFace.getId()); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
| 174 | // find an eligible upstream that is used earliest |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 175 | it = findEligibleNextHopWithEarliestOutRecord(inFace, interest, nexthops, pitEntry); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 176 | if (it == nexthops.end()) { |
| 177 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop"); |
| 178 | } |
| 179 | else { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 180 | Face& outFace = it->getFace(); |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 181 | this->sendInterest(pitEntry, outFace, interest); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 182 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 183 | << " retransmit-retry-to=" << outFace.getId()); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 187 | void |
| 188 | BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack, |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 189 | const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 190 | { |
Junxiao Shi | 9954007 | 2017-01-27 19:57:33 +0000 | [diff] [blame] | 191 | this->processNack(inFace, nack, pitEntry); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 194 | } // namespace fw |
| 195 | } // namespace nfd |