blob: 985d05cd9ad8ca4b3e5e8c74070d2262e78acfbf [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{
Junxiao Shic34d1672016-12-09 15:57:59 +000055 auto entry = make_unique<Entry>(Name());
Junxiao Shif15058a2016-12-11 20:15:05 +000056 entry->setStrategy(Strategy::create(strategyName, m_forwarder));
57 NFD_LOG_INFO("setDefaultStrategy " << entry->getStrategyName());
Junxiao Shic34d1672016-12-09 15:57:59 +000058
59 // don't use .insert here, because it will invoke findEffectiveStrategy
60 // which expects an existing root entry
61 name_tree::Entry& nte = m_nameTree.lookup(Name());
62 nte.setStrategyChoiceEntry(std::move(entry));
63 ++m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -070064}
65
66bool
Junxiao Shie93d6a32014-09-07 16:13:22 -070067StrategyChoice::hasStrategy(const Name& strategyName, bool isExact) const
Junxiao Shibb5105f2014-03-03 12:06:45 -070068{
Junxiao Shie93d6a32014-09-07 16:13:22 -070069 if (isExact) {
70 return m_strategyInstances.count(strategyName) > 0;
71 }
72 else {
Junxiao Shif15058a2016-12-11 20:15:05 +000073 return Strategy::canCreate(strategyName) ||
74 this->getStrategy(strategyName) != nullptr;
Junxiao Shie93d6a32014-09-07 16:13:22 -070075 }
Junxiao Shibb5105f2014-03-03 12:06:45 -070076}
77
Junxiao Shia49a1ab2016-07-15 18:24:36 +000078std::pair<bool, Strategy*>
79StrategyChoice::install(unique_ptr<Strategy> strategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -070080{
Junxiao Shif15058a2016-12-11 20:15:05 +000081 /// \todo #3868
82 /// Strategy instance should always be created from Strategy::create;
83 /// m_strategyInstances, install, getStrategy, hasStrategy should be eliminated.
84
Junxiao Shib184e532016-05-26 18:09:57 +000085 BOOST_ASSERT(strategy != nullptr);
Junxiao Shia49a1ab2016-07-15 18:24:36 +000086 Name strategyName = strategy->getName();
87 // copying Name, so that strategyName remains available even if strategy is deallocated
Junxiao Shibb5105f2014-03-03 12:06:45 -070088
Junxiao Shia49a1ab2016-07-15 18:24:36 +000089 bool isInserted = false;
90 StrategyInstanceTable::iterator it;
91 std::tie(it, isInserted) = m_strategyInstances.emplace(strategyName, std::move(strategy));
92
93 if (!isInserted) {
Junxiao Shibb5105f2014-03-03 12:06:45 -070094 NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName");
Junxiao Shibb5105f2014-03-03 12:06:45 -070095 }
Junxiao Shia49a1ab2016-07-15 18:24:36 +000096 return std::make_pair(isInserted, it->second.get());
Junxiao Shibb5105f2014-03-03 12:06:45 -070097}
98
Junxiao Shia49a1ab2016-07-15 18:24:36 +000099Strategy*
Junxiao Shie93d6a32014-09-07 16:13:22 -0700100StrategyChoice::getStrategy(const Name& strategyName) const
101{
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000102 Strategy* candidate = nullptr;
Junxiao Shi838c4f12014-11-03 18:55:24 -0700103 for (auto it = m_strategyInstances.lower_bound(strategyName);
Junxiao Shie93d6a32014-09-07 16:13:22 -0700104 it != m_strategyInstances.end() && strategyName.isPrefixOf(it->first); ++it) {
105 switch (it->first.size() - strategyName.size()) {
106 case 0: // exact match
Junxiao Shi838c4f12014-11-03 18:55:24 -0700107 return it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700108 case 1: // unversioned strategyName matches versioned strategy
Junxiao Shi838c4f12014-11-03 18:55:24 -0700109 candidate = it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700110 break;
111 }
112 }
113 return candidate;
114}
115
Junxiao Shibb5105f2014-03-03 12:06:45 -0700116bool
117StrategyChoice::insert(const Name& prefix, const Name& strategyName)
118{
Junxiao Shif15058a2016-12-11 20:15:05 +0000119 unique_ptr<Strategy> createdStrategy = Strategy::create(strategyName, m_forwarder);
120 Strategy* strategy = createdStrategy.get();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700121 if (strategy == nullptr) {
Junxiao Shif15058a2016-12-11 20:15:05 +0000122 strategy = this->getStrategy(strategyName);
123 if (strategy == nullptr) {
124 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered or installed");
125 return false;
126 }
127 NFD_LOG_WARN("insert(" << prefix << "," << strategyName << ") using shared strategy instance");
Junxiao Shibb5105f2014-03-03 12:06:45 -0700128 }
129
Junxiao Shi7f358432016-08-11 17:06:33 +0000130 name_tree::Entry& nte = m_nameTree.lookup(prefix);
131 Entry* entry = nte.getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700132 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +0000133 if (entry != nullptr) {
Junxiao Shif15058a2016-12-11 20:15:05 +0000134 if (entry->getStrategyName() == strategy->getName()) {
Junxiao Shie93d6a32014-09-07 16:13:22 -0700135 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getName());
136 return true;
137 }
Junxiao Shi838c4f12014-11-03 18:55:24 -0700138 oldStrategy = &entry->getStrategy();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700139 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getName() <<
140 " to " << strategy->getName());
141 }
Junxiao Shif15058a2016-12-11 20:15:05 +0000142 else {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700143 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000144 auto newEntry = make_unique<Entry>(prefix);
145 entry = newEntry.get();
Junxiao Shi7f358432016-08-11 17:06:33 +0000146 nte.setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700147 ++m_nItems;
Junxiao Shie93d6a32014-09-07 16:13:22 -0700148 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700149 }
150
Junxiao Shi838c4f12014-11-03 18:55:24 -0700151 this->changeStrategy(*entry, *oldStrategy, *strategy);
Junxiao Shif15058a2016-12-11 20:15:05 +0000152 if (createdStrategy != nullptr) {
153 entry->setStrategy(std::move(createdStrategy));
154 }
155 else {
156 entry->setStrategy(*strategy);
157 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700158 return true;
159}
160
161void
162StrategyChoice::erase(const Name& prefix)
163{
164 BOOST_ASSERT(prefix.size() > 0);
165
Junxiao Shi811c0102016-08-10 04:12:45 +0000166 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000167 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700168 return;
169 }
170
Junxiao Shiff10da62016-07-13 17:57:43 +0000171 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000172 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700173 return;
174 }
175
176 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700177
178 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700179 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700180
Junxiao Shiff10da62016-07-13 17:57:43 +0000181 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000182 m_nameTree.eraseIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700183 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700184}
185
Junxiao Shi838c4f12014-11-03 18:55:24 -0700186std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700187StrategyChoice::get(const Name& prefix) const
188{
Junxiao Shi811c0102016-08-10 04:12:45 +0000189 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000190 if (nte == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000191 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700192 }
193
Junxiao Shiff10da62016-07-13 17:57:43 +0000194 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000195 if (entry == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000196 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700197 }
198
Junxiao Shiff10da62016-07-13 17:57:43 +0000199 return {true, entry->getStrategy().getName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700200}
201
Junxiao Shidd8f6612016-08-12 15:42:52 +0000202template<typename K>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700203Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000204StrategyChoice::findEffectiveStrategyImpl(const K& key) const
Junxiao Shibb5105f2014-03-03 12:06:45 -0700205{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000206 const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000207 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700208 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700209}
210
211Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000212StrategyChoice::findEffectiveStrategy(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +0800213{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000214 return this->findEffectiveStrategyImpl(prefix);
HangZhangcb4fc832014-03-11 16:57:11 +0800215}
216
217Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700218StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
219{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000220 return this->findEffectiveStrategyImpl(pitEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700221}
222
Junxiao Shi7bb01512014-03-05 21:34:09 -0700223Strategy&
224StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
225{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000226 return this->findEffectiveStrategyImpl(measurementsEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700227}
228
Junxiao Shi838c4f12014-11-03 18:55:24 -0700229static inline void
230clearStrategyInfo(const name_tree::Entry& nte)
Junxiao Shie349ea12014-03-12 01:32:42 -0700231{
Junxiao Shie3cf2852016-08-09 03:50:56 +0000232 NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
Junxiao Shi838c4f12014-11-03 18:55:24 -0700233
234 for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
235 pitEntry->clearStrategyInfo();
236 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
237 const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
238 }
239 for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
240 const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
241 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700242 }
Junxiao Shib184e532016-05-26 18:09:57 +0000243 if (nte.getMeasurementsEntry() != nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700244 nte.getMeasurementsEntry()->clearStrategyInfo();
Junxiao Shie349ea12014-03-12 01:32:42 -0700245 }
246}
247
Junxiao Shibb5105f2014-03-03 12:06:45 -0700248void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000249StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700250{
Junxiao Shi838c4f12014-11-03 18:55:24 -0700251 if (&oldStrategy == &newStrategy) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700252 return;
253 }
254
Junxiao Shi838c4f12014-11-03 18:55:24 -0700255 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ")"
256 << " from " << oldStrategy.getName()
257 << " to " << newStrategy.getName());
258
259 // reset StrategyInfo on a portion of NameTree,
260 // where entry's effective strategy is covered by the changing StrategyChoice entry
Junxiao Shi7f358432016-08-11 17:06:33 +0000261 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000262 BOOST_ASSERT(rootNte != nullptr);
Junxiao Shi60607c72014-11-26 22:40:36 -0700263 auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
Junxiao Shi838c4f12014-11-03 18:55:24 -0700264 [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
265 if (&nte == rootNte) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700266 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700267 }
Junxiao Shib184e532016-05-26 18:09:57 +0000268 if (nte.getStrategyChoiceEntry() != nullptr) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700269 return {false, false};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700270 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700271 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700272 });
Junxiao Shi60607c72014-11-26 22:40:36 -0700273 for (const name_tree::Entry& nte : ntChanged) {
274 clearStrategyInfo(nte);
275 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700276}
277
Junxiao Shib30863e2016-08-15 21:32:01 +0000278StrategyChoice::Range
279StrategyChoice::getRange() const
Junxiao Shib5888d22014-05-26 07:35:22 -0700280{
Junxiao Shib30863e2016-08-15 21:32:01 +0000281 return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
282 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
283 &name_tree::Entry::getStrategyChoiceEntry));
Junxiao Shib5888d22014-05-26 07:35:22 -0700284}
285
Junxiao Shiff10da62016-07-13 17:57:43 +0000286} // namespace strategy_choice
Junxiao Shibb5105f2014-03-03 12:06:45 -0700287} // namespace nfd