blob: 260eea7c42c2220b4c0c170293c8727ea65f8aec [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 Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, 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 Pesavento3dade002019-03-19 11:29:56 -060029#include "daemon/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
Junxiao Shieef49a92018-11-10 12:19:36 +000037static const std::string SECTION_GENERAL = "general";
38static const std::string SECTION_NETDEVBOUND = "netdev_bound";
39
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();
Junxiao Shib47247d2017-01-24 15:09:16 +000045 for (const std::string& id : ProtocolFactory::listRegistered()) {
46 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 Pesaventoe4b22382018-06-10 14:37:24 -040056 auto addFace = [&ft = m_faceTable] (auto face) { ft.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*
Junxiao Shi38b24c72017-01-05 02:59:31 +000073FaceSystem::getFactoryById(const std::string& id)
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*
80FaceSystem::getFactoryByScheme(const std::string& scheme)
81{
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{
95 configFile.addSectionHandler("face_system", bind(&FaceSystem::processConfig, this, _1, _2, _3));
96}
97
98void
99FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename)
100{
Junxiao Shi38b24c72017-01-05 02:59:31 +0000101 ConfigContext context;
102 context.isDryRun = isDryRun;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000103
Eric Newberry0c841642018-01-17 15:01:00 -0700104 // process general protocol factory config section
Junxiao Shieef49a92018-11-10 12:19:36 +0000105 auto generalSection = configSection.get_child_optional(SECTION_GENERAL);
Eric Newberry0c841642018-01-17 15:01:00 -0700106 if (generalSection) {
107 for (const auto& pair : *generalSection) {
108 const std::string& key = pair.first;
109 if (key == "enable_congestion_marking") {
Eric Newberry0c841642018-01-17 15:01:00 -0700110 context.generalConfig.wantCongestionMarking = ConfigFile::parseYesNo(pair, "face_system.general");
111 }
112 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500113 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.general." + key));
Eric Newberry0c841642018-01-17 15:01:00 -0700114 }
115 }
116 }
117
Junxiao Shieef49a92018-11-10 12:19:36 +0000118 // process in protocol factories
Junxiao Shi38b24c72017-01-05 02:59:31 +0000119 for (const auto& pair : m_factories) {
120 const std::string& sectionName = pair.first;
Junxiao Shib47247d2017-01-24 15:09:16 +0000121 ProtocolFactory* factory = pair.second.get();
Junxiao Shi38b24c72017-01-05 02:59:31 +0000122
123 std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes();
124 factory->processConfig(configSection.get_child_optional(sectionName), context);
125
126 if (!isDryRun) {
127 for (const std::string& scheme : factory->getProvidedSchemes()) {
128 m_factoryByScheme[scheme] = factory;
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 }
134 for (const std::string& scheme : oldProvidedSchemes) {
135 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
143 auto netdevBoundSection = configSection.get_child_optional(SECTION_NETDEVBOUND);
144 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 Pesavento19779d82019-02-14 13:40:04 -0500153 NDN_THROW(ConfigFile::Error("Duplicate section face_system." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000154 }
155
Junxiao Shieef49a92018-11-10 12:19:36 +0000156 if (sectionName == SECTION_GENERAL || sectionName == SECTION_NETDEVBOUND ||
157 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 Pesavento19779d82019-02-14 13:40:04 -0500161 NDN_THROW(ConfigFile::Error("Unrecognized option face_system." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000162 }
163}
164
Junxiao Shib8590312016-12-29 21:22:25 +0000165} // namespace face
166} // namespace nfd