blob: 96791beb7a8e304079886c388f1226751884c6a8 [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
53 * \param wantUnused if true, NextHop must not have unexpired OutRecord
54 * \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{
62 shared_ptr<Face> upstream = nexthop.getFace();
63
64 // upstream is current downstream
65 if (upstream->getId() == currentDownstream)
66 return false;
67
68 // forwarding would violate scope
Junxiao Shifef73e42016-03-29 14:15:05 -070069 if (violatesScope(*pitEntry, *upstream))
Junxiao Shi986b8492014-08-20 12:07:14 -070070 return false;
71
72 if (wantUnused) {
73 // NextHop must not have unexpired OutRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070074 pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*upstream);
Junxiao Shi986b8492014-08-20 12:07:14 -070075 if (outRecord != pitEntry->getOutRecords().end() &&
76 outRecord->getExpiry() > now) {
77 return false;
78 }
79 }
80
81 return true;
82}
83
Junxiao Shi986b8492014-08-20 12:07:14 -070084/** \brief pick an eligible NextHop with earliest OutRecord
85 * \note It is assumed that every nexthop has an OutRecord
86 */
87static inline fib::NextHopList::const_iterator
88findEligibleNextHopWithEarliestOutRecord(const shared_ptr<pit::Entry>& pitEntry,
89 const fib::NextHopList& nexthops,
90 FaceId currentDownstream)
91{
92 fib::NextHopList::const_iterator found = nexthops.end();
93 time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max();
94 for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
95 if (!predicate_NextHop_eligible(pitEntry, *it, currentDownstream))
96 continue;
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070097 pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*it->getFace());
Junxiao Shi986b8492014-08-20 12:07:14 -070098 BOOST_ASSERT(outRecord != pitEntry->getOutRecords().end());
99 if (outRecord->getLastRenewed() < earliestRenewed) {
100 found = it;
101 earliestRenewed = outRecord->getLastRenewed();
102 }
103 }
104 return found;
105}
106
107void
108BestRouteStrategy2::afterReceiveInterest(const Face& inFace,
109 const Interest& interest,
110 shared_ptr<fib::Entry> fibEntry,
111 shared_ptr<pit::Entry> pitEntry)
112{
113 const fib::NextHopList& nexthops = fibEntry->getNextHops();
114 fib::NextHopList::const_iterator it = nexthops.end();
115
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700116 RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry);
Junxiao Shia788e252015-01-28 17:06:25 -0700117 if (suppression == RetxSuppression::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700118 // forward to nexthop with lowest cost except downstream
119 it = std::find_if(nexthops.begin(), nexthops.end(),
120 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
121 false, time::steady_clock::TimePoint::min()));
122
123 if (it == nexthops.end()) {
124 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700125
126 lp::NackHeader nackHeader;
127 nackHeader.setReason(lp::NackReason::NO_ROUTE);
128 this->sendNack(pitEntry, inFace, nackHeader);
129
Junxiao Shi986b8492014-08-20 12:07:14 -0700130 this->rejectPendingInterest(pitEntry);
131 return;
132 }
133
134 shared_ptr<Face> outFace = it->getFace();
135 this->sendInterest(pitEntry, outFace);
136 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
137 << " newPitEntry-to=" << outFace->getId());
138 return;
139 }
140
Junxiao Shia788e252015-01-28 17:06:25 -0700141 if (suppression == RetxSuppression::SUPPRESS) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700142 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shi192af1f2015-01-13 23:19:39 -0700143 << " suppressed");
Junxiao Shi986b8492014-08-20 12:07:14 -0700144 return;
145 }
146
147 // find an unused upstream with lowest cost except downstream
148 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi192af1f2015-01-13 23:19:39 -0700149 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
150 true, time::steady_clock::now()));
Junxiao Shi986b8492014-08-20 12:07:14 -0700151 if (it != nexthops.end()) {
152 shared_ptr<Face> outFace = it->getFace();
153 this->sendInterest(pitEntry, outFace);
154 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
155 << " retransmit-unused-to=" << outFace->getId());
156 return;
157 }
158
159 // find an eligible upstream that is used earliest
160 it = findEligibleNextHopWithEarliestOutRecord(pitEntry, nexthops, inFace.getId());
161 if (it == nexthops.end()) {
162 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
163 }
164 else {
165 shared_ptr<Face> outFace = it->getFace();
166 this->sendInterest(pitEntry, outFace);
167 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
168 << " retransmit-retry-to=" << outFace->getId());
169 }
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,
190 shared_ptr<fib::Entry> fibEntry,
191 shared_ptr<pit::Entry> pitEntry)
192{
193 int nOutRecordsNotNacked = 0;
194 Face* lastFaceNotNacked = nullptr;
195 lp::NackReason leastSevereReason = lp::NackReason::NONE;
196 for (const pit::OutRecord& outR : pitEntry->getOutRecords()) {
197 const lp::NackHeader* inNack = outR.getIncomingNack();
198 if (inNack == nullptr) {
199 ++nOutRecordsNotNacked;
200 lastFaceNotNacked = outR.getFace().get();
201 continue;
202 }
203
204 leastSevereReason = compareLessSevere(leastSevereReason, inNack->getReason());
205 }
206
207 lp::NackHeader outNack;
208 outNack.setReason(leastSevereReason);
209
210 if (nOutRecordsNotNacked == 1) {
211 BOOST_ASSERT(lastFaceNotNacked != nullptr);
212 pit::InRecordCollection::const_iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
213 if (inR != pitEntry->getInRecords().end()) {
214 // one out-record not Nacked, which is also a downstream
215 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
216 " nack=" << nack.getReason() <<
217 " nack-to(bidirectional)=" << lastFaceNotNacked->getId() <<
218 " out-nack=" << outNack.getReason());
219 this->sendNack(pitEntry, *lastFaceNotNacked, outNack);
220 return;
221 }
222 }
223
224 if (nOutRecordsNotNacked > 0) {
225 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
226 " nack=" << nack.getReason() <<
227 " waiting=" << nOutRecordsNotNacked);
228 // continue waiting
229 return;
230 }
231
232
233 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
234 " nack=" << nack.getReason() <<
235 " nack-to=all out-nack=" << outNack.getReason());
236 this->sendNacks(pitEntry, outNack);
237}
238
Junxiao Shi986b8492014-08-20 12:07:14 -0700239} // namespace fw
240} // namespace nfd