NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
ethernet-factory.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2017, 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 "ethernet-factory.hpp"
27 #include "generic-link-service.hpp"
29 #include <boost/range/adaptors.hpp>
30 #include <boost/range/algorithm/copy.hpp>
31 
32 namespace nfd {
33 namespace face {
34 
35 NFD_LOG_INIT("EthernetFactory");
37 
38 const std::string&
40 {
41  static std::string id("ether");
42  return id;
43 }
44 
46  : ProtocolFactory(params)
47 {
48  m_netifAddConn = netmon->onInterfaceAdded.connect(
49  [this] (const shared_ptr<const ndn::net::NetworkInterface>& netif) {
50  this->applyUnicastConfigToNetif(netif);
51  this->applyMcastConfigToNetif(*netif);
52  });
53 }
54 
55 void
58 {
59  // ether
60  // {
61  // listen yes
62  // idle_timeout 600
63  // mcast yes
64  // mcast_group 01:00:5E:00:17:AA
65  // mcast_ad_hoc no
66  // whitelist
67  // {
68  // *
69  // }
70  // blacklist
71  // {
72  // }
73  // }
74 
75  UnicastConfig unicastConfig;
76  MulticastConfig mcastConfig;
77 
78  if (configSection) {
79  // listen and mcast default to 'yes' but only if face_system.ether section is present
80  unicastConfig.isEnabled = unicastConfig.wantListen = mcastConfig.isEnabled = true;
81 
82  for (const auto& pair : *configSection) {
83  const std::string& key = pair.first;
84  const ConfigSection& value = pair.second;
85 
86  if (key == "listen") {
87  unicastConfig.wantListen = ConfigFile::parseYesNo(pair, "face_system.ether");
88  }
89  else if (key == "idle_timeout") {
90  unicastConfig.idleTimeout = time::seconds(ConfigFile::parseNumber<uint32_t>(pair, "face_system.ether"));
91  }
92  else if (key == "mcast") {
93  mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.ether");
94  }
95  else if (key == "mcast_group") {
96  const std::string& valueStr = value.get_value<std::string>();
97  mcastConfig.group = ethernet::Address::fromString(valueStr);
98  if (mcastConfig.group.isNull()) {
99  BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" +
100  valueStr + "' cannot be parsed as an Ethernet address"));
101  }
102  else if (!mcastConfig.group.isMulticast()) {
103  BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" +
104  valueStr + "' is not a multicast address"));
105  }
106  }
107  else if (key == "mcast_ad_hoc") {
108  bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.ether");
109  mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
110  }
111  else if (key == "whitelist") {
112  mcastConfig.netifPredicate.parseWhitelist(value);
113  }
114  else if (key == "blacklist") {
115  mcastConfig.netifPredicate.parseBlacklist(value);
116  }
117  else {
118  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.ether." + key));
119  }
120  }
121  }
122 
123  if (context.isDryRun) {
124  return;
125  }
126 
127  if (unicastConfig.isEnabled) {
128  if (m_unicastConfig.wantListen && !unicastConfig.wantListen && !m_channels.empty()) {
129  NFD_LOG_WARN("Cannot stop listening on Ethernet channels");
130  }
131  if (m_unicastConfig.idleTimeout != unicastConfig.idleTimeout && !m_channels.empty()) {
132  NFD_LOG_WARN("Idle timeout setting applies to new Ethernet channels only");
133  }
134  }
135  else if (m_unicastConfig.isEnabled && !m_channels.empty()) {
136  NFD_LOG_WARN("Cannot disable Ethernet channels after initialization");
137  }
138 
139  if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
140  if (mcastConfig.isEnabled) {
141  NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
142  }
143  else {
144  NFD_LOG_INFO("disabling multicast");
145  }
146  }
147  else if (mcastConfig.isEnabled) {
148  if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
149  NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
150  }
151  if (m_mcastConfig.group != mcastConfig.group) {
152  NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group <<
153  " to " << mcastConfig.group);
154  }
155  if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
156  NFD_LOG_INFO("changing whitelist/blacklist");
157  }
158  }
159 
160  // Even if there's no configuration change, we still need to re-apply configuration because
161  // netifs may have changed.
162  m_unicastConfig = unicastConfig;
163  m_mcastConfig = mcastConfig;
164  this->applyConfig(context);
165 }
166 
167 void
169  const FaceCreatedCallback& onCreated,
170  const FaceCreationFailedCallback& onFailure)
171 {
172  BOOST_ASSERT(params.remoteUri.isCanonical());
173 
174  if (!params.localUri || params.localUri->getScheme() != "dev") {
175  NFD_LOG_TRACE("Cannot create unicast Ethernet face without dev:// LocalUri");
176  onFailure(406, "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme");
177  return;
178  }
179  BOOST_ASSERT(params.localUri->isCanonical());
180 
182  NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
183  onFailure(406, "Outgoing Ethernet faces do not support on-demand persistency");
184  return;
185  }
186 
188  std::string localEndpoint(params.localUri->getHost());
189 
190  if (remoteEndpoint.isMulticast()) {
191  NFD_LOG_TRACE("createFace does not support multicast faces");
192  onFailure(406, "Cannot create multicast Ethernet faces");
193  return;
194  }
195 
196  if (params.wantLocalFields) {
197  // Ethernet faces are never local
198  NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
199  onFailure(406, "Local fields can only be enabled on faces with local scope");
200  return;
201  }
202 
203  for (const auto& i : m_channels) {
204  if (i.first == localEndpoint) {
205  i.second->connect(remoteEndpoint, params.persistency, params.wantLpReliability,
206  onCreated, onFailure);
207  return;
208  }
209  }
210 
211  NFD_LOG_TRACE("No channels available to connect to " << remoteEndpoint);
212  onFailure(504, "No channels available to connect");
213 }
214 
215 shared_ptr<EthernetChannel>
216 EthernetFactory::createChannel(const shared_ptr<const ndn::net::NetworkInterface>& localEndpoint,
217  time::nanoseconds idleTimeout)
218 {
219  auto it = m_channels.find(localEndpoint->getName());
220  if (it != m_channels.end())
221  return it->second;
222 
223  auto channel = std::make_shared<EthernetChannel>(localEndpoint, idleTimeout);
224  m_channels[localEndpoint->getName()] = channel;
225 
226  return channel;
227 }
228 
229 std::vector<shared_ptr<const Channel>>
231 {
232  return getChannelsFromMap(m_channels);
233 }
234 
235 shared_ptr<Face>
237  const ethernet::Address& address)
238 {
239  BOOST_ASSERT(address.isMulticast());
240 
241  auto key = std::make_pair(netif.getName(), address);
242  auto found = m_mcastFaces.find(key);
243  if (found != m_mcastFaces.end()) {
244  return found->second;
245  }
246 
248  opts.allowFragmentation = true;
249  opts.allowReassembly = true;
250 
251  auto linkService = make_unique<GenericLinkService>(opts);
252  auto transport = make_unique<MulticastEthernetTransport>(netif, address, m_mcastConfig.linkType);
253  auto face = make_shared<Face>(std::move(linkService), std::move(transport));
254 
255  m_mcastFaces[key] = face;
256  connectFaceClosedSignal(*face, [this, key] { m_mcastFaces.erase(key); });
257 
258  return face;
259 }
260 
261 shared_ptr<EthernetChannel>
262 EthernetFactory::applyUnicastConfigToNetif(const shared_ptr<const ndn::net::NetworkInterface>& netif)
263 {
264  if (!m_unicastConfig.isEnabled) {
265  return nullptr;
266  }
267 
268  if (netif->getType() != ndn::net::InterfaceType::ETHERNET) {
269  NFD_LOG_DEBUG("Not creating channel on " << netif->getName() << ": incompatible netif type");
270  return nullptr;
271  }
272 
273  if (!netif->isUp()) {
274  NFD_LOG_DEBUG("Not creating channel on " << netif->getName() << ": netif is down");
275  return nullptr;
276  }
277 
278  if (netif->getEthernetAddress().isNull()) {
279  NFD_LOG_DEBUG("Not creating channel on " << netif->getName() << ": invalid Ethernet address");
280  return nullptr;
281  }
282 
283  auto channel = this->createChannel(netif, m_unicastConfig.idleTimeout);
284  if (m_unicastConfig.wantListen && !channel->isListening()) {
285  try {
286  channel->listen(this->addFace, nullptr);
287  }
288  catch (const EthernetChannel::Error& e) {
289  NFD_LOG_WARN("Cannot listen on " << netif->getName() << ": " << e.what());
290  }
291  }
292  return channel;
293 }
294 
295 shared_ptr<Face>
296 EthernetFactory::applyMcastConfigToNetif(const ndn::net::NetworkInterface& netif)
297 {
298  if (!m_mcastConfig.isEnabled) {
299  return nullptr;
300  }
301 
303  NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": incompatible netif type");
304  return nullptr;
305  }
306 
307  if (!netif.isUp()) {
308  NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": netif is down");
309  return nullptr;
310  }
311 
312  if (!netif.canMulticast()) {
313  NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": netif cannot multicast");
314  return nullptr;
315  }
316 
317  if (netif.getEthernetAddress().isNull()) {
318  NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": invalid Ethernet address");
319  return nullptr;
320  }
321 
322  if (!m_mcastConfig.netifPredicate(netif)) {
323  NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": rejected by whitelist/blacklist");
324  return nullptr;
325  }
326 
327  NFD_LOG_DEBUG("Creating multicast face on " << netif.getName());
328  shared_ptr<Face> face;
329  try {
330  face = this->createMulticastFace(netif, m_mcastConfig.group);
331  }
332  catch (const EthernetTransport::Error& e) {
333  NFD_LOG_WARN("Cannot create multicast face on " << netif.getName() << ": " << e.what());
334  return nullptr;
335  }
336 
337  if (face->getId() == face::INVALID_FACEID) {
338  // new face: register with forwarding
339  this->addFace(face);
340  }
341  return face;
342 }
343 
344 void
345 EthernetFactory::applyConfig(const FaceSystem::ConfigContext& context)
346 {
347  if (m_unicastConfig.isEnabled) {
348  providedSchemes.insert("ether");
349  }
350  else {
351  providedSchemes.erase("ether");
352  }
353 
354  // collect old multicast faces
355  std::set<shared_ptr<Face>> oldFaces;
356  boost::copy(m_mcastFaces | boost::adaptors::map_values, std::inserter(oldFaces, oldFaces.end()));
357 
358  // create channels and multicast faces if requested by config
359  for (const auto& netif : netmon->listNetworkInterfaces()) {
360  this->applyUnicastConfigToNetif(netif);
361 
362  auto face = this->applyMcastConfigToNetif(*netif);
363  if (face != nullptr) {
364  // don't destroy face
365  oldFaces.erase(face);
366  }
367  }
368 
369  // destroy old multicast faces that are not needed in new configuration
370  for (const auto& face : oldFaces) {
371  face->close();
372  }
373 }
374 
375 } // namespace face
376 } // namespace nfd
std::set< std::string > providedSchemes
FaceUri schemes provided by this ProtocolFactory.
static Address fromString(const std::string &str)
Creates an Address from a string containing an Ethernet address in hexadecimal notation, with colons or hyphens as separators.
Definition: ethernet.cpp:91
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:59
shared_ptr< ndn::net::NetworkMonitor > netmon
NetworkMonitor for listing available network interfaces and monitoring their changes.
void createFace(const CreateFaceParams &params, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure) override
Try to create face using the supplied parameters.
bool canMulticast() const
Returns true if the interface supports multicast communication.
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:55
NFD_REGISTER_PROTOCOL_FACTORY(EthernetFactory)
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
Definition: channel.cpp:40
Parameters to ProtocolFactory::createFace.
std::vector< shared_ptr< const Channel > > getChannels() const override
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context) override
process face_system.ether config section
FaceCreatedCallback addFace
callback when a new face is created
Represents one network interface attached to the host.
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
Provides support for an underlying protocol.
EthernetChannel-related error.
InterfaceType getType() const
Returns the hardware type of the interface.
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
EthernetFactory(const CtorParams &params)
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
protocol factory for Ethernet
Parameters to ProtocolFactory constructor.
boost::property_tree::ptree ConfigSection
a config file section
bool isMulticast() const
True if this is a multicast address.
Definition: ethernet.cpp:65
shared_ptr< Face > createMulticastFace(const ndn::net::NetworkInterface &localEndpoint, const ethernet::Address &group)
Create a face to communicate on the given Ethernet multicast group.
std::string getName() const
Returns the name of the interface, unique on the system.
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:85
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
Definition: channel.hpp:44
represents an Ethernet hardware address
Definition: ethernet.hpp:52
Options that control the behavior of GenericLinkService.
#define NFD_LOG_WARN(expression)
Definition: logger.hpp:58
bool isCanonical() const
determine whether this FaceUri is in canonical form
Definition: face-uri.cpp:641
bool isUp() const
Returns true if the interface is administratively up.
static const std::string & getId()
bool isNull() const
True if this is a null address (00:00:00:00:00:00)
Definition: ethernet.cpp:71
ethernet::Address getEthernetAddress() const
Returns the link-layer (Ethernet) address of the interface.
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
const std::string & getHost() const
get host (domain)
Definition: face-uri.hpp:119
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face.hpp:42
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:35
shared_ptr< EthernetChannel > createChannel(const shared_ptr< const ndn::net::NetworkInterface > &localEndpoint, time::nanoseconds idleTimeout)
Create Ethernet-based channel on the specified network interface.