blob: e0e31dc88a7a5e4d5c7c13a8a08b2274ef485622 [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
37BestRouteStrategy2::BestRouteStrategy2(Forwarder& forwarder, const Name& name)
38 : Strategy(forwarder, name)
39{
40}
41
42/** \brief determines whether a NextHop is eligible
43 * \param currentDownstream incoming FaceId of current Interest
44 * \param wantUnused if true, NextHop must not have unexpired OutRecord
45 * \param now time::steady_clock::now(), ignored if !wantUnused
46 */
47static inline bool
48predicate_NextHop_eligible(const shared_ptr<pit::Entry>& pitEntry,
49 const fib::NextHop& nexthop, FaceId currentDownstream,
50 bool wantUnused = false,
51 time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
52{
53 shared_ptr<Face> upstream = nexthop.getFace();
54
55 // upstream is current downstream
56 if (upstream->getId() == currentDownstream)
57 return false;
58
59 // forwarding would violate scope
60 if (pitEntry->violatesScope(*upstream))
61 return false;
62
63 if (wantUnused) {
64 // NextHop must not have unexpired OutRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070065 pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*upstream);
Junxiao Shi986b8492014-08-20 12:07:14 -070066 if (outRecord != pitEntry->getOutRecords().end() &&
67 outRecord->getExpiry() > now) {
68 return false;
69 }
70 }
71
72 return true;
73}
74
Junxiao Shi986b8492014-08-20 12:07:14 -070075/** \brief pick an eligible NextHop with earliest OutRecord
76 * \note It is assumed that every nexthop has an OutRecord
77 */
78static inline fib::NextHopList::const_iterator
79findEligibleNextHopWithEarliestOutRecord(const shared_ptr<pit::Entry>& pitEntry,
80 const fib::NextHopList& nexthops,
81 FaceId currentDownstream)
82{
83 fib::NextHopList::const_iterator found = nexthops.end();
84 time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max();
85 for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
86 if (!predicate_NextHop_eligible(pitEntry, *it, currentDownstream))
87 continue;
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070088 pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*it->getFace());
Junxiao Shi986b8492014-08-20 12:07:14 -070089 BOOST_ASSERT(outRecord != pitEntry->getOutRecords().end());
90 if (outRecord->getLastRenewed() < earliestRenewed) {
91 found = it;
92 earliestRenewed = outRecord->getLastRenewed();
93 }
94 }
95 return found;
96}
97
98void
99BestRouteStrategy2::afterReceiveInterest(const Face& inFace,
100 const Interest& interest,
101 shared_ptr<fib::Entry> fibEntry,
102 shared_ptr<pit::Entry> pitEntry)
103{
104 const fib::NextHopList& nexthops = fibEntry->getNextHops();
105 fib::NextHopList::const_iterator it = nexthops.end();
106
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700107 RetxSuppression::Result suppression = m_retxSuppression.decide(inFace, interest, *pitEntry);
Junxiao Shia788e252015-01-28 17:06:25 -0700108 if (suppression == RetxSuppression::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700109 // forward to nexthop with lowest cost except downstream
110 it = std::find_if(nexthops.begin(), nexthops.end(),
111 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
112 false, time::steady_clock::TimePoint::min()));
113
114 if (it == nexthops.end()) {
115 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700116
117 lp::NackHeader nackHeader;
118 nackHeader.setReason(lp::NackReason::NO_ROUTE);
119 this->sendNack(pitEntry, inFace, nackHeader);
120
Junxiao Shi986b8492014-08-20 12:07:14 -0700121 this->rejectPendingInterest(pitEntry);
122 return;
123 }
124
125 shared_ptr<Face> outFace = it->getFace();
126 this->sendInterest(pitEntry, outFace);
127 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
128 << " newPitEntry-to=" << outFace->getId());
129 return;
130 }
131
Junxiao Shia788e252015-01-28 17:06:25 -0700132 if (suppression == RetxSuppression::SUPPRESS) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700133 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shi192af1f2015-01-13 23:19:39 -0700134 << " suppressed");
Junxiao Shi986b8492014-08-20 12:07:14 -0700135 return;
136 }
137
138 // find an unused upstream with lowest cost except downstream
139 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi192af1f2015-01-13 23:19:39 -0700140 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
141 true, time::steady_clock::now()));
Junxiao Shi986b8492014-08-20 12:07:14 -0700142 if (it != nexthops.end()) {
143 shared_ptr<Face> outFace = it->getFace();
144 this->sendInterest(pitEntry, outFace);
145 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
146 << " retransmit-unused-to=" << outFace->getId());
147 return;
148 }
149
150 // find an eligible upstream that is used earliest
151 it = findEligibleNextHopWithEarliestOutRecord(pitEntry, nexthops, inFace.getId());
152 if (it == nexthops.end()) {
153 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
154 }
155 else {
156 shared_ptr<Face> outFace = it->getFace();
157 this->sendInterest(pitEntry, outFace);
158 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
159 << " retransmit-retry-to=" << outFace->getId());
160 }
161}
162
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700163/** \return less severe NackReason between x and y
164 *
165 * lp::NackReason::NONE is treated as most severe
166 */
167inline lp::NackReason
168compareLessSevere(lp::NackReason x, lp::NackReason y)
169{
170 if (x == lp::NackReason::NONE) {
171 return y;
172 }
173 if (y == lp::NackReason::NONE) {
174 return x;
175 }
176 return static_cast<lp::NackReason>(std::min(static_cast<int>(x), static_cast<int>(y)));
177}
178
179void
180BestRouteStrategy2::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
181 shared_ptr<fib::Entry> fibEntry,
182 shared_ptr<pit::Entry> pitEntry)
183{
184 int nOutRecordsNotNacked = 0;
185 Face* lastFaceNotNacked = nullptr;
186 lp::NackReason leastSevereReason = lp::NackReason::NONE;
187 for (const pit::OutRecord& outR : pitEntry->getOutRecords()) {
188 const lp::NackHeader* inNack = outR.getIncomingNack();
189 if (inNack == nullptr) {
190 ++nOutRecordsNotNacked;
191 lastFaceNotNacked = outR.getFace().get();
192 continue;
193 }
194
195 leastSevereReason = compareLessSevere(leastSevereReason, inNack->getReason());
196 }
197
198 lp::NackHeader outNack;
199 outNack.setReason(leastSevereReason);
200
201 if (nOutRecordsNotNacked == 1) {
202 BOOST_ASSERT(lastFaceNotNacked != nullptr);
203 pit::InRecordCollection::const_iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
204 if (inR != pitEntry->getInRecords().end()) {
205 // one out-record not Nacked, which is also a downstream
206 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
207 " nack=" << nack.getReason() <<
208 " nack-to(bidirectional)=" << lastFaceNotNacked->getId() <<
209 " out-nack=" << outNack.getReason());
210 this->sendNack(pitEntry, *lastFaceNotNacked, outNack);
211 return;
212 }
213 }
214
215 if (nOutRecordsNotNacked > 0) {
216 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
217 " nack=" << nack.getReason() <<
218 " waiting=" << nOutRecordsNotNacked);
219 // continue waiting
220 return;
221 }
222
223
224 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
225 " nack=" << nack.getReason() <<
226 " nack-to=all out-nack=" << outNack.getReason());
227 this->sendNacks(pitEntry, outNack);
228}
229
Junxiao Shi986b8492014-08-20 12:07:14 -0700230} // namespace fw
231} // namespace nfd