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