blob: 9ff5fd527aaf6faccea6498f9a99b7b34a20ad60 [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0d82d042017-07-07 06:15:27 +00002/*
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 "generic-link-service.hpp"
Davide Pesavento6bd6d0b2017-03-25 15:16:40 -040028#include "multicast-ethernet-transport.hpp"
Junxiao Shi7003c602017-01-10 13:35:28 +000029#include <boost/range/adaptors.hpp>
30#include <boost/range/algorithm/copy.hpp>
Davide Pesavento44deacc2014-02-19 10:48:07 +010031
Davide Pesavento44deacc2014-02-19 10:48:07 +010032namespace nfd {
Junxiao Shi7003c602017-01-10 13:35:28 +000033namespace face {
Davide Pesavento44deacc2014-02-19 10:48:07 +010034
Junxiao Shi7003c602017-01-10 13:35:28 +000035NFD_LOG_INIT("EthernetFactory");
Junxiao Shib47247d2017-01-24 15:09:16 +000036NFD_REGISTER_PROTOCOL_FACTORY(EthernetFactory);
37
38const std::string&
39EthernetFactory::getId()
40{
41 static std::string id("ether");
42 return id;
43}
44
Junxiao Shi0ba6d642017-07-17 00:53:22 +000045EthernetFactory::EthernetFactory(const CtorParams& params)
46 : ProtocolFactory(params)
47{
48}
Junxiao Shi7003c602017-01-10 13:35:28 +000049
50void
51EthernetFactory::processConfig(OptionalConfigSection configSection,
52 FaceSystem::ConfigContext& context)
Davide Pesavento44deacc2014-02-19 10:48:07 +010053{
Junxiao Shi7003c602017-01-10 13:35:28 +000054 // ether
55 // {
Davide Pesavento46afec42017-05-28 14:28:47 -040056 // listen yes
57 // idle_timeout 600
Junxiao Shi7003c602017-01-10 13:35:28 +000058 // mcast yes
59 // mcast_group 01:00:5E:00:17:AA
Teng Liangbfea5752017-03-29 04:51:10 +000060 // mcast_ad_hoc no
Junxiao Shi7003c602017-01-10 13:35:28 +000061 // whitelist
62 // {
63 // *
64 // }
65 // blacklist
66 // {
67 // }
68 // }
Davide Pesavento44deacc2014-02-19 10:48:07 +010069
Davide Pesavento46afec42017-05-28 14:28:47 -040070 bool wantListen = true;
71 uint32_t idleTimeout = 600;
Junxiao Shi7003c602017-01-10 13:35:28 +000072 MulticastConfig mcastConfig;
Davide Pesavento44deacc2014-02-19 10:48:07 +010073
Junxiao Shi7003c602017-01-10 13:35:28 +000074 if (configSection) {
75 // face_system.ether.mcast defaults to 'yes' but only if face_system.ether section is present
76 mcastConfig.isEnabled = true;
Davide Pesavento35120ea2015-11-17 21:13:18 +010077
Junxiao Shi7003c602017-01-10 13:35:28 +000078 for (const auto& pair : *configSection) {
79 const std::string& key = pair.first;
80 const ConfigSection& value = pair.second;
Davide Pesavento7726ae52014-11-23 21:01:05 +010081
Davide Pesavento46afec42017-05-28 14:28:47 -040082 if (key == "listen") {
83 wantListen = ConfigFile::parseYesNo(pair, "face_system.ether");
84 }
85 else if (key == "idle_timeout") {
86 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.ether");
87 }
88 else if (key == "mcast") {
89 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.ether");
Junxiao Shi7003c602017-01-10 13:35:28 +000090 }
91 else if (key == "mcast_group") {
92 const std::string& valueStr = value.get_value<std::string>();
93 mcastConfig.group = ethernet::Address::fromString(valueStr);
94 if (mcastConfig.group.isNull()) {
95 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" +
96 valueStr + "' cannot be parsed as an Ethernet address"));
97 }
98 else if (!mcastConfig.group.isMulticast()) {
99 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" +
100 valueStr + "' is not a multicast address"));
101 }
102 }
Teng Liangbfea5752017-03-29 04:51:10 +0000103 else if (key == "mcast_ad_hoc") {
Davide Pesavento46afec42017-05-28 14:28:47 -0400104 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.ether");
Teng Liangbfea5752017-03-29 04:51:10 +0000105 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
106 }
Junxiao Shi7003c602017-01-10 13:35:28 +0000107 else if (key == "whitelist") {
108 mcastConfig.netifPredicate.parseWhitelist(value);
109 }
110 else if (key == "blacklist") {
111 mcastConfig.netifPredicate.parseBlacklist(value);
112 }
113 else {
114 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.ether." + key));
115 }
116 }
117 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100118
Junxiao Shi7003c602017-01-10 13:35:28 +0000119 if (!context.isDryRun) {
Davide Pesavento46afec42017-05-28 14:28:47 -0400120 if (configSection) {
121 providedSchemes.insert("ether");
122
123 // determine the interfaces on which channels should be created
124 auto netifs = context.listNetifs() |
Junxiao Shi0d82d042017-07-07 06:15:27 +0000125 boost::adaptors::transformed([] (const NetworkInterfaceInfo& nii) { return nii.asNetworkInterface(); }) |
126 boost::adaptors::filtered([] (const shared_ptr<const ndn::net::NetworkInterface>& netif) {
127 return netif->isUp() && !netif->isLoopback();
128 });
Davide Pesavento46afec42017-05-28 14:28:47 -0400129
130 // create channels
131 for (const auto& netif : netifs) {
132 auto channel = this->createChannel(netif, time::seconds(idleTimeout));
133 if (wantListen && !channel->isListening()) {
134 try {
Junxiao Shi2d491752017-07-14 21:32:05 +0000135 channel->listen(this->addFace, nullptr);
Davide Pesavento46afec42017-05-28 14:28:47 -0400136 }
137 catch (const EthernetChannel::Error& e) {
Junxiao Shi0d82d042017-07-07 06:15:27 +0000138 NFD_LOG_WARN("Cannot listen on " << netif->getName() << ": " << e.what());
Davide Pesavento46afec42017-05-28 14:28:47 -0400139 }
140 }
141 }
142 }
143 else if (!m_channels.empty()) {
144 NFD_LOG_WARN("Cannot disable dev channels after initialization");
145 }
146
Junxiao Shi7003c602017-01-10 13:35:28 +0000147 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
148 if (mcastConfig.isEnabled) {
149 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
150 }
151 else {
152 NFD_LOG_INFO("disabling multicast");
153 }
154 }
Teng Liangbfea5752017-03-29 04:51:10 +0000155 else if (mcastConfig.isEnabled) {
156 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
157 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
158 }
159 if (m_mcastConfig.group != mcastConfig.group) {
160 NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group <<
161 " to " << mcastConfig.group);
162 }
163 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
164 NFD_LOG_INFO("changing whitelist/blacklist");
165 }
Junxiao Shi7003c602017-01-10 13:35:28 +0000166 }
167
Teng Liangbfea5752017-03-29 04:51:10 +0000168 // Even if there's no configuration change, we still need to re-apply configuration because
169 // netifs may have changed.
Junxiao Shi7003c602017-01-10 13:35:28 +0000170 m_mcastConfig = mcastConfig;
171 this->applyConfig(context);
172 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100173}
174
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800175void
Eric Newberry78e32b02017-04-01 14:34:44 +0000176EthernetFactory::createFace(const FaceUri& remoteUri,
177 const ndn::optional<FaceUri>& localUri,
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800178 ndn::nfd::FacePersistency persistency,
Eric Newberryf40551a2016-09-05 15:41:16 -0700179 bool wantLocalFieldsEnabled,
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800180 const FaceCreatedCallback& onCreated,
Eric Newberry42602412016-08-27 09:33:18 -0700181 const FaceCreationFailedCallback& onFailure)
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800182{
Davide Pesavento46afec42017-05-28 14:28:47 -0400183 BOOST_ASSERT(remoteUri.isCanonical());
184
185 if (!localUri || localUri->getScheme() != "dev") {
186 NFD_LOG_TRACE("Cannot create unicast Ethernet face without dev:// LocalUri");
187 onFailure(406, "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme");
188 return;
189 }
190 BOOST_ASSERT(localUri->isCanonical());
191
192 if (persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
193 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
194 onFailure(406, "Outgoing Ethernet faces do not support on-demand persistency");
195 return;
196 }
197
198 ethernet::Address remoteEndpoint(ethernet::Address::fromString(remoteUri.getHost()));
199 std::string localEndpoint(localUri->getHost());
200
201 if (remoteEndpoint.isMulticast()) {
202 NFD_LOG_TRACE("createFace does not support multicast faces");
203 onFailure(406, "Cannot create multicast Ethernet faces");
204 return;
205 }
206
207 if (wantLocalFieldsEnabled) {
208 // Ethernet faces are never local
209 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
210 onFailure(406, "Local fields can only be enabled on faces with local scope");
211 return;
212 }
213
214 for (const auto& i : m_channels) {
215 if (i.first == localEndpoint) {
216 i.second->connect(remoteEndpoint, persistency, onCreated, onFailure);
217 return;
218 }
219 }
220
221 NFD_LOG_TRACE("No channels available to connect to " << remoteEndpoint);
222 onFailure(504, "No channels available to connect");
223}
224
225shared_ptr<EthernetChannel>
Junxiao Shi0d82d042017-07-07 06:15:27 +0000226EthernetFactory::createChannel(const shared_ptr<const ndn::net::NetworkInterface>& localEndpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -0400227 time::nanoseconds idleTimeout)
228{
Junxiao Shi0d82d042017-07-07 06:15:27 +0000229 auto it = m_channels.find(localEndpoint->getName());
Davide Pesavento46afec42017-05-28 14:28:47 -0400230 if (it != m_channels.end())
231 return it->second;
232
233 auto channel = std::make_shared<EthernetChannel>(localEndpoint, idleTimeout);
Junxiao Shi0d82d042017-07-07 06:15:27 +0000234 m_channels[localEndpoint->getName()] = channel;
Davide Pesavento46afec42017-05-28 14:28:47 -0400235
236 return channel;
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800237}
238
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200239std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600240EthernetFactory::getChannels() const
241{
Davide Pesavento46afec42017-05-28 14:28:47 -0400242 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600243}
244
Junxiao Shicde37ad2015-12-24 01:02:05 -0700245shared_ptr<Face>
Junxiao Shi0d82d042017-07-07 06:15:27 +0000246EthernetFactory::createMulticastFace(const ndn::net::NetworkInterface& netif,
Junxiao Shi7003c602017-01-10 13:35:28 +0000247 const ethernet::Address& address)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200248{
Junxiao Shi7003c602017-01-10 13:35:28 +0000249 BOOST_ASSERT(address.isMulticast());
250
Junxiao Shi0d82d042017-07-07 06:15:27 +0000251 auto key = std::make_pair(netif.getName(), address);
Junxiao Shi7003c602017-01-10 13:35:28 +0000252 auto found = m_mcastFaces.find(key);
253 if (found != m_mcastFaces.end()) {
254 return found->second;
255 }
256
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400257 GenericLinkService::Options opts;
Junxiao Shi7003c602017-01-10 13:35:28 +0000258 opts.allowFragmentation = true;
259 opts.allowReassembly = true;
260
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400261 auto linkService = make_unique<GenericLinkService>(opts);
262 auto transport = make_unique<MulticastEthernetTransport>(netif, address, m_mcastConfig.linkType);
Junxiao Shi7003c602017-01-10 13:35:28 +0000263 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
264
265 m_mcastFaces[key] = face;
266 connectFaceClosedSignal(*face, [this, key] { m_mcastFaces.erase(key); });
267
268 return face;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200269}
270
Junxiao Shi7003c602017-01-10 13:35:28 +0000271void
272EthernetFactory::applyConfig(const FaceSystem::ConfigContext& context)
273{
274 // collect old faces
275 std::set<shared_ptr<Face>> oldFaces;
276 boost::copy(m_mcastFaces | boost::adaptors::map_values,
277 std::inserter(oldFaces, oldFaces.end()));
278
279 if (m_mcastConfig.isEnabled) {
280 // determine interfaces on which faces should be created or retained
281 auto capableNetifs = context.listNetifs() |
Junxiao Shi0d82d042017-07-07 06:15:27 +0000282 boost::adaptors::transformed([] (const NetworkInterfaceInfo& nii) { return nii.asNetworkInterface(); }) |
283 boost::adaptors::filtered([this] (const shared_ptr<const ndn::net::NetworkInterface>& netif) {
284 return netif->isUp() && netif->canMulticast() && m_mcastConfig.netifPredicate(*netif);
285 });
Junxiao Shi7003c602017-01-10 13:35:28 +0000286
287 // create faces
288 for (const auto& netif : capableNetifs) {
289 shared_ptr<Face> face;
290 try {
Junxiao Shi0d82d042017-07-07 06:15:27 +0000291 face = this->createMulticastFace(*netif, m_mcastConfig.group);
Junxiao Shi7003c602017-01-10 13:35:28 +0000292 }
293 catch (const EthernetTransport::Error& e) {
Junxiao Shi0d82d042017-07-07 06:15:27 +0000294 NFD_LOG_WARN("Cannot create Ethernet multicast face on " << netif->getName() << ": " << e.what());
Junxiao Shi7003c602017-01-10 13:35:28 +0000295 continue;
296 }
297
298 if (face->getId() == face::INVALID_FACEID) {
299 // new face: register with forwarding
Junxiao Shi2d491752017-07-14 21:32:05 +0000300 this->addFace(face);
Junxiao Shi7003c602017-01-10 13:35:28 +0000301 }
302 else {
303 // existing face: don't destroy
304 oldFaces.erase(face);
305 }
306 }
307 }
308
309 // destroy old faces that are not needed in new configuration
310 for (const auto& face : oldFaces) {
311 face->close();
312 }
313}
314
315} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100316} // namespace nfd