blob: 773e906e7d518fea1cdc98606dc06acb7b7e9cd8 [file] [log] [blame]
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehman63ab1bb2015-09-04 17:06:58 -05003 * 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 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
28#include "common.hpp"
29#include "core/logger.hpp"
30#include "core/config-file.hpp"
31
32namespace nfd {
33
34NFD_LOG_INIT("TablesConfigSection");
35
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060036const size_t TablesConfigSection::DEFAULT_CS_MAX_PACKETS = 65536;
37
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060038TablesConfigSection::TablesConfigSection(Cs& cs,
39 Pit& pit,
40 Fib& fib,
41 StrategyChoice& strategyChoice,
Vince Lehman63ab1bb2015-09-04 17:06:58 -050042 Measurements& measurements,
43 NetworkRegionTable& networkRegionTable)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060044 : m_cs(cs)
45 // , m_pit(pit)
46 // , m_fib(fib)
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070047 , m_strategyChoice(strategyChoice)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060048 // , m_measurements(measurements)
Vince Lehman63ab1bb2015-09-04 17:06:58 -050049 , m_networkRegionTable(networkRegionTable)
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060050 , m_areTablesConfigured(false)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060051{
52
53}
54
55void
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060056TablesConfigSection::setConfigFile(ConfigFile& configFile)
57{
58 configFile.addSectionHandler("tables",
Vince Lehman63ab1bb2015-09-04 17:06:58 -050059 bind(&TablesConfigSection::processConfig, this, _1, _2, _3));
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060060}
61
62
63void
64TablesConfigSection::ensureTablesAreConfigured()
65{
Vince Lehman63ab1bb2015-09-04 17:06:58 -050066 if (m_areTablesConfigured) {
67 return;
68 }
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060069
70 NFD_LOG_INFO("Setting CS max packets to " << DEFAULT_CS_MAX_PACKETS);
71 m_cs.setLimit(DEFAULT_CS_MAX_PACKETS);
Steve DiBenedettofbea5902014-07-08 15:29:12 -060072
73 m_areTablesConfigured = true;
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060074}
75
76void
Vince Lehman63ab1bb2015-09-04 17:06:58 -050077TablesConfigSection::processConfig(const ConfigSection& configSection,
78 bool isDryRun,
79 const std::string& filename)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060080{
81 // tables
82 // {
83 // cs_max_packets 65536
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070084 //
85 // strategy_choice
86 // {
Vince Lehman63ab1bb2015-09-04 17:06:58 -050087 // / /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 DiBenedettoc0640f52014-11-03 15:55:43 -070097 // }
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060098 // }
99
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600100 size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
101
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600102 boost::optional<const ConfigSection&> csMaxPacketsNode =
103 configSection.get_child_optional("cs_max_packets");
104
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500105 if (csMaxPacketsNode) {
106 boost::optional<size_t> valCsMaxPackets =
107 configSection.get_optional<size_t>("cs_max_packets");
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600108
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500109 if (!valCsMaxPackets) {
110 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"cs_max_packets\""
111 " in \"tables\" section"));
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600112 }
113
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500114 nCsMaxPackets = *valCsMaxPackets;
115 }
116
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700117 boost::optional<const ConfigSection&> strategyChoiceSection =
118 configSection.get_child_optional("strategy_choice");
119
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500120 if (strategyChoiceSection) {
121 processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
122 }
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700123
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500124 boost::optional<const ConfigSection&> networkRegionSection =
125 configSection.get_child_optional("network_region");
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600126
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500127 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 DiBenedetto3a4f83d2014-06-02 14:58:54 -0600137}
138
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700139void
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500140TablesConfigSection::processStrategyChoiceSection(const ConfigSection& configSection,
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700141 bool isDryRun)
142{
143 // strategy_choice
144 // {
145 // / /localhost/nfd/strategy/best-route
Junxiao Shi67ba8d22015-08-21 21:21:28 -0700146 // /localhost /localhost/nfd/strategy/multicast
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700147 // /localhost/nfd /localhost/nfd/strategy/best-route
Junxiao Shi67ba8d22015-08-21 21:21:28 -0700148 // /ndn/broadcast /localhost/nfd/strategy/multicast
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700149 // }
150
151 std::map<Name, Name> choices;
152
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500153 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 DiBenedettoc0640f52014-11-03 15:55:43 -0700159 }
160
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500161 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 DiBenedettoc0640f52014-11-03 15:55:43 -0700166 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500167
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 DiBenedettoc0640f52014-11-03 15:55:43 -0700188}
189
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500190void
191TablesConfigSection::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 DiBenedettoc0640f52014-11-03 15:55:43 -0700212
213
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600214} // namespace nfd