access-strategy.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, Regents of the University of California,
4  * 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"
27 #include "algorithm.hpp"
28 #include "common/global.hpp"
29 #include "common/logger.hpp"
30 
31 namespace nfd::fw {
32 
35 
36 AccessStrategy::AccessStrategy(Forwarder& forwarder, const Name& name)
37  : Strategy(forwarder)
38  , m_rttEstimatorOpts(make_shared<RttEstimator::Options>()) // use the default options
39  , m_removeFaceConn(beforeRemoveFace.connect([this] (const Face& face) { m_fit.erase(face.getId()); }))
40 {
41  ParsedInstanceName parsed = parseInstanceName(name);
42  if (!parsed.parameters.empty()) {
43  NDN_THROW(std::invalid_argument("AccessStrategy does not accept parameters"));
44  }
45  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
46  NDN_THROW(std::invalid_argument("AccessStrategy does not support version " +
47  std::to_string(*parsed.version)));
48  }
50 }
51 
52 const Name&
54 {
55  static const auto strategyName = Name("/localhost/nfd/strategy/access").appendVersion(1);
56  return strategyName;
57 }
58 
59 void
60 AccessStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
61  const shared_ptr<pit::Entry>& pitEntry)
62 {
63  switch (auto res = m_retxSuppression.decidePerPitEntry(*pitEntry); res) {
65  return afterReceiveNewInterest(interest, ingress, pitEntry);
67  return afterReceiveRetxInterest(interest, ingress, pitEntry);
69  NFD_LOG_INTEREST_FROM(interest, ingress, "suppressed");
70  return;
71  }
72 }
73 
74 void
75 AccessStrategy::afterReceiveNewInterest(const Interest& interest, const FaceEndpoint& ingress,
76  const shared_ptr<pit::Entry>& pitEntry)
77 {
78  const auto& fibEntry = this->lookupFib(*pitEntry);
79  auto [miName, mi] = this->findPrefixMeasurements(*pitEntry);
80 
81  // has measurements for Interest Name?
82  if (mi != nullptr) {
83  NFD_LOG_INTEREST_FROM(interest, ingress, "new mi=" << miName);
84 
85  // send to last working nexthop
86  bool isSentToLastNexthop = this->sendToLastNexthop(interest, ingress, pitEntry, *mi, fibEntry);
87  if (isSentToLastNexthop) {
88  return;
89  }
90  }
91  else {
92  NFD_LOG_INTEREST_FROM(interest, ingress, "new no-mi");
93  }
94 
95  // no measurements, or last working nexthop unavailable
96 
97  // multicast to all nexthops except incoming face
98  size_t nMulticastSent = this->multicast(interest, ingress.face, pitEntry, fibEntry);
99 
100  if (nMulticastSent == 0) {
101  this->rejectPendingInterest(pitEntry);
102  }
103 }
104 
105 void
106 AccessStrategy::afterReceiveRetxInterest(const Interest& interest, const FaceEndpoint& ingress,
107  const shared_ptr<pit::Entry>& pitEntry)
108 {
109  NFD_LOG_INTEREST_FROM(interest, ingress, "retx");
110  const auto& fibEntry = this->lookupFib(*pitEntry);
111  this->multicast(interest, ingress.face, pitEntry, fibEntry);
112 }
113 
114 bool
115 AccessStrategy::sendToLastNexthop(const Interest& interest, const FaceEndpoint& ingress,
116  const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
117  const fib::Entry& fibEntry)
118 {
119  if (mi.lastNexthop == face::INVALID_FACEID) {
120  NFD_LOG_INTEREST_FROM(interest, ingress, "no-last-nexthop");
121  return false;
122  }
123 
124  if (mi.lastNexthop == ingress.face.getId()) {
125  NFD_LOG_INTEREST_FROM(interest, ingress, "last-nexthop-is-downstream");
126  return false;
127  }
128 
129  Face* outFace = this->getFace(mi.lastNexthop);
130  if (outFace == nullptr || !fibEntry.hasNextHop(*outFace)) {
131  NFD_LOG_INTEREST_FROM(interest, ingress, "last-nexthop-gone");
132  return false;
133  }
134 
135  if (wouldViolateScope(ingress.face, interest, *outFace)) {
136  NFD_LOG_INTEREST_FROM(interest, ingress, "last-nexthop-violates-scope");
137  return false;
138  }
139 
140  auto rto = mi.rtt.getEstimatedRto();
141  NFD_LOG_INTEREST_FROM(interest, ingress, "to=" << mi.lastNexthop << " last-nexthop rto="
142  << time::duration_cast<time::microseconds>(rto).count() << "us");
143 
144  if (!this->sendInterest(interest, *outFace, pitEntry)) {
145  return false;
146  }
147 
148  // schedule RTO timeout
149  PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
150  pi->rtoTimer = getScheduler().schedule(rto,
151  [this, pitWeak = weak_ptr<pit::Entry>(pitEntry), face = ingress.face.getId(), nh = mi.lastNexthop] {
152  afterRtoTimeout(pitWeak, face, nh);
153  });
154 
155  return true;
156 }
157 
158 void
159 AccessStrategy::afterRtoTimeout(const weak_ptr<pit::Entry>& pitWeak,
160  FaceId inFaceId, FaceId firstOutFaceId)
161 {
162  shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
163  // if PIT entry is gone, RTO timer should have been cancelled
164  BOOST_ASSERT(pitEntry != nullptr);
165 
166  Face* inFace = this->getFace(inFaceId);
167  if (inFace == nullptr) {
168  NFD_LOG_DEBUG(pitEntry->getName() << " timeout from=" << firstOutFaceId
169  << " in-face-gone=" << inFaceId);
170  return;
171  }
172 
173  auto inRecord = pitEntry->findInRecord(*inFace);
174  // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
175  // 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());
178 
179  const Interest& interest = inRecord->getInterest();
180  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
181 
182  NFD_LOG_DEBUG(pitEntry->getName() << " timeout from=" << firstOutFaceId << " multicast");
183  this->multicast(interest, *inFace, pitEntry, fibEntry, firstOutFaceId);
184 }
185 
186 size_t
187 AccessStrategy::multicast(const Interest& interest, const Face& inFace,
188  const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
189  FaceId exceptFace)
190 {
191  size_t nSent = 0;
192  for (const auto& nexthop : fibEntry.getNextHops()) {
193  Face& outFace = nexthop.getFace();
194  if (&outFace == &inFace || outFace.getId() == exceptFace ||
195  wouldViolateScope(inFace, interest, outFace)) {
196  continue;
197  }
198  NFD_LOG_DEBUG(interest.getName() << " nonce=" << interest.getNonce()
199  << " multicast to=" << outFace.getId());
200  if (this->sendInterest(interest, outFace, pitEntry)) {
201  ++nSent;
202  }
203  }
204  return nSent;
205 }
206 
207 void
208 AccessStrategy::beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
209  const shared_ptr<pit::Entry>& pitEntry)
210 {
211  PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
212  if (pi != nullptr) {
213  pi->rtoTimer.cancel();
214  }
215 
216  if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
217  NFD_LOG_DATA_FROM(data, ingress, "not-fastest");
218  return;
219  }
220 
221  auto outRecord = pitEntry->findOutRecord(ingress.face);
222  if (outRecord == pitEntry->out_end()) {
223  NFD_LOG_DATA_FROM(data, ingress, "no-out-record");
224  return;
225  }
226 
227  auto rtt = time::steady_clock::now() - outRecord->getLastRenewed();
228  NFD_LOG_DATA_FROM(data, ingress, "rtt=" << time::duration_cast<time::microseconds>(rtt).count() << "us");
229  this->updateMeasurements(ingress.face, data, rtt);
230 }
231 
232 void
233 AccessStrategy::updateMeasurements(const Face& inFace, const Data& data, time::nanoseconds rtt)
234 {
235  FaceInfo& fi = m_fit.try_emplace(inFace.getId(), m_rttEstimatorOpts).first->second;
236  fi.rtt.addMeasurement(rtt);
237 
238  MtInfo* mi = this->addPrefixMeasurements(data);
239  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 
248 std::tuple<Name, AccessStrategy::MtInfo*>
249 AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
250 {
251  auto me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
252  if (me == nullptr) {
253  return {Name{}, nullptr};
254  }
255 
256  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.
260  return {me->getName(), mi};
261 }
262 
263 AccessStrategy::MtInfo*
264 AccessStrategy::addPrefixMeasurements(const Data& data)
265 {
266  measurements::Entry* me = nullptr;
267  if (!data.getName().empty()) {
268  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 
276  this->getMeasurements().extendLifetime(*me, 8_s);
277  return me->insertStrategyInfo<MtInfo>(m_rttEstimatorOpts).first;
278 }
279 
280 } // namespace nfd::fw
This file contains common algorithms used by forwarding strategies.
Represents a face-endpoint pair in the forwarder.
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:54
T * getStrategyInfo() const
Get a StrategyInfo item.
Generalization of a network interface.
Definition: face.hpp:118
FaceId getId() const noexcept
Returns the face ID.
Definition: face.hpp:195
A forwarding strategy for "access" routers.
void afterReceiveInterest(const Interest &interest, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry) override
Trigger after an Interest is received.
static const Name & getStrategyName()
AccessStrategy(Forwarder &forwarder, const Name &name=getStrategyName())
void beforeSatisfyInterest(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry) override
Trigger before a PIT entry is satisfied.
RetxSuppressionResult decidePerPitEntry(pit::Entry &pitEntry) const
Determines whether Interest is a retransmission, and if so, whether it shall be forwarded or suppress...
Base class of all forwarding strategies.
Definition: strategy.hpp:46
Face * getFace(FaceId id) const noexcept
Definition: strategy.hpp:405
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
Schedule the PIT entry for immediate deletion.
Definition: strategy.hpp:335
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
Performs a FIB lookup, considering Link object if present.
Definition: strategy.cpp:313
pit::OutRecord * sendInterest(const Interest &interest, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send an Interest packet.
Definition: strategy.cpp:236
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
MeasurementsAccessor & getMeasurements() noexcept
Definition: strategy.hpp:399
void setInstanceName(const Name &name) noexcept
Set strategy instance name.
Definition: strategy.hpp:448
static Name makeInstanceName(const Name &input, const Name &strategyName)
Construct a strategy instance name.
Definition: strategy.cpp:134
void extendLifetime(Entry &entry, const time::nanoseconds &lifetime)
Extend lifetime of an entry.
Entry * findLongestPrefixMatch(const Name &name, const EntryPredicate &pred=AnyEntry()) const
Perform a longest prefix match for name.
Entry * get(const Name &name)
Find or insert a Measurements entry for name.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
constexpr FaceId INVALID_FACEID
Indicates an invalid FaceId.
Definition: face-common.hpp:51
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:48
@ SUPPRESS
Interest is a retransmission and should be suppressed.
@ NEW
Interest is new (not a retransmission).
@ FORWARD
Interest is a retransmission and should be forwarded.
NFD_REGISTER_STRATEGY(SelfLearningStrategy)
bool wouldViolateScope(const Face &inFace, const Interest &interest, const Face &outFace)
Determine whether forwarding the Interest in pitEntry to outFace would violate scope.
Definition: algorithm.cpp:33
ndn::Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:45
#define NFD_LOG_INTEREST_FROM(interest, ingress, msg)
Logs the reception of interest on ingress, followed by msg, at DEBUG level.
Definition: strategy.hpp:542
#define NFD_LOG_DATA_FROM(data, ingress, msg)
Logs the reception of data on ingress, followed by msg, at DEBUG level.
Definition: strategy.hpp:549