blob: 96599856334157e73cc039c6cb840753babd7ff9 [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/*
Eric Newberrycb6551e2020-03-02 14:12:16 -08003 * Copyright (c) 2014-2020, 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"
Davide Pesavento15b55052018-01-27 19:09:28 -050029
Junxiao Shi7003c602017-01-10 13:35:28 +000030#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
Davide Pesaventoa3148082018-04-12 18:21:54 -040036NFD_LOG_INIT(EthernetFactory);
Junxiao Shib47247d2017-01-24 15:09:16 +000037NFD_REGISTER_PROTOCOL_FACTORY(EthernetFactory);
38
39const std::string&
Davide Pesaventod27841b2018-11-13 00:22:24 -050040EthernetFactory::getId() noexcept
Junxiao Shib47247d2017-01-24 15:09:16 +000041{
42 static std::string id("ether");
43 return id;
44}
45
Junxiao Shi0ba6d642017-07-17 00:53:22 +000046EthernetFactory::EthernetFactory(const CtorParams& params)
47 : ProtocolFactory(params)
48{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040049 m_netifAddConn = netmon->onInterfaceAdded.connect([this] (const auto& netif) {
50 this->applyUnicastConfigToNetif(netif);
51 this->applyMcastConfigToNetif(*netif);
52 });
Junxiao Shi0ba6d642017-07-17 00:53:22 +000053}
Junxiao Shi7003c602017-01-10 13:35:28 +000054
55void
Davide Pesaventod27841b2018-11-13 00:22:24 -050056EthernetFactory::doProcessConfig(OptionalConfigSection configSection,
57 FaceSystem::ConfigContext& context)
Davide Pesavento44deacc2014-02-19 10:48:07 +010058{
Junxiao Shi7003c602017-01-10 13:35:28 +000059 // ether
60 // {
Davide Pesavento46afec42017-05-28 14:28:47 -040061 // listen yes
62 // idle_timeout 600
Junxiao Shi7003c602017-01-10 13:35:28 +000063 // mcast yes
64 // mcast_group 01:00:5E:00:17:AA
Teng Liangbfea5752017-03-29 04:51:10 +000065 // mcast_ad_hoc no
Junxiao Shi7003c602017-01-10 13:35:28 +000066 // whitelist
67 // {
68 // *
69 // }
70 // blacklist
71 // {
72 // }
73 // }
Davide Pesavento44deacc2014-02-19 10:48:07 +010074
Junxiao Shi79a92082017-08-08 02:40:59 +000075 UnicastConfig unicastConfig;
Junxiao Shi7003c602017-01-10 13:35:28 +000076 MulticastConfig mcastConfig;
Davide Pesavento44deacc2014-02-19 10:48:07 +010077
Junxiao Shi7003c602017-01-10 13:35:28 +000078 if (configSection) {
Junxiao Shi79a92082017-08-08 02:40:59 +000079 // listen and mcast default to 'yes' but only if face_system.ether section is present
80 unicastConfig.isEnabled = unicastConfig.wantListen = mcastConfig.isEnabled = true;
Davide Pesavento35120ea2015-11-17 21:13:18 +010081
Junxiao Shi7003c602017-01-10 13:35:28 +000082 for (const auto& pair : *configSection) {
83 const std::string& key = pair.first;
84 const ConfigSection& value = pair.second;
Davide Pesavento7726ae52014-11-23 21:01:05 +010085
Davide Pesavento46afec42017-05-28 14:28:47 -040086 if (key == "listen") {
Junxiao Shi79a92082017-08-08 02:40:59 +000087 unicastConfig.wantListen = ConfigFile::parseYesNo(pair, "face_system.ether");
Davide Pesavento46afec42017-05-28 14:28:47 -040088 }
89 else if (key == "idle_timeout") {
Junxiao Shi79a92082017-08-08 02:40:59 +000090 unicastConfig.idleTimeout = time::seconds(ConfigFile::parseNumber<uint32_t>(pair, "face_system.ether"));
Davide Pesavento46afec42017-05-28 14:28:47 -040091 }
92 else if (key == "mcast") {
93 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.ether");
Junxiao Shi7003c602017-01-10 13:35:28 +000094 }
95 else if (key == "mcast_group") {
96 const std::string& valueStr = value.get_value<std::string>();
97 mcastConfig.group = ethernet::Address::fromString(valueStr);
98 if (mcastConfig.group.isNull()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050099 NDN_THROW(ConfigFile::Error("face_system.ether.mcast_group: '" +
100 valueStr + "' cannot be parsed as an Ethernet address"));
Junxiao Shi7003c602017-01-10 13:35:28 +0000101 }
102 else if (!mcastConfig.group.isMulticast()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500103 NDN_THROW(ConfigFile::Error("face_system.ether.mcast_group: '" +
104 valueStr + "' is not a multicast address"));
Junxiao Shi7003c602017-01-10 13:35:28 +0000105 }
106 }
Teng Liangbfea5752017-03-29 04:51:10 +0000107 else if (key == "mcast_ad_hoc") {
Davide Pesavento46afec42017-05-28 14:28:47 -0400108 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.ether");
Teng Liangbfea5752017-03-29 04:51:10 +0000109 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
110 }
Junxiao Shi7003c602017-01-10 13:35:28 +0000111 else if (key == "whitelist") {
112 mcastConfig.netifPredicate.parseWhitelist(value);
113 }
114 else if (key == "blacklist") {
115 mcastConfig.netifPredicate.parseBlacklist(value);
116 }
117 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500118 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.ether." + key));
Junxiao Shi7003c602017-01-10 13:35:28 +0000119 }
120 }
121 }
Davide Pesavento44deacc2014-02-19 10:48:07 +0100122
Junxiao Shi79a92082017-08-08 02:40:59 +0000123 if (context.isDryRun) {
124 return;
Junxiao Shi7003c602017-01-10 13:35:28 +0000125 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000126
127 if (unicastConfig.isEnabled) {
128 if (m_unicastConfig.wantListen && !unicastConfig.wantListen && !m_channels.empty()) {
129 NFD_LOG_WARN("Cannot stop listening on Ethernet channels");
130 }
131 if (m_unicastConfig.idleTimeout != unicastConfig.idleTimeout && !m_channels.empty()) {
132 NFD_LOG_WARN("Idle timeout setting applies to new Ethernet channels only");
133 }
134 }
135 else if (m_unicastConfig.isEnabled && !m_channels.empty()) {
136 NFD_LOG_WARN("Cannot disable Ethernet channels after initialization");
137 }
138
139 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
140 if (mcastConfig.isEnabled) {
141 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
142 }
143 else {
144 NFD_LOG_INFO("disabling multicast");
145 }
146 }
147 else if (mcastConfig.isEnabled) {
148 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
149 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
150 }
151 if (m_mcastConfig.group != mcastConfig.group) {
152 NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group <<
153 " to " << mcastConfig.group);
154 }
155 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
156 NFD_LOG_INFO("changing whitelist/blacklist");
157 }
158 }
159
160 // Even if there's no configuration change, we still need to re-apply configuration because
161 // netifs may have changed.
162 m_unicastConfig = unicastConfig;
163 m_mcastConfig = mcastConfig;
164 this->applyConfig(context);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100165}
166
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800167void
Davide Pesaventod27841b2018-11-13 00:22:24 -0500168EthernetFactory::doCreateFace(const CreateFaceRequest& req,
169 const FaceCreatedCallback& onCreated,
170 const FaceCreationFailedCallback& onFailure)
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800171{
Davide Pesavento15b55052018-01-27 19:09:28 -0500172 if (!req.localUri || req.localUri->getScheme() != "dev") {
Davide Pesavento46afec42017-05-28 14:28:47 -0400173 NFD_LOG_TRACE("Cannot create unicast Ethernet face without dev:// LocalUri");
174 onFailure(406, "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme");
175 return;
176 }
Davide Pesavento46afec42017-05-28 14:28:47 -0400177
Davide Pesavento15b55052018-01-27 19:09:28 -0500178 if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Davide Pesavento46afec42017-05-28 14:28:47 -0400179 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
180 onFailure(406, "Outgoing Ethernet faces do not support on-demand persistency");
181 return;
182 }
183
Davide Pesavento15b55052018-01-27 19:09:28 -0500184 ethernet::Address remoteEndpoint(ethernet::Address::fromString(req.remoteUri.getHost()));
185 std::string localEndpoint(req.localUri->getHost());
Davide Pesavento46afec42017-05-28 14:28:47 -0400186
187 if (remoteEndpoint.isMulticast()) {
188 NFD_LOG_TRACE("createFace does not support multicast faces");
189 onFailure(406, "Cannot create multicast Ethernet faces");
190 return;
191 }
192
Davide Pesavento15b55052018-01-27 19:09:28 -0500193 if (req.params.wantLocalFields) {
Davide Pesavento46afec42017-05-28 14:28:47 -0400194 // Ethernet faces are never local
195 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
196 onFailure(406, "Local fields can only be enabled on faces with local scope");
197 return;
198 }
199
Eric Newberrycb6551e2020-03-02 14:12:16 -0800200 if (req.params.mtu && *req.params.mtu < MIN_MTU) {
Eric Newberry812d6152018-06-06 15:06:01 -0700201 // The specified MTU must be greater than the minimum possible
Eric Newberrycb6551e2020-03-02 14:12:16 -0800202 NFD_LOG_TRACE("createFace: override MTU cannot be less than " << MIN_MTU);
203 onFailure(406, "Override MTU cannot be less than " + to_string(MIN_MTU));
Eric Newberry812d6152018-06-06 15:06:01 -0700204 return;
205 }
206
Davide Pesavento46afec42017-05-28 14:28:47 -0400207 for (const auto& i : m_channels) {
208 if (i.first == localEndpoint) {
Davide Pesavento15b55052018-01-27 19:09:28 -0500209 i.second->connect(remoteEndpoint, req.params, onCreated, onFailure);
Davide Pesavento46afec42017-05-28 14:28:47 -0400210 return;
211 }
212 }
213
214 NFD_LOG_TRACE("No channels available to connect to " << remoteEndpoint);
215 onFailure(504, "No channels available to connect");
216}
217
218shared_ptr<EthernetChannel>
Junxiao Shi0d82d042017-07-07 06:15:27 +0000219EthernetFactory::createChannel(const shared_ptr<const ndn::net::NetworkInterface>& localEndpoint,
Davide Pesavento46afec42017-05-28 14:28:47 -0400220 time::nanoseconds idleTimeout)
221{
Junxiao Shi0d82d042017-07-07 06:15:27 +0000222 auto it = m_channels.find(localEndpoint->getName());
Davide Pesavento46afec42017-05-28 14:28:47 -0400223 if (it != m_channels.end())
224 return it->second;
225
226 auto channel = std::make_shared<EthernetChannel>(localEndpoint, idleTimeout);
Junxiao Shi0d82d042017-07-07 06:15:27 +0000227 m_channels[localEndpoint->getName()] = channel;
Davide Pesavento46afec42017-05-28 14:28:47 -0400228 return channel;
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800229}
230
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200231std::vector<shared_ptr<const Channel>>
Davide Pesaventod27841b2018-11-13 00:22:24 -0500232EthernetFactory::doGetChannels() const
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600233{
Davide Pesavento46afec42017-05-28 14:28:47 -0400234 return getChannelsFromMap(m_channels);
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600235}
236
Junxiao Shicde37ad2015-12-24 01:02:05 -0700237shared_ptr<Face>
Junxiao Shi0d82d042017-07-07 06:15:27 +0000238EthernetFactory::createMulticastFace(const ndn::net::NetworkInterface& netif,
Junxiao Shi7003c602017-01-10 13:35:28 +0000239 const ethernet::Address& address)
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200240{
Junxiao Shi7003c602017-01-10 13:35:28 +0000241 BOOST_ASSERT(address.isMulticast());
242
Junxiao Shi0d82d042017-07-07 06:15:27 +0000243 auto key = std::make_pair(netif.getName(), address);
Junxiao Shi7003c602017-01-10 13:35:28 +0000244 auto found = m_mcastFaces.find(key);
245 if (found != m_mcastFaces.end()) {
246 return found->second;
247 }
248
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400249 GenericLinkService::Options opts;
Junxiao Shi7003c602017-01-10 13:35:28 +0000250 opts.allowFragmentation = true;
251 opts.allowReassembly = true;
252
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400253 auto linkService = make_unique<GenericLinkService>(opts);
254 auto transport = make_unique<MulticastEthernetTransport>(netif, address, m_mcastConfig.linkType);
Junxiao Shi7003c602017-01-10 13:35:28 +0000255 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
256
257 m_mcastFaces[key] = face;
258 connectFaceClosedSignal(*face, [this, key] { m_mcastFaces.erase(key); });
259
260 return face;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200261}
262
Junxiao Shi79a92082017-08-08 02:40:59 +0000263shared_ptr<EthernetChannel>
264EthernetFactory::applyUnicastConfigToNetif(const shared_ptr<const ndn::net::NetworkInterface>& netif)
265{
266 if (!m_unicastConfig.isEnabled) {
267 return nullptr;
268 }
269
Davide Pesavento61265472017-08-08 15:48:10 -0400270 if (netif->getType() != ndn::net::InterfaceType::ETHERNET) {
271 NFD_LOG_DEBUG("Not creating channel on " << netif->getName() << ": incompatible netif type");
272 return nullptr;
273 }
274
Junxiao Shi79a92082017-08-08 02:40:59 +0000275 if (!netif->isUp()) {
276 NFD_LOG_DEBUG("Not creating channel on " << netif->getName() << ": netif is down");
277 return nullptr;
278 }
279
Davide Pesavento61265472017-08-08 15:48:10 -0400280 if (netif->getEthernetAddress().isNull()) {
281 NFD_LOG_DEBUG("Not creating channel on " << netif->getName() << ": invalid Ethernet address");
Junxiao Shi79a92082017-08-08 02:40:59 +0000282 return nullptr;
283 }
284
285 auto channel = this->createChannel(netif, m_unicastConfig.idleTimeout);
286 if (m_unicastConfig.wantListen && !channel->isListening()) {
287 try {
288 channel->listen(this->addFace, nullptr);
289 }
290 catch (const EthernetChannel::Error& e) {
291 NFD_LOG_WARN("Cannot listen on " << netif->getName() << ": " << e.what());
292 }
293 }
294 return channel;
295}
296
297shared_ptr<Face>
298EthernetFactory::applyMcastConfigToNetif(const ndn::net::NetworkInterface& netif)
299{
300 if (!m_mcastConfig.isEnabled) {
301 return nullptr;
302 }
303
Davide Pesavento61265472017-08-08 15:48:10 -0400304 if (netif.getType() != ndn::net::InterfaceType::ETHERNET) {
305 NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": incompatible netif type");
306 return nullptr;
307 }
308
Junxiao Shi79a92082017-08-08 02:40:59 +0000309 if (!netif.isUp()) {
310 NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": netif is down");
311 return nullptr;
312 }
313
314 if (!netif.canMulticast()) {
315 NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": netif cannot multicast");
316 return nullptr;
317 }
318
Davide Pesavento61265472017-08-08 15:48:10 -0400319 if (netif.getEthernetAddress().isNull()) {
320 NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": invalid Ethernet address");
321 return nullptr;
322 }
323
Junxiao Shi79a92082017-08-08 02:40:59 +0000324 if (!m_mcastConfig.netifPredicate(netif)) {
325 NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": rejected by whitelist/blacklist");
326 return nullptr;
327 }
328
329 NFD_LOG_DEBUG("Creating multicast face on " << netif.getName());
330 shared_ptr<Face> face;
331 try {
332 face = this->createMulticastFace(netif, m_mcastConfig.group);
333 }
334 catch (const EthernetTransport::Error& e) {
335 NFD_LOG_WARN("Cannot create multicast face on " << netif.getName() << ": " << e.what());
336 return nullptr;
337 }
338
339 if (face->getId() == face::INVALID_FACEID) {
340 // new face: register with forwarding
341 this->addFace(face);
342 }
343 return face;
344}
345
Junxiao Shi7003c602017-01-10 13:35:28 +0000346void
Davide Pesaventod27841b2018-11-13 00:22:24 -0500347EthernetFactory::applyConfig(const FaceSystem::ConfigContext&)
Junxiao Shi7003c602017-01-10 13:35:28 +0000348{
Junxiao Shi79a92082017-08-08 02:40:59 +0000349 if (m_unicastConfig.isEnabled) {
350 providedSchemes.insert("ether");
351 }
352 else {
353 providedSchemes.erase("ether");
354 }
355
356 // collect old multicast faces
Junxiao Shi7003c602017-01-10 13:35:28 +0000357 std::set<shared_ptr<Face>> oldFaces;
Junxiao Shi79a92082017-08-08 02:40:59 +0000358 boost::copy(m_mcastFaces | boost::adaptors::map_values, std::inserter(oldFaces, oldFaces.end()));
Junxiao Shi7003c602017-01-10 13:35:28 +0000359
Junxiao Shi79a92082017-08-08 02:40:59 +0000360 // create channels and multicast faces if requested by config
361 for (const auto& netif : netmon->listNetworkInterfaces()) {
362 this->applyUnicastConfigToNetif(netif);
Junxiao Shi7003c602017-01-10 13:35:28 +0000363
Junxiao Shi79a92082017-08-08 02:40:59 +0000364 auto face = this->applyMcastConfigToNetif(*netif);
365 if (face != nullptr) {
366 // don't destroy face
367 oldFaces.erase(face);
Junxiao Shi7003c602017-01-10 13:35:28 +0000368 }
369 }
370
Junxiao Shi79a92082017-08-08 02:40:59 +0000371 // destroy old multicast faces that are not needed in new configuration
Junxiao Shi7003c602017-01-10 13:35:28 +0000372 for (const auto& face : oldFaces) {
373 face->close();
374 }
375}
376
377} // namespace face
Davide Pesavento44deacc2014-02-19 10:48:07 +0100378} // namespace nfd