blob: 2c154bc06acfb2582c83f59292a734225d71cc50 [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 Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, 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 Pesavento2c9d2ca2024-01-27 16:36:51 -050046 NDN_THROW(std::invalid_argument("AccessStrategy does not support version " +
47 std::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{
Eric Newberry358414d2021-03-21 20:56:42 -070055 static const auto strategyName = Name("/localhost/nfd/strategy/access").appendVersion(1);
Junxiao Shi037f4ab2016-12-13 04:27:06 +000056 return strategyName;
57}
58
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070059void
Davide Pesavento0498ce82021-06-14 02:02:21 -040060AccessStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Junxiao Shi15e98b02016-08-12 11:21:44 +000061 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070062{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040063 switch (auto res = m_retxSuppression.decidePerPitEntry(*pitEntry); res) {
Ashlesh Gawande90015992017-07-11 17:25:48 -050064 case RetxSuppressionResult::NEW:
Davide Pesavento0498ce82021-06-14 02:02:21 -040065 return afterReceiveNewInterest(interest, ingress, pitEntry);
Ashlesh Gawande90015992017-07-11 17:25:48 -050066 case RetxSuppressionResult::FORWARD:
Davide Pesavento0498ce82021-06-14 02:02:21 -040067 return afterReceiveRetxInterest(interest, ingress, pitEntry);
Ashlesh Gawande90015992017-07-11 17:25:48 -050068 case RetxSuppressionResult::SUPPRESS:
Alex Lane653eb072023-07-27 22:11:46 -040069 NFD_LOG_INTEREST_FROM(interest, ingress, "suppressed");
Davide Pesavento17c172b2019-03-23 15:11:44 -040070 return;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070071 }
72}
73
74void
Davide Pesavento0498ce82021-06-14 02:02:21 -040075AccessStrategy::afterReceiveNewInterest(const Interest& interest, const FaceEndpoint& ingress,
Junxiao Shi15e98b02016-08-12 11:21:44 +000076 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070077{
Davide Pesavento17c172b2019-03-23 15:11:44 -040078 const auto& fibEntry = this->lookupFib(*pitEntry);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040079 auto [miName, mi] = this->findPrefixMeasurements(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070080
81 // has measurements for Interest Name?
82 if (mi != nullptr) {
Alex Lane653eb072023-07-27 22:11:46 -040083 NFD_LOG_INTEREST_FROM(interest, ingress, "new mi=" << miName);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070084
85 // send to last working nexthop
Davide Pesavento0498ce82021-06-14 02:02:21 -040086 bool isSentToLastNexthop = this->sendToLastNexthop(interest, ingress, pitEntry, *mi, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070087 if (isSentToLastNexthop) {
88 return;
89 }
90 }
91 else {
Alex Lane653eb072023-07-27 22:11:46 -040092 NFD_LOG_INTEREST_FROM(interest, ingress, "new no-mi");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070093 }
94
95 // no measurements, or last working nexthop unavailable
96
Junxiao Shi965d3a42015-06-01 06:55:23 -070097 // multicast to all nexthops except incoming face
Davide Pesavento0498ce82021-06-14 02:02:21 -040098 size_t nMulticastSent = this->multicast(interest, ingress.face, pitEntry, fibEntry);
Junxiao Shia7f9a292016-11-22 16:31:38 +000099
Davide Pesavento17c172b2019-03-23 15:11:44 -0400100 if (nMulticastSent == 0) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000101 this->rejectPendingInterest(pitEntry);
102 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700103}
104
105void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400106AccessStrategy::afterReceiveRetxInterest(const Interest& interest, const FaceEndpoint& ingress,
Junxiao Shi15e98b02016-08-12 11:21:44 +0000107 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700108{
Alex Lane653eb072023-07-27 22:11:46 -0400109 NFD_LOG_INTEREST_FROM(interest, ingress, "retx");
Davide Pesavento17c172b2019-03-23 15:11:44 -0400110 const auto& fibEntry = this->lookupFib(*pitEntry);
Davide Pesavento0498ce82021-06-14 02:02:21 -0400111 this->multicast(interest, ingress.face, pitEntry, fibEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700112}
113
114bool
Davide Pesavento0498ce82021-06-14 02:02:21 -0400115AccessStrategy::sendToLastNexthop(const Interest& interest, const FaceEndpoint& ingress,
Junxiao Shia7f9a292016-11-22 16:31:38 +0000116 const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
117 const fib::Entry& fibEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700118{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700119 if (mi.lastNexthop == face::INVALID_FACEID) {
Alex Lane653eb072023-07-27 22:11:46 -0400120 NFD_LOG_INTEREST_FROM(interest, ingress, "no-last-nexthop");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700121 return false;
122 }
123
Davide Pesavento17c172b2019-03-23 15:11:44 -0400124 if (mi.lastNexthop == ingress.face.getId()) {
Alex Lane653eb072023-07-27 22:11:46 -0400125 NFD_LOG_INTEREST_FROM(interest, ingress, "last-nexthop-is-downstream");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700126 return false;
127 }
128
Junxiao Shia7f9a292016-11-22 16:31:38 +0000129 Face* outFace = this->getFace(mi.lastNexthop);
Md Ashiqur Rahman6be93872019-08-07 01:25:31 +0000130 if (outFace == nullptr || !fibEntry.hasNextHop(*outFace)) {
Alex Lane653eb072023-07-27 22:11:46 -0400131 NFD_LOG_INTEREST_FROM(interest, ingress, "last-nexthop-gone");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700132 return false;
133 }
134
Davide Pesavento17c172b2019-03-23 15:11:44 -0400135 if (wouldViolateScope(ingress.face, interest, *outFace)) {
Alex Lane653eb072023-07-27 22:11:46 -0400136 NFD_LOG_INTEREST_FROM(interest, ingress, "last-nexthop-violates-scope");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700137 return false;
138 }
139
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400140 auto rto = mi.rtt.getEstimatedRto();
Alex Lane653eb072023-07-27 22:11:46 -0400141 NFD_LOG_INTEREST_FROM(interest, ingress, "to=" << mi.lastNexthop << " last-nexthop rto="
142 << time::duration_cast<time::microseconds>(rto).count() << "us");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700143
Davide Pesavento0498ce82021-06-14 02:02:21 -0400144 if (!this->sendInterest(interest, *outFace, pitEntry)) {
Eric Newberry2377ada2020-09-28 22:40:14 -0700145 return false;
146 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700147
148 // schedule RTO timeout
Junxiao Shifc021862016-08-25 21:51:18 +0000149 PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400150 pi->rtoTimer = getScheduler().schedule(rto,
Teng Liangebc20f62020-06-23 16:55:20 -0700151 [this, pitWeak = weak_ptr<pit::Entry>(pitEntry), face = ingress.face.getId(), nh = mi.lastNexthop] {
152 afterRtoTimeout(pitWeak, face, nh);
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400153 });
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700154
155 return true;
156}
157
158void
Davide Pesavento17c172b2019-03-23 15:11:44 -0400159AccessStrategy::afterRtoTimeout(const weak_ptr<pit::Entry>& pitWeak,
Teng Liangebc20f62020-06-23 16:55:20 -0700160 FaceId inFaceId, FaceId firstOutFaceId)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700161{
162 shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
Davide Pesavento17c172b2019-03-23 15:11:44 -0400163 // if PIT entry is gone, RTO timer should have been cancelled
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700164 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000165
166 Face* inFace = this->getFace(inFaceId);
167 if (inFace == nullptr) {
Alex Lane653eb072023-07-27 22:11:46 -0400168 NFD_LOG_DEBUG(pitEntry->getName() << " timeout from=" << firstOutFaceId
169 << " in-face-gone=" << inFaceId);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000170 return;
171 }
172
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500173 auto inRecord = pitEntry->findInRecord(*inFace);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000174 // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
Davide Pesavento17c172b2019-03-23 15:11:44 -0400175 // note: if this strategy is extended to send Nacks, that would also erase the in-record,
176 // and the RTO timer should be cancelled in that case as well
177 BOOST_ASSERT(inRecord != pitEntry->in_end());
Junxiao Shia7f9a292016-11-22 16:31:38 +0000178
179 const Interest& interest = inRecord->getInterest();
Junxiao Shi8d843142016-07-11 22:42:42 +0000180 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700181
Alex Lane653eb072023-07-27 22:11:46 -0400182 NFD_LOG_DEBUG(pitEntry->getName() << " timeout from=" << firstOutFaceId << " multicast");
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 }
Alex Lane653eb072023-07-27 22:11:46 -0400198 NFD_LOG_DEBUG(interest.getName() << " nonce=" << interest.getNonce()
199 << " multicast to=" << outFace.getId());
Davide Pesavento0498ce82021-06-14 02:02:21 -0400200 if (this->sendInterest(interest, outFace, pitEntry)) {
Eric Newberry2377ada2020-09-28 22:40:14 -0700201 ++nSent;
202 }
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700203 }
Junxiao Shia7f9a292016-11-22 16:31:38 +0000204 return nSent;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700205}
206
207void
Davide Pesavento0498ce82021-06-14 02:02:21 -0400208AccessStrategy::beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
209 const shared_ptr<pit::Entry>& pitEntry)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700210{
Junxiao Shifc021862016-08-25 21:51:18 +0000211 PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700212 if (pi != nullptr) {
213 pi->rtoTimer.cancel();
214 }
215
Junxiao Shi4846f372016-04-05 13:39:30 -0700216 if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
Alex Lane653eb072023-07-27 22:11:46 -0400217 NFD_LOG_DATA_FROM(data, ingress, "not-fastest");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700218 return;
219 }
220
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500221 auto outRecord = pitEntry->findOutRecord(ingress.face);
Alex Lane653eb072023-07-27 22:11:46 -0400222 if (outRecord == pitEntry->out_end()) {
223 NFD_LOG_DATA_FROM(data, ingress, "no-out-record");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700224 return;
225 }
226
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400227 auto rtt = time::steady_clock::now() - outRecord->getLastRenewed();
Alex Lane653eb072023-07-27 22:11:46 -0400228 NFD_LOG_DATA_FROM(data, ingress, "rtt=" << time::duration_cast<time::microseconds>(rtt).count() << "us");
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