blob: 477b0a3082d5091a4676ff89e291cd8556f624bd [file] [log] [blame]
Junxiao Shi986b8492014-08-20 12:07:14 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi192af1f2015-01-13 23:19:39 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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"
27#include "core/logger.hpp"
28
29namespace nfd {
30namespace fw {
31
32NFD_LOG_INIT("BestRouteStrategy2");
33
Junxiao Shi5e5e4452015-09-24 16:56:52 -070034const Name BestRouteStrategy2::STRATEGY_NAME("ndn:/localhost/nfd/strategy/best-route/%FD%04");
Junxiao Shifaf3eb02015-02-16 10:50:36 -070035NFD_REGISTER_STRATEGY(BestRouteStrategy2);
Junxiao Shi986b8492014-08-20 12:07:14 -070036
Junxiao Shi52e85402015-11-11 06:06:58 -070037const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_INITIAL(10);
38const time::milliseconds BestRouteStrategy2::RETX_SUPPRESSION_MAX(250);
39
Junxiao Shi986b8492014-08-20 12:07:14 -070040BestRouteStrategy2::BestRouteStrategy2(Forwarder& forwarder, const Name& name)
41 : Strategy(forwarder, name)
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{
46}
47
48/** \brief determines whether a NextHop is eligible
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050049 * \param pitEntry PIT entry
50 * \param nexthop next hop
Junxiao Shi986b8492014-08-20 12:07:14 -070051 * \param currentDownstream incoming FaceId of current Interest
52 * \param wantUnused if true, NextHop must not have unexpired OutRecord
53 * \param now time::steady_clock::now(), ignored if !wantUnused
54 */
55static inline bool
56predicate_NextHop_eligible(const shared_ptr<pit::Entry>& pitEntry,
57 const fib::NextHop& nexthop, FaceId currentDownstream,
58 bool wantUnused = false,
59 time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
60{
61 shared_ptr<Face> upstream = nexthop.getFace();
62
63 // upstream is current downstream
64 if (upstream->getId() == currentDownstream)
65 return false;
66
67 // forwarding would violate scope
68 if (pitEntry->violatesScope(*upstream))
69 return false;
70
71 if (wantUnused) {
72 // NextHop must not have unexpired OutRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070073 pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*upstream);
Junxiao Shi986b8492014-08-20 12:07:14 -070074 if (outRecord != pitEntry->getOutRecords().end() &&
75 outRecord->getExpiry() > now) {
76 return false;
77 }
78 }
79
80 return true;
81}
82
Junxiao Shi986b8492014-08-20 12:07:14 -070083/** \brief pick an eligible NextHop with earliest OutRecord
84 * \note It is assumed that every nexthop has an OutRecord
85 */
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 Shib2bcbcd2014-11-08 09:30:28 -070096 pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*it->getFace());
Junxiao Shi986b8492014-08-20 12:07:14 -070097 BOOST_ASSERT(outRecord != pitEntry->getOutRecords().end());
98 if (outRecord->getLastRenewed() < earliestRenewed) {
99 found = it;
100 earliestRenewed = outRecord->getLastRenewed();
101 }
102 }
103 return found;
104}
105
106void
107BestRouteStrategy2::afterReceiveInterest(const Face& inFace,
108 const Interest& interest,
109 shared_ptr<fib::Entry> fibEntry,
110 shared_ptr<pit::Entry> pitEntry)
111{
112 const fib::NextHopList& nexthops = fibEntry->getNextHops();
113 fib::NextHopList::const_iterator it = nexthops.end();
114
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700115 RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry);
Junxiao Shia788e252015-01-28 17:06:25 -0700116 if (suppression == RetxSuppression::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700117 // forward to nexthop with lowest cost except downstream
118 it = std::find_if(nexthops.begin(), nexthops.end(),
119 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
120 false, time::steady_clock::TimePoint::min()));
121
122 if (it == nexthops.end()) {
123 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700124
125 lp::NackHeader nackHeader;
126 nackHeader.setReason(lp::NackReason::NO_ROUTE);
127 this->sendNack(pitEntry, inFace, nackHeader);
128
Junxiao Shi986b8492014-08-20 12:07:14 -0700129 this->rejectPendingInterest(pitEntry);
130 return;
131 }
132
133 shared_ptr<Face> outFace = it->getFace();
134 this->sendInterest(pitEntry, outFace);
135 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
136 << " newPitEntry-to=" << outFace->getId());
137 return;
138 }
139
Junxiao Shia788e252015-01-28 17:06:25 -0700140 if (suppression == RetxSuppression::SUPPRESS) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700141 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shi192af1f2015-01-13 23:19:39 -0700142 << " suppressed");
Junxiao Shi986b8492014-08-20 12:07:14 -0700143 return;
144 }
145
146 // find an unused upstream with lowest cost except downstream
147 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi192af1f2015-01-13 23:19:39 -0700148 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
149 true, time::steady_clock::now()));
Junxiao Shi986b8492014-08-20 12:07:14 -0700150 if (it != nexthops.end()) {
151 shared_ptr<Face> outFace = it->getFace();
152 this->sendInterest(pitEntry, outFace);
153 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
154 << " retransmit-unused-to=" << outFace->getId());
155 return;
156 }
157
158 // find an eligible upstream that is used earliest
159 it = findEligibleNextHopWithEarliestOutRecord(pitEntry, nexthops, inFace.getId());
160 if (it == nexthops.end()) {
161 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
162 }
163 else {
164 shared_ptr<Face> outFace = it->getFace();
165 this->sendInterest(pitEntry, outFace);
166 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
167 << " retransmit-retry-to=" << outFace->getId());
168 }
169}
170
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700171/** \return less severe NackReason between x and y
172 *
173 * lp::NackReason::NONE is treated as most severe
174 */
175inline lp::NackReason
176compareLessSevere(lp::NackReason x, lp::NackReason y)
177{
178 if (x == lp::NackReason::NONE) {
179 return y;
180 }
181 if (y == lp::NackReason::NONE) {
182 return x;
183 }
184 return static_cast<lp::NackReason>(std::min(static_cast<int>(x), static_cast<int>(y)));
185}
186
187void
188BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
189 shared_ptr<fib::Entry> fibEntry,
190 shared_ptr<pit::Entry> pitEntry)
191{
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;
199 lastFaceNotNacked = outR.getFace().get();
200 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);
211 pit::InRecordCollection::const_iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
212 if (inR != pitEntry->getInRecords().end()) {
213 // 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
231
232 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
233 " nack=" << nack.getReason() <<
234 " nack-to=all out-nack=" << outNack.getReason());
235 this->sendNacks(pitEntry, outNack);
236}
237
Junxiao Shi986b8492014-08-20 12:07:14 -0700238} // namespace fw
239} // namespace nfd