blob: 1a8c6c724c9865e3923365c36de732f2adede7eb [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 Pesavento412c9822021-07-02 00:21:05 -04003 * Copyright (c) 2014-2021, 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 Afanasyev8e61b802019-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
Davide Pesavento412c9822021-07-02 00:21:05 -040048const std::string CFG_RIB = "rib";
Wenkai Zheng6598fb02019-09-08 18:03:46 -070049const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
50const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
Teng Liang18c2b292019-10-18 14:31:04 -070051const std::string CFG_PA_VALIDATION = "prefix_announcement_validation";
Wenkai Zheng6598fb02019-09-08 18:03:46 -070052const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
53const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
54const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
55const uint64_t PROPAGATE_DEFAULT_COST = 15;
56const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
Junxiao Shif4cfed12018-08-22 23:26:29 +000057
Alexander Afanasyev8e61b802019-02-17 14:16:32 -050058Service::Service(const ConfigSection& configSection, ndn::Face& face, ndn::KeyChain& keyChain)
59 : Service(keyChain, face,
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000060 [&configSection] (ConfigFile& config, bool isDryRun) {
61 config.parse(configSection, isDryRun, "internal://nfd.conf");
62 })
Junxiao Shif4cfed12018-08-22 23:26:29 +000063{
Junxiao Shif4cfed12018-08-22 23:26:29 +000064}
65
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000066template<typename ConfigParseFunc>
Alexander Afanasyev8e61b802019-02-17 14:16:32 -050067Service::Service(ndn::KeyChain& keyChain, ndn::Face& face, const ConfigParseFunc& configParse)
Junxiao Shif4cfed12018-08-22 23:26:29 +000068 : m_keyChain(keyChain)
Alexander Afanasyev8e61b802019-02-17 14:16:32 -050069 , m_face(face)
70 , m_scheduler(m_face.getIoService())
Junxiao Shif4cfed12018-08-22 23:26:29 +000071 , m_nfdController(m_face, m_keyChain)
72 , m_fibUpdater(m_rib, m_nfdController)
73 , m_dispatcher(m_face, m_keyChain)
Davide Pesavento0a71dd32019-03-17 20:36:18 -040074 , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
Alexander Afanasyev31367922015-02-09 20:51:10 -080075{
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000076 ConfigFile config(ConfigFile::ignoreUnknownSection);
Davide Pesavento412c9822021-07-02 00:21:05 -040077 config.addSectionHandler(CFG_RIB, [this] (auto&&... args) {
78 processConfig(std::forward<decltype(args)>(args)...);
79 });
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000080 configParse(config, true);
81 configParse(config, false);
82
83 m_ribManager.registerWithNfd();
84 m_ribManager.enableLocalFields();
Alexander Afanasyev31367922015-02-09 20:51:10 -080085}
86
Teng Liang04d5ce62018-08-06 10:20:24 +080087Service::~Service()
88{
Teng Liang04d5ce62018-08-06 10:20:24 +080089}
90
91Service&
92Service::get()
93{
Alexander Afanasyev8e61b802019-02-17 14:16:32 -050094 auto node = ::ns3::NodeList::GetNode(::ns3::Simulator::GetContext());
95 auto l3 = node->GetObject<::ns3::ndn::L3Protocol>();
96 return l3->getRibService();
Teng Liang04d5ce62018-08-06 10:20:24 +080097}
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -080098
Alexander Afanasyev31367922015-02-09 20:51:10 -080099void
Junxiao Shif4cfed12018-08-22 23:26:29 +0000100Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
Alexander Afanasyev31367922015-02-09 20:51:10 -0800101{
Junxiao Shif4cfed12018-08-22 23:26:29 +0000102 if (isDryRun) {
103 checkConfig(section, filename);
104 }
105 else {
106 applyConfig(section, filename);
107 }
108}
Alexander Afanasyev31367922015-02-09 20:51:10 -0800109
Junxiao Shif4cfed12018-08-22 23:26:29 +0000110void
111Service::checkConfig(const ConfigSection& section, const std::string& filename)
112{
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700113 bool hasLocalhop = false;
114 bool hasPropagate = false;
115
Junxiao Shif4cfed12018-08-22 23:26:29 +0000116 for (const auto& item : section) {
117 const std::string& key = item.first;
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000118 const ConfigSection& value = item.second;
Jongseok Lee854866f2020-03-10 19:16:25 -0400119 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;
Alexander Afanasyev3c0c0352018-10-17 11:59:23 -0400125 ndn::ValidatorConfig testValidator(m_face);
126 testValidator.load(value, filename);
Junxiao Shif4cfed12018-08-22 23:26:29 +0000127 }
128 else if (key == CFG_PREFIX_PROPAGATE) {
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700129 hasPropagate = true;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000130 // AutoPrefixPropagator does not support config dry-run
131 }
132 else if (key == CFG_READVERTISE_NLSR) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400133 ConfigFile::parseYesNo(item, CFG_RIB + "." + CFG_READVERTISE_NLSR);
Junxiao Shif4cfed12018-08-22 23:26:29 +0000134 }
135 else {
Davide Pesavento412c9822021-07-02 00:21:05 -0400136 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000137 }
138 }
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700139
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 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000144}
145
146void
147Service::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 }
Teng Liang18c2b292019-10-18 14:31:04 -0700161 else if (key == CFG_PA_VALIDATION) {
162 m_ribManager.applyPaConfig(value, filename);
163 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000164 else if (key == CFG_PREFIX_PROPAGATE) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000165 wantPrefixPropagate = true;
Yanbiao Lif48d0802018-06-01 03:00:02 -0700166
167 if (!m_readvertisePropagation) {
168 NFD_LOG_DEBUG("Enabling automatic prefix propagation");
169
Yanbiao Lif48d0802018-06-01 03:00:02 -0700170 auto cost = item.second.get_optional<uint64_t>("cost");
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700171 auto parameters = ndn::nfd::ControlParameters()
172 .setCost(cost.value_or(PROPAGATE_DEFAULT_COST))
173 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
Yanbiao Lif48d0802018-06-01 03:00:02 -0700174
Yanbiao Lif48d0802018-06-01 03:00:02 -0700175 auto timeout = item.second.get_optional<uint64_t>("timeout");
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700176 auto options = ndn::nfd::CommandOptions()
177 .setPrefix(RibManager::LOCALHOP_TOP_PREFIX)
178 .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT);
Yanbiao Lif48d0802018-06-01 03:00:02 -0700179
180 m_readvertisePropagation = make_unique<Readvertise>(
181 m_rib,
Yanbiao Lif48d0802018-06-01 03:00:02 -0700182 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
183 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
184 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000185 }
186 else if (key == CFG_READVERTISE_NLSR) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400187 wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_RIB + "." + CFG_READVERTISE_NLSR);
Junxiao Shif4cfed12018-08-22 23:26:29 +0000188 }
189 else {
Davide Pesavento412c9822021-07-02 00:21:05 -0400190 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000191 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800192 }
193
Yanbiao Lif48d0802018-06-01 03:00:02 -0700194 if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
195 NFD_LOG_DEBUG("Disabling automatic prefix propagation");
196 m_readvertisePropagation.reset();
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000197 }
198
Junxiao Shif4cfed12018-08-22 23:26:29 +0000199 if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
200 NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
Yanbiao Lif48d0802018-06-01 03:00:02 -0700201 auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000202 m_readvertiseNlsr = make_unique<Readvertise>(
203 m_rib,
204 make_unique<ClientToNlsrReadvertisePolicy>(),
Yanbiao Lif48d0802018-06-01 03:00:02 -0700205 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000206 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000207 else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
208 NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000209 m_readvertiseNlsr.reset();
210 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800211}
212
Alexander Afanasyev31367922015-02-09 20:51:10 -0800213} // namespace rib
214} // namespace nfd