blob: bd585905003ad684931d7f271bd342091495c583 [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 Shie93d6a32014-09-07 16:13:22 -070034const Name BestRouteStrategy2::STRATEGY_NAME("ndn:/localhost/nfd/strategy/best-route/%FD%02");
Junxiao Shi986b8492014-08-20 12:07:14 -070035
36BestRouteStrategy2::BestRouteStrategy2(Forwarder& forwarder, const Name& name)
37 : Strategy(forwarder, name)
38{
39}
40
41/** \brief determines whether a NextHop is eligible
42 * \param currentDownstream incoming FaceId of current Interest
43 * \param wantUnused if true, NextHop must not have unexpired OutRecord
44 * \param now time::steady_clock::now(), ignored if !wantUnused
45 */
46static inline bool
47predicate_NextHop_eligible(const shared_ptr<pit::Entry>& pitEntry,
48 const fib::NextHop& nexthop, FaceId currentDownstream,
49 bool wantUnused = false,
50 time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
51{
52 shared_ptr<Face> upstream = nexthop.getFace();
53
54 // upstream is current downstream
55 if (upstream->getId() == currentDownstream)
56 return false;
57
58 // forwarding would violate scope
59 if (pitEntry->violatesScope(*upstream))
60 return false;
61
62 if (wantUnused) {
63 // NextHop must not have unexpired OutRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070064 pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*upstream);
Junxiao Shi986b8492014-08-20 12:07:14 -070065 if (outRecord != pitEntry->getOutRecords().end() &&
66 outRecord->getExpiry() > now) {
67 return false;
68 }
69 }
70
71 return true;
72}
73
Junxiao Shi986b8492014-08-20 12:07:14 -070074/** \brief pick an eligible NextHop with earliest OutRecord
75 * \note It is assumed that every nexthop has an OutRecord
76 */
77static inline fib::NextHopList::const_iterator
78findEligibleNextHopWithEarliestOutRecord(const shared_ptr<pit::Entry>& pitEntry,
79 const fib::NextHopList& nexthops,
80 FaceId currentDownstream)
81{
82 fib::NextHopList::const_iterator found = nexthops.end();
83 time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max();
84 for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
85 if (!predicate_NextHop_eligible(pitEntry, *it, currentDownstream))
86 continue;
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070087 pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*it->getFace());
Junxiao Shi986b8492014-08-20 12:07:14 -070088 BOOST_ASSERT(outRecord != pitEntry->getOutRecords().end());
89 if (outRecord->getLastRenewed() < earliestRenewed) {
90 found = it;
91 earliestRenewed = outRecord->getLastRenewed();
92 }
93 }
94 return found;
95}
96
97void
98BestRouteStrategy2::afterReceiveInterest(const Face& inFace,
99 const Interest& interest,
100 shared_ptr<fib::Entry> fibEntry,
101 shared_ptr<pit::Entry> pitEntry)
102{
103 const fib::NextHopList& nexthops = fibEntry->getNextHops();
104 fib::NextHopList::const_iterator it = nexthops.end();
105
Junxiao Shi192af1f2015-01-13 23:19:39 -0700106 RetransmissionSuppression::Result suppression =
107 m_retransmissionSuppression.decide(inFace, interest, *pitEntry);
108 if (suppression == RetransmissionSuppression::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");
116 this->rejectPendingInterest(pitEntry);
117 return;
118 }
119
120 shared_ptr<Face> outFace = it->getFace();
121 this->sendInterest(pitEntry, outFace);
122 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
123 << " newPitEntry-to=" << outFace->getId());
124 return;
125 }
126
Junxiao Shi192af1f2015-01-13 23:19:39 -0700127 if (suppression == RetransmissionSuppression::SUPPRESS) {
Junxiao Shi986b8492014-08-20 12:07:14 -0700128 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
Junxiao Shi192af1f2015-01-13 23:19:39 -0700129 << " suppressed");
Junxiao Shi986b8492014-08-20 12:07:14 -0700130 return;
131 }
132
133 // find an unused upstream with lowest cost except downstream
134 it = std::find_if(nexthops.begin(), nexthops.end(),
Junxiao Shi192af1f2015-01-13 23:19:39 -0700135 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
136 true, time::steady_clock::now()));
Junxiao Shi986b8492014-08-20 12:07:14 -0700137 if (it != nexthops.end()) {
138 shared_ptr<Face> outFace = it->getFace();
139 this->sendInterest(pitEntry, outFace);
140 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
141 << " retransmit-unused-to=" << outFace->getId());
142 return;
143 }
144
145 // find an eligible upstream that is used earliest
146 it = findEligibleNextHopWithEarliestOutRecord(pitEntry, nexthops, inFace.getId());
147 if (it == nexthops.end()) {
148 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
149 }
150 else {
151 shared_ptr<Face> outFace = it->getFace();
152 this->sendInterest(pitEntry, outFace);
153 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
154 << " retransmit-retry-to=" << outFace->getId());
155 }
156}
157
158} // namespace fw
159} // namespace nfd