blob: f98d771acf581f5ba66f6a774fc9f153b48d78c9 [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 Afanasyev41b563b2019-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
Wenkai Zheng6598fb02019-09-08 18:03:46 -070048const std::string CFG_SECTION = "rib";
49const 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 Afanasyev41b563b2019-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 Afanasyev41b563b2019-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 Afanasyev41b563b2019-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);
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();
Alexander Afanasyev31367922015-02-09 20:51:10 -080083}
84
Teng Liang04d5ce62018-08-06 10:20:24 +080085Service::~Service()
86{
Teng Liang04d5ce62018-08-06 10:20:24 +080087}
88
89Service&
90Service::get()
91{
Alexander Afanasyev41b563b2019-02-17 14:16:32 -050092 auto node = ::ns3::NodeList::GetNode(::ns3::Simulator::GetContext());
93 auto l3 = node->GetObject<::ns3::ndn::L3Protocol>();
94 return l3->getRibService();
Teng Liang04d5ce62018-08-06 10:20:24 +080095}
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -080096
Alexander Afanasyev31367922015-02-09 20:51:10 -080097void
Junxiao Shif4cfed12018-08-22 23:26:29 +000098Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
Alexander Afanasyev31367922015-02-09 20:51:10 -080099{
Junxiao Shif4cfed12018-08-22 23:26:29 +0000100 if (isDryRun) {
101 checkConfig(section, filename);
102 }
103 else {
104 applyConfig(section, filename);
105 }
106}
Alexander Afanasyev31367922015-02-09 20:51:10 -0800107
Junxiao Shif4cfed12018-08-22 23:26:29 +0000108void
109Service::checkConfig(const ConfigSection& section, const std::string& filename)
110{
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700111 bool hasLocalhop = false;
112 bool hasPropagate = false;
113
Junxiao Shif4cfed12018-08-22 23:26:29 +0000114 for (const auto& item : section) {
115 const std::string& key = item.first;
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000116 const ConfigSection& value = item.second;
Teng Liang18c2b292019-10-18 14:31:04 -0700117 if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY || key == CFG_PA_VALIDATION) {
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700118 hasLocalhop = key == CFG_LOCALHOP_SECURITY;
Alexander Afanasyev3c0c0352018-10-17 11:59:23 -0400119 ndn::ValidatorConfig testValidator(m_face);
120 testValidator.load(value, filename);
Junxiao Shif4cfed12018-08-22 23:26:29 +0000121 }
122 else if (key == CFG_PREFIX_PROPAGATE) {
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700123 hasPropagate = true;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000124 // AutoPrefixPropagator does not support config dry-run
125 }
126 else if (key == CFG_READVERTISE_NLSR) {
127 ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
128 }
129 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500130 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000131 }
132 }
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700133
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 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000138}
139
140void
141Service::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 }
Teng Liang18c2b292019-10-18 14:31:04 -0700155 else if (key == CFG_PA_VALIDATION) {
156 m_ribManager.applyPaConfig(value, filename);
157 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000158 else if (key == CFG_PREFIX_PROPAGATE) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000159 wantPrefixPropagate = true;
Yanbiao Lif48d0802018-06-01 03:00:02 -0700160
161 if (!m_readvertisePropagation) {
162 NFD_LOG_DEBUG("Enabling automatic prefix propagation");
163
Yanbiao Lif48d0802018-06-01 03:00:02 -0700164 auto cost = item.second.get_optional<uint64_t>("cost");
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700165 auto parameters = ndn::nfd::ControlParameters()
166 .setCost(cost.value_or(PROPAGATE_DEFAULT_COST))
167 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
Yanbiao Lif48d0802018-06-01 03:00:02 -0700168
Yanbiao Lif48d0802018-06-01 03:00:02 -0700169 auto timeout = item.second.get_optional<uint64_t>("timeout");
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700170 auto options = ndn::nfd::CommandOptions()
171 .setPrefix(RibManager::LOCALHOP_TOP_PREFIX)
172 .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT);
Yanbiao Lif48d0802018-06-01 03:00:02 -0700173
174 m_readvertisePropagation = make_unique<Readvertise>(
175 m_rib,
Yanbiao Lif48d0802018-06-01 03:00:02 -0700176 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
177 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
178 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000179 }
180 else if (key == CFG_READVERTISE_NLSR) {
181 wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
182 }
183 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500184 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000185 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800186 }
187
Yanbiao Lif48d0802018-06-01 03:00:02 -0700188 if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
189 NFD_LOG_DEBUG("Disabling automatic prefix propagation");
190 m_readvertisePropagation.reset();
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000191 }
192
Junxiao Shif4cfed12018-08-22 23:26:29 +0000193 if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
194 NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
Yanbiao Lif48d0802018-06-01 03:00:02 -0700195 auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000196 m_readvertiseNlsr = make_unique<Readvertise>(
197 m_rib,
198 make_unique<ClientToNlsrReadvertisePolicy>(),
Yanbiao Lif48d0802018-06-01 03:00:02 -0700199 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000200 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000201 else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
202 NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000203 m_readvertiseNlsr.reset();
204 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800205}
206
Alexander Afanasyev31367922015-02-09 20:51:10 -0800207} // namespace rib
208} // namespace nfd