blob: 81de636e7e8f08cd2cbf86f23ef12cc824c48279 [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shib184e532016-05-26 18:09:57 +00003 * Copyright (c) 2014-2016, 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.
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"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060029#include "core/logger.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070030#include "fw/strategy.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070031
32namespace nfd {
Junxiao Shiff10da62016-07-13 17:57:43 +000033namespace strategy_choice {
Junxiao Shibb5105f2014-03-03 12:06:45 -070034
Junxiao Shibb5105f2014-03-03 12:06:45 -070035using fw::Strategy;
36
37NFD_LOG_INIT("StrategyChoice");
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 Shib5888d22014-05-26 07:35:22 -070048 , m_nItems(0)
Junxiao Shibb5105f2014-03-03 12:06:45 -070049{
Junxiao Shic34d1672016-12-09 15:57:59 +000050}
51
52void
53StrategyChoice::setDefaultStrategy(const Name& strategyName)
54{
55 unique_ptr<Strategy> strategy = Strategy::create(strategyName, m_forwarder);
56
57 bool isInstalled = false;
58 Strategy* instance = nullptr;
59 std::tie(isInstalled, instance) = this->install(std::move(strategy));
60 BOOST_ASSERT(isInstalled);
61
62 auto entry = make_unique<Entry>(Name());
63 entry->setStrategy(*instance);
64
65 // don't use .insert here, because it will invoke findEffectiveStrategy
66 // which expects an existing root entry
67 name_tree::Entry& nte = m_nameTree.lookup(Name());
68 nte.setStrategyChoiceEntry(std::move(entry));
69 ++m_nItems;
70 NFD_LOG_INFO("setDefaultStrategy " << instance->getName());
71}
72
73void
74StrategyChoice::installFromRegistry()
75{
76 /// \todo #3868
77 /// * Give every StrategyChoice entry its own Strategy instance.
78 /// Don't share Strategy instances.
79 /// * Create Strategy instance with Strategy::create.
80 /// * Eliminate install, installFromRegistry, getStrategy functions.
81
82 // This is temporary code until the above is done.
83
84 for (const Name& strategyName : Strategy::listRegistered()) {
85 this->install(Strategy::create(strategyName, m_forwarder));
86 }
Junxiao Shibb5105f2014-03-03 12:06:45 -070087}
88
89bool
Junxiao Shie93d6a32014-09-07 16:13:22 -070090StrategyChoice::hasStrategy(const Name& strategyName, bool isExact) const
Junxiao Shibb5105f2014-03-03 12:06:45 -070091{
Junxiao Shie93d6a32014-09-07 16:13:22 -070092 if (isExact) {
93 return m_strategyInstances.count(strategyName) > 0;
94 }
95 else {
Junxiao Shib184e532016-05-26 18:09:57 +000096 return this->getStrategy(strategyName) != nullptr;
Junxiao Shie93d6a32014-09-07 16:13:22 -070097 }
Junxiao Shibb5105f2014-03-03 12:06:45 -070098}
99
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000100std::pair<bool, Strategy*>
101StrategyChoice::install(unique_ptr<Strategy> strategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700102{
Junxiao Shib184e532016-05-26 18:09:57 +0000103 BOOST_ASSERT(strategy != nullptr);
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000104 Name strategyName = strategy->getName();
105 // copying Name, so that strategyName remains available even if strategy is deallocated
Junxiao Shibb5105f2014-03-03 12:06:45 -0700106
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000107 bool isInserted = false;
108 StrategyInstanceTable::iterator it;
109 std::tie(it, isInserted) = m_strategyInstances.emplace(strategyName, std::move(strategy));
110
111 if (!isInserted) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700112 NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName");
Junxiao Shibb5105f2014-03-03 12:06:45 -0700113 }
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000114 return std::make_pair(isInserted, it->second.get());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700115}
116
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000117Strategy*
Junxiao Shie93d6a32014-09-07 16:13:22 -0700118StrategyChoice::getStrategy(const Name& strategyName) const
119{
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000120 Strategy* candidate = nullptr;
Junxiao Shi838c4f12014-11-03 18:55:24 -0700121 for (auto it = m_strategyInstances.lower_bound(strategyName);
Junxiao Shie93d6a32014-09-07 16:13:22 -0700122 it != m_strategyInstances.end() && strategyName.isPrefixOf(it->first); ++it) {
123 switch (it->first.size() - strategyName.size()) {
124 case 0: // exact match
Junxiao Shi838c4f12014-11-03 18:55:24 -0700125 return it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700126 case 1: // unversioned strategyName matches versioned strategy
Junxiao Shi838c4f12014-11-03 18:55:24 -0700127 candidate = it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700128 break;
129 }
130 }
131 return candidate;
132}
133
Junxiao Shibb5105f2014-03-03 12:06:45 -0700134bool
135StrategyChoice::insert(const Name& prefix, const Name& strategyName)
136{
Junxiao Shi838c4f12014-11-03 18:55:24 -0700137 Strategy* strategy = this->getStrategy(strategyName);
138 if (strategy == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700139 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed");
140 return false;
141 }
142
Junxiao Shi7f358432016-08-11 17:06:33 +0000143 name_tree::Entry& nte = m_nameTree.lookup(prefix);
144 Entry* entry = nte.getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700145 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +0000146 if (entry != nullptr) {
Junxiao Shie93d6a32014-09-07 16:13:22 -0700147 if (entry->getStrategy().getName() == strategy->getName()) {
148 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getName());
149 return true;
150 }
Junxiao Shi838c4f12014-11-03 18:55:24 -0700151 oldStrategy = &entry->getStrategy();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700152 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getName() <<
153 " to " << strategy->getName());
154 }
155
Junxiao Shib184e532016-05-26 18:09:57 +0000156 if (entry == nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700157 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000158 auto newEntry = make_unique<Entry>(prefix);
159 entry = newEntry.get();
Junxiao Shi7f358432016-08-11 17:06:33 +0000160 nte.setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700161 ++m_nItems;
Junxiao Shie93d6a32014-09-07 16:13:22 -0700162 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700163 }
164
Junxiao Shi838c4f12014-11-03 18:55:24 -0700165 this->changeStrategy(*entry, *oldStrategy, *strategy);
166 entry->setStrategy(*strategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700167 return true;
168}
169
170void
171StrategyChoice::erase(const Name& prefix)
172{
173 BOOST_ASSERT(prefix.size() > 0);
174
Junxiao Shi811c0102016-08-10 04:12:45 +0000175 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000176 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700177 return;
178 }
179
Junxiao Shiff10da62016-07-13 17:57:43 +0000180 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000181 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700182 return;
183 }
184
185 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700186
187 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700188 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700189
Junxiao Shiff10da62016-07-13 17:57:43 +0000190 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000191 m_nameTree.eraseIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700192 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700193}
194
Junxiao Shi838c4f12014-11-03 18:55:24 -0700195std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700196StrategyChoice::get(const Name& prefix) const
197{
Junxiao Shi811c0102016-08-10 04:12:45 +0000198 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000199 if (nte == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000200 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700201 }
202
Junxiao Shiff10da62016-07-13 17:57:43 +0000203 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000204 if (entry == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000205 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700206 }
207
Junxiao Shiff10da62016-07-13 17:57:43 +0000208 return {true, entry->getStrategy().getName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700209}
210
Junxiao Shidd8f6612016-08-12 15:42:52 +0000211template<typename K>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700212Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000213StrategyChoice::findEffectiveStrategyImpl(const K& key) const
Junxiao Shibb5105f2014-03-03 12:06:45 -0700214{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000215 const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000216 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700217 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700218}
219
220Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000221StrategyChoice::findEffectiveStrategy(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +0800222{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000223 return this->findEffectiveStrategyImpl(prefix);
HangZhangcb4fc832014-03-11 16:57:11 +0800224}
225
226Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700227StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
228{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000229 return this->findEffectiveStrategyImpl(pitEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700230}
231
Junxiao Shi7bb01512014-03-05 21:34:09 -0700232Strategy&
233StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
234{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000235 return this->findEffectiveStrategyImpl(measurementsEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700236}
237
Junxiao Shi838c4f12014-11-03 18:55:24 -0700238static inline void
239clearStrategyInfo(const name_tree::Entry& nte)
Junxiao Shie349ea12014-03-12 01:32:42 -0700240{
Junxiao Shie3cf2852016-08-09 03:50:56 +0000241 NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
Junxiao Shi838c4f12014-11-03 18:55:24 -0700242
243 for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
244 pitEntry->clearStrategyInfo();
245 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
246 const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
247 }
248 for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
249 const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
250 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700251 }
Junxiao Shib184e532016-05-26 18:09:57 +0000252 if (nte.getMeasurementsEntry() != nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700253 nte.getMeasurementsEntry()->clearStrategyInfo();
Junxiao Shie349ea12014-03-12 01:32:42 -0700254 }
255}
256
Junxiao Shibb5105f2014-03-03 12:06:45 -0700257void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000258StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700259{
Junxiao Shi838c4f12014-11-03 18:55:24 -0700260 if (&oldStrategy == &newStrategy) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700261 return;
262 }
263
Junxiao Shi838c4f12014-11-03 18:55:24 -0700264 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ")"
265 << " from " << oldStrategy.getName()
266 << " to " << newStrategy.getName());
267
268 // reset StrategyInfo on a portion of NameTree,
269 // where entry's effective strategy is covered by the changing StrategyChoice entry
Junxiao Shi7f358432016-08-11 17:06:33 +0000270 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000271 BOOST_ASSERT(rootNte != nullptr);
Junxiao Shi60607c72014-11-26 22:40:36 -0700272 auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
Junxiao Shi838c4f12014-11-03 18:55:24 -0700273 [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
274 if (&nte == rootNte) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700275 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700276 }
Junxiao Shib184e532016-05-26 18:09:57 +0000277 if (nte.getStrategyChoiceEntry() != nullptr) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700278 return {false, false};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700279 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700280 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700281 });
Junxiao Shi60607c72014-11-26 22:40:36 -0700282 for (const name_tree::Entry& nte : ntChanged) {
283 clearStrategyInfo(nte);
284 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700285}
286
Junxiao Shib30863e2016-08-15 21:32:01 +0000287StrategyChoice::Range
288StrategyChoice::getRange() const
Junxiao Shib5888d22014-05-26 07:35:22 -0700289{
Junxiao Shib30863e2016-08-15 21:32:01 +0000290 return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
291 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
292 &name_tree::Entry::getStrategyChoiceEntry));
Junxiao Shib5888d22014-05-26 07:35:22 -0700293}
294
Junxiao Shiff10da62016-07-13 17:57:43 +0000295} // namespace strategy_choice
Junxiao Shibb5105f2014-03-03 12:06:45 -0700296} // namespace nfd