blob: 8422a5b94bfc9097c750707acf932ff2431f5792 [file] [log] [blame]
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi9f5b01d2016-08-05 03:54:28 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Vince Lehman63ab1bb2015-09-04 17:06:58 -05004 * 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 DiBenedetto3a4f83d2014-06-02 14:58:54 -060010 *
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 DiBenedetto3a4f83d2014-06-02 14:58:54 -060028#include "core/logger.hpp"
29#include "core/config-file.hpp"
30
31namespace nfd {
32
33NFD_LOG_INIT("TablesConfigSection");
34
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060035const size_t TablesConfigSection::DEFAULT_CS_MAX_PACKETS = 65536;
36
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060037TablesConfigSection::TablesConfigSection(Cs& cs,
38 Pit& pit,
39 Fib& fib,
40 StrategyChoice& strategyChoice,
Vince Lehman63ab1bb2015-09-04 17:06:58 -050041 Measurements& measurements,
42 NetworkRegionTable& networkRegionTable)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060043 : m_cs(cs)
44 // , m_pit(pit)
45 // , m_fib(fib)
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070046 , m_strategyChoice(strategyChoice)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060047 // , m_measurements(measurements)
Vince Lehman63ab1bb2015-09-04 17:06:58 -050048 , m_networkRegionTable(networkRegionTable)
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060049 , m_areTablesConfigured(false)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060050{
51
52}
53
54void
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060055TablesConfigSection::setConfigFile(ConfigFile& configFile)
56{
57 configFile.addSectionHandler("tables",
Vince Lehman63ab1bb2015-09-04 17:06:58 -050058 bind(&TablesConfigSection::processConfig, this, _1, _2, _3));
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060059}
60
61
62void
63TablesConfigSection::ensureTablesAreConfigured()
64{
Vince Lehman63ab1bb2015-09-04 17:06:58 -050065 if (m_areTablesConfigured) {
66 return;
67 }
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060068
69 NFD_LOG_INFO("Setting CS max packets to " << DEFAULT_CS_MAX_PACKETS);
70 m_cs.setLimit(DEFAULT_CS_MAX_PACKETS);
Steve DiBenedettofbea5902014-07-08 15:29:12 -060071
72 m_areTablesConfigured = true;
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060073}
74
75void
Vince Lehman63ab1bb2015-09-04 17:06:58 -050076TablesConfigSection::processConfig(const ConfigSection& configSection,
77 bool isDryRun,
78 const std::string& filename)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060079{
80 // tables
81 // {
82 // cs_max_packets 65536
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070083 //
84 // strategy_choice
85 // {
Vince Lehman63ab1bb2015-09-04 17:06:58 -050086 // / /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 DiBenedettoc0640f52014-11-03 15:55:43 -070096 // }
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060097 // }
98
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060099 size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
100
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600101 boost::optional<const ConfigSection&> csMaxPacketsNode =
102 configSection.get_child_optional("cs_max_packets");
103
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500104 if (csMaxPacketsNode) {
105 boost::optional<size_t> valCsMaxPackets =
106 configSection.get_optional<size_t>("cs_max_packets");
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600107
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500108 if (!valCsMaxPackets) {
109 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"cs_max_packets\""
110 " in \"tables\" section"));
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600111 }
112
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500113 nCsMaxPackets = *valCsMaxPackets;
114 }
115
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700116 boost::optional<const ConfigSection&> strategyChoiceSection =
117 configSection.get_child_optional("strategy_choice");
118
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500119 if (strategyChoiceSection) {
120 processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
121 }
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700122
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500123 boost::optional<const ConfigSection&> networkRegionSection =
124 configSection.get_child_optional("network_region");
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600125
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500126 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 DiBenedetto3a4f83d2014-06-02 14:58:54 -0600136}
137
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700138void
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500139TablesConfigSection::processStrategyChoiceSection(const ConfigSection& configSection,
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700140 bool isDryRun)
141{
142 // strategy_choice
143 // {
144 // / /localhost/nfd/strategy/best-route
Junxiao Shi67ba8d22015-08-21 21:21:28 -0700145 // /localhost /localhost/nfd/strategy/multicast
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700146 // /localhost/nfd /localhost/nfd/strategy/best-route
Junxiao Shi67ba8d22015-08-21 21:21:28 -0700147 // /ndn/broadcast /localhost/nfd/strategy/multicast
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700148 // }
149
150 std::map<Name, Name> choices;
151
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500152 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 DiBenedettoc0640f52014-11-03 15:55:43 -0700158 }
159
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500160 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 DiBenedettoc0640f52014-11-03 15:55:43 -0700165 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500166
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 DiBenedettoc0640f52014-11-03 15:55:43 -0700187}
188
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500189void
190TablesConfigSection::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 DiBenedettoc0640f52014-11-03 15:55:43 -0700211
212
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600213} // namespace nfd