blob: 7e2f5bfe394be42f3e181a18a20e4b7d1ad3e2c3 [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));
Junxiao Shi4cb74312016-12-25 20:48:47 +000060 NFD_LOG_INFO("setDefaultStrategy " << entry->getStrategyInstanceName());
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 Shibb5105f2014-03-03 12:06:45 -070070StrategyChoice::insert(const Name& prefix, const Name& strategyName)
71{
Junxiao Shi7f566dd2016-12-27 02:28:31 +000072 unique_ptr<Strategy> strategy;
Junxiao Shi18739c42016-12-22 08:03:00 +000073 try {
Junxiao Shi7f566dd2016-12-27 02:28:31 +000074 strategy = Strategy::create(strategyName, m_forwarder);
Junxiao Shi18739c42016-12-22 08:03:00 +000075 }
76 catch (const std::invalid_argument& e) {
77 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
78 return false;
79 }
80
Junxiao Shi838c4f12014-11-03 18:55:24 -070081 if (strategy == nullptr) {
Junxiao Shi7f566dd2016-12-27 02:28:31 +000082 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered");
83 return false;
Junxiao Shibb5105f2014-03-03 12:06:45 -070084 }
85
Junxiao Shi7f358432016-08-11 17:06:33 +000086 name_tree::Entry& nte = m_nameTree.lookup(prefix);
87 Entry* entry = nte.getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -070088 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +000089 if (entry != nullptr) {
Junxiao Shi4cb74312016-12-25 20:48:47 +000090 if (entry->getStrategyInstanceName() == strategy->getInstanceName()) {
91 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getInstanceName());
Junxiao Shie93d6a32014-09-07 16:13:22 -070092 return true;
93 }
Junxiao Shi838c4f12014-11-03 18:55:24 -070094 oldStrategy = &entry->getStrategy();
Junxiao Shi4cb74312016-12-25 20:48:47 +000095 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getInstanceName() <<
96 " to " << strategy->getInstanceName());
Junxiao Shie93d6a32014-09-07 16:13:22 -070097 }
Junxiao Shif15058a2016-12-11 20:15:05 +000098 else {
Junxiao Shi838c4f12014-11-03 18:55:24 -070099 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000100 auto newEntry = make_unique<Entry>(prefix);
101 entry = newEntry.get();
Junxiao Shi7f358432016-08-11 17:06:33 +0000102 nte.setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700103 ++m_nItems;
Junxiao Shi4cb74312016-12-25 20:48:47 +0000104 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getInstanceName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700105 }
106
Junxiao Shi838c4f12014-11-03 18:55:24 -0700107 this->changeStrategy(*entry, *oldStrategy, *strategy);
Junxiao Shi7f566dd2016-12-27 02:28:31 +0000108 entry->setStrategy(std::move(strategy));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700109 return true;
110}
111
112void
113StrategyChoice::erase(const Name& prefix)
114{
115 BOOST_ASSERT(prefix.size() > 0);
116
Junxiao Shi811c0102016-08-10 04:12:45 +0000117 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000118 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700119 return;
120 }
121
Junxiao Shiff10da62016-07-13 17:57:43 +0000122 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000123 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700124 return;
125 }
126
127 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700128
129 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700130 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700131
Junxiao Shiff10da62016-07-13 17:57:43 +0000132 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000133 m_nameTree.eraseIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700134 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700135}
136
Junxiao Shi838c4f12014-11-03 18:55:24 -0700137std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700138StrategyChoice::get(const Name& prefix) const
139{
Junxiao Shi811c0102016-08-10 04:12:45 +0000140 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000141 if (nte == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000142 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700143 }
144
Junxiao Shiff10da62016-07-13 17:57:43 +0000145 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000146 if (entry == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000147 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700148 }
149
Junxiao Shi4cb74312016-12-25 20:48:47 +0000150 return {true, entry->getStrategyInstanceName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700151}
152
Junxiao Shidd8f6612016-08-12 15:42:52 +0000153template<typename K>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700154Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000155StrategyChoice::findEffectiveStrategyImpl(const K& key) const
Junxiao Shibb5105f2014-03-03 12:06:45 -0700156{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000157 const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000158 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700159 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700160}
161
162Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000163StrategyChoice::findEffectiveStrategy(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +0800164{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000165 return this->findEffectiveStrategyImpl(prefix);
HangZhangcb4fc832014-03-11 16:57:11 +0800166}
167
168Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700169StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
170{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000171 return this->findEffectiveStrategyImpl(pitEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700172}
173
Junxiao Shi7bb01512014-03-05 21:34:09 -0700174Strategy&
175StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
176{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000177 return this->findEffectiveStrategyImpl(measurementsEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700178}
179
Junxiao Shi838c4f12014-11-03 18:55:24 -0700180static inline void
181clearStrategyInfo(const name_tree::Entry& nte)
Junxiao Shie349ea12014-03-12 01:32:42 -0700182{
Junxiao Shie3cf2852016-08-09 03:50:56 +0000183 NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
Junxiao Shi838c4f12014-11-03 18:55:24 -0700184
185 for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
186 pitEntry->clearStrategyInfo();
187 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
188 const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
189 }
190 for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
191 const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
192 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700193 }
Junxiao Shib184e532016-05-26 18:09:57 +0000194 if (nte.getMeasurementsEntry() != nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700195 nte.getMeasurementsEntry()->clearStrategyInfo();
Junxiao Shie349ea12014-03-12 01:32:42 -0700196 }
197}
198
Junxiao Shibb5105f2014-03-03 12:06:45 -0700199void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000200StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700201{
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000202 ///\todo #3868 don't clear StrategyInfo if only parameter differs
Junxiao Shi838c4f12014-11-03 18:55:24 -0700203 if (&oldStrategy == &newStrategy) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700204 return;
205 }
206
Junxiao Shi838c4f12014-11-03 18:55:24 -0700207 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ")"
Junxiao Shi4cb74312016-12-25 20:48:47 +0000208 << " from " << oldStrategy.getInstanceName()
209 << " to " << newStrategy.getInstanceName());
Junxiao Shi838c4f12014-11-03 18:55:24 -0700210
211 // reset StrategyInfo on a portion of NameTree,
212 // where entry's effective strategy is covered by the changing StrategyChoice entry
Junxiao Shi7f358432016-08-11 17:06:33 +0000213 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000214 BOOST_ASSERT(rootNte != nullptr);
Junxiao Shi60607c72014-11-26 22:40:36 -0700215 auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
Junxiao Shi838c4f12014-11-03 18:55:24 -0700216 [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
217 if (&nte == rootNte) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700218 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700219 }
Junxiao Shib184e532016-05-26 18:09:57 +0000220 if (nte.getStrategyChoiceEntry() != nullptr) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700221 return {false, false};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700222 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700223 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700224 });
Junxiao Shi60607c72014-11-26 22:40:36 -0700225 for (const name_tree::Entry& nte : ntChanged) {
226 clearStrategyInfo(nte);
227 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700228}
229
Junxiao Shib30863e2016-08-15 21:32:01 +0000230StrategyChoice::Range
231StrategyChoice::getRange() const
Junxiao Shib5888d22014-05-26 07:35:22 -0700232{
Junxiao Shib30863e2016-08-15 21:32:01 +0000233 return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
234 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
235 &name_tree::Entry::getStrategyChoiceEntry));
Junxiao Shib5888d22014-05-26 07:35:22 -0700236}
237
Junxiao Shiff10da62016-07-13 17:57:43 +0000238} // namespace strategy_choice
Junxiao Shibb5105f2014-03-03 12:06:45 -0700239} // namespace nfd