blob: e5383a4afe2bb476652fe7bc79d97cb411dc1b16 [file] [log] [blame]
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento19779d82019-02-14 13:40:04 -05002/*
3 * Copyright (c) 2014-2019, 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{
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060063 size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
Junxiao Shi38b24c72017-01-05 02:59:31 +000064 OptionalConfigSection csMaxPacketsNode = section.get_child_optional("cs_max_packets");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050065 if (csMaxPacketsNode) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +000066 nCsMaxPackets = ConfigFile::parseNumber<size_t>(*csMaxPacketsNode, "cs_max_packets", "tables");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050067 }
68
Junxiao Shib4a5acd2016-12-07 19:59:18 +000069 unique_ptr<cs::Policy> csPolicy;
Junxiao Shi38b24c72017-01-05 02:59:31 +000070 OptionalConfigSection csPolicyNode = section.get_child_optional("cs_policy");
Junxiao Shib4a5acd2016-12-07 19:59:18 +000071 if (csPolicyNode) {
72 std::string policyName = csPolicyNode->get_value<std::string>();
73 csPolicy = cs::Policy::create(policyName);
74 if (csPolicy == nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -050075 NDN_THROW(ConfigFile::Error("Unknown cs_policy '" + policyName + "' in section 'tables'"));
Junxiao Shib4a5acd2016-12-07 19:59:18 +000076 }
77 }
78
Junxiao Shi9685cc52016-08-29 12:47:05 +000079 unique_ptr<fw::UnsolicitedDataPolicy> unsolicitedDataPolicy;
Junxiao Shi38b24c72017-01-05 02:59:31 +000080 OptionalConfigSection unsolicitedDataPolicyNode = section.get_child_optional("cs_unsolicited_policy");
Junxiao Shi9685cc52016-08-29 12:47:05 +000081 if (unsolicitedDataPolicyNode) {
Junxiao Shib4a5acd2016-12-07 19:59:18 +000082 std::string policyName = unsolicitedDataPolicyNode->get_value<std::string>();
83 unsolicitedDataPolicy = fw::UnsolicitedDataPolicy::create(policyName);
Junxiao Shi9685cc52016-08-29 12:47:05 +000084 if (unsolicitedDataPolicy == nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -050085 NDN_THROW(ConfigFile::Error("Unknown cs_unsolicited_policy '" + policyName + "' in section 'tables'"));
Junxiao Shi9685cc52016-08-29 12:47:05 +000086 }
87 }
88 else {
89 unsolicitedDataPolicy = make_unique<fw::DefaultUnsolicitedDataPolicy>();
90 }
91
Junxiao Shi38b24c72017-01-05 02:59:31 +000092 OptionalConfigSection strategyChoiceSection = section.get_child_optional("strategy_choice");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050093 if (strategyChoiceSection) {
94 processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
95 }
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070096
Junxiao Shi38b24c72017-01-05 02:59:31 +000097 OptionalConfigSection networkRegionSection = section.get_child_optional("network_region");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050098 if (networkRegionSection) {
99 processNetworkRegionSection(*networkRegionSection, isDryRun);
100 }
101
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000102 if (isDryRun) {
103 return;
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500104 }
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000105
Junxiao Shib4a5acd2016-12-07 19:59:18 +0000106 Cs& cs = m_forwarder.getCs();
107 cs.setLimit(nCsMaxPackets);
108 if (cs.size() == 0 && csPolicy != nullptr) {
109 cs.setPolicy(std::move(csPolicy));
110 }
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000111
Junxiao Shi9685cc52016-08-29 12:47:05 +0000112 m_forwarder.setUnsolicitedDataPolicy(std::move(unsolicitedDataPolicy));
113
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000114 m_isConfigured = true;
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600115}
116
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700117void
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000118TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun)
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700119{
Junxiao Shi593887c2016-12-24 02:39:29 +0000120 using fw::Strategy;
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700121
122 std::map<Name, Name> choices;
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000123 for (const auto& prefixAndStrategy : section) {
124 Name prefix(prefixAndStrategy.first);
125 Name strategy(prefixAndStrategy.second.get_value<std::string>());
126
Junxiao Shi593887c2016-12-24 02:39:29 +0000127 if (!Strategy::canCreate(strategy)) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500128 NDN_THROW(ConfigFile::Error(
129 "Unknown strategy '" + prefixAndStrategy.second.get_value<std::string>() +
130 "' for prefix '" + prefix.toUri() + "' in section 'strategy_choice'"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700131 }
132
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000133 if (!choices.emplace(prefix, strategy).second) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500134 NDN_THROW(ConfigFile::Error(
135 "Duplicate strategy choice for prefix '" + prefix.toUri() + "' in section 'strategy_choice'"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700136 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500137 }
138
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000139 if (isDryRun) {
140 return;
141 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500142
Junxiao Shi593887c2016-12-24 02:39:29 +0000143 StrategyChoice& sc = m_forwarder.getStrategyChoice();
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500144 for (const auto& prefixAndStrategy : choices) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000145 if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500146 NDN_THROW(ConfigFile::Error(
147 "Failed to set strategy '" + prefixAndStrategy.second.toUri() + "' for prefix '" +
148 prefixAndStrategy.first.toUri() + "' in section 'strategy_choice'"));
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500149 }
150 }
Junxiao Shi593887c2016-12-24 02:39:29 +0000151 ///\todo redesign so that strategy parameter errors can be catched during dry-run
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700152}
153
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500154void
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000155TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun)
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500156{
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000157 if (isDryRun) {
158 return;
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500159 }
160
Davide Pesavento19779d82019-02-14 13:40:04 -0500161 auto& nrt = m_forwarder.getNetworkRegionTable();
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000162 nrt.clear();
163 for (const auto& pair : section) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500164 nrt.insert(Name(pair.first));
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500165 }
166}
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700167
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600168} // namespace nfd