face-system.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2023, Regents of the University of California,
4  * 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"
27 #include "netdev-bound.hpp"
28 #include "protocol-factory.hpp"
29 #include "fw/face-table.hpp"
30 
31 namespace nfd::face {
32 
33 NFD_LOG_INIT(FaceSystem);
34 
35 const std::string CFGSEC_FACESYSTEM = "face_system";
36 const std::string CFGSEC_GENERAL = "general";
37 const std::string CFGSEC_GENERAL_FQ = CFGSEC_FACESYSTEM + ".general";
38 const std::string CFGSEC_NETDEVBOUND = "netdev_bound";
39 
40 FaceSystem::FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon)
41  : m_faceTable(faceTable)
42  , m_netmon(std::move(netmon))
43 {
44  auto pfCtorParams = this->makePFCtorParams();
45  for (const auto& id : ProtocolFactory::listRegistered()) {
46  NFD_LOG_TRACE("creating factory " << id);
47  m_factories[id] = ProtocolFactory::create(id, pfCtorParams);
48  }
49 
50  m_netdevBound = make_unique<NetdevBound>(pfCtorParams, *this);
51 }
52 
54 FaceSystem::makePFCtorParams()
55 {
56  auto addFace = [this] (auto face) { m_faceTable.add(std::move(face)); };
57  return {addFace, m_netmon};
58 }
59 
60 FaceSystem::~FaceSystem() = default;
61 
62 std::set<const ProtocolFactory*>
64 {
65  std::set<const ProtocolFactory*> factories;
66  for (const auto& p : m_factories) {
67  factories.insert(p.second.get());
68  }
69  return factories;
70 }
71 
73 FaceSystem::getFactoryById(const std::string& id) const
74 {
75  auto found = m_factories.find(id);
76  return found == m_factories.end() ? nullptr : found->second.get();
77 }
78 
80 FaceSystem::getFactoryByScheme(const std::string& scheme) const
81 {
82  auto found = m_factoryByScheme.find(scheme);
83  return found == m_factoryByScheme.end() ? nullptr : found->second;
84 }
85 
86 bool
87 FaceSystem::hasFactoryForScheme(const std::string& scheme) const
88 {
89  return m_factoryByScheme.count(scheme) > 0;
90 }
91 
92 void
94 {
95  configFile.addSectionHandler(CFGSEC_FACESYSTEM, [this] (auto&&... args) {
96  processConfig(std::forward<decltype(args)>(args)...);
97  });
98 }
99 
100 void
101 FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string&)
102 {
103  ConfigContext context;
104  context.isDryRun = isDryRun;
105 
106  // process general protocol factory config section
107  auto generalSection = configSection.get_child_optional(CFGSEC_GENERAL);
108  if (generalSection) {
109  for (const auto& pair : *generalSection) {
110  const std::string& key = pair.first;
111  if (key == "enable_congestion_marking") {
112  context.generalConfig.wantCongestionMarking = ConfigFile::parseYesNo(pair, CFGSEC_GENERAL_FQ);
113  }
114  else {
115  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_GENERAL_FQ + "." + key));
116  }
117  }
118  }
119 
120  // process in protocol factories
121  for (const auto& [sectionName, factory] : m_factories) {
122  std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes();
123  factory->processConfig(configSection.get_child_optional(sectionName), context);
124 
125  if (!isDryRun) {
126  for (const auto& scheme : factory->getProvidedSchemes()) {
127  m_factoryByScheme[scheme] = factory.get();
128  if (oldProvidedSchemes.erase(scheme) == 0) {
129  NFD_LOG_TRACE("factory " << sectionName <<
130  " provides " << scheme << " FaceUri scheme");
131  }
132  }
133  for (const auto& scheme : oldProvidedSchemes) {
134  m_factoryByScheme.erase(scheme);
135  NFD_LOG_TRACE("factory " << sectionName <<
136  " no longer provides " << scheme << " FaceUri scheme");
137  }
138  }
139  }
140 
141  // process netdev_bound section, after factories start providing *+dev schemes
142  auto netdevBoundSection = configSection.get_child_optional(CFGSEC_NETDEVBOUND);
143  m_netdevBound->processConfig(netdevBoundSection, context);
144 
145  // process other sections
146  std::set<std::string> seenSections;
147  for (const auto& pair : configSection) {
148  const std::string& sectionName = pair.first;
149  // const ConfigSection& subSection = pair.second;
150 
151  if (!seenSections.insert(sectionName).second) {
152  NDN_THROW(ConfigFile::Error("Duplicate section " + CFGSEC_FACESYSTEM + "." + sectionName));
153  }
154 
155  if (sectionName == CFGSEC_GENERAL || sectionName == CFGSEC_NETDEVBOUND ||
156  m_factories.count(sectionName) > 0) {
157  continue;
158  }
159 
160  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_FACESYSTEM + "." + sectionName));
161  }
162 }
163 
164 } // namespace nfd::face
Configuration file parsing utility.
Definition: config-file.hpp:66
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
Setup notification of configuration file sections.
Definition: config-file.cpp:77
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string &sectionName)
Parse a config option that can be either "yes" or "no".
Definition: config-file.cpp:60
Container of all faces.
Definition: face-table.hpp:42
void add(shared_ptr< Face > face)
Add a face.
Definition: face-table.cpp:51
std::set< const ProtocolFactory * > listProtocolFactories() const
Returns all ProtocolFactory objects owned by the face system.
Definition: face-system.cpp:63
ProtocolFactory * getFactoryById(const std::string &id) const
Definition: face-system.cpp:73
FaceSystem(FaceTable &faceTable, shared_ptr< ndn::net::NetworkMonitor > netmon)
Definition: face-system.cpp:40
bool hasFactoryForScheme(const std::string &scheme) const
Definition: face-system.cpp:87
ProtocolFactory * getFactoryByScheme(const std::string &scheme) const
Definition: face-system.cpp:80
void setConfigFile(ConfigFile &configFile)
Register handler for the face_system section of NFD's configuration file.
Definition: face-system.cpp:93
Provides support for an underlying protocol.
static std::set< std::string > listRegistered()
Get all registered protocol factory IDs.
static unique_ptr< ProtocolFactory > create(const std::string &id, const CtorParams &params)
Create a protocol factory instance.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
#define NFD_LOG_TRACE
Definition: logger.hpp:37
const std::string CFGSEC_FACESYSTEM
Definition: face-system.cpp:35
const std::string CFGSEC_GENERAL
Definition: face-system.cpp:36
const std::string CFGSEC_GENERAL_FQ
Definition: face-system.cpp:37
const std::string CFGSEC_NETDEVBOUND
Definition: face-system.cpp:38
boost::property_tree::ptree ConfigSection
A configuration file section.
Definition: config-file.hpp:41
Parameters to ProtocolFactory constructor.