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-2022, 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 #include <boost/lexical_cast/try_lexical_convert.hpp>
33 
34 namespace nfd::fw {
35 
36 class StrategyParameters;
37 
41 class Strategy : noncopyable
42 {
43 public: // registry
51  template<typename S>
52  static void
53  registerType(const Name& strategyName = S::getStrategyName())
54  {
55  BOOST_ASSERT(strategyName.size() > 1);
56  BOOST_ASSERT(strategyName.at(-1).isVersion());
57  auto r = getRegistry().insert_or_assign(strategyName, [] (auto&&... args) {
58  return make_unique<S>(std::forward<decltype(args)>(args)...);
59  });
60  BOOST_VERIFY(r.second);
61  }
62 
69  static bool
70  canCreate(const Name& instanceName);
71 
78  static unique_ptr<Strategy>
79  create(const Name& instanceName, Forwarder& forwarder);
80 
84  static bool
85  areSameType(const Name& instanceNameA, const Name& instanceNameB);
86 
90  [[nodiscard]] static std::set<Name>
92 
93 public: // constructor, destructor, strategy info
98  explicit
99  Strategy(Forwarder& forwarder);
100 
101  virtual
103 
104 #ifdef DOXYGEN
111  static const Name&
113 #endif
114 
121  const Name&
122  getInstanceName() const noexcept
123  {
124  return m_name;
125  }
126 
127 public: // triggers
155  virtual void
156  afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
157  const shared_ptr<pit::Entry>& pitEntry) = 0;
158 
168  virtual void
169  afterContentStoreHit(const Data& data, const FaceEndpoint& ingress,
170  const shared_ptr<pit::Entry>& pitEntry);
171 
194  virtual void
195  beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
196  const shared_ptr<pit::Entry>& pitEntry);
197 
226  virtual void
227  afterReceiveData(const Data& data, const FaceEndpoint& ingress,
228  const shared_ptr<pit::Entry>& pitEntry);
229 
254  virtual void
255  afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
256  const shared_ptr<pit::Entry>& pitEntry);
257 
263  virtual void
264  onDroppedInterest(const Interest& interest, Face& egress);
265 
273  virtual void
274  afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry);
275 
276 protected: // actions
285  sendInterest(const Interest& interest, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
286 
295  sendData(const Data& data, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
296 
307  sendDataToAll(const Data& data, const shared_ptr<pit::Entry>& pitEntry, const Face& inFace);
308 
317  rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
318  {
319  this->setExpiryTimer(pitEntry, 0_ms);
320  }
321 
333  sendNack(const lp::NackHeader& header, Face& egress, const shared_ptr<pit::Entry>& pitEntry)
334  {
335  return m_forwarder.onOutgoingNack(header, egress, pitEntry);
336  }
337 
345  void
346  sendNacks(const lp::NackHeader& header, const shared_ptr<pit::Entry>& pitEntry,
347  std::initializer_list<const Face*> exceptFaces = {});
348 
352  void
353  setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
354  {
355  m_forwarder.setExpiryTimer(pitEntry, duration);
356  }
357 
358 protected: // accessors
362  const fib::Entry&
363  lookupFib(const pit::Entry& pitEntry) const;
364 
366  getMeasurements() noexcept
367  {
368  return m_measurements;
369  }
370 
371  Face*
372  getFace(FaceId id) const noexcept
373  {
374  return getFaceTable().get(id);
375  }
376 
377  const FaceTable&
378  getFaceTable() const noexcept
379  {
380  return m_forwarder.m_faceTable;
381  }
382 
383 protected: // instance name
385  {
387  std::optional<uint64_t> version;
388  PartialName parameters;
389  };
390 
395  static ParsedInstanceName
396  parseInstanceName(const Name& input);
397 
408  static Name
409  makeInstanceName(const Name& input, const Name& strategyName);
410 
414  void
415  setInstanceName(const Name& name) noexcept
416  {
417  m_name = name;
418  }
419 
426  static StrategyParameters
427  parseParameters(const PartialName& params);
428 
429 private: // registry
430  using CreateFunc = std::function<unique_ptr<Strategy>(Forwarder&, const Name& /*strategyName*/)>;
431  using Registry = std::map<Name, CreateFunc>; // indexed by strategy name
432 
433  static Registry&
434  getRegistry();
435 
436  static Registry::const_iterator
437  find(const Name& instanceName);
438 
439 protected: // accessors
440  signal::Signal<FaceTable, Face>& afterAddFace;
441  signal::Signal<FaceTable, Face>& beforeRemoveFace;
442 
443 private: // instance fields
444  Name m_name;
445  Forwarder& m_forwarder;
446  MeasurementsAccessor m_measurements;
447 };
448 
449 class StrategyParameters : public std::map<std::string, std::string>
450 {
451 public:
452  // Note: only arithmetic types are supported by getOrDefault() for now
453 
454  template<typename T>
455  std::enable_if_t<std::is_signed_v<T>, T>
456  getOrDefault(const key_type& key, const T& defaultVal) const
457  {
458  auto it = find(key);
459  if (it == end()) {
460  return defaultVal;
461  }
462 
463  T val{};
464  if (!boost::conversion::try_lexical_convert(it->second, val)) {
465  NDN_THROW(std::invalid_argument(key + " value is malformed"));
466  }
467  return val;
468  }
469 
470  template<typename T>
471  std::enable_if_t<std::is_unsigned_v<T>, T>
472  getOrDefault(const key_type& key, const T& defaultVal) const
473  {
474  auto it = find(key);
475  if (it == end()) {
476  return defaultVal;
477  }
478 
479  if (it->second.find('-') != std::string::npos) {
480  NDN_THROW(std::invalid_argument(key + " cannot be negative"));
481  }
482 
483  T val{};
484  if (!boost::conversion::try_lexical_convert(it->second, val)) {
485  NDN_THROW(std::invalid_argument(key + " value is malformed"));
486  }
487  return val;
488  }
489 };
490 
491 } // namespace nfd::fw
492 
497 #define NFD_REGISTER_STRATEGY(S) \
498 static class NfdAuto ## S ## StrategyRegistrationClass \
499 { \
500 public: \
501  NfdAuto ## S ## StrategyRegistrationClass() \
502  { \
503  ::nfd::fw::Strategy::registerType<S>(); \
504  } \
505 } g_nfdAuto ## S ## StrategyRegistrationVariable
506 
507 #endif // NFD_DAEMON_FW_STRATEGY_HPP
Represents a face-endpoint pair in the forwarder.
Container of all faces.
Definition: face-table.hpp:40
Face * get(FaceId id) const noexcept
Get face by FaceId.
Definition: face-table.cpp:40
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:54
Generalization of a network interface.
Definition: face.hpp:56
Represents an entry in the FIB.
Definition: fib-entry.hpp:54
Represents a nexthop record in a FIB entry.
Definition: fib-nexthop.hpp:37
Base class of all forwarding strategies.
Definition: strategy.hpp:42
virtual void onDroppedInterest(const Interest &interest, Face &egress)
Trigger after an Interest is dropped (e.g., for exceeding allowed retransmissions).
Definition: strategy.cpp:213
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:177
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:206
static void registerType(const Name &strategyName=S::getStrategyName())
Register a strategy type.
Definition: strategy.hpp:53
const Name & getInstanceName() const noexcept
Returns the strategy's instance name.
Definition: strategy.hpp:122
Face * getFace(FaceId id) const noexcept
Definition: strategy.hpp:372
static const Name & getStrategyName()
Returns the strategy's program name.
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
Schedule the PIT entry for immediate deletion.
Definition: strategy.hpp:317
const FaceTable & getFaceTable() const noexcept
Definition: strategy.hpp:378
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:282
Strategy(Forwarder &forwarder)
Construct a strategy instance.
Definition: strategy.cpp:166
static bool canCreate(const Name &instanceName)
Returns whether a strategy instance can be created from 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:304
bool sendNack(const lp::NackHeader &header, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Nack packet.
Definition: strategy.hpp:333
signal::Signal< FaceTable, Face > & beforeRemoveFace
Definition: strategy.hpp:441
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
Trigger after a new nexthop is added.
Definition: strategy.cpp:219
pit::OutRecord * sendInterest(const Interest &interest, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send an Interest packet.
Definition: strategy.cpp:226
bool sendData(const Data &data, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Data packet.
Definition: strategy.cpp:237
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
MeasurementsAccessor & getMeasurements() noexcept
Definition: strategy.hpp:366
void setExpiryTimer(const shared_ptr< pit::Entry > &pitEntry, time::milliseconds duration)
Schedule the PIT entry to be erased after duration.
Definition: strategy.hpp:353
void setInstanceName(const Name &name) noexcept
Set strategy instance name.
Definition: strategy.hpp:415
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:260
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:195
static std::set< Name > listRegistered()
Returns all registered versioned strategy names.
Definition: strategy.cpp:114
static StrategyParameters parseParameters(const PartialName &params)
Parse strategy parameters encoded in a strategy instance name.
Definition: strategy.cpp:144
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:187
signal::Signal< FaceTable, Face > & afterAddFace
Definition: strategy.hpp:440
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)
Returns a strategy instance created from instanceName.
Definition: strategy.cpp:92
static bool areSameType(const Name &instanceNameA, const Name &instanceNameB)
Returns whether two names will instantiate the same strategy type.
Definition: strategy.cpp:108
std::enable_if_t< std::is_signed_v< T >, T > getOrDefault(const key_type &key, const T &defaultVal) const
Definition: strategy.hpp:456
std::enable_if_t< std::is_unsigned_v< T >, T > getOrDefault(const key_type &key, const T &defaultVal) const
Definition: strategy.hpp:472
Allows fw::Strategy to access the portion of Measurements table under its namespace.
Represents an entry in the Interest table (PIT).
Definition: pit-entry.hpp:62
Contains information about an Interest toward an outgoing face.
#define NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:40
#define NFD_VIRTUAL_WITH_TESTS
Definition: common.hpp:39
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:47
std::optional< uint64_t > version
The strategy version number, if present.
Definition: strategy.hpp:387
Name strategyName
Strategy name without parameters.
Definition: strategy.hpp:386
PartialName parameters
Parameter components, may be empty.
Definition: strategy.hpp:388