blob: 48ff69c95cd7a8532886fba09b27a8e1828b909a [file] [log] [blame]
Alexander Afanasyev31367922015-02-09 20:51:10 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Alexander Afanasyev31367922015-02-09 20:51:10 -08004 * 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
Junxiao Shib2600172016-07-11 08:53:53 +000026#include "service.hpp"
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000027
28#include "auto-prefix-propagator.hpp"
29#include "fib-updater.hpp"
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000030#include "readvertise/client-to-nlsr-readvertise-policy.hpp"
31#include "readvertise/nfd-rib-readvertise-destination.hpp"
32#include "readvertise/readvertise.hpp"
33
Alexander Afanasyev31367922015-02-09 20:51:10 -080034#include "core/global-io.hpp"
Junxiao Shif4cfed12018-08-22 23:26:29 +000035#include "core/logger.hpp"
Alexander Afanasyev31367922015-02-09 20:51:10 -080036
37#include <boost/property_tree/info_parser.hpp>
Alexander Afanasyev31367922015-02-09 20:51:10 -080038#include <ndn-cxx/transport/tcp-transport.hpp>
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000039#include <ndn-cxx/transport/unix-transport.hpp>
Alexander Afanasyev31367922015-02-09 20:51:10 -080040
41namespace nfd {
42namespace rib {
43
Junxiao Shif4cfed12018-08-22 23:26:29 +000044NFD_LOG_INIT(RibService);
Alexander Afanasyev31367922015-02-09 20:51:10 -080045
Teng Liang04d5ce62018-08-06 10:20:24 +080046Service* Service::s_instance = nullptr;
47
Junxiao Shif4cfed12018-08-22 23:26:29 +000048static const std::string CFG_SECTION = "rib";
49static const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
50static const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
51static const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
52static const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
53static const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
54
55static ConfigSection
56loadConfigSectionFromFile(const std::string& filename)
Alexander Afanasyev31367922015-02-09 20:51:10 -080057{
Junxiao Shif4cfed12018-08-22 23:26:29 +000058 ConfigSection config;
59 // Any format errors should have been caught already
60 boost::property_tree::read_info(filename, config);
61 return config;
62}
63
64/**
65 * \brief Look into the config file and construct appropriate transport to communicate with NFD
66 * If NFD-RIB instance was initialized with config file, INFO format is assumed
67 */
68static shared_ptr<ndn::Transport>
69makeLocalNfdTransport(const ConfigSection& config)
70{
71 if (config.get_child_optional("face_system.unix")) {
72 // default socket path should be the same as in UnixStreamFactory::processConfig
73 auto path = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
74 return make_shared<ndn::UnixTransport>(path);
Teng Liang04d5ce62018-08-06 10:20:24 +080075 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000076 else if (config.get_child_optional("face_system.tcp") &&
77 config.get<std::string>("face_system.tcp.listen", "yes") == "yes") {
78 // default port should be the same as in TcpFactory::processConfig
79 auto port = config.get<std::string>("face_system.tcp.port", "6363");
80 return make_shared<ndn::TcpTransport>("localhost", port);
Teng Liang04d5ce62018-08-06 10:20:24 +080081 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000082 else {
83 BOOST_THROW_EXCEPTION(ConfigFile::Error("No transport is available to communicate with NFD"));
84 }
85}
86
87Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000088 : Service(keyChain, makeLocalNfdTransport(loadConfigSectionFromFile(configFile)),
89 [&configFile] (ConfigFile& config, bool isDryRun) {
90 config.parse(configFile, isDryRun);
91 })
Junxiao Shif4cfed12018-08-22 23:26:29 +000092{
Alexander Afanasyev31367922015-02-09 20:51:10 -080093}
94
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000095Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain)
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000096 : Service(keyChain, makeLocalNfdTransport(configSection),
97 [&configSection] (ConfigFile& config, bool isDryRun) {
98 config.parse(configSection, isDryRun, "internal://nfd.conf");
99 })
Junxiao Shif4cfed12018-08-22 23:26:29 +0000100{
Junxiao Shif4cfed12018-08-22 23:26:29 +0000101}
102
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000103template<typename ConfigParseFunc>
104Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
105 const ConfigParseFunc& configParse)
Junxiao Shif4cfed12018-08-22 23:26:29 +0000106 : m_keyChain(keyChain)
107 , m_face(std::move(localNfdTransport), getGlobalIoService(), m_keyChain)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400108 , m_scheduler(m_face.getIoService())
Junxiao Shif4cfed12018-08-22 23:26:29 +0000109 , m_nfdController(m_face, m_keyChain)
110 , m_fibUpdater(m_rib, m_nfdController)
111 , m_dispatcher(m_face, m_keyChain)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400112 , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher, m_scheduler)
Alexander Afanasyev31367922015-02-09 20:51:10 -0800113{
Teng Liang04d5ce62018-08-06 10:20:24 +0800114 if (s_instance != nullptr) {
115 BOOST_THROW_EXCEPTION(std::logic_error("RIB service cannot be instantiated more than once"));
116 }
117 if (&getGlobalIoService() != &getRibIoService()) {
118 BOOST_THROW_EXCEPTION(std::logic_error("RIB service must run on RIB thread"));
119 }
120 s_instance = this;
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000121
122 ConfigFile config(ConfigFile::ignoreUnknownSection);
123 config.addSectionHandler(CFG_SECTION, bind(&Service::processConfig, this, _1, _2, _3));
124 configParse(config, true);
125 configParse(config, false);
126
127 m_ribManager.registerWithNfd();
128 m_ribManager.enableLocalFields();
Alexander Afanasyev31367922015-02-09 20:51:10 -0800129}
130
Teng Liang04d5ce62018-08-06 10:20:24 +0800131Service::~Service()
132{
133 s_instance = nullptr;
134}
135
136Service&
137Service::get()
138{
139 if (s_instance == nullptr) {
140 BOOST_THROW_EXCEPTION(std::logic_error("RIB service is not instantiated"));
141 }
142 if (&getGlobalIoService() != &getRibIoService()) {
143 BOOST_THROW_EXCEPTION(std::logic_error("Must get RIB service on RIB thread"));
144 }
145 return *s_instance;
146}
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -0800147
Alexander Afanasyev31367922015-02-09 20:51:10 -0800148void
Junxiao Shif4cfed12018-08-22 23:26:29 +0000149Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
Alexander Afanasyev31367922015-02-09 20:51:10 -0800150{
Junxiao Shif4cfed12018-08-22 23:26:29 +0000151 if (isDryRun) {
152 checkConfig(section, filename);
153 }
154 else {
155 applyConfig(section, filename);
156 }
157}
Alexander Afanasyev31367922015-02-09 20:51:10 -0800158
Junxiao Shif4cfed12018-08-22 23:26:29 +0000159void
160Service::checkConfig(const ConfigSection& section, const std::string& filename)
161{
162 for (const auto& item : section) {
163 const std::string& key = item.first;
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000164 const ConfigSection& value = item.second;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000165 if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY) {
Alexander Afanasyev3c0c0352018-10-17 11:59:23 -0400166 ndn::ValidatorConfig testValidator(m_face);
167 testValidator.load(value, filename);
Junxiao Shif4cfed12018-08-22 23:26:29 +0000168 }
169 else if (key == CFG_PREFIX_PROPAGATE) {
170 // AutoPrefixPropagator does not support config dry-run
171 }
172 else if (key == CFG_READVERTISE_NLSR) {
173 ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
174 }
175 else {
176 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
177 }
178 }
179}
180
181void
182Service::applyConfig(const ConfigSection& section, const std::string& filename)
183{
184 bool wantPrefixPropagate = false;
185 bool wantReadvertiseNlsr = false;
186
187 for (const auto& item : section) {
188 const std::string& key = item.first;
189 const ConfigSection& value = item.second;
190 if (key == CFG_LOCALHOST_SECURITY) {
191 m_ribManager.applyLocalhostConfig(value, filename);
192 }
193 else if (key == CFG_LOCALHOP_SECURITY) {
194 m_ribManager.enableLocalhop(value, filename);
195 }
196 else if (key == CFG_PREFIX_PROPAGATE) {
197 if (m_prefixPropagator == nullptr) {
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400198 m_prefixPropagator = make_unique<AutoPrefixPropagator>(m_nfdController, m_keyChain,
199 m_scheduler, m_rib);
Alexander Afanasyev31367922015-02-09 20:51:10 -0800200 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000201 m_prefixPropagator->loadConfig(item.second);
202 m_prefixPropagator->enable();
203 wantPrefixPropagate = true;
204 }
205 else if (key == CFG_READVERTISE_NLSR) {
206 wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
207 }
208 else {
209 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
210 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800211 }
212
Junxiao Shif4cfed12018-08-22 23:26:29 +0000213 if (!wantPrefixPropagate && m_prefixPropagator != nullptr) {
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000214 m_prefixPropagator->disable();
215 }
216
Junxiao Shif4cfed12018-08-22 23:26:29 +0000217 if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
218 NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000219 m_readvertiseNlsr = make_unique<Readvertise>(
220 m_rib,
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400221 m_scheduler,
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000222 make_unique<ClientToNlsrReadvertisePolicy>(),
Junxiao Shif4cfed12018-08-22 23:26:29 +0000223 make_unique<NfdRibReadvertiseDestination>(m_nfdController, READVERTISE_NLSR_PREFIX, m_rib));
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000224 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000225 else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
226 NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000227 m_readvertiseNlsr.reset();
228 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800229}
230
Alexander Afanasyev31367922015-02-09 20:51:10 -0800231} // namespace rib
232} // namespace nfd