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-2018, 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 "core/global-io.hpp"
35 #include "core/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 static const std::string CFG_SECTION = "rib";
49 static const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
50 static const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
51 static const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
52 static const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
53 static const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
54 static const uint64_t PROPAGATE_DEFAULT_COST = 15;
55 static const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
56 
57 Service::Service(const ConfigSection& configSection, ndn::Face& face, ndn::KeyChain& keyChain)
58  : Service(keyChain, face,
59  [&configSection] (ConfigFile& config, bool isDryRun) {
60  config.parse(configSection, isDryRun, "internal://nfd.conf");
61  })
62 {
63 }
64 
65 template<typename ConfigParseFunc>
66 Service::Service(ndn::KeyChain& keyChain, ndn::Face& face, const ConfigParseFunc& configParse)
67  : m_keyChain(keyChain)
68  , m_face(face)
69  , m_scheduler(m_face.getIoService())
70  , m_nfdController(m_face, m_keyChain)
71  , m_fibUpdater(m_rib, m_nfdController)
72  , m_dispatcher(m_face, m_keyChain)
73  , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher, m_scheduler)
74 {
76  config.addSectionHandler(CFG_SECTION, bind(&Service::processConfig, this, _1, _2, _3));
77  configParse(config, true);
78  configParse(config, false);
79 
80  m_ribManager.registerWithNfd();
81  m_ribManager.enableLocalFields();
82 }
83 
85 {
86 }
87 
88 Service&
90 {
91  auto node = ::ns3::NodeList::GetNode(::ns3::Simulator::GetContext());
92  auto l3 = node->GetObject<::ns3::ndn::L3Protocol>();
93  return l3->getRibService();
94 }
95 
96 void
97 Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
98 {
99  if (isDryRun) {
100  checkConfig(section, filename);
101  }
102  else {
103  applyConfig(section, filename);
104  }
105 }
106 
107 void
108 Service::checkConfig(const ConfigSection& section, const std::string& filename)
109 {
110  for (const auto& item : section) {
111  const std::string& key = item.first;
112  const ConfigSection& value = item.second;
113  if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY) {
114  ndn::ValidatorConfig testValidator(m_face);
115  testValidator.load(value, filename);
116  }
117  else if (key == CFG_PREFIX_PROPAGATE) {
118  // AutoPrefixPropagator does not support config dry-run
119  }
120  else if (key == CFG_READVERTISE_NLSR) {
122  }
123  else {
124  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
125  }
126  }
127 }
128 
129 void
130 Service::applyConfig(const ConfigSection& section, const std::string& filename)
131 {
132  bool wantPrefixPropagate = false;
133  bool wantReadvertiseNlsr = false;
134 
135  for (const auto& item : section) {
136  const std::string& key = item.first;
137  const ConfigSection& value = item.second;
138  if (key == CFG_LOCALHOST_SECURITY) {
139  m_ribManager.applyLocalhostConfig(value, filename);
140  }
141  else if (key == CFG_LOCALHOP_SECURITY) {
142  m_ribManager.enableLocalhop(value, filename);
143  }
144  else if (key == CFG_PREFIX_PROPAGATE) {
145  wantPrefixPropagate = true;
146 
147  if (!m_readvertisePropagation) {
148  NFD_LOG_DEBUG("Enabling automatic prefix propagation");
149 
150  auto parameters = ndn::nfd::ControlParameters()
151  .setCost(PROPAGATE_DEFAULT_COST)
152  .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
153  auto cost = item.second.get_optional<uint64_t>("cost");
154  if (cost) {
155  parameters.setCost(*cost);
156  }
157 
158  auto options = ndn::nfd::CommandOptions()
161  auto timeout = item.second.get_optional<uint64_t>("timeout");
162  if (timeout) {
163  options.setTimeout(time::milliseconds(*timeout));
164  }
165 
166  m_readvertisePropagation = make_unique<Readvertise>(
167  m_rib,
168  m_scheduler,
169  make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
170  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
171  }
172  }
173  else if (key == CFG_READVERTISE_NLSR) {
174  wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
175  }
176  else {
177  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
178  }
179  }
180 
181  if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
182  NFD_LOG_DEBUG("Disabling automatic prefix propagation");
183  m_readvertisePropagation.reset();
184  }
185 
186  if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
187  NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
189  m_readvertiseNlsr = make_unique<Readvertise>(
190  m_rib,
191  m_scheduler,
192  make_unique<ClientToNlsrReadvertisePolicy>(),
193  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
194  }
195  else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
196  NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
197  m_readvertiseNlsr.reset();
198  }
199 }
200 
201 } // namespace rib
202 } // namespace nfd
static const Name READVERTISE_NLSR_PREFIX
Definition: service.cpp:53
void applyLocalhostConfig(const ConfigSection &section, const std::string &filename)
Apply localhost_security configuration.
Definition: rib-manager.cpp:72
static const std::string CFG_SECTION
Definition: service.cpp:48
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:59
static const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT
Definition: service.cpp:55
The interface of signing key management.
Definition: key-chain.hpp:46
initializes and executes NFD-RIB service thread
Definition: service.hpp:51
Helper for validator that uses CommandInterest + Config policy and NetworkFetcher.
configuration file parsing utility
Definition: config-file.hpp:57
static const std::string CFG_LOCALHOST_SECURITY
Definition: service.cpp:49
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:78
static const std::string CFG_READVERTISE_NLSR
Definition: service.cpp:52
CommandOptions & setTimeout(const time::milliseconds &timeout)
sets command timeout
::nfd::rib::Service & getRibService()
static const uint64_t PROPAGATE_DEFAULT_COST
Definition: service.cpp:54
contains options for ControlCommand execution
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:93
boost::property_tree::ptree ConfigSection
a config file section
Represents an absolute name.
Definition: name.hpp:43
Implementation network-layer of NDN stack.
static 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:89
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
Service(const ConfigSection &configSection, ndn::Face &face, ndn::KeyChain &keyChain)
Definition: service.cpp:57
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:50
static const std::string CFG_PREFIX_PROPAGATE
Definition: service.cpp:51
~Service()
Destructor.
Definition: service.cpp:84
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
static const Name LOCALHOP_TOP_PREFIX
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands.
CommandOptions & setPrefix(const Name &prefix)
sets command prefix
void registerWithNfd()
Start accepting commands and dataset requests.
Definition: rib-manager.cpp:91