blob: 06acaaec3dfdc10efb94f4b946ebbf0a13ee8804 [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"
Junxiao Shi3aba7542016-12-24 02:42:52 +000029#include "core/asserts.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060030#include "core/logger.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070031#include "fw/strategy.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070032
33namespace nfd {
Junxiao Shiff10da62016-07-13 17:57:43 +000034namespace strategy_choice {
Junxiao Shibb5105f2014-03-03 12:06:45 -070035
Junxiao Shibb5105f2014-03-03 12:06:45 -070036using fw::Strategy;
37
Junxiao Shi3aba7542016-12-24 02:42:52 +000038NFD_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator);
39
Junxiao Shibb5105f2014-03-03 12:06:45 -070040NFD_LOG_INIT("StrategyChoice");
41
Junxiao Shi811c0102016-08-10 04:12:45 +000042static inline bool
43nteHasStrategyChoiceEntry(const name_tree::Entry& nte)
44{
45 return nte.getStrategyChoiceEntry() != nullptr;
46}
47
Junxiao Shic34d1672016-12-09 15:57:59 +000048StrategyChoice::StrategyChoice(Forwarder& forwarder)
49 : m_forwarder(forwarder)
50 , m_nameTree(m_forwarder.getNameTree())
Junxiao Shib5888d22014-05-26 07:35:22 -070051 , m_nItems(0)
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));
60 NFD_LOG_INFO("setDefaultStrategy " << entry->getStrategyName());
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
69bool
Junxiao Shie93d6a32014-09-07 16:13:22 -070070StrategyChoice::hasStrategy(const Name& strategyName, bool isExact) const
Junxiao Shibb5105f2014-03-03 12:06:45 -070071{
Junxiao Shie93d6a32014-09-07 16:13:22 -070072 if (isExact) {
73 return m_strategyInstances.count(strategyName) > 0;
74 }
75 else {
Junxiao Shif15058a2016-12-11 20:15:05 +000076 return Strategy::canCreate(strategyName) ||
77 this->getStrategy(strategyName) != nullptr;
Junxiao Shie93d6a32014-09-07 16:13:22 -070078 }
Junxiao Shibb5105f2014-03-03 12:06:45 -070079}
80
Junxiao Shia49a1ab2016-07-15 18:24:36 +000081std::pair<bool, Strategy*>
82StrategyChoice::install(unique_ptr<Strategy> strategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -070083{
Junxiao Shif15058a2016-12-11 20:15:05 +000084 /// \todo #3868
85 /// Strategy instance should always be created from Strategy::create;
86 /// m_strategyInstances, install, getStrategy, hasStrategy should be eliminated.
87
Junxiao Shib184e532016-05-26 18:09:57 +000088 BOOST_ASSERT(strategy != nullptr);
Junxiao Shia49a1ab2016-07-15 18:24:36 +000089 Name strategyName = strategy->getName();
90 // copying Name, so that strategyName remains available even if strategy is deallocated
Junxiao Shibb5105f2014-03-03 12:06:45 -070091
Junxiao Shia49a1ab2016-07-15 18:24:36 +000092 bool isInserted = false;
93 StrategyInstanceTable::iterator it;
94 std::tie(it, isInserted) = m_strategyInstances.emplace(strategyName, std::move(strategy));
95
96 if (!isInserted) {
Junxiao Shibb5105f2014-03-03 12:06:45 -070097 NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName");
Junxiao Shibb5105f2014-03-03 12:06:45 -070098 }
Junxiao Shia49a1ab2016-07-15 18:24:36 +000099 return std::make_pair(isInserted, it->second.get());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700100}
101
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000102Strategy*
Junxiao Shie93d6a32014-09-07 16:13:22 -0700103StrategyChoice::getStrategy(const Name& strategyName) const
104{
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000105 Strategy* candidate = nullptr;
Junxiao Shi838c4f12014-11-03 18:55:24 -0700106 for (auto it = m_strategyInstances.lower_bound(strategyName);
Junxiao Shie93d6a32014-09-07 16:13:22 -0700107 it != m_strategyInstances.end() && strategyName.isPrefixOf(it->first); ++it) {
108 switch (it->first.size() - strategyName.size()) {
109 case 0: // exact match
Junxiao Shi838c4f12014-11-03 18:55:24 -0700110 return it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700111 case 1: // unversioned strategyName matches versioned strategy
Junxiao Shi838c4f12014-11-03 18:55:24 -0700112 candidate = it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700113 break;
114 }
115 }
116 return candidate;
117}
118
Junxiao Shibb5105f2014-03-03 12:06:45 -0700119bool
120StrategyChoice::insert(const Name& prefix, const Name& strategyName)
121{
Junxiao Shi18739c42016-12-22 08:03:00 +0000122 unique_ptr<Strategy> createdStrategy;
123 try {
124 createdStrategy = Strategy::create(strategyName, m_forwarder);
125 }
126 catch (const std::invalid_argument& e) {
127 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
128 return false;
129 }
130
Junxiao Shif15058a2016-12-11 20:15:05 +0000131 Strategy* strategy = createdStrategy.get();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700132 if (strategy == nullptr) {
Junxiao Shif15058a2016-12-11 20:15:05 +0000133 strategy = this->getStrategy(strategyName);
134 if (strategy == nullptr) {
135 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered or installed");
136 return false;
137 }
138 NFD_LOG_WARN("insert(" << prefix << "," << strategyName << ") using shared strategy instance");
Junxiao Shibb5105f2014-03-03 12:06:45 -0700139 }
140
Junxiao Shi7f358432016-08-11 17:06:33 +0000141 name_tree::Entry& nte = m_nameTree.lookup(prefix);
142 Entry* entry = nte.getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700143 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +0000144 if (entry != nullptr) {
Junxiao Shif15058a2016-12-11 20:15:05 +0000145 if (entry->getStrategyName() == strategy->getName()) {
Junxiao Shie93d6a32014-09-07 16:13:22 -0700146 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getName());
147 return true;
148 }
Junxiao Shi838c4f12014-11-03 18:55:24 -0700149 oldStrategy = &entry->getStrategy();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700150 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getName() <<
151 " to " << strategy->getName());
152 }
Junxiao Shif15058a2016-12-11 20:15:05 +0000153 else {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700154 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000155 auto newEntry = make_unique<Entry>(prefix);
156 entry = newEntry.get();
Junxiao Shi7f358432016-08-11 17:06:33 +0000157 nte.setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700158 ++m_nItems;
Junxiao Shie93d6a32014-09-07 16:13:22 -0700159 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700160 }
161
Junxiao Shi838c4f12014-11-03 18:55:24 -0700162 this->changeStrategy(*entry, *oldStrategy, *strategy);
Junxiao Shif15058a2016-12-11 20:15:05 +0000163 if (createdStrategy != nullptr) {
164 entry->setStrategy(std::move(createdStrategy));
165 }
166 else {
167 entry->setStrategy(*strategy);
168 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700169 return true;
170}
171
172void
173StrategyChoice::erase(const Name& prefix)
174{
175 BOOST_ASSERT(prefix.size() > 0);
176
Junxiao Shi811c0102016-08-10 04:12:45 +0000177 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000178 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700179 return;
180 }
181
Junxiao Shiff10da62016-07-13 17:57:43 +0000182 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000183 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700184 return;
185 }
186
187 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700188
189 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700190 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700191
Junxiao Shiff10da62016-07-13 17:57:43 +0000192 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000193 m_nameTree.eraseIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700194 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700195}
196
Junxiao Shi838c4f12014-11-03 18:55:24 -0700197std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700198StrategyChoice::get(const Name& prefix) const
199{
Junxiao Shi811c0102016-08-10 04:12:45 +0000200 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000201 if (nte == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000202 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700203 }
204
Junxiao Shiff10da62016-07-13 17:57:43 +0000205 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000206 if (entry == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000207 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700208 }
209
Junxiao Shiff10da62016-07-13 17:57:43 +0000210 return {true, entry->getStrategy().getName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700211}
212
Junxiao Shidd8f6612016-08-12 15:42:52 +0000213template<typename K>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700214Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000215StrategyChoice::findEffectiveStrategyImpl(const K& key) const
Junxiao Shibb5105f2014-03-03 12:06:45 -0700216{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000217 const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000218 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700219 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700220}
221
222Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000223StrategyChoice::findEffectiveStrategy(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +0800224{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000225 return this->findEffectiveStrategyImpl(prefix);
HangZhangcb4fc832014-03-11 16:57:11 +0800226}
227
228Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700229StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
230{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000231 return this->findEffectiveStrategyImpl(pitEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700232}
233
Junxiao Shi7bb01512014-03-05 21:34:09 -0700234Strategy&
235StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
236{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000237 return this->findEffectiveStrategyImpl(measurementsEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700238}
239
Junxiao Shi838c4f12014-11-03 18:55:24 -0700240static inline void
241clearStrategyInfo(const name_tree::Entry& nte)
Junxiao Shie349ea12014-03-12 01:32:42 -0700242{
Junxiao Shie3cf2852016-08-09 03:50:56 +0000243 NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
Junxiao Shi838c4f12014-11-03 18:55:24 -0700244
245 for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
246 pitEntry->clearStrategyInfo();
247 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
248 const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
249 }
250 for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
251 const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
252 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700253 }
Junxiao Shib184e532016-05-26 18:09:57 +0000254 if (nte.getMeasurementsEntry() != nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700255 nte.getMeasurementsEntry()->clearStrategyInfo();
Junxiao Shie349ea12014-03-12 01:32:42 -0700256 }
257}
258
Junxiao Shibb5105f2014-03-03 12:06:45 -0700259void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000260StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700261{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000262 ///\todo #3868 don't clear StrategyInfo if only parameter differs
Junxiao Shi838c4f12014-11-03 18:55:24 -0700263 if (&oldStrategy == &newStrategy) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700264 return;
265 }
266
Junxiao Shi838c4f12014-11-03 18:55:24 -0700267 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ")"
268 << " from " << oldStrategy.getName()
269 << " to " << newStrategy.getName());
270
271 // reset StrategyInfo on a portion of NameTree,
272 // where entry's effective strategy is covered by the changing StrategyChoice entry
Junxiao Shi7f358432016-08-11 17:06:33 +0000273 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000274 BOOST_ASSERT(rootNte != nullptr);
Junxiao Shi60607c72014-11-26 22:40:36 -0700275 auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
Junxiao Shi838c4f12014-11-03 18:55:24 -0700276 [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
277 if (&nte == rootNte) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700278 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700279 }
Junxiao Shib184e532016-05-26 18:09:57 +0000280 if (nte.getStrategyChoiceEntry() != nullptr) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700281 return {false, false};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700282 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700283 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700284 });
Junxiao Shi60607c72014-11-26 22:40:36 -0700285 for (const name_tree::Entry& nte : ntChanged) {
286 clearStrategyInfo(nte);
287 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700288}
289
Junxiao Shib30863e2016-08-15 21:32:01 +0000290StrategyChoice::Range
291StrategyChoice::getRange() const
Junxiao Shib5888d22014-05-26 07:35:22 -0700292{
Junxiao Shib30863e2016-08-15 21:32:01 +0000293 return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
294 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
295 &name_tree::Entry::getStrategyChoiceEntry));
Junxiao Shib5888d22014-05-26 07:35:22 -0700296}
297
Junxiao Shiff10da62016-07-13 17:57:43 +0000298} // namespace strategy_choice
Junxiao Shibb5105f2014-03-03 12:06:45 -0700299} // namespace nfd