blob: 044f5e95a82912eb7bd953a0f199eb604034de9c [file] [log] [blame]
Junxiao Shi986b8492014-08-20 12:07:14 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, 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
10 *
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/// \todo don't use fixed interval; make it adaptive or use exponential back-off #1913
36const time::milliseconds BestRouteStrategy2::MIN_RETRANSMISSION_INTERVAL(100);
37
38BestRouteStrategy2::BestRouteStrategy2(Forwarder& forwarder, const Name& name)
39 : Strategy(forwarder, name)
40{
41}
42
43/** \brief determines whether a NextHop is eligible
44 * \param currentDownstream incoming FaceId of current Interest
45 * \param wantUnused if true, NextHop must not have unexpired OutRecord
46 * \param now time::steady_clock::now(), ignored if !wantUnused
47 */
48static inline bool
49predicate_NextHop_eligible(const shared_ptr<pit::Entry>& pitEntry,
50 const fib::NextHop& nexthop, FaceId currentDownstream,
51 bool wantUnused = false,
52 time::steady_clock::TimePoint now = time::steady_clock::TimePoint::min())
53{
54 shared_ptr<Face> upstream = nexthop.getFace();
55
56 // upstream is current downstream
57 if (upstream->getId() == currentDownstream)
58 return false;
59
60 // forwarding would violate scope
61 if (pitEntry->violatesScope(*upstream))
62 return false;
63
64 if (wantUnused) {
65 // NextHop must not have unexpired OutRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070066 pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*upstream);
Junxiao Shi986b8492014-08-20 12:07:14 -070067 if (outRecord != pitEntry->getOutRecords().end() &&
68 outRecord->getExpiry() > now) {
69 return false;
70 }
71 }
72
73 return true;
74}
75
76static inline bool
77compare_OutRecord_lastRenewed(const pit::OutRecord& a, const pit::OutRecord& b)
78{
79 return a.getLastRenewed() < b.getLastRenewed();
80}
81
82/** \brief pick an eligible NextHop with earliest OutRecord
83 * \note It is assumed that every nexthop has an OutRecord
84 */
85static inline fib::NextHopList::const_iterator
86findEligibleNextHopWithEarliestOutRecord(const shared_ptr<pit::Entry>& pitEntry,
87 const fib::NextHopList& nexthops,
88 FaceId currentDownstream)
89{
90 fib::NextHopList::const_iterator found = nexthops.end();
91 time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max();
92 for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
93 if (!predicate_NextHop_eligible(pitEntry, *it, currentDownstream))
94 continue;
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070095 pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(*it->getFace());
Junxiao Shi986b8492014-08-20 12:07:14 -070096 BOOST_ASSERT(outRecord != pitEntry->getOutRecords().end());
97 if (outRecord->getLastRenewed() < earliestRenewed) {
98 found = it;
99 earliestRenewed = outRecord->getLastRenewed();
100 }
101 }
102 return found;
103}
104
105void
106BestRouteStrategy2::afterReceiveInterest(const Face& inFace,
107 const Interest& interest,
108 shared_ptr<fib::Entry> fibEntry,
109 shared_ptr<pit::Entry> pitEntry)
110{
111 const fib::NextHopList& nexthops = fibEntry->getNextHops();
112 fib::NextHopList::const_iterator it = nexthops.end();
113
114 bool isNewPitEntry = !pitEntry->hasUnexpiredOutRecords();
115 if (isNewPitEntry) {
116 // forward to nexthop with lowest cost except downstream
117 it = std::find_if(nexthops.begin(), nexthops.end(),
118 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(),
119 false, time::steady_clock::TimePoint::min()));
120
121 if (it == nexthops.end()) {
122 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
123 this->rejectPendingInterest(pitEntry);
124 return;
125 }
126
127 shared_ptr<Face> outFace = it->getFace();
128 this->sendInterest(pitEntry, outFace);
129 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
130 << " newPitEntry-to=" << outFace->getId());
131 return;
132 }
133
134 // when was the last outgoing Interest?
135 const pit::OutRecordCollection& outRecords = pitEntry->getOutRecords();
136 pit::OutRecordCollection::const_iterator lastOutgoing = std::max_element(
137 outRecords.begin(), outRecords.end(), &compare_OutRecord_lastRenewed);
138 BOOST_ASSERT(lastOutgoing != outRecords.end()); // otherwise it's new PIT entry
139
140 time::steady_clock::TimePoint now = time::steady_clock::now();
141 time::steady_clock::Duration sinceLastOutgoing = now - lastOutgoing->getLastRenewed();
142 bool shouldRetransmit = sinceLastOutgoing >= MIN_RETRANSMISSION_INTERVAL;
143 if (!shouldRetransmit) {
144 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
145 << " dontRetransmit sinceLastOutgoing=" << sinceLastOutgoing.count());
146 return;
147 }
148
149 // find an unused upstream with lowest cost except downstream
150 it = std::find_if(nexthops.begin(), nexthops.end(),
151 bind(&predicate_NextHop_eligible, pitEntry, _1, inFace.getId(), true, now));
152 if (it != nexthops.end()) {
153 shared_ptr<Face> outFace = it->getFace();
154 this->sendInterest(pitEntry, outFace);
155 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
156 << " retransmit-unused-to=" << outFace->getId());
157 return;
158 }
159
160 // find an eligible upstream that is used earliest
161 it = findEligibleNextHopWithEarliestOutRecord(pitEntry, nexthops, inFace.getId());
162 if (it == nexthops.end()) {
163 NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " retransmitNoNextHop");
164 }
165 else {
166 shared_ptr<Face> outFace = it->getFace();
167 this->sendInterest(pitEntry, outFace);
168 NFD_LOG_DEBUG(interest << " from=" << inFace.getId()
169 << " retransmit-retry-to=" << outFace->getId());
170 }
171}
172
173} // namespace fw
174} // namespace nfd