blob: ace5426dda25fa369ceb388f98eb29975104cc03 [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 Shi00dc9142016-11-21 14:23:12 +000027#include "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
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070045void
Junxiao Shi15e98b02016-08-12 11:21:44 +000046AccessStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
47 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070048{
Junxiao Shia788e252015-01-28 17:06:25 -070049 RetxSuppression::Result suppressResult = m_retxSuppression.decide(inFace, interest, *pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070050 switch (suppressResult) {
Junxiao Shia788e252015-01-28 17:06:25 -070051 case RetxSuppression::NEW:
Junxiao Shi8d843142016-07-11 22:42:42 +000052 this->afterReceiveNewInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070053 break;
Junxiao Shia788e252015-01-28 17:06:25 -070054 case RetxSuppression::FORWARD:
Junxiao Shi8d843142016-07-11 22:42:42 +000055 this->afterReceiveRetxInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070056 break;
Junxiao Shia788e252015-01-28 17:06:25 -070057 case RetxSuppression::SUPPRESS:
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070058 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-suppress");
59 break;
60 default:
61 BOOST_ASSERT(false);
62 break;
63 }
64}
65
66void
Junxiao Shi15e98b02016-08-12 11:21:44 +000067AccessStrategy::afterReceiveNewInterest(const Face& inFace, const Interest& interest,
68 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070069{
Junxiao Shi8d843142016-07-11 22:42:42 +000070 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070071 Name miName;
Junxiao Shifc021862016-08-25 21:51:18 +000072 MtInfo* mi = nullptr;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070073 std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
74
75 // has measurements for Interest Name?
76 if (mi != nullptr) {
77 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
78 " new-interest mi=" << miName);
79
80 // send to last working nexthop
Junxiao Shia7f9a292016-11-22 16:31:38 +000081 bool isSentToLastNexthop = this->sendToLastNexthop(inFace, interest, pitEntry, *mi, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070082
83 if (isSentToLastNexthop) {
84 return;
85 }
86 }
87 else {
88 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
89 " new-interest no-mi");
90 }
91
92 // no measurements, or last working nexthop unavailable
93
Junxiao Shi965d3a42015-06-01 06:55:23 -070094 // multicast to all nexthops except incoming face
Junxiao Shia7f9a292016-11-22 16:31:38 +000095 int nMulticastSent = this->multicast(inFace, interest, pitEntry, fibEntry);
96
97 if (nMulticastSent < 1) {
98 this->rejectPendingInterest(pitEntry);
99 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700100}
101
102void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000103AccessStrategy::afterReceiveRetxInterest(const Face& inFace, const Interest& interest,
104 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700105{
Junxiao Shi8d843142016-07-11 22:42:42 +0000106 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700107 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-forward");
Junxiao Shia7f9a292016-11-22 16:31:38 +0000108 this->multicast(inFace, interest, pitEntry, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700109}
110
111bool
Junxiao Shia7f9a292016-11-22 16:31:38 +0000112AccessStrategy::sendToLastNexthop(const Face& inFace, const Interest& interest,
113 const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
114 const fib::Entry& fibEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700115{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700116 if (mi.lastNexthop == face::INVALID_FACEID) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700117 NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
118 return false;
119 }
120
121 if (mi.lastNexthop == inFace.getId()) {
122 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
123 return false;
124 }
125
Junxiao Shia7f9a292016-11-22 16:31:38 +0000126 Face* outFace = this->getFace(mi.lastNexthop);
127 if (outFace == nullptr || !fibEntry.hasNextHop(*outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700128 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
129 return false;
130 }
131
Junxiao Shia7f9a292016-11-22 16:31:38 +0000132 if (wouldViolateScope(inFace, interest, *outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700133 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
134 return false;
135 }
136
137 RttEstimator::Duration rto = mi.rtt.computeRto();
138 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop <<
139 " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
140
Junxiao Shia7f9a292016-11-22 16:31:38 +0000141 this->sendInterest(pitEntry, *outFace, interest);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700142
143 // schedule RTO timeout
Junxiao Shifc021862016-08-25 21:51:18 +0000144 PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700145 pi->rtoTimer = scheduler::schedule(rto,
146 bind(&AccessStrategy::afterRtoTimeout, this, weak_ptr<pit::Entry>(pitEntry),
Junxiao Shi8d843142016-07-11 22:42:42 +0000147 inFace.getId(), mi.lastNexthop));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700148
149 return true;
150}
151
152void
Junxiao Shia7f9a292016-11-22 16:31:38 +0000153AccessStrategy::afterRtoTimeout(weak_ptr<pit::Entry> pitWeak, FaceId inFaceId, FaceId firstOutFaceId)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700154{
155 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
156 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000157 // if pitEntry is gone, RTO timer should have been cancelled
158
159 Face* inFace = this->getFace(inFaceId);
160 if (inFace == nullptr) {
161 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
162 " inFace-gone " << inFaceId);
163 return;
164 }
165
166 pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(*inFace);
167 BOOST_ASSERT(inRecord != pitEntry->in_end());
168 // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
169 // note: if this strategy is extended to send Nacks, that would also erase in-record,
170 // and RTO timer should be cancelled in that case as well
171
172 const Interest& interest = inRecord->getInterest();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700173
Junxiao Shi8d843142016-07-11 22:42:42 +0000174 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700175
Junxiao Shia7f9a292016-11-22 16:31:38 +0000176 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
177 " multicast-except " << firstOutFaceId);
178 this->multicast(*inFace, interest, pitEntry, fibEntry, firstOutFaceId);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700179}
180
Junxiao Shia7f9a292016-11-22 16:31:38 +0000181int
182AccessStrategy::multicast(const Face& inFace, const Interest& interest,
183 const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
184 FaceId exceptFace)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700185{
Junxiao Shia7f9a292016-11-22 16:31:38 +0000186 int nSent = 0;
Junxiao Shi8d843142016-07-11 22:42:42 +0000187 for (const fib::NextHop& nexthop : fibEntry.getNextHops()) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000188 Face& outFace = nexthop.getFace();
189 if (&outFace == &inFace || outFace.getId() == exceptFace ||
190 wouldViolateScope(inFace, interest, outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700191 continue;
192 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000193 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() <<
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700194 " multicast");
Junxiao Shia7f9a292016-11-22 16:31:38 +0000195 this->sendInterest(pitEntry, outFace, interest);
196 ++nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700197 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000198 return nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700199}
200
201void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000202AccessStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700203 const Face& inFace, const Data& data)
204{
Junxiao Shifc021862016-08-25 21:51:18 +0000205 PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700206 if (pi != nullptr) {
207 pi->rtoTimer.cancel();
208 }
209
Junxiao Shi4846f372016-04-05 13:39:30 -0700210 if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700211 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
212 " not-fastest");
213 return;
214 }
215
Junxiao Shi4846f372016-04-05 13:39:30 -0700216 pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
217 if (outRecord == pitEntry->out_end()) { // no out-record
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700218 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
219 " no-out-record");
220 return;
221 }
222
223 time::steady_clock::Duration rtt = time::steady_clock::now() - outRecord->getLastRenewed();
224 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
225 " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
226 this->updateMeasurements(inFace, data, time::duration_cast<RttEstimator::Duration>(rtt));
227}
228
229void
230AccessStrategy::updateMeasurements(const Face& inFace, const Data& data,
231 const RttEstimator::Duration& rtt)
232{
233 FaceInfo& fi = m_fit[inFace.getId()];
234 fi.rtt.addMeasurement(rtt);
235
Junxiao Shifc021862016-08-25 21:51:18 +0000236 MtInfo* mi = this->addPrefixMeasurements(data);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700237 if (mi->lastNexthop != inFace.getId()) {
238 mi->lastNexthop = inFace.getId();
239 mi->rtt = fi.rtt;
240 }
241 else {
242 mi->rtt.addMeasurement(rtt);
243 }
244}
245
246AccessStrategy::MtInfo::MtInfo()
Junxiao Shicde37ad2015-12-24 01:02:05 -0700247 : lastNexthop(face::INVALID_FACEID)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700248 , rtt(1, time::milliseconds(1), 0.1)
249{
250}
251
Junxiao Shifc021862016-08-25 21:51:18 +0000252std::tuple<Name, AccessStrategy::MtInfo*>
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700253AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
254{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000255 measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700256 if (me == nullptr) {
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000257 return std::make_tuple(Name(), nullptr);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700258 }
259
Junxiao Shifc021862016-08-25 21:51:18 +0000260 MtInfo* mi = me->getStrategyInfo<MtInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700261 BOOST_ASSERT(mi != nullptr);
262 // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
263 // this case needs another longest prefix match until mi is found
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000264 return std::make_tuple(me->getName(), mi);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700265}
266
Junxiao Shifc021862016-08-25 21:51:18 +0000267AccessStrategy::MtInfo*
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700268AccessStrategy::addPrefixMeasurements(const Data& data)
269{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000270 measurements::Entry* me = nullptr;
271 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700272 me = this->getMeasurements().get(data.getName().getPrefix(-1));
273 }
274 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
275 me = this->getMeasurements().get(data.getName());
276 // Data Name must be in this strategy
277 BOOST_ASSERT(me != nullptr);
278 }
279
Junxiao Shi7b0f4d12015-05-10 21:40:29 -0700280 static const time::nanoseconds ME_LIFETIME = time::seconds(8);
281 this->getMeasurements().extendLifetime(*me, ME_LIFETIME);
282
Junxiao Shifc021862016-08-25 21:51:18 +0000283 return me->insertStrategyInfo<MtInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700284}
285
286AccessStrategy::FaceInfo::FaceInfo()
287 : rtt(1, time::milliseconds(1), 0.1)
288{
289}
290
291void
Junxiao Shiae04d342016-07-19 13:20:22 +0000292AccessStrategy::removeFaceInfo(const Face& face)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700293{
Junxiao Shiae04d342016-07-19 13:20:22 +0000294 m_fit.erase(face.getId());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700295}
296
297} // namespace fw
298} // namespace nfd