blob: 7ab98791640643272b89964aa668aa6bc6a9bb85 [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 Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::face {
Junxiao Shib8590312016-12-29 21:22:25 +000033
Davide Pesaventoa3148082018-04-12 18:21:54 -040034NFD_LOG_INIT(FaceSystem);
Junxiao Shib8590312016-12-29 21:22:25 +000035
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040036const std::string CFGSEC_FACESYSTEM = "face_system";
37const std::string CFGSEC_GENERAL = "general";
38const std::string CFGSEC_GENERAL_FQ = CFGSEC_FACESYSTEM + ".general";
39const std::string CFGSEC_NETDEVBOUND = "netdev_bound";
Junxiao Shieef49a92018-11-10 12:19:36 +000040
Junxiao Shi0ba6d642017-07-17 00:53:22 +000041FaceSystem::FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon)
Junxiao Shib8590312016-12-29 21:22:25 +000042 : m_faceTable(faceTable)
Junxiao Shi0ba6d642017-07-17 00:53:22 +000043 , m_netmon(std::move(netmon))
Junxiao Shib8590312016-12-29 21:22:25 +000044{
Junxiao Shi0ba6d642017-07-17 00:53:22 +000045 auto pfCtorParams = this->makePFCtorParams();
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040046 for (const auto& id : ProtocolFactory::listRegistered()) {
Junxiao Shib47247d2017-01-24 15:09:16 +000047 NFD_LOG_TRACE("creating factory " << id);
Junxiao Shi0ba6d642017-07-17 00:53:22 +000048 m_factories[id] = ProtocolFactory::create(id, pfCtorParams);
Junxiao Shib47247d2017-01-24 15:09:16 +000049 }
Junxiao Shieef49a92018-11-10 12:19:36 +000050
51 m_netdevBound = make_unique<NetdevBound>(pfCtorParams, *this);
Junxiao Shib8590312016-12-29 21:22:25 +000052}
53
Junxiao Shi0ba6d642017-07-17 00:53:22 +000054ProtocolFactoryCtorParams
55FaceSystem::makePFCtorParams()
56{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040057 auto addFace = [this] (auto face) { m_faceTable.add(std::move(face)); };
Junxiao Shi0ba6d642017-07-17 00:53:22 +000058 return {addFace, m_netmon};
59}
60
Junxiao Shib47247d2017-01-24 15:09:16 +000061FaceSystem::~FaceSystem() = default;
62
Junxiao Shib8590312016-12-29 21:22:25 +000063std::set<const ProtocolFactory*>
64FaceSystem::listProtocolFactories() const
65{
66 std::set<const ProtocolFactory*> factories;
Junxiao Shib47247d2017-01-24 15:09:16 +000067 for (const auto& p : m_factories) {
Junxiao Shib8590312016-12-29 21:22:25 +000068 factories.insert(p.second.get());
69 }
70 return factories;
71}
72
73ProtocolFactory*
Junxiao Shi38b24c72017-01-05 02:59:31 +000074FaceSystem::getFactoryById(const std::string& id)
Junxiao Shib8590312016-12-29 21:22:25 +000075{
Junxiao Shi38b24c72017-01-05 02:59:31 +000076 auto found = m_factories.find(id);
Junxiao Shib8590312016-12-29 21:22:25 +000077 return found == m_factories.end() ? nullptr : found->second.get();
78}
79
Junxiao Shi38b24c72017-01-05 02:59:31 +000080ProtocolFactory*
81FaceSystem::getFactoryByScheme(const std::string& scheme)
82{
83 auto found = m_factoryByScheme.find(scheme);
Junxiao Shib47247d2017-01-24 15:09:16 +000084 return found == m_factoryByScheme.end() ? nullptr : found->second;
Junxiao Shi38b24c72017-01-05 02:59:31 +000085}
86
Junxiao Shieef49a92018-11-10 12:19:36 +000087bool
88FaceSystem::hasFactoryForScheme(const std::string& scheme) const
89{
90 return m_factoryByScheme.count(scheme) > 0;
91}
92
Junxiao Shib8590312016-12-29 21:22:25 +000093void
94FaceSystem::setConfigFile(ConfigFile& configFile)
95{
Davide Pesavento412c9822021-07-02 00:21:05 -040096 configFile.addSectionHandler(CFGSEC_FACESYSTEM, [this] (auto&&... args) {
97 processConfig(std::forward<decltype(args)>(args)...);
98 });
Junxiao Shib8590312016-12-29 21:22:25 +000099}
100
101void
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400102FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string&)
Junxiao Shib8590312016-12-29 21:22:25 +0000103{
Junxiao Shi38b24c72017-01-05 02:59:31 +0000104 ConfigContext context;
105 context.isDryRun = isDryRun;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000106
Eric Newberry0c841642018-01-17 15:01:00 -0700107 // process general protocol factory config section
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400108 auto generalSection = configSection.get_child_optional(CFGSEC_GENERAL);
Eric Newberry0c841642018-01-17 15:01:00 -0700109 if (generalSection) {
110 for (const auto& pair : *generalSection) {
111 const std::string& key = pair.first;
112 if (key == "enable_congestion_marking") {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400113 context.generalConfig.wantCongestionMarking = ConfigFile::parseYesNo(pair, CFGSEC_GENERAL_FQ);
Eric Newberry0c841642018-01-17 15:01:00 -0700114 }
115 else {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400116 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_GENERAL_FQ + "." + key));
Eric Newberry0c841642018-01-17 15:01:00 -0700117 }
118 }
119 }
120
Junxiao Shieef49a92018-11-10 12:19:36 +0000121 // process in protocol factories
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400122 for (const auto& [sectionName, factory] : m_factories) {
Junxiao Shi38b24c72017-01-05 02:59:31 +0000123 std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes();
124 factory->processConfig(configSection.get_child_optional(sectionName), context);
125
126 if (!isDryRun) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400127 for (const auto& scheme : factory->getProvidedSchemes()) {
128 m_factoryByScheme[scheme] = factory.get();
Junxiao Shib47247d2017-01-24 15:09:16 +0000129 if (oldProvidedSchemes.erase(scheme) == 0) {
130 NFD_LOG_TRACE("factory " << sectionName <<
131 " provides " << scheme << " FaceUri scheme");
132 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000133 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400134 for (const auto& scheme : oldProvidedSchemes) {
Junxiao Shi38b24c72017-01-05 02:59:31 +0000135 m_factoryByScheme.erase(scheme);
Junxiao Shib47247d2017-01-24 15:09:16 +0000136 NFD_LOG_TRACE("factory " << sectionName <<
137 " no longer provides " << scheme << " FaceUri scheme");
Junxiao Shi38b24c72017-01-05 02:59:31 +0000138 }
139 }
140 }
141
Junxiao Shieef49a92018-11-10 12:19:36 +0000142 // process netdev_bound section, after factories start providing *+dev schemes
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400143 auto netdevBoundSection = configSection.get_child_optional(CFGSEC_NETDEVBOUND);
Junxiao Shieef49a92018-11-10 12:19:36 +0000144 m_netdevBound->processConfig(netdevBoundSection, context);
145
Junxiao Shi38b24c72017-01-05 02:59:31 +0000146 // process other sections
Junxiao Shib8590312016-12-29 21:22:25 +0000147 std::set<std::string> seenSections;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000148 for (const auto& pair : configSection) {
149 const std::string& sectionName = pair.first;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000150 // const ConfigSection& subSection = pair.second;
Junxiao Shib8590312016-12-29 21:22:25 +0000151
Junxiao Shi38b24c72017-01-05 02:59:31 +0000152 if (!seenSections.insert(sectionName).second) {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400153 NDN_THROW(ConfigFile::Error("Duplicate section " + CFGSEC_FACESYSTEM + "." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000154 }
155
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400156 if (sectionName == CFGSEC_GENERAL || sectionName == CFGSEC_NETDEVBOUND ||
Junxiao Shieef49a92018-11-10 12:19:36 +0000157 m_factories.count(sectionName) > 0) {
Junxiao Shi38b24c72017-01-05 02:59:31 +0000158 continue;
Junxiao Shib8590312016-12-29 21:22:25 +0000159 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000160
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400161 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_FACESYSTEM + "." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000162 }
163}
164
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400165} // namespace nfd::face