blob: 688867f71c7523a9173ddcb261182293949d6d33 [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/*
Alex Lane653eb072023-07-27 22:11:46 -04003 * Copyright (c) 2014-2023, 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:
Alex Lane653eb072023-07-27 22:11:46 -040068 NFD_LOG_INTEREST_FROM(interest, ingress, "suppressed");
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) {
Alex Lane653eb072023-07-27 22:11:46 -040082 NFD_LOG_INTEREST_FROM(interest, ingress, "new 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 {
Alex Lane653eb072023-07-27 22:11:46 -040091 NFD_LOG_INTEREST_FROM(interest, ingress, "new 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{
Alex Lane653eb072023-07-27 22:11:46 -0400108 NFD_LOG_INTEREST_FROM(interest, ingress, "retx");
Davide Pesavento17c172b2019-03-23 15:11:44 -0400109 const auto& fibEntry = this->lookupFib(*pitEntry);
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) {
Alex Lane653eb072023-07-27 22:11:46 -0400119 NFD_LOG_INTEREST_FROM(interest, ingress, "no-last-nexthop");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700120 return false;
121 }
122
Davide Pesavento17c172b2019-03-23 15:11:44 -0400123 if (mi.lastNexthop == ingress.face.getId()) {
Alex Lane653eb072023-07-27 22:11:46 -0400124 NFD_LOG_INTEREST_FROM(interest, ingress, "last-nexthop-is-downstream");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700125 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)) {
Alex Lane653eb072023-07-27 22:11:46 -0400130 NFD_LOG_INTEREST_FROM(interest, ingress, "last-nexthop-gone");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700131 return false;
132 }
133
Davide Pesavento17c172b2019-03-23 15:11:44 -0400134 if (wouldViolateScope(ingress.face, interest, *outFace)) {
Alex Lane653eb072023-07-27 22:11:46 -0400135 NFD_LOG_INTEREST_FROM(interest, ingress, "last-nexthop-violates-scope");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700136 return false;
137 }
138
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400139 auto rto = mi.rtt.getEstimatedRto();
Alex Lane653eb072023-07-27 22:11:46 -0400140 NFD_LOG_INTEREST_FROM(interest, ingress, "to=" << mi.lastNexthop << " last-nexthop rto="
141 << time::duration_cast<time::microseconds>(rto).count() << "us");
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) {
Alex Lane653eb072023-07-27 22:11:46 -0400167 NFD_LOG_DEBUG(pitEntry->getName() << " timeout from=" << firstOutFaceId
168 << " in-face-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
Alex Lane653eb072023-07-27 22:11:46 -0400181 NFD_LOG_DEBUG(pitEntry->getName() << " timeout from=" << firstOutFaceId << " multicast");
Davide Pesavento0498ce82021-06-14 02:02:21 -0400182 this->multicast(interest, *inFace, pitEntry, fibEntry, firstOutFaceId);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700183}
184
Davide Pesavento17c172b2019-03-23 15:11:44 -0400185size_t
Davide Pesavento0498ce82021-06-14 02:02:21 -0400186AccessStrategy::multicast(const Interest& interest, const Face& inFace,
Junxiao Shia7f9a292016-11-22 16:31:38 +0000187 const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
188 FaceId exceptFace)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700189{
Davide Pesavento17c172b2019-03-23 15:11:44 -0400190 size_t nSent = 0;
191 for (const auto& nexthop : fibEntry.getNextHops()) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000192 Face& outFace = nexthop.getFace();
193 if (&outFace == &inFace || outFace.getId() == exceptFace ||
194 wouldViolateScope(inFace, interest, outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700195 continue;
196 }
Alex Lane653eb072023-07-27 22:11:46 -0400197 NFD_LOG_DEBUG(interest.getName() << " nonce=" << interest.getNonce()
198 << " multicast to=" << outFace.getId());
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
Alex Lane653eb072023-07-27 22:11:46 -0400216 NFD_LOG_DATA_FROM(data, 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);
Alex Lane653eb072023-07-27 22:11:46 -0400221 if (outRecord == pitEntry->out_end()) {
222 NFD_LOG_DATA_FROM(data, 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();
Alex Lane653eb072023-07-27 22:11:46 -0400227 NFD_LOG_DATA_FROM(data, ingress, "rtt=" << time::duration_cast<time::microseconds>(rtt).count() << "us");
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400228 this->updateMeasurements(ingress.face, data, rtt);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700229}
230
231void
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400232AccessStrategy::updateMeasurements(const Face& inFace, const Data& data, time::nanoseconds rtt)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700233{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400234 FaceInfo& fi = m_fit.try_emplace(inFace.getId(), m_rttEstimatorOpts).first->second;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700235 fi.rtt.addMeasurement(rtt);
236
Junxiao Shifc021862016-08-25 21:51:18 +0000237 MtInfo* mi = this->addPrefixMeasurements(data);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700238 if (mi->lastNexthop != inFace.getId()) {
239 mi->lastNexthop = inFace.getId();
240 mi->rtt = fi.rtt;
241 }
242 else {
243 mi->rtt.addMeasurement(rtt);
244 }
245}
246
Junxiao Shifc021862016-08-25 21:51:18 +0000247std::tuple<Name, AccessStrategy::MtInfo*>
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700248AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
249{
Davide Pesavento0bba81d2020-10-05 17:50:28 -0400250 auto me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700251 if (me == nullptr) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400252 return {Name{}, nullptr};
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700253 }
254
Davide Pesavento0bba81d2020-10-05 17:50:28 -0400255 auto mi = me->getStrategyInfo<MtInfo>();
256 // TODO: after a runtime strategy change, it's possible that a measurements::Entry exists but
257 // the corresponding MtInfo doesn't exist (mi == nullptr); this case needs another longest
258 // prefix match until an MtInfo is found.
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400259 return {me->getName(), mi};
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700260}
261
Junxiao Shifc021862016-08-25 21:51:18 +0000262AccessStrategy::MtInfo*
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700263AccessStrategy::addPrefixMeasurements(const Data& data)
264{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000265 measurements::Entry* me = nullptr;
266 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700267 me = this->getMeasurements().get(data.getName().getPrefix(-1));
268 }
269 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
270 me = this->getMeasurements().get(data.getName());
271 // Data Name must be in this strategy
272 BOOST_ASSERT(me != nullptr);
273 }
274
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400275 this->getMeasurements().extendLifetime(*me, 8_s);
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400276 return me->insertStrategyInfo<MtInfo>(m_rttEstimatorOpts).first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700277}
278
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400279} // namespace nfd::fw