blob: fcd0916177612279bbcdab78650eaf14d6d9a924 [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"
Junxiao Shi593887c2016-12-24 02:39:29 +000027#include "fw/strategy.hpp"
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060028
29namespace nfd {
30
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060031const size_t TablesConfigSection::DEFAULT_CS_MAX_PACKETS = 65536;
32
Junxiao Shi0cc125c2016-08-25 21:50:04 +000033TablesConfigSection::TablesConfigSection(Forwarder& forwarder)
34 : m_forwarder(forwarder)
35 , m_isConfigured(false)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060036{
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060037}
38
39void
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060040TablesConfigSection::setConfigFile(ConfigFile& configFile)
41{
42 configFile.addSectionHandler("tables",
Junxiao Shi0cc125c2016-08-25 21:50:04 +000043 bind(&TablesConfigSection::processConfig, this, _1, _2));
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060044}
45
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060046void
Junxiao Shi0cc125c2016-08-25 21:50:04 +000047TablesConfigSection::ensureConfigured()
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060048{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000049 if (m_isConfigured) {
Vince Lehman63ab1bb2015-09-04 17:06:58 -050050 return;
51 }
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060052
Junxiao Shi0cc125c2016-08-25 21:50:04 +000053 m_forwarder.getCs().setLimit(DEFAULT_CS_MAX_PACKETS);
Junxiao Shib4a5acd2016-12-07 19:59:18 +000054 // Don't set default cs_policy because it's already created by CS itself.
Junxiao Shi9685cc52016-08-29 12:47:05 +000055 m_forwarder.setUnsolicitedDataPolicy(make_unique<fw::DefaultUnsolicitedDataPolicy>());
Steve DiBenedettofbea5902014-07-08 15:29:12 -060056
Junxiao Shi0cc125c2016-08-25 21:50:04 +000057 m_isConfigured = true;
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060058}
59
60void
Junxiao Shi0cc125c2016-08-25 21:50:04 +000061TablesConfigSection::processConfig(const ConfigSection& section, bool isDryRun)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060062{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000063 typedef boost::optional<const ConfigSection&> OptionalNode;
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060064
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060065 size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
Junxiao Shi0cc125c2016-08-25 21:50:04 +000066 OptionalNode csMaxPacketsNode = section.get_child_optional("cs_max_packets");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050067 if (csMaxPacketsNode) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +000068 nCsMaxPackets = ConfigFile::parseNumber<size_t>(*csMaxPacketsNode, "cs_max_packets", "tables");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050069 }
70
Junxiao Shib4a5acd2016-12-07 19:59:18 +000071 unique_ptr<cs::Policy> csPolicy;
72 OptionalNode csPolicyNode = section.get_child_optional("cs_policy");
73 if (csPolicyNode) {
74 std::string policyName = csPolicyNode->get_value<std::string>();
75 csPolicy = cs::Policy::create(policyName);
76 if (csPolicy == nullptr) {
77 BOOST_THROW_EXCEPTION(ConfigFile::Error(
78 "Unknown cs_policy \"" + policyName + "\" in \"tables\" section"));
79 }
80 }
81
Junxiao Shi9685cc52016-08-29 12:47:05 +000082 unique_ptr<fw::UnsolicitedDataPolicy> unsolicitedDataPolicy;
83 OptionalNode unsolicitedDataPolicyNode = section.get_child_optional("cs_unsolicited_policy");
84 if (unsolicitedDataPolicyNode) {
Junxiao Shib4a5acd2016-12-07 19:59:18 +000085 std::string policyName = unsolicitedDataPolicyNode->get_value<std::string>();
86 unsolicitedDataPolicy = fw::UnsolicitedDataPolicy::create(policyName);
Junxiao Shi9685cc52016-08-29 12:47:05 +000087 if (unsolicitedDataPolicy == nullptr) {
88 BOOST_THROW_EXCEPTION(ConfigFile::Error(
Junxiao Shib4a5acd2016-12-07 19:59:18 +000089 "Unknown cs_unsolicited_policy \"" + policyName + "\" in \"tables\" section"));
Junxiao Shi9685cc52016-08-29 12:47:05 +000090 }
91 }
92 else {
93 unsolicitedDataPolicy = make_unique<fw::DefaultUnsolicitedDataPolicy>();
94 }
95
Junxiao Shi0cc125c2016-08-25 21:50:04 +000096 OptionalNode strategyChoiceSection = section.get_child_optional("strategy_choice");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050097 if (strategyChoiceSection) {
98 processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
99 }
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700100
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000101 OptionalNode networkRegionSection = section.get_child_optional("network_region");
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500102 if (networkRegionSection) {
103 processNetworkRegionSection(*networkRegionSection, isDryRun);
104 }
105
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000106 if (isDryRun) {
107 return;
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500108 }
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000109
Junxiao Shib4a5acd2016-12-07 19:59:18 +0000110 Cs& cs = m_forwarder.getCs();
111 cs.setLimit(nCsMaxPackets);
112 if (cs.size() == 0 && csPolicy != nullptr) {
113 cs.setPolicy(std::move(csPolicy));
114 }
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000115
Junxiao Shi9685cc52016-08-29 12:47:05 +0000116 m_forwarder.setUnsolicitedDataPolicy(std::move(unsolicitedDataPolicy));
117
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000118 m_isConfigured = true;
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600119}
120
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700121void
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000122TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun)
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700123{
Junxiao Shi593887c2016-12-24 02:39:29 +0000124 using fw::Strategy;
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700125
126 std::map<Name, Name> choices;
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000127 for (const auto& prefixAndStrategy : section) {
128 Name prefix(prefixAndStrategy.first);
129 Name strategy(prefixAndStrategy.second.get_value<std::string>());
130
Junxiao Shi593887c2016-12-24 02:39:29 +0000131 if (!Strategy::canCreate(strategy)) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000132 BOOST_THROW_EXCEPTION(ConfigFile::Error(
133 "Unknown strategy \"" + prefixAndStrategy.second.get_value<std::string>() +
134 "\" for prefix \"" + prefix.toUri() + "\" in \"strategy_choice\" section"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700135 }
136
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000137 if (!choices.emplace(prefix, strategy).second) {
138 BOOST_THROW_EXCEPTION(ConfigFile::Error(
139 "Duplicate strategy choice for prefix \"" + prefix.toUri() +
140 "\" in \"strategy_choice\" section"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700141 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500142 }
143
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000144 if (isDryRun) {
145 return;
146 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500147
Junxiao Shi593887c2016-12-24 02:39:29 +0000148 StrategyChoice& sc = m_forwarder.getStrategyChoice();
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500149 for (const auto& prefixAndStrategy : choices) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000150 if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
151 BOOST_THROW_EXCEPTION(ConfigFile::Error(
152 "Failed to set strategy \"" + prefixAndStrategy.second.toUri() + "\" for "
153 "prefix \"" + prefixAndStrategy.first.toUri() + "\" in \"strategy_choicev\""));
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500154 }
155 }
Junxiao Shi593887c2016-12-24 02:39:29 +0000156 ///\todo redesign so that strategy parameter errors can be catched during dry-run
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700157}
158
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500159void
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000160TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun)
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500161{
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000162 if (isDryRun) {
163 return;
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500164 }
165
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000166 NetworkRegionTable& nrt = m_forwarder.getNetworkRegionTable();
167 nrt.clear();
168 for (const auto& pair : section) {
169 Name region(pair.first);
170 nrt.insert(region);
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500171 }
172}
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700173
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600174} // namespace nfd