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 | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | 46788bd | 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 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 48 | static const std::string CFG_SECTION = "rib"; |
| 49 | static const std::string CFG_LOCALHOST_SECURITY = "localhost_security"; |
| 50 | static const std::string CFG_LOCALHOP_SECURITY = "localhop_security"; |
| 51 | static const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate"; |
| 52 | static const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr"; |
| 53 | static const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr"; |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 54 | static const uint64_t PROPAGATE_DEFAULT_COST = 15; |
| 55 | static const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s; |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 56 | |
Alexander Afanasyev | 46788bd | 2019-02-17 14:16:32 -0500 | [diff] [blame^] | 57 | Service::Service(const ConfigSection& configSection, ndn::Face& face, ndn::KeyChain& keyChain) |
| 58 | : Service(keyChain, face, |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 59 | [&configSection] (ConfigFile& config, bool isDryRun) { |
| 60 | config.parse(configSection, isDryRun, "internal://nfd.conf"); |
| 61 | }) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 62 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 65 | template<typename ConfigParseFunc> |
Alexander Afanasyev | 46788bd | 2019-02-17 14:16:32 -0500 | [diff] [blame^] | 66 | Service::Service(ndn::KeyChain& keyChain, ndn::Face& face, const ConfigParseFunc& configParse) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 67 | : m_keyChain(keyChain) |
Alexander Afanasyev | 46788bd | 2019-02-17 14:16:32 -0500 | [diff] [blame^] | 68 | , m_face(face) |
| 69 | , m_scheduler(m_face.getIoService()) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 70 | , m_nfdController(m_face, m_keyChain) |
| 71 | , m_fibUpdater(m_rib, m_nfdController) |
| 72 | , m_dispatcher(m_face, m_keyChain) |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 73 | , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 74 | { |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 75 | 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 Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 84 | Service::~Service() |
| 85 | { |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | Service& |
| 89 | Service::get() |
| 90 | { |
Alexander Afanasyev | 46788bd | 2019-02-17 14:16:32 -0500 | [diff] [blame^] | 91 | auto node = ::ns3::NodeList::GetNode(::ns3::Simulator::GetContext()); |
| 92 | auto l3 = node->GetObject<::ns3::ndn::L3Protocol>(); |
| 93 | return l3->getRibService(); |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 94 | } |
Alexander Afanasyev | c3ea5a7 | 2015-02-12 20:14:16 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 96 | void |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 97 | Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 98 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 99 | if (isDryRun) { |
| 100 | checkConfig(section, filename); |
| 101 | } |
| 102 | else { |
| 103 | applyConfig(section, filename); |
| 104 | } |
| 105 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 106 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 107 | void |
| 108 | Service::checkConfig(const ConfigSection& section, const std::string& filename) |
| 109 | { |
| 110 | for (const auto& item : section) { |
| 111 | const std::string& key = item.first; |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 112 | const ConfigSection& value = item.second; |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 113 | if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY) { |
Alexander Afanasyev | 3c0c035 | 2018-10-17 11:59:23 -0400 | [diff] [blame] | 114 | ndn::ValidatorConfig testValidator(m_face); |
| 115 | testValidator.load(value, filename); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 116 | } |
| 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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 124 | NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key)); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | Service::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 Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 145 | wantPrefixPropagate = true; |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 146 | |
| 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 Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 168 | make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second), |
| 169 | make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters)); |
| 170 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 171 | } |
| 172 | else if (key == CFG_READVERTISE_NLSR) { |
| 173 | wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR); |
| 174 | } |
| 175 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 176 | NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key)); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 177 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 180 | if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) { |
| 181 | NFD_LOG_DEBUG("Disabling automatic prefix propagation"); |
| 182 | m_readvertisePropagation.reset(); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 185 | if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) { |
| 186 | NFD_LOG_DEBUG("Enabling readvertise-to-nlsr"); |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 187 | auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 188 | m_readvertiseNlsr = make_unique<Readvertise>( |
| 189 | m_rib, |
| 190 | make_unique<ClientToNlsrReadvertisePolicy>(), |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 191 | make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options)); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 192 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 193 | else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) { |
| 194 | NFD_LOG_DEBUG("Disabling readvertise-to-nlsr"); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 195 | m_readvertiseNlsr.reset(); |
| 196 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 199 | } // namespace rib |
| 200 | } // namespace nfd |