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-2022, 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 namespace nfd {
30 
31 constexpr size_t DEFAULT_CS_MAX_PACKETS = 65536;
32 
34  : m_forwarder(forwarder)
35  , m_isConfigured(false)
36 {
37 }
38 
39 void
41 {
42  configFile.addSectionHandler("tables", [this] (auto&&... args) {
43  processConfig(std::forward<decltype(args)>(args)...);
44  });
45 }
46 
47 void
49 {
50  if (m_isConfigured) {
51  return;
52  }
53 
54  m_forwarder.getCs().setLimit(DEFAULT_CS_MAX_PACKETS);
55  // Don't set default cs_policy because it's already created by CS itself.
56  m_forwarder.setUnsolicitedDataPolicy(make_unique<fw::DefaultUnsolicitedDataPolicy>());
57 
58  m_isConfigured = true;
59 }
60 
61 void
62 TablesConfigSection::processConfig(const ConfigSection& section, bool isDryRun, const std::string&)
63 {
64  size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
65  OptionalConfigSection csMaxPacketsNode = section.get_child_optional("cs_max_packets");
66  if (csMaxPacketsNode) {
67  nCsMaxPackets = ConfigFile::parseNumber<size_t>(*csMaxPacketsNode, "cs_max_packets", "tables");
68  }
69 
70  unique_ptr<cs::Policy> csPolicy;
71  OptionalConfigSection csPolicyNode = section.get_child_optional("cs_policy");
72  if (csPolicyNode) {
73  std::string policyName = csPolicyNode->get_value<std::string>();
74  csPolicy = cs::Policy::create(policyName);
75  if (csPolicy == nullptr) {
76  NDN_THROW(ConfigFile::Error("Unknown cs_policy '" + policyName + "' in section 'tables'"));
77  }
78  }
79 
80  unique_ptr<fw::UnsolicitedDataPolicy> unsolicitedDataPolicy;
81  OptionalConfigSection unsolicitedDataPolicyNode = section.get_child_optional("cs_unsolicited_policy");
82  if (unsolicitedDataPolicyNode) {
83  std::string policyName = unsolicitedDataPolicyNode->get_value<std::string>();
84  unsolicitedDataPolicy = fw::UnsolicitedDataPolicy::create(policyName);
85  if (unsolicitedDataPolicy == nullptr) {
86  NDN_THROW(ConfigFile::Error("Unknown cs_unsolicited_policy '" + policyName + "' in section 'tables'"));
87  }
88  }
89  else {
90  unsolicitedDataPolicy = make_unique<fw::DefaultUnsolicitedDataPolicy>();
91  }
92 
93  OptionalConfigSection strategyChoiceSection = section.get_child_optional("strategy_choice");
94  if (strategyChoiceSection) {
95  processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
96  }
97 
98  OptionalConfigSection networkRegionSection = section.get_child_optional("network_region");
99  if (networkRegionSection) {
100  processNetworkRegionSection(*networkRegionSection, isDryRun);
101  }
102 
103  if (isDryRun) {
104  return;
105  }
106 
107  Cs& cs = m_forwarder.getCs();
108  cs.setLimit(nCsMaxPackets);
109  if (cs.size() == 0 && csPolicy != nullptr) {
110  cs.setPolicy(std::move(csPolicy));
111  }
112 
113  m_forwarder.setUnsolicitedDataPolicy(std::move(unsolicitedDataPolicy));
114 
115  m_isConfigured = true;
116 }
117 
118 void
119 TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun)
120 {
121  std::map<Name, Name> choices;
122  for (const auto& prefixAndStrategy : section) {
123  Name prefix(prefixAndStrategy.first);
124  Name strategy(prefixAndStrategy.second.get_value<std::string>());
125 
126  if (!fw::Strategy::canCreate(strategy)) {
127  NDN_THROW(ConfigFile::Error("Unknown strategy '" + prefixAndStrategy.second.get_value<std::string>() +
128  "' for prefix '" + prefix.toUri() + "' in section 'strategy_choice'"));
129  }
130 
131  if (!choices.try_emplace(prefix, std::move(strategy)).second) {
132  NDN_THROW(ConfigFile::Error("Duplicate strategy choice for prefix '" + prefix.toUri() +
133  "' in section 'strategy_choice'"));
134  }
135  }
136 
137  if (isDryRun) {
138  return;
139  }
140 
141  StrategyChoice& sc = m_forwarder.getStrategyChoice();
142  for (const auto& prefixAndStrategy : choices) {
143  if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
144  NDN_THROW(ConfigFile::Error(
145  "Failed to set strategy '" + prefixAndStrategy.second.toUri() + "' for prefix '" +
146  prefixAndStrategy.first.toUri() + "' in section 'strategy_choice'"));
147  }
148  }
150 }
151 
152 void
153 TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun)
154 {
155  if (isDryRun) {
156  return;
157  }
158 
159  auto& nrt = m_forwarder.getNetworkRegionTable();
160  nrt.clear();
161  for (const auto& pair : section) {
162  nrt.insert(Name(pair.first));
163  }
164 }
165 
166 } // namespace nfd
Configuration file parsing utility.
Definition: config-file.hpp:63
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)
Definition: common.hpp:77
boost::optional< const ConfigSection & > OptionalConfigSection
An optional configuration file section.
Definition: config-file.hpp:43
boost::property_tree::ptree ConfigSection
A configuration file section.
Definition: config-file.hpp:38
constexpr size_t DEFAULT_CS_MAX_PACKETS