blob: 0ba94e8cf6d7d2a73ec702bdeb4d002817be66de [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)
Junxiao Shi18739c42016-12-22 08:03:00 +000040 : Strategy(forwarder)
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{
Junxiao Shi18739c42016-12-22 08:03:00 +000045 ParsedInstanceName parsed = parseInstanceName(name);
46 if (!parsed.parameters.empty()) {
47 BOOST_THROW_EXCEPTION(std::invalid_argument("BestRouteStrategy2 does not accept parameters"));
48 }
49 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Junxiao Shi986b8492014-08-20 12:07:14 -070050}
51
Junxiao Shi037f4ab2016-12-13 04:27:06 +000052const Name&
53BestRouteStrategy2::getStrategyName()
54{
55 static Name strategyName("/localhost/nfd/strategy/best-route/%FD%04");
56 return strategyName;
57}
58
Junxiao Shi986b8492014-08-20 12:07:14 -070059/** \brief determines whether a NextHop is eligible
Junxiao Shi00dc9142016-11-21 14:23:12 +000060 * \param inFace incoming face of current Interest
61 * \param interest incoming Interest
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050062 * \param nexthop next hop
Junxiao Shi00dc9142016-11-21 14:23:12 +000063 * \param pitEntry PIT entry
Junxiao Shi4846f372016-04-05 13:39:30 -070064 * \param wantUnused if true, NextHop must not have unexpired out-record
Junxiao Shi986b8492014-08-20 12:07:14 -070065 * \param now time::steady_clock::now(), ignored if !wantUnused
66 */
67static inline bool
Junxiao Shi00dc9142016-11-21 14:23:12 +000068isNextHopEligible(const Face& inFace, const Interest& interest,
69 const fib::NextHop& nexthop,
70 const shared_ptr<pit::Entry>& pitEntry,
71 bool wantUnused = false,
72 time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
Junxiao Shi986b8492014-08-20 12:07:14 -070073{
Junxiao Shi00dc9142016-11-21 14:23:12 +000074 const Face& outFace = nexthop.getFace();
Junxiao Shi986b8492014-08-20 12:07:14 -070075
Junxiao Shi00dc9142016-11-21 14:23:12 +000076 // do not forward back to the same face
77 if (&outFace == &inFace)
Junxiao Shi986b8492014-08-20 12:07:14 -070078 return false;
79
80 // forwarding would violate scope
Junxiao Shi00dc9142016-11-21 14:23:12 +000081 if (wouldViolateScope(inFace, interest, outFace))
Junxiao Shi986b8492014-08-20 12:07:14 -070082 return false;
83
84 if (wantUnused) {
Junxiao Shi00dc9142016-11-21 14:23:12 +000085 // nexthop must not have unexpired out-record
86 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(outFace);
Junxiao Shi4846f372016-04-05 13:39:30 -070087 if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
Junxiao Shi986b8492014-08-20 12:07:14 -070088 return false;
89 }
90 }
91
92 return true;
93}
94
Junxiao Shi4846f372016-04-05 13:39:30 -070095/** \brief pick an eligible NextHop with earliest out-record
96 * \note It is assumed that every nexthop has an out-record.
Junxiao Shi986b8492014-08-20 12:07:14 -070097 */
98static inline fib::NextHopList::const_iterator
Junxiao Shi00dc9142016-11-21 14:23:12 +000099findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest,
Junxiao Shi986b8492014-08-20 12:07:14 -0700100 const fib::NextHopList& nexthops,
Junxiao Shi00dc9142016-11-21 14:23:12 +0000101 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -0700102{
103 fib::NextHopList::const_iterator found = nexthops.end();
104 time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max();
105 for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
Junxiao Shi00dc9142016-11-21 14:23:12 +0000106 if (!isNextHopEligible(inFace, interest, *it, pitEntry))
Junxiao Shi986b8492014-08-20 12:07:14 -0700107 continue;
Junxiao Shia6de4292016-07-12 02:08:10 +0000108 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(it->getFace());
Junxiao Shi4846f372016-04-05 13:39:30 -0700109 BOOST_ASSERT(outRecord != pitEntry->out_end());
Junxiao Shi986b8492014-08-20 12:07:14 -0700110 if (outRecord->getLastRenewed() < earliestRenewed) {
111 found = it;
112 earliestRenewed = outRecord->getLastRenewed();
113 }
114 }
115 return found;
116}
117
118void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000119BestRouteStrategy2::afterReceiveInterest(const Face& inFace, const Interest& interest,
120 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -0700121{
Junxiao Shi8d843142016-07-11 22:42:42 +0000122 RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry);
123 if (suppression == RetxSuppression::SUPPRESS) {
124 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
125 << " suppressed");
126 return;
127 }
128
129 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
130 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Junxiao Shi986b8492014-08-20 12:07:14 -0700131 fib::NextHopList::const_iterator it = nexthops.end();
132
Junxiao Shia788e252015-01-28 17:06:25 -0700133 if (suppression == RetxSuppression::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700134 // forward to nexthop with lowest cost except downstream
135 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi00dc9142016-11-21 14:23:12 +0000136 bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
Junxiao Shi986b8492014-08-20 12:07:14 -0700137 false, time::steady_clock::TimePoint::min()));
138
139 if (it == nexthops.end()) {
140 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700141
142 lp::NackHeader nackHeader;
143 nackHeader.setReason(lp::NackReason::NO_ROUTE);
144 this->sendNack(pitEntry, inFace, nackHeader);
145
Junxiao Shi986b8492014-08-20 12:07:14 -0700146 this->rejectPendingInterest(pitEntry);
147 return;
148 }
149
Junxiao Shia6de4292016-07-12 02:08:10 +0000150 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000151 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700152 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000153 << " newPitEntry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700154 return;
155 }
156
Junxiao Shi986b8492014-08-20 12:07:14 -0700157 // find an unused upstream with lowest cost except downstream
158 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi00dc9142016-11-21 14:23:12 +0000159 bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
Junxiao Shi192af1f2015-01-13 23:19:39 -0700160 true, time::steady_clock::now()));
Junxiao Shi986b8492014-08-20 12:07:14 -0700161 if (it != nexthops.end()) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000162 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000163 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700164 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000165 << " retransmit-unused-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700166 return;
167 }
168
169 // find an eligible upstream that is used earliest
Junxiao Shi00dc9142016-11-21 14:23:12 +0000170 it = findEligibleNextHopWithEarliestOutRecord(inFace, interest, nexthops, pitEntry);
Junxiao Shi986b8492014-08-20 12:07:14 -0700171 if (it == nexthops.end()) {
172 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
173 }
174 else {
Junxiao Shia6de4292016-07-12 02:08:10 +0000175 Face& outFace = it->getFace();
Junxiao Shi00dc9142016-11-21 14:23:12 +0000176 this->sendInterest(pitEntry, outFace, interest);
Junxiao Shi986b8492014-08-20 12:07:14 -0700177 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000178 << " retransmit-retry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700179 }
180}
181
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700182/** \return less severe NackReason between x and y
183 *
184 * lp::NackReason::NONE is treated as most severe
185 */
186inline lp::NackReason
187compareLessSevere(lp::NackReason x, lp::NackReason y)
188{
189 if (x == lp::NackReason::NONE) {
190 return y;
191 }
192 if (y == lp::NackReason::NONE) {
193 return x;
194 }
195 return static_cast<lp::NackReason>(std::min(static_cast<int>(x), static_cast<int>(y)));
196}
197
198void
199BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000200 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700201{
202 int nOutRecordsNotNacked = 0;
203 Face* lastFaceNotNacked = nullptr;
204 lp::NackReason leastSevereReason = lp::NackReason::NONE;
205 for (const pit::OutRecord& outR : pitEntry->getOutRecords()) {
206 const lp::NackHeader* inNack = outR.getIncomingNack();
207 if (inNack == nullptr) {
208 ++nOutRecordsNotNacked;
Junxiao Shi9cff7792016-08-01 21:45:11 +0000209 lastFaceNotNacked = &outR.getFace();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700210 continue;
211 }
212
213 leastSevereReason = compareLessSevere(leastSevereReason, inNack->getReason());
214 }
215
216 lp::NackHeader outNack;
217 outNack.setReason(leastSevereReason);
218
219 if (nOutRecordsNotNacked == 1) {
220 BOOST_ASSERT(lastFaceNotNacked != nullptr);
Junxiao Shi4846f372016-04-05 13:39:30 -0700221 pit::InRecordCollection::iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
222 if (inR != pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700223 // one out-record not Nacked, which is also a downstream
224 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
225 " nack=" << nack.getReason() <<
226 " nack-to(bidirectional)=" << lastFaceNotNacked->getId() <<
227 " out-nack=" << outNack.getReason());
228 this->sendNack(pitEntry, *lastFaceNotNacked, outNack);
229 return;
230 }
231 }
232
233 if (nOutRecordsNotNacked > 0) {
234 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
235 " nack=" << nack.getReason() <<
236 " waiting=" << nOutRecordsNotNacked);
237 // continue waiting
238 return;
239 }
240
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700241 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
242 " nack=" << nack.getReason() <<
243 " nack-to=all out-nack=" << outNack.getReason());
244 this->sendNacks(pitEntry, outNack);
245}
246
Junxiao Shi986b8492014-08-20 12:07:14 -0700247} // namespace fw
248} // namespace nfd