NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2018, 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 "strategy.hpp"
27 #include "forwarder.hpp"
28 #include "core/logger.hpp"
29 #include "core/random.hpp"
30 
31 #include <boost/range/adaptor/map.hpp>
32 #include <boost/range/algorithm/copy.hpp>
33 
34 namespace nfd {
35 namespace fw {
36 
38 
39 Strategy::Registry&
40 Strategy::getRegistry()
41 {
42  static Registry registry;
43  return registry;
44 }
45 
46 Strategy::Registry::const_iterator
47 Strategy::find(const Name& instanceName)
48 {
49  const Registry& registry = getRegistry();
50  ParsedInstanceName parsed = parseInstanceName(instanceName);
51 
52  if (parsed.version) {
53  // specified version: find exact or next higher version
54 
55  auto found = registry.lower_bound(parsed.strategyName);
56  if (found != registry.end()) {
57  if (parsed.strategyName.getPrefix(-1).isPrefixOf(found->first)) {
58  NFD_LOG_TRACE("find " << instanceName << " versioned found=" << found->first);
59  return found;
60  }
61  }
62 
63  NFD_LOG_TRACE("find " << instanceName << " versioned not-found");
64  return registry.end();
65  }
66 
67  // no version specified: find highest version
68 
69  if (!parsed.strategyName.empty()) { // Name().getSuccessor() would be invalid
70  auto found = registry.lower_bound(parsed.strategyName.getSuccessor());
71  if (found != registry.begin()) {
72  --found;
73  if (parsed.strategyName.isPrefixOf(found->first)) {
74  NFD_LOG_TRACE("find " << instanceName << " unversioned found=" << found->first);
75  return found;
76  }
77  }
78  }
79 
80  NFD_LOG_TRACE("find " << instanceName << " unversioned not-found");
81  return registry.end();
82 }
83 
84 bool
85 Strategy::canCreate(const Name& instanceName)
86 {
87  return Strategy::find(instanceName) != getRegistry().end();
88 }
89 
90 unique_ptr<Strategy>
91 Strategy::create(const Name& instanceName, Forwarder& forwarder)
92 {
93  auto found = Strategy::find(instanceName);
94  if (found == getRegistry().end()) {
95  NFD_LOG_DEBUG("create " << instanceName << " not-found");
96  return nullptr;
97  }
98 
99  unique_ptr<Strategy> instance = found->second(forwarder, instanceName);
100  NFD_LOG_DEBUG("create " << instanceName << " found=" << found->first <<
101  " created=" << instance->getInstanceName());
102  BOOST_ASSERT(!instance->getInstanceName().empty());
103  return instance;
104 }
105 
106 bool
107 Strategy::areSameType(const Name& instanceNameA, const Name& instanceNameB)
108 {
109  return Strategy::find(instanceNameA) == Strategy::find(instanceNameB);
110 }
111 
112 std::set<Name>
114 {
115  std::set<Name> strategyNames;
116  boost::copy(getRegistry() | boost::adaptors::map_keys,
117  std::inserter(strategyNames, strategyNames.end()));
118  return strategyNames;
119 }
120 
123 {
124  for (ssize_t i = input.size() - 1; i > 0; --i) {
125  if (input[i].isVersion()) {
126  return {input.getPrefix(i + 1), input[i].toVersion(), input.getSubName(i + 1)};
127  }
128  }
129  return {input, nullopt, PartialName()};
130 }
131 
132 Name
133 Strategy::makeInstanceName(const Name& input, const Name& strategyName)
134 {
135  BOOST_ASSERT(strategyName.at(-1).isVersion());
136 
137  bool hasVersion = std::any_of(input.rbegin(), input.rend(),
138  [] (const name::Component& comp) { return comp.isVersion(); });
139  return hasVersion ? input : Name(input).append(strategyName.at(-1));
140 }
141 
143  : afterAddFace(forwarder.getFaceTable().afterAdd)
144  , beforeRemoveFace(forwarder.getFaceTable().beforeRemove)
145  , m_forwarder(forwarder)
146  , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this)
147 {
148 }
149 
150 Strategy::~Strategy() = default;
151 
152 void
153 Strategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
154  const Face& inFace, const Data& data)
155 {
156  NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName() <<
157  " inFace=" << inFace.getId() << " data=" << data.getName());
158 }
159 
160 void
161 Strategy::afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
162  const Face& inFace, const Data& data)
163 {
164  NFD_LOG_DEBUG("afterContentStoreHit pitEntry=" << pitEntry->getName() <<
165  " inFace=" << inFace.getId() << " data=" << data.getName());
166 
167  this->sendData(pitEntry, data, inFace);
168 }
169 
170 void
171 Strategy::afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
172  const Face& inFace, const Data& data)
173 {
174  NFD_LOG_DEBUG("afterReceiveData pitEntry=" << pitEntry->getName() <<
175  " inFace=" << inFace.getId() << " data=" << data.getName());
176 
177  this->beforeSatisfyInterest(pitEntry, inFace, data);
178 
179  this->sendDataToAll(pitEntry, inFace, data);
180 }
181 
182 void
183 Strategy::afterReceiveNack(const Face& inFace, const lp::Nack& nack,
184  const shared_ptr<pit::Entry>& pitEntry)
185 {
186  NFD_LOG_DEBUG("afterReceiveNack inFace=" << inFace.getId() <<
187  " pitEntry=" << pitEntry->getName());
188 }
189 
190 void
191 Strategy::onDroppedInterest(const Face& outFace, const Interest& interest)
192 {
193  NFD_LOG_DEBUG("onDroppedInterest outFace=" << outFace.getId() << " name=" << interest.getName());
194 }
195 
196 void
197 Strategy::sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, const Face& outFace)
198 {
199  BOOST_ASSERT(pitEntry->getInterest().matchesData(data));
200 
201  // delete the PIT entry's in-record based on outFace,
202  // since Data is sent to outFace from which the Interest was received
203  pitEntry->deleteInRecord(outFace);
204 
205  m_forwarder.onOutgoingData(data, *const_pointer_cast<Face>(outFace.shared_from_this()));
206 }
207 
208 void
209 Strategy::sendDataToAll(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data)
210 {
211  std::set<Face*> pendingDownstreams;
212  auto now = time::steady_clock::now();
213 
214  // remember pending downstreams
215  for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
216  if (inRecord.getExpiry() > now) {
217  if (inRecord.getFace().getId() == inFace.getId() &&
218  inRecord.getFace().getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
219  continue;
220  }
221  pendingDownstreams.insert(&inRecord.getFace());
222  }
223  }
224 
225  for (const Face* pendingDownstream : pendingDownstreams) {
226  this->sendData(pitEntry, data, *pendingDownstream);
227  }
228 }
229 
230 void
231 Strategy::sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
232  std::initializer_list<const Face*> exceptFaces)
233 {
234  // populate downstreams with all downstreams faces
235  std::unordered_set<const Face*> downstreams;
236  std::transform(pitEntry->in_begin(), pitEntry->in_end(), std::inserter(downstreams, downstreams.end()),
237  [] (const pit::InRecord& inR) { return &inR.getFace(); });
238 
239  // delete excluded faces
240  // .erase in a loop is more efficient than std::set_difference because that requires sorted range
241  for (const Face* exceptFace : exceptFaces) {
242  downstreams.erase(exceptFace);
243  }
244 
245  // send Nacks
246  for (const Face* downstream : downstreams) {
247  this->sendNack(pitEntry, *downstream, header);
248  }
249  // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack
250 }
251 
252 const fib::Entry&
253 Strategy::lookupFib(const pit::Entry& pitEntry) const
254 {
255  const Fib& fib = m_forwarder.getFib();
256 
257  const Interest& interest = pitEntry.getInterest();
258  // has forwarding hint?
259  if (interest.getForwardingHint().empty()) {
260  // FIB lookup with Interest name
261  const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
262  NFD_LOG_TRACE("lookupFib noForwardingHint found=" << fibEntry.getPrefix());
263  return fibEntry;
264  }
265 
266  const DelegationList& fh = interest.getForwardingHint();
267  // Forwarding hint should have been stripped by incoming Interest pipeline when reaching producer region
268  BOOST_ASSERT(!m_forwarder.getNetworkRegionTable().isInProducerRegion(fh));
269 
270  const fib::Entry* fibEntry = nullptr;
271  for (const Delegation& del : fh) {
272  fibEntry = &fib.findLongestPrefixMatch(del.name);
273  if (fibEntry->hasNextHops()) {
274  if (fibEntry->getPrefix().size() == 0) {
275  // in consumer region, return the default route
276  NFD_LOG_TRACE("lookupFib inConsumerRegion found=" << fibEntry->getPrefix());
277  }
278  else {
279  // in default-free zone, use the first delegation that finds a FIB entry
280  NFD_LOG_TRACE("lookupFib delegation=" << del.name << " found=" << fibEntry->getPrefix());
281  }
282  return *fibEntry;
283  }
284  BOOST_ASSERT(fibEntry->getPrefix().size() == 0); // only ndn:/ FIB entry can have zero nexthop
285  }
286  BOOST_ASSERT(fibEntry != nullptr && fibEntry->getPrefix().size() == 0);
287  return *fibEntry; // only occurs if no delegation finds a FIB nexthop
288 }
289 
290 } // namespace fw
291 } // namespace nfd
Declares the global pseudorandom number generator (PRNG) for NFD.
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:203
const Name & getPrefix() const
Definition: fib-entry.hpp:58
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:122
const Name & getName() const
Get name.
Definition: data.hpp:124
static std::set< Name > listRegistered()
Definition: strategy.cpp:113
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
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const Face &outFace, const lp::NackHeader &header)
send Nack to outFace
Definition: strategy.hpp:286
void sendNacks(const shared_ptr< pit::Entry > &pitEntry, const lp::NackHeader &header, std::initializer_list< const Face * > exceptFaces=std::initializer_list< const Face * >())
send Nack to every face that has an in-record, except those in exceptFaces
Definition: strategy.cpp:231
contains information about an Interest from an incoming face
Fib & getFib()
Definition: forwarder.hpp:146
virtual void afterContentStoreHit(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
trigger after a Data is matched in CS
Definition: strategy.cpp:161
Strategy(Forwarder &forwarder)
construct a strategy instance
Definition: strategy.cpp:142
const_reverse_iterator rend() const
Reverse end iterator.
Definition: name.hpp:239
main class of NFD
Definition: forwarder.hpp:54
Represents an Interest packet.
Definition: interest.hpp:44
const_reverse_iterator rbegin() const
Reverse begin iterator.
Definition: name.hpp:231
represents a delegation
Definition: delegation.hpp:32
const Entry & findLongestPrefixMatch(const Name &prefix) const
performs a longest prefix match
Definition: fib.cpp:63
Face & getFace() const
represents a Network Nack
Definition: nack.hpp:38
virtual void afterReceiveNack(const Face &inFace, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
trigger after Nack is received
Definition: strategy.cpp:183
virtual ~Strategy()
const Component & at(ssize_t i) const
Get the component at the given index.
Definition: name.cpp:179
bool isInProducerRegion(const DelegationList &forwardingHint) const
determines whether an Interest has reached a producer region
virtual void onDroppedInterest(const Face &outFace, const Interest &interest)
trigger after Interest dropped for exceeding allowed retransmissions
Definition: strategy.cpp:191
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
#define NFD_LOG_TRACE
Definition: logger.hpp:37
an Interest table entry
Definition: pit-entry.hpp:59
virtual void afterReceiveData(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
trigger after Data is received
Definition: strategy.cpp:171
represents the Forwarding Information Base (FIB)
Definition: fib.hpp:49
FaceId getId() const
Definition: face.hpp:233
Name PartialName
Represents an arbitrary sequence of name components.
Definition: name.hpp:39
virtual void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
trigger before PIT entry is satisfied
Definition: strategy.cpp:153
Represents an absolute name.
Definition: name.hpp:43
const Interest & getInterest() const
Definition: pit-entry.hpp:71
size_t size() const
Get number of components.
Definition: name.hpp:147
represents a forwarding strategy
Definition: strategy.hpp:37
void sendData(const shared_ptr< pit::Entry > &pitEntry, const Data &data, const Face &outFace)
send data to outFace
Definition: strategy.cpp:197
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:91
Represents a name component.
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:253
bool empty() const noexcept
NetworkRegionTable & getNetworkRegionTable()
Definition: forwarder.hpp:182
const DelegationList & getForwardingHint() const
Definition: interest.hpp:223
represents a list of Delegations
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
Definition: strategy.cpp:133
void sendDataToAll(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
send data to all matched and qualified faces
Definition: strategy.cpp:209
bool isVersion() const
Check if the component is version per NDN naming conventions.
static bool canCreate(const Name &instanceName)
Definition: strategy.cpp:85
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
Represents a Data packet.
Definition: data.hpp:35
represents a Network NACK header
Definition: nack-header.hpp:57
PartialName getSubName(ssize_t iStartComponent, size_t nComponents=npos) const
Extract some components as a sub-name (PartialName)
Definition: name.cpp:193
const nullopt_t nullopt((nullopt_t::init()))
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:107
const Name & getName() const
Definition: interest.hpp:134