strategy-choice.cpp
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 #include "strategy-choice.hpp"
27 #include "measurements-entry.hpp"
28 #include "pit-entry.hpp"
29 
30 #include "common/logger.hpp"
31 #include "fw/strategy.hpp"
32 
33 #include <ndn-cxx/util/concepts.hpp>
34 
35 namespace nfd::strategy_choice {
36 
38 
40 
41 using fw::Strategy;
42 
43 static inline bool
45 {
46  return nte.getStrategyChoiceEntry() != nullptr;
47 }
48 
50  : m_forwarder(forwarder)
51  , m_nameTree(m_forwarder.getNameTree())
52 {
53 }
54 
55 void
56 StrategyChoice::setDefaultStrategy(const Name& strategyName)
57 {
58  auto entry = make_unique<Entry>(Name());
59  entry->setStrategy(Strategy::create(strategyName, m_forwarder));
60  NFD_LOG_INFO("setDefaultStrategy " << entry->getStrategyInstanceName());
61 
62  // don't use .insert here, because it will invoke findEffectiveStrategy
63  // which expects an existing root entry
64  name_tree::Entry& nte = m_nameTree.lookup(Name());
65  nte.setStrategyChoiceEntry(std::move(entry));
66  ++m_nItems;
67 }
68 
70 StrategyChoice::insert(const Name& prefix, const Name& strategyName)
71 {
72  if (prefix.size() > NameTree::getMaxDepth()) {
73  return InsertResult::DEPTH_EXCEEDED;
74  }
75 
76  unique_ptr<Strategy> strategy;
77  try {
78  strategy = Strategy::create(strategyName, m_forwarder);
79  }
80  catch (const std::invalid_argument& e) {
81  NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
82  return InsertResult(InsertResult::EXCEPTION, e.what());
83  }
84 
85  if (strategy == nullptr) {
86  NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered");
87  return InsertResult::NOT_REGISTERED;
88  }
89 
90  name_tree::Entry& nte = m_nameTree.lookup(prefix);
91  Entry* entry = nte.getStrategyChoiceEntry();
92  Strategy* oldStrategy = nullptr;
93  if (entry != nullptr) {
94  if (entry->getStrategyInstanceName() == strategy->getInstanceName()) {
95  NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getInstanceName());
96  return InsertResult::OK;
97  }
98  oldStrategy = &entry->getStrategy();
99  NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getInstanceName() <<
100  " to " << strategy->getInstanceName());
101  }
102  else {
103  oldStrategy = &this->findEffectiveStrategy(prefix);
104  auto newEntry = make_unique<Entry>(prefix);
105  entry = newEntry.get();
106  nte.setStrategyChoiceEntry(std::move(newEntry));
107  ++m_nItems;
108  NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getInstanceName());
109  }
110 
111  this->changeStrategy(*entry, *oldStrategy, *strategy);
112  entry->setStrategy(std::move(strategy));
113  return InsertResult::OK;
114 }
115 
116 StrategyChoice::InsertResult::InsertResult(Status status, const std::string& exceptionMessage)
117  : m_status(status)
118  , m_exceptionMessage(exceptionMessage)
119 {
120 }
121 
122 std::ostream&
123 operator<<(std::ostream& os, const StrategyChoice::InsertResult& res)
124 {
125  switch (res.m_status) {
126  case StrategyChoice::InsertResult::OK:
127  return os << "OK";
128  case StrategyChoice::InsertResult::NOT_REGISTERED:
129  return os << "Strategy not registered";
130  case StrategyChoice::InsertResult::EXCEPTION:
131  return os << "Error instantiating strategy: " << res.m_exceptionMessage;
132  case StrategyChoice::InsertResult::DEPTH_EXCEEDED:
133  return os << "Prefix has too many components (limit is "
134  << to_string(NameTree::getMaxDepth()) << ")";
135  }
136  return os;
137 }
138 
139 void
140 StrategyChoice::erase(const Name& prefix)
141 {
142  BOOST_ASSERT(prefix.size() > 0);
143 
144  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
145  if (nte == nullptr) {
146  return;
147  }
148 
149  Entry* entry = nte->getStrategyChoiceEntry();
150  if (entry == nullptr) {
151  return;
152  }
153 
154  Strategy& oldStrategy = entry->getStrategy();
155 
156  Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
157  this->changeStrategy(*entry, oldStrategy, parentStrategy);
158 
159  nte->setStrategyChoiceEntry(nullptr);
160  m_nameTree.eraseIfEmpty(nte);
161  --m_nItems;
162 }
163 
164 std::pair<bool, Name>
165 StrategyChoice::get(const Name& prefix) const
166 {
167  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
168  if (nte == nullptr) {
169  return {false, {}};
170  }
171 
172  Entry* entry = nte->getStrategyChoiceEntry();
173  if (entry == nullptr) {
174  return {false, {}};
175  }
176 
177  return {true, entry->getStrategyInstanceName()};
178 }
179 
180 template<typename K>
181 Strategy&
182 StrategyChoice::findEffectiveStrategyImpl(const K& key) const
183 {
185  BOOST_ASSERT(nte != nullptr);
186  return nte->getStrategyChoiceEntry()->getStrategy();
187 }
188 
189 Strategy&
190 StrategyChoice::findEffectiveStrategy(const Name& prefix) const
191 {
192  return this->findEffectiveStrategyImpl(prefix);
193 }
194 
195 Strategy&
197 {
198  return this->findEffectiveStrategyImpl(pitEntry);
199 }
200 
201 Strategy&
203 {
204  return this->findEffectiveStrategyImpl(measurementsEntry);
205 }
206 
207 static inline void
209 {
210  NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
211 
212  for (const auto& pitEntry : nte.getPitEntries()) {
213  pitEntry->clearStrategyInfo();
214  for (const auto& inRecord : pitEntry->getInRecords()) {
215  const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
216  }
217  for (const auto& outRecord : pitEntry->getOutRecords()) {
218  const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
219  }
220  }
221  if (nte.getMeasurementsEntry() != nullptr) {
223  }
224 }
225 
226 void
227 StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
228 {
229  const Name& oldInstanceName = oldStrategy.getInstanceName();
230  const Name& newInstanceName = newStrategy.getInstanceName();
231  if (Strategy::areSameType(oldInstanceName, newInstanceName)) {
232  // same Strategy subclass type: no need to clear StrategyInfo
233  NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
234  << oldInstanceName << " -> " << newInstanceName << " same-type");
235  return;
236  }
237 
238  NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
239  << oldInstanceName << " -> " << newInstanceName);
240 
241  // reset StrategyInfo on a portion of NameTree,
242  // where entry's effective strategy is covered by the changing StrategyChoice entry
243  const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
244  BOOST_ASSERT(rootNte != nullptr);
245  const auto& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
246  [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
247  if (&nte == rootNte) {
248  return {true, true};
249  }
250  if (nte.getStrategyChoiceEntry() != nullptr) {
251  return {false, false};
252  }
253  return {true, true};
254  });
255  for (const auto& nte : ntChanged) {
256  clearStrategyInfo(nte);
257  }
258 }
259 
261 StrategyChoice::getRange() const
262 {
263  return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
264  boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
266 }
267 
268 } // namespace nfd::strategy_choice
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:54
void clearStrategyInfo()
Clear all StrategyInfo items.
Base class of all forwarding strategies.
Definition: strategy.hpp:42
const Name & getInstanceName() const noexcept
Returns the strategy's instance name.
Definition: strategy.hpp:122
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
Represents an entry in the Measurements table.
An entry in the name tree.
measurements::Entry * getMeasurementsEntry() const
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
void setStrategyChoiceEntry(unique_ptr< strategy_choice::Entry > strategyChoiceEntry)
const Name & getName() const noexcept
strategy_choice::Entry * getStrategyChoiceEntry() const
Range partialEnumerate(const Name &prefix, const EntrySubTreeSelector &entrySubTreeSelector=AnyEntrySubTree()) const
Enumerate all entries under a prefix.
Definition: name-tree.cpp:231
size_t eraseIfEmpty(Entry *entry, bool canEraseAncestors=true)
Delete the entry if it is empty.
Definition: name-tree.cpp:122
Range fullEnumerate(const EntrySelector &entrySelector=AnyEntry()) const
Enumerate all entries.
Definition: name-tree.cpp:225
static constexpr size_t getMaxDepth()
Maximum depth of the name tree.
Definition: name-tree.hpp:51
Entry & lookup(const Name &name, size_t prefixLen)
Find or insert an entry by name.
Definition: name-tree.cpp:43
Entry * getEntry(const EntryT &tableEntry) const
Definition: name-tree.hpp:77
Entry * findExactMatch(const Name &name, size_t prefixLen=std::numeric_limits< size_t >::max()) const
Exact match lookup.
Definition: name-tree.cpp:149
Entry * findLongestPrefixMatch(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
Longest prefix matching.
Definition: name-tree.cpp:161
Represents an entry in the Interest table (PIT).
Definition: pit-entry.hpp:62
Contains information about an Interest from an incoming face.
Contains information about an Interest toward an outgoing face.
Represents an entry in the Strategy Choice table.
const Name & getStrategyInstanceName() const
fw::Strategy & getStrategy() const
void setStrategy(unique_ptr< fw::Strategy > strategy)
Represents the Strategy Choice table.
void erase(const Name &prefix)
Make prefix to inherit strategy from its parent.
boost::range_iterator< Range >::type const_iterator
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
InsertResult insert(const Name &prefix, const Name &strategyName)
Set strategy of prefix to be strategyName.
void setDefaultStrategy(const Name &strategyName)
Set the default strategy.
fw::Strategy & findEffectiveStrategy(const Name &prefix) const
Get effective strategy for prefix.
std::pair< bool, Name > get(const Name &prefix) const
Get strategy Name of prefix.
#define NFD_LOG_ERROR
Definition: logger.hpp:41
#define NFD_LOG_INFO
Definition: logger.hpp:39
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
#define NFD_LOG_TRACE
Definition: logger.hpp:37
static void clearStrategyInfo(const name_tree::Entry &nte)
static bool nteHasStrategyChoiceEntry(const name_tree::Entry &nte)
std::ostream & operator<<(std::ostream &os, const StrategyChoice::InsertResult &res)
NDN_CXX_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator)