blob: 57230cddc0cc857e598465a1f1e0a9829c1e5e82 [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"
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060027
28namespace nfd {
29
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060030const size_t TablesConfigSection::DEFAULT_CS_MAX_PACKETS = 65536;
31
Junxiao Shi0cc125c2016-08-25 21:50:04 +000032TablesConfigSection::TablesConfigSection(Forwarder& forwarder)
33 : m_forwarder(forwarder)
34 , m_isConfigured(false)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060035{
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060036}
37
38void
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060039TablesConfigSection::setConfigFile(ConfigFile& configFile)
40{
41 configFile.addSectionHandler("tables",
Junxiao Shi0cc125c2016-08-25 21:50:04 +000042 bind(&TablesConfigSection::processConfig, this, _1, _2));
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060043}
44
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060045void
Junxiao Shi0cc125c2016-08-25 21:50:04 +000046TablesConfigSection::ensureConfigured()
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060047{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000048 if (m_isConfigured) {
Vince Lehman63ab1bb2015-09-04 17:06:58 -050049 return;
50 }
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060051
Junxiao Shi0cc125c2016-08-25 21:50:04 +000052 m_forwarder.getCs().setLimit(DEFAULT_CS_MAX_PACKETS);
Steve DiBenedettofbea5902014-07-08 15:29:12 -060053
Junxiao Shi0cc125c2016-08-25 21:50:04 +000054 m_isConfigured = true;
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060055}
56
57void
Junxiao Shi0cc125c2016-08-25 21:50:04 +000058TablesConfigSection::processConfig(const ConfigSection& section, bool isDryRun)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060059{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000060 typedef boost::optional<const ConfigSection&> OptionalNode;
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060061
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060062 size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
Junxiao Shi0cc125c2016-08-25 21:50:04 +000063 OptionalNode csMaxPacketsNode = section.get_child_optional("cs_max_packets");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050064 if (csMaxPacketsNode) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +000065 nCsMaxPackets = ConfigFile::parseNumber<size_t>(*csMaxPacketsNode, "cs_max_packets", "tables");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050066 }
67
Junxiao Shi0cc125c2016-08-25 21:50:04 +000068 OptionalNode strategyChoiceSection = section.get_child_optional("strategy_choice");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050069 if (strategyChoiceSection) {
70 processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
71 }
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070072
Junxiao Shi0cc125c2016-08-25 21:50:04 +000073 OptionalNode networkRegionSection = section.get_child_optional("network_region");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050074 if (networkRegionSection) {
75 processNetworkRegionSection(*networkRegionSection, isDryRun);
76 }
77
Junxiao Shi0cc125c2016-08-25 21:50:04 +000078 if (isDryRun) {
79 return;
Vince Lehman63ab1bb2015-09-04 17:06:58 -050080 }
Junxiao Shi0cc125c2016-08-25 21:50:04 +000081
Junxiao Shi0cc125c2016-08-25 21:50:04 +000082 m_forwarder.getCs().setLimit(nCsMaxPackets);
83
84 m_isConfigured = true;
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060085}
86
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070087void
Junxiao Shi0cc125c2016-08-25 21:50:04 +000088TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun)
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070089{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000090 StrategyChoice& sc = m_forwarder.getStrategyChoice();
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070091
92 std::map<Name, Name> choices;
93
Junxiao Shi0cc125c2016-08-25 21:50:04 +000094 for (const auto& prefixAndStrategy : section) {
95 Name prefix(prefixAndStrategy.first);
96 Name strategy(prefixAndStrategy.second.get_value<std::string>());
97
98 if (!sc.hasStrategy(strategy)) {
99 BOOST_THROW_EXCEPTION(ConfigFile::Error(
100 "Unknown strategy \"" + prefixAndStrategy.second.get_value<std::string>() +
101 "\" for prefix \"" + prefix.toUri() + "\" in \"strategy_choice\" section"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700102 }
103
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000104 if (!choices.emplace(prefix, strategy).second) {
105 BOOST_THROW_EXCEPTION(ConfigFile::Error(
106 "Duplicate strategy choice for prefix \"" + prefix.toUri() +
107 "\" in \"strategy_choice\" section"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700108 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500109 }
110
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000111 if (isDryRun) {
112 return;
113 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500114
115 for (const auto& prefixAndStrategy : choices) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000116 if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
117 BOOST_THROW_EXCEPTION(ConfigFile::Error(
118 "Failed to set strategy \"" + prefixAndStrategy.second.toUri() + "\" for "
119 "prefix \"" + prefixAndStrategy.first.toUri() + "\" in \"strategy_choicev\""));
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500120 }
121 }
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700122}
123
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500124void
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000125TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun)
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500126{
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000127 if (isDryRun) {
128 return;
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500129 }
130
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000131 NetworkRegionTable& nrt = m_forwarder.getNetworkRegionTable();
132 nrt.clear();
133 for (const auto& pair : section) {
134 Name region(pair.first);
135 nrt.insert(region);
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500136 }
137}
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700138
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600139} // namespace nfd