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 | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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) |
| 40 | : Strategy(forwarder, name) |
Junxiao Shi | 52e8540 | 2015-11-11 06:06:58 -0700 | [diff] [blame] | 41 | , m_retxSuppression(RETX_SUPPRESSION_INITIAL, |
| 42 | RetxSuppressionExponential::DEFAULT_MULTIPLIER, |
| 43 | RETX_SUPPRESSION_MAX) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Junxiao Shi | 037f4ab | 2016-12-13 04:27:06 +0000 | [diff] [blame^] | 47 | const Name& |
| 48 | BestRouteStrategy2::getStrategyName() |
| 49 | { |
| 50 | static Name strategyName("/localhost/nfd/strategy/best-route/%FD%04"); |
| 51 | return strategyName; |
| 52 | } |
| 53 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 54 | /** \brief determines whether a NextHop is eligible |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 55 | * \param inFace incoming face of current Interest |
| 56 | * \param interest incoming Interest |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 57 | * \param nexthop next hop |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 58 | * \param pitEntry PIT entry |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 59 | * \param wantUnused if true, NextHop must not have unexpired out-record |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 60 | * \param now time::steady_clock::now(), ignored if !wantUnused |
| 61 | */ |
| 62 | static inline bool |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 63 | isNextHopEligible(const Face& inFace, const Interest& interest, |
| 64 | const fib::NextHop& nexthop, |
| 65 | const shared_ptr<pit::Entry>& pitEntry, |
| 66 | bool wantUnused = false, |
| 67 | time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min()) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 68 | { |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 69 | const Face& outFace = nexthop.getFace(); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 70 | |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 71 | // do not forward back to the same face |
| 72 | if (&outFace == &inFace) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 73 | return false; |
| 74 | |
| 75 | // forwarding would violate scope |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 76 | if (wouldViolateScope(inFace, interest, outFace)) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 77 | return false; |
| 78 | |
| 79 | if (wantUnused) { |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 80 | // nexthop must not have unexpired out-record |
| 81 | pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(outFace); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 82 | if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) { |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 90 | /** \brief pick an eligible NextHop with earliest out-record |
| 91 | * \note It is assumed that every nexthop has an out-record. |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 92 | */ |
| 93 | static inline fib::NextHopList::const_iterator |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 94 | findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest, |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 95 | const fib::NextHopList& nexthops, |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 96 | const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 97 | { |
| 98 | fib::NextHopList::const_iterator found = nexthops.end(); |
| 99 | time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max(); |
| 100 | for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) { |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 101 | if (!isNextHopEligible(inFace, interest, *it, pitEntry)) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 102 | continue; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 103 | pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(it->getFace()); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 104 | BOOST_ASSERT(outRecord != pitEntry->out_end()); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 105 | if (outRecord->getLastRenewed() < earliestRenewed) { |
| 106 | found = it; |
| 107 | earliestRenewed = outRecord->getLastRenewed(); |
| 108 | } |
| 109 | } |
| 110 | return found; |
| 111 | } |
| 112 | |
| 113 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 114 | BestRouteStrategy2::afterReceiveInterest(const Face& inFace, const Interest& interest, |
| 115 | const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 116 | { |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 117 | RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry); |
| 118 | if (suppression == RetxSuppression::SUPPRESS) { |
| 119 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() |
| 120 | << " suppressed"); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
| 125 | const fib::NextHopList& nexthops = fibEntry.getNextHops(); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 126 | fib::NextHopList::const_iterator it = nexthops.end(); |
| 127 | |
Junxiao Shi | a788e25 | 2015-01-28 17:06:25 -0700 | [diff] [blame] | 128 | if (suppression == RetxSuppression::NEW) { |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 129 | // forward to nexthop with lowest cost except downstream |
| 130 | it = std::find_if(nexthops.begin(), nexthops.end(), |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 131 | bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry, |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 132 | false, time::steady_clock::TimePoint::min())); |
| 133 | |
| 134 | if (it == nexthops.end()) { |
| 135 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop"); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 136 | |
| 137 | lp::NackHeader nackHeader; |
| 138 | nackHeader.setReason(lp::NackReason::NO_ROUTE); |
| 139 | this->sendNack(pitEntry, inFace, nackHeader); |
| 140 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 141 | this->rejectPendingInterest(pitEntry); |
| 142 | return; |
| 143 | } |
| 144 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 145 | Face& outFace = it->getFace(); |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 146 | this->sendInterest(pitEntry, outFace, interest); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 147 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 148 | << " newPitEntry-to=" << outFace.getId()); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 152 | // find an unused upstream with lowest cost except downstream |
| 153 | it = std::find_if(nexthops.begin(), nexthops.end(), |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 154 | bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry, |
Junxiao Shi | 192af1f | 2015-01-13 23:19:39 -0700 | [diff] [blame] | 155 | true, time::steady_clock::now())); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 156 | if (it != nexthops.end()) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 157 | Face& outFace = it->getFace(); |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 158 | this->sendInterest(pitEntry, outFace, interest); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 159 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 160 | << " retransmit-unused-to=" << outFace.getId()); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | |
| 164 | // find an eligible upstream that is used earliest |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 165 | it = findEligibleNextHopWithEarliestOutRecord(inFace, interest, nexthops, pitEntry); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 166 | if (it == nexthops.end()) { |
| 167 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop"); |
| 168 | } |
| 169 | else { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 170 | Face& outFace = it->getFace(); |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 171 | this->sendInterest(pitEntry, outFace, interest); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 172 | NFD_LOG_DEBUG(interest << " from=" << inFace.getId() |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 173 | << " retransmit-retry-to=" << outFace.getId()); |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 177 | /** \return less severe NackReason between x and y |
| 178 | * |
| 179 | * lp::NackReason::NONE is treated as most severe |
| 180 | */ |
| 181 | inline lp::NackReason |
| 182 | compareLessSevere(lp::NackReason x, lp::NackReason y) |
| 183 | { |
| 184 | if (x == lp::NackReason::NONE) { |
| 185 | return y; |
| 186 | } |
| 187 | if (y == lp::NackReason::NONE) { |
| 188 | return x; |
| 189 | } |
| 190 | return static_cast<lp::NackReason>(std::min(static_cast<int>(x), static_cast<int>(y))); |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack, |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 195 | const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 196 | { |
| 197 | int nOutRecordsNotNacked = 0; |
| 198 | Face* lastFaceNotNacked = nullptr; |
| 199 | lp::NackReason leastSevereReason = lp::NackReason::NONE; |
| 200 | for (const pit::OutRecord& outR : pitEntry->getOutRecords()) { |
| 201 | const lp::NackHeader* inNack = outR.getIncomingNack(); |
| 202 | if (inNack == nullptr) { |
| 203 | ++nOutRecordsNotNacked; |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 204 | lastFaceNotNacked = &outR.getFace(); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 205 | continue; |
| 206 | } |
| 207 | |
| 208 | leastSevereReason = compareLessSevere(leastSevereReason, inNack->getReason()); |
| 209 | } |
| 210 | |
| 211 | lp::NackHeader outNack; |
| 212 | outNack.setReason(leastSevereReason); |
| 213 | |
| 214 | if (nOutRecordsNotNacked == 1) { |
| 215 | BOOST_ASSERT(lastFaceNotNacked != nullptr); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 216 | pit::InRecordCollection::iterator inR = pitEntry->getInRecord(*lastFaceNotNacked); |
| 217 | if (inR != pitEntry->in_end()) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 218 | // one out-record not Nacked, which is also a downstream |
| 219 | NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() << |
| 220 | " nack=" << nack.getReason() << |
| 221 | " nack-to(bidirectional)=" << lastFaceNotNacked->getId() << |
| 222 | " out-nack=" << outNack.getReason()); |
| 223 | this->sendNack(pitEntry, *lastFaceNotNacked, outNack); |
| 224 | return; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if (nOutRecordsNotNacked > 0) { |
| 229 | NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() << |
| 230 | " nack=" << nack.getReason() << |
| 231 | " waiting=" << nOutRecordsNotNacked); |
| 232 | // continue waiting |
| 233 | return; |
| 234 | } |
| 235 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 236 | NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() << |
| 237 | " nack=" << nack.getReason() << |
| 238 | " nack-to=all out-nack=" << outNack.getReason()); |
| 239 | this->sendNacks(pitEntry, outNack); |
| 240 | } |
| 241 | |
Junxiao Shi | 986b849 | 2014-08-20 12:07:14 -0700 | [diff] [blame] | 242 | } // namespace fw |
| 243 | } // namespace nfd |