blob: 6bb1d428f6edc5596246650aba077b045bdaae34 [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");
34
Junxiao Shi5e5e4452015-09-24 16:56:52 -070035const Name BestRouteStrategy2::STRATEGY_NAME("ndn:/localhost/nfd/strategy/best-route/%FD%04");
Junxiao Shifaf3eb02015-02-16 10:50:36 -070036NFD_REGISTER_STRATEGY(BestRouteStrategy2);
Junxiao Shi986b8492014-08-20 12:07:14 -070037
Junxiao Shi52e85402015-11-11 06:06:58 -070038const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_INITIAL(10);
39const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_MAX(250);
40
Junxiao Shi986b8492014-08-20 12:07:14 -070041BestRouteStrategy2::BestRouteStrategy2(Forwarder& forwarder, const Name& name)
42 : Strategy(forwarder, name)
Junxiao Shi52e85402015-11-11 06:06:58 -070043 , m_retxSuppression(RETX_SUPPRESSION_INITIAL,
44 RetxSuppressionExponential::DEFAULT_MULTIPLIER,
45 RETX_SUPPRESSION_MAX)
Junxiao Shi986b8492014-08-20 12:07:14 -070046{
47}
48
49/** \brief determines whether a NextHop is eligible
Junxiao Shi00dc9142016-11-21 14:23:12 +000050 * \param inFace incoming face of current Interest
51 * \param interest incoming Interest
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050052 * \param nexthop next hop
Junxiao Shi00dc9142016-11-21 14:23:12 +000053 * \param pitEntry PIT entry
Junxiao Shi4846f372016-04-05 13:39:30 -070054 * \param wantUnused if true, NextHop must not have unexpired out-record
Junxiao Shi986b8492014-08-20 12:07:14 -070055 * \param now time::steady_clock::now(), ignored if !wantUnused
56 */
57static inline bool
Junxiao Shi00dc9142016-11-21 14:23:12 +000058isNextHopEligible(const Face& inFace, const Interest& interest,
59 const fib::NextHop& nexthop,
60 const shared_ptr<pit::Entry>& pitEntry,
61 bool wantUnused = false,
62 time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
Junxiao Shi986b8492014-08-20 12:07:14 -070063{
Junxiao Shi00dc9142016-11-21 14:23:12 +000064 const Face& outFace = nexthop.getFace();
Junxiao Shi986b8492014-08-20 12:07:14 -070065
Junxiao Shi00dc9142016-11-21 14:23:12 +000066 // do not forward back to the same face
67 if (&outFace == &inFace)
Junxiao Shi986b8492014-08-20 12:07:14 -070068 return false;
69
70 // forwarding would violate scope
Junxiao Shi00dc9142016-11-21 14:23:12 +000071 if (wouldViolateScope(inFace, interest, outFace))
Junxiao Shi986b8492014-08-20 12:07:14 -070072 return false;
73
74 if (wantUnused) {
Junxiao Shi00dc9142016-11-21 14:23:12 +000075 // nexthop must not have unexpired out-record
76 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(outFace);
Junxiao Shi4846f372016-04-05 13:39:30 -070077 if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
Junxiao Shi986b8492014-08-20 12:07:14 -070078 return false;
79 }
80 }
81
82 return true;
83}
84
Junxiao Shi4846f372016-04-05 13:39:30 -070085/** \brief pick an eligible NextHop with earliest out-record
86 * \note It is assumed that every nexthop has an out-record.
Junxiao Shi986b8492014-08-20 12:07:14 -070087 */
88static inline fib::NextHopList::const_iterator
Junxiao Shi00dc9142016-11-21 14:23:12 +000089findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest,
Junxiao Shi986b8492014-08-20 12:07:14 -070090 const fib::NextHopList& nexthops,
Junxiao Shi00dc9142016-11-21 14:23:12 +000091 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -070092{
93 fib::NextHopList::const_iterator found = nexthops.end();
94 time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max();
95 for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
Junxiao Shi00dc9142016-11-21 14:23:12 +000096 if (!isNextHopEligible(inFace, interest, *it, pitEntry))
Junxiao Shi986b8492014-08-20 12:07:14 -070097 continue;
Junxiao Shia6de4292016-07-12 02:08:10 +000098 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(it->getFace());
Junxiao Shi4846f372016-04-05 13:39:30 -070099 BOOST_ASSERT(outRecord != pitEntry->out_end());
Junxiao Shi986b8492014-08-20 12:07:14 -0700100 if (outRecord->getLastRenewed() < earliestRenewed) {
101 found = it;
102 earliestRenewed = outRecord->getLastRenewed();
103 }
104 }
105 return found;
106}
107
108void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000109BestRouteStrategy2::afterReceiveInterest(const Face& inFace, const Interest& interest,
110 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -0700111{
Junxiao Shi8d843142016-07-11 22:42:42 +0000112 RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry);
113 if (suppression == RetxSuppression::SUPPRESS) {
114 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
115 << " suppressed");
116 return;
117 }
118
119 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
120 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Junxiao Shi986b8492014-08-20 12:07:14 -0700121 fib::NextHopList::const_iterator it = nexthops.end();
122
Junxiao Shia788e252015-01-28 17:06:25 -0700123 if (suppression == RetxSuppression::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700124 // forward to nexthop with lowest cost except downstream
125 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi00dc9142016-11-21 14:23:12 +0000126 bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
Junxiao Shi986b8492014-08-20 12:07:14 -0700127 false, time::steady_clock::TimePoint::min()));
128
129 if (it == nexthops.end()) {
130 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700131
132 lp::NackHeader nackHeader;
133 nackHeader.setReason(lp::NackReason::NO_ROUTE);
134 this->sendNack(pitEntry, inFace, nackHeader);
135
Junxiao Shi986b8492014-08-20 12:07:14 -0700136 this->rejectPendingInterest(pitEntry);
137 return;
138 }
139
Junxiao Shia6de4292016-07-12 02:08:10 +0000140 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000141 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700142 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000143 << " newPitEntry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700144 return;
145 }
146
Junxiao Shi986b8492014-08-20 12:07:14 -0700147 // find an unused upstream with lowest cost except downstream
148 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi00dc9142016-11-21 14:23:12 +0000149 bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
Junxiao Shi192af1f2015-01-13 23:19:39 -0700150 true, time::steady_clock::now()));
Junxiao Shi986b8492014-08-20 12:07:14 -0700151 if (it != nexthops.end()) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000152 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000153 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700154 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000155 << " retransmit-unused-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700156 return;
157 }
158
159 // find an eligible upstream that is used earliest
Junxiao Shi00dc9142016-11-21 14:23:12 +0000160 it = findEligibleNextHopWithEarliestOutRecord(inFace, interest, nexthops, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -0700161 if (it == nexthops.end()) {
162 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
163 }
164 else {
Junxiao Shia6de4292016-07-12 02:08:10 +0000165 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000166 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700167 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000168 << " retransmit-retry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700169 }
170}
171
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700172/** \return less severe NackReason between x and y
173 *
174 * lp::NackReason::NONE is treated as most severe
175 */
176inline lp::NackReason
177compareLessSevere(lp::NackReason x, lp::NackReason y)
178{
179 if (x == lp::NackReason::NONE) {
180 return y;
181 }
182 if (y == lp::NackReason::NONE) {
183 return x;
184 }
185 return static_cast<lp::NackReason>(std::min(static_cast<int>(x), static_cast<int>(y)));
186}
187
188void
189BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000190 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700191{
192 int nOutRecordsNotNacked = 0;
193 Face* lastFaceNotNacked = nullptr;
194 lp::NackReason leastSevereReason = lp::NackReason::NONE;
195 for (const pit::OutRecord& outR : pitEntry->getOutRecords()) {
196 const lp::NackHeader* inNack = outR.getIncomingNack();
197 if (inNack == nullptr) {
198 ++nOutRecordsNotNacked;
Junxiao Shi9cff7792016-08-01 21:45:11 +0000199 lastFaceNotNacked = &outR.getFace();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700200 continue;
201 }
202
203 leastSevereReason = compareLessSevere(leastSevereReason, inNack->getReason());
204 }
205
206 lp::NackHeader outNack;
207 outNack.setReason(leastSevereReason);
208
209 if (nOutRecordsNotNacked == 1) {
210 BOOST_ASSERT(lastFaceNotNacked != nullptr);
Junxiao Shi4846f372016-04-05 13:39:30 -0700211 pit::InRecordCollection::iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
212 if (inR != pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700213 // one out-record not Nacked, which is also a downstream
214 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
215 " nack=" << nack.getReason() <<
216 " nack-to(bidirectional)=" << lastFaceNotNacked->getId() <<
217 " out-nack=" << outNack.getReason());
218 this->sendNack(pitEntry, *lastFaceNotNacked, outNack);
219 return;
220 }
221 }
222
223 if (nOutRecordsNotNacked > 0) {
224 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
225 " nack=" << nack.getReason() <<
226 " waiting=" << nOutRecordsNotNacked);
227 // continue waiting
228 return;
229 }
230
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700231 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 Shi986b8492014-08-20 12:07:14 -0700237} // namespace fw
238} // namespace nfd