blob: b07211b6aa79ff878094259ad075c21d2d4424c5 [file] [log] [blame]
Junxiao Shi80ee7cb2014-12-14 10:53:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande90015992017-07-11 17:25:48 -05002/*
ashiqopu3ad49db2018-10-20 22:38:47 +00003 * Copyright (c) 2014-2019, 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
Davide Pesaventoa3148082018-04-12 18:21:54 -040033NFD_LOG_INIT(AccessStrategy);
Junxiao Shifaf3eb02015-02-16 10:50:36 -070034NFD_REGISTER_STRATEGY(AccessStrategy);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070035
36AccessStrategy::AccessStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi18739c42016-12-22 08:03:00 +000037 : Strategy(forwarder)
Davide Pesaventoe4b22382018-06-10 14:37:24 -040038 , m_removeFaceInfoConn(beforeRemoveFace.connect([this] (const Face& face) { removeFaceInfo(face); }))
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070039{
Junxiao Shi18739c42016-12-22 08:03:00 +000040 ParsedInstanceName parsed = parseInstanceName(name);
41 if (!parsed.parameters.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050042 NDN_THROW(std::invalid_argument("AccessStrategy does not accept parameters"));
Junxiao Shi18739c42016-12-22 08:03:00 +000043 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000044 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050045 NDN_THROW(std::invalid_argument(
Alexander Afanasyev0c63c632017-12-05 11:17:09 -050046 "AccessStrategy does not support version " + to_string(*parsed.version)));
Junxiao Shi91f6ee02016-12-29 21:44:44 +000047 }
Junxiao Shi18739c42016-12-22 08:03:00 +000048 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070049}
50
Junxiao Shi037f4ab2016-12-13 04:27:06 +000051const Name&
52AccessStrategy::getStrategyName()
53{
54 static Name strategyName("/localhost/nfd/strategy/access/%FD%01");
55 return strategyName;
56}
57
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070058void
Junxiao Shi15e98b02016-08-12 11:21:44 +000059AccessStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
60 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070061{
Ashlesh Gawande90015992017-07-11 17:25:48 -050062 RetxSuppressionResult suppressResult = m_retxSuppression.decidePerPitEntry(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070063 switch (suppressResult) {
Ashlesh Gawande90015992017-07-11 17:25:48 -050064 case RetxSuppressionResult::NEW:
Junxiao Shi8d843142016-07-11 22:42:42 +000065 this->afterReceiveNewInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070066 break;
Ashlesh Gawande90015992017-07-11 17:25:48 -050067 case RetxSuppressionResult::FORWARD:
Junxiao Shi8d843142016-07-11 22:42:42 +000068 this->afterReceiveRetxInterest(inFace, interest, pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070069 break;
Ashlesh Gawande90015992017-07-11 17:25:48 -050070 case RetxSuppressionResult::SUPPRESS:
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070071 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-suppress");
72 break;
73 default:
74 BOOST_ASSERT(false);
75 break;
76 }
77}
78
79void
Junxiao Shi15e98b02016-08-12 11:21:44 +000080AccessStrategy::afterReceiveNewInterest(const Face& inFace, const Interest& interest,
81 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070082{
Junxiao Shi8d843142016-07-11 22:42:42 +000083 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070084 Name miName;
Junxiao Shifc021862016-08-25 21:51:18 +000085 MtInfo* mi = nullptr;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070086 std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
87
88 // has measurements for Interest Name?
89 if (mi != nullptr) {
90 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
91 " new-interest mi=" << miName);
92
93 // send to last working nexthop
Junxiao Shia7f9a292016-11-22 16:31:38 +000094 bool isSentToLastNexthop = this->sendToLastNexthop(inFace, interest, pitEntry, *mi, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070095
96 if (isSentToLastNexthop) {
97 return;
98 }
99 }
100 else {
101 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
102 " new-interest no-mi");
103 }
104
105 // no measurements, or last working nexthop unavailable
106
Junxiao Shi965d3a42015-06-01 06:55:23 -0700107 // multicast to all nexthops except incoming face
Junxiao Shia7f9a292016-11-22 16:31:38 +0000108 int nMulticastSent = this->multicast(inFace, interest, pitEntry, fibEntry);
109
110 if (nMulticastSent < 1) {
111 this->rejectPendingInterest(pitEntry);
112 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700113}
114
115void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000116AccessStrategy::afterReceiveRetxInterest(const Face& inFace, const Interest& interest,
117 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700118{
Junxiao Shi8d843142016-07-11 22:42:42 +0000119 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700120 NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-forward");
Junxiao Shia7f9a292016-11-22 16:31:38 +0000121 this->multicast(inFace, interest, pitEntry, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700122}
123
124bool
Junxiao Shia7f9a292016-11-22 16:31:38 +0000125AccessStrategy::sendToLastNexthop(const Face& inFace, const Interest& interest,
126 const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
127 const fib::Entry& fibEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700128{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700129 if (mi.lastNexthop == face::INVALID_FACEID) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700130 NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
131 return false;
132 }
133
134 if (mi.lastNexthop == inFace.getId()) {
135 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
136 return false;
137 }
138
Junxiao Shia7f9a292016-11-22 16:31:38 +0000139 Face* outFace = this->getFace(mi.lastNexthop);
ashiqopu3ad49db2018-10-20 22:38:47 +0000140 if (outFace == nullptr || !fibEntry.hasNextHop(*outFace, 0)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700141 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
142 return false;
143 }
144
Junxiao Shia7f9a292016-11-22 16:31:38 +0000145 if (wouldViolateScope(inFace, interest, *outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700146 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
147 return false;
148 }
149
150 RttEstimator::Duration rto = mi.rtt.computeRto();
151 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop <<
152 " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
153
Junxiao Shia7f9a292016-11-22 16:31:38 +0000154 this->sendInterest(pitEntry, *outFace, interest);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700155
156 // schedule RTO timeout
Junxiao Shifc021862016-08-25 21:51:18 +0000157 PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700158 pi->rtoTimer = scheduler::schedule(rto,
159 bind(&AccessStrategy::afterRtoTimeout, this, weak_ptr<pit::Entry>(pitEntry),
Junxiao Shi8d843142016-07-11 22:42:42 +0000160 inFace.getId(), mi.lastNexthop));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700161
162 return true;
163}
164
165void
Junxiao Shia7f9a292016-11-22 16:31:38 +0000166AccessStrategy::afterRtoTimeout(weak_ptr<pit::Entry> pitWeak, FaceId inFaceId, FaceId firstOutFaceId)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700167{
168 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
169 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000170 // if pitEntry is gone, RTO timer should have been cancelled
171
172 Face* inFace = this->getFace(inFaceId);
173 if (inFace == nullptr) {
174 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
175 " inFace-gone " << inFaceId);
176 return;
177 }
178
ashiqopud3ae85d2019-02-17 02:29:55 +0000179 auto inRecord = pitEntry->getInRecord(*inFace, 0);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000180 BOOST_ASSERT(inRecord != pitEntry->in_end());
181 // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
182 // note: if this strategy is extended to send Nacks, that would also erase in-record,
183 // and RTO timer should be cancelled in that case as well
184
185 const Interest& interest = inRecord->getInterest();
Junxiao Shi8d843142016-07-11 22:42:42 +0000186 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700187
Junxiao Shia7f9a292016-11-22 16:31:38 +0000188 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
189 " multicast-except " << firstOutFaceId);
190 this->multicast(*inFace, interest, pitEntry, fibEntry, firstOutFaceId);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700191}
192
Junxiao Shia7f9a292016-11-22 16:31:38 +0000193int
194AccessStrategy::multicast(const Face& inFace, const Interest& interest,
195 const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
196 FaceId exceptFace)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700197{
Junxiao Shia7f9a292016-11-22 16:31:38 +0000198 int nSent = 0;
Junxiao Shi8d843142016-07-11 22:42:42 +0000199 for (const fib::NextHop& nexthop : fibEntry.getNextHops()) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000200 Face& outFace = nexthop.getFace();
201 if (&outFace == &inFace || outFace.getId() == exceptFace ||
202 wouldViolateScope(inFace, interest, outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700203 continue;
204 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000205 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() <<
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700206 " multicast");
Junxiao Shia7f9a292016-11-22 16:31:38 +0000207 this->sendInterest(pitEntry, outFace, interest);
208 ++nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700209 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000210 return nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700211}
212
213void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000214AccessStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700215 const Face& inFace, const Data& data)
216{
Junxiao Shifc021862016-08-25 21:51:18 +0000217 PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700218 if (pi != nullptr) {
219 pi->rtoTimer.cancel();
220 }
221
Junxiao Shi4846f372016-04-05 13:39:30 -0700222 if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700223 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
224 " not-fastest");
225 return;
226 }
227
ashiqopud3ae85d2019-02-17 02:29:55 +0000228 auto outRecord = pitEntry->getOutRecord(inFace, 0);
Junxiao Shi4846f372016-04-05 13:39:30 -0700229 if (outRecord == pitEntry->out_end()) { // no out-record
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700230 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
231 " no-out-record");
232 return;
233 }
234
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400235 auto rtt = time::steady_clock::now() - outRecord->getLastRenewed();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700236 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
237 " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
238 this->updateMeasurements(inFace, data, time::duration_cast<RttEstimator::Duration>(rtt));
239}
240
241void
242AccessStrategy::updateMeasurements(const Face& inFace, const Data& data,
243 const RttEstimator::Duration& rtt)
244{
Junxiao Shif15058a2016-12-11 20:15:05 +0000245 ///\todo move FaceInfoTable out of AccessStrategy instance, to Measurements or somewhere else
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700246 FaceInfo& fi = m_fit[inFace.getId()];
247 fi.rtt.addMeasurement(rtt);
248
Junxiao Shifc021862016-08-25 21:51:18 +0000249 MtInfo* mi = this->addPrefixMeasurements(data);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700250 if (mi->lastNexthop != inFace.getId()) {
251 mi->lastNexthop = inFace.getId();
252 mi->rtt = fi.rtt;
253 }
254 else {
255 mi->rtt.addMeasurement(rtt);
256 }
257}
258
259AccessStrategy::MtInfo::MtInfo()
Junxiao Shicde37ad2015-12-24 01:02:05 -0700260 : lastNexthop(face::INVALID_FACEID)
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400261 , rtt(1, 1_ms, 0.1)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700262{
263}
264
Junxiao Shifc021862016-08-25 21:51:18 +0000265std::tuple<Name, AccessStrategy::MtInfo*>
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700266AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
267{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000268 measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700269 if (me == nullptr) {
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000270 return std::make_tuple(Name(), nullptr);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700271 }
272
Junxiao Shifc021862016-08-25 21:51:18 +0000273 MtInfo* mi = me->getStrategyInfo<MtInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700274 BOOST_ASSERT(mi != nullptr);
275 // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
276 // this case needs another longest prefix match until mi is found
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000277 return std::make_tuple(me->getName(), mi);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700278}
279
Junxiao Shifc021862016-08-25 21:51:18 +0000280AccessStrategy::MtInfo*
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700281AccessStrategy::addPrefixMeasurements(const Data& data)
282{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000283 measurements::Entry* me = nullptr;
284 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700285 me = this->getMeasurements().get(data.getName().getPrefix(-1));
286 }
287 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
288 me = this->getMeasurements().get(data.getName());
289 // Data Name must be in this strategy
290 BOOST_ASSERT(me != nullptr);
291 }
292
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400293 this->getMeasurements().extendLifetime(*me, 8_s);
Junxiao Shi7b0f4d12015-05-10 21:40:29 -0700294
Junxiao Shifc021862016-08-25 21:51:18 +0000295 return me->insertStrategyInfo<MtInfo>().first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700296}
297
298AccessStrategy::FaceInfo::FaceInfo()
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400299 : rtt(1, 1_ms, 0.1)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700300{
301}
302
303void
Junxiao Shiae04d342016-07-19 13:20:22 +0000304AccessStrategy::removeFaceInfo(const Face& face)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700305{
Junxiao Shiae04d342016-07-19 13:20:22 +0000306 m_fit.erase(face.getId());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700307}
308
309} // namespace fw
310} // namespace nfd