tables-config-section.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, Regents of the University of California,
4  * 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.
10  *
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 
27 #include "fw/strategy.hpp"
28 
29 #include <map>
30 
31 namespace nfd {
32 
33 constexpr size_t DEFAULT_CS_MAX_PACKETS = 65536;
34 
36  : m_forwarder(forwarder)
37  , m_isConfigured(false)
38 {
39 }
40 
41 void
43 {
44  configFile.addSectionHandler("tables", [this] (auto&&... args) {
45  processConfig(std::forward<decltype(args)>(args)...);
46  });
47 }
48 
49 void
51 {
52  if (m_isConfigured) {
53  return;
54  }
55 
56  m_forwarder.getCs().setLimit(DEFAULT_CS_MAX_PACKETS);
57  // Don't set default cs_policy because it's already created by CS itself.
58  m_forwarder.setUnsolicitedDataPolicy(make_unique<fw::DefaultUnsolicitedDataPolicy>());
59 
60  m_isConfigured = true;
61 }
62 
63 void
64 TablesConfigSection::processConfig(const ConfigSection& section, bool isDryRun, const std::string&)
65 {
66  size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
67  OptionalConfigSection csMaxPacketsNode = section.get_child_optional("cs_max_packets");
68  if (csMaxPacketsNode) {
69  nCsMaxPackets = ConfigFile::parseNumber<size_t>(*csMaxPacketsNode, "cs_max_packets", "tables");
70  }
71 
72  unique_ptr<cs::Policy> csPolicy;
73  OptionalConfigSection csPolicyNode = section.get_child_optional("cs_policy");
74  if (csPolicyNode) {
75  std::string policyName = csPolicyNode->get_value<std::string>();
76  csPolicy = cs::Policy::create(policyName);
77  if (csPolicy == nullptr) {
78  NDN_THROW(ConfigFile::Error("Unknown cs_policy '" + policyName + "' in section 'tables'"));
79  }
80  }
81 
82  unique_ptr<fw::UnsolicitedDataPolicy> unsolicitedDataPolicy;
83  OptionalConfigSection unsolicitedDataPolicyNode = section.get_child_optional("cs_unsolicited_policy");
84  if (unsolicitedDataPolicyNode) {
85  std::string policyName = unsolicitedDataPolicyNode->get_value<std::string>();
86  unsolicitedDataPolicy = fw::UnsolicitedDataPolicy::create(policyName);
87  if (unsolicitedDataPolicy == nullptr) {
88  NDN_THROW(ConfigFile::Error("Unknown cs_unsolicited_policy '" + policyName + "' in section 'tables'"));
89  }
90  }
91  else {
92  unsolicitedDataPolicy = make_unique<fw::DefaultUnsolicitedDataPolicy>();
93  }
94 
95  OptionalConfigSection strategyChoiceSection = section.get_child_optional("strategy_choice");
96  if (strategyChoiceSection) {
97  processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
98  }
99 
100  OptionalConfigSection networkRegionSection = section.get_child_optional("network_region");
101  if (networkRegionSection) {
102  processNetworkRegionSection(*networkRegionSection, isDryRun);
103  }
104 
105  if (isDryRun) {
106  return;
107  }
108 
109  Cs& cs = m_forwarder.getCs();
110  cs.setLimit(nCsMaxPackets);
111  if (cs.size() == 0 && csPolicy != nullptr) {
112  cs.setPolicy(std::move(csPolicy));
113  }
114 
115  m_forwarder.setUnsolicitedDataPolicy(std::move(unsolicitedDataPolicy));
116 
117  m_isConfigured = true;
118 }
119 
120 void
121 TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun)
122 {
123  std::map<Name, Name> choices;
124  for (const auto& prefixAndStrategy : section) {
125  Name prefix(prefixAndStrategy.first);
126  Name strategy(prefixAndStrategy.second.get_value<std::string>());
127 
128  if (!fw::Strategy::canCreate(strategy)) {
129  NDN_THROW(ConfigFile::Error("Unknown strategy '" + prefixAndStrategy.second.get_value<std::string>() +
130  "' for prefix '" + prefix.toUri() + "' in section 'strategy_choice'"));
131  }
132 
133  if (!choices.try_emplace(prefix, std::move(strategy)).second) {
134  NDN_THROW(ConfigFile::Error("Duplicate strategy choice for prefix '" + prefix.toUri() +
135  "' in section 'strategy_choice'"));
136  }
137  }
138 
139  if (isDryRun) {
140  return;
141  }
142 
143  StrategyChoice& sc = m_forwarder.getStrategyChoice();
144  for (const auto& prefixAndStrategy : choices) {
145  if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
146  NDN_THROW(ConfigFile::Error(
147  "Failed to set strategy '" + prefixAndStrategy.second.toUri() + "' for prefix '" +
148  prefixAndStrategy.first.toUri() + "' in section 'strategy_choice'"));
149  }
150  }
152 }
153 
154 void
155 TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun)
156 {
157  if (isDryRun) {
158  return;
159  }
160 
161  auto& nrt = m_forwarder.getNetworkRegionTable();
162  nrt.clear();
163  for (const auto& pair : section) {
164  nrt.insert(Name(pair.first));
165  }
166 }
167 
168 } // namespace nfd
Configuration file parsing utility.
Definition: config-file.hpp:66
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
Setup notification of configuration file sections.
Definition: config-file.cpp:77
Main class of NFD's forwarding engine.
Definition: forwarder.hpp:54
Cs & getCs() noexcept
Definition: forwarder.hpp:102
NetworkRegionTable & getNetworkRegionTable() noexcept
Definition: forwarder.hpp:126
void setUnsolicitedDataPolicy(unique_ptr< fw::UnsolicitedDataPolicy > policy) noexcept
Definition: forwarder.hpp:77
StrategyChoice & getStrategyChoice() noexcept
Definition: forwarder.hpp:114
void setConfigFile(ConfigFile &configFile)
TablesConfigSection(Forwarder &forwarder)
void ensureConfigured()
Apply default configuration, if tables section was omitted in configuration file.
void setLimit(size_t nMaxPackets)
Change capacity (in number of packets).
Definition: cs.hpp:111
static unique_ptr< Policy > create(const std::string &policyName)
Returns a cs::Policy identified by policyName, or nullptr if policyName is unknown.
Definition: cs-policy.cpp:45
static bool canCreate(const Name &instanceName)
Returns whether a strategy instance can be created from instanceName.
Definition: strategy.cpp:86
static unique_ptr< UnsolicitedDataPolicy > create(const std::string &policyName)
-status-http-server
Definition: common.hpp:71
boost::optional< const ConfigSection & > OptionalConfigSection
An optional configuration file section.
Definition: config-file.hpp:46
boost::property_tree::ptree ConfigSection
A configuration file section.
Definition: config-file.hpp:41
constexpr size_t DEFAULT_CS_MAX_PACKETS