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-2024, 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 #include <functional>
35 #include <map>
36 #include <set>
37 
38 namespace nfd::fw {
39 
40 class StrategyParameters;
41 
45 class Strategy : noncopyable
46 {
47 public: // registry
55  template<typename S>
56  static void
57  registerType(const Name& strategyName = S::getStrategyName())
58  {
59  BOOST_ASSERT(strategyName.size() > 1);
60  BOOST_ASSERT(strategyName.at(-1).isVersion());
61  auto r = getRegistry().insert_or_assign(strategyName, [] (auto&&... args) {
62  return make_unique<S>(std::forward<decltype(args)>(args)...);
63  });
64  BOOST_VERIFY(r.second);
65  }
66 
73  static bool
74  canCreate(const Name& instanceName);
75 
82  static unique_ptr<Strategy>
83  create(const Name& instanceName, Forwarder& forwarder);
84 
88  static bool
89  areSameType(const Name& instanceNameA, const Name& instanceNameB);
90 
94  [[nodiscard]] static std::set<Name>
96 
97 public: // constructor, destructor, strategy info
102  explicit
103  Strategy(Forwarder& forwarder);
104 
105  virtual
107 
108 #ifdef DOXYGEN
115  static const Name&
117 #endif
118 
125  const Name&
126  getInstanceName() const noexcept
127  {
128  return m_name;
129  }
130 
131 public: // triggers
159  virtual void
160  afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
161  const shared_ptr<pit::Entry>& pitEntry) = 0;
162 
174  virtual void
175  onInterestLoop(const Interest& interest, const FaceEndpoint& ingress);
176 
186  virtual void
187  afterContentStoreHit(const Data& data, const FaceEndpoint& ingress,
188  const shared_ptr<pit::Entry>& pitEntry);
189 
212  virtual void
213  beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
214  const shared_ptr<pit::Entry>& pitEntry);
215 
244  virtual void
245  afterReceiveData(const Data& data, const FaceEndpoint& ingress,
246  const shared_ptr<pit::Entry>& pitEntry);
247 
272  virtual void
273  afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
274  const shared_ptr<pit::Entry>& pitEntry);
275 
281  virtual void
282  onDroppedInterest(const Interest& interest, Face& egress);
283 
291  virtual void
292  afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry);
293 
294 protected: // actions
303  sendInterest(const Interest& interest, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
304 
313  sendData(const Data& data, Face& egress, const shared_ptr<pit::Entry>& pitEntry);
314 
325  sendDataToAll(const Data& data, const shared_ptr<pit::Entry>& pitEntry, const Face& inFace);
326 
335  rejectPendingInterest(const shared_ptr<pit::Entry>& pitEntry)
336  {
337  this->setExpiryTimer(pitEntry, 0_ms);
338  }
339 
351  sendNack(const lp::NackHeader& header, Face& egress, const shared_ptr<pit::Entry>& pitEntry)
352  {
353  return m_forwarder.onOutgoingNack(header, egress, pitEntry);
354  }
355 
364  sendNack(const lp::Nack& nack, Face& egress)
365  {
366  egress.sendNack(nack);
367  ++m_forwarder.m_counters.nOutNacks;
368  return true;
369  }
370 
378  void
379  sendNacks(const lp::NackHeader& header, const shared_ptr<pit::Entry>& pitEntry,
380  std::initializer_list<const Face*> exceptFaces = {});
381 
385  void
386  setExpiryTimer(const shared_ptr<pit::Entry>& pitEntry, time::milliseconds duration)
387  {
388  m_forwarder.setExpiryTimer(pitEntry, duration);
389  }
390 
391 protected: // accessors
395  const fib::Entry&
396  lookupFib(const pit::Entry& pitEntry) const;
397 
399  getMeasurements() noexcept
400  {
401  return m_measurements;
402  }
403 
404  Face*
405  getFace(FaceId id) const noexcept
406  {
407  return getFaceTable().get(id);
408  }
409 
410  const FaceTable&
411  getFaceTable() const noexcept
412  {
413  return m_forwarder.m_faceTable;
414  }
415 
416 protected: // instance name
418  {
420  std::optional<uint64_t> version;
421  PartialName parameters;
422  };
423 
428  static ParsedInstanceName
429  parseInstanceName(const Name& input);
430 
441  static Name
442  makeInstanceName(const Name& input, const Name& strategyName);
443 
447  void
448  setInstanceName(const Name& name) noexcept
449  {
450  m_name = name;
451  }
452 
459  static StrategyParameters
460  parseParameters(const PartialName& params);
461 
462 private: // registry
463  using CreateFunc = std::function<unique_ptr<Strategy>(Forwarder&, const Name& /*strategyName*/)>;
464  using Registry = std::map<Name, CreateFunc>; // indexed by strategy name
465 
466  static Registry&
467  getRegistry();
468 
469  static Registry::const_iterator
470  find(const Name& instanceName);
471 
472 protected: // accessors
473  signal::Signal<FaceTable, Face>& afterAddFace;
474  signal::Signal<FaceTable, Face>& beforeRemoveFace;
475 
476 private: // instance fields
477  Name m_name;
478  Forwarder& m_forwarder;
479  MeasurementsAccessor m_measurements;
480 };
481 
482 class StrategyParameters : public std::map<std::string, std::string>
483 {
484 public:
485  // Note: only arithmetic types are supported by getOrDefault() for now
486 
487  template<typename T>
488  std::enable_if_t<std::is_signed_v<T>, T>
489  getOrDefault(const key_type& key, const T& defaultVal) const
490  {
491  auto it = find(key);
492  if (it == end()) {
493  return defaultVal;
494  }
495 
496  T val{};
497  if (!boost::conversion::try_lexical_convert(it->second, val)) {
498  NDN_THROW(std::invalid_argument(key + " value is malformed"));
499  }
500  return val;
501  }
502 
503  template<typename T>
504  std::enable_if_t<std::is_unsigned_v<T>, T>
505  getOrDefault(const key_type& key, const T& defaultVal) const
506  {
507  auto it = find(key);
508  if (it == end()) {
509  return defaultVal;
510  }
511 
512  if (it->second.find('-') != std::string::npos) {
513  NDN_THROW(std::invalid_argument(key + " cannot be negative"));
514  }
515 
516  T val{};
517  if (!boost::conversion::try_lexical_convert(it->second, val)) {
518  NDN_THROW(std::invalid_argument(key + " value is malformed"));
519  }
520  return val;
521  }
522 };
523 
524 } // namespace nfd::fw
525 
531 #define NFD_REGISTER_STRATEGY(S) \
532 static class NfdAuto ## S ## StrategyRegistrationClass \
533 { \
534 public: \
535  NfdAuto ## S ## StrategyRegistrationClass() \
536  { \
537  ::nfd::fw::Strategy::registerType<S>(); \
538  } \
539 } g_nfdAuto ## S ## StrategyRegistrationVariable
540 
542 #define NFD_LOG_INTEREST_FROM(interest, ingress, msg) \
543  NFD_LOG_DEBUG("interest=" << (interest).getName() << \
544  " nonce=" << (interest).getNonce() << \
545  " from=" << (ingress) << \
546  ' ' << msg)
547 
549 #define NFD_LOG_DATA_FROM(data, ingress, msg) \
550  NFD_LOG_DEBUG("data=" << (data).getName() << \
551  " from=" << (ingress) << \
552  ' ' << msg)
553 
555 #define NFD_LOG_NACK_FROM(nack, ingress, msg) \
556  NFD_LOG_DEBUG("nack=" << (nack).getInterest().getName() << \
557  " nonce=" << (nack).getInterest().getNonce() << \
558  " reason=" << (nack).getReason() << \
559  " from=" << (ingress) << \
560  ' ' << msg)
561 
562 #endif // NFD_DAEMON_FW_STRATEGY_HPP
Represents a face-endpoint pair in the forwarder.
Container of all faces.
Definition: face-table.hpp:42
Face * get(FaceId id) const noexcept
Get face by FaceId.
Definition: face-table.cpp:38
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:54
Generalization of a network interface.
Definition: face.hpp:118
void sendNack(const lp::Nack &nack)
Send Nack.
Definition: face.hpp:173
Represents an entry in the FIB.
Definition: fib-entry.hpp:91
Represents a nexthop record in a FIB entry.
Definition: fib-entry.hpp:50
Base class of all forwarding strategies.
Definition: strategy.hpp:46
virtual void onDroppedInterest(const Interest &interest, Face &egress)
Trigger after an Interest is dropped (e.g., for exceeding allowed retransmissions).
Definition: strategy.cpp:223
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:187
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:216
static void registerType(const Name &strategyName=S::getStrategyName())
Register a strategy type.
Definition: strategy.hpp:57
const Name & getInstanceName() const noexcept
Returns the strategy's instance name.
Definition: strategy.hpp:126
Face * getFace(FaceId id) const noexcept
Definition: strategy.hpp:405
static const Name & getStrategyName()
Returns the strategy's program name.
bool sendNack(const lp::Nack &nack, Face &egress)
Send a Nack packet without going through the outgoing Nack pipeline.
Definition: strategy.hpp:364
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
Schedule the PIT entry for immediate deletion.
Definition: strategy.hpp:335
const FaceTable & getFaceTable() const noexcept
Definition: strategy.hpp:411
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:291
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:313
bool sendNack(const lp::NackHeader &header, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Nack packet.
Definition: strategy.hpp:351
signal::Signal< FaceTable, Face > & beforeRemoveFace
Definition: strategy.hpp:474
virtual void afterNewNextHop(const fib::NextHop &nextHop, const shared_ptr< pit::Entry > &pitEntry)
Trigger after a new nexthop is added.
Definition: strategy.cpp:229
pit::OutRecord * sendInterest(const Interest &interest, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send an Interest packet.
Definition: strategy.cpp:236
bool sendData(const Data &data, Face &egress, const shared_ptr< pit::Entry > &pitEntry)
Send a Data packet.
Definition: strategy.cpp:247
static ParsedInstanceName parseInstanceName(const Name &input)
Parse a strategy instance name.
Definition: strategy.cpp:123
MeasurementsAccessor & getMeasurements() noexcept
Definition: strategy.hpp:399
void setExpiryTimer(const shared_ptr< pit::Entry > &pitEntry, time::milliseconds duration)
Schedule the PIT entry to be erased after duration.
Definition: strategy.hpp:386
void setInstanceName(const Name &name) noexcept
Set strategy instance name.
Definition: strategy.hpp:448
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:269
virtual void onInterestLoop(const Interest &interest, const FaceEndpoint &ingress)
Trigger after an Interest loop is detected.
Definition: strategy.cpp:177
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:205
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:197
signal::Signal< FaceTable, Face > & afterAddFace
Definition: strategy.hpp:473
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:489
std::enable_if_t< std::is_unsigned_v< T >, T > getOrDefault(const key_type &key, const T &defaultVal) const
Definition: strategy.hpp:505
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:197
Contains information about an Interest toward an outgoing face.
Definition: pit-entry.hpp:129
#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:48
std::optional< uint64_t > version
The strategy version number, if present.
Definition: strategy.hpp:420
Name strategyName
Strategy name without parameters.
Definition: strategy.hpp:419
PartialName parameters
Parameter components, may be empty.
Definition: strategy.hpp:421