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-2022, 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 " + to_string(*parsed.version)));
47  }
49 }
50 
51 const Name&
53 {
54  static const auto strategyName = Name("/localhost/nfd/strategy/access").appendVersion(1);
55  return strategyName;
56 }
57 
58 void
59 AccessStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
60  const shared_ptr<pit::Entry>& pitEntry)
61 {
62  switch (auto res = m_retxSuppression.decidePerPitEntry(*pitEntry); res) {
64  return afterReceiveNewInterest(interest, ingress, pitEntry);
66  return afterReceiveRetxInterest(interest, ingress, pitEntry);
68  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-suppress");
69  return;
70  }
71 }
72 
73 void
74 AccessStrategy::afterReceiveNewInterest(const Interest& interest, const FaceEndpoint& ingress,
75  const shared_ptr<pit::Entry>& pitEntry)
76 {
77  const auto& fibEntry = this->lookupFib(*pitEntry);
78  auto [miName, mi] = this->findPrefixMeasurements(*pitEntry);
79 
80  // has measurements for Interest Name?
81  if (mi != nullptr) {
82  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest mi=" << miName);
83 
84  // send to last working nexthop
85  bool isSentToLastNexthop = this->sendToLastNexthop(interest, ingress, pitEntry, *mi, fibEntry);
86  if (isSentToLastNexthop) {
87  return;
88  }
89  }
90  else {
91  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest no-mi");
92  }
93 
94  // no measurements, or last working nexthop unavailable
95 
96  // multicast to all nexthops except incoming face
97  size_t nMulticastSent = this->multicast(interest, ingress.face, pitEntry, fibEntry);
98 
99  if (nMulticastSent == 0) {
100  this->rejectPendingInterest(pitEntry);
101  }
102 }
103 
104 void
105 AccessStrategy::afterReceiveRetxInterest(const Interest& interest, const FaceEndpoint& ingress,
106  const shared_ptr<pit::Entry>& pitEntry)
107 {
108  const auto& fibEntry = this->lookupFib(*pitEntry);
109  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-forward");
110  this->multicast(interest, ingress.face, pitEntry, fibEntry);
111 }
112 
113 bool
114 AccessStrategy::sendToLastNexthop(const Interest& interest, const FaceEndpoint& ingress,
115  const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
116  const fib::Entry& fibEntry)
117 {
118  if (mi.lastNexthop == face::INVALID_FACEID) {
119  NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
120  return false;
121  }
122 
123  if (mi.lastNexthop == ingress.face.getId()) {
124  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
125  return false;
126  }
127 
128  Face* outFace = this->getFace(mi.lastNexthop);
129  if (outFace == nullptr || !fibEntry.hasNextHop(*outFace)) {
130  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
131  return false;
132  }
133 
134  if (wouldViolateScope(ingress.face, interest, *outFace)) {
135  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
136  return false;
137  }
138 
139  auto rto = mi.rtt.getEstimatedRto();
140  NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop
141  << " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
142 
143  if (!this->sendInterest(interest, *outFace, pitEntry)) {
144  return false;
145  }
146 
147  // schedule RTO timeout
148  PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
149  pi->rtoTimer = getScheduler().schedule(rto,
150  [this, pitWeak = weak_ptr<pit::Entry>(pitEntry), face = ingress.face.getId(), nh = mi.lastNexthop] {
151  afterRtoTimeout(pitWeak, face, nh);
152  });
153 
154  return true;
155 }
156 
157 void
158 AccessStrategy::afterRtoTimeout(const weak_ptr<pit::Entry>& pitWeak,
159  FaceId inFaceId, FaceId firstOutFaceId)
160 {
161  shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
162  // if PIT entry is gone, RTO timer should have been cancelled
163  BOOST_ASSERT(pitEntry != nullptr);
164 
165  Face* inFace = this->getFace(inFaceId);
166  if (inFace == nullptr) {
167  NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
168  << " inFace-gone " << inFaceId);
169  return;
170  }
171 
172  auto inRecord = pitEntry->getInRecord(*inFace);
173  // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
174  // 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());
177 
178  const Interest& interest = inRecord->getInterest();
179  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
180 
181  NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
182  << " multicast-except " << firstOutFaceId);
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(pitEntry->getInterest() << " interestTo " << outFace.getId() << " multicast");
199  if (this->sendInterest(interest, outFace, pitEntry)) {
200  ++nSent;
201  }
202  }
203  return nSent;
204 }
205 
206 void
207 AccessStrategy::beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
208  const shared_ptr<pit::Entry>& pitEntry)
209 {
210  PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
211  if (pi != nullptr) {
212  pi->rtoTimer.cancel();
213  }
214 
215  if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
216  NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " not-fastest");
217  return;
218  }
219 
220  auto outRecord = pitEntry->getOutRecord(ingress.face);
221  if (outRecord == pitEntry->out_end()) { // no out-record
222  NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " no-out-record");
223  return;
224  }
225 
226  auto rtt = time::steady_clock::now() - outRecord->getLastRenewed();
227  NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress
228  << " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
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:56
FaceId getId() const noexcept
Returns the face ID.
Definition: face.hpp:121
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:42
Face * getFace(FaceId id) const noexcept
Definition: strategy.hpp:372
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
Schedule the PIT entry for immediate deletion.
Definition: strategy.hpp:317
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
Performs a FIB lookup, considering Link object if present.
Definition: strategy.cpp:304
pit::OutRecord * sendInterest(const Interest &interest, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send an Interest packet.
Definition: strategy.cpp:226
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
MeasurementsAccessor & getMeasurements() noexcept
Definition: strategy.hpp:366
void setInstanceName(const Name &name) noexcept
Set strategy instance name.
Definition: strategy.hpp:415
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:50
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:47
@ SUPPRESS
Interest is retransmission and should be suppressed.
@ NEW
Interest is new (not a retransmission)
@ FORWARD
Interest is 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:32
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:45