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