blob: 8fc9822ec272f632657854c51a9365dcdcadaf0c [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 Shi7003c602017-01-10 13:35:28 +000045
46void
47EthernetFactory::processConfig(OptionalConfigSection configSection,
48 FaceSystem::ConfigContext& context)
Davide Pesavento44deacc2014-02-19 10:48:07 +010049{
Junxiao Shi7003c602017-01-10 13:35:28 +000050 // ether
51 // {
Davide Pesavento46afec42017-05-28 14:28:47 -040052 // listen yes
53 // idle_timeout 600
Junxiao Shi7003c602017-01-10 13:35:28 +000054 // mcast yes
55 // mcast_group 01:00:5E:00:17:AA
Teng Liangbfea5752017-03-29 04:51:10 +000056 // mcast_ad_hoc no
Junxiao Shi7003c602017-01-10 13:35:28 +000057 // whitelist
58 // {
59 // *
60 // }
61 // blacklist
62 // {
63 // }
64 // }
Davide Pesavento44deacc2014-02-19 10:48:07 +010065
Davide Pesavento46afec42017-05-28 14:28:47 -040066 bool wantListen = true;
67 uint32_t idleTimeout = 600;
Junxiao Shi7003c602017-01-10 13:35:28 +000068 MulticastConfig mcastConfig;
Davide Pesavento44deacc2014-02-19 10:48:07 +010069
Junxiao Shi7003c602017-01-10 13:35:28 +000070 if (configSection) {
71 // face_system.ether.mcast defaults to 'yes' but only if face_system.ether section is present
72 mcastConfig.isEnabled = true;
Davide Pesavento35120ea2015-11-17 21:13:18 +010073
Junxiao Shi7003c602017-01-10 13:35:28 +000074 for (const auto& pair : *configSection) {
75 const std::string& key = pair.first;
76 const ConfigSection& value = pair.second;
Davide Pesavento7726ae52014-11-23 21:01:05 +010077
Davide Pesavento46afec42017-05-28 14:28:47 -040078 if (key == "listen") {
79 wantListen = ConfigFile::parseYesNo(pair, "face_system.ether");
80 }
81 else if (key == "idle_timeout") {
82 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.ether");
83 }
84 else if (key == "mcast") {
85 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.ether");
Junxiao Shi7003c602017-01-10 13:35:28 +000086 }
87 else if (key == "mcast_group") {
88 const std::string& valueStr = value.get_value<std::string>();
89 mcastConfig.group = ethernet::Address::fromString(valueStr);
90 if (mcastConfig.group.isNull()) {
91 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" +
92 valueStr + "' cannot be parsed as an Ethernet address"));
93 }
94 else if (!mcastConfig.group.isMulticast()) {
95 BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" +
96 valueStr + "' is not a multicast address"));
97 }
98 }
Teng Liangbfea5752017-03-29 04:51:10 +000099 else if (key == "mcast_ad_hoc") {
Davide Pesavento46afec42017-05-28 14:28:47 -0400100 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.ether");
Teng Liangbfea5752017-03-29 04:51:10 +0000101 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
102 }
Junxiao Shi7003c602017-01-10 13:35:28 +0000103 else if (key == "whitelist") {
104 mcastConfig.netifPredicate.parseWhitelist(value);
105 }
106 else if (key == "blacklist") {
107 mcastConfig.netifPredicate.parseBlacklist(value);
108 }
109 else {
110 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.ether." + key));
111 }
112 }
113 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100114
Junxiao Shi7003c602017-01-10 13:35:28 +0000115 if (!context.isDryRun) {
Davide Pesavento46afec42017-05-28 14:28:47 -0400116 if (configSection) {
117 providedSchemes.insert("ether");
118
119 // determine the interfaces on which channels should be created
120 auto netifs = context.listNetifs() |
Junxiao Shi0d82d042017-07-07 06:15:27 +0000121 boost::adaptors::transformed([] (const NetworkInterfaceInfo& nii) { return nii.asNetworkInterface(); }) |
122 boost::adaptors::filtered([] (const shared_ptr<const ndn::net::NetworkInterface>& netif) {
123 return netif->isUp() && !netif->isLoopback();
124 });
Davide Pesavento46afec42017-05-28 14:28:47 -0400125
126 // create channels
127 for (const auto& netif : netifs) {
128 auto channel = this->createChannel(netif, time::seconds(idleTimeout));
129 if (wantListen && !channel->isListening()) {
130 try {
131 channel->listen(context.addFace, nullptr);
132 }
133 catch (const EthernetChannel::Error& e) {
Junxiao Shi0d82d042017-07-07 06:15:27 +0000134 NFD_LOG_WARN("Cannot listen on " << netif->getName() << ": " << e.what());
Davide Pesavento46afec42017-05-28 14:28:47 -0400135 }
136 }
137 }
138 }
139 else if (!m_channels.empty()) {
140 NFD_LOG_WARN("Cannot disable dev channels after initialization");
141 }
142
Junxiao Shi7003c602017-01-10 13:35:28 +0000143 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
144 if (mcastConfig.isEnabled) {
145 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
146 }
147 else {
148 NFD_LOG_INFO("disabling multicast");
149 }
150 }
Teng Liangbfea5752017-03-29 04:51:10 +0000151 else if (mcastConfig.isEnabled) {
152 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
153 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
154 }
155 if (m_mcastConfig.group != mcastConfig.group) {
156 NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group <<
157 " to " << mcastConfig.group);
158 }
159 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
160 NFD_LOG_INFO("changing whitelist/blacklist");
161 }
Junxiao Shi7003c602017-01-10 13:35:28 +0000162 }
163
Teng Liangbfea5752017-03-29 04:51:10 +0000164 // Even if there's no configuration change, we still need to re-apply configuration because
165 // netifs may have changed.
Junxiao Shi7003c602017-01-10 13:35:28 +0000166 m_mcastConfig = mcastConfig;
167 this->applyConfig(context);
168 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100169}
170
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800171void
Eric Newberry78e32b02017-04-01 14:34:44 +0000172EthernetFactory::createFace(const FaceUri& remoteUri,
173 const ndn::optional<FaceUri>& localUri,
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800174 ndn::nfd::FacePersistency persistency,
Eric Newberryf40551a2016-09-05 15:41:16 -0700175 bool wantLocalFieldsEnabled,
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800176 const FaceCreatedCallback& onCreated,
Eric Newberry42602412016-08-27 09:33:18 -0700177 const FaceCreationFailedCallback& onFailure)
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800178{
Davide Pesavento46afec42017-05-28 14:28:47 -0400179 BOOST_ASSERT(remoteUri.isCanonical());
180
181 if (!localUri || localUri->getScheme() != "dev") {
182 NFD_LOG_TRACE("Cannot create unicast Ethernet face without dev:// LocalUri");
183 onFailure(406, "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme");
184 return;
185 }
186 BOOST_ASSERT(localUri->isCanonical());
187
188 if (persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
189 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
190 onFailure(406, "Outgoing Ethernet faces do not support on-demand persistency");
191 return;
192 }
193
194 ethernet::Address remoteEndpoint(ethernet::Address::fromString(remoteUri.getHost()));
195 std::string localEndpoint(localUri->getHost());
196
197 if (remoteEndpoint.isMulticast()) {
198 NFD_LOG_TRACE("createFace does not support multicast faces");
199 onFailure(406, "Cannot create multicast Ethernet faces");
200 return;
201 }
202
203 if (wantLocalFieldsEnabled) {
204 // Ethernet faces are never local
205 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
206 onFailure(406, "Local fields can only be enabled on faces with local scope");
207 return;
208 }
209
210 for (const auto& i : m_channels) {
211 if (i.first == localEndpoint) {
212 i.second->connect(remoteEndpoint, persistency, onCreated, onFailure);
213 return;
214 }
215 }
216
217 NFD_LOG_TRACE("No channels available to connect to " << remoteEndpoint);
218 onFailure(504, "No channels available to connect");
219}
220
221shared_ptr<EthernetChannel>
Junxiao Shi0d82d042017-07-07 06:15:27 +0000222EthernetFactory::createChannel(const shared_ptr<const ndn::net::NetworkInterface>& localEndpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -0400223 time::nanoseconds idleTimeout)
224{
Junxiao Shi0d82d042017-07-07 06:15:27 +0000225 auto it = m_channels.find(localEndpoint->getName());
Davide Pesavento46afec42017-05-28 14:28:47 -0400226 if (it != m_channels.end())
227 return it->second;
228
229 auto channel = std::make_shared<EthernetChannel>(localEndpoint, idleTimeout);
Junxiao Shi0d82d042017-07-07 06:15:27 +0000230 m_channels[localEndpoint->getName()] = channel;
Davide Pesavento46afec42017-05-28 14:28:47 -0400231
232 return channel;
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800233}
234
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200235std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600236EthernetFactory::getChannels() const
237{
Davide Pesavento46afec42017-05-28 14:28:47 -0400238 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600239}
240
Junxiao Shicde37ad2015-12-24 01:02:05 -0700241shared_ptr<Face>
Junxiao Shi0d82d042017-07-07 06:15:27 +0000242EthernetFactory::createMulticastFace(const ndn::net::NetworkInterface& netif,
Junxiao Shi7003c602017-01-10 13:35:28 +0000243 const ethernet::Address& address)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200244{
Junxiao Shi7003c602017-01-10 13:35:28 +0000245 BOOST_ASSERT(address.isMulticast());
246
Junxiao Shi0d82d042017-07-07 06:15:27 +0000247 auto key = std::make_pair(netif.getName(), address);
Junxiao Shi7003c602017-01-10 13:35:28 +0000248 auto found = m_mcastFaces.find(key);
249 if (found != m_mcastFaces.end()) {
250 return found->second;
251 }
252
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400253 GenericLinkService::Options opts;
Junxiao Shi7003c602017-01-10 13:35:28 +0000254 opts.allowFragmentation = true;
255 opts.allowReassembly = true;
256
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400257 auto linkService = make_unique<GenericLinkService>(opts);
258 auto transport = make_unique<MulticastEthernetTransport>(netif, address, m_mcastConfig.linkType);
Junxiao Shi7003c602017-01-10 13:35:28 +0000259 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
260
261 m_mcastFaces[key] = face;
262 connectFaceClosedSignal(*face, [this, key] { m_mcastFaces.erase(key); });
263
264 return face;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200265}
266
Junxiao Shi7003c602017-01-10 13:35:28 +0000267void
268EthernetFactory::applyConfig(const FaceSystem::ConfigContext& context)
269{
270 // collect old faces
271 std::set<shared_ptr<Face>> oldFaces;
272 boost::copy(m_mcastFaces | boost::adaptors::map_values,
273 std::inserter(oldFaces, oldFaces.end()));
274
275 if (m_mcastConfig.isEnabled) {
276 // determine interfaces on which faces should be created or retained
277 auto capableNetifs = context.listNetifs() |
Junxiao Shi0d82d042017-07-07 06:15:27 +0000278 boost::adaptors::transformed([] (const NetworkInterfaceInfo& nii) { return nii.asNetworkInterface(); }) |
279 boost::adaptors::filtered([this] (const shared_ptr<const ndn::net::NetworkInterface>& netif) {
280 return netif->isUp() && netif->canMulticast() && m_mcastConfig.netifPredicate(*netif);
281 });
Junxiao Shi7003c602017-01-10 13:35:28 +0000282
283 // create faces
284 for (const auto& netif : capableNetifs) {
285 shared_ptr<Face> face;
286 try {
Junxiao Shi0d82d042017-07-07 06:15:27 +0000287 face = this->createMulticastFace(*netif, m_mcastConfig.group);
Junxiao Shi7003c602017-01-10 13:35:28 +0000288 }
289 catch (const EthernetTransport::Error& e) {
Junxiao Shi0d82d042017-07-07 06:15:27 +0000290 NFD_LOG_WARN("Cannot create Ethernet multicast face on " << netif->getName() << ": " << e.what());
Junxiao Shi7003c602017-01-10 13:35:28 +0000291 continue;
292 }
293
294 if (face->getId() == face::INVALID_FACEID) {
295 // new face: register with forwarding
296 context.addFace(face);
297 }
298 else {
299 // existing face: don't destroy
300 oldFaces.erase(face);
301 }
302 }
303 }
304
305 // destroy old faces that are not needed in new configuration
306 for (const auto& face : oldFaces) {
307 face->close();
308 }
309}
310
311} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100312} // namespace nfd