NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2021, 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 const 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  using fw::Strategy;
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 (!Strategy::canCreate(strategy)) {
130  "Unknown strategy '" + prefixAndStrategy.second.get_value<std::string>() +
131  "' for prefix '" + prefix.toUri() + "' in section 'strategy_choice'"));
132  }
133 
134  if (!choices.emplace(prefix, strategy).second) {
136  "Duplicate strategy choice for prefix '" + prefix.toUri() + "' in section 'strategy_choice'"));
137  }
138  }
139 
140  if (isDryRun) {
141  return;
142  }
143 
144  StrategyChoice& sc = m_forwarder.getStrategyChoice();
145  for (const auto& prefixAndStrategy : choices) {
146  if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
148  "Failed to set strategy '" + prefixAndStrategy.second.toUri() + "' for prefix '" +
149  prefixAndStrategy.first.toUri() + "' in section 'strategy_choice'"));
150  }
151  }
153 }
154 
155 void
156 TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun)
157 {
158  if (isDryRun) {
159  return;
160  }
161 
162  auto& nrt = m_forwarder.getNetworkRegionTable();
163  nrt.clear();
164  for (const auto& pair : section) {
165  nrt.insert(Name(pair.first));
166  }
167 }
168 
169 } // namespace nfd
static unique_ptr< Policy > create(const std::string &policyName)
Definition: cs-policy.cpp:46
configuration file parsing utility
Definition: config-file.hpp:57
Main class of NFD&#39;s forwarding engine.
Definition: forwarder.hpp:53
void ensureConfigured()
apply default configuration, if tables section was omitted in configuration file
StrategyChoice & getStrategyChoice()
Definition: forwarder.hpp:112
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
InsertResult insert(const Name &prefix, const Name &strategyName)
Set strategy of prefix to be strategyName.
#define NDN_THROW(e)
Definition: exception.hpp:61
size_t size() const
get number of stored packets
Definition: cs.hpp:94
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:77
static unique_ptr< UnsolicitedDataPolicy > create(const std::string &policyName)
void setPolicy(unique_ptr< Policy > policy)
change replacement policy
Definition: cs.cpp:144
void setLimit(size_t nMaxPackets)
change capacity (in number of packets)
Definition: cs.hpp:111
boost::property_tree::ptree ConfigSection
a config file section
Represents an absolute name.
Definition: name.hpp:41
implements the Content Store
Definition: cs.hpp:44
Represents the Strategy Choice table.
NetworkRegionTable & getNetworkRegionTable()
Definition: forwarder.hpp:124
const size_t DEFAULT_CS_MAX_PACKETS
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Definition: name.cpp:349
void setConfigFile(ConfigFile &configFile)
void setUnsolicitedDataPolicy(unique_ptr< fw::UnsolicitedDataPolicy > policy)
Definition: forwarder.hpp:75
TablesConfigSection(Forwarder &forwarder)