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-2019, 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 "core/logger.hpp"
29 
30 namespace nfd {
31 namespace fw {
32 
35 
37  : Strategy(forwarder)
38  , m_removeFaceInfoConn(beforeRemoveFace.connect([this] (const Face& face) { removeFaceInfo(face); }))
39 {
40  ParsedInstanceName parsed = parseInstanceName(name);
41  if (!parsed.parameters.empty()) {
42  BOOST_THROW_EXCEPTION(std::invalid_argument("AccessStrategy does not accept parameters"));
43  }
44  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
45  BOOST_THROW_EXCEPTION(std::invalid_argument(
46  "AccessStrategy does not support version " + to_string(*parsed.version)));
47  }
49 }
50 
51 const Name&
53 {
54  static Name strategyName("/localhost/nfd/strategy/access/%FD%01");
55  return strategyName;
56 }
57 
58 void
59 AccessStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
60  const shared_ptr<pit::Entry>& pitEntry)
61 {
62  RetxSuppressionResult suppressResult = m_retxSuppression.decidePerPitEntry(*pitEntry);
63  switch (suppressResult) {
65  this->afterReceiveNewInterest(inFace, interest, pitEntry);
66  break;
68  this->afterReceiveRetxInterest(inFace, interest, pitEntry);
69  break;
71  NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-suppress");
72  break;
73  default:
74  BOOST_ASSERT(false);
75  break;
76  }
77 }
78 
79 void
80 AccessStrategy::afterReceiveNewInterest(const Face& inFace, const Interest& interest,
81  const shared_ptr<pit::Entry>& pitEntry)
82 {
83  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
84  Name miName;
85  MtInfo* mi = nullptr;
86  std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
87 
88  // has measurements for Interest Name?
89  if (mi != nullptr) {
90  NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
91  " new-interest mi=" << miName);
92 
93  // send to last working nexthop
94  bool isSentToLastNexthop = this->sendToLastNexthop(inFace, interest, pitEntry, *mi, fibEntry);
95 
96  if (isSentToLastNexthop) {
97  return;
98  }
99  }
100  else {
101  NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() <<
102  " new-interest no-mi");
103  }
104 
105  // no measurements, or last working nexthop unavailable
106 
107  // multicast to all nexthops except incoming face
108  int nMulticastSent = this->multicast(inFace, interest, pitEntry, fibEntry);
109 
110  if (nMulticastSent < 1) {
111  this->rejectPendingInterest(pitEntry);
112  }
113 }
114 
115 void
116 AccessStrategy::afterReceiveRetxInterest(const Face& inFace, const Interest& interest,
117  const shared_ptr<pit::Entry>& pitEntry)
118 {
119  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
120  NFD_LOG_DEBUG(interest << " interestFrom " << inFace.getId() << " retx-forward");
121  this->multicast(inFace, interest, pitEntry, fibEntry);
122 }
123 
124 bool
125 AccessStrategy::sendToLastNexthop(const Face& inFace, const Interest& interest,
126  const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
127  const fib::Entry& fibEntry)
128 {
129  if (mi.lastNexthop == face::INVALID_FACEID) {
130  NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
131  return false;
132  }
133 
134  if (mi.lastNexthop == inFace.getId()) {
135  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
136  return false;
137  }
138 
139  Face* outFace = this->getFace(mi.lastNexthop);
140  if (outFace == nullptr || !fibEntry.hasNextHop(*outFace, 0)) {
141  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
142  return false;
143  }
144 
145  if (wouldViolateScope(inFace, interest, *outFace)) {
146  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
147  return false;
148  }
149 
150  RttEstimator::Duration rto = mi.rtt.computeRto();
151  NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop <<
152  " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
153 
154  this->sendInterest(pitEntry, *outFace, interest);
155 
156  // schedule RTO timeout
157  PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
158  pi->rtoTimer = scheduler::schedule(rto,
159  bind(&AccessStrategy::afterRtoTimeout, this, weak_ptr<pit::Entry>(pitEntry),
160  inFace.getId(), mi.lastNexthop));
161 
162  return true;
163 }
164 
165 void
166 AccessStrategy::afterRtoTimeout(weak_ptr<pit::Entry> pitWeak, FaceId inFaceId, FaceId firstOutFaceId)
167 {
168  shared_ptr<pit::Entry> pitEntry = pitWeak.lock();
169  BOOST_ASSERT(pitEntry != nullptr);
170  // if pitEntry is gone, RTO timer should have been cancelled
171 
172  Face* inFace = this->getFace(inFaceId);
173  if (inFace == nullptr) {
174  NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
175  " inFace-gone " << inFaceId);
176  return;
177  }
178 
179  auto inRecord = pitEntry->getInRecord(*inFace);
180  BOOST_ASSERT(inRecord != pitEntry->in_end());
181  // in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
182  // note: if this strategy is extended to send Nacks, that would also erase in-record,
183  // and RTO timer should be cancelled in that case as well
184 
185  const Interest& interest = inRecord->getInterest();
186  const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
187 
188  NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
189  " multicast-except " << firstOutFaceId);
190  this->multicast(*inFace, interest, pitEntry, fibEntry, firstOutFaceId);
191 }
192 
193 int
194 AccessStrategy::multicast(const Face& inFace, const Interest& interest,
195  const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
196  FaceId exceptFace)
197 {
198  int nSent = 0;
199  for (const fib::NextHop& nexthop : fibEntry.getNextHops()) {
200  Face& outFace = nexthop.getFace();
201  if (&outFace == &inFace || outFace.getId() == exceptFace ||
202  wouldViolateScope(inFace, interest, outFace)) {
203  continue;
204  }
205  NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() <<
206  " multicast");
207  this->sendInterest(pitEntry, outFace, interest);
208  ++nSent;
209  }
210  return nSent;
211 }
212 
213 void
214 AccessStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
215  const Face& inFace, const Data& data)
216 {
217  PitInfo* pi = pitEntry->getStrategyInfo<PitInfo>();
218  if (pi != nullptr) {
219  pi->rtoTimer.cancel();
220  }
221 
222  if (!pitEntry->hasInRecords()) { // already satisfied by another upstream
223  NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
224  " not-fastest");
225  return;
226  }
227 
228  auto outRecord = pitEntry->getOutRecord(inFace);
229  if (outRecord == pitEntry->out_end()) { // no out-record
230  NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
231  " no-out-record");
232  return;
233  }
234 
235  auto rtt = time::steady_clock::now() - outRecord->getLastRenewed();
236  NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
237  " rtt=" << time::duration_cast<time::microseconds>(rtt).count());
238  this->updateMeasurements(inFace, data, time::duration_cast<RttEstimator::Duration>(rtt));
239 }
240 
241 void
242 AccessStrategy::updateMeasurements(const Face& inFace, const Data& data,
243  const RttEstimator::Duration& rtt)
244 {
246  FaceInfo& fi = m_fit[inFace.getId()];
247  fi.rtt.addMeasurement(rtt);
248 
249  MtInfo* mi = this->addPrefixMeasurements(data);
250  if (mi->lastNexthop != inFace.getId()) {
251  mi->lastNexthop = inFace.getId();
252  mi->rtt = fi.rtt;
253  }
254  else {
255  mi->rtt.addMeasurement(rtt);
256  }
257 }
258 
259 AccessStrategy::MtInfo::MtInfo()
260  : lastNexthop(face::INVALID_FACEID)
261  , rtt(1, 1_ms, 0.1)
262 {
263 }
264 
265 std::tuple<Name, AccessStrategy::MtInfo*>
266 AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
267 {
268  measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
269  if (me == nullptr) {
270  return std::make_tuple(Name(), nullptr);
271  }
272 
273  MtInfo* mi = me->getStrategyInfo<MtInfo>();
274  BOOST_ASSERT(mi != nullptr);
275  // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
276  // this case needs another longest prefix match until mi is found
277  return std::make_tuple(me->getName(), mi);
278 }
279 
280 AccessStrategy::MtInfo*
281 AccessStrategy::addPrefixMeasurements(const Data& data)
282 {
283  measurements::Entry* me = nullptr;
284  if (!data.getName().empty()) {
285  me = this->getMeasurements().get(data.getName().getPrefix(-1));
286  }
287  if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
288  me = this->getMeasurements().get(data.getName());
289  // Data Name must be in this strategy
290  BOOST_ASSERT(me != nullptr);
291  }
292 
293  this->getMeasurements().extendLifetime(*me, 8_s);
294 
295  return me->insertStrategyInfo<MtInfo>().first;
296 }
297 
298 AccessStrategy::FaceInfo::FaceInfo()
299  : rtt(1, 1_ms, 0.1)
300 {
301 }
302 
303 void
304 AccessStrategy::removeFaceInfo(const Face& face)
305 {
306  m_fit.erase(face.getId());
307 }
308 
309 } // namespace fw
310 } // namespace nfd
Interest is retransmission and should be forwarded.
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:122
time::microseconds Duration
generalization of a network interface
Definition: face.hpp:67
represents a FIB entry
Definition: fib-entry.hpp:51
static time_point now() noexcept
Definition: time.cpp:80
Face * getFace(FaceId id) const
Definition: strategy.hpp:324
main class of NFD
Definition: forwarder.hpp:54
Interest is retransmission and should be suppressed.
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:367
Represents an Interest packet.
Definition: interest.hpp:44
void afterReceiveInterest(const Face &inFace, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Interest is received
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
ndn Face
Definition: face-impl.hpp:42
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:318
Interest is new (not a retransmission)
static const Name & getStrategyName()
FaceId getId() const
Definition: face.hpp:233
NFD_REGISTER_STRATEGY(AccessStrategy)
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
schedule the PIT entry for immediate deletion
Definition: strategy.hpp:273
Represents an absolute name.
Definition: name.hpp:43
represents a forwarding strategy
Definition: strategy.hpp:37
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data) override
trigger before PIT entry is satisfied
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:253
EventId schedule(time::nanoseconds after, const EventCallback &event)
Schedule an event.
Definition: scheduler.cpp:48
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:133
std::string to_string(const V &v)
Definition: backports.hpp:67
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 version 1.
uint64_t FaceId
identifies a face
Definition: face.hpp:39
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
Represents a Data packet.
Definition: data.hpp:35
AccessStrategy(Forwarder &forwarder, const Name &name=getStrategyName())
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &outFace, const Interest &interest)
send Interest to outFace
Definition: strategy.hpp:241
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
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face.hpp:42