NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
service.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 
26 #include "service.hpp"
27 
28 #include "fib-updater.hpp"
33 
34 #include "common/global.hpp"
35 #include "common/logger.hpp"
36 
37 #include "ns3/node-list.h"
38 #include "ns3/node.h"
39 #include "ns3/simulator.h"
40 
41 #include "ns3/ndnSIM/model/ndn-l3-protocol.hpp"
42 
43 namespace nfd {
44 namespace rib {
45 
47 
48 const std::string CFG_RIB = "rib";
49 const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
50 const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
51 const std::string CFG_PA_VALIDATION = "prefix_announcement_validation";
52 const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
53 const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
54 const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
55 const uint64_t PROPAGATE_DEFAULT_COST = 15;
57 
58 Service::Service(const ConfigSection& configSection, ndn::Face& face, ndn::KeyChain& keyChain)
59  : Service(keyChain, face,
60  [&configSection] (ConfigFile& config, bool isDryRun) {
61  config.parse(configSection, isDryRun, "internal://nfd.conf");
62  })
63 {
64 }
65 
66 template<typename ConfigParseFunc>
67 Service::Service(ndn::KeyChain& keyChain, ndn::Face& face, const ConfigParseFunc& configParse)
68  : m_keyChain(keyChain)
69  , m_face(face)
70  , m_scheduler(m_face.getIoService())
71  , m_nfdController(m_face, m_keyChain)
72  , m_fibUpdater(m_rib, m_nfdController)
73  , m_dispatcher(m_face, m_keyChain)
74  , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
75 {
77  config.addSectionHandler(CFG_RIB, [this] (auto&&... args) {
78  processConfig(std::forward<decltype(args)>(args)...);
79  });
80  configParse(config, true);
81  configParse(config, false);
82 
83  m_ribManager.registerWithNfd();
84  m_ribManager.enableLocalFields();
85 }
86 
88 {
89 }
90 
91 Service&
93 {
94  auto node = ::ns3::NodeList::GetNode(::ns3::Simulator::GetContext());
95  auto l3 = node->GetObject<::ns3::ndn::L3Protocol>();
96  return l3->getRibService();
97 }
98 
99 void
100 Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
101 {
102  if (isDryRun) {
103  checkConfig(section, filename);
104  }
105  else {
106  applyConfig(section, filename);
107  }
108 }
109 
110 void
111 Service::checkConfig(const ConfigSection& section, const std::string& filename)
112 {
113  bool hasLocalhop = false;
114  bool hasPropagate = false;
115 
116  for (const auto& item : section) {
117  const std::string& key = item.first;
118  const ConfigSection& value = item.second;
119  if (key == CFG_LOCALHOST_SECURITY || key == CFG_PA_VALIDATION) {
120  ndn::ValidatorConfig testValidator(m_face);
121  testValidator.load(value, filename);
122  }
123  else if (key == CFG_LOCALHOP_SECURITY) {
124  hasLocalhop = true;
125  ndn::ValidatorConfig testValidator(m_face);
126  testValidator.load(value, filename);
127  }
128  else if (key == CFG_PREFIX_PROPAGATE) {
129  hasPropagate = true;
130  // AutoPrefixPropagator does not support config dry-run
131  }
132  else if (key == CFG_READVERTISE_NLSR) {
133  ConfigFile::parseYesNo(item, CFG_RIB + "." + CFG_READVERTISE_NLSR);
134  }
135  else {
136  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key));
137  }
138  }
139 
140  if (hasLocalhop && hasPropagate) {
141  NDN_THROW(ConfigFile::Error(CFG_LOCALHOP_SECURITY + " and " + CFG_PREFIX_PROPAGATE +
142  " cannot be enabled at the same time"));
143  }
144 }
145 
146 void
147 Service::applyConfig(const ConfigSection& section, const std::string& filename)
148 {
149  bool wantPrefixPropagate = false;
150  bool wantReadvertiseNlsr = false;
151 
152  for (const auto& item : section) {
153  const std::string& key = item.first;
154  const ConfigSection& value = item.second;
155  if (key == CFG_LOCALHOST_SECURITY) {
156  m_ribManager.applyLocalhostConfig(value, filename);
157  }
158  else if (key == CFG_LOCALHOP_SECURITY) {
159  m_ribManager.enableLocalhop(value, filename);
160  }
161  else if (key == CFG_PA_VALIDATION) {
162  m_ribManager.applyPaConfig(value, filename);
163  }
164  else if (key == CFG_PREFIX_PROPAGATE) {
165  wantPrefixPropagate = true;
166 
167  if (!m_readvertisePropagation) {
168  NFD_LOG_DEBUG("Enabling automatic prefix propagation");
169 
170  auto cost = item.second.get_optional<uint64_t>("cost");
171  auto parameters = ndn::nfd::ControlParameters()
172  .setCost(cost.value_or(PROPAGATE_DEFAULT_COST))
173  .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
174 
175  auto timeout = item.second.get_optional<uint64_t>("timeout");
176  auto options = ndn::nfd::CommandOptions()
178  .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT);
179 
180  m_readvertisePropagation = make_unique<Readvertise>(
181  m_rib,
182  make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
183  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
184  }
185  }
186  else if (key == CFG_READVERTISE_NLSR) {
187  wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_RIB + "." + CFG_READVERTISE_NLSR);
188  }
189  else {
190  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key));
191  }
192  }
193 
194  if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
195  NFD_LOG_DEBUG("Disabling automatic prefix propagation");
196  m_readvertisePropagation.reset();
197  }
198 
199  if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
200  NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
201  auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
202  m_readvertiseNlsr = make_unique<Readvertise>(
203  m_rib,
204  make_unique<ClientToNlsrReadvertisePolicy>(),
205  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
206  }
207  else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
208  NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
209  m_readvertiseNlsr.reset();
210  }
211 }
212 
213 } // namespace rib
214 } // namespace nfd
const Name READVERTISE_NLSR_PREFIX
Definition: service.cpp:54
const std::string CFG_PA_VALIDATION
Definition: service.cpp:51
void load(const std::string &filename)
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string &sectionName)
parse a config option that can be either "yes" or "no"
Definition: config-file.cpp:60
const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT
Definition: service.cpp:56
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands. ...
initializes and executes NFD-RIB service thread
Definition: service.hpp:52
ndn security KeyChain
Definition: key-chain.cpp:70
Helper for validator that uses SignedInterest + CommandInterest + Config policy and NetworkFetcher...
configuration file parsing utility
Definition: config-file.hpp:57
const std::string CFG_LOCALHOST_SECURITY
Definition: service.cpp:49
DummyIoService & getIoService()
Definition: face.hpp:383
const std::string CFG_READVERTISE_NLSR
Definition: service.cpp:53
#define NDN_THROW(e)
Definition: exception.hpp:61
const std::string CFG_RIB
Definition: service.cpp:48
CommandOptions & setTimeout(const time::milliseconds &timeout)
sets command timeout
::nfd::rib::Service & getRibService()
const uint64_t PROPAGATE_DEFAULT_COST
Definition: service.cpp:55
void enableLocalhop(const ConfigSection &section, const std::string &filename)
Apply localhop_security configuration and allow accepting commands on /localhop/nfd/rib prefix...
Definition: rib-manager.cpp:79
contains options for ControlCommand execution
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
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
boost::property_tree::ptree ConfigSection
a config file section
Represents an absolute name.
Definition: name.hpp:41
Implementation network-layer of NDN stack.
const std::string CFG_LOCALHOP_SECURITY
Definition: service.cpp:50
static Service & get()
Get a reference to the only instance of this class.
Definition: service.cpp:92
Service(const ConfigSection &configSection, ndn::Face &face, ndn::KeyChain &keyChain)
Definition: service.cpp:58
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51
const std::string CFG_PREFIX_PROPAGATE
Definition: service.cpp:52
static const Name LOCALHOP_TOP_PREFIX
void applyPaConfig(const ConfigSection &section, const std::string &filename)
Apply prefix_announcement_validation configuration.
Definition: rib-manager.cpp:92
~Service()
Destructor.
Definition: service.cpp:87
void registerWithNfd()
Start accepting commands and dataset requests.
Definition: rib-manager.cpp:98
void applyLocalhostConfig(const ConfigSection &section, const std::string &filename)
Apply localhost_security configuration.
Definition: rib-manager.cpp:73
CommandOptions & setPrefix(const Name &prefix)
sets command prefix
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48