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
96 
97 #ifdef DOXYGEN
103  static const Name&
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 
159  virtual void
160  afterContentStoreHit(const Data& data, const FaceEndpoint& ingress,
161  const shared_ptr<pit::Entry>& pitEntry);
162 
185  virtual void
186  beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
187  const shared_ptr<pit::Entry>& pitEntry);
188 
217  virtual void
218  afterReceiveData(const Data& data, const FaceEndpoint& ingress,
219  const shared_ptr<pit::Entry>& pitEntry);
220 
245  virtual void
246  afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
247  const shared_ptr<pit::Entry>& pitEntry);
248 
254  virtual void
255  onDroppedInterest(const Interest& interest, Face& egress);
256 
264  virtual void
265  afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry);
266 
267 protected: // actions
276  sendInterest(const Interest& interest, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
277 
286  sendData(const Data& data, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
287 
298  sendDataToAll(const Data& data, const shared_ptr<pit::Entry>& pitEntry, const Face& inFace);
299 
308  rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
309  {
310  this->setExpiryTimer(pitEntry, 0_ms);
311  }
312 
324  sendNack(const lp::NackHeader& header, Face& egress, const shared_ptr<pit::Entry>& pitEntry)
325  {
326  return m_forwarder.onOutgoingNack(header, egress, pitEntry);
327  }
328 
336  void
337  sendNacks(const lp::NackHeader& header, const shared_ptr<pit::Entry>& pitEntry,
338  std::initializer_list<const Face*> exceptFaces = {});
339 
343  void
344  setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
345  {
346  m_forwarder.setExpiryTimer(pitEntry, duration);
347  }
348 
349 protected: // accessors
353  const fib::Entry&
354  lookupFib(const pit::Entry& pitEntry) const;
355 
358  {
359  return m_measurements;
360  }
361 
362  Face*
363  getFace(FaceId id) const
364  {
365  return getFaceTable().get(id);
366  }
367 
368  const FaceTable&
369  getFaceTable() const
370  {
371  return m_forwarder.m_faceTable;
372  }
373 
374 protected: // instance name
376  {
378  optional<uint64_t> version;
379  PartialName parameters;
380  };
381 
386  static ParsedInstanceName
387  parseInstanceName(const Name& input);
388 
399  static Name
400  makeInstanceName(const Name& input, const Name& strategyName);
401 
405  void
406  setInstanceName(const Name& name)
407  {
408  m_name = name;
409  }
410 
411 private: // registry
412  using CreateFunc = std::function<unique_ptr<Strategy>(Forwarder&, const Name& /*strategyName*/)>;
413  using Registry = std::map<Name, CreateFunc>; // indexed by strategy name
414 
415  static Registry&
416  getRegistry();
417 
418  static Registry::const_iterator
419  find(const Name& instanceName);
420 
421 protected: // accessors
422  signal::Signal<FaceTable, Face>& afterAddFace;
423  signal::Signal<FaceTable, Face>& beforeRemoveFace;
424 
425 private: // instance fields
426  Name m_name;
427  Forwarder& m_forwarder;
428  MeasurementsAccessor m_measurements;
429 };
430 
431 } // namespace fw
432 } // namespace nfd
433 
438 #define NFD_REGISTER_STRATEGY(S) \
439 static class NfdAuto ## S ## StrategyRegistrationClass \
440 { \
441 public: \
442  NfdAuto ## S ## StrategyRegistrationClass() \
443  { \
444  ::nfd::fw::Strategy::registerType<S>(); \
445  } \
446 } g_nfdAuto ## S ## StrategyRegistrationVariable
447 
448 #endif // NFD_DAEMON_FW_STRATEGY_HPP
Represents a face-endpoint pair in the forwarder.
container of all faces
Definition: face-table.hpp:39
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:45
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:54
generalization of a network interface
Definition: face.hpp:55
represents a FIB entry
Definition: fib-entry.hpp:54
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:38
Represents a forwarding strategy.
Definition: strategy.hpp:39
virtual void onDroppedInterest(const Interest &interest, Face &egress)
Trigger after an Interest is dropped (e.g., for exceeding allowed retransmissions).
Definition: strategy.cpp:190
const FaceTable & getFaceTable() const
Definition: strategy.hpp:369
virtual void afterContentStoreHit(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger after a matching Data is found in the Content Store.
Definition: strategy.cpp:154
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:183
static void registerType(const Name &strategyName=S::getStrategyName())
Register a strategy type.
Definition: strategy.hpp:49
void setInstanceName(const Name &name)
Set strategy instance name.
Definition: strategy.hpp:406
static const Name & getStrategyName()
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:357
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
Schedule the PIT entry for immediate deletion.
Definition: strategy.hpp:308
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:259
Strategy(Forwarder &forwarder)
Construct a strategy instance.
Definition: strategy.cpp:143
static bool canCreate(const Name &instanceName)
Definition: strategy.cpp:86
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
Performs a FIB lookup, considering Link object if present.
Definition: strategy.cpp:281
bool sendNack(const lp::NackHeader &header, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Nack packet.
Definition: strategy.hpp:324
signal::Signal< FaceTable, Face > & beforeRemoveFace
Definition: strategy.hpp:423
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
Trigger after a new nexthop is added.
Definition: strategy.cpp:196
pit::OutRecord * sendInterest(const Interest &interest, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send an Interest packet.
Definition: strategy.cpp:203
bool sendData(const Data &data, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Data packet.
Definition: strategy.cpp:214
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
void setExpiryTimer(const shared_ptr< pit::Entry > &pitEntry, time::milliseconds duration)
Schedule the PIT entry to be erased after duration.
Definition: strategy.hpp:344
const Name & getInstanceName() const
Definition: strategy.hpp:113
Face * getFace(FaceId id) const
Definition: strategy.hpp:363
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:237
virtual ~Strategy()
virtual void afterReceiveInterest(const Interest &interest, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)=0
Trigger after an Interest is received.
virtual void afterReceiveData(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger after Data is received.
Definition: strategy.cpp:172
static std::set< Name > listRegistered()
Definition: strategy.cpp:114
virtual void beforeSatisfyInterest(const Data &data, const FaceEndpoint &ingress, const shared_ptr< pit::Entry > &pitEntry)
Trigger before a PIT entry is satisfied.
Definition: strategy.cpp:164
signal::Signal< FaceTable, Face > & afterAddFace
Definition: strategy.hpp:422
static Name makeInstanceName(const Name &input, const Name &strategyName)
Construct a strategy instance name.
Definition: strategy.cpp:134
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:92
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:108
allows Strategy to access portion of Measurements table under its namespace
An Interest table entry.
Definition: pit-entry.hpp:59
Contains information about an Interest toward an outgoing face.
#define NFD_VIRTUAL_WITH_TESTS
Definition: common.hpp:39
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents,...
Definition: algorithm.hpp:32
Name strategyName
strategy name without parameters
Definition: strategy.hpp:377
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:378
PartialName parameters
parameter components
Definition: strategy.hpp:379