NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2021, 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 {
32 namespace fw {
33 
36 
38  : Strategy(forwarder)
39  , m_rttEstimatorOpts(make_shared<RttEstimator::Options>()) // use the default options
40  , m_removeFaceConn(beforeRemoveFace.connect([this] (const Face& face) { m_fit.erase(face.getId()); }))
41 {
43  if (!parsed.parameters.empty()) {
44  NDN_THROW(std::invalid_argument("AccessStrategy does not accept parameters"));
45  }
46  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
47  NDN_THROW(std::invalid_argument("AccessStrategy does not support version " + 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
61  const shared_ptr<pit::Entry>& pitEntry)
62 {
63  auto suppressResult = m_retxSuppression.decidePerPitEntry(*pitEntry);
64  switch (suppressResult) {
66  return afterReceiveNewInterest(interest, ingress, pitEntry);
68  return afterReceiveRetxInterest(interest, ingress, pitEntry);
70  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-suppress");
71  return;
72  }
73 }
74 
75 void
76 AccessStrategy::afterReceiveNewInterest(const Interest& interest, const FaceEndpoint& ingress,
77  const shared_ptr<pit::Entry>& pitEntry)
78 {
79  const auto& fibEntry = this->lookupFib(*pitEntry);
80  Name miName;
81  MtInfo* mi = nullptr;
82  std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
83 
84  // has measurements for Interest Name?
85  if (mi != nullptr) {
86  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest mi=" << miName);
87 
88  // send to last working nexthop
89  bool isSentToLastNexthop = this->sendToLastNexthop(interest, ingress, pitEntry, *mi, fibEntry);
90  if (isSentToLastNexthop) {
91  return;
92  }
93  }
94  else {
95  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest no-mi");
96  }
97 
98  // no measurements, or last working nexthop unavailable
99 
100  // multicast to all nexthops except incoming face
101  size_t nMulticastSent = this->multicast(interest, ingress.face, pitEntry, fibEntry);
102 
103  if (nMulticastSent == 0) {
104  this->rejectPendingInterest(pitEntry);
105  }
106 }
107 
108 void
109 AccessStrategy::afterReceiveRetxInterest(const Interest& interest, const FaceEndpoint& ingress,
110  const shared_ptr<pit::Entry>& pitEntry)
111 {
112  const auto& fibEntry = this->lookupFib(*pitEntry);
113  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-forward");
114  this->multicast(interest, ingress.face, pitEntry, fibEntry);
115 }
116 
117 bool
118 AccessStrategy::sendToLastNexthop(const Interest& interest, const FaceEndpoint& ingress,
119  const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
120  const fib::Entry& fibEntry)
121 {
122  if (mi.lastNexthop == face::INVALID_FACEID) {
123  NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
124  return false;
125  }
126 
127  if (mi.lastNexthop == ingress.face.getId()) {
128  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
129  return false;
130  }
131 
132  Face* outFace = this->getFace(mi.lastNexthop);
133  if (outFace == nullptr || !fibEntry.hasNextHop(*outFace)) {
134  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
135  return false;
136  }
137 
138  if (wouldViolateScope(ingress.face, interest, *outFace)) {
139  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
140  return false;
141  }
142 
143  auto rto = mi.rtt.getEstimatedRto();
144  NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop
145  << " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
146 
147  if (!this->sendInterest(interest, *outFace, pitEntry)) {
148  return false;
149  }
150 
151  // schedule RTO timeout
152  PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
153  pi->rtoTimer = getScheduler().schedule(rto,
154  [this, pitWeak = weak_ptr<pit::Entry>(pitEntry), face = ingress.face.getId(), nh = mi.lastNexthop] {
155  afterRtoTimeout(pitWeak, face, nh);
156  });
157 
158  return true;
159 }
160 
161 void
162 AccessStrategy::afterRtoTimeout(const weak_ptr<pit::Entry>& pitWeak,
163  FaceId inFaceId, FaceId firstOutFaceId)
164 {
165  shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
166  // if PIT entry is gone, RTO timer should have been cancelled
167  BOOST_ASSERT(pitEntry != nullptr);
168 
169  Face* inFace = this->getFace(inFaceId);
170  if (inFace == nullptr) {
171  NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
172  << " inFace-gone " << inFaceId);
173  return;
174  }
175 
176  auto inRecord = pitEntry->getInRecord(*inFace);
177  // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
178  // note: if this strategy is extended to send Nacks, that would also erase the in-record,
179  // and the RTO timer should be cancelled in that case as well
180  BOOST_ASSERT(inRecord != pitEntry->in_end());
181 
182  const Interest& interest = inRecord->getInterest();
183  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
184 
185  NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId
186  << " multicast-except " << firstOutFaceId);
187  this->multicast(interest, *inFace, pitEntry, fibEntry, firstOutFaceId);
188 }
189 
190 size_t
191 AccessStrategy::multicast(const Interest& interest, const Face& inFace,
192  const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
193  FaceId exceptFace)
194 {
195  size_t nSent = 0;
196  for (const auto& nexthop : fibEntry.getNextHops()) {
197  Face& outFace = nexthop.getFace();
198  if (&outFace == &inFace || outFace.getId() == exceptFace ||
199  wouldViolateScope(inFace, interest, outFace)) {
200  continue;
201  }
202  NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() << " multicast");
203  if (this->sendInterest(interest, outFace, pitEntry)) {
204  ++nSent;
205  }
206  }
207  return nSent;
208 }
209 
210 void
212  const shared_ptr<pit::Entry>& pitEntry)
213 {
214  PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
215  if (pi != nullptr) {
216  pi->rtoTimer.cancel();
217  }
218 
219  if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
220  NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " not-fastest");
221  return;
222  }
223 
224  auto outRecord = pitEntry->getOutRecord(ingress.face);
225  if (outRecord == pitEntry->out_end()) { // no out-record
226  NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress << " no-out-record");
227  return;
228  }
229 
230  auto rtt = time::steady_clock::now() - outRecord->getLastRenewed();
231  NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << ingress
232  << " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
233  this->updateMeasurements(ingress.face, data, rtt);
234 }
235 
236 void
237 AccessStrategy::updateMeasurements(const Face& inFace, const Data& data, time::nanoseconds rtt)
238 {
239  auto ret = m_fit.emplace(std::piecewise_construct,
240  std::forward_as_tuple(inFace.getId()),
241  std::forward_as_tuple(m_rttEstimatorOpts));
242  FaceInfo& fi = ret.first->second;
243  fi.rtt.addMeasurement(rtt);
244 
245  MtInfo* mi = this->addPrefixMeasurements(data);
246  if (mi->lastNexthop != inFace.getId()) {
247  mi->lastNexthop = inFace.getId();
248  mi->rtt = fi.rtt;
249  }
250  else {
251  mi->rtt.addMeasurement(rtt);
252  }
253 }
254 
255 std::tuple<Name, AccessStrategy::MtInfo*>
256 AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
257 {
258  auto me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
259  if (me == nullptr) {
260  return std::make_tuple(Name(), nullptr);
261  }
262 
263  auto mi = me->getStrategyInfo<MtInfo>();
264  // TODO: after a runtime strategy change, it's possible that a measurements::Entry exists but
265  // the corresponding MtInfo doesn't exist (mi == nullptr); this case needs another longest
266  // prefix match until an MtInfo is found.
267  return std::make_tuple(me->getName(), mi);
268 }
269 
270 AccessStrategy::MtInfo*
271 AccessStrategy::addPrefixMeasurements(const Data& data)
272 {
273  measurements::Entry* me = nullptr;
274  if (!data.getName().empty()) {
275  me = this->getMeasurements().get(data.getName().getPrefix(-1));
276  }
277  if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
278  me = this->getMeasurements().get(data.getName());
279  // Data Name must be in this strategy
280  BOOST_ASSERT(me != nullptr);
281  }
282 
283  this->getMeasurements().extendLifetime(*me, 8_s);
284  return me->insertStrategyInfo<MtInfo>(m_rttEstimatorOpts).first;
285 }
286 
287 } // namespace fw
288 } // namespace nfd
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:209
Interest is retransmission and should be forwarded.
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
std::string to_string(const T &val)
Definition: backports.hpp:86
RTT/RTO estimator.
Represents a face-endpoint pair in the forwarder.
represents a FIB entry
Definition: fib-entry.hpp:53
PartialName parameters
parameter components
Definition: strategy.hpp:390
static time_point now() noexcept
Definition: time.cpp:80
Face * getFace(FaceId id) const
Definition: strategy.hpp:374
boost::chrono::microseconds microseconds
Definition: time.hpp:49
Main class of NFD&#39;s forwarding engine.
Definition: forwarder.hpp:53
Interest is retransmission and should be suppressed.
void setInstanceName(const Name &name)
Set strategy instance name.
Definition: strategy.hpp:417
Represents an Interest packet.
Definition: interest.hpp:48
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
NFD_VIRTUAL_WITH_TESTS void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
Schedule the PIT entry for immediate deletion.
Definition: strategy.hpp:319
#define NDN_THROW(e)
Definition: exception.hpp:61
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:70
Represents a Measurements entry.
ndn Face
Definition: face-impl.hpp:42
NDN_CXX_NODISCARD bool empty() const
Checks if the name is empty, i.e.
Definition: name.hpp:143
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:368
An Interest table entry.
Definition: pit-entry.hpp:58
Interest is new (not a retransmission)
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
static const Name & getStrategyName()
NFD_REGISTER_STRATEGY(AccessStrategy)
const Name & getName() const noexcept
Get name.
Definition: data.hpp:127
Represents an absolute name.
Definition: name.hpp:41
std::pair< T *, bool > insertStrategyInfo(A &&... args)
Insert a StrategyInfo item.
Represents a forwarding strategy.
Definition: strategy.hpp:38
void afterReceiveInterest(const Interest &interest, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry) override
Trigger after an Interest is received.
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:389
This file contains common algorithms used by forwarding strategies.
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
Performs a FIB lookup, considering Link object if present.
Definition: strategy.cpp:331
Entry * get(const Name &name)
find or insert a Measurements entry for name
static Name makeInstanceName(const Name &input, const Name &strategyName)
Construct a strategy instance name.
Definition: strategy.cpp:134
void beforeSatisfyInterest(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry) override
trigger before PIT entry is satisfied
const NextHopList & getNextHops() const
Definition: fib-entry.hpp:66
RetxSuppressionResult decidePerPitEntry(pit::Entry &pitEntry) const
determines whether Interest is a retransmission, and if so, whether it shall be forwarded or suppress...
Access Router strategy.
NFD_VIRTUAL_WITH_TESTS pit::OutRecord * sendInterest(const Interest &interest, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send an Interest packet.
Definition: strategy.cpp:237
Represents a Data packet.
Definition: data.hpp:37
AccessStrategy(Forwarder &forwarder, const Name &name=getStrategyName())
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
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face-common.hpp:47
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50
bool hasNextHop(const Face &face) const
Definition: fib-entry.cpp:46