blob: cefb245b92013997db66a8c131e56b1d5f55106d [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi4e837f82017-10-02 22:14:43 +00002/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shib184e532016-05-26 18:09:57 +00004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shiee5a4442014-07-27 17:13:43 -070024 */
Junxiao Shibb5105f2014-03-03 12:06:45 -070025
26#include "strategy-choice.hpp"
Junxiao Shic34d1672016-12-09 15:57:59 +000027#include "measurements-entry.hpp"
28#include "pit-entry.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040029
30#include "common/logger.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070031#include "fw/strategy.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070032
Davide Pesavento981db802018-04-10 18:05:04 -040033#include <ndn-cxx/util/concepts.hpp>
34
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::strategy_choice {
Junxiao Shibb5105f2014-03-03 12:06:45 -070036
Davide Pesavento981db802018-04-10 18:05:04 -040037NDN_CXX_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator);
Junxiao Shi3aba7542016-12-24 02:42:52 +000038
Davide Pesaventoa3148082018-04-12 18:21:54 -040039NFD_LOG_INIT(StrategyChoice);
Junxiao Shibb5105f2014-03-03 12:06:45 -070040
Davide Pesavento981db802018-04-10 18:05:04 -040041using fw::Strategy;
42
Junxiao Shi811c0102016-08-10 04:12:45 +000043static inline bool
44nteHasStrategyChoiceEntry(const name_tree::Entry& nte)
45{
46 return nte.getStrategyChoiceEntry() != nullptr;
47}
48
Junxiao Shic34d1672016-12-09 15:57:59 +000049StrategyChoice::StrategyChoice(Forwarder& forwarder)
50 : m_forwarder(forwarder)
51 , m_nameTree(m_forwarder.getNameTree())
Junxiao Shibb5105f2014-03-03 12:06:45 -070052{
Junxiao Shic34d1672016-12-09 15:57:59 +000053}
54
55void
56StrategyChoice::setDefaultStrategy(const Name& strategyName)
57{
Junxiao Shic34d1672016-12-09 15:57:59 +000058 auto entry = make_unique<Entry>(Name());
Junxiao Shif15058a2016-12-11 20:15:05 +000059 entry->setStrategy(Strategy::create(strategyName, m_forwarder));
Junxiao Shi4cb74312016-12-25 20:48:47 +000060 NFD_LOG_INFO("setDefaultStrategy " << entry->getStrategyInstanceName());
Junxiao Shic34d1672016-12-09 15:57:59 +000061
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;
Junxiao Shibb5105f2014-03-03 12:06:45 -070067}
68
Junxiao Shi530cf002017-01-03 14:43:16 +000069StrategyChoice::InsertResult
Junxiao Shibb5105f2014-03-03 12:06:45 -070070StrategyChoice::insert(const Name& prefix, const Name& strategyName)
71{
Junxiao Shi4e837f82017-10-02 22:14:43 +000072 if (prefix.size() > NameTree::getMaxDepth()) {
73 return InsertResult::DEPTH_EXCEEDED;
74 }
75
Junxiao Shi7f566dd2016-12-27 02:28:31 +000076 unique_ptr<Strategy> strategy;
Junxiao Shi18739c42016-12-22 08:03:00 +000077 try {
Junxiao Shi7f566dd2016-12-27 02:28:31 +000078 strategy = Strategy::create(strategyName, m_forwarder);
Junxiao Shi18739c42016-12-22 08:03:00 +000079 }
80 catch (const std::invalid_argument& e) {
81 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
Junxiao Shi530cf002017-01-03 14:43:16 +000082 return InsertResult(InsertResult::EXCEPTION, e.what());
Junxiao Shi18739c42016-12-22 08:03:00 +000083 }
84
Junxiao Shi838c4f12014-11-03 18:55:24 -070085 if (strategy == nullptr) {
Junxiao Shi7f566dd2016-12-27 02:28:31 +000086 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered");
Junxiao Shi530cf002017-01-03 14:43:16 +000087 return InsertResult::NOT_REGISTERED;
Junxiao Shibb5105f2014-03-03 12:06:45 -070088 }
89
Junxiao Shi057d1492018-03-20 17:14:18 +000090 name_tree::Entry& nte = m_nameTree.lookup(prefix);
Junxiao Shi7f358432016-08-11 17:06:33 +000091 Entry* entry = nte.getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -070092 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +000093 if (entry != nullptr) {
Junxiao Shi4cb74312016-12-25 20:48:47 +000094 if (entry->getStrategyInstanceName() == strategy->getInstanceName()) {
95 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getInstanceName());
Junxiao Shi530cf002017-01-03 14:43:16 +000096 return InsertResult::OK;
Junxiao Shie93d6a32014-09-07 16:13:22 -070097 }
Junxiao Shi838c4f12014-11-03 18:55:24 -070098 oldStrategy = &entry->getStrategy();
Junxiao Shi4cb74312016-12-25 20:48:47 +000099 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getInstanceName() <<
100 " to " << strategy->getInstanceName());
Junxiao Shie93d6a32014-09-07 16:13:22 -0700101 }
Junxiao Shif15058a2016-12-11 20:15:05 +0000102 else {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700103 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000104 auto newEntry = make_unique<Entry>(prefix);
105 entry = newEntry.get();
Junxiao Shi7f358432016-08-11 17:06:33 +0000106 nte.setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700107 ++m_nItems;
Junxiao Shi4cb74312016-12-25 20:48:47 +0000108 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getInstanceName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700109 }
110
Junxiao Shi838c4f12014-11-03 18:55:24 -0700111 this->changeStrategy(*entry, *oldStrategy, *strategy);
Junxiao Shi7f566dd2016-12-27 02:28:31 +0000112 entry->setStrategy(std::move(strategy));
Junxiao Shi530cf002017-01-03 14:43:16 +0000113 return InsertResult::OK;
114}
115
116StrategyChoice::InsertResult::InsertResult(Status status, const std::string& exceptionMessage)
117 : m_status(status)
118 , m_exceptionMessage(exceptionMessage)
119{
120}
121
122std::ostream&
123operator<<(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;
Junxiao Shi4e837f82017-10-02 22:14:43 +0000132 case StrategyChoice::InsertResult::DEPTH_EXCEEDED:
133 return os << "Prefix has too many components (limit is "
134 << to_string(NameTree::getMaxDepth()) << ")";
Junxiao Shi530cf002017-01-03 14:43:16 +0000135 }
136 return os;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700137}
138
139void
140StrategyChoice::erase(const Name& prefix)
141{
142 BOOST_ASSERT(prefix.size() > 0);
143
Junxiao Shi811c0102016-08-10 04:12:45 +0000144 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000145 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700146 return;
147 }
148
Junxiao Shiff10da62016-07-13 17:57:43 +0000149 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000150 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700151 return;
152 }
153
154 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700155
156 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700157 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700158
Junxiao Shiff10da62016-07-13 17:57:43 +0000159 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000160 m_nameTree.eraseIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700161 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700162}
163
Junxiao Shi838c4f12014-11-03 18:55:24 -0700164std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700165StrategyChoice::get(const Name& prefix) const
166{
Junxiao Shi811c0102016-08-10 04:12:45 +0000167 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000168 if (nte == nullptr) {
Davide Pesavento50a6af32019-02-21 00:04:40 -0500169 return {false, {}};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700170 }
171
Junxiao Shiff10da62016-07-13 17:57:43 +0000172 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000173 if (entry == nullptr) {
Davide Pesavento50a6af32019-02-21 00:04:40 -0500174 return {false, {}};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700175 }
176
Junxiao Shi4cb74312016-12-25 20:48:47 +0000177 return {true, entry->getStrategyInstanceName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700178}
179
Junxiao Shidd8f6612016-08-12 15:42:52 +0000180template<typename K>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700181Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000182StrategyChoice::findEffectiveStrategyImpl(const K& key) const
Junxiao Shibb5105f2014-03-03 12:06:45 -0700183{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000184 const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000185 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700186 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700187}
188
189Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000190StrategyChoice::findEffectiveStrategy(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +0800191{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000192 return this->findEffectiveStrategyImpl(prefix);
HangZhangcb4fc832014-03-11 16:57:11 +0800193}
194
195Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700196StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
197{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000198 return this->findEffectiveStrategyImpl(pitEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700199}
200
Junxiao Shi7bb01512014-03-05 21:34:09 -0700201Strategy&
202StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
203{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000204 return this->findEffectiveStrategyImpl(measurementsEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700205}
206
Junxiao Shi838c4f12014-11-03 18:55:24 -0700207static inline void
208clearStrategyInfo(const name_tree::Entry& nte)
Junxiao Shie349ea12014-03-12 01:32:42 -0700209{
Junxiao Shie3cf2852016-08-09 03:50:56 +0000210 NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
Junxiao Shi838c4f12014-11-03 18:55:24 -0700211
Davide Pesavento50a6af32019-02-21 00:04:40 -0500212 for (const auto& pitEntry : nte.getPitEntries()) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700213 pitEntry->clearStrategyInfo();
Davide Pesavento50a6af32019-02-21 00:04:40 -0500214 for (const auto& inRecord : pitEntry->getInRecords()) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700215 const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
216 }
Davide Pesavento50a6af32019-02-21 00:04:40 -0500217 for (const auto& outRecord : pitEntry->getOutRecords()) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700218 const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
219 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700220 }
Junxiao Shib184e532016-05-26 18:09:57 +0000221 if (nte.getMeasurementsEntry() != nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700222 nte.getMeasurementsEntry()->clearStrategyInfo();
Junxiao Shie349ea12014-03-12 01:32:42 -0700223 }
224}
225
Junxiao Shibb5105f2014-03-03 12:06:45 -0700226void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000227StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700228{
Junxiao Shi55e21b92017-01-23 03:27:47 +0000229 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() << ") "
Davide Pesavento50a6af32019-02-21 00:04:40 -0500234 << oldInstanceName << " -> " << newInstanceName << " same-type");
Junxiao Shibb5105f2014-03-03 12:06:45 -0700235 return;
236 }
237
Junxiao Shi55e21b92017-01-23 03:27:47 +0000238 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
239 << oldInstanceName << " -> " << newInstanceName);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700240
241 // reset StrategyInfo on a portion of NameTree,
242 // where entry's effective strategy is covered by the changing StrategyChoice entry
Junxiao Shi7f358432016-08-11 17:06:33 +0000243 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000244 BOOST_ASSERT(rootNte != nullptr);
Davide Pesavento50a6af32019-02-21 00:04:40 -0500245 const auto& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
Junxiao Shi838c4f12014-11-03 18:55:24 -0700246 [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
247 if (&nte == rootNte) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700248 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700249 }
Junxiao Shib184e532016-05-26 18:09:57 +0000250 if (nte.getStrategyChoiceEntry() != nullptr) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700251 return {false, false};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700252 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700253 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700254 });
Davide Pesavento50a6af32019-02-21 00:04:40 -0500255 for (const auto& nte : ntChanged) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700256 clearStrategyInfo(nte);
257 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700258}
259
Junxiao Shib30863e2016-08-15 21:32:01 +0000260StrategyChoice::Range
261StrategyChoice::getRange() const
Junxiao Shib5888d22014-05-26 07:35:22 -0700262{
Junxiao Shib30863e2016-08-15 21:32:01 +0000263 return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
264 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
265 &name_tree::Entry::getStrategyChoiceEntry));
Junxiao Shib5888d22014-05-26 07:35:22 -0700266}
267
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400268} // namespace nfd::strategy_choice