blob: 51f7c8d159824f2cd28daa7fee33d32382f8591b [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"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/global.hpp"
29#include "common/logger.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070030
31namespace nfd {
32namespace fw {
33
Davide Pesaventoa3148082018-04-12 18:21:54 -040034NFD_LOG_INIT(AccessStrategy);
Junxiao Shifaf3eb02015-02-16 10:50:36 -070035NFD_REGISTER_STRATEGY(AccessStrategy);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070036
37AccessStrategy::AccessStrategy(Forwarder& forwarder, const Name& name)
Junxiao Shi18739c42016-12-22 08:03:00 +000038 : Strategy(forwarder)
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -040039 , m_rttEstimatorOpts(make_shared<RttEstimator::Options>()) // use the default options
40 , m_removeFaceConn(beforeRemoveFace.connect([this] (const Face& face) { m_fit.erase(face.getId()); }))
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070041{
Junxiao Shi18739c42016-12-22 08:03:00 +000042 ParsedInstanceName parsed = parseInstanceName(name);
43 if (!parsed.parameters.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050044 NDN_THROW(std::invalid_argument("AccessStrategy does not accept parameters"));
Junxiao Shi18739c42016-12-22 08:03:00 +000045 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000046 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento3dade002019-03-19 11:29:56 -060047 NDN_THROW(std::invalid_argument("AccessStrategy does not support version " + to_string(*parsed.version)));
Junxiao Shi91f6ee02016-12-29 21:44:44 +000048 }
Junxiao Shi18739c42016-12-22 08:03:00 +000049 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070050}
51
Junxiao Shi037f4ab2016-12-13 04:27:06 +000052const Name&
53AccessStrategy::getStrategyName()
54{
55 static Name strategyName("/localhost/nfd/strategy/access/%FD%01");
56 return strategyName;
57}
58
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070059void
ashiqopuc7079482019-02-20 05:34:37 +000060AccessStrategy::afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
Junxiao Shi15e98b02016-08-12 11:21:44 +000061 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070062{
Davide Pesavento17c172b2019-03-23 15:11:44 -040063 auto suppressResult = m_retxSuppression.decidePerPitEntry(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070064 switch (suppressResult) {
Ashlesh Gawande90015992017-07-11 17:25:48 -050065 case RetxSuppressionResult::NEW:
Davide Pesavento17c172b2019-03-23 15:11:44 -040066 return afterReceiveNewInterest(ingress, interest, pitEntry);
Ashlesh Gawande90015992017-07-11 17:25:48 -050067 case RetxSuppressionResult::FORWARD:
Davide Pesavento17c172b2019-03-23 15:11:44 -040068 return afterReceiveRetxInterest(ingress, interest, pitEntry);
Ashlesh Gawande90015992017-07-11 17:25:48 -050069 case RetxSuppressionResult::SUPPRESS:
ashiqopuc7079482019-02-20 05:34:37 +000070 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-suppress");
Davide Pesavento17c172b2019-03-23 15:11:44 -040071 return;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070072 }
73}
74
75void
Davide Pesavento17c172b2019-03-23 15:11:44 -040076AccessStrategy::afterReceiveNewInterest(const FaceEndpoint& ingress, const Interest& interest,
Junxiao Shi15e98b02016-08-12 11:21:44 +000077 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070078{
Davide Pesavento17c172b2019-03-23 15:11:44 -040079 const auto& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070080 Name miName;
Junxiao Shifc021862016-08-25 21:51:18 +000081 MtInfo* mi = nullptr;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070082 std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
83
84 // has measurements for Interest Name?
85 if (mi != nullptr) {
Davide Pesavento17c172b2019-03-23 15:11:44 -040086 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest mi=" << miName);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070087
88 // send to last working nexthop
Davide Pesavento17c172b2019-03-23 15:11:44 -040089 bool isSentToLastNexthop = this->sendToLastNexthop(ingress, interest, pitEntry, *mi, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070090 if (isSentToLastNexthop) {
91 return;
92 }
93 }
94 else {
Davide Pesavento17c172b2019-03-23 15:11:44 -040095 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest no-mi");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070096 }
97
98 // no measurements, or last working nexthop unavailable
99
Junxiao Shi965d3a42015-06-01 06:55:23 -0700100 // multicast to all nexthops except incoming face
Davide Pesavento17c172b2019-03-23 15:11:44 -0400101 size_t nMulticastSent = this->multicast(ingress.face, interest, pitEntry, fibEntry);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000102
Davide Pesavento17c172b2019-03-23 15:11:44 -0400103 if (nMulticastSent == 0) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000104 this->rejectPendingInterest(pitEntry);
105 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700106}
107
108void
Davide Pesavento17c172b2019-03-23 15:11:44 -0400109AccessStrategy::afterReceiveRetxInterest(const FaceEndpoint& ingress, const Interest& interest,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000110 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700111{
Davide Pesavento17c172b2019-03-23 15:11:44 -0400112 const auto& fibEntry = this->lookupFib(*pitEntry);
113 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-forward");
114 this->multicast(ingress.face, interest, pitEntry, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700115}
116
117bool
Davide Pesavento17c172b2019-03-23 15:11:44 -0400118AccessStrategy::sendToLastNexthop(const FaceEndpoint& ingress, const Interest& interest,
Junxiao Shia7f9a292016-11-22 16:31:38 +0000119 const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
120 const fib::Entry& fibEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700121{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700122 if (mi.lastNexthop == face::INVALID_FACEID) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700123 NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
124 return false;
125 }
126
Davide Pesavento17c172b2019-03-23 15:11:44 -0400127 if (mi.lastNexthop == ingress.face.getId()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700128 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
129 return false;
130 }
131
Junxiao Shia7f9a292016-11-22 16:31:38 +0000132 Face* outFace = this->getFace(mi.lastNexthop);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000133 if (outFace == nullptr || !fibEntry.hasNextHop(*outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700134 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
135 return false;
136 }
137
Davide Pesavento17c172b2019-03-23 15:11:44 -0400138 if (wouldViolateScope(ingress.face, interest, *outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700139 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
140 return false;
141 }
142
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400143 auto rto = mi.rtt.getEstimatedRto();
ashiqopuc7079482019-02-20 05:34:37 +0000144 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop
145 << " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700146
ashiqopuc7079482019-02-20 05:34:37 +0000147 this->sendInterest(pitEntry, FaceEndpoint(*outFace, 0), interest);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700148
149 // schedule RTO timeout
Junxiao Shifc021862016-08-25 21:51:18 +0000150 PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400151 pi->rtoTimer = getScheduler().schedule(rto,
152 [this, pitWeak = weak_ptr<pit::Entry>(pitEntry), face = ingress.face.getId(),
153 endpoint = ingress.endpoint, lastNexthop = mi.lastNexthop] {
154 afterRtoTimeout(pitWeak, face, endpoint, lastNexthop);
155 });
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700156
157 return true;
158}
159
160void
Davide Pesavento17c172b2019-03-23 15:11:44 -0400161AccessStrategy::afterRtoTimeout(const weak_ptr<pit::Entry>& pitWeak,
162 FaceId inFaceId, EndpointId inEndpointId, FaceId firstOutFaceId)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700163{
164 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
Davide Pesavento17c172b2019-03-23 15:11:44 -0400165 // if PIT entry is gone, RTO timer should have been cancelled
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700166 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000167
168 Face* inFace = this->getFace(inFaceId);
169 if (inFace == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000170 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
171 << " inFace-gone " << inFaceId);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000172 return;
173 }
174
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000175 auto inRecord = pitEntry->getInRecord(*inFace);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000176 // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
Davide Pesavento17c172b2019-03-23 15:11:44 -0400177 // note: if this strategy is extended to send Nacks, that would also erase the in-record,
178 // and the RTO timer should be cancelled in that case as well
179 BOOST_ASSERT(inRecord != pitEntry->in_end());
Junxiao Shia7f9a292016-11-22 16:31:38 +0000180
181 const Interest& interest = inRecord->getInterest();
Junxiao Shi8d843142016-07-11 22:42:42 +0000182 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700183
ashiqopuc7079482019-02-20 05:34:37 +0000184 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
185 << " multicast-except " << firstOutFaceId);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000186 this->multicast(*inFace, interest, pitEntry, fibEntry, firstOutFaceId);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700187}
188
Davide Pesavento17c172b2019-03-23 15:11:44 -0400189size_t
Junxiao Shia7f9a292016-11-22 16:31:38 +0000190AccessStrategy::multicast(const Face& inFace, const Interest& interest,
191 const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
192 FaceId exceptFace)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700193{
Davide Pesavento17c172b2019-03-23 15:11:44 -0400194 size_t nSent = 0;
195 for (const auto& nexthop : fibEntry.getNextHops()) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000196 Face& outFace = nexthop.getFace();
197 if (&outFace == &inFace || outFace.getId() == exceptFace ||
198 wouldViolateScope(inFace, interest, outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700199 continue;
200 }
ashiqopuc7079482019-02-20 05:34:37 +0000201 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() << " multicast");
202 this->sendInterest(pitEntry, FaceEndpoint(outFace, 0), interest);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000203 ++nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700204 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000205 return nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700206}
207
208void
Junxiao Shi15e98b02016-08-12 11:21:44 +0000209AccessStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
ashiqopuc7079482019-02-20 05:34:37 +0000210 const FaceEndpoint& ingress, const Data& data)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700211{
Junxiao Shifc021862016-08-25 21:51:18 +0000212 PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700213 if (pi != nullptr) {
214 pi->rtoTimer.cancel();
215 }
216
Junxiao Shi4846f372016-04-05 13:39:30 -0700217 if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
ashiqopuc7079482019-02-20 05:34:37 +0000218 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " not-fastest");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700219 return;
220 }
221
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000222 auto outRecord = pitEntry->getOutRecord(ingress.face);
Junxiao Shi4846f372016-04-05 13:39:30 -0700223 if (outRecord == pitEntry->out_end()) { // no out-record
ashiqopuc7079482019-02-20 05:34:37 +0000224 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " no-out-record");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700225 return;
226 }
227
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400228 auto rtt = time::steady_clock::now() - outRecord->getLastRenewed();
ashiqopuc7079482019-02-20 05:34:37 +0000229 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress
230 << " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400231 this->updateMeasurements(ingress.face, data, rtt);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700232}
233
234void
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400235AccessStrategy::updateMeasurements(const Face& inFace, const Data& data, time::nanoseconds rtt)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700236{
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400237 auto ret = m_fit.emplace(std::piecewise_construct,
238 std::forward_as_tuple(inFace.getId()),
239 std::forward_as_tuple(m_rttEstimatorOpts));
240 FaceInfo& fi = ret.first->second;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700241 fi.rtt.addMeasurement(rtt);
242
Junxiao Shifc021862016-08-25 21:51:18 +0000243 MtInfo* mi = this->addPrefixMeasurements(data);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700244 if (mi->lastNexthop != inFace.getId()) {
245 mi->lastNexthop = inFace.getId();
246 mi->rtt = fi.rtt;
247 }
248 else {
249 mi->rtt.addMeasurement(rtt);
250 }
251}
252
Junxiao Shifc021862016-08-25 21:51:18 +0000253std::tuple<Name, AccessStrategy::MtInfo*>
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700254AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
255{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000256 measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700257 if (me == nullptr) {
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000258 return std::make_tuple(Name(), nullptr);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700259 }
260
Junxiao Shifc021862016-08-25 21:51:18 +0000261 MtInfo* mi = me->getStrategyInfo<MtInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700262 BOOST_ASSERT(mi != nullptr);
263 // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
264 // this case needs another longest prefix match until mi is found
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000265 return std::make_tuple(me->getName(), mi);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700266}
267
Junxiao Shifc021862016-08-25 21:51:18 +0000268AccessStrategy::MtInfo*
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700269AccessStrategy::addPrefixMeasurements(const Data& data)
270{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000271 measurements::Entry* me = nullptr;
272 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700273 me = this->getMeasurements().get(data.getName().getPrefix(-1));
274 }
275 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
276 me = this->getMeasurements().get(data.getName());
277 // Data Name must be in this strategy
278 BOOST_ASSERT(me != nullptr);
279 }
280
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400281 this->getMeasurements().extendLifetime(*me, 8_s);
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400282 return me->insertStrategyInfo<MtInfo>(m_rttEstimatorOpts).first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700283}
284
285} // namespace fw
286} // namespace nfd