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