Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, 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. |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 10 | * |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "tables-config-section.hpp" |
| 27 | |
| 28 | #include "common.hpp" |
| 29 | #include "core/logger.hpp" |
| 30 | #include "core/config-file.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | |
| 34 | NFD_LOG_INIT("TablesConfigSection"); |
| 35 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 36 | const size_t TablesConfigSection::DEFAULT_CS_MAX_PACKETS = 65536; |
| 37 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 38 | TablesConfigSection::TablesConfigSection(Cs& cs, |
| 39 | Pit& pit, |
| 40 | Fib& fib, |
| 41 | StrategyChoice& strategyChoice, |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 42 | Measurements& measurements, |
| 43 | NetworkRegionTable& networkRegionTable) |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 44 | : m_cs(cs) |
| 45 | // , m_pit(pit) |
| 46 | // , m_fib(fib) |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 47 | , m_strategyChoice(strategyChoice) |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 48 | // , m_measurements(measurements) |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 49 | , m_networkRegionTable(networkRegionTable) |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 50 | , m_areTablesConfigured(false) |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 51 | { |
| 52 | |
| 53 | } |
| 54 | |
| 55 | void |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 56 | TablesConfigSection::setConfigFile(ConfigFile& configFile) |
| 57 | { |
| 58 | configFile.addSectionHandler("tables", |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 59 | bind(&TablesConfigSection::processConfig, this, _1, _2, _3)); |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | void |
| 64 | TablesConfigSection::ensureTablesAreConfigured() |
| 65 | { |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 66 | if (m_areTablesConfigured) { |
| 67 | return; |
| 68 | } |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 69 | |
| 70 | NFD_LOG_INFO("Setting CS max packets to " << DEFAULT_CS_MAX_PACKETS); |
| 71 | m_cs.setLimit(DEFAULT_CS_MAX_PACKETS); |
Steve DiBenedetto | fbea590 | 2014-07-08 15:29:12 -0600 | [diff] [blame] | 72 | |
| 73 | m_areTablesConfigured = true; |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 77 | TablesConfigSection::processConfig(const ConfigSection& configSection, |
| 78 | bool isDryRun, |
| 79 | const std::string& filename) |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 80 | { |
| 81 | // tables |
| 82 | // { |
| 83 | // cs_max_packets 65536 |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 84 | // |
| 85 | // strategy_choice |
| 86 | // { |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 87 | // / /localhost/nfd/strategy/best-route |
| 88 | // /localhost /localhost/nfd/strategy/multicast |
| 89 | // /localhost/nfd /localhost/nfd/strategy/best-route |
| 90 | // /ndn/broadcast /localhost/nfd/strategy/multicast |
| 91 | // } |
| 92 | // |
| 93 | // network_region |
| 94 | // { |
| 95 | // /example/region1 |
| 96 | // /example/region2 |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 97 | // } |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 98 | // } |
| 99 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 100 | size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS; |
| 101 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 102 | boost::optional<const ConfigSection&> csMaxPacketsNode = |
| 103 | configSection.get_child_optional("cs_max_packets"); |
| 104 | |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 105 | if (csMaxPacketsNode) { |
| 106 | boost::optional<size_t> valCsMaxPackets = |
| 107 | configSection.get_optional<size_t>("cs_max_packets"); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 108 | |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 109 | if (!valCsMaxPackets) { |
| 110 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"cs_max_packets\"" |
| 111 | " in \"tables\" section")); |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 112 | } |
| 113 | |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 114 | nCsMaxPackets = *valCsMaxPackets; |
| 115 | } |
| 116 | |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 117 | boost::optional<const ConfigSection&> strategyChoiceSection = |
| 118 | configSection.get_child_optional("strategy_choice"); |
| 119 | |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 120 | if (strategyChoiceSection) { |
| 121 | processStrategyChoiceSection(*strategyChoiceSection, isDryRun); |
| 122 | } |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 123 | |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 124 | boost::optional<const ConfigSection&> networkRegionSection = |
| 125 | configSection.get_child_optional("network_region"); |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 126 | |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 127 | if (networkRegionSection) { |
| 128 | processNetworkRegionSection(*networkRegionSection, isDryRun); |
| 129 | } |
| 130 | |
| 131 | if (!isDryRun) { |
| 132 | NFD_LOG_INFO("Setting CS max packets to " << nCsMaxPackets); |
| 133 | |
| 134 | m_cs.setLimit(nCsMaxPackets); |
| 135 | m_areTablesConfigured = true; |
| 136 | } |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 137 | } |
| 138 | |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 139 | void |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 140 | TablesConfigSection::processStrategyChoiceSection(const ConfigSection& configSection, |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 141 | bool isDryRun) |
| 142 | { |
| 143 | // strategy_choice |
| 144 | // { |
| 145 | // / /localhost/nfd/strategy/best-route |
Junxiao Shi | 67ba8d2 | 2015-08-21 21:21:28 -0700 | [diff] [blame] | 146 | // /localhost /localhost/nfd/strategy/multicast |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 147 | // /localhost/nfd /localhost/nfd/strategy/best-route |
Junxiao Shi | 67ba8d2 | 2015-08-21 21:21:28 -0700 | [diff] [blame] | 148 | // /ndn/broadcast /localhost/nfd/strategy/multicast |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 149 | // } |
| 150 | |
| 151 | std::map<Name, Name> choices; |
| 152 | |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 153 | for (const auto& prefixAndStrategy : configSection) { |
| 154 | const Name prefix(prefixAndStrategy.first); |
| 155 | if (choices.find(prefix) != choices.end()) { |
| 156 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate strategy choice for prefix \"" + |
| 157 | prefix.toUri() + "\" in \"strategy_choice\" " |
| 158 | "section")); |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 161 | const std::string strategyString(prefixAndStrategy.second.get_value<std::string>()); |
| 162 | if (strategyString.empty()) { |
| 163 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid strategy choice \"\" for prefix \"" + |
| 164 | prefix.toUri() + "\" in \"strategy_choice\" " |
| 165 | "section")); |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 166 | } |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 167 | |
| 168 | const Name strategyName(strategyString); |
| 169 | if (!m_strategyChoice.hasStrategy(strategyName)) { |
| 170 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid strategy choice \"" + |
| 171 | strategyName.toUri() + "\" for prefix \"" + |
| 172 | prefix.toUri() + "\" in \"strategy_choice\" " |
| 173 | "section")); |
| 174 | } |
| 175 | |
| 176 | choices[prefix] = strategyName; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | for (const auto& prefixAndStrategy : choices) { |
| 181 | if (!isDryRun && !m_strategyChoice.insert(prefixAndStrategy.first, prefixAndStrategy.second)) { |
| 182 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Failed to set strategy \"" + |
| 183 | prefixAndStrategy.second.toUri() + "\" for " |
| 184 | "prefix \"" + prefixAndStrategy.first.toUri() + |
| 185 | "\" in \"strategy_choicev\"")); |
| 186 | } |
| 187 | } |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 190 | void |
| 191 | TablesConfigSection::processNetworkRegionSection(const ConfigSection& configSection, |
| 192 | bool isDryRun) |
| 193 | { |
| 194 | // network_region |
| 195 | // { |
| 196 | // /example/region1 |
| 197 | // /example/region2 |
| 198 | // } |
| 199 | |
| 200 | if (!isDryRun) { |
| 201 | m_networkRegionTable.clear(); |
| 202 | } |
| 203 | |
| 204 | for (const auto& pair : configSection) { |
| 205 | const Name region(pair.first); |
| 206 | |
| 207 | if (!isDryRun) { |
| 208 | m_networkRegionTable.insert(region); |
| 209 | } |
| 210 | } |
| 211 | } |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 212 | |
| 213 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 214 | } // namespace nfd |