blob: 9b53b4bd970603dd13fde342cc6682f7726a74b3 [file] [log] [blame]
Junxiao Shi986b8492014-08-20 12:07:14 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shifef73e42016-03-29 14:15:05 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi192af1f2015-01-13 23:19:39 -07004 * 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 Shi986b8492014-08-20 12:07:14 -070010 *
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 Shi00dc9142016-11-21 14:23:12 +000027#include "algorithm.hpp"
Junxiao Shi986b8492014-08-20 12:07:14 -070028#include "core/logger.hpp"
29
30namespace nfd {
31namespace fw {
32
33NFD_LOG_INIT("BestRouteStrategy2");
Junxiao Shifaf3eb02015-02-16 10:50:36 -070034NFD_REGISTER_STRATEGY(BestRouteStrategy2);
Junxiao Shi986b8492014-08-20 12:07:14 -070035
Junxiao Shi52e85402015-11-11 06:06:58 -070036const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_INITIAL(10);
37const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_MAX(250);
38
Junxiao Shi986b8492014-08-20 12:07:14 -070039BestRouteStrategy2::BestRouteStrategy2(Forwarder& forwarder, const Name& name)
40 : Strategy(forwarder, name)
Junxiao Shi52e85402015-11-11 06:06:58 -070041 , m_retxSuppression(RETX_SUPPRESSION_INITIAL,
42 RetxSuppressionExponential::DEFAULT_MULTIPLIER,
43 RETX_SUPPRESSION_MAX)
Junxiao Shi986b8492014-08-20 12:07:14 -070044{
45}
46
Junxiao Shi037f4ab2016-12-13 04:27:06 +000047const Name&
48BestRouteStrategy2::getStrategyName()
49{
50 static Name strategyName("/localhost/nfd/strategy/best-route/%FD%04");
51 return strategyName;
52}
53
Junxiao Shi986b8492014-08-20 12:07:14 -070054/** \brief determines whether a NextHop is eligible
Junxiao Shi00dc9142016-11-21 14:23:12 +000055 * \param inFace incoming face of current Interest
56 * \param interest incoming Interest
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050057 * \param nexthop next hop
Junxiao Shi00dc9142016-11-21 14:23:12 +000058 * \param pitEntry PIT entry
Junxiao Shi4846f372016-04-05 13:39:30 -070059 * \param wantUnused if true, NextHop must not have unexpired out-record
Junxiao Shi986b8492014-08-20 12:07:14 -070060 * \param now time::steady_clock::now(), ignored if !wantUnused
61 */
62static inline bool
Junxiao Shi00dc9142016-11-21 14:23:12 +000063isNextHopEligible(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 Shi986b8492014-08-20 12:07:14 -070068{
Junxiao Shi00dc9142016-11-21 14:23:12 +000069 const Face& outFace = nexthop.getFace();
Junxiao Shi986b8492014-08-20 12:07:14 -070070
Junxiao Shi00dc9142016-11-21 14:23:12 +000071 // do not forward back to the same face
72 if (&outFace == &inFace)
Junxiao Shi986b8492014-08-20 12:07:14 -070073 return false;
74
75 // forwarding would violate scope
Junxiao Shi00dc9142016-11-21 14:23:12 +000076 if (wouldViolateScope(inFace, interest, outFace))
Junxiao Shi986b8492014-08-20 12:07:14 -070077 return false;
78
79 if (wantUnused) {
Junxiao Shi00dc9142016-11-21 14:23:12 +000080 // nexthop must not have unexpired out-record
81 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(outFace);
Junxiao Shi4846f372016-04-05 13:39:30 -070082 if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
Junxiao Shi986b8492014-08-20 12:07:14 -070083 return false;
84 }
85 }
86
87 return true;
88}
89
Junxiao Shi4846f372016-04-05 13:39:30 -070090/** \brief pick an eligible NextHop with earliest out-record
91 * \note It is assumed that every nexthop has an out-record.
Junxiao Shi986b8492014-08-20 12:07:14 -070092 */
93static inline fib::NextHopList::const_iterator
Junxiao Shi00dc9142016-11-21 14:23:12 +000094findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest,
Junxiao Shi986b8492014-08-20 12:07:14 -070095 const fib::NextHopList& nexthops,
Junxiao Shi00dc9142016-11-21 14:23:12 +000096 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -070097{
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 Shi00dc9142016-11-21 14:23:12 +0000101 if (!isNextHopEligible(inFace, interest, *it, pitEntry))
Junxiao Shi986b8492014-08-20 12:07:14 -0700102 continue;
Junxiao Shia6de4292016-07-12 02:08:10 +0000103 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(it->getFace());
Junxiao Shi4846f372016-04-05 13:39:30 -0700104 BOOST_ASSERT(outRecord != pitEntry->out_end());
Junxiao Shi986b8492014-08-20 12:07:14 -0700105 if (outRecord->getLastRenewed() < earliestRenewed) {
106 found = it;
107 earliestRenewed = outRecord->getLastRenewed();
108 }
109 }
110 return found;
111}
112
113void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000114BestRouteStrategy2::afterReceiveInterest(const Face& inFace, const Interest& interest,
115 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -0700116{
Junxiao Shi8d843142016-07-11 22:42:42 +0000117 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 Shi986b8492014-08-20 12:07:14 -0700126 fib::NextHopList::const_iterator it = nexthops.end();
127
Junxiao Shia788e252015-01-28 17:06:25 -0700128 if (suppression == RetxSuppression::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700129 // forward to nexthop with lowest cost except downstream
130 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi00dc9142016-11-21 14:23:12 +0000131 bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
Junxiao Shi986b8492014-08-20 12:07:14 -0700132 false, time::steady_clock::TimePoint::min()));
133
134 if (it == nexthops.end()) {
135 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700136
137 lp::NackHeader nackHeader;
138 nackHeader.setReason(lp::NackReason::NO_ROUTE);
139 this->sendNack(pitEntry, inFace, nackHeader);
140
Junxiao Shi986b8492014-08-20 12:07:14 -0700141 this->rejectPendingInterest(pitEntry);
142 return;
143 }
144
Junxiao Shia6de4292016-07-12 02:08:10 +0000145 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000146 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700147 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000148 << " newPitEntry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700149 return;
150 }
151
Junxiao Shi986b8492014-08-20 12:07:14 -0700152 // find an unused upstream with lowest cost except downstream
153 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi00dc9142016-11-21 14:23:12 +0000154 bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
Junxiao Shi192af1f2015-01-13 23:19:39 -0700155 true, time::steady_clock::now()));
Junxiao Shi986b8492014-08-20 12:07:14 -0700156 if (it != nexthops.end()) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000157 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000158 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700159 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000160 << " retransmit-unused-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700161 return;
162 }
163
164 // find an eligible upstream that is used earliest
Junxiao Shi00dc9142016-11-21 14:23:12 +0000165 it = findEligibleNextHopWithEarliestOutRecord(inFace, interest, nexthops, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -0700166 if (it == nexthops.end()) {
167 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
168 }
169 else {
Junxiao Shia6de4292016-07-12 02:08:10 +0000170 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000171 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700172 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000173 << " retransmit-retry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700174 }
175}
176
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700177/** \return less severe NackReason between x and y
178 *
179 * lp::NackReason::NONE is treated as most severe
180 */
181inline lp::NackReason
182compareLessSevere(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
193void
194BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000195 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700196{
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 Shi9cff7792016-08-01 21:45:11 +0000204 lastFaceNotNacked = &outR.getFace();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700205 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 Shi4846f372016-04-05 13:39:30 -0700216 pit::InRecordCollection::iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
217 if (inR != pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700218 // 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 Shi5e5e4452015-09-24 16:56:52 -0700236 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 Shi986b8492014-08-20 12:07:14 -0700242} // namespace fw
243} // namespace nfd