blob: e897b491d42292d5dcc6fd4d77d35bebdd62a46a [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 Shifef73e42016-03-29 14:15:05 -070027#include "pit-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
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050050 * \param pitEntry PIT entry
51 * \param nexthop next hop
Junxiao Shi986b8492014-08-20 12:07:14 -070052 * \param currentDownstream incoming FaceId of current Interest
Junxiao Shi4846f372016-04-05 13:39:30 -070053 * \param wantUnused if true, NextHop must not have unexpired out-record
Junxiao Shi986b8492014-08-20 12:07:14 -070054 * \param now time::steady_clock::now(), ignored if !wantUnused
55 */
56static inline bool
57predicate_NextHop_eligible(const shared_ptr<pit::Entry>& pitEntry,
58 const fib::NextHop& nexthop, FaceId currentDownstream,
59 bool wantUnused = false,
60 time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
61{
Junxiao Shia6de4292016-07-12 02:08:10 +000062 Face& upstream = nexthop.getFace();
Junxiao Shi986b8492014-08-20 12:07:14 -070063
64 // upstream is current downstream
Junxiao Shia6de4292016-07-12 02:08:10 +000065 if (upstream.getId() == currentDownstream)
Junxiao Shi986b8492014-08-20 12:07:14 -070066 return false;
67
68 // forwarding would violate scope
Junxiao Shia6de4292016-07-12 02:08:10 +000069 if (violatesScope(*pitEntry, upstream))
Junxiao Shi986b8492014-08-20 12:07:14 -070070 return false;
71
72 if (wantUnused) {
Junxiao Shi4846f372016-04-05 13:39:30 -070073 // NextHop must not have unexpired out-record
Junxiao Shia6de4292016-07-12 02:08:10 +000074 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(upstream);
Junxiao Shi4846f372016-04-05 13:39:30 -070075 if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
Junxiao Shi986b8492014-08-20 12:07:14 -070076 return false;
77 }
78 }
79
80 return true;
81}
82
Junxiao Shi4846f372016-04-05 13:39:30 -070083/** \brief pick an eligible NextHop with earliest out-record
84 * \note It is assumed that every nexthop has an out-record.
Junxiao Shi986b8492014-08-20 12:07:14 -070085 */
86static inline fib::NextHopList::const_iterator
87findEligibleNextHopWithEarliestOutRecord(const shared_ptr<pit::Entry>& pitEntry,
88 const fib::NextHopList& nexthops,
89 FaceId currentDownstream)
90{
91 fib::NextHopList::const_iterator found = nexthops.end();
92 time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max();
93 for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
94 if (!predicate_NextHop_eligible(pitEntry, *it, currentDownstream))
95 continue;
Junxiao Shia6de4292016-07-12 02:08:10 +000096 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(it->getFace());
Junxiao Shi4846f372016-04-05 13:39:30 -070097 BOOST_ASSERT(outRecord != pitEntry->out_end());
Junxiao Shi986b8492014-08-20 12:07:14 -070098 if (outRecord->getLastRenewed() < earliestRenewed) {
99 found = it;
100 earliestRenewed = outRecord->getLastRenewed();
101 }
102 }
103 return found;
104}
105
106void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000107BestRouteStrategy2::afterReceiveInterest(const Face& inFace, const Interest& interest,
108 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi986b8492014-08-20 12:07:14 -0700109{
Junxiao Shi8d843142016-07-11 22:42:42 +0000110 RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry);
111 if (suppression == RetxSuppression::SUPPRESS) {
112 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
113 << " suppressed");
114 return;
115 }
116
117 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
118 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Junxiao Shi986b8492014-08-20 12:07:14 -0700119 fib::NextHopList::const_iterator it = nexthops.end();
120
Junxiao Shia788e252015-01-28 17:06:25 -0700121 if (suppression == RetxSuppression::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700122 // forward to nexthop with lowest cost except downstream
123 it = std::find_if(nexthops.begin(), nexthops.end(),
124 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
125 false, time::steady_clock::TimePoint::min()));
126
127 if (it == nexthops.end()) {
128 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700129
130 lp::NackHeader nackHeader;
131 nackHeader.setReason(lp::NackReason::NO_ROUTE);
132 this->sendNack(pitEntry, inFace, nackHeader);
133
Junxiao Shi986b8492014-08-20 12:07:14 -0700134 this->rejectPendingInterest(pitEntry);
135 return;
136 }
137
Junxiao Shia6de4292016-07-12 02:08:10 +0000138 Face& outFace = it->getFace();
Junxiao Shi986b8492014-08-20 12:07:14 -0700139 this->sendInterest(pitEntry, outFace);
140 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000141 << " newPitEntry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700142 return;
143 }
144
Junxiao Shi986b8492014-08-20 12:07:14 -0700145 // find an unused upstream with lowest cost except downstream
146 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi192af1f2015-01-13 23:19:39 -0700147 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
148 true, time::steady_clock::now()));
Junxiao Shi986b8492014-08-20 12:07:14 -0700149 if (it != nexthops.end()) {
Junxiao Shia6de4292016-07-12 02:08:10 +0000150 Face& outFace = it->getFace();
Junxiao Shi986b8492014-08-20 12:07:14 -0700151 this->sendInterest(pitEntry, outFace);
152 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000153 << " retransmit-unused-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700154 return;
155 }
156
157 // find an eligible upstream that is used earliest
158 it = findEligibleNextHopWithEarliestOutRecord(pitEntry, nexthops, inFace.getId());
159 if (it == nexthops.end()) {
160 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
161 }
162 else {
Junxiao Shia6de4292016-07-12 02:08:10 +0000163 Face& outFace = it->getFace();
Junxiao Shi986b8492014-08-20 12:07:14 -0700164 this->sendInterest(pitEntry, outFace);
165 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shia6de4292016-07-12 02:08:10 +0000166 << " retransmit-retry-to=" << outFace.getId());
Junxiao Shi986b8492014-08-20 12:07:14 -0700167 }
168}
169
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700170/** \return less severe NackReason between x and y
171 *
172 * lp::NackReason::NONE is treated as most severe
173 */
174inline lp::NackReason
175compareLessSevere(lp::NackReason x, lp::NackReason y)
176{
177 if (x == lp::NackReason::NONE) {
178 return y;
179 }
180 if (y == lp::NackReason::NONE) {
181 return x;
182 }
183 return static_cast<lp::NackReason>(std::min(static_cast<int>(x), static_cast<int>(y)));
184}
185
186void
187BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000188 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700189{
190 int nOutRecordsNotNacked = 0;
191 Face* lastFaceNotNacked = nullptr;
192 lp::NackReason leastSevereReason = lp::NackReason::NONE;
193 for (const pit::OutRecord& outR : pitEntry->getOutRecords()) {
194 const lp::NackHeader* inNack = outR.getIncomingNack();
195 if (inNack == nullptr) {
196 ++nOutRecordsNotNacked;
Junxiao Shi9cff7792016-08-01 21:45:11 +0000197 lastFaceNotNacked = &outR.getFace();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700198 continue;
199 }
200
201 leastSevereReason = compareLessSevere(leastSevereReason, inNack->getReason());
202 }
203
204 lp::NackHeader outNack;
205 outNack.setReason(leastSevereReason);
206
207 if (nOutRecordsNotNacked == 1) {
208 BOOST_ASSERT(lastFaceNotNacked != nullptr);
Junxiao Shi4846f372016-04-05 13:39:30 -0700209 pit::InRecordCollection::iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
210 if (inR != pitEntry->in_end()) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700211 // one out-record not Nacked, which is also a downstream
212 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
213 " nack=" << nack.getReason() <<
214 " nack-to(bidirectional)=" << lastFaceNotNacked->getId() <<
215 " out-nack=" << outNack.getReason());
216 this->sendNack(pitEntry, *lastFaceNotNacked, outNack);
217 return;
218 }
219 }
220
221 if (nOutRecordsNotNacked > 0) {
222 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
223 " nack=" << nack.getReason() <<
224 " waiting=" << nOutRecordsNotNacked);
225 // continue waiting
226 return;
227 }
228
229
230 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
231 " nack=" << nack.getReason() <<
232 " nack-to=all out-nack=" << outNack.getReason());
233 this->sendNacks(pitEntry, outNack);
234}
235
Junxiao Shi986b8492014-08-20 12:07:14 -0700236} // namespace fw
237} // namespace nfd