blob: e80c58102ad2442c3be4afe1aef1895f0a7267ae [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
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{
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:
ashiqopuc7079482019-02-20 05:34:37 +000069 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-suppress");
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) {
Davide Pesavento17c172b2019-03-23 15:11:44 -040083 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest 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 {
Davide Pesavento17c172b2019-03-23 15:11:44 -040092 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest 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{
Davide Pesavento17c172b2019-03-23 15:11:44 -0400109 const auto& fibEntry = this->lookupFib(*pitEntry);
110 NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-forward");
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) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700120 NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
121 return false;
122 }
123
Davide Pesavento17c172b2019-03-23 15:11:44 -0400124 if (mi.lastNexthop == ingress.face.getId()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700125 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
126 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)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700131 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
132 return false;
133 }
134
Davide Pesavento17c172b2019-03-23 15:11:44 -0400135 if (wouldViolateScope(ingress.face, interest, *outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700136 NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
137 return false;
138 }
139
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400140 auto rto = mi.rtt.getEstimatedRto();
ashiqopuc7079482019-02-20 05:34:37 +0000141 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop
142 << " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
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) {
ashiqopuc7079482019-02-20 05:34:37 +0000168 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
169 << " inFace-gone " << inFaceId);
Junxiao Shia7f9a292016-11-22 16:31:38 +0000170 return;
171 }
172
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000173 auto inRecord = pitEntry->getInRecord(*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
ashiqopuc7079482019-02-20 05:34:37 +0000182 NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
183 << " multicast-except " << firstOutFaceId);
Davide Pesavento0498ce82021-06-14 02:02:21 -0400184 this->multicast(interest, *inFace, pitEntry, fibEntry, firstOutFaceId);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700185}
186
Davide Pesavento17c172b2019-03-23 15:11:44 -0400187size_t
Davide Pesavento0498ce82021-06-14 02:02:21 -0400188AccessStrategy::multicast(const Interest& interest, const Face& inFace,
Junxiao Shia7f9a292016-11-22 16:31:38 +0000189 const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
190 FaceId exceptFace)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700191{
Davide Pesavento17c172b2019-03-23 15:11:44 -0400192 size_t nSent = 0;
193 for (const auto& nexthop : fibEntry.getNextHops()) {
Junxiao Shia7f9a292016-11-22 16:31:38 +0000194 Face& outFace = nexthop.getFace();
195 if (&outFace == &inFace || outFace.getId() == exceptFace ||
196 wouldViolateScope(inFace, interest, outFace)) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700197 continue;
198 }
ashiqopuc7079482019-02-20 05:34:37 +0000199 NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() << " multicast");
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
ashiqopuc7079482019-02-20 05:34:37 +0000217 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " not-fastest");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700218 return;
219 }
220
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000221 auto outRecord = pitEntry->getOutRecord(ingress.face);
Junxiao Shi4846f372016-04-05 13:39:30 -0700222 if (outRecord == pitEntry->out_end()) { // no out-record
ashiqopuc7079482019-02-20 05:34:37 +0000223 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << 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();
ashiqopuc7079482019-02-20 05:34:37 +0000228 NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress
229 << " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400230 this->updateMeasurements(ingress.face, data, rtt);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700231}
232
233void
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400234AccessStrategy::updateMeasurements(const Face& inFace, const Data& data, time::nanoseconds rtt)
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700235{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400236 FaceInfo& fi = m_fit.try_emplace(inFace.getId(), m_rttEstimatorOpts).first->second;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700237 fi.rtt.addMeasurement(rtt);
238
Junxiao Shifc021862016-08-25 21:51:18 +0000239 MtInfo* mi = this->addPrefixMeasurements(data);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700240 if (mi->lastNexthop != inFace.getId()) {
241 mi->lastNexthop = inFace.getId();
242 mi->rtt = fi.rtt;
243 }
244 else {
245 mi->rtt.addMeasurement(rtt);
246 }
247}
248
Junxiao Shifc021862016-08-25 21:51:18 +0000249std::tuple<Name, AccessStrategy::MtInfo*>
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700250AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
251{
Davide Pesavento0bba81d2020-10-05 17:50:28 -0400252 auto me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700253 if (me == nullptr) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400254 return {Name{}, nullptr};
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700255 }
256
Davide Pesavento0bba81d2020-10-05 17:50:28 -0400257 auto mi = me->getStrategyInfo<MtInfo>();
258 // TODO: after a runtime strategy change, it's possible that a measurements::Entry exists but
259 // the corresponding MtInfo doesn't exist (mi == nullptr); this case needs another longest
260 // prefix match until an MtInfo is found.
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400261 return {me->getName(), mi};
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700262}
263
Junxiao Shifc021862016-08-25 21:51:18 +0000264AccessStrategy::MtInfo*
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700265AccessStrategy::addPrefixMeasurements(const Data& data)
266{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000267 measurements::Entry* me = nullptr;
268 if (!data.getName().empty()) {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700269 me = this->getMeasurements().get(data.getName().getPrefix(-1));
270 }
271 if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
272 me = this->getMeasurements().get(data.getName());
273 // Data Name must be in this strategy
274 BOOST_ASSERT(me != nullptr);
275 }
276
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400277 this->getMeasurements().extendLifetime(*me, 8_s);
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400278 return me->insertStrategyInfo<MtInfo>(m_rttEstimatorOpts).first;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700279}
280
281} // namespace fw
282} // namespace nfd