blob: 2ec786c2edf6dfd7e3081b3ac4ec2fa1819763c5 [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 Shi684fa0f2015-02-23 21:39:57 -070034const Name BestRouteStrategy2::STRATEGY_NAME("ndn:/localhost/nfd/strategy/best-route/%FD%03");
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 Shia788e252015-01-28 17:06:25 -0700107 RetxSuppression::Result suppression =
108 m_retxSuppression.decide(inFace, interest, *pitEntry);
109 if (suppression == RetxSuppression::NEW) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700110 // forward to nexthop with lowest cost except downstream
111 it = std::find_if(nexthops.begin(), nexthops.end(),
112 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
113 false, time::steady_clock::TimePoint::min()));
114
115 if (it == nexthops.end()) {
116 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
117 this->rejectPendingInterest(pitEntry);
118 return;
119 }
120
121 shared_ptr<Face> outFace = it->getFace();
122 this->sendInterest(pitEntry, outFace);
123 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
124 << " newPitEntry-to=" << outFace->getId());
125 return;
126 }
127
Junxiao Shia788e252015-01-28 17:06:25 -0700128 if (suppression == RetxSuppression::SUPPRESS) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700129 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shi192af1f2015-01-13 23:19:39 -0700130 << " suppressed");
Junxiao Shi986b8492014-08-20 12:07:14 -0700131 return;
132 }
133
134 // find an unused upstream with lowest cost except downstream
135 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi192af1f2015-01-13 23:19:39 -0700136 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
137 true, time::steady_clock::now()));
Junxiao Shi986b8492014-08-20 12:07:14 -0700138 if (it != nexthops.end()) {
139 shared_ptr<Face> outFace = it->getFace();
140 this->sendInterest(pitEntry, outFace);
141 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
142 << " retransmit-unused-to=" << outFace->getId());
143 return;
144 }
145
146 // find an eligible upstream that is used earliest
147 it = findEligibleNextHopWithEarliestOutRecord(pitEntry, nexthops, inFace.getId());
148 if (it == nexthops.end()) {
149 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
150 }
151 else {
152 shared_ptr<Face> outFace = it->getFace();
153 this->sendInterest(pitEntry, outFace);
154 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
155 << " retransmit-retry-to=" << outFace->getId());
156 }
157}
158
159} // namespace fw
160} // namespace nfd