Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 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 | |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 26 | #include "service.hpp" |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 27 | |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 28 | #include "fib-updater.hpp" |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 29 | #include "readvertise/client-to-nlsr-readvertise-policy.hpp" |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 30 | #include "readvertise/host-to-gateway-readvertise-policy.hpp" |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 31 | #include "readvertise/nfd-rib-readvertise-destination.hpp" |
| 32 | #include "readvertise/readvertise.hpp" |
| 33 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 34 | #include "common/global.hpp" |
| 35 | #include "common/logger.hpp" |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 8e61b80 | 2019-02-17 14:16:32 -0500 | [diff] [blame] | 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" |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 42 | |
| 43 | namespace nfd { |
| 44 | namespace rib { |
| 45 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 46 | NFD_LOG_INIT(RibService); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 47 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 48 | const std::string CFG_RIB = "rib"; |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 49 | const std::string CFG_LOCALHOST_SECURITY = "localhost_security"; |
| 50 | const std::string CFG_LOCALHOP_SECURITY = "localhop_security"; |
Teng Liang | 18c2b29 | 2019-10-18 14:31:04 -0700 | [diff] [blame] | 51 | const std::string CFG_PA_VALIDATION = "prefix_announcement_validation"; |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 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; |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 57 | |
Alexander Afanasyev | 8e61b80 | 2019-02-17 14:16:32 -0500 | [diff] [blame] | 58 | Service::Service(const ConfigSection& configSection, ndn::Face& face, ndn::KeyChain& keyChain) |
| 59 | : Service(keyChain, face, |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 60 | [&configSection] (ConfigFile& config, bool isDryRun) { |
| 61 | config.parse(configSection, isDryRun, "internal://nfd.conf"); |
| 62 | }) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 63 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 66 | template<typename ConfigParseFunc> |
Alexander Afanasyev | 8e61b80 | 2019-02-17 14:16:32 -0500 | [diff] [blame] | 67 | Service::Service(ndn::KeyChain& keyChain, ndn::Face& face, const ConfigParseFunc& configParse) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 68 | : m_keyChain(keyChain) |
Alexander Afanasyev | 8e61b80 | 2019-02-17 14:16:32 -0500 | [diff] [blame] | 69 | , m_face(face) |
| 70 | , m_scheduler(m_face.getIoService()) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 71 | , m_nfdController(m_face, m_keyChain) |
| 72 | , m_fibUpdater(m_rib, m_nfdController) |
| 73 | , m_dispatcher(m_face, m_keyChain) |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 74 | , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 75 | { |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 76 | ConfigFile config(ConfigFile::ignoreUnknownSection); |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 77 | config.addSectionHandler(CFG_RIB, [this] (auto&&... args) { |
| 78 | processConfig(std::forward<decltype(args)>(args)...); |
| 79 | }); |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 80 | configParse(config, true); |
| 81 | configParse(config, false); |
| 82 | |
| 83 | m_ribManager.registerWithNfd(); |
| 84 | m_ribManager.enableLocalFields(); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 87 | Service::~Service() |
| 88 | { |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | Service& |
| 92 | Service::get() |
| 93 | { |
Alexander Afanasyev | 8e61b80 | 2019-02-17 14:16:32 -0500 | [diff] [blame] | 94 | auto node = ::ns3::NodeList::GetNode(::ns3::Simulator::GetContext()); |
| 95 | auto l3 = node->GetObject<::ns3::ndn::L3Protocol>(); |
| 96 | return l3->getRibService(); |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 97 | } |
Alexander Afanasyev | c3ea5a7 | 2015-02-12 20:14:16 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 99 | void |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 100 | Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 101 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 102 | if (isDryRun) { |
| 103 | checkConfig(section, filename); |
| 104 | } |
| 105 | else { |
| 106 | applyConfig(section, filename); |
| 107 | } |
| 108 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 109 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 110 | void |
| 111 | Service::checkConfig(const ConfigSection& section, const std::string& filename) |
| 112 | { |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 113 | bool hasLocalhop = false; |
| 114 | bool hasPropagate = false; |
| 115 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 116 | for (const auto& item : section) { |
| 117 | const std::string& key = item.first; |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 118 | const ConfigSection& value = item.second; |
Jongseok Lee | 854866f | 2020-03-10 19:16:25 -0400 | [diff] [blame] | 119 | 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 Afanasyev | 3c0c035 | 2018-10-17 11:59:23 -0400 | [diff] [blame] | 125 | ndn::ValidatorConfig testValidator(m_face); |
| 126 | testValidator.load(value, filename); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 127 | } |
| 128 | else if (key == CFG_PREFIX_PROPAGATE) { |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 129 | hasPropagate = true; |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 130 | // AutoPrefixPropagator does not support config dry-run |
| 131 | } |
| 132 | else if (key == CFG_READVERTISE_NLSR) { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 133 | ConfigFile::parseYesNo(item, CFG_RIB + "." + CFG_READVERTISE_NLSR); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 134 | } |
| 135 | else { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 136 | NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key)); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 139 | |
| 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 Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void |
| 147 | Service::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 Liang | 18c2b29 | 2019-10-18 14:31:04 -0700 | [diff] [blame] | 161 | else if (key == CFG_PA_VALIDATION) { |
| 162 | m_ribManager.applyPaConfig(value, filename); |
| 163 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 164 | else if (key == CFG_PREFIX_PROPAGATE) { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 165 | wantPrefixPropagate = true; |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 166 | |
| 167 | if (!m_readvertisePropagation) { |
| 168 | NFD_LOG_DEBUG("Enabling automatic prefix propagation"); |
| 169 | |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 170 | auto cost = item.second.get_optional<uint64_t>("cost"); |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 171 | auto parameters = ndn::nfd::ControlParameters() |
| 172 | .setCost(cost.value_or(PROPAGATE_DEFAULT_COST)) |
| 173 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT); |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 174 | |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 175 | auto timeout = item.second.get_optional<uint64_t>("timeout"); |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 176 | auto options = ndn::nfd::CommandOptions() |
| 177 | .setPrefix(RibManager::LOCALHOP_TOP_PREFIX) |
| 178 | .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT); |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 179 | |
| 180 | m_readvertisePropagation = make_unique<Readvertise>( |
| 181 | m_rib, |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 182 | make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second), |
| 183 | make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters)); |
| 184 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 185 | } |
| 186 | else if (key == CFG_READVERTISE_NLSR) { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 187 | wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_RIB + "." + CFG_READVERTISE_NLSR); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 188 | } |
| 189 | else { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 190 | NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key)); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 191 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 194 | if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) { |
| 195 | NFD_LOG_DEBUG("Disabling automatic prefix propagation"); |
| 196 | m_readvertisePropagation.reset(); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 199 | if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) { |
| 200 | NFD_LOG_DEBUG("Enabling readvertise-to-nlsr"); |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 201 | auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 202 | m_readvertiseNlsr = make_unique<Readvertise>( |
| 203 | m_rib, |
| 204 | make_unique<ClientToNlsrReadvertisePolicy>(), |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 205 | make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options)); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 206 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 207 | else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) { |
| 208 | NFD_LOG_DEBUG("Disabling readvertise-to-nlsr"); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 209 | m_readvertiseNlsr.reset(); |
| 210 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 213 | } // namespace rib |
| 214 | } // namespace nfd |