blob: 59ac4638711f984512f5c7477491fd143e94fd43 [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"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070028#include "fw/strategy.hpp"
29#include "pit-entry.hpp"
Junxiao Shi7bb01512014-03-05 21:34:09 -070030#include "measurements-entry.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 Shia49a1ab2016-07-15 18:24:36 +000045StrategyChoice::StrategyChoice(NameTree& nameTree, unique_ptr<Strategy> defaultStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -070046 : m_nameTree(nameTree)
Junxiao Shib5888d22014-05-26 07:35:22 -070047 , m_nItems(0)
Junxiao Shibb5105f2014-03-03 12:06:45 -070048{
Junxiao Shia49a1ab2016-07-15 18:24:36 +000049 this->setDefaultStrategy(std::move(defaultStrategy));
Junxiao Shibb5105f2014-03-03 12:06:45 -070050}
51
52bool
Junxiao Shie93d6a32014-09-07 16:13:22 -070053StrategyChoice::hasStrategy(const Name& strategyName, bool isExact) const
Junxiao Shibb5105f2014-03-03 12:06:45 -070054{
Junxiao Shie93d6a32014-09-07 16:13:22 -070055 if (isExact) {
56 return m_strategyInstances.count(strategyName) > 0;
57 }
58 else {
Junxiao Shib184e532016-05-26 18:09:57 +000059 return this->getStrategy(strategyName) != nullptr;
Junxiao Shie93d6a32014-09-07 16:13:22 -070060 }
Junxiao Shibb5105f2014-03-03 12:06:45 -070061}
62
Junxiao Shia49a1ab2016-07-15 18:24:36 +000063std::pair<bool, Strategy*>
64StrategyChoice::install(unique_ptr<Strategy> strategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -070065{
Junxiao Shib184e532016-05-26 18:09:57 +000066 BOOST_ASSERT(strategy != nullptr);
Junxiao Shia49a1ab2016-07-15 18:24:36 +000067 Name strategyName = strategy->getName();
68 // copying Name, so that strategyName remains available even if strategy is deallocated
Junxiao Shibb5105f2014-03-03 12:06:45 -070069
Junxiao Shia49a1ab2016-07-15 18:24:36 +000070 bool isInserted = false;
71 StrategyInstanceTable::iterator it;
72 std::tie(it, isInserted) = m_strategyInstances.emplace(strategyName, std::move(strategy));
73
74 if (!isInserted) {
Junxiao Shibb5105f2014-03-03 12:06:45 -070075 NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName");
Junxiao Shibb5105f2014-03-03 12:06:45 -070076 }
Junxiao Shia49a1ab2016-07-15 18:24:36 +000077 return std::make_pair(isInserted, it->second.get());
Junxiao Shibb5105f2014-03-03 12:06:45 -070078}
79
Junxiao Shia49a1ab2016-07-15 18:24:36 +000080Strategy*
Junxiao Shie93d6a32014-09-07 16:13:22 -070081StrategyChoice::getStrategy(const Name& strategyName) const
82{
Junxiao Shia49a1ab2016-07-15 18:24:36 +000083 Strategy* candidate = nullptr;
Junxiao Shi838c4f12014-11-03 18:55:24 -070084 for (auto it = m_strategyInstances.lower_bound(strategyName);
Junxiao Shie93d6a32014-09-07 16:13:22 -070085 it != m_strategyInstances.end() && strategyName.isPrefixOf(it->first); ++it) {
86 switch (it->first.size() - strategyName.size()) {
87 case 0: // exact match
Junxiao Shi838c4f12014-11-03 18:55:24 -070088 return it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -070089 case 1: // unversioned strategyName matches versioned strategy
Junxiao Shi838c4f12014-11-03 18:55:24 -070090 candidate = it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -070091 break;
92 }
93 }
94 return candidate;
95}
96
Junxiao Shibb5105f2014-03-03 12:06:45 -070097bool
98StrategyChoice::insert(const Name& prefix, const Name& strategyName)
99{
Junxiao Shi838c4f12014-11-03 18:55:24 -0700100 Strategy* strategy = this->getStrategy(strategyName);
101 if (strategy == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700102 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed");
103 return false;
104 }
105
Junxiao Shi838c4f12014-11-03 18:55:24 -0700106 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000107 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700108 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +0000109 if (entry != nullptr) {
Junxiao Shie93d6a32014-09-07 16:13:22 -0700110 if (entry->getStrategy().getName() == strategy->getName()) {
111 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getName());
112 return true;
113 }
Junxiao Shi838c4f12014-11-03 18:55:24 -0700114 oldStrategy = &entry->getStrategy();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700115 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getName() <<
116 " to " << strategy->getName());
117 }
118
Junxiao Shib184e532016-05-26 18:09:57 +0000119 if (entry == nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700120 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000121 auto newEntry = make_unique<Entry>(prefix);
122 entry = newEntry.get();
123 nte->setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700124 ++m_nItems;
Junxiao Shie93d6a32014-09-07 16:13:22 -0700125 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700126 }
127
Junxiao Shi838c4f12014-11-03 18:55:24 -0700128 this->changeStrategy(*entry, *oldStrategy, *strategy);
129 entry->setStrategy(*strategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700130 return true;
131}
132
133void
134StrategyChoice::erase(const Name& prefix)
135{
136 BOOST_ASSERT(prefix.size() > 0);
137
Junxiao Shi811c0102016-08-10 04:12:45 +0000138 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000139 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700140 return;
141 }
142
Junxiao Shiff10da62016-07-13 17:57:43 +0000143 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000144 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700145 return;
146 }
147
148 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700149
150 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700151 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700152
Junxiao Shiff10da62016-07-13 17:57:43 +0000153 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000154 m_nameTree.eraseIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700155 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700156}
157
Junxiao Shi838c4f12014-11-03 18:55:24 -0700158std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700159StrategyChoice::get(const Name& prefix) const
160{
Junxiao Shi811c0102016-08-10 04:12:45 +0000161 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000162 if (nte == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000163 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700164 }
165
Junxiao Shiff10da62016-07-13 17:57:43 +0000166 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000167 if (entry == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000168 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700169 }
170
Junxiao Shiff10da62016-07-13 17:57:43 +0000171 return {true, entry->getStrategy().getName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700172}
173
174Strategy&
175StrategyChoice::findEffectiveStrategy(const Name& prefix) const
176{
Junxiao Shi811c0102016-08-10 04:12:45 +0000177 name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(prefix, &nteHasStrategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000178 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700179 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700180}
181
182Strategy&
Junxiao Shif2420fc2016-08-11 13:18:21 +0000183StrategyChoice::findEffectiveStrategy(const name_tree::Entry& nte) const
HangZhangcb4fc832014-03-11 16:57:11 +0800184{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000185 Entry* entry = nte.getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000186 if (entry != nullptr)
HangZhangcb4fc832014-03-11 16:57:11 +0800187 return entry->getStrategy();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700188
Junxiao Shif2420fc2016-08-11 13:18:21 +0000189 const name_tree::Entry* nte2 = m_nameTree.findLongestPrefixMatch(nte, &nteHasStrategyChoiceEntry);
190 BOOST_ASSERT(nte2 != nullptr);
191 return nte2->getStrategyChoiceEntry()->getStrategy();
HangZhangcb4fc832014-03-11 16:57:11 +0800192}
193
194Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700195StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
196{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000197 const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(pitEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000198 BOOST_ASSERT(nte != nullptr);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000199 return this->findEffectiveStrategy(*nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700200}
201
Junxiao Shi7bb01512014-03-05 21:34:09 -0700202Strategy&
203StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
204{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000205 shared_ptr<name_tree::Entry> nte = m_nameTree.getEntry(measurementsEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000206 BOOST_ASSERT(nte != nullptr);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000207 return this->findEffectiveStrategy(*nte);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700208}
209
Junxiao Shibb5105f2014-03-03 12:06:45 -0700210void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000211StrategyChoice::setDefaultStrategy(unique_ptr<Strategy> strategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700212{
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000213 bool isInstalled = false;
214 Strategy* instance = nullptr;
215 std::tie(isInstalled, instance) = this->install(std::move(strategy));
216 BOOST_ASSERT(isInstalled);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700217
Junxiao Shiff10da62016-07-13 17:57:43 +0000218 auto entry = make_unique<Entry>(Name());
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000219 entry->setStrategy(*instance);
Junxiao Shiff10da62016-07-13 17:57:43 +0000220
Junxiao Shibb5105f2014-03-03 12:06:45 -0700221 // don't use .insert here, because it will invoke findEffectiveStrategy
222 // which expects an existing root entry
Junxiao Shi838c4f12014-11-03 18:55:24 -0700223 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(Name());
Junxiao Shiff10da62016-07-13 17:57:43 +0000224 nte->setStrategyChoiceEntry(std::move(entry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700225 ++m_nItems;
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000226 NFD_LOG_INFO("setDefaultStrategy " << instance->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -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 Shif2420fc2016-08-11 13:18:21 +0000261 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry).get();
262 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 Shib5888d22014-05-26 07:35:22 -0700278StrategyChoice::const_iterator
279StrategyChoice::begin() const
280{
Junxiao Shi811c0102016-08-10 04:12:45 +0000281 return const_iterator(m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry).begin());
Junxiao Shib5888d22014-05-26 07:35:22 -0700282}
283
Junxiao Shiff10da62016-07-13 17:57:43 +0000284} // namespace strategy_choice
Junxiao Shibb5105f2014-03-03 12:06:45 -0700285} // namespace nfd