blob: c2b6504cd9fcff46240df39d556ecc650313502a [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/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::fw {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070032
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 Pesaventoeb7b7ab2019-08-14 19:00:15 -040038 , m_rttEstimatorOpts(make_shared<RttEstimator::Options>()) // use the default options
39 , m_removeFaceConn(beforeRemoveFace.connect([this] (const Face& face) { m_fit.erase(face.getId()); }))
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070040{
Junxiao Shi18739c42016-12-22 08:03:00 +000041 ParsedInstanceName parsed = parseInstanceName(name);
42 if (!parsed.parameters.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050043 NDN_THROW(std::invalid_argument("AccessStrategy does not accept parameters"));
Junxiao Shi18739c42016-12-22 08:03:00 +000044 }
Junxiao Shi91f6ee02016-12-29 21:44:44 +000045 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento3dade002019-03-19 11:29:56 -060046 NDN_THROW(std::invalid_argument("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{
Eric Newberry358414d2021-03-21 20:56:42 -070054 static const auto strategyName = Name("/localhost/nfd/strategy/access").appendVersion(1);
Junxiao Shi037f4ab2016-12-13 04:27:06 +000055 return strategyName;
56}
57
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070058void
Davide Pesavento0498ce82021-06-14 02:02:21 -040059AccessStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Junxiao Shi15e98b02016-08-12 11:21:44 +000060 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070061{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040062 switch (auto res = m_retxSuppression.decidePerPitEntry(*pitEntry); res) {
Ashlesh Gawande90015992017-07-11 17:25:48 -050063 case RetxSuppressionResult::NEW:
Davide Pesavento0498ce82021-06-14 02:02:21 -040064 return afterReceiveNewInterest(interest, ingress, pitEntry);
Ashlesh Gawande90015992017-07-11 17:25:48 -050065 case RetxSuppressionResult::FORWARD:
Davide Pesavento0498ce82021-06-14 02:02:21 -040066 return afterReceiveRetxInterest(interest, ingress, pitEntry);
Ashlesh Gawande90015992017-07-11 17:25:48 -050067 case RetxSuppressionResult::SUPPRESS:
ashiqopuc7079482019-02-20 05:34:37 +000068 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-suppress");
Davide Pesavento17c172b2019-03-23 15:11:44 -040069 return;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070070 }
71}
72
73void
Davide Pesavento0498ce82021-06-14 02:02:21 -040074AccessStrategy::afterReceiveNewInterest(const Interest& interest, const FaceEndpoint& ingress,
Junxiao Shi15e98b02016-08-12 11:21:44 +000075 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070076{
Davide Pesavento17c172b2019-03-23 15:11:44 -040077 const auto& fibEntry = this->lookupFib(*pitEntry);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040078 auto [miName, mi] = this->findPrefixMeasurements(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070079
80 // has measurements for Interest Name?
81 if (mi != nullptr) {
Davide Pesavento17c172b2019-03-23 15:11:44 -040082 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest mi=" << miName);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070083
84 // send to last working nexthop
Davide Pesavento0498ce82021-06-14 02:02:21 -040085 bool isSentToLastNexthop = this->sendToLastNexthop(interest, ingress, pitEntry, *mi, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070086 if (isSentToLastNexthop) {
87 return;
88 }
89 }
90 else {
Davide Pesavento17c172b2019-03-23 15:11:44 -040091 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest no-mi");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070092 }
93
94 // no measurements, or last working nexthop unavailable
95
Junxiao Shi965d3a42015-06-01 06:55:23 -070096 // multicast to all nexthops except incoming face
Davide Pesavento0498ce82021-06-14 02:02:21 -040097 size_t nMulticastSent = this->multicast(interest, ingress.face, pitEntry, fibEntry);
Junxiao Shia7f9a292016-11-22 16:31:38 +000098
Davide Pesavento17c172b2019-03-23 15:11:44 -040099 if (nMulticastSent == 0) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000100 this->rejectPendingInterest(pitEntry);
101 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700102}
103
104void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400105AccessStrategy::afterReceiveRetxInterest(const Interest& interest, const FaceEndpoint& ingress,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000106 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700107{
Davide Pesavento17c172b2019-03-23 15:11:44 -0400108 const auto& fibEntry = this->lookupFib(*pitEntry);
109 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-forward");
Davide Pesavento0498ce82021-06-14 02:02:21 -0400110 this->multicast(interest, ingress.face, pitEntry, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700111}
112
113bool
Davide Pesavento0498ce82021-06-14 02:02:21 -0400114AccessStrategy::sendToLastNexthop(const Interest& interest, const FaceEndpoint& ingress,
Junxiao Shia7f9a292016-11-22 16:31:38 +0000115 const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
116 const fib::Entry& fibEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700117{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700118 if (mi.lastNexthop == face::INVALID_FACEID) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700119 NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
120 return false;
121 }
122
Davide Pesavento17c172b2019-03-23 15:11:44 -0400123 if (mi.lastNexthop == ingress.face.getId()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700124 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
125 return false;
126 }
127
Junxiao Shia7f9a292016-11-22 16:31:38 +0000128 Face* outFace = this->getFace(mi.lastNexthop);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000129 if (outFace == nullptr || !fibEntry.hasNextHop(*outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700130 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
131 return false;
132 }
133
Davide Pesavento17c172b2019-03-23 15:11:44 -0400134 if (wouldViolateScope(ingress.face, interest, *outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700135 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
136 return false;
137 }
138
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400139 auto rto = mi.rtt.getEstimatedRto();
ashiqopuc7079482019-02-20 05:34:37 +0000140 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop
141 << " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700142
Davide Pesavento0498ce82021-06-14 02:02:21 -0400143 if (!this->sendInterest(interest, *outFace, pitEntry)) {
Eric Newberry2377ada2020-09-28 22:40:14 -0700144 return false;
145 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700146
147 // schedule RTO timeout
Junxiao Shifc021862016-08-25 21:51:18 +0000148 PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400149 pi->rtoTimer = getScheduler().schedule(rto,
Teng Liangebc20f62020-06-23 16:55:20 -0700150 [this, pitWeak = weak_ptr<pit::Entry>(pitEntry), face = ingress.face.getId(), nh = mi.lastNexthop] {
151 afterRtoTimeout(pitWeak, face, nh);
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400152 });
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700153
154 return true;
155}
156
157void
Davide Pesavento17c172b2019-03-23 15:11:44 -0400158AccessStrategy::afterRtoTimeout(const weak_ptr<pit::Entry>& pitWeak,
Teng Liangebc20f62020-06-23 16:55:20 -0700159 FaceId inFaceId, FaceId firstOutFaceId)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700160{
161 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
Davide Pesavento17c172b2019-03-23 15:11:44 -0400162 // if PIT entry is gone, RTO timer should have been cancelled
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700163 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000164
165 Face* inFace = this->getFace(inFaceId);
166 if (inFace == nullptr) {
ashiqopuc7079482019-02-20 05:34:37 +0000167 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
168 << " inFace-gone " << inFaceId);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000169 return;
170 }
171
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000172 auto inRecord = pitEntry->getInRecord(*inFace);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000173 // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
Davide Pesavento17c172b2019-03-23 15:11:44 -0400174 // note: if this strategy is extended to send Nacks, that would also erase the in-record,
175 // and the RTO timer should be cancelled in that case as well
176 BOOST_ASSERT(inRecord != pitEntry->in_end());
Junxiao Shia7f9a292016-11-22 16:31:38 +0000177
178 const Interest& interest = inRecord->getInterest();
Junxiao Shi8d843142016-07-11 22:42:42 +0000179 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700180
ashiqopuc7079482019-02-20 05:34:37 +0000181 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
182 << " multicast-except " << firstOutFaceId);
Davide Pesavento0498ce82021-06-14 02:02:21 -0400183 this->multicast(interest, *inFace, pitEntry, fibEntry, firstOutFaceId);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700184}
185
Davide Pesavento17c172b2019-03-23 15:11:44 -0400186size_t
Davide Pesavento0498ce82021-06-14 02:02:21 -0400187AccessStrategy::multicast(const Interest& interest, const Face& inFace,
Junxiao Shia7f9a292016-11-22 16:31:38 +0000188 const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
189 FaceId exceptFace)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700190{
Davide Pesavento17c172b2019-03-23 15:11:44 -0400191 size_t nSent = 0;
192 for (const auto& nexthop : fibEntry.getNextHops()) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000193 Face& outFace = nexthop.getFace();
194 if (&outFace == &inFace || outFace.getId() == exceptFace ||
195 wouldViolateScope(inFace, interest, outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700196 continue;
197 }
ashiqopuc7079482019-02-20 05:34:37 +0000198 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() << " multicast");
Davide Pesavento0498ce82021-06-14 02:02:21 -0400199 if (this->sendInterest(interest, outFace, pitEntry)) {
Eric Newberry2377ada2020-09-28 22:40:14 -0700200 ++nSent;
201 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700202 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000203 return nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700204}
205
206void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400207AccessStrategy::beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
208 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700209{
Junxiao Shifc021862016-08-25 21:51:18 +0000210 PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700211 if (pi != nullptr) {
212 pi->rtoTimer.cancel();
213 }
214
Junxiao Shi4846f372016-04-05 13:39:30 -0700215 if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
ashiqopuc7079482019-02-20 05:34:37 +0000216 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " not-fastest");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700217 return;
218 }
219
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000220 auto outRecord = pitEntry->getOutRecord(ingress.face);
Junxiao Shi4846f372016-04-05 13:39:30 -0700221 if (outRecord == pitEntry->out_end()) { // no out-record
ashiqopuc7079482019-02-20 05:34:37 +0000222 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " no-out-record");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700223 return;
224 }
225
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400226 auto rtt = time::steady_clock::now() - outRecord->getLastRenewed();
ashiqopuc7079482019-02-20 05:34:37 +0000227 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress
228 << " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400229 this->updateMeasurements(ingress.face, data, rtt);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700230}
231
232void
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400233AccessStrategy::updateMeasurements(const Face& inFace, const Data& data, time::nanoseconds rtt)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700234{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400235 FaceInfo& fi = m_fit.try_emplace(inFace.getId(), m_rttEstimatorOpts).first->second;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700236 fi.rtt.addMeasurement(rtt);
237
Junxiao Shifc021862016-08-25 21:51:18 +0000238 MtInfo* mi = this->addPrefixMeasurements(data);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700239 if (mi->lastNexthop != inFace.getId()) {
240 mi->lastNexthop = inFace.getId();
241 mi->rtt = fi.rtt;
242 }
243 else {
244 mi->rtt.addMeasurement(rtt);
245 }
246}
247
Junxiao Shifc021862016-08-25 21:51:18 +0000248std::tuple<Name, AccessStrategy::MtInfo*>
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700249AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
250{
Davide Pesavento0bba81d2020-10-05 17:50:28 -0400251 auto me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700252 if (me == nullptr) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400253 return {Name{}, nullptr};
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700254 }
255
Davide Pesavento0bba81d2020-10-05 17:50:28 -0400256 auto mi = me->getStrategyInfo<MtInfo>();
257 // TODO: after a runtime strategy change, it's possible that a measurements::Entry exists but
258 // the corresponding MtInfo doesn't exist (mi == nullptr); this case needs another longest
259 // prefix match until an MtInfo is found.
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400260 return {me->getName(), mi};
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700261}
262
Junxiao Shifc021862016-08-25 21:51:18 +0000263AccessStrategy::MtInfo*
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700264AccessStrategy::addPrefixMeasurements(const Data& data)
265{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000266 measurements::Entry* me = nullptr;
267 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700268 me = this->getMeasurements().get(data.getName().getPrefix(-1));
269 }
270 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
271 me = this->getMeasurements().get(data.getName());
272 // Data Name must be in this strategy
273 BOOST_ASSERT(me != nullptr);
274 }
275
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400276 this->getMeasurements().extendLifetime(*me, 8_s);
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400277 return me->insertStrategyInfo<MtInfo>(m_rttEstimatorOpts).first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700278}
279
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400280} // namespace nfd::fw