NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
strategy.hpp
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 #ifndef NFD_DAEMON_FW_STRATEGY_HPP
27 #define NFD_DAEMON_FW_STRATEGY_HPP
28 
29 #include "forwarder.hpp"
31 
32 namespace nfd {
33 namespace fw {
34 
38 class Strategy : noncopyable
39 {
40 public: // registry
47  template<typename S>
48  static void
49  registerType(const Name& strategyName = S::getStrategyName())
50  {
51  BOOST_ASSERT(strategyName.size() > 1);
52  BOOST_ASSERT(strategyName.at(-1).isVersion());
53  Registry& registry = getRegistry();
54  BOOST_ASSERT(registry.count(strategyName) == 0);
55  registry[strategyName] = [] (auto&&... args) {
56  return make_unique<S>(std::forward<decltype(args)>(args)...);
57  };
58  }
59 
65  static bool
66  canCreate(const Name& instanceName);
67 
73  static unique_ptr<Strategy>
74  create(const Name& instanceName, Forwarder& forwarder);
75 
78  static bool
79  areSameType(const Name& instanceNameA, const Name& instanceNameB);
80 
83  static std::set<Name>
85 
86 public: // constructor, destructor, strategy info
91  explicit
92  Strategy(Forwarder& forwarder);
93 
94  virtual
95  ~Strategy();
96 
97 #ifdef DOXYGEN
98 
103  static const Name&
104  getStrategyName();
105 #endif
106 
112  const Name&
114  {
115  return m_name;
116  }
117 
118 public: // triggers
146  virtual void
147  afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
148  const shared_ptr<pit::Entry>& pitEntry) = 0;
149 
150 
159  virtual void
160  afterReceiveLoopedInterest(const FaceEndpoint& ingress, const Interest& interest,
161  pit::Entry& pitEntry);
162 
182  virtual void
183  beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
184  const shared_ptr<pit::Entry>& pitEntry);
185 
186 
187  virtual void
188  satisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
189  const FaceEndpoint& ingress, const Data& data,
190  std::set<std::pair<Face*, EndpointId>>& satisfiedDownstreams,
191  std::set<std::pair<Face*, EndpointId>>& unsatisfiedDownstreams);
192 
197  virtual void
198  afterContentStoreHit(const Data& data, const FaceEndpoint& ingress,
199  const shared_ptr<pit::Entry>& pitEntry);
200 
228  virtual void
229  afterReceiveData(const Data& data, const FaceEndpoint& ingress,
230  const shared_ptr<pit::Entry>& pitEntry);
231 
256  virtual void
257  afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
258  const shared_ptr<pit::Entry>& pitEntry);
259 
265  virtual void
266  onDroppedInterest(const Interest& interest, Face& egress);
267 
275  virtual void
276  afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry);
277 
278 protected: // actions
287  sendInterest(const Interest& interest, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
288 
297  sendData(const Data& data, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
298 
309  sendDataToAll(const Data& data, const shared_ptr<pit::Entry>& pitEntry, const Face& inFace);
310 
319  rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
320  {
321  this->setExpiryTimer(pitEntry, 0_ms);
322  }
323 
335  sendNack(const lp::NackHeader& header, Face& egress, const shared_ptr<pit::Entry>& pitEntry)
336  {
337  return m_forwarder.onOutgoingNack(header, egress, pitEntry);
338  }
339 
347  void
348  sendNacks(const lp::NackHeader& header, const shared_ptr<pit::Entry>& pitEntry,
349  std::initializer_list<const Face*> exceptFaces = {});
350 
354  void
355  setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
356  {
357  m_forwarder.setExpiryTimer(pitEntry, duration);
358  }
359 
360 protected: // accessors
364  const fib::Entry&
365  lookupFib(const pit::Entry& pitEntry) const;
366 
369  {
370  return m_measurements;
371  }
372 
373  Face*
374  getFace(FaceId id) const
375  {
376  return getFaceTable().get(id);
377  }
378 
379  const FaceTable&
380  getFaceTable() const
381  {
382  return m_forwarder.m_faceTable;
383  }
384 
385 protected: // instance name
387  {
389  optional<uint64_t> version;
391  };
392 
397  static ParsedInstanceName
398  parseInstanceName(const Name& input);
399 
410  static Name
411  makeInstanceName(const Name& input, const Name& strategyName);
412 
416  void
418  {
419  m_name = name;
420  }
421 
422 private: // registry
423  using CreateFunc = std::function<unique_ptr<Strategy>(Forwarder&, const Name& /*strategyName*/)>;
424  using Registry = std::map<Name, CreateFunc>; // indexed by strategy name
425 
426  static Registry&
427  getRegistry();
428 
429  static Registry::const_iterator
430  find(const Name& instanceName);
431 
432 protected: // accessors
435 
436 private: // instance fields
437  Name m_name;
438  Forwarder& m_forwarder;
439  MeasurementsAccessor m_measurements;
440 };
441 
442 } // namespace fw
443 } // namespace nfd
444 
449 #define NFD_REGISTER_STRATEGY(S) \
450 static class NfdAuto ## S ## StrategyRegistrationClass \
451 { \
452 public: \
453  NfdAuto ## S ## StrategyRegistrationClass() \
454  { \
455  ::nfd::fw::Strategy::registerType<S>(); \
456  } \
457 } g_nfdAuto ## S ## StrategyRegistrationVariable
458 
459 #endif // NFD_DAEMON_FW_STRATEGY_HPP
#define NFD_VIRTUAL_WITH_TESTS
Definition: common.hpp:39
virtual void satisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data, std::set< std::pair< Face *, EndpointId >> &satisfiedDownstreams, std::set< std::pair< Face *, EndpointId >> &unsatisfiedDownstreams)
Definition: strategy.cpp:171
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
Trigger after a new nexthop is added.
Definition: strategy.cpp:230
signal::Signal< FaceTable, Face > & beforeRemoveFace
Definition: strategy.hpp:434
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
static std::set< Name > listRegistered()
Definition: strategy.cpp:114
void sendNacks(const lp::NackHeader &header, const shared_ptr< pit::Entry > &pitEntry, std::initializer_list< const Face *> exceptFaces={})
Send Nack to every face that has an in-record, except those in exceptFaces.
Definition: strategy.cpp:309
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
Strategy(Forwarder &forwarder)
Construct a strategy instance.
Definition: strategy.cpp:143
Face * getFace(FaceId id) const
Definition: strategy.hpp:374
Main class of NFD&#39;s forwarding engine.
Definition: forwarder.hpp:53
boost::chrono::duration< Rep, Period > duration
Definition: time.hpp:34
void setInstanceName(const Name &name)
Set strategy instance name.
Definition: strategy.hpp:417
Represents an Interest packet.
Definition: interest.hpp:48
NFD_VIRTUAL_WITH_TESTS bool sendData(const Data &data, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Data packet.
Definition: strategy.cpp:248
provides a lightweight signal / event system
Definition: signal.hpp:52
virtual void afterContentStoreHit(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
trigger after a Data is matched in CS
Definition: strategy.cpp:196
NFD_VIRTUAL_WITH_TESTS bool onOutgoingNack(const lp::NackHeader &nack, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
outgoing Nack pipeline
Definition: forwarder.cpp:503
NFD_VIRTUAL_WITH_TESTS void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
Schedule the PIT entry for immediate deletion.
Definition: strategy.hpp:319
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:45
represents a Network Nack
Definition: nack.hpp:38
virtual void afterReceiveLoopedInterest(const FaceEndpoint &ingress, const Interest &interest, pit::Entry &pitEntry)
trigger after a looped Interest is received
Definition: strategy.cpp:155
virtual ~Strategy()
ndn Face
Definition: face-impl.hpp:42
virtual void afterReceiveInterest(const Interest &interest, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)=0
Trigger after an Interest is received.
Name strategyName
strategy name without parameters
Definition: strategy.hpp:388
container of all faces
Definition: face-table.hpp:38
NFD_VIRTUAL_WITH_TESTS void sendDataToAll(const Data &data, const shared_ptr< pit::Entry > &pitEntry, const Face &inFace)
Send a Data packet to all matched and qualified faces.
Definition: strategy.cpp:279
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
Name PartialName
Represents an arbitrary sequence of name components.
Definition: name.hpp:36
virtual void afterReceiveNack(const lp::Nack &nack, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger after a Nack is received.
Definition: strategy.cpp:217
Represents an absolute name.
Definition: name.hpp:41
NFD_VIRTUAL_WITH_TESTS bool sendNack(const lp::NackHeader &header, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Nack packet.
Definition: strategy.hpp:335
Represents a forwarding strategy.
Definition: strategy.hpp:38
static const Name & getStrategyName()
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:389
allows Strategy to access portion of Measurements table under its namespace
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:92
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
Performs a FIB lookup, considering Link object if present.
Definition: strategy.cpp:331
signal::Signal< FaceTable, Face > & afterAddFace
Definition: strategy.hpp:433
const FaceTable & getFaceTable() const
Definition: strategy.hpp:380
const Name & getInstanceName() const
Definition: strategy.hpp:113
Contains information about an Interest toward an outgoing face.
static Name makeInstanceName(const Name &input, const Name &strategyName)
Construct a strategy instance name.
Definition: strategy.cpp:134
void setExpiryTimer(const shared_ptr< pit::Entry > &pitEntry, time::milliseconds duration)
Schedule the PIT entry to be erased after duration.
Definition: strategy.hpp:355
static bool canCreate(const Name &instanceName)
Definition: strategy.cpp:86
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
represents a Network NACK header
Definition: nack-header.hpp:57
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44
virtual void onDroppedInterest(const Interest &interest, Face &egress)
Trigger after an Interest is dropped (e.g., for exceeding allowed retransmissions).
Definition: strategy.cpp:224
virtual void beforeSatisfyInterest(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
trigger before PIT entry is satisfied
Definition: strategy.cpp:163
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:108
virtual void afterReceiveData(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
trigger after Data is received
Definition: strategy.cpp:206
static void registerType(const Name &strategyName=S::getStrategyName())
Register a strategy type.
Definition: strategy.hpp:49
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:37
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48