blob: 5095da80e1dea87f8f329b5311952124bb4e32f6 [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#include "core/logger.hpp"
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060028
29namespace nfd {
30
31NFD_LOG_INIT("TablesConfigSection");
32
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060033const size_t TablesConfigSection::DEFAULT_CS_MAX_PACKETS = 65536;
34
Junxiao Shi0cc125c2016-08-25 21:50:04 +000035TablesConfigSection::TablesConfigSection(Forwarder& forwarder)
36 : m_forwarder(forwarder)
37 , m_isConfigured(false)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060038{
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060039}
40
41void
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060042TablesConfigSection::setConfigFile(ConfigFile& configFile)
43{
44 configFile.addSectionHandler("tables",
Junxiao Shi0cc125c2016-08-25 21:50:04 +000045 bind(&TablesConfigSection::processConfig, this, _1, _2));
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060046}
47
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060048void
Junxiao Shi0cc125c2016-08-25 21:50:04 +000049TablesConfigSection::ensureConfigured()
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060050{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000051 if (m_isConfigured) {
Vince Lehman63ab1bb2015-09-04 17:06:58 -050052 return;
53 }
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060054
55 NFD_LOG_INFO("Setting CS max packets to " << DEFAULT_CS_MAX_PACKETS);
Junxiao Shi0cc125c2016-08-25 21:50:04 +000056 m_forwarder.getCs().setLimit(DEFAULT_CS_MAX_PACKETS);
Steve DiBenedettofbea5902014-07-08 15:29:12 -060057
Junxiao Shi0cc125c2016-08-25 21:50:04 +000058 m_isConfigured = true;
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060059}
60
61void
Junxiao Shi0cc125c2016-08-25 21:50:04 +000062TablesConfigSection::processConfig(const ConfigSection& section, bool isDryRun)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060063{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000064 typedef boost::optional<const ConfigSection&> OptionalNode;
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060065
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060066 size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
Junxiao Shi0cc125c2016-08-25 21:50:04 +000067 OptionalNode csMaxPacketsNode = section.get_child_optional("cs_max_packets");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050068 if (csMaxPacketsNode) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +000069 nCsMaxPackets = ConfigFile::parseNumber<size_t>(*csMaxPacketsNode, "cs_max_packets", "tables");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050070 }
71
Junxiao Shi0cc125c2016-08-25 21:50:04 +000072 OptionalNode strategyChoiceSection = section.get_child_optional("strategy_choice");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050073 if (strategyChoiceSection) {
74 processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
75 }
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070076
Junxiao Shi0cc125c2016-08-25 21:50:04 +000077 OptionalNode networkRegionSection = section.get_child_optional("network_region");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050078 if (networkRegionSection) {
79 processNetworkRegionSection(*networkRegionSection, isDryRun);
80 }
81
Junxiao Shi0cc125c2016-08-25 21:50:04 +000082 if (isDryRun) {
83 return;
Vince Lehman63ab1bb2015-09-04 17:06:58 -050084 }
Junxiao Shi0cc125c2016-08-25 21:50:04 +000085
86 NFD_LOG_INFO("Setting CS max packets to " << nCsMaxPackets);
87 m_forwarder.getCs().setLimit(nCsMaxPackets);
88
89 m_isConfigured = true;
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060090}
91
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070092void
Junxiao Shi0cc125c2016-08-25 21:50:04 +000093TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun)
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070094{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000095 StrategyChoice& sc = m_forwarder.getStrategyChoice();
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070096
97 std::map<Name, Name> choices;
98
Junxiao Shi0cc125c2016-08-25 21:50:04 +000099 for (const auto& prefixAndStrategy : section) {
100 Name prefix(prefixAndStrategy.first);
101 Name strategy(prefixAndStrategy.second.get_value<std::string>());
102
103 if (!sc.hasStrategy(strategy)) {
104 BOOST_THROW_EXCEPTION(ConfigFile::Error(
105 "Unknown strategy \"" + prefixAndStrategy.second.get_value<std::string>() +
106 "\" for prefix \"" + prefix.toUri() + "\" in \"strategy_choice\" section"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700107 }
108
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000109 if (!choices.emplace(prefix, strategy).second) {
110 BOOST_THROW_EXCEPTION(ConfigFile::Error(
111 "Duplicate strategy choice for prefix \"" + prefix.toUri() +
112 "\" in \"strategy_choice\" section"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700113 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500114 }
115
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000116 if (isDryRun) {
117 return;
118 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500119
120 for (const auto& prefixAndStrategy : choices) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000121 if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
122 BOOST_THROW_EXCEPTION(ConfigFile::Error(
123 "Failed to set strategy \"" + prefixAndStrategy.second.toUri() + "\" for "
124 "prefix \"" + prefixAndStrategy.first.toUri() + "\" in \"strategy_choicev\""));
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500125 }
126 }
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700127}
128
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500129void
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000130TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun)
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500131{
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000132 if (isDryRun) {
133 return;
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500134 }
135
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000136 NetworkRegionTable& nrt = m_forwarder.getNetworkRegionTable();
137 nrt.clear();
138 for (const auto& pair : section) {
139 Name region(pair.first);
140 nrt.insert(region);
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500141 }
142}
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700143
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600144} // namespace nfd