blob: c7661ce9542bcb09f0d4921d916488f8abfa3d6d [file] [log] [blame]
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, 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
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
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,
42 Measurements& measurements)
43 : 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)
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060048 , m_areTablesConfigured(false)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060049{
50
51}
52
53void
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060054TablesConfigSection::setConfigFile(ConfigFile& configFile)
55{
56 configFile.addSectionHandler("tables",
57 bind(&TablesConfigSection::onConfig, this, _1, _2, _3));
58}
59
60
61void
62TablesConfigSection::ensureTablesAreConfigured()
63{
64 if (m_areTablesConfigured)
65 {
66 return;
67 }
68
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
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060076TablesConfigSection::onConfig(const ConfigSection& configSection,
77 bool isDryRun,
78 const std::string& filename)
79{
80 // tables
81 // {
82 // cs_max_packets 65536
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070083 //
84 // strategy_choice
85 // {
86 // / /localhost/nfd/strategy/best-route
87 // /localhost /localhost/nfd/strategy/broadcast
88 // /localhost/nfd /localhost/nfd/strategy/best-route
89 // /ndn/broadcast /localhost/nfd/strategy/broadcast
90 // }
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060091 // }
92
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060093 size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
94
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060095 boost::optional<const ConfigSection&> csMaxPacketsNode =
96 configSection.get_child_optional("cs_max_packets");
97
98 if (csMaxPacketsNode)
99 {
100 boost::optional<size_t> valCsMaxPackets =
101 configSection.get_optional<size_t>("cs_max_packets");
102
103 if (!valCsMaxPackets)
104 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700105 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"cs_max_packets\""
106 " in \"tables\" section"));
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600107 }
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600108
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600109 nCsMaxPackets = *valCsMaxPackets;
110 }
111
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700112 boost::optional<const ConfigSection&> strategyChoiceSection =
113 configSection.get_child_optional("strategy_choice");
114
115 if (strategyChoiceSection)
116 {
117 processSectionStrategyChoice(*strategyChoiceSection, isDryRun);
118 }
119
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -0600120 if (!isDryRun)
121 {
122 NFD_LOG_INFO("Setting CS max packets to " << nCsMaxPackets);
123
124 m_cs.setLimit(nCsMaxPackets);
125 m_areTablesConfigured = true;
126 }
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600127}
128
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700129void
130TablesConfigSection::processSectionStrategyChoice(const ConfigSection& configSection,
131 bool isDryRun)
132{
133 // strategy_choice
134 // {
135 // / /localhost/nfd/strategy/best-route
136 // /localhost /localhost/nfd/strategy/broadcast
137 // /localhost/nfd /localhost/nfd/strategy/best-route
138 // /ndn/broadcast /localhost/nfd/strategy/broadcast
139 // }
140
141 std::map<Name, Name> choices;
142
143 for (const auto& prefixAndStrategy : configSection)
144 {
145 const Name prefix(prefixAndStrategy.first);
146 if (choices.find(prefix) != choices.end())
147 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700148 BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate strategy choice for prefix \"" +
149 prefix.toUri() + "\" in \"strategy_choice\" "
150 "section"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700151 }
152
153 const std::string strategyString(prefixAndStrategy.second.get_value<std::string>());
154 if (strategyString.empty())
155 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700156 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid strategy choice \"\" for prefix \"" +
157 prefix.toUri() + "\" in \"strategy_choice\" "
158 "section"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700159 }
160
161 const Name strategyName(strategyString);
162 if (!m_strategyChoice.hasStrategy(strategyName))
163 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700164 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid strategy choice \"" +
165 strategyName.toUri() + "\" for prefix \"" +
166 prefix.toUri() + "\" in \"strategy_choice\" "
167 "section"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700168 }
169
170 choices[prefix] = strategyName;
171 }
172
173
174 for (const auto& prefixAndStrategy : choices)
175 {
176 if (!isDryRun && !m_strategyChoice.insert(prefixAndStrategy.first, prefixAndStrategy.second))
177 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700178 BOOST_THROW_EXCEPTION(ConfigFile::Error("Failed to set strategy \"" +
179 prefixAndStrategy.second.toUri() + "\" for "
180 "prefix \"" + prefixAndStrategy.first.toUri() +
181 "\" in \"strategy_choicev\""));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700182 }
183 }
184}
185
186
187
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600188} // namespace nfd