blob: 79cb4365deea86dccf295418661d586095c593f5 [file] [log] [blame]
Junxiao Shib8590312016-12-29 21:22:25 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2d491752017-07-14 21:32:05 +00002/*
Davide Pesavento412c9822021-07-02 00:21:05 -04003 * Copyright (c) 2014-2021, Regents of the University of California,
Junxiao Shib8590312016-12-29 21:22:25 +00004 * 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 Shib47247d2017-01-24 15:09:16 +000027#include "protocol-factory.hpp"
Junxiao Shieef49a92018-11-10 12:19:36 +000028#include "netdev-bound.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040029#include "common/global.hpp"
Junxiao Shib8590312016-12-29 21:22:25 +000030#include "fw/face-table.hpp"
31
Junxiao Shib8590312016-12-29 21:22:25 +000032namespace nfd {
33namespace face {
34
Davide Pesaventoa3148082018-04-12 18:21:54 -040035NFD_LOG_INIT(FaceSystem);
Junxiao Shib8590312016-12-29 21:22:25 +000036
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040037const std::string CFGSEC_FACESYSTEM = "face_system";
38const std::string CFGSEC_GENERAL = "general";
39const std::string CFGSEC_GENERAL_FQ = CFGSEC_FACESYSTEM + ".general";
40const std::string CFGSEC_NETDEVBOUND = "netdev_bound";
Junxiao Shieef49a92018-11-10 12:19:36 +000041
Junxiao Shi0ba6d642017-07-17 00:53:22 +000042FaceSystem::FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon)
Junxiao Shib8590312016-12-29 21:22:25 +000043 : m_faceTable(faceTable)
Junxiao Shi0ba6d642017-07-17 00:53:22 +000044 , m_netmon(std::move(netmon))
Junxiao Shib8590312016-12-29 21:22:25 +000045{
Junxiao Shi0ba6d642017-07-17 00:53:22 +000046 auto pfCtorParams = this->makePFCtorParams();
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040047 for (const auto& id : ProtocolFactory::listRegistered()) {
Junxiao Shib47247d2017-01-24 15:09:16 +000048 NFD_LOG_TRACE("creating factory " << id);
Junxiao Shi0ba6d642017-07-17 00:53:22 +000049 m_factories[id] = ProtocolFactory::create(id, pfCtorParams);
Junxiao Shib47247d2017-01-24 15:09:16 +000050 }
Junxiao Shieef49a92018-11-10 12:19:36 +000051
52 m_netdevBound = make_unique<NetdevBound>(pfCtorParams, *this);
Junxiao Shib8590312016-12-29 21:22:25 +000053}
54
Junxiao Shi0ba6d642017-07-17 00:53:22 +000055ProtocolFactoryCtorParams
56FaceSystem::makePFCtorParams()
57{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040058 auto addFace = [this] (auto face) { m_faceTable.add(std::move(face)); };
Junxiao Shi0ba6d642017-07-17 00:53:22 +000059 return {addFace, m_netmon};
60}
61
Junxiao Shib47247d2017-01-24 15:09:16 +000062FaceSystem::~FaceSystem() = default;
63
Junxiao Shib8590312016-12-29 21:22:25 +000064std::set<const ProtocolFactory*>
65FaceSystem::listProtocolFactories() const
66{
67 std::set<const ProtocolFactory*> factories;
Junxiao Shib47247d2017-01-24 15:09:16 +000068 for (const auto& p : m_factories) {
Junxiao Shib8590312016-12-29 21:22:25 +000069 factories.insert(p.second.get());
70 }
71 return factories;
72}
73
74ProtocolFactory*
Junxiao Shi38b24c72017-01-05 02:59:31 +000075FaceSystem::getFactoryById(const std::string& id)
Junxiao Shib8590312016-12-29 21:22:25 +000076{
Junxiao Shi38b24c72017-01-05 02:59:31 +000077 auto found = m_factories.find(id);
Junxiao Shib8590312016-12-29 21:22:25 +000078 return found == m_factories.end() ? nullptr : found->second.get();
79}
80
Junxiao Shi38b24c72017-01-05 02:59:31 +000081ProtocolFactory*
82FaceSystem::getFactoryByScheme(const std::string& scheme)
83{
84 auto found = m_factoryByScheme.find(scheme);
Junxiao Shib47247d2017-01-24 15:09:16 +000085 return found == m_factoryByScheme.end() ? nullptr : found->second;
Junxiao Shi38b24c72017-01-05 02:59:31 +000086}
87
Junxiao Shieef49a92018-11-10 12:19:36 +000088bool
89FaceSystem::hasFactoryForScheme(const std::string& scheme) const
90{
91 return m_factoryByScheme.count(scheme) > 0;
92}
93
Junxiao Shib8590312016-12-29 21:22:25 +000094void
95FaceSystem::setConfigFile(ConfigFile& configFile)
96{
Davide Pesavento412c9822021-07-02 00:21:05 -040097 configFile.addSectionHandler(CFGSEC_FACESYSTEM, [this] (auto&&... args) {
98 processConfig(std::forward<decltype(args)>(args)...);
99 });
Junxiao Shib8590312016-12-29 21:22:25 +0000100}
101
102void
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400103FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string&)
Junxiao Shib8590312016-12-29 21:22:25 +0000104{
Junxiao Shi38b24c72017-01-05 02:59:31 +0000105 ConfigContext context;
106 context.isDryRun = isDryRun;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000107
Eric Newberry0c841642018-01-17 15:01:00 -0700108 // process general protocol factory config section
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400109 auto generalSection = configSection.get_child_optional(CFGSEC_GENERAL);
Eric Newberry0c841642018-01-17 15:01:00 -0700110 if (generalSection) {
111 for (const auto& pair : *generalSection) {
112 const std::string& key = pair.first;
113 if (key == "enable_congestion_marking") {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400114 context.generalConfig.wantCongestionMarking = ConfigFile::parseYesNo(pair, CFGSEC_GENERAL_FQ);
Eric Newberry0c841642018-01-17 15:01:00 -0700115 }
116 else {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400117 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_GENERAL_FQ + "." + key));
Eric Newberry0c841642018-01-17 15:01:00 -0700118 }
119 }
120 }
121
Junxiao Shieef49a92018-11-10 12:19:36 +0000122 // process in protocol factories
Junxiao Shi38b24c72017-01-05 02:59:31 +0000123 for (const auto& pair : m_factories) {
124 const std::string& sectionName = pair.first;
Junxiao Shib47247d2017-01-24 15:09:16 +0000125 ProtocolFactory* factory = pair.second.get();
Junxiao Shi38b24c72017-01-05 02:59:31 +0000126
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 Shib47247d2017-01-24 15:09:16 +0000133 if (oldProvidedSchemes.erase(scheme) == 0) {
134 NFD_LOG_TRACE("factory " << sectionName <<
135 " provides " << scheme << " FaceUri scheme");
136 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000137 }
138 for (const std::string& scheme : oldProvidedSchemes) {
139 m_factoryByScheme.erase(scheme);
Junxiao Shib47247d2017-01-24 15:09:16 +0000140 NFD_LOG_TRACE("factory " << sectionName <<
141 " no longer provides " << scheme << " FaceUri scheme");
Junxiao Shi38b24c72017-01-05 02:59:31 +0000142 }
143 }
144 }
145
Junxiao Shieef49a92018-11-10 12:19:36 +0000146 // process netdev_bound section, after factories start providing *+dev schemes
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400147 auto netdevBoundSection = configSection.get_child_optional(CFGSEC_NETDEVBOUND);
Junxiao Shieef49a92018-11-10 12:19:36 +0000148 m_netdevBound->processConfig(netdevBoundSection, context);
149
Junxiao Shi38b24c72017-01-05 02:59:31 +0000150 // process other sections
Junxiao Shib8590312016-12-29 21:22:25 +0000151 std::set<std::string> seenSections;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000152 for (const auto& pair : configSection) {
153 const std::string& sectionName = pair.first;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000154 // const ConfigSection& subSection = pair.second;
Junxiao Shib8590312016-12-29 21:22:25 +0000155
Junxiao Shi38b24c72017-01-05 02:59:31 +0000156 if (!seenSections.insert(sectionName).second) {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400157 NDN_THROW(ConfigFile::Error("Duplicate section " + CFGSEC_FACESYSTEM + "." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000158 }
159
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400160 if (sectionName == CFGSEC_GENERAL || sectionName == CFGSEC_NETDEVBOUND ||
Junxiao Shieef49a92018-11-10 12:19:36 +0000161 m_factories.count(sectionName) > 0) {
Junxiao Shi38b24c72017-01-05 02:59:31 +0000162 continue;
Junxiao Shib8590312016-12-29 21:22:25 +0000163 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000164
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400165 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_FACESYSTEM + "." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000166 }
167}
168
Junxiao Shib8590312016-12-29 21:22:25 +0000169} // namespace face
170} // namespace nfd