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-2019, 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_SECTION = "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;
56 const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
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_SECTION, bind(&Service::processConfig, this, _1, _2, _3));
78  configParse(config, true);
79  configParse(config, false);
80 
81  m_ribManager.registerWithNfd();
82  m_ribManager.enableLocalFields();
83 }
84 
86 {
87 }
88 
89 Service&
91 {
92  auto node = ::ns3::NodeList::GetNode(::ns3::Simulator::GetContext());
93  auto l3 = node->GetObject<::ns3::ndn::L3Protocol>();
94  return l3->getRibService();
95 }
96 
97 void
98 Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
99 {
100  if (isDryRun) {
101  checkConfig(section, filename);
102  }
103  else {
104  applyConfig(section, filename);
105  }
106 }
107 
108 void
109 Service::checkConfig(const ConfigSection& section, const std::string& filename)
110 {
111  bool hasLocalhop = false;
112  bool hasPropagate = false;
113 
114  for (const auto& item : section) {
115  const std::string& key = item.first;
116  const ConfigSection& value = item.second;
117  if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY || key == CFG_PA_VALIDATION) {
118  hasLocalhop = key == CFG_LOCALHOP_SECURITY;
119  ndn::ValidatorConfig testValidator(m_face);
120  testValidator.load(value, filename);
121  }
122  else if (key == CFG_PREFIX_PROPAGATE) {
123  hasPropagate = true;
124  // AutoPrefixPropagator does not support config dry-run
125  }
126  else if (key == CFG_READVERTISE_NLSR) {
128  }
129  else {
130  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
131  }
132  }
133 
134  if (hasLocalhop && hasPropagate) {
135  NDN_THROW(ConfigFile::Error(CFG_LOCALHOP_SECURITY + " and " + CFG_PREFIX_PROPAGATE +
136  " cannot be enabled at the same time"));
137  }
138 }
139 
140 void
141 Service::applyConfig(const ConfigSection& section, const std::string& filename)
142 {
143  bool wantPrefixPropagate = false;
144  bool wantReadvertiseNlsr = false;
145 
146  for (const auto& item : section) {
147  const std::string& key = item.first;
148  const ConfigSection& value = item.second;
149  if (key == CFG_LOCALHOST_SECURITY) {
150  m_ribManager.applyLocalhostConfig(value, filename);
151  }
152  else if (key == CFG_LOCALHOP_SECURITY) {
153  m_ribManager.enableLocalhop(value, filename);
154  }
155  else if (key == CFG_PA_VALIDATION) {
156  m_ribManager.applyPaConfig(value, filename);
157  }
158  else if (key == CFG_PREFIX_PROPAGATE) {
159  wantPrefixPropagate = true;
160 
161  if (!m_readvertisePropagation) {
162  NFD_LOG_DEBUG("Enabling automatic prefix propagation");
163 
164  auto cost = item.second.get_optional<uint64_t>("cost");
165  auto parameters = ndn::nfd::ControlParameters()
166  .setCost(cost.value_or(PROPAGATE_DEFAULT_COST))
167  .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
168 
169  auto timeout = item.second.get_optional<uint64_t>("timeout");
170  auto options = ndn::nfd::CommandOptions()
172  .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT);
173 
174  m_readvertisePropagation = make_unique<Readvertise>(
175  m_rib,
176  make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
177  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
178  }
179  }
180  else if (key == CFG_READVERTISE_NLSR) {
181  wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
182  }
183  else {
184  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
185  }
186  }
187 
188  if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
189  NFD_LOG_DEBUG("Disabling automatic prefix propagation");
190  m_readvertisePropagation.reset();
191  }
192 
193  if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
194  NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
196  m_readvertiseNlsr = make_unique<Readvertise>(
197  m_rib,
198  make_unique<ClientToNlsrReadvertisePolicy>(),
199  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
200  }
201  else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
202  NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
203  m_readvertiseNlsr.reset();
204  }
205 }
206 
207 } // namespace rib
208 } // namespace nfd
nfd::rib::CFG_LOCALHOST_SECURITY
const std::string CFG_LOCALHOST_SECURITY
Definition: service.cpp:49
nfd::rib::RibService
RibService
Definition: service.cpp:46
global.hpp
nfd::ConfigFile::parseYesNo
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
nfd::rib::Service::Service
Service(const ConfigSection &configSection, ndn::Face &face, ndn::KeyChain &keyChain)
Definition: service.cpp:58
host-to-gateway-readvertise-policy.hpp
nfd-rib-readvertise-destination.hpp
ndn::nfd::CommandOptions::setPrefix
CommandOptions & setPrefix(const Name &prefix)
sets command prefix
Definition: command-options.cpp:48
nfd::ConfigFile::ignoreUnknownSection
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51
nfd::rib::PROPAGATE_DEFAULT_COST
const uint64_t PROPAGATE_DEFAULT_COST
Definition: service.cpp:55
nfd::RibManager::registerWithNfd
void registerWithNfd()
Start accepting commands and dataset requests.
Definition: rib-manager.cpp:98
nfd::rib::PROPAGATE_DEFAULT_TIMEOUT
const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT
Definition: service.cpp:56
ndn::Face
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
readvertise.hpp
nfd::RibManager::enableLocalhop
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
ndn::nfd::CommandOptions::setTimeout
CommandOptions & setTimeout(const time::milliseconds &timeout)
sets command timeout
Definition: command-options.cpp:37
client-to-nlsr-readvertise-policy.hpp
nfd::rib::Service
initializes and executes NFD-RIB service thread
Definition: service.hpp:53
ns3::ndn::L3Protocol::getRibService
::nfd::rib::Service & getRibService()
Definition: ndn-l3-protocol.cpp:352
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
nfd::rib::CFG_PREFIX_PROPAGATE
const std::string CFG_PREFIX_PROPAGATE
Definition: service.cpp:52
fib-updater.hpp
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::ConfigFile
configuration file parsing utility
Definition: config-file.hpp:58
nfd::RibManager::enableLocalFields
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands.
Definition: rib-manager.cpp:114
ndn::security::v2::KeyChain
The interface of signing key management.
Definition: key-chain.hpp:47
nfd::rib::CFG_PA_VALIDATION
const std::string CFG_PA_VALIDATION
Definition: service.cpp:51
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::tlv::nfd::ControlParameters
@ ControlParameters
Definition: tlv-nfd.hpp:35
nfd::rib::Service::~Service
~Service()
Destructor.
Definition: service.cpp:85
nfd::rib::CFG_LOCALHOP_SECURITY
const std::string CFG_LOCALHOP_SECURITY
Definition: service.cpp:50
ndn::nfd::ROUTE_ORIGIN_CLIENT
@ ROUTE_ORIGIN_CLIENT
Definition: nfd-constants.hpp:103
ndn::security::ValidatorConfig
Helper for validator that uses CommandInterest + Config policy and NetworkFetcher.
Definition: validator-config.hpp:36
ndn::nfd::CommandOptions
contains options for ControlCommand execution
Definition: command-options.hpp:35
NFD_LOG_DEBUG
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
nfd::ConfigSection
boost::property_tree::ptree ConfigSection
a config file section
Definition: ndn-l3-protocol.hpp:39
nfd::RibManager::LOCALHOP_TOP_PREFIX
static const Name LOCALHOP_TOP_PREFIX
Definition: rib-manager.hpp:243
nfd::rib::READVERTISE_NLSR_PREFIX
const Name READVERTISE_NLSR_PREFIX
Definition: service.cpp:54
nfd::rib::CFG_READVERTISE_NLSR
const std::string CFG_READVERTISE_NLSR
Definition: service.cpp:53
ns3::ndn::L3Protocol
Implementation network-layer of NDN stack.
Definition: ndn-l3-protocol.hpp:81
nfd::RibManager::applyLocalhostConfig
void applyLocalhostConfig(const ConfigSection &section, const std::string &filename)
Apply localhost_security configuration.
Definition: rib-manager.cpp:73
service.hpp
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
nfd::RibManager::applyPaConfig
void applyPaConfig(const ConfigSection &section, const std::string &filename)
Apply prefix_announcement_validation configuration.
Definition: rib-manager.cpp:92
nfd::rib::Service::get
static Service & get()
Get a reference to the only instance of this class.
Definition: service.cpp:90
logger.hpp
nfd::rib::CFG_SECTION
const std::string CFG_SECTION
Definition: service.cpp:48