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 "common/global.hpp"
29 #include "common/logger.hpp"
30 
31 namespace nfd {
32 namespace fw {
33 
36 
37 AccessStrategy::AccessStrategy(Forwarder& forwarder, const Name& name)
38  : Strategy(forwarder)
39  , m_removeFaceInfoConn(beforeRemoveFace.connect([this] (const Face& face) { removeFaceInfo(face); }))
40 {
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 Name strategyName("/localhost/nfd/strategy/access/%FD%01");
55  return strategyName;
56 }
57 
58 void
59 AccessStrategy::afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
60  const shared_ptr<pit::Entry>& pitEntry)
61 {
62  auto suppressResult = m_retxSuppression.decidePerPitEntry(*pitEntry);
63  switch (suppressResult) {
65  return afterReceiveNewInterest(ingress, interest, pitEntry);
67  return afterReceiveRetxInterest(ingress, interest, pitEntry);
69  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-suppress");
70  return;
71  }
72 }
73 
74 void
75 AccessStrategy::afterReceiveNewInterest(const FaceEndpoint& ingress, const Interest& interest,
76  const shared_ptr<pit::Entry>& pitEntry)
77 {
78  const auto& fibEntry = this->lookupFib(*pitEntry);
79  Name miName;
80  MtInfo* mi = nullptr;
81  std::tie(miName, mi) = this->findPrefixMeasurements(*pitEntry);
82 
83  // has measurements for Interest Name?
84  if (mi != nullptr) {
85  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest mi=" << miName);
86 
87  // send to last working nexthop
88  bool isSentToLastNexthop = this->sendToLastNexthop(ingress, interest, pitEntry, *mi, fibEntry);
89  if (isSentToLastNexthop) {
90  return;
91  }
92  }
93  else {
94  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " new-interest no-mi");
95  }
96 
97  // no measurements, or last working nexthop unavailable
98 
99  // multicast to all nexthops except incoming face
100  size_t nMulticastSent = this->multicast(ingress.face, interest, pitEntry, fibEntry);
101 
102  if (nMulticastSent == 0) {
103  this->rejectPendingInterest(pitEntry);
104  }
105 }
106 
107 void
108 AccessStrategy::afterReceiveRetxInterest(const FaceEndpoint& ingress, const Interest& interest,
109  const shared_ptr<pit::Entry>& pitEntry)
110 {
111  const auto& fibEntry = this->lookupFib(*pitEntry);
112  NFD_LOG_DEBUG(interest << " interestFrom " << ingress << " retx-forward");
113  this->multicast(ingress.face, interest, pitEntry, fibEntry);
114 }
115 
116 bool
117 AccessStrategy::sendToLastNexthop(const FaceEndpoint& ingress, const Interest& interest,
118  const shared_ptr<pit::Entry>& pitEntry, MtInfo& mi,
119  const fib::Entry& fibEntry)
120 {
121  if (mi.lastNexthop == face::INVALID_FACEID) {
122  NFD_LOG_DEBUG(pitEntry->getInterest() << " no-last-nexthop");
123  return false;
124  }
125 
126  if (mi.lastNexthop == ingress.face.getId()) {
127  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-is-downstream");
128  return false;
129  }
130 
131  Face* outFace = this->getFace(mi.lastNexthop);
132  if (outFace == nullptr || !fibEntry.hasNextHop(*outFace, 0)) {
133  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
134  return false;
135  }
136 
137  if (wouldViolateScope(ingress.face, interest, *outFace)) {
138  NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-violates-scope");
139  return false;
140  }
141 
142  auto rto = mi.rtt.computeRto();
143  NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << mi.lastNexthop
144  << " last-nexthop rto=" << time::duration_cast<time::microseconds>(rto).count());
145 
146  this->sendInterest(pitEntry, FaceEndpoint(*outFace, 0), interest);
147 
148  // schedule RTO timeout
149  PitInfo* pi = pitEntry->insertStrategyInfo<PitInfo>().first;
150  pi->rtoTimer = getScheduler().schedule(rto, [=] {
151  afterRtoTimeout(weak_ptr<pit::Entry>(pitEntry),
152  ingress.face.getId(), ingress.endpoint, mi.lastNexthop);
153  });
154 
155  return true;
156 }
157 
158 void
159 AccessStrategy::afterRtoTimeout(const weak_ptr<pit::Entry>& pitWeak,
160  FaceId inFaceId, EndpointId inEndpointId, 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->getInterest() << " timeoutFrom " << firstOutFaceId
169  << " inFace-gone " << inFaceId);
170  return;
171  }
172 
173  auto inRecord = pitEntry->getInRecord(*inFace, inEndpointId);
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->getInterest() << " timeoutFrom " << firstOutFaceId
183  << " multicast-except " << firstOutFaceId);
184  this->multicast(*inFace, interest, pitEntry, fibEntry, firstOutFaceId);
185 }
186 
187 size_t
188 AccessStrategy::multicast(const Face& inFace, const Interest& interest,
189  const shared_ptr<pit::Entry>& pitEntry, const fib::Entry& fibEntry,
190  FaceId exceptFace)
191 {
192  size_t nSent = 0;
193  for (const auto& nexthop : fibEntry.getNextHops()) {
194  Face& outFace = nexthop.getFace();
195  if (&outFace == &inFace || outFace.getId() == exceptFace ||
196  wouldViolateScope(inFace, interest, outFace)) {
197  continue;
198  }
199  NFD_LOG_DEBUG(pitEntry->getInterest() << " interestTo " << outFace.getId() << " multicast");
200  this->sendInterest(pitEntry, FaceEndpoint(outFace, 0), interest);
201  ++nSent;
202  }
203  return nSent;
204 }
205 
206 void
207 AccessStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
208  const FaceEndpoint& ingress, const Data& data)
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, 0);
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, time::duration_cast<RttEstimator::Duration>(rtt));
230 }
231 
232 void
233 AccessStrategy::updateMeasurements(const Face& inFace, const Data& data,
234  const RttEstimator::Duration& rtt)
235 {
237  FaceInfo& fi = m_fit[inFace.getId()];
238  fi.rtt.addMeasurement(rtt);
239 
240  MtInfo* mi = this->addPrefixMeasurements(data);
241  if (mi->lastNexthop != inFace.getId()) {
242  mi->lastNexthop = inFace.getId();
243  mi->rtt = fi.rtt;
244  }
245  else {
246  mi->rtt.addMeasurement(rtt);
247  }
248 }
249 
250 std::tuple<Name, AccessStrategy::MtInfo*>
251 AccessStrategy::findPrefixMeasurements(const pit::Entry& pitEntry)
252 {
253  measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
254  if (me == nullptr) {
255  return std::make_tuple(Name(), nullptr);
256  }
257 
258  MtInfo* mi = me->getStrategyInfo<MtInfo>();
259  BOOST_ASSERT(mi != nullptr);
260  // XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
261  // this case needs another longest prefix match until mi is found
262  return std::make_tuple(me->getName(), mi);
263 }
264 
265 AccessStrategy::MtInfo*
266 AccessStrategy::addPrefixMeasurements(const Data& data)
267 {
268  measurements::Entry* me = nullptr;
269  if (!data.getName().empty()) {
270  me = this->getMeasurements().get(data.getName().getPrefix(-1));
271  }
272  if (me == nullptr) { // parent of Data Name is not in this strategy, or Data Name is empty
273  me = this->getMeasurements().get(data.getName());
274  // Data Name must be in this strategy
275  BOOST_ASSERT(me != nullptr);
276  }
277 
278  this->getMeasurements().extendLifetime(*me, 8_s);
279 
280  return me->insertStrategyInfo<MtInfo>().first;
281 }
282 
283 void
284 AccessStrategy::removeFaceInfo(const Face& face)
285 {
286  m_fit.erase(face.getId());
287 }
288 
289 } // namespace fw
290 } // namespace nfd
const EndpointId endpoint
Main class of NFD forwarding engine.
Definition: forwarder.hpp:51
Interest is retransmission and should be forwarded.
void setInstanceName(const Name &name)
set strategy instance name
Definition: strategy.hpp:369
const Name & getName() const
Represents a Measurements entry.
represents a FIB entry
Definition: fib-entry.hpp:51
RetxSuppressionResult decidePerPitEntry(pit::Entry &pitEntry) const
determines whether Interest is a retransmission, and if so, whether it shall be forwarded or suppress...
Interest is retransmission and should be suppressed.
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
Definition: strategy.cpp:132
std::pair< T *, bool > insertStrategyInfo(A &&...args)
Insert a StrategyInfo item.
AccessStrategy(Forwarder &forwarder, const Name &name=getStrategyName())
time::microseconds Duration
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:320
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:45
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &egress, const Interest &interest)
send Interest to egress
Definition: strategy.hpp:243
An Interest table entry.
Definition: pit-entry.hpp:58
Access Router Strategy version 1.
Represents a face-endpoint pair in the forwarder.
uint64_t EndpointId
identifies an endpoint on the link
Definition: transport.hpp:38
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
Interest is new (not a retransmission)
PartialName parameters
parameter components
Definition: strategy.hpp:342
void afterReceiveInterest(const FaceEndpoint &ingress, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Interest is received
represents a forwarding strategy
Definition: strategy.hpp:37
This file contains common algorithms used by forwarding strategies.
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:121
T * getStrategyInfo() const
Get a StrategyInfo item.
static const Name & getStrategyName()
uint64_t FaceId
identifies a face
Definition: face.hpp:39
bool hasNextHop(const Face &face, EndpointId endpointId) const
Definition: fib-entry.cpp:46
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
const NextHopList & getNextHops() const
Definition: fib-entry.hpp:64
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face.hpp:42
Face * getFace(FaceId id) const
Definition: strategy.hpp:326
void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data) override
trigger before PIT entry is satisfied
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
schedule the PIT entry for immediate deletion
Definition: strategy.hpp:276
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:255
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:341