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-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 "strategy.hpp"
27 #include "forwarder.hpp"
28 #include "common/logger.hpp"
29 
30 #include <ndn-cxx/lp/pit-token.hpp>
31 
32 #include <boost/range/adaptor/map.hpp>
33 #include <boost/range/algorithm/copy.hpp>
34 
35 namespace nfd {
36 namespace fw {
37 
39 
40 Strategy::Registry&
41 Strategy::getRegistry()
42 {
43  static Registry registry;
44  return registry;
45 }
46 
47 Strategy::Registry::const_iterator
48 Strategy::find(const Name& instanceName)
49 {
50  const Registry& registry = getRegistry();
51  ParsedInstanceName parsed = parseInstanceName(instanceName);
52 
53  if (parsed.version) {
54  // specified version: find exact or next higher version
55 
56  auto found = registry.lower_bound(parsed.strategyName);
57  if (found != registry.end()) {
58  if (parsed.strategyName.getPrefix(-1).isPrefixOf(found->first)) {
59  NFD_LOG_TRACE("find " << instanceName << " versioned found=" << found->first);
60  return found;
61  }
62  }
63 
64  NFD_LOG_TRACE("find " << instanceName << " versioned not-found");
65  return registry.end();
66  }
67 
68  // no version specified: find highest version
69 
70  if (!parsed.strategyName.empty()) { // Name().getSuccessor() would be invalid
71  auto found = registry.lower_bound(parsed.strategyName.getSuccessor());
72  if (found != registry.begin()) {
73  --found;
74  if (parsed.strategyName.isPrefixOf(found->first)) {
75  NFD_LOG_TRACE("find " << instanceName << " unversioned found=" << found->first);
76  return found;
77  }
78  }
79  }
80 
81  NFD_LOG_TRACE("find " << instanceName << " unversioned not-found");
82  return registry.end();
83 }
84 
85 bool
86 Strategy::canCreate(const Name& instanceName)
87 {
88  return Strategy::find(instanceName) != getRegistry().end();
89 }
90 
91 unique_ptr<Strategy>
92 Strategy::create(const Name& instanceName, Forwarder& forwarder)
93 {
94  auto found = Strategy::find(instanceName);
95  if (found == getRegistry().end()) {
96  NFD_LOG_DEBUG("create " << instanceName << " not-found");
97  return nullptr;
98  }
99 
100  unique_ptr<Strategy> instance = found->second(forwarder, instanceName);
101  NFD_LOG_DEBUG("create " << instanceName << " found=" << found->first
102  << " created=" << instance->getInstanceName());
103  BOOST_ASSERT(!instance->getInstanceName().empty());
104  return instance;
105 }
106 
107 bool
108 Strategy::areSameType(const Name& instanceNameA, const Name& instanceNameB)
109 {
110  return Strategy::find(instanceNameA) == Strategy::find(instanceNameB);
111 }
112 
113 std::set<Name>
115 {
116  std::set<Name> strategyNames;
117  boost::copy(getRegistry() | boost::adaptors::map_keys,
118  std::inserter(strategyNames, strategyNames.end()));
119  return strategyNames;
120 }
121 
124 {
125  for (ssize_t i = input.size() - 1; i > 0; --i) {
126  if (input[i].isVersion()) {
127  return {input.getPrefix(i + 1), input[i].toVersion(), input.getSubName(i + 1)};
128  }
129  }
130  return {input, nullopt, PartialName()};
131 }
132 
133 Name
134 Strategy::makeInstanceName(const Name& input, const Name& strategyName)
135 {
136  BOOST_ASSERT(strategyName.at(-1).isVersion());
137 
138  bool hasVersion = std::any_of(input.rbegin(), input.rend(),
139  [] (const name::Component& comp) { return comp.isVersion(); });
140  return hasVersion ? input : Name(input).append(strategyName.at(-1));
141 }
142 
144  : afterAddFace(forwarder.m_faceTable.afterAdd)
145  , beforeRemoveFace(forwarder.m_faceTable.beforeRemove)
146  , m_forwarder(forwarder)
147  , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this)
148 {
149 }
150 
151 Strategy::~Strategy() = default;
152 
153 
154 void
156  pit::Entry& pitEntry)
157 {
158  NFD_LOG_DEBUG("afterReceiveLoopedInterest pitEntry=" << pitEntry.getName()
159  << " in=" << ingress);
160 }
161 
162 void
163 Strategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
164  const FaceEndpoint& ingress, const Data& data)
165 {
166  NFD_LOG_DEBUG("beforeSatisfyInterest pitEntry=" << pitEntry->getName()
167  << " in=" << ingress << " data=" << data.getName());
168 }
169 
170 void
171 Strategy::afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
172  const FaceEndpoint& ingress, const Data& data)
173 {
174  NFD_LOG_DEBUG("afterContentStoreHit pitEntry=" << pitEntry->getName()
175  << " in=" << ingress << " data=" << data.getName());
176 
177  this->sendData(pitEntry, data, ingress);
178 }
179 
180 void
181 Strategy::afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
182  const FaceEndpoint& ingress, const Data& data)
183 {
184  NFD_LOG_DEBUG("afterReceiveData pitEntry=" << pitEntry->getName()
185  << " in=" << ingress << " data=" << data.getName());
186 
187  this->beforeSatisfyInterest(pitEntry, ingress, data);
188 
189  this->sendDataToAll(pitEntry, ingress, data);
190 }
191 
192 void
194  const shared_ptr<pit::Entry>& pitEntry)
195 {
196  NFD_LOG_DEBUG("afterReceiveNack in=" << ingress << " pitEntry=" << pitEntry->getName());
197 }
198 
199 void
200 Strategy::onDroppedInterest(const FaceEndpoint& egress, const Interest& interest)
201 {
202  NFD_LOG_DEBUG("onDroppedInterest out=" << egress << " name=" << interest.getName());
203 }
204 
205 void
206 Strategy::sendInterest(const shared_ptr<pit::Entry>& pitEntry,
207  const FaceEndpoint& egress, const Interest& interest)
208 {
209  if (interest.getTag<lp::PitToken>() != nullptr) {
210  Interest interest2 = interest; // make a copy to preserve tag on original packet
211  interest2.removeTag<lp::PitToken>();
212  m_forwarder.onOutgoingInterest(pitEntry, egress, interest2);
213  return;
214  }
215  m_forwarder.onOutgoingInterest(pitEntry, egress, interest);
216 }
217 
218 void
219 Strategy::afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry)
220 {
221  NFD_LOG_DEBUG("afterNewNextHop pitEntry=" << pitEntry->getName()
222  << " nexthop=" << nextHop.getFace().getId());
223 }
224 
225 void
226 Strategy::sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data,
227  const FaceEndpoint& egress)
228 {
229  BOOST_ASSERT(pitEntry->getInterest().matchesData(data));
230 
231  shared_ptr<lp::PitToken> pitToken;
232  auto inRecord = pitEntry->getInRecord(egress.face);
233  if (inRecord != pitEntry->in_end()) {
234  pitToken = inRecord->getInterest().getTag<lp::PitToken>();
235  }
236 
237  // delete the PIT entry's in-record based on egress,
238  // since Data is sent to face and endpoint from which the Interest was received
239  pitEntry->deleteInRecord(egress.face);
240 
241  if (pitToken != nullptr) {
242  Data data2 = data; // make a copy so each downstream can get a different PIT token
243  data2.setTag(pitToken);
244  m_forwarder.onOutgoingData(data2, egress);
245  return;
246  }
247  m_forwarder.onOutgoingData(data, egress);
248 }
249 
250 void
251 Strategy::sendDataToAll(const shared_ptr<pit::Entry>& pitEntry,
252  const FaceEndpoint& ingress, const Data& data)
253 {
254  std::set<Face*> pendingDownstreams;
255  auto now = time::steady_clock::now();
256 
257  // remember pending downstreams
258  for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
259  if (inRecord.getExpiry() > now) {
260  if (inRecord.getFace().getId() == ingress.face.getId() &&
261  inRecord.getFace().getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) {
262  continue;
263  }
264  pendingDownstreams.emplace(&inRecord.getFace());
265  }
266  }
267 
268  for (const auto& pendingDownstream : pendingDownstreams) {
269  this->sendData(pitEntry, data, FaceEndpoint(*pendingDownstream, 0));
270  }
271 }
272 
273 void
274 Strategy::sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
275  std::initializer_list<FaceEndpoint> exceptFaceEndpoints)
276 {
277  // populate downstreams with all downstreams faces
278  std::set<Face*> downstreams;
279  std::transform(pitEntry->in_begin(), pitEntry->in_end(), std::inserter(downstreams, downstreams.end()),
280  [] (const pit::InRecord& inR) {
281  return &inR.getFace();
282  });
283 
284  // delete excluded faces
285  for (const auto& exceptFaceEndpoint : exceptFaceEndpoints) {
286  downstreams.erase(&exceptFaceEndpoint.face);
287  }
288 
289  // send Nacks
290  for (const auto& downstream : downstreams) {
291  this->sendNack(pitEntry, FaceEndpoint(*downstream, 0), header);
292  }
293  // warning: don't loop on pitEntry->getInRecords(), because in-record is deleted when sending Nack
294 }
295 
296 const fib::Entry&
297 Strategy::lookupFib(const pit::Entry& pitEntry) const
298 {
299  const Fib& fib = m_forwarder.getFib();
300 
301  const Interest& interest = pitEntry.getInterest();
302  // has forwarding hint?
303  if (interest.getForwardingHint().empty()) {
304  // FIB lookup with Interest name
305  const fib::Entry& fibEntry = fib.findLongestPrefixMatch(pitEntry);
306  NFD_LOG_TRACE("lookupFib noForwardingHint found=" << fibEntry.getPrefix());
307  return fibEntry;
308  }
309 
310  const DelegationList& fh = interest.getForwardingHint();
311  // Forwarding hint should have been stripped by incoming Interest pipeline when reaching producer region
312  BOOST_ASSERT(!m_forwarder.getNetworkRegionTable().isInProducerRegion(fh));
313 
314  const fib::Entry* fibEntry = nullptr;
315  for (const Delegation& del : fh) {
316  fibEntry = &fib.findLongestPrefixMatch(del.name);
317  if (fibEntry->hasNextHops()) {
318  if (fibEntry->getPrefix().size() == 0) {
319  // in consumer region, return the default route
320  NFD_LOG_TRACE("lookupFib inConsumerRegion found=" << fibEntry->getPrefix());
321  }
322  else {
323  // in default-free zone, use the first delegation that finds a FIB entry
324  NFD_LOG_TRACE("lookupFib delegation=" << del.name << " found=" << fibEntry->getPrefix());
325  }
326  return *fibEntry;
327  }
328  BOOST_ASSERT(fibEntry->getPrefix().size() == 0); // only ndn:/ FIB entry can have zero nexthop
329  }
330  BOOST_ASSERT(fibEntry != nullptr && fibEntry->getPrefix().size() == 0);
331  return *fibEntry; // only occurs if no delegation finds a FIB nexthop
332 }
333 
334 } // namespace fw
335 } // namespace nfd
NFD_LOG_TRACE
#define NFD_LOG_TRACE
Definition: logger.hpp:37
nfd::fw::Strategy::sendNacks
void sendNacks(const shared_ptr< pit::Entry > &pitEntry, const lp::NackHeader &header, std::initializer_list< FaceEndpoint > exceptFaceEndpoints={})
send Nack to every face-endpoint pair that has an in-record, except those in exceptFaceEndpoints
Definition: strategy.cpp:274
nfd::fib::Fib::findLongestPrefixMatch
const Entry & findLongestPrefixMatch(const Name &prefix) const
Performs a longest prefix match.
Definition: fib.cpp:62
ndn::DelegationList::empty
NDN_CXX_NODISCARD bool empty() const noexcept
Definition: delegation-list.hpp:102
nfd::FaceEndpoint::face
Face & face
Definition: face-endpoint.hpp:46
ndn::Name::rbegin
const_reverse_iterator rbegin() const
Reverse begin iterator.
Definition: name.hpp:239
ndn::lp::NackHeader
represents a Network NACK header
Definition: nack-header.hpp:58
nfd::fw::Strategy::sendInterest
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &egress, const Interest &interest)
send Interest to egress
Definition: strategy.cpp:206
nfd::fw::Strategy::Strategy
Strategy(Forwarder &forwarder)
Construct a strategy instance.
Definition: strategy.cpp:143
nfd::fw::Strategy::afterReceiveLoopedInterest
virtual void afterReceiveLoopedInterest(const FaceEndpoint &ingress, const Interest &interest, pit::Entry &pitEntry)
trigger after a looped Interest is received
Definition: strategy.cpp:155
ndn::PartialName
Name PartialName
Represents an arbitrary sequence of name components.
Definition: name.hpp:39
ndn::Name::size
size_t size() const
Returns the number of components.
Definition: name.hpp:153
ndn::time::steady_clock::now
static time_point now() noexcept
Definition: time.cpp:80
nfd::fw::Strategy::~Strategy
virtual ~Strategy()
ndn::nfd::LINK_TYPE_AD_HOC
@ LINK_TYPE_AD_HOC
link is ad hoc
Definition: nfd-constants.hpp:61
nfd::fw::Strategy::canCreate
static bool canCreate(const Name &instanceName)
Definition: strategy.cpp:86
ndn::TagHost::setTag
void setTag(shared_ptr< T > tag) const
set a tag item
Definition: tag-host.hpp:79
nfd::fib::NextHop::getFace
Face & getFace() const
Definition: fib-nexthop.hpp:47
ndn::TagHost::getTag
shared_ptr< T > getTag() const
get a tag item
Definition: tag-host.hpp:66
ndn::Data::getName
const Name & getName() const
Get name.
Definition: data.hpp:124
nfd::fw::Strategy::create
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:92
forwarder.hpp
nfd::fw::Strategy::areSameType
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:108
pit-token.hpp
ndn::Delegation
Represents a Delegation.
Definition: delegation.hpp:33
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
ndn::Name::getSubName
PartialName getSubName(ssize_t iStartComponent, size_t nComponents=npos) const
Extracts some components as a sub-name (PartialName).
Definition: name.cpp:185
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
ndn::Name::rend
const_reverse_iterator rend() const
Reverse end iterator.
Definition: name.hpp:247
ndn::name::Component::isVersion
bool isVersion() const
Check if the component is a version per NDN naming conventions.
Definition: name-component.cpp:218
nfd::fw::Strategy::listRegistered
static std::set< Name > listRegistered()
Definition: strategy.cpp:114
ndn::TagHost::removeTag
void removeTag() const
remove tag item
Definition: tag-host.hpp:93
nfd::Forwarder::getNetworkRegionTable
NetworkRegionTable & getNetworkRegionTable()
Definition: forwarder.hpp:163
ndn::Name::getPrefix
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:211
nfd::fw::Strategy::sendNack
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &egress, const lp::NackHeader &header)
send Nack to egress
Definition: strategy.hpp:315
nonstd::optional_lite::nullopt
const nullopt_t nullopt((nullopt_t::init()))
nfd::fw::Strategy::afterReceiveData
virtual void afterReceiveData(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger after Data is received
Definition: strategy.cpp:181
nfd::pit::Entry
An Interest table entry.
Definition: pit-entry.hpp:59
ndn::Interest::getForwardingHint
const DelegationList & getForwardingHint() const noexcept
Definition: interest.hpp:192
nfd::FaceEndpoint
Represents a face-endpoint pair in the forwarder.
Definition: face-endpoint.hpp:37
nfd::pit::InRecord
Contains information about an Interest from an incoming face.
Definition: pit-in-record.hpp:37
nfd::fw::Strategy::lookupFib
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
Definition: strategy.cpp:297
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
nfd::fw::Strategy::parseInstanceName
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
Definition: strategy.cpp:123
nfd::fib::NextHop
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:38
nfd::face::Face::getId
FaceId getId() const
Definition: face.hpp:214
nfd::pit::Entry::getName
const Name & getName() const
Definition: pit-entry.hpp:78
ndn::DelegationList
represents a list of Delegations
Definition: delegation-list.hpp:38
nfd::fib::Entry
represents a FIB entry
Definition: fib-entry.hpp:54
nfd::fw::Strategy::onDroppedInterest
virtual void onDroppedInterest(const FaceEndpoint &egress, const Interest &interest)
trigger after Interest dropped for exceeding allowed retransmissions
Definition: strategy.cpp:200
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
nfd::fw::Strategy
represents a forwarding strategy
Definition: strategy.hpp:38
NFD_LOG_DEBUG
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
ndn::Name::at
const Component & at(ssize_t i) const
Returns an immutable reference to the component at the specified index, with bounds checking.
Definition: name.cpp:171
nfd::fw::Strategy::afterReceiveNack
virtual void afterReceiveNack(const FaceEndpoint &ingress, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
trigger after Nack is received
Definition: strategy.cpp:193
ndn::lp::PitToken
represent a PIT token field
Definition: pit-token.hpp:35
nfd::Forwarder
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:52
nfd::fw::Strategy::sendDataToAll
void sendDataToAll(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
send data to all matched and qualified face-endpoint pairs
Definition: strategy.cpp:251
nfd::Forwarder::getFib
Fib & getFib()
Definition: forwarder.hpp:127
nfd::fw::Strategy::afterContentStoreHit
virtual void afterContentStoreHit(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger after a Data is matched in CS
Definition: strategy.cpp:171
ndn::name::Component
Represents a name component.
Definition: name-component.hpp:94
ndn::lp::Nack
represents a Network Nack
Definition: nack.hpp:39
strategy.hpp
nfd::fw::Strategy::sendData
void sendData(const shared_ptr< pit::Entry > &pitEntry, const Data &data, const FaceEndpoint &egress)
send data to egress
Definition: strategy.cpp:226
nfd::fw::Strategy::makeInstanceName
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
Definition: strategy.cpp:134
nfd::fib::Fib
Represents the Forwarding Information Base (FIB)
Definition: fib.hpp:48
nfd::fw::Strategy::afterNewNextHop
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
trigger after new nexthop is added
Definition: strategy.cpp:219
nfd::fw::Strategy::beforeSatisfyInterest
virtual void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
trigger before PIT entry is satisfied
Definition: strategy.cpp:163
ndn::Interest::getName
const Name & getName() const noexcept
Definition: interest.hpp:121
nfd::pit::Entry::getInterest
const Interest & getInterest() const
Definition: pit-entry.hpp:70
nfd::fw::Strategy::ParsedInstanceName
Definition: strategy.hpp:365
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
logger.hpp
nfd::fib::Entry::getPrefix
const Name & getPrefix() const
Definition: fib-entry.hpp:60
nfd::fib::Entry::hasNextHops
bool hasNextHops() const
Definition: fib-entry.hpp:74
nfd::NetworkRegionTable::isInProducerRegion
bool isInProducerRegion(const DelegationList &forwardingHint) const
determines whether an Interest has reached a producer region
Definition: network-region-table.cpp:31