blob: c7b7a1a4a4868833b8a10188e1452eb28263e5de [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 Pesavento8f0b8b62023-08-29 21:04:08 -04003 * Copyright (c) 2014-2023, 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 Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::strategy_choice {
Junxiao Shibb5105f2014-03-03 12:06:45 -070034
Davide Pesaventoa3148082018-04-12 18:21:54 -040035NFD_LOG_INIT(StrategyChoice);
Junxiao Shibb5105f2014-03-03 12:06:45 -070036
Davide Pesavento981db802018-04-10 18:05:04 -040037using fw::Strategy;
38
Junxiao Shi811c0102016-08-10 04:12:45 +000039static inline bool
40nteHasStrategyChoiceEntry(const name_tree::Entry& nte)
41{
42 return nte.getStrategyChoiceEntry() != nullptr;
43}
44
Junxiao Shic34d1672016-12-09 15:57:59 +000045StrategyChoice::StrategyChoice(Forwarder& forwarder)
46 : m_forwarder(forwarder)
47 , m_nameTree(m_forwarder.getNameTree())
Junxiao Shibb5105f2014-03-03 12:06:45 -070048{
Junxiao Shic34d1672016-12-09 15:57:59 +000049}
50
51void
52StrategyChoice::setDefaultStrategy(const Name& strategyName)
53{
Junxiao Shic34d1672016-12-09 15:57:59 +000054 auto entry = make_unique<Entry>(Name());
Junxiao Shif15058a2016-12-11 20:15:05 +000055 entry->setStrategy(Strategy::create(strategyName, m_forwarder));
Junxiao Shi4cb74312016-12-25 20:48:47 +000056 NFD_LOG_INFO("setDefaultStrategy " << entry->getStrategyInstanceName());
Junxiao Shic34d1672016-12-09 15:57:59 +000057
58 // don't use .insert here, because it will invoke findEffectiveStrategy
59 // which expects an existing root entry
60 name_tree::Entry& nte = m_nameTree.lookup(Name());
61 nte.setStrategyChoiceEntry(std::move(entry));
62 ++m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -070063}
64
Junxiao Shi530cf002017-01-03 14:43:16 +000065StrategyChoice::InsertResult
Junxiao Shibb5105f2014-03-03 12:06:45 -070066StrategyChoice::insert(const Name& prefix, const Name& strategyName)
67{
Junxiao Shi4e837f82017-10-02 22:14:43 +000068 if (prefix.size() > NameTree::getMaxDepth()) {
69 return InsertResult::DEPTH_EXCEEDED;
70 }
71
Junxiao Shi7f566dd2016-12-27 02:28:31 +000072 unique_ptr<Strategy> strategy;
Junxiao Shi18739c42016-12-22 08:03:00 +000073 try {
Junxiao Shi7f566dd2016-12-27 02:28:31 +000074 strategy = Strategy::create(strategyName, m_forwarder);
Junxiao Shi18739c42016-12-22 08:03:00 +000075 }
76 catch (const std::invalid_argument& e) {
77 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
Junxiao Shi530cf002017-01-03 14:43:16 +000078 return InsertResult(InsertResult::EXCEPTION, e.what());
Junxiao Shi18739c42016-12-22 08:03:00 +000079 }
80
Junxiao Shi838c4f12014-11-03 18:55:24 -070081 if (strategy == nullptr) {
Junxiao Shi7f566dd2016-12-27 02:28:31 +000082 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered");
Junxiao Shi530cf002017-01-03 14:43:16 +000083 return InsertResult::NOT_REGISTERED;
Junxiao Shibb5105f2014-03-03 12:06:45 -070084 }
85
Junxiao Shi057d1492018-03-20 17:14:18 +000086 name_tree::Entry& nte = m_nameTree.lookup(prefix);
Junxiao Shi7f358432016-08-11 17:06:33 +000087 Entry* entry = nte.getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -070088 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +000089 if (entry != nullptr) {
Junxiao Shi4cb74312016-12-25 20:48:47 +000090 if (entry->getStrategyInstanceName() == strategy->getInstanceName()) {
91 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getInstanceName());
Junxiao Shi530cf002017-01-03 14:43:16 +000092 return InsertResult::OK;
Junxiao Shie93d6a32014-09-07 16:13:22 -070093 }
Junxiao Shi838c4f12014-11-03 18:55:24 -070094 oldStrategy = &entry->getStrategy();
Junxiao Shi4cb74312016-12-25 20:48:47 +000095 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getInstanceName() <<
96 " to " << strategy->getInstanceName());
Junxiao Shie93d6a32014-09-07 16:13:22 -070097 }
Junxiao Shif15058a2016-12-11 20:15:05 +000098 else {
Junxiao Shi838c4f12014-11-03 18:55:24 -070099 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000100 auto newEntry = make_unique<Entry>(prefix);
101 entry = newEntry.get();
Junxiao Shi7f358432016-08-11 17:06:33 +0000102 nte.setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700103 ++m_nItems;
Junxiao Shi4cb74312016-12-25 20:48:47 +0000104 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getInstanceName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700105 }
106
Junxiao Shi838c4f12014-11-03 18:55:24 -0700107 this->changeStrategy(*entry, *oldStrategy, *strategy);
Junxiao Shi7f566dd2016-12-27 02:28:31 +0000108 entry->setStrategy(std::move(strategy));
Junxiao Shi530cf002017-01-03 14:43:16 +0000109 return InsertResult::OK;
110}
111
112StrategyChoice::InsertResult::InsertResult(Status status, const std::string& exceptionMessage)
113 : m_status(status)
114 , m_exceptionMessage(exceptionMessage)
115{
116}
117
118std::ostream&
119operator<<(std::ostream& os, const StrategyChoice::InsertResult& res)
120{
121 switch (res.m_status) {
122 case StrategyChoice::InsertResult::OK:
123 return os << "OK";
124 case StrategyChoice::InsertResult::NOT_REGISTERED:
125 return os << "Strategy not registered";
126 case StrategyChoice::InsertResult::EXCEPTION:
127 return os << "Error instantiating strategy: " << res.m_exceptionMessage;
Junxiao Shi4e837f82017-10-02 22:14:43 +0000128 case StrategyChoice::InsertResult::DEPTH_EXCEEDED:
129 return os << "Prefix has too many components (limit is "
130 << to_string(NameTree::getMaxDepth()) << ")";
Junxiao Shi530cf002017-01-03 14:43:16 +0000131 }
132 return os;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700133}
134
135void
136StrategyChoice::erase(const Name& prefix)
137{
138 BOOST_ASSERT(prefix.size() > 0);
139
Junxiao Shi811c0102016-08-10 04:12:45 +0000140 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000141 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700142 return;
143 }
144
Junxiao Shiff10da62016-07-13 17:57:43 +0000145 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000146 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700147 return;
148 }
149
150 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700151
152 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700153 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700154
Junxiao Shiff10da62016-07-13 17:57:43 +0000155 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000156 m_nameTree.eraseIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700157 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700158}
159
Junxiao Shi838c4f12014-11-03 18:55:24 -0700160std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700161StrategyChoice::get(const Name& prefix) const
162{
Junxiao Shi811c0102016-08-10 04:12:45 +0000163 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000164 if (nte == nullptr) {
Davide Pesavento50a6af32019-02-21 00:04:40 -0500165 return {false, {}};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700166 }
167
Junxiao Shiff10da62016-07-13 17:57:43 +0000168 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000169 if (entry == nullptr) {
Davide Pesavento50a6af32019-02-21 00:04:40 -0500170 return {false, {}};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700171 }
172
Junxiao Shi4cb74312016-12-25 20:48:47 +0000173 return {true, entry->getStrategyInstanceName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700174}
175
Junxiao Shidd8f6612016-08-12 15:42:52 +0000176template<typename K>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700177Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000178StrategyChoice::findEffectiveStrategyImpl(const K& key) const
Junxiao Shibb5105f2014-03-03 12:06:45 -0700179{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000180 const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000181 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700182 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700183}
184
185Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000186StrategyChoice::findEffectiveStrategy(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +0800187{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000188 return this->findEffectiveStrategyImpl(prefix);
HangZhangcb4fc832014-03-11 16:57:11 +0800189}
190
191Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700192StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
193{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000194 return this->findEffectiveStrategyImpl(pitEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700195}
196
Junxiao Shi7bb01512014-03-05 21:34:09 -0700197Strategy&
198StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
199{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000200 return this->findEffectiveStrategyImpl(measurementsEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700201}
202
Junxiao Shi838c4f12014-11-03 18:55:24 -0700203static inline void
204clearStrategyInfo(const name_tree::Entry& nte)
Junxiao Shie349ea12014-03-12 01:32:42 -0700205{
Junxiao Shie3cf2852016-08-09 03:50:56 +0000206 NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
Junxiao Shi838c4f12014-11-03 18:55:24 -0700207
Davide Pesavento50a6af32019-02-21 00:04:40 -0500208 for (const auto& pitEntry : nte.getPitEntries()) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700209 pitEntry->clearStrategyInfo();
Davide Pesavento50a6af32019-02-21 00:04:40 -0500210 for (const auto& inRecord : pitEntry->getInRecords()) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700211 const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
212 }
Davide Pesavento50a6af32019-02-21 00:04:40 -0500213 for (const auto& outRecord : pitEntry->getOutRecords()) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700214 const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
215 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700216 }
Junxiao Shib184e532016-05-26 18:09:57 +0000217 if (nte.getMeasurementsEntry() != nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700218 nte.getMeasurementsEntry()->clearStrategyInfo();
Junxiao Shie349ea12014-03-12 01:32:42 -0700219 }
220}
221
Junxiao Shibb5105f2014-03-03 12:06:45 -0700222void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000223StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700224{
Junxiao Shi55e21b92017-01-23 03:27:47 +0000225 const Name& oldInstanceName = oldStrategy.getInstanceName();
226 const Name& newInstanceName = newStrategy.getInstanceName();
227 if (Strategy::areSameType(oldInstanceName, newInstanceName)) {
228 // same Strategy subclass type: no need to clear StrategyInfo
229 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
Davide Pesavento50a6af32019-02-21 00:04:40 -0500230 << oldInstanceName << " -> " << newInstanceName << " same-type");
Junxiao Shibb5105f2014-03-03 12:06:45 -0700231 return;
232 }
233
Junxiao Shi55e21b92017-01-23 03:27:47 +0000234 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
235 << oldInstanceName << " -> " << newInstanceName);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700236
237 // reset StrategyInfo on a portion of NameTree,
238 // where entry's effective strategy is covered by the changing StrategyChoice entry
Junxiao Shi7f358432016-08-11 17:06:33 +0000239 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000240 BOOST_ASSERT(rootNte != nullptr);
Davide Pesavento50a6af32019-02-21 00:04:40 -0500241 const auto& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
Junxiao Shi838c4f12014-11-03 18:55:24 -0700242 [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
243 if (&nte == rootNte) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700244 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700245 }
Junxiao Shib184e532016-05-26 18:09:57 +0000246 if (nte.getStrategyChoiceEntry() != nullptr) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700247 return {false, false};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700248 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700249 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700250 });
Davide Pesavento50a6af32019-02-21 00:04:40 -0500251 for (const auto& nte : ntChanged) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700252 clearStrategyInfo(nte);
253 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700254}
255
Junxiao Shib30863e2016-08-15 21:32:01 +0000256StrategyChoice::Range
257StrategyChoice::getRange() const
Junxiao Shib5888d22014-05-26 07:35:22 -0700258{
Junxiao Shib30863e2016-08-15 21:32:01 +0000259 return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
260 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
261 &name_tree::Entry::getStrategyChoiceEntry));
Junxiao Shib5888d22014-05-26 07:35:22 -0700262}
263
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400264} // namespace nfd::strategy_choice