blob: f6adced6ab39ebfd28b106268e4420f1e47f2043 [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/*
Davide Pesavento412c9822021-07-02 00:21:05 -04003 * Copyright (c) 2014-2021, 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
Davide Pesavento412c9822021-07-02 00:21:05 -040031const size_t DEFAULT_CS_MAX_PACKETS = 65536;
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060032
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{
Davide Pesavento412c9822021-07-02 00:21:05 -040042 configFile.addSectionHandler("tables", [this] (auto&&... args) {
43 processConfig(std::forward<decltype(args)>(args)...);
44 });
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060045}
46
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060047void
Junxiao Shi0cc125c2016-08-25 21:50:04 +000048TablesConfigSection::ensureConfigured()
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060049{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000050 if (m_isConfigured) {
Vince Lehman63ab1bb2015-09-04 17:06:58 -050051 return;
52 }
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060053
Junxiao Shi0cc125c2016-08-25 21:50:04 +000054 m_forwarder.getCs().setLimit(DEFAULT_CS_MAX_PACKETS);
Junxiao Shib4a5acd2016-12-07 19:59:18 +000055 // Don't set default cs_policy because it's already created by CS itself.
Junxiao Shi9685cc52016-08-29 12:47:05 +000056 m_forwarder.setUnsolicitedDataPolicy(make_unique<fw::DefaultUnsolicitedDataPolicy>());
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
Davide Pesavento412c9822021-07-02 00:21:05 -040062TablesConfigSection::processConfig(const ConfigSection& section, bool isDryRun, const std::string&)
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060063{
Steve DiBenedetto9bcc88f2014-07-08 09:52:13 -060064 size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
Junxiao Shi38b24c72017-01-05 02:59:31 +000065 OptionalConfigSection csMaxPacketsNode = section.get_child_optional("cs_max_packets");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050066 if (csMaxPacketsNode) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +000067 nCsMaxPackets = ConfigFile::parseNumber<size_t>(*csMaxPacketsNode, "cs_max_packets", "tables");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050068 }
69
Junxiao Shib4a5acd2016-12-07 19:59:18 +000070 unique_ptr<cs::Policy> csPolicy;
Junxiao Shi38b24c72017-01-05 02:59:31 +000071 OptionalConfigSection csPolicyNode = section.get_child_optional("cs_policy");
Junxiao Shib4a5acd2016-12-07 19:59:18 +000072 if (csPolicyNode) {
73 std::string policyName = csPolicyNode->get_value<std::string>();
74 csPolicy = cs::Policy::create(policyName);
75 if (csPolicy == nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -050076 NDN_THROW(ConfigFile::Error("Unknown cs_policy '" + policyName + "' in section 'tables'"));
Junxiao Shib4a5acd2016-12-07 19:59:18 +000077 }
78 }
79
Junxiao Shi9685cc52016-08-29 12:47:05 +000080 unique_ptr<fw::UnsolicitedDataPolicy> unsolicitedDataPolicy;
Junxiao Shi38b24c72017-01-05 02:59:31 +000081 OptionalConfigSection unsolicitedDataPolicyNode = section.get_child_optional("cs_unsolicited_policy");
Junxiao Shi9685cc52016-08-29 12:47:05 +000082 if (unsolicitedDataPolicyNode) {
Junxiao Shib4a5acd2016-12-07 19:59:18 +000083 std::string policyName = unsolicitedDataPolicyNode->get_value<std::string>();
84 unsolicitedDataPolicy = fw::UnsolicitedDataPolicy::create(policyName);
Junxiao Shi9685cc52016-08-29 12:47:05 +000085 if (unsolicitedDataPolicy == nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -050086 NDN_THROW(ConfigFile::Error("Unknown cs_unsolicited_policy '" + policyName + "' in section 'tables'"));
Junxiao Shi9685cc52016-08-29 12:47:05 +000087 }
88 }
89 else {
90 unsolicitedDataPolicy = make_unique<fw::DefaultUnsolicitedDataPolicy>();
91 }
92
Junxiao Shi38b24c72017-01-05 02:59:31 +000093 OptionalConfigSection strategyChoiceSection = section.get_child_optional("strategy_choice");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050094 if (strategyChoiceSection) {
95 processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
96 }
Steve DiBenedettoc0640f52014-11-03 15:55:43 -070097
Junxiao Shi38b24c72017-01-05 02:59:31 +000098 OptionalConfigSection networkRegionSection = section.get_child_optional("network_region");
Vince Lehman63ab1bb2015-09-04 17:06:58 -050099 if (networkRegionSection) {
100 processNetworkRegionSection(*networkRegionSection, isDryRun);
101 }
102
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000103 if (isDryRun) {
104 return;
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500105 }
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000106
Junxiao Shib4a5acd2016-12-07 19:59:18 +0000107 Cs& cs = m_forwarder.getCs();
108 cs.setLimit(nCsMaxPackets);
109 if (cs.size() == 0 && csPolicy != nullptr) {
110 cs.setPolicy(std::move(csPolicy));
111 }
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000112
Junxiao Shi9685cc52016-08-29 12:47:05 +0000113 m_forwarder.setUnsolicitedDataPolicy(std::move(unsolicitedDataPolicy));
114
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000115 m_isConfigured = true;
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600116}
117
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700118void
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000119TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun)
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700120{
Junxiao Shi593887c2016-12-24 02:39:29 +0000121 using fw::Strategy;
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700122
123 std::map<Name, Name> choices;
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000124 for (const auto& prefixAndStrategy : section) {
125 Name prefix(prefixAndStrategy.first);
126 Name strategy(prefixAndStrategy.second.get_value<std::string>());
127
Junxiao Shi593887c2016-12-24 02:39:29 +0000128 if (!Strategy::canCreate(strategy)) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500129 NDN_THROW(ConfigFile::Error(
130 "Unknown strategy '" + prefixAndStrategy.second.get_value<std::string>() +
131 "' for prefix '" + prefix.toUri() + "' in section 'strategy_choice'"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700132 }
133
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000134 if (!choices.emplace(prefix, strategy).second) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500135 NDN_THROW(ConfigFile::Error(
136 "Duplicate strategy choice for prefix '" + prefix.toUri() + "' in section 'strategy_choice'"));
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700137 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500138 }
139
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000140 if (isDryRun) {
141 return;
142 }
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500143
Junxiao Shi593887c2016-12-24 02:39:29 +0000144 StrategyChoice& sc = m_forwarder.getStrategyChoice();
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500145 for (const auto& prefixAndStrategy : choices) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000146 if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500147 NDN_THROW(ConfigFile::Error(
148 "Failed to set strategy '" + prefixAndStrategy.second.toUri() + "' for prefix '" +
149 prefixAndStrategy.first.toUri() + "' in section 'strategy_choice'"));
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500150 }
151 }
Junxiao Shi593887c2016-12-24 02:39:29 +0000152 ///\todo redesign so that strategy parameter errors can be catched during dry-run
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700153}
154
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500155void
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000156TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun)
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500157{
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000158 if (isDryRun) {
159 return;
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500160 }
161
Davide Pesavento19779d82019-02-14 13:40:04 -0500162 auto& nrt = m_forwarder.getNetworkRegionTable();
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000163 nrt.clear();
164 for (const auto& pair : section) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500165 nrt.insert(Name(pair.first));
Vince Lehman63ab1bb2015-09-04 17:06:58 -0500166 }
167}
Steve DiBenedettoc0640f52014-11-03 15:55:43 -0700168
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -0600169} // namespace nfd