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-2020, 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 
37 class Strategy : noncopyable
38 {
39 public: // registry
46  template<typename S>
47  static void
48  registerType(const Name& strategyName = S::getStrategyName())
49  {
50  BOOST_ASSERT(strategyName.size() > 1);
51  BOOST_ASSERT(strategyName.at(-1).isVersion());
52  Registry& registry = getRegistry();
53  BOOST_ASSERT(registry.count(strategyName) == 0);
54  registry[strategyName] = [] (auto&&... args) {
55  return make_unique<S>(std::forward<decltype(args)>(args)...);
56  };
57  }
58 
64  static bool
65  canCreate(const Name& instanceName);
66 
72  static unique_ptr<Strategy>
73  create(const Name& instanceName, Forwarder& forwarder);
74 
77  static bool
78  areSameType(const Name& instanceNameA, const Name& instanceNameB);
79 
82  static std::set<Name>
84 
85 public: // constructor, destructor, strategy info
90  explicit
91  Strategy(Forwarder& forwarder);
92 
93  virtual
94  ~Strategy();
95 
96 #ifdef DOXYGEN
97 
102  static const Name&
103  getStrategyName();
104 #endif
105 
111  const Name&
113  {
114  return m_name;
115  }
116 
119  bool
121  {
122  return m_wantNewNextHopTrigger;
123  }
124 
125 public: // triggers
151  virtual void
152  afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
153  const shared_ptr<pit::Entry>& pitEntry) = 0;
154 
174  virtual void
175  beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry,
176  const FaceEndpoint& ingress, const Data& data);
177 
182  virtual void
183  afterContentStoreHit(const shared_ptr<pit::Entry>& pitEntry,
184  const FaceEndpoint& ingress, const Data& data);
185 
209  virtual void
210  afterReceiveData(const shared_ptr<pit::Entry>& pitEntry,
211  const FaceEndpoint& ingress, const Data& data);
212 
234  virtual void
235  afterReceiveNack(const FaceEndpoint& ingress, const lp::Nack& nack,
236  const shared_ptr<pit::Entry>& pitEntry);
237 
242  virtual void
243  onDroppedInterest(const Face& egress, const Interest& interest);
244 
250  virtual void
251  afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry);
252 
253 protected: // actions
261  sendInterest(const shared_ptr<pit::Entry>& pitEntry, Face& egress,
262  const Interest& interest);
263 
270  VIRTUAL_WITH_TESTS bool
271  sendData(const shared_ptr<pit::Entry>& pitEntry, const Data& data, Face& egress);
272 
281  VIRTUAL_WITH_TESTS void
282  sendDataToAll(const shared_ptr<pit::Entry>& pitEntry, const Face& inFace, const Data& data);
283 
290  VIRTUAL_WITH_TESTS void
291  rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
292  {
293  this->setExpiryTimer(pitEntry, 0_ms);
294  }
295 
305  VIRTUAL_WITH_TESTS bool
306  sendNack(const shared_ptr<pit::Entry>& pitEntry, Face& egress,
307  const lp::NackHeader& header)
308  {
309  return m_forwarder.onOutgoingNack(pitEntry, egress, header);
310  }
311 
318  void
319  sendNacks(const shared_ptr<pit::Entry>& pitEntry, const lp::NackHeader& header,
320  std::initializer_list<const Face*> exceptFaces = {});
321 
324  void
325  setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
326  {
327  m_forwarder.setExpiryTimer(pitEntry, duration);
328  }
329 
330 protected: // accessors
333  const fib::Entry&
334  lookupFib(const pit::Entry& pitEntry) const;
335 
338  {
339  return m_measurements;
340  }
341 
342  Face*
343  getFace(FaceId id) const
344  {
345  return getFaceTable().get(id);
346  }
347 
348  const FaceTable&
349  getFaceTable() const
350  {
351  return m_forwarder.m_faceTable;
352  }
353 
354 protected: // instance name
356  {
358  optional<uint64_t> version;
359  PartialName parameters;
360  };
361 
366  static ParsedInstanceName
367  parseInstanceName(const Name& input);
368 
379  static Name
380  makeInstanceName(const Name& input, const Name& strategyName);
381 
385  void
386  setInstanceName(const Name& name)
387  {
388  m_name = name;
389  }
390 
394  void
396  {
397  m_wantNewNextHopTrigger = enabled;
398  }
399 
400 private: // registry
401  typedef std::function<unique_ptr<Strategy>(Forwarder& forwarder, const Name& strategyName)> CreateFunc;
402  typedef std::map<Name, CreateFunc> Registry; // indexed by strategy name
403 
404  static Registry&
405  getRegistry();
406 
407  static Registry::const_iterator
408  find(const Name& instanceName);
409 
410 protected: // accessors
411  signal::Signal<FaceTable, Face>& afterAddFace;
412  signal::Signal<FaceTable, Face>& beforeRemoveFace;
413 
414 private: // instance fields
415  Name m_name;
416 
421  Forwarder& m_forwarder;
422 
423  MeasurementsAccessor m_measurements;
424 
425  bool m_wantNewNextHopTrigger = false;
426 };
427 
428 } // namespace fw
429 } // namespace nfd
430 
435 #define NFD_REGISTER_STRATEGY(S) \
436 static class NfdAuto ## S ## StrategyRegistrationClass \
437 { \
438 public: \
439  NfdAuto ## S ## StrategyRegistrationClass() \
440  { \
441  ::nfd::fw::Strategy::registerType<S>(); \
442  } \
443 } g_nfdAuto ## S ## StrategyRegistrationVariable
444 
445 #endif // NFD_DAEMON_FW_STRATEGY_HPP
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
Trigger after new nexthop is added.
Definition: strategy.cpp:208
Main class of NFD&#39;s forwarding engine.
Definition: forwarder.hpp:51
void setInstanceName(const Name &name)
Set strategy instance name.
Definition: strategy.hpp:386
Strategy(Forwarder &forwarder)
Construct a strategy instance.
Definition: strategy.cpp:143
bool sendNack(const shared_ptr< pit::Entry > &pitEntry, Face &egress, const lp::NackHeader &header)
Send a Nack packet.
Definition: strategy.hpp:306
Contains information about an Interest toward an outgoing face.
virtual void afterReceiveInterest(const FaceEndpoint &ingress, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry)=0
Trigger after Interest is received.
bool sendData(const shared_ptr< pit::Entry > &pitEntry, const Data &data, Face &egress)
Send a Data packet.
Definition: strategy.cpp:215
represents a FIB entry
Definition: fib-entry.hpp:53
signal::Signal< FaceTable, Face > & beforeRemoveFace
Definition: strategy.hpp:412
#define PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:40
container of all faces
Definition: face-table.hpp:38
static Name makeInstanceName(const Name &input, const Name &strategyName)
Construct a strategy instance name.
Definition: strategy.cpp:134
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:45
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:282
MeasurementsAccessor & getMeasurements()
Definition: strategy.hpp:337
virtual void afterReceiveNack(const FaceEndpoint &ingress, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry)
Trigger after Nack is received.
Definition: strategy.cpp:184
void enableNewNextHopTrigger(bool enabled)
Set whether the afterNewNextHop trigger should be invoked for this strategy.
Definition: strategy.hpp:395
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Definition: strategy.cpp:108
static void registerType(const Name &strategyName=S::getStrategyName())
Register a strategy type.
Definition: strategy.hpp:48
An Interest table entry.
Definition: pit-entry.hpp:58
Represents a face-endpoint pair in the forwarder.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
virtual void onDroppedInterest(const Face &egress, const Interest &interest)
Trigger after Interest dropped (e.g., for exceeding allowed retransmissions)
Definition: strategy.cpp:191
static const Name & getStrategyName()
void sendDataToAll(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
Send a Data packet to all matched and qualified faces.
Definition: strategy.cpp:238
allows Strategy to access portion of Measurements table under its namespace
generalization of a network interface
Definition: face.hpp:54
void setExpiryTimer(const shared_ptr< pit::Entry > &pitEntry, time::milliseconds duration)
Schedule the PIT entry to be erased after duration.
Definition: strategy.hpp:325
void sendNacks(const shared_ptr< pit::Entry > &pitEntry, const lp::NackHeader &header, std::initializer_list< const Face *> exceptFaces={})
Send Nack to every face that has an in-record, except those in exceptFaces.
Definition: strategy.cpp:261
PartialName parameters
parameter components
Definition: strategy.hpp:359
static unique_ptr< Strategy > create(const Name &instanceName, Forwarder &forwarder)
Definition: strategy.cpp:92
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:162
const Name & getInstanceName() const
Definition: strategy.hpp:112
Represents a forwarding strategy.
Definition: strategy.hpp:37
#define VIRTUAL_WITH_TESTS
Definition: common.hpp:39
virtual void afterReceiveData(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
Trigger after Data is received.
Definition: strategy.cpp:172
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
const FaceTable & getFaceTable() const
Definition: strategy.hpp:349
static std::set< Name > listRegistered()
Definition: strategy.cpp:114
signal::Signal< FaceTable, Face > & afterAddFace
Definition: strategy.hpp:411
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:37
virtual ~Strategy()
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44
Face * getFace(FaceId id) const
Definition: strategy.hpp:343
virtual void beforeSatisfyInterest(const shared_ptr< pit::Entry > &pitEntry, const FaceEndpoint &ingress, const Data &data)
Trigger before PIT entry is satisfied.
Definition: strategy.cpp:154
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
Schedule the PIT entry for immediate deletion.
Definition: strategy.hpp:291
Name strategyName
strategy name without parameters
Definition: strategy.hpp:357
pit::OutRecord * sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &egress, const Interest &interest)
Send an Interest packet.
Definition: strategy.cpp:197
bool wantNewNextHopTrigger() const
Definition: strategy.hpp:120
optional< uint64_t > version
whether strategyName contains a version component
Definition: strategy.hpp:358