blob: 0ab6b253e461a492fb9a2b74a9116d6ecc6d5a98 [file] [log] [blame]
Junxiao Shi986b8492014-08-20 12:07:14 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande90015992017-07-11 17:25:48 -05002/*
Davide Pesaventoa3148082018-04-12 18:21:54 -04003 * Copyright (c) 2014-2018, 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
Davide Pesaventoa3148082018-04-12 18:21:54 -040033NFD_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)
Junxiao Shi18739c42016-12-22 08:03:00 +000040 : Strategy(forwarder)
Junxiao Shi99540072017-01-27 19:57:33 +000041 , ProcessNackTraits(this)
Junxiao Shi52e85402015-11-11 06:06:58 -070042 , m_retxSuppression(RETX_SUPPRESSION_INITIAL,
43 RetxSuppressionExponential::DEFAULT_MULTIPLIER,
44 RETX_SUPPRESSION_MAX)
Junxiao Shi986b8492014-08-20 12:07:14 -070045{
Junxiao Shi18739c42016-12-22 08:03:00 +000046 ParsedInstanceName parsed = parseInstanceName(name);
47 if (!parsed.parameters.empty()) {
48 BOOST_THROW_EXCEPTION(std::invalid_argument("BestRouteStrategy2 does not accept parameters"));
49 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000050 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
51 BOOST_THROW_EXCEPTION(std::invalid_argument(
Alexander Afanasyev0c63c632017-12-05 11:17:09 -050052 "BestRouteStrategy2 does not support version " + to_string(*parsed.version)));
Junxiao Shi91f6ee02016-12-29 21:44:44 +000053 }
Junxiao Shi18739c42016-12-22 08:03:00 +000054 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Junxiao Shi986b8492014-08-20 12:07:14 -070055}
56
Junxiao Shi037f4ab2016-12-13 04:27:06 +000057const Name&
58BestRouteStrategy2::getStrategyName()
59{
Teng Liangf995f382017-04-04 22:09:39 +000060 static Name strategyName("/localhost/nfd/strategy/best-route/%FD%05");
Junxiao Shi037f4ab2016-12-13 04:27:06 +000061 return strategyName;
62}
63
Junxiao Shi986b8492014-08-20 12:07:14 -070064/** \brief determines whether a NextHop is eligible
Junxiao Shi00dc9142016-11-21 14:23:12 +000065 * \param inFace incoming face of current Interest
66 * \param interest incoming Interest
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050067 * \param nexthop next hop
Junxiao Shi00dc9142016-11-21 14:23:12 +000068 * \param pitEntry PIT entry
Junxiao Shi4846f372016-04-05 13:39:30 -070069 * \param wantUnused if true, NextHop must not have unexpired out-record
Junxiao Shi986b8492014-08-20 12:07:14 -070070 * \param now time::steady_clock::now(), ignored if !wantUnused
71 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -040072static bool
Junxiao Shi00dc9142016-11-21 14:23:12 +000073isNextHopEligible(const Face& inFace, const Interest& interest,
74 const fib::NextHop& nexthop,
75 const shared_ptr<pit::Entry>& pitEntry,
76 bool wantUnused = false,
77 time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
Junxiao Shi986b8492014-08-20 12:07:14 -070078{
Junxiao Shi00dc9142016-11-21 14:23:12 +000079 const Face& outFace = nexthop.getFace();
Junxiao Shi986b8492014-08-20 12:07:14 -070080
Teng Liangf995f382017-04-04 22:09:39 +000081 // do not forward back to the same face, unless it is ad hoc
82 if (outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC)
Junxiao Shi986b8492014-08-20 12:07:14 -070083 return false;
84
85 // forwarding would violate scope
Junxiao Shi00dc9142016-11-21 14:23:12 +000086 if (wouldViolateScope(inFace, interest, outFace))
Junxiao Shi986b8492014-08-20 12:07:14 -070087 return false;
88
89 if (wantUnused) {
Junxiao Shi00dc9142016-11-21 14:23:12 +000090 // nexthop must not have unexpired out-record
Davide Pesaventoe4b22382018-06-10 14:37:24 -040091 auto outRecord = pitEntry->getOutRecord(outFace);
Junxiao Shi4846f372016-04-05 13:39:30 -070092 if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
Junxiao Shi986b8492014-08-20 12:07:14 -070093 return false;
94 }
95 }
96
97 return true;
98}
99
Junxiao Shi4846f372016-04-05 13:39:30 -0700100/** \brief pick an eligible NextHop with earliest out-record
101 * \note It is assumed that every nexthop has an out-record.
Junxiao Shi986b8492014-08-20 12:07:14 -0700102 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400103static fib::NextHopList::const_iterator
Junxiao Shi00dc9142016-11-21 14:23:12 +0000104findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest,
Junxiao Shi986b8492014-08-20 12:07:14 -0700105 const fib::NextHopList& nexthops,
Junxiao Shi00dc9142016-11-21 14:23:12 +0000106 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -0700107{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400108 auto found = nexthops.end();
109 auto earliestRenewed = time::steady_clock::TimePoint::max();
110
111 for (auto it = nexthops.begin(); it != nexthops.end(); ++it) {
Junxiao Shi00dc9142016-11-21 14:23:12 +0000112 if (!isNextHopEligible(inFace, interest, *it, pitEntry))
Junxiao Shi986b8492014-08-20 12:07:14 -0700113 continue;
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400114
115 auto outRecord = pitEntry->getOutRecord(it->getFace());
Junxiao Shi4846f372016-04-05 13:39:30 -0700116 BOOST_ASSERT(outRecord != pitEntry->out_end());
Junxiao Shi986b8492014-08-20 12:07:14 -0700117 if (outRecord->getLastRenewed() < earliestRenewed) {
118 found = it;
119 earliestRenewed = outRecord->getLastRenewed();
120 }
121 }
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400122
Junxiao Shi986b8492014-08-20 12:07:14 -0700123 return found;
124}
125
126void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000127BestRouteStrategy2::afterReceiveInterest(const Face& inFace, const Interest& interest,
128 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -0700129{
Ashlesh Gawande90015992017-07-11 17:25:48 -0500130 RetxSuppressionResult suppression = m_retxSuppression.decidePerPitEntry(*pitEntry);
131 if (suppression == RetxSuppressionResult::SUPPRESS) {
Junxiao Shi8d843142016-07-11 22:42:42 +0000132 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
133 << " suppressed");
134 return;
135 }
136
137 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
138 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400139 auto it = nexthops.end();
Junxiao Shi986b8492014-08-20 12:07:14 -0700140
Ashlesh Gawande90015992017-07-11 17:25:48 -0500141 if (suppression == RetxSuppressionResult::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700142 // forward to nexthop with lowest cost except downstream
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400143 it = std::find_if(nexthops.begin(), nexthops.end(), [&] (const auto& nexthop) {
144 return isNextHopEligible(inFace, interest, nexthop, pitEntry);
145 });
Junxiao Shi986b8492014-08-20 12:07:14 -0700146
147 if (it == nexthops.end()) {
148 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700149
150 lp::NackHeader nackHeader;
151 nackHeader.setReason(lp::NackReason::NO_ROUTE);
152 this->sendNack(pitEntry, inFace, nackHeader);
153
Junxiao Shi986b8492014-08-20 12:07:14 -0700154 this->rejectPendingInterest(pitEntry);
155 return;
156 }
157
Junxiao Shia6de4292016-07-12 02:08:10 +0000158 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000159 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700160 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000161 << " newPitEntry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700162 return;
163 }
164
Junxiao Shi986b8492014-08-20 12:07:14 -0700165 // find an unused upstream with lowest cost except downstream
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400166 it = std::find_if(nexthops.begin(), nexthops.end(), [&] (const auto& nexthop) {
167 return isNextHopEligible(inFace, interest, nexthop, pitEntry, true, time::steady_clock::now());
168 });
169
Junxiao Shi986b8492014-08-20 12:07:14 -0700170 if (it != nexthops.end()) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000171 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000172 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700173 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000174 << " retransmit-unused-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700175 return;
176 }
177
178 // find an eligible upstream that is used earliest
Junxiao Shi00dc9142016-11-21 14:23:12 +0000179 it = findEligibleNextHopWithEarliestOutRecord(inFace, interest, nexthops, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -0700180 if (it == nexthops.end()) {
181 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
182 }
183 else {
Junxiao Shia6de4292016-07-12 02:08:10 +0000184 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000185 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700186 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000187 << " retransmit-retry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700188 }
189}
190
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700191void
192BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000193 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700194{
Junxiao Shi99540072017-01-27 19:57:33 +0000195 this->processNack(inFace, nack, pitEntry);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700196}
197
Junxiao Shi986b8492014-08-20 12:07:14 -0700198} // namespace fw
199} // namespace nfd