blob: 20fda6d72231b5c47854d0df083b129939ede5b4 [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 "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() |
121 boost::adaptors::filtered([this] (const NetworkInterfaceInfo& netif) {
122 return netif.isUp() && !netif.isLoopback();
123 });
124
125 // create channels
126 for (const auto& netif : netifs) {
127 auto channel = this->createChannel(netif, time::seconds(idleTimeout));
128 if (wantListen && !channel->isListening()) {
129 try {
130 channel->listen(context.addFace, nullptr);
131 }
132 catch (const EthernetChannel::Error& e) {
133 NFD_LOG_WARN("Cannot listen on " << netif.name << ": " << e.what());
134 }
135 }
136 }
137 }
138 else if (!m_channels.empty()) {
139 NFD_LOG_WARN("Cannot disable dev channels after initialization");
140 }
141
Junxiao Shi7003c602017-01-10 13:35:28 +0000142 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
143 if (mcastConfig.isEnabled) {
144 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
145 }
146 else {
147 NFD_LOG_INFO("disabling multicast");
148 }
149 }
Teng Liangbfea5752017-03-29 04:51:10 +0000150 else if (mcastConfig.isEnabled) {
151 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
152 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
153 }
154 if (m_mcastConfig.group != mcastConfig.group) {
155 NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group <<
156 " to " << mcastConfig.group);
157 }
158 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
159 NFD_LOG_INFO("changing whitelist/blacklist");
160 }
Junxiao Shi7003c602017-01-10 13:35:28 +0000161 }
162
Teng Liangbfea5752017-03-29 04:51:10 +0000163 // Even if there's no configuration change, we still need to re-apply configuration because
164 // netifs may have changed.
Junxiao Shi7003c602017-01-10 13:35:28 +0000165 m_mcastConfig = mcastConfig;
166 this->applyConfig(context);
167 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100168}
169
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800170void
Eric Newberry78e32b02017-04-01 14:34:44 +0000171EthernetFactory::createFace(const FaceUri& remoteUri,
172 const ndn::optional<FaceUri>& localUri,
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800173 ndn::nfd::FacePersistency persistency,
Eric Newberryf40551a2016-09-05 15:41:16 -0700174 bool wantLocalFieldsEnabled,
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800175 const FaceCreatedCallback& onCreated,
Eric Newberry42602412016-08-27 09:33:18 -0700176 const FaceCreationFailedCallback& onFailure)
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800177{
Davide Pesavento46afec42017-05-28 14:28:47 -0400178 BOOST_ASSERT(remoteUri.isCanonical());
179
180 if (!localUri || localUri->getScheme() != "dev") {
181 NFD_LOG_TRACE("Cannot create unicast Ethernet face without dev:// LocalUri");
182 onFailure(406, "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme");
183 return;
184 }
185 BOOST_ASSERT(localUri->isCanonical());
186
187 if (persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
188 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
189 onFailure(406, "Outgoing Ethernet faces do not support on-demand persistency");
190 return;
191 }
192
193 ethernet::Address remoteEndpoint(ethernet::Address::fromString(remoteUri.getHost()));
194 std::string localEndpoint(localUri->getHost());
195
196 if (remoteEndpoint.isMulticast()) {
197 NFD_LOG_TRACE("createFace does not support multicast faces");
198 onFailure(406, "Cannot create multicast Ethernet faces");
199 return;
200 }
201
202 if (wantLocalFieldsEnabled) {
203 // Ethernet faces are never local
204 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
205 onFailure(406, "Local fields can only be enabled on faces with local scope");
206 return;
207 }
208
209 for (const auto& i : m_channels) {
210 if (i.first == localEndpoint) {
211 i.second->connect(remoteEndpoint, persistency, onCreated, onFailure);
212 return;
213 }
214 }
215
216 NFD_LOG_TRACE("No channels available to connect to " << remoteEndpoint);
217 onFailure(504, "No channels available to connect");
218}
219
220shared_ptr<EthernetChannel>
221EthernetFactory::createChannel(const NetworkInterfaceInfo& localEndpoint,
222 time::nanoseconds idleTimeout)
223{
224 auto it = m_channels.find(localEndpoint.name);
225 if (it != m_channels.end())
226 return it->second;
227
228 auto channel = std::make_shared<EthernetChannel>(localEndpoint, idleTimeout);
229 m_channels[localEndpoint.name] = channel;
230
231 return channel;
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800232}
233
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200234std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600235EthernetFactory::getChannels() const
236{
Davide Pesavento46afec42017-05-28 14:28:47 -0400237 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600238}
239
Junxiao Shicde37ad2015-12-24 01:02:05 -0700240shared_ptr<Face>
Junxiao Shi7003c602017-01-10 13:35:28 +0000241EthernetFactory::createMulticastFace(const NetworkInterfaceInfo& netif,
242 const ethernet::Address& address)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200243{
Junxiao Shi7003c602017-01-10 13:35:28 +0000244 BOOST_ASSERT(address.isMulticast());
245
246 auto key = std::make_pair(netif.name, address);
247 auto found = m_mcastFaces.find(key);
248 if (found != m_mcastFaces.end()) {
249 return found->second;
250 }
251
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400252 GenericLinkService::Options opts;
Junxiao Shi7003c602017-01-10 13:35:28 +0000253 opts.allowFragmentation = true;
254 opts.allowReassembly = true;
255
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400256 auto linkService = make_unique<GenericLinkService>(opts);
257 auto transport = make_unique<MulticastEthernetTransport>(netif, address, m_mcastConfig.linkType);
Junxiao Shi7003c602017-01-10 13:35:28 +0000258 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
259
260 m_mcastFaces[key] = face;
261 connectFaceClosedSignal(*face, [this, key] { m_mcastFaces.erase(key); });
262
263 return face;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200264}
265
Junxiao Shi7003c602017-01-10 13:35:28 +0000266void
267EthernetFactory::applyConfig(const FaceSystem::ConfigContext& context)
268{
269 // collect old faces
270 std::set<shared_ptr<Face>> oldFaces;
271 boost::copy(m_mcastFaces | boost::adaptors::map_values,
272 std::inserter(oldFaces, oldFaces.end()));
273
274 if (m_mcastConfig.isEnabled) {
275 // determine interfaces on which faces should be created or retained
276 auto capableNetifs = context.listNetifs() |
277 boost::adaptors::filtered([this] (const NetworkInterfaceInfo& netif) {
278 return netif.isUp() && netif.isMulticastCapable() &&
279 m_mcastConfig.netifPredicate(netif);
280 });
281
282 // create faces
283 for (const auto& netif : capableNetifs) {
284 shared_ptr<Face> face;
285 try {
286 face = this->createMulticastFace(netif, m_mcastConfig.group);
287 }
288 catch (const EthernetTransport::Error& e) {
Davide Pesavento46afec42017-05-28 14:28:47 -0400289 NFD_LOG_WARN("Cannot create Ethernet multicast face on " << netif.name << ": " << e.what());
Junxiao Shi7003c602017-01-10 13:35:28 +0000290 continue;
291 }
292
293 if (face->getId() == face::INVALID_FACEID) {
294 // new face: register with forwarding
295 context.addFace(face);
296 }
297 else {
298 // existing face: don't destroy
299 oldFaces.erase(face);
300 }
301 }
302 }
303
304 // destroy old faces that are not needed in new configuration
305 for (const auto& face : oldFaces) {
306 face->close();
307 }
308}
309
310} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100311} // namespace nfd