blob: bcc8a5e19f73f93886f20dafb6bb6a1f2285b8d3 [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 Pesaventoc5a9f812023-10-17 18:01:52 -04003 * Copyright (c) 2014-2023, 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 Shieef49a92018-11-10 12:19:36 +000027#include "netdev-bound.hpp"
Davide Pesaventoc5a9f812023-10-17 18:01:52 -040028#include "protocol-factory.hpp"
Junxiao Shib8590312016-12-29 21:22:25 +000029#include "fw/face-table.hpp"
30
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::face {
Junxiao Shib8590312016-12-29 21:22:25 +000032
Davide Pesaventoa3148082018-04-12 18:21:54 -040033NFD_LOG_INIT(FaceSystem);
Junxiao Shib8590312016-12-29 21:22:25 +000034
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040035const std::string CFGSEC_FACESYSTEM = "face_system";
36const std::string CFGSEC_GENERAL = "general";
37const std::string CFGSEC_GENERAL_FQ = CFGSEC_FACESYSTEM + ".general";
38const std::string CFGSEC_NETDEVBOUND = "netdev_bound";
Junxiao Shieef49a92018-11-10 12:19:36 +000039
Junxiao Shi0ba6d642017-07-17 00:53:22 +000040FaceSystem::FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon)
Junxiao Shib8590312016-12-29 21:22:25 +000041 : m_faceTable(faceTable)
Junxiao Shi0ba6d642017-07-17 00:53:22 +000042 , m_netmon(std::move(netmon))
Junxiao Shib8590312016-12-29 21:22:25 +000043{
Junxiao Shi0ba6d642017-07-17 00:53:22 +000044 auto pfCtorParams = this->makePFCtorParams();
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040045 for (const auto& id : ProtocolFactory::listRegistered()) {
Junxiao Shib47247d2017-01-24 15:09:16 +000046 NFD_LOG_TRACE("creating factory " << id);
Junxiao Shi0ba6d642017-07-17 00:53:22 +000047 m_factories[id] = ProtocolFactory::create(id, pfCtorParams);
Junxiao Shib47247d2017-01-24 15:09:16 +000048 }
Junxiao Shieef49a92018-11-10 12:19:36 +000049
50 m_netdevBound = make_unique<NetdevBound>(pfCtorParams, *this);
Junxiao Shib8590312016-12-29 21:22:25 +000051}
52
Junxiao Shi0ba6d642017-07-17 00:53:22 +000053ProtocolFactoryCtorParams
54FaceSystem::makePFCtorParams()
55{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040056 auto addFace = [this] (auto face) { m_faceTable.add(std::move(face)); };
Junxiao Shi0ba6d642017-07-17 00:53:22 +000057 return {addFace, m_netmon};
58}
59
Junxiao Shib47247d2017-01-24 15:09:16 +000060FaceSystem::~FaceSystem() = default;
61
Junxiao Shib8590312016-12-29 21:22:25 +000062std::set<const ProtocolFactory*>
63FaceSystem::listProtocolFactories() const
64{
65 std::set<const ProtocolFactory*> factories;
Junxiao Shib47247d2017-01-24 15:09:16 +000066 for (const auto& p : m_factories) {
Junxiao Shib8590312016-12-29 21:22:25 +000067 factories.insert(p.second.get());
68 }
69 return factories;
70}
71
72ProtocolFactory*
Davide Pesaventoc5a9f812023-10-17 18:01:52 -040073FaceSystem::getFactoryById(const std::string& id) const
Junxiao Shib8590312016-12-29 21:22:25 +000074{
Junxiao Shi38b24c72017-01-05 02:59:31 +000075 auto found = m_factories.find(id);
Junxiao Shib8590312016-12-29 21:22:25 +000076 return found == m_factories.end() ? nullptr : found->second.get();
77}
78
Junxiao Shi38b24c72017-01-05 02:59:31 +000079ProtocolFactory*
Davide Pesaventoc5a9f812023-10-17 18:01:52 -040080FaceSystem::getFactoryByScheme(const std::string& scheme) const
Junxiao Shi38b24c72017-01-05 02:59:31 +000081{
82 auto found = m_factoryByScheme.find(scheme);
Junxiao Shib47247d2017-01-24 15:09:16 +000083 return found == m_factoryByScheme.end() ? nullptr : found->second;
Junxiao Shi38b24c72017-01-05 02:59:31 +000084}
85
Junxiao Shieef49a92018-11-10 12:19:36 +000086bool
87FaceSystem::hasFactoryForScheme(const std::string& scheme) const
88{
89 return m_factoryByScheme.count(scheme) > 0;
90}
91
Junxiao Shib8590312016-12-29 21:22:25 +000092void
93FaceSystem::setConfigFile(ConfigFile& configFile)
94{
Davide Pesavento412c9822021-07-02 00:21:05 -040095 configFile.addSectionHandler(CFGSEC_FACESYSTEM, [this] (auto&&... args) {
96 processConfig(std::forward<decltype(args)>(args)...);
97 });
Junxiao Shib8590312016-12-29 21:22:25 +000098}
99
100void
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400101FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string&)
Junxiao Shib8590312016-12-29 21:22:25 +0000102{
Junxiao Shi38b24c72017-01-05 02:59:31 +0000103 ConfigContext context;
104 context.isDryRun = isDryRun;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000105
Eric Newberry0c841642018-01-17 15:01:00 -0700106 // process general protocol factory config section
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400107 auto generalSection = configSection.get_child_optional(CFGSEC_GENERAL);
Eric Newberry0c841642018-01-17 15:01:00 -0700108 if (generalSection) {
109 for (const auto& pair : *generalSection) {
110 const std::string& key = pair.first;
111 if (key == "enable_congestion_marking") {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400112 context.generalConfig.wantCongestionMarking = ConfigFile::parseYesNo(pair, CFGSEC_GENERAL_FQ);
Eric Newberry0c841642018-01-17 15:01:00 -0700113 }
114 else {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400115 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_GENERAL_FQ + "." + key));
Eric Newberry0c841642018-01-17 15:01:00 -0700116 }
117 }
118 }
119
Junxiao Shieef49a92018-11-10 12:19:36 +0000120 // process in protocol factories
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400121 for (const auto& [sectionName, factory] : m_factories) {
Junxiao Shi38b24c72017-01-05 02:59:31 +0000122 std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes();
123 factory->processConfig(configSection.get_child_optional(sectionName), context);
124
125 if (!isDryRun) {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400126 for (const auto& scheme : factory->getProvidedSchemes()) {
127 m_factoryByScheme[scheme] = factory.get();
Junxiao Shib47247d2017-01-24 15:09:16 +0000128 if (oldProvidedSchemes.erase(scheme) == 0) {
129 NFD_LOG_TRACE("factory " << sectionName <<
130 " provides " << scheme << " FaceUri scheme");
131 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000132 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400133 for (const auto& scheme : oldProvidedSchemes) {
Junxiao Shi38b24c72017-01-05 02:59:31 +0000134 m_factoryByScheme.erase(scheme);
Junxiao Shib47247d2017-01-24 15:09:16 +0000135 NFD_LOG_TRACE("factory " << sectionName <<
136 " no longer provides " << scheme << " FaceUri scheme");
Junxiao Shi38b24c72017-01-05 02:59:31 +0000137 }
138 }
139 }
140
Junxiao Shieef49a92018-11-10 12:19:36 +0000141 // process netdev_bound section, after factories start providing *+dev schemes
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400142 auto netdevBoundSection = configSection.get_child_optional(CFGSEC_NETDEVBOUND);
Junxiao Shieef49a92018-11-10 12:19:36 +0000143 m_netdevBound->processConfig(netdevBoundSection, context);
144
Junxiao Shi38b24c72017-01-05 02:59:31 +0000145 // process other sections
Junxiao Shib8590312016-12-29 21:22:25 +0000146 std::set<std::string> seenSections;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000147 for (const auto& pair : configSection) {
148 const std::string& sectionName = pair.first;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000149 // const ConfigSection& subSection = pair.second;
Junxiao Shib8590312016-12-29 21:22:25 +0000150
Junxiao Shi38b24c72017-01-05 02:59:31 +0000151 if (!seenSections.insert(sectionName).second) {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400152 NDN_THROW(ConfigFile::Error("Duplicate section " + CFGSEC_FACESYSTEM + "." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000153 }
154
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400155 if (sectionName == CFGSEC_GENERAL || sectionName == CFGSEC_NETDEVBOUND ||
Junxiao Shieef49a92018-11-10 12:19:36 +0000156 m_factories.count(sectionName) > 0) {
Junxiao Shi38b24c72017-01-05 02:59:31 +0000157 continue;
Junxiao Shib8590312016-12-29 21:22:25 +0000158 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000159
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400160 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_FACESYSTEM + "." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000161 }
162}
163
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400164} // namespace nfd::face