blob: f86ea25fa49e3bce5e553320562ae87f746d8a3d [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
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
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400123 for (const auto& [sectionName, factory] : m_factories) {
Junxiao Shi38b24c72017-01-05 02:59:31 +0000124 std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes();
125 factory->processConfig(configSection.get_child_optional(sectionName), context);
126
127 if (!isDryRun) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400128 for (const auto& scheme : factory->getProvidedSchemes()) {
129 m_factoryByScheme[scheme] = factory.get();
Junxiao Shib47247d2017-01-24 15:09:16 +0000130 if (oldProvidedSchemes.erase(scheme) == 0) {
131 NFD_LOG_TRACE("factory " << sectionName <<
132 " provides " << scheme << " FaceUri scheme");
133 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000134 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400135 for (const auto& scheme : oldProvidedSchemes) {
Junxiao Shi38b24c72017-01-05 02:59:31 +0000136 m_factoryByScheme.erase(scheme);
Junxiao Shib47247d2017-01-24 15:09:16 +0000137 NFD_LOG_TRACE("factory " << sectionName <<
138 " no longer provides " << scheme << " FaceUri scheme");
Junxiao Shi38b24c72017-01-05 02:59:31 +0000139 }
140 }
141 }
142
Junxiao Shieef49a92018-11-10 12:19:36 +0000143 // process netdev_bound section, after factories start providing *+dev schemes
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400144 auto netdevBoundSection = configSection.get_child_optional(CFGSEC_NETDEVBOUND);
Junxiao Shieef49a92018-11-10 12:19:36 +0000145 m_netdevBound->processConfig(netdevBoundSection, context);
146
Junxiao Shi38b24c72017-01-05 02:59:31 +0000147 // process other sections
Junxiao Shib8590312016-12-29 21:22:25 +0000148 std::set<std::string> seenSections;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000149 for (const auto& pair : configSection) {
150 const std::string& sectionName = pair.first;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000151 // const ConfigSection& subSection = pair.second;
Junxiao Shib8590312016-12-29 21:22:25 +0000152
Junxiao Shi38b24c72017-01-05 02:59:31 +0000153 if (!seenSections.insert(sectionName).second) {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400154 NDN_THROW(ConfigFile::Error("Duplicate section " + CFGSEC_FACESYSTEM + "." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000155 }
156
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400157 if (sectionName == CFGSEC_GENERAL || sectionName == CFGSEC_NETDEVBOUND ||
Junxiao Shieef49a92018-11-10 12:19:36 +0000158 m_factories.count(sectionName) > 0) {
Junxiao Shi38b24c72017-01-05 02:59:31 +0000159 continue;
Junxiao Shib8590312016-12-29 21:22:25 +0000160 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000161
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400162 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_FACESYSTEM + "." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000163 }
164}
165
Junxiao Shib8590312016-12-29 21:22:25 +0000166} // namespace face
167} // namespace nfd