blob: 8d531681283915b7177a6e272e56b7db895cb7ac [file] [log] [blame]
Junxiao Shi80ee7cb2014-12-14 10:53:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shifef73e42016-03-29 14:15:05 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -07004 * 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 "access-strategy.hpp"
Junxiao Shifef73e42016-03-29 14:15:05 -070027#include "pit-algorithm.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070028#include "core/logger.hpp"
29
30namespace nfd {
31namespace fw {
32
33NFD_LOG_INIT("AccessStrategy");
34
35const Name AccessStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/access/%FD%01");
Junxiao Shifaf3eb02015-02-16 10:50:36 -070036NFD_REGISTER_STRATEGY(AccessStrategy);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070037
38AccessStrategy::AccessStrategy(Forwarder& forwarder, const Name& name)
39 : Strategy(forwarder, name)
40 , m_removeFaceInfoConn(this->beforeRemoveFace.connect(
41 bind(&AccessStrategy::removeFaceInfo, this, _1)))
42{
43}
44
45AccessStrategy::~AccessStrategy()
46{
47}
48
49void
50AccessStrategy::afterReceiveInterest(const Face& inFace,
51 const Interest& interest,
52 shared_ptr<fib::Entry> fibEntry,
53 shared_ptr<pit::Entry> pitEntry)
54{
Junxiao Shia788e252015-01-28 17:06:25 -070055 RetxSuppression::Result suppressResult = m_retxSuppression.decide(inFace, interest, *pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070056 switch (suppressResult) {
Junxiao Shia788e252015-01-28 17:06:25 -070057 case RetxSuppression::NEW:
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070058 this->afterReceiveNewInterest(inFace, interest, fibEntry, pitEntry);
59 break;
Junxiao Shia788e252015-01-28 17:06:25 -070060 case RetxSuppression::FORWARD:
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070061 this->afterReceiveRetxInterest(inFace, interest, fibEntry, pitEntry);
62 break;
Junxiao Shia788e252015-01-28 17:06:25 -070063 case RetxSuppression::SUPPRESS:
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070064 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-suppress");
65 break;
66 default:
67 BOOST_ASSERT(false);
68 break;
69 }
70}
71
72void
73AccessStrategy::afterReceiveNewInterest(const Face& inFace,
74 const Interest& interest,
75 shared_ptr<fib::Entry> fibEntry,
76 shared_ptr<pit::Entry> pitEntry)
77{
78 Name miName;
79 shared_ptr<MtInfo> mi;
80 std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
81
82 // has measurements for Interest Name?
83 if (mi != nullptr) {
84 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
85 " new-interest mi=" << miName);
86
87 // send to last working nexthop
88 bool isSentToLastNexthop = this->sendToLastNexthop(inFace, pitEntry, *mi, fibEntry);
89
90 if (isSentToLastNexthop) {
91 return;
92 }
93 }
94 else {
95 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
96 " new-interest no-mi");
97 }
98
99 // no measurements, or last working nexthop unavailable
100
Junxiao Shi965d3a42015-06-01 06:55:23 -0700101 // multicast to all nexthops except incoming face
102 this->multicast(pitEntry, fibEntry, {inFace.getId()});
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700103}
104
105void
106AccessStrategy::afterReceiveRetxInterest(const Face& inFace,
107 const Interest& interest,
108 shared_ptr<fib::Entry> fibEntry,
109 shared_ptr<pit::Entry> pitEntry)
110{
111 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-forward");
Junxiao Shi965d3a42015-06-01 06:55:23 -0700112 this->multicast(pitEntry, fibEntry, {inFace.getId()});
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700113}
114
115bool
116AccessStrategy::sendToLastNexthop(const Face& inFace, shared_ptr<pit::Entry> pitEntry, MtInfo& mi,
117 shared_ptr<fib::Entry> fibEntry)
118{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700119 if (mi.lastNexthop == face::INVALID_FACEID) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700120 NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
121 return false;
122 }
123
124 if (mi.lastNexthop == inFace.getId()) {
125 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
126 return false;
127 }
128
129 shared_ptr<Face> face = this->getFace(mi.lastNexthop);
130 if (face == nullptr || !fibEntry->hasNextHop(face)) {
131 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
132 return false;
133 }
134
Junxiao Shifef73e42016-03-29 14:15:05 -0700135 if (violatesScope(*pitEntry, *face)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700136 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
137 return false;
138 }
139
140 RttEstimator::Duration rto = mi.rtt.computeRto();
141 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop <<
142 " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
143
144 this->sendInterest(pitEntry, face);
145
146 // schedule RTO timeout
147 shared_ptr<PitInfo> pi = pitEntry->getOrCreateStrategyInfo<PitInfo>();
148 pi->rtoTimer = scheduler::schedule(rto,
149 bind(&AccessStrategy::afterRtoTimeout, this, weak_ptr<pit::Entry>(pitEntry),
150 weak_ptr<fib::Entry>(fibEntry), inFace.getId(), mi.lastNexthop));
151
152 return true;
153}
154
155void
156AccessStrategy::afterRtoTimeout(weak_ptr<pit::Entry> pitWeak, weak_ptr<fib::Entry> fibWeak,
157 FaceId inFace, FaceId firstOutFace)
158{
159 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
160 BOOST_ASSERT(pitEntry != nullptr);
161 // pitEntry can't become nullptr, because RTO timer should be cancelled upon pitEntry destruction
162
163 shared_ptr<fib::Entry> fibEntry = fibWeak.lock();
164 if (fibEntry == nullptr) {
165 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFace << " fib-gone");
166 return;
167 }
168
169 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFace <<
170 " multicast-except " << inFace << ',' << firstOutFace);
Junxiao Shi965d3a42015-06-01 06:55:23 -0700171 this->multicast(pitEntry, fibEntry, {inFace, firstOutFace});
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700172}
173
174void
175AccessStrategy::multicast(shared_ptr<pit::Entry> pitEntry, shared_ptr<fib::Entry> fibEntry,
176 std::unordered_set<FaceId> exceptFaces)
177{
178 for (const fib::NextHop& nexthop : fibEntry->getNextHops()) {
179 shared_ptr<Face> face = nexthop.getFace();
180 if (exceptFaces.count(face->getId()) > 0) {
181 continue;
182 }
183 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << face->getId() <<
184 " multicast");
185 this->sendInterest(pitEntry, face);
186 }
187}
188
189void
190AccessStrategy::beforeSatisfyInterest(shared_ptr<pit::Entry> pitEntry,
191 const Face& inFace, const Data& data)
192{
193 shared_ptr<PitInfo> pi = pitEntry->getStrategyInfo<PitInfo>();
194 if (pi != nullptr) {
195 pi->rtoTimer.cancel();
196 }
197
198 if (pitEntry->getInRecords().empty()) { // already satisfied by another upstream
199 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
200 " not-fastest");
201 return;
202 }
203
204 pit::OutRecordCollection::const_iterator outRecord = pitEntry->getOutRecord(inFace);
205 if (outRecord == pitEntry->getOutRecords().end()) { // no OutRecord
206 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
207 " no-out-record");
208 return;
209 }
210
211 time::steady_clock::Duration rtt = time::steady_clock::now() - outRecord->getLastRenewed();
212 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
213 " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
214 this->updateMeasurements(inFace, data, time::duration_cast<RttEstimator::Duration>(rtt));
215}
216
217void
218AccessStrategy::updateMeasurements(const Face& inFace, const Data& data,
219 const RttEstimator::Duration& rtt)
220{
221 FaceInfo& fi = m_fit[inFace.getId()];
222 fi.rtt.addMeasurement(rtt);
223
224 shared_ptr<MtInfo> mi = this->addPrefixMeasurements(data);
225 if (mi->lastNexthop != inFace.getId()) {
226 mi->lastNexthop = inFace.getId();
227 mi->rtt = fi.rtt;
228 }
229 else {
230 mi->rtt.addMeasurement(rtt);
231 }
232}
233
234AccessStrategy::MtInfo::MtInfo()
Junxiao Shicde37ad2015-12-24 01:02:05 -0700235 : lastNexthop(face::INVALID_FACEID)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700236 , rtt(1, time::milliseconds(1), 0.1)
237{
238}
239
240std::tuple<Name, shared_ptr<AccessStrategy::MtInfo>>
241AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
242{
243 shared_ptr<measurements::Entry> me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
244 if (me == nullptr) {
245 return std::forward_as_tuple(Name(), nullptr);
246 }
247
248 shared_ptr<MtInfo> mi = me->getStrategyInfo<MtInfo>();
249 BOOST_ASSERT(mi != nullptr);
250 // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
251 // this case needs another longest prefix match until mi is found
252 return std::forward_as_tuple(me->getName(), mi);
253}
254
255shared_ptr<AccessStrategy::MtInfo>
256AccessStrategy::addPrefixMeasurements(const Data& data)
257{
258 shared_ptr<measurements::Entry> me;
259 if (data.getName().size() >= 1) {
260 me = this->getMeasurements().get(data.getName().getPrefix(-1));
261 }
262 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
263 me = this->getMeasurements().get(data.getName());
264 // Data Name must be in this strategy
265 BOOST_ASSERT(me != nullptr);
266 }
267
Junxiao Shi7b0f4d12015-05-10 21:40:29 -0700268 static const time::nanoseconds ME_LIFETIME = time::seconds(8);
269 this->getMeasurements().extendLifetime(*me, ME_LIFETIME);
270
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700271 return me->getOrCreateStrategyInfo<MtInfo>();
272}
273
274AccessStrategy::FaceInfo::FaceInfo()
275 : rtt(1, time::milliseconds(1), 0.1)
276{
277}
278
279void
280AccessStrategy::removeFaceInfo(shared_ptr<Face> face)
281{
282 m_fit.erase(face->getId());
283}
284
285} // namespace fw
286} // namespace nfd