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