Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [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, |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [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 | |
| 26 | #include "face-system.hpp" |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 27 | #include "protocol-factory.hpp" |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 28 | #include "netdev-bound.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 29 | #include "common/global.hpp" |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 30 | #include "fw/face-table.hpp" |
| 31 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 32 | namespace nfd { |
| 33 | namespace face { |
| 34 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 35 | NFD_LOG_INIT(FaceSystem); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 36 | |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 37 | const std::string CFGSEC_FACESYSTEM = "face_system"; |
| 38 | const std::string CFGSEC_GENERAL = "general"; |
| 39 | const std::string CFGSEC_GENERAL_FQ = CFGSEC_FACESYSTEM + ".general"; |
| 40 | const std::string CFGSEC_NETDEVBOUND = "netdev_bound"; |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 41 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 42 | FaceSystem::FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon) |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 43 | : m_faceTable(faceTable) |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 44 | , m_netmon(std::move(netmon)) |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 45 | { |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 46 | auto pfCtorParams = this->makePFCtorParams(); |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 47 | for (const auto& id : ProtocolFactory::listRegistered()) { |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 48 | NFD_LOG_TRACE("creating factory " << id); |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 49 | m_factories[id] = ProtocolFactory::create(id, pfCtorParams); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 50 | } |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 51 | |
| 52 | m_netdevBound = make_unique<NetdevBound>(pfCtorParams, *this); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 55 | ProtocolFactoryCtorParams |
| 56 | FaceSystem::makePFCtorParams() |
| 57 | { |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 58 | auto addFace = [this] (auto face) { m_faceTable.add(std::move(face)); }; |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 59 | return {addFace, m_netmon}; |
| 60 | } |
| 61 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 62 | FaceSystem::~FaceSystem() = default; |
| 63 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 64 | std::set<const ProtocolFactory*> |
| 65 | FaceSystem::listProtocolFactories() const |
| 66 | { |
| 67 | std::set<const ProtocolFactory*> factories; |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 68 | for (const auto& p : m_factories) { |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 69 | factories.insert(p.second.get()); |
| 70 | } |
| 71 | return factories; |
| 72 | } |
| 73 | |
| 74 | ProtocolFactory* |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 75 | FaceSystem::getFactoryById(const std::string& id) |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 76 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 77 | auto found = m_factories.find(id); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 78 | return found == m_factories.end() ? nullptr : found->second.get(); |
| 79 | } |
| 80 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 81 | ProtocolFactory* |
| 82 | FaceSystem::getFactoryByScheme(const std::string& scheme) |
| 83 | { |
| 84 | auto found = m_factoryByScheme.find(scheme); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 85 | return found == m_factoryByScheme.end() ? nullptr : found->second; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 88 | bool |
| 89 | FaceSystem::hasFactoryForScheme(const std::string& scheme) const |
| 90 | { |
| 91 | return m_factoryByScheme.count(scheme) > 0; |
| 92 | } |
| 93 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 94 | void |
| 95 | FaceSystem::setConfigFile(ConfigFile& configFile) |
| 96 | { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 97 | configFile.addSectionHandler(CFGSEC_FACESYSTEM, [this] (auto&&... args) { |
| 98 | processConfig(std::forward<decltype(args)>(args)...); |
| 99 | }); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 103 | FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string&) |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 104 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 105 | ConfigContext context; |
| 106 | context.isDryRun = isDryRun; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 107 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 108 | // process general protocol factory config section |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 109 | auto generalSection = configSection.get_child_optional(CFGSEC_GENERAL); |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 110 | if (generalSection) { |
| 111 | for (const auto& pair : *generalSection) { |
| 112 | const std::string& key = pair.first; |
| 113 | if (key == "enable_congestion_marking") { |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 114 | context.generalConfig.wantCongestionMarking = ConfigFile::parseYesNo(pair, CFGSEC_GENERAL_FQ); |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 115 | } |
| 116 | else { |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 117 | NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_GENERAL_FQ + "." + key)); |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 122 | // process in protocol factories |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 123 | for (const auto& pair : m_factories) { |
| 124 | const std::string& sectionName = pair.first; |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 125 | ProtocolFactory* factory = pair.second.get(); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 126 | |
| 127 | std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes(); |
| 128 | factory->processConfig(configSection.get_child_optional(sectionName), context); |
| 129 | |
| 130 | if (!isDryRun) { |
| 131 | for (const std::string& scheme : factory->getProvidedSchemes()) { |
| 132 | m_factoryByScheme[scheme] = factory; |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 133 | if (oldProvidedSchemes.erase(scheme) == 0) { |
| 134 | NFD_LOG_TRACE("factory " << sectionName << |
| 135 | " provides " << scheme << " FaceUri scheme"); |
| 136 | } |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 137 | } |
| 138 | for (const std::string& scheme : oldProvidedSchemes) { |
| 139 | m_factoryByScheme.erase(scheme); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 140 | NFD_LOG_TRACE("factory " << sectionName << |
| 141 | " no longer provides " << scheme << " FaceUri scheme"); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 146 | // process netdev_bound section, after factories start providing *+dev schemes |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 147 | auto netdevBoundSection = configSection.get_child_optional(CFGSEC_NETDEVBOUND); |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 148 | m_netdevBound->processConfig(netdevBoundSection, context); |
| 149 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 150 | // process other sections |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 151 | std::set<std::string> seenSections; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 152 | for (const auto& pair : configSection) { |
| 153 | const std::string& sectionName = pair.first; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 154 | // const ConfigSection& subSection = pair.second; |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 155 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 156 | if (!seenSections.insert(sectionName).second) { |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 157 | NDN_THROW(ConfigFile::Error("Duplicate section " + CFGSEC_FACESYSTEM + "." + sectionName)); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 160 | if (sectionName == CFGSEC_GENERAL || sectionName == CFGSEC_NETDEVBOUND || |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 161 | m_factories.count(sectionName) > 0) { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 162 | continue; |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 163 | } |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 164 | |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 165 | NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_FACESYSTEM + "." + sectionName)); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 169 | } // namespace face |
| 170 | } // namespace nfd |