blob: befefb72bb895ecd129b21aa86abdf12fd4f7d02 [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/*
Davide Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, 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
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000028#include "fib-updater.hpp"
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000029#include "readvertise/client-to-nlsr-readvertise-policy.hpp"
Yanbiao Lif48d0802018-06-01 03:00:02 -070030#include "readvertise/host-to-gateway-readvertise-policy.hpp"
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000031#include "readvertise/nfd-rib-readvertise-destination.hpp"
32#include "readvertise/readvertise.hpp"
33
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040034#include "common/global.hpp"
35#include "common/logger.hpp"
Alexander Afanasyev31367922015-02-09 20:51:10 -080036
Alexander Afanasyev46788bd2019-02-17 14:16:32 -050037#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"
Alexander Afanasyev31367922015-02-09 20:51:10 -080042
43namespace nfd {
44namespace rib {
45
Junxiao Shif4cfed12018-08-22 23:26:29 +000046NFD_LOG_INIT(RibService);
Alexander Afanasyev31367922015-02-09 20:51:10 -080047
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";
Yanbiao Lif48d0802018-06-01 03:00:02 -070054static const uint64_t PROPAGATE_DEFAULT_COST = 15;
55static const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
Junxiao Shif4cfed12018-08-22 23:26:29 +000056
Alexander Afanasyev46788bd2019-02-17 14:16:32 -050057Service::Service(const ConfigSection& configSection, ndn::Face& face, ndn::KeyChain& keyChain)
58 : Service(keyChain, face,
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000059 [&configSection] (ConfigFile& config, bool isDryRun) {
60 config.parse(configSection, isDryRun, "internal://nfd.conf");
61 })
Junxiao Shif4cfed12018-08-22 23:26:29 +000062{
Junxiao Shif4cfed12018-08-22 23:26:29 +000063}
64
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000065template<typename ConfigParseFunc>
Alexander Afanasyev46788bd2019-02-17 14:16:32 -050066Service::Service(ndn::KeyChain& keyChain, ndn::Face& face, const ConfigParseFunc& configParse)
Junxiao Shif4cfed12018-08-22 23:26:29 +000067 : m_keyChain(keyChain)
Alexander Afanasyev46788bd2019-02-17 14:16:32 -050068 , m_face(face)
69 , m_scheduler(m_face.getIoService())
Junxiao Shif4cfed12018-08-22 23:26:29 +000070 , m_nfdController(m_face, m_keyChain)
71 , m_fibUpdater(m_rib, m_nfdController)
72 , m_dispatcher(m_face, m_keyChain)
Davide Pesavento0a71dd32019-03-17 20:36:18 -040073 , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
Alexander Afanasyev31367922015-02-09 20:51:10 -080074{
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000075 ConfigFile config(ConfigFile::ignoreUnknownSection);
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();
Alexander Afanasyev31367922015-02-09 20:51:10 -080082}
83
Teng Liang04d5ce62018-08-06 10:20:24 +080084Service::~Service()
85{
Teng Liang04d5ce62018-08-06 10:20:24 +080086}
87
88Service&
89Service::get()
90{
Alexander Afanasyev46788bd2019-02-17 14:16:32 -050091 auto node = ::ns3::NodeList::GetNode(::ns3::Simulator::GetContext());
92 auto l3 = node->GetObject<::ns3::ndn::L3Protocol>();
93 return l3->getRibService();
Teng Liang04d5ce62018-08-06 10:20:24 +080094}
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -080095
Alexander Afanasyev31367922015-02-09 20:51:10 -080096void
Junxiao Shif4cfed12018-08-22 23:26:29 +000097Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
Alexander Afanasyev31367922015-02-09 20:51:10 -080098{
Junxiao Shif4cfed12018-08-22 23:26:29 +000099 if (isDryRun) {
100 checkConfig(section, filename);
101 }
102 else {
103 applyConfig(section, filename);
104 }
105}
Alexander Afanasyev31367922015-02-09 20:51:10 -0800106
Junxiao Shif4cfed12018-08-22 23:26:29 +0000107void
108Service::checkConfig(const ConfigSection& section, const std::string& filename)
109{
110 for (const auto& item : section) {
111 const std::string& key = item.first;
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000112 const ConfigSection& value = item.second;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000113 if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY) {
Alexander Afanasyev3c0c0352018-10-17 11:59:23 -0400114 ndn::ValidatorConfig testValidator(m_face);
115 testValidator.load(value, filename);
Junxiao Shif4cfed12018-08-22 23:26:29 +0000116 }
117 else if (key == CFG_PREFIX_PROPAGATE) {
118 // AutoPrefixPropagator does not support config dry-run
119 }
120 else if (key == CFG_READVERTISE_NLSR) {
121 ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
122 }
123 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500124 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000125 }
126 }
127}
128
129void
130Service::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) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000145 wantPrefixPropagate = true;
Yanbiao Lif48d0802018-06-01 03:00:02 -0700146
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()
159 .setPrefix(RibManager::LOCALHOP_TOP_PREFIX)
160 .setTimeout(PROPAGATE_DEFAULT_TIMEOUT);
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,
Yanbiao Lif48d0802018-06-01 03:00:02 -0700168 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
169 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
170 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000171 }
172 else if (key == CFG_READVERTISE_NLSR) {
173 wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
174 }
175 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500176 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000177 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800178 }
179
Yanbiao Lif48d0802018-06-01 03:00:02 -0700180 if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
181 NFD_LOG_DEBUG("Disabling automatic prefix propagation");
182 m_readvertisePropagation.reset();
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000183 }
184
Junxiao Shif4cfed12018-08-22 23:26:29 +0000185 if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
186 NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
Yanbiao Lif48d0802018-06-01 03:00:02 -0700187 auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000188 m_readvertiseNlsr = make_unique<Readvertise>(
189 m_rib,
190 make_unique<ClientToNlsrReadvertisePolicy>(),
Yanbiao Lif48d0802018-06-01 03:00:02 -0700191 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000192 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000193 else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
194 NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000195 m_readvertiseNlsr.reset();
196 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800197}
198
Alexander Afanasyev31367922015-02-09 20:51:10 -0800199} // namespace rib
200} // namespace nfd