blob: 63e33ee08333d25131c667074155944a79e88909 [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 Shi18739c42016-12-22 08:03:00 +0000119 unique_ptr<Strategy> createdStrategy;
120 try {
121 createdStrategy = Strategy::create(strategyName, m_forwarder);
122 }
123 catch (const std::invalid_argument& e) {
124 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
125 return false;
126 }
127
Junxiao Shif15058a2016-12-11 20:15:05 +0000128 Strategy* strategy = createdStrategy.get();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700129 if (strategy == nullptr) {
Junxiao Shif15058a2016-12-11 20:15:05 +0000130 strategy = this->getStrategy(strategyName);
131 if (strategy == nullptr) {
132 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered or installed");
133 return false;
134 }
135 NFD_LOG_WARN("insert(" << prefix << "," << strategyName << ") using shared strategy instance");
Junxiao Shibb5105f2014-03-03 12:06:45 -0700136 }
137
Junxiao Shi7f358432016-08-11 17:06:33 +0000138 name_tree::Entry& nte = m_nameTree.lookup(prefix);
139 Entry* entry = nte.getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700140 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +0000141 if (entry != nullptr) {
Junxiao Shif15058a2016-12-11 20:15:05 +0000142 if (entry->getStrategyName() == strategy->getName()) {
Junxiao Shie93d6a32014-09-07 16:13:22 -0700143 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getName());
144 return true;
145 }
Junxiao Shi838c4f12014-11-03 18:55:24 -0700146 oldStrategy = &entry->getStrategy();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700147 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getName() <<
148 " to " << strategy->getName());
149 }
Junxiao Shif15058a2016-12-11 20:15:05 +0000150 else {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700151 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000152 auto newEntry = make_unique<Entry>(prefix);
153 entry = newEntry.get();
Junxiao Shi7f358432016-08-11 17:06:33 +0000154 nte.setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700155 ++m_nItems;
Junxiao Shie93d6a32014-09-07 16:13:22 -0700156 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700157 }
158
Junxiao Shi838c4f12014-11-03 18:55:24 -0700159 this->changeStrategy(*entry, *oldStrategy, *strategy);
Junxiao Shif15058a2016-12-11 20:15:05 +0000160 if (createdStrategy != nullptr) {
161 entry->setStrategy(std::move(createdStrategy));
162 }
163 else {
164 entry->setStrategy(*strategy);
165 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700166 return true;
167}
168
169void
170StrategyChoice::erase(const Name& prefix)
171{
172 BOOST_ASSERT(prefix.size() > 0);
173
Junxiao Shi811c0102016-08-10 04:12:45 +0000174 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000175 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700176 return;
177 }
178
Junxiao Shiff10da62016-07-13 17:57:43 +0000179 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000180 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700181 return;
182 }
183
184 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700185
186 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700187 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700188
Junxiao Shiff10da62016-07-13 17:57:43 +0000189 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000190 m_nameTree.eraseIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700191 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700192}
193
Junxiao Shi838c4f12014-11-03 18:55:24 -0700194std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700195StrategyChoice::get(const Name& prefix) const
196{
Junxiao Shi811c0102016-08-10 04:12:45 +0000197 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000198 if (nte == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000199 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700200 }
201
Junxiao Shiff10da62016-07-13 17:57:43 +0000202 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000203 if (entry == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000204 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700205 }
206
Junxiao Shiff10da62016-07-13 17:57:43 +0000207 return {true, entry->getStrategy().getName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700208}
209
Junxiao Shidd8f6612016-08-12 15:42:52 +0000210template<typename K>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700211Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000212StrategyChoice::findEffectiveStrategyImpl(const K& key) const
Junxiao Shibb5105f2014-03-03 12:06:45 -0700213{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000214 const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000215 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700216 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700217}
218
219Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000220StrategyChoice::findEffectiveStrategy(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +0800221{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000222 return this->findEffectiveStrategyImpl(prefix);
HangZhangcb4fc832014-03-11 16:57:11 +0800223}
224
225Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700226StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
227{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000228 return this->findEffectiveStrategyImpl(pitEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700229}
230
Junxiao Shi7bb01512014-03-05 21:34:09 -0700231Strategy&
232StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
233{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000234 return this->findEffectiveStrategyImpl(measurementsEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700235}
236
Junxiao Shi838c4f12014-11-03 18:55:24 -0700237static inline void
238clearStrategyInfo(const name_tree::Entry& nte)
Junxiao Shie349ea12014-03-12 01:32:42 -0700239{
Junxiao Shie3cf2852016-08-09 03:50:56 +0000240 NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
Junxiao Shi838c4f12014-11-03 18:55:24 -0700241
242 for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
243 pitEntry->clearStrategyInfo();
244 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
245 const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
246 }
247 for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
248 const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
249 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700250 }
Junxiao Shib184e532016-05-26 18:09:57 +0000251 if (nte.getMeasurementsEntry() != nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700252 nte.getMeasurementsEntry()->clearStrategyInfo();
Junxiao Shie349ea12014-03-12 01:32:42 -0700253 }
254}
255
Junxiao Shibb5105f2014-03-03 12:06:45 -0700256void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000257StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700258{
Junxiao Shi838c4f12014-11-03 18:55:24 -0700259 if (&oldStrategy == &newStrategy) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700260 return;
261 }
262
Junxiao Shi838c4f12014-11-03 18:55:24 -0700263 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ")"
264 << " from " << oldStrategy.getName()
265 << " to " << newStrategy.getName());
266
267 // reset StrategyInfo on a portion of NameTree,
268 // where entry's effective strategy is covered by the changing StrategyChoice entry
Junxiao Shi7f358432016-08-11 17:06:33 +0000269 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000270 BOOST_ASSERT(rootNte != nullptr);
Junxiao Shi60607c72014-11-26 22:40:36 -0700271 auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
Junxiao Shi838c4f12014-11-03 18:55:24 -0700272 [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
273 if (&nte == rootNte) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700274 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700275 }
Junxiao Shib184e532016-05-26 18:09:57 +0000276 if (nte.getStrategyChoiceEntry() != nullptr) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700277 return {false, false};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700278 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700279 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700280 });
Junxiao Shi60607c72014-11-26 22:40:36 -0700281 for (const name_tree::Entry& nte : ntChanged) {
282 clearStrategyInfo(nte);
283 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700284}
285
Junxiao Shib30863e2016-08-15 21:32:01 +0000286StrategyChoice::Range
287StrategyChoice::getRange() const
Junxiao Shib5888d22014-05-26 07:35:22 -0700288{
Junxiao Shib30863e2016-08-15 21:32:01 +0000289 return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
290 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
291 &name_tree::Entry::getStrategyChoiceEntry));
Junxiao Shib5888d22014-05-26 07:35:22 -0700292}
293
Junxiao Shiff10da62016-07-13 17:57:43 +0000294} // namespace strategy_choice
Junxiao Shibb5105f2014-03-03 12:06:45 -0700295} // namespace nfd