blob: 8d3e737123489caa4ab4444be6be5e91f2b9dcf7 [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7003c602017-01-10 13:35:28 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Steve DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010025
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080026#include "ethernet-factory.hpp"
Davide Pesavento35120ea2015-11-17 21:13:18 +010027#include "ethernet-transport.hpp"
28#include "generic-link-service.hpp"
Junxiao Shi7003c602017-01-10 13:35:28 +000029#include "core/logger.hpp"
30#include <boost/range/adaptors.hpp>
31#include <boost/range/algorithm/copy.hpp>
Davide Pesavento44deacc2014-02-19 10:48:07 +010032
Davide Pesavento44deacc2014-02-19 10:48:07 +010033namespace nfd {
Junxiao Shi7003c602017-01-10 13:35:28 +000034namespace face {
Davide Pesavento44deacc2014-02-19 10:48:07 +010035
Junxiao Shi7003c602017-01-10 13:35:28 +000036NFD_LOG_INIT("EthernetFactory");
Junxiao Shib47247d2017-01-24 15:09:16 +000037NFD_REGISTER_PROTOCOL_FACTORY(EthernetFactory);
38
39const std::string&
40EthernetFactory::getId()
41{
42 static std::string id("ether");
43 return id;
44}
45
Junxiao Shi7003c602017-01-10 13:35:28 +000046
47void
48EthernetFactory::processConfig(OptionalConfigSection configSection,
49 FaceSystem::ConfigContext& context)
Davide Pesavento44deacc2014-02-19 10:48:07 +010050{
Junxiao Shi7003c602017-01-10 13:35:28 +000051 // ether
52 // {
53 // mcast yes
54 // mcast_group 01:00:5E:00:17:AA
55 // whitelist
56 // {
57 // *
58 // }
59 // blacklist
60 // {
61 // }
62 // }
Davide Pesavento44deacc2014-02-19 10:48:07 +010063
Junxiao Shi7003c602017-01-10 13:35:28 +000064 MulticastConfig mcastConfig;
Davide Pesavento44deacc2014-02-19 10:48:07 +010065
Junxiao Shi7003c602017-01-10 13:35:28 +000066 if (configSection) {
67 // face_system.ether.mcast defaults to 'yes' but only if face_system.ether section is present
68 mcastConfig.isEnabled = true;
Davide Pesavento35120ea2015-11-17 21:13:18 +010069
Junxiao Shi7003c602017-01-10 13:35:28 +000070 for (const auto& pair : *configSection) {
71 const std::string& key = pair.first;
72 const ConfigSection& value = pair.second;
Davide Pesavento7726ae52014-11-23 21:01:05 +010073
Junxiao Shi7003c602017-01-10 13:35:28 +000074 if (key == "mcast") {
75 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "ether");
76 }
77 else if (key == "mcast_group") {
78 const std::string& valueStr = value.get_value<std::string>();
79 mcastConfig.group = ethernet::Address::fromString(valueStr);
80 if (mcastConfig.group.isNull()) {
81 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" +
82 valueStr + "' cannot be parsed as an Ethernet address"));
83 }
84 else if (!mcastConfig.group.isMulticast()) {
85 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" +
86 valueStr + "' is not a multicast address"));
87 }
88 }
89 else if (key == "whitelist") {
90 mcastConfig.netifPredicate.parseWhitelist(value);
91 }
92 else if (key == "blacklist") {
93 mcastConfig.netifPredicate.parseBlacklist(value);
94 }
95 else {
96 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.ether." + key));
97 }
98 }
99 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100100
Junxiao Shi7003c602017-01-10 13:35:28 +0000101 if (!context.isDryRun) {
102 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
103 if (mcastConfig.isEnabled) {
104 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
105 }
106 else {
107 NFD_LOG_INFO("disabling multicast");
108 }
109 }
110 else if (m_mcastConfig.group != mcastConfig.group) {
111 NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group <<
112 " to " << mcastConfig.group);
113 }
114 else if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
115 NFD_LOG_INFO("changing whitelist/blacklist");
116 }
117 else {
118 // There's no configuration change, but we still need to re-apply configuration because
119 // netifs may have changed.
120 }
121
122 m_mcastConfig = mcastConfig;
123 this->applyConfig(context);
124 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100125}
126
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800127void
128EthernetFactory::createFace(const FaceUri& uri,
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800129 ndn::nfd::FacePersistency persistency,
Eric Newberryf40551a2016-09-05 15:41:16 -0700130 bool wantLocalFieldsEnabled,
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800131 const FaceCreatedCallback& onCreated,
Eric Newberry42602412016-08-27 09:33:18 -0700132 const FaceCreationFailedCallback& onFailure)
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800133{
Eric Newberry42602412016-08-27 09:33:18 -0700134 onFailure(406, "Unsupported protocol");
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800135}
136
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200137std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600138EthernetFactory::getChannels() const
139{
Davide Pesavento7726ae52014-11-23 21:01:05 +0100140 return {};
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600141}
142
Junxiao Shicde37ad2015-12-24 01:02:05 -0700143shared_ptr<Face>
Junxiao Shi7003c602017-01-10 13:35:28 +0000144EthernetFactory::createMulticastFace(const NetworkInterfaceInfo& netif,
145 const ethernet::Address& address)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200146{
Junxiao Shi7003c602017-01-10 13:35:28 +0000147 BOOST_ASSERT(address.isMulticast());
148
149 auto key = std::make_pair(netif.name, address);
150 auto found = m_mcastFaces.find(key);
151 if (found != m_mcastFaces.end()) {
152 return found->second;
153 }
154
155 face::GenericLinkService::Options opts;
156 opts.allowFragmentation = true;
157 opts.allowReassembly = true;
158
159 auto linkService = make_unique<face::GenericLinkService>(opts);
160 auto transport = make_unique<face::EthernetTransport>(netif, address);
161 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
162
163 m_mcastFaces[key] = face;
164 connectFaceClosedSignal(*face, [this, key] { m_mcastFaces.erase(key); });
165
166 return face;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200167}
168
Junxiao Shi7003c602017-01-10 13:35:28 +0000169void
170EthernetFactory::applyConfig(const FaceSystem::ConfigContext& context)
171{
172 // collect old faces
173 std::set<shared_ptr<Face>> oldFaces;
174 boost::copy(m_mcastFaces | boost::adaptors::map_values,
175 std::inserter(oldFaces, oldFaces.end()));
176
177 if (m_mcastConfig.isEnabled) {
178 // determine interfaces on which faces should be created or retained
179 auto capableNetifs = context.listNetifs() |
180 boost::adaptors::filtered([this] (const NetworkInterfaceInfo& netif) {
181 return netif.isUp() && netif.isMulticastCapable() &&
182 m_mcastConfig.netifPredicate(netif);
183 });
184
185 // create faces
186 for (const auto& netif : capableNetifs) {
187 shared_ptr<Face> face;
188 try {
189 face = this->createMulticastFace(netif, m_mcastConfig.group);
190 }
191 catch (const EthernetTransport::Error& e) {
192 NFD_LOG_ERROR("Cannot create Ethernet multicast face on " << netif.name << ": " <<
193 e.what() << ", continuing");
194 continue;
195 }
196
197 if (face->getId() == face::INVALID_FACEID) {
198 // new face: register with forwarding
199 context.addFace(face);
200 }
201 else {
202 // existing face: don't destroy
203 oldFaces.erase(face);
204 }
205 }
206 }
207
208 // destroy old faces that are not needed in new configuration
209 for (const auto& face : oldFaces) {
210 face->close();
211 }
212}
213
214} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100215} // namespace nfd