blob: 0ba49ae689c1ee275496efff1b9c1a3c643bdb83 [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 Newberry944f38b2017-07-20 20:54:22 -0400176EthernetFactory::createFace(const CreateFaceParams& params,
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800177 const FaceCreatedCallback& onCreated,
Eric Newberry42602412016-08-27 09:33:18 -0700178 const FaceCreationFailedCallback& onFailure)
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800179{
Eric Newberry944f38b2017-07-20 20:54:22 -0400180 BOOST_ASSERT(params.remoteUri.isCanonical());
Davide Pesavento46afec42017-05-28 14:28:47 -0400181
Eric Newberry944f38b2017-07-20 20:54:22 -0400182 if (!params.localUri || params.localUri->getScheme() != "dev") {
Davide Pesavento46afec42017-05-28 14:28:47 -0400183 NFD_LOG_TRACE("Cannot create unicast Ethernet face without dev:// LocalUri");
184 onFailure(406, "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme");
185 return;
186 }
Eric Newberry944f38b2017-07-20 20:54:22 -0400187 BOOST_ASSERT(params.localUri->isCanonical());
Davide Pesavento46afec42017-05-28 14:28:47 -0400188
Eric Newberry944f38b2017-07-20 20:54:22 -0400189 if (params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Davide Pesavento46afec42017-05-28 14:28:47 -0400190 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
191 onFailure(406, "Outgoing Ethernet faces do not support on-demand persistency");
192 return;
193 }
194
Eric Newberry944f38b2017-07-20 20:54:22 -0400195 ethernet::Address remoteEndpoint(ethernet::Address::fromString(params.remoteUri.getHost()));
196 std::string localEndpoint(params.localUri->getHost());
Davide Pesavento46afec42017-05-28 14:28:47 -0400197
198 if (remoteEndpoint.isMulticast()) {
199 NFD_LOG_TRACE("createFace does not support multicast faces");
200 onFailure(406, "Cannot create multicast Ethernet faces");
201 return;
202 }
203
Eric Newberry944f38b2017-07-20 20:54:22 -0400204 if (params.wantLocalFieldsEnabled) {
Davide Pesavento46afec42017-05-28 14:28:47 -0400205 // Ethernet faces are never local
206 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
207 onFailure(406, "Local fields can only be enabled on faces with local scope");
208 return;
209 }
210
211 for (const auto& i : m_channels) {
212 if (i.first == localEndpoint) {
Eric Newberry944f38b2017-07-20 20:54:22 -0400213 i.second->connect(remoteEndpoint, params.persistency, onCreated, onFailure);
Davide Pesavento46afec42017-05-28 14:28:47 -0400214 return;
215 }
216 }
217
218 NFD_LOG_TRACE("No channels available to connect to " << remoteEndpoint);
219 onFailure(504, "No channels available to connect");
220}
221
222shared_ptr<EthernetChannel>
Junxiao Shi0d82d042017-07-07 06:15:27 +0000223EthernetFactory::createChannel(const shared_ptr<const ndn::net::NetworkInterface>& localEndpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -0400224 time::nanoseconds idleTimeout)
225{
Junxiao Shi0d82d042017-07-07 06:15:27 +0000226 auto it = m_channels.find(localEndpoint->getName());
Davide Pesavento46afec42017-05-28 14:28:47 -0400227 if (it != m_channels.end())
228 return it->second;
229
230 auto channel = std::make_shared<EthernetChannel>(localEndpoint, idleTimeout);
Junxiao Shi0d82d042017-07-07 06:15:27 +0000231 m_channels[localEndpoint->getName()] = channel;
Davide Pesavento46afec42017-05-28 14:28:47 -0400232
233 return channel;
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800234}
235
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200236std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600237EthernetFactory::getChannels() const
238{
Davide Pesavento46afec42017-05-28 14:28:47 -0400239 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600240}
241
Junxiao Shicde37ad2015-12-24 01:02:05 -0700242shared_ptr<Face>
Junxiao Shi0d82d042017-07-07 06:15:27 +0000243EthernetFactory::createMulticastFace(const ndn::net::NetworkInterface& netif,
Junxiao Shi7003c602017-01-10 13:35:28 +0000244 const ethernet::Address& address)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200245{
Junxiao Shi7003c602017-01-10 13:35:28 +0000246 BOOST_ASSERT(address.isMulticast());
247
Junxiao Shi0d82d042017-07-07 06:15:27 +0000248 auto key = std::make_pair(netif.getName(), address);
Junxiao Shi7003c602017-01-10 13:35:28 +0000249 auto found = m_mcastFaces.find(key);
250 if (found != m_mcastFaces.end()) {
251 return found->second;
252 }
253
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400254 GenericLinkService::Options opts;
Junxiao Shi7003c602017-01-10 13:35:28 +0000255 opts.allowFragmentation = true;
256 opts.allowReassembly = true;
257
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400258 auto linkService = make_unique<GenericLinkService>(opts);
259 auto transport = make_unique<MulticastEthernetTransport>(netif, address, m_mcastConfig.linkType);
Junxiao Shi7003c602017-01-10 13:35:28 +0000260 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
261
262 m_mcastFaces[key] = face;
263 connectFaceClosedSignal(*face, [this, key] { m_mcastFaces.erase(key); });
264
265 return face;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200266}
267
Junxiao Shi7003c602017-01-10 13:35:28 +0000268void
269EthernetFactory::applyConfig(const FaceSystem::ConfigContext& context)
270{
271 // collect old faces
272 std::set<shared_ptr<Face>> oldFaces;
273 boost::copy(m_mcastFaces | boost::adaptors::map_values,
274 std::inserter(oldFaces, oldFaces.end()));
275
276 if (m_mcastConfig.isEnabled) {
277 // determine interfaces on which faces should be created or retained
278 auto capableNetifs = context.listNetifs() |
Junxiao Shi0d82d042017-07-07 06:15:27 +0000279 boost::adaptors::transformed([] (const NetworkInterfaceInfo& nii) { return nii.asNetworkInterface(); }) |
280 boost::adaptors::filtered([this] (const shared_ptr<const ndn::net::NetworkInterface>& netif) {
281 return netif->isUp() && netif->canMulticast() && m_mcastConfig.netifPredicate(*netif);
282 });
Junxiao Shi7003c602017-01-10 13:35:28 +0000283
284 // create faces
285 for (const auto& netif : capableNetifs) {
286 shared_ptr<Face> face;
287 try {
Junxiao Shi0d82d042017-07-07 06:15:27 +0000288 face = this->createMulticastFace(*netif, m_mcastConfig.group);
Junxiao Shi7003c602017-01-10 13:35:28 +0000289 }
290 catch (const EthernetTransport::Error& e) {
Junxiao Shi0d82d042017-07-07 06:15:27 +0000291 NFD_LOG_WARN("Cannot create Ethernet multicast face on " << netif->getName() << ": " << e.what());
Junxiao Shi7003c602017-01-10 13:35:28 +0000292 continue;
293 }
294
295 if (face->getId() == face::INVALID_FACEID) {
296 // new face: register with forwarding
Junxiao Shi2d491752017-07-14 21:32:05 +0000297 this->addFace(face);
Junxiao Shi7003c602017-01-10 13:35:28 +0000298 }
299 else {
300 // existing face: don't destroy
301 oldFaces.erase(face);
302 }
303 }
304 }
305
306 // destroy old faces that are not needed in new configuration
307 for (const auto& face : oldFaces) {
308 face->close();
309 }
310}
311
312} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100313} // namespace nfd