Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 25 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 26 | #include "ethernet-factory.hpp" |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 27 | #include "generic-link-service.hpp" |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 28 | #include "multicast-ethernet-transport.hpp" |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 29 | #include "core/logger.hpp" |
| 30 | #include <boost/range/adaptors.hpp> |
| 31 | #include <boost/range/algorithm/copy.hpp> |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 32 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 33 | namespace nfd { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 34 | namespace face { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 35 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 36 | NFD_LOG_INIT("EthernetFactory"); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 37 | NFD_REGISTER_PROTOCOL_FACTORY(EthernetFactory); |
| 38 | |
| 39 | const std::string& |
| 40 | EthernetFactory::getId() |
| 41 | { |
| 42 | static std::string id("ether"); |
| 43 | return id; |
| 44 | } |
| 45 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 46 | |
| 47 | void |
| 48 | EthernetFactory::processConfig(OptionalConfigSection configSection, |
| 49 | FaceSystem::ConfigContext& context) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 50 | { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 51 | // ether |
| 52 | // { |
| 53 | // mcast yes |
| 54 | // mcast_group 01:00:5E:00:17:AA |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 55 | // mcast_ad_hoc no |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 56 | // whitelist |
| 57 | // { |
| 58 | // * |
| 59 | // } |
| 60 | // blacklist |
| 61 | // { |
| 62 | // } |
| 63 | // } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 64 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 65 | MulticastConfig mcastConfig; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 66 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 67 | if (configSection) { |
| 68 | // face_system.ether.mcast defaults to 'yes' but only if face_system.ether section is present |
| 69 | mcastConfig.isEnabled = true; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 70 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 71 | for (const auto& pair : *configSection) { |
| 72 | const std::string& key = pair.first; |
| 73 | const ConfigSection& value = pair.second; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 74 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 75 | if (key == "mcast") { |
| 76 | mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "ether"); |
| 77 | } |
| 78 | else if (key == "mcast_group") { |
| 79 | const std::string& valueStr = value.get_value<std::string>(); |
| 80 | mcastConfig.group = ethernet::Address::fromString(valueStr); |
| 81 | if (mcastConfig.group.isNull()) { |
| 82 | BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" + |
| 83 | valueStr + "' cannot be parsed as an Ethernet address")); |
| 84 | } |
| 85 | else if (!mcastConfig.group.isMulticast()) { |
| 86 | BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" + |
| 87 | valueStr + "' is not a multicast address")); |
| 88 | } |
| 89 | } |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 90 | else if (key == "mcast_ad_hoc") { |
| 91 | bool wantAdHoc = ConfigFile::parseYesNo(pair, "ether"); |
| 92 | mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS; |
| 93 | } |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 94 | else if (key == "whitelist") { |
| 95 | mcastConfig.netifPredicate.parseWhitelist(value); |
| 96 | } |
| 97 | else if (key == "blacklist") { |
| 98 | mcastConfig.netifPredicate.parseBlacklist(value); |
| 99 | } |
| 100 | else { |
| 101 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.ether." + key)); |
| 102 | } |
| 103 | } |
| 104 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 105 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 106 | if (!context.isDryRun) { |
| 107 | if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) { |
| 108 | if (mcastConfig.isEnabled) { |
| 109 | NFD_LOG_INFO("enabling multicast on " << mcastConfig.group); |
| 110 | } |
| 111 | else { |
| 112 | NFD_LOG_INFO("disabling multicast"); |
| 113 | } |
| 114 | } |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 115 | else if (mcastConfig.isEnabled) { |
| 116 | if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) { |
| 117 | NFD_LOG_WARN("Cannot change ad hoc setting on existing faces"); |
| 118 | } |
| 119 | if (m_mcastConfig.group != mcastConfig.group) { |
| 120 | NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group << |
| 121 | " to " << mcastConfig.group); |
| 122 | } |
| 123 | if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) { |
| 124 | NFD_LOG_INFO("changing whitelist/blacklist"); |
| 125 | } |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 128 | // Even if there's no configuration change, we still need to re-apply configuration because |
| 129 | // netifs may have changed. |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 130 | m_mcastConfig = mcastConfig; |
| 131 | this->applyConfig(context); |
| 132 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 135 | void |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame^] | 136 | EthernetFactory::createFace(const FaceUri& remoteUri, |
| 137 | const ndn::optional<FaceUri>& localUri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 138 | ndn::nfd::FacePersistency persistency, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 139 | bool wantLocalFieldsEnabled, |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 140 | const FaceCreatedCallback& onCreated, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 141 | const FaceCreationFailedCallback& onFailure) |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 142 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 143 | onFailure(406, "Unsupported protocol"); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 146 | std::vector<shared_ptr<const Channel>> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 147 | EthernetFactory::getChannels() const |
| 148 | { |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 149 | return {}; |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 150 | } |
| 151 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 152 | shared_ptr<Face> |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 153 | EthernetFactory::createMulticastFace(const NetworkInterfaceInfo& netif, |
| 154 | const ethernet::Address& address) |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 155 | { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 156 | BOOST_ASSERT(address.isMulticast()); |
| 157 | |
| 158 | auto key = std::make_pair(netif.name, address); |
| 159 | auto found = m_mcastFaces.find(key); |
| 160 | if (found != m_mcastFaces.end()) { |
| 161 | return found->second; |
| 162 | } |
| 163 | |
| 164 | face::GenericLinkService::Options opts; |
| 165 | opts.allowFragmentation = true; |
| 166 | opts.allowReassembly = true; |
| 167 | |
| 168 | auto linkService = make_unique<face::GenericLinkService>(opts); |
Davide Pesavento | 6bd6d0b | 2017-03-25 15:16:40 -0400 | [diff] [blame] | 169 | auto transport = make_unique<face::MulticastEthernetTransport>(netif, address, m_mcastConfig.linkType); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 170 | auto face = make_shared<Face>(std::move(linkService), std::move(transport)); |
| 171 | |
| 172 | m_mcastFaces[key] = face; |
| 173 | connectFaceClosedSignal(*face, [this, key] { m_mcastFaces.erase(key); }); |
| 174 | |
| 175 | return face; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 178 | void |
| 179 | EthernetFactory::applyConfig(const FaceSystem::ConfigContext& context) |
| 180 | { |
| 181 | // collect old faces |
| 182 | std::set<shared_ptr<Face>> oldFaces; |
| 183 | boost::copy(m_mcastFaces | boost::adaptors::map_values, |
| 184 | std::inserter(oldFaces, oldFaces.end())); |
| 185 | |
| 186 | if (m_mcastConfig.isEnabled) { |
| 187 | // determine interfaces on which faces should be created or retained |
| 188 | auto capableNetifs = context.listNetifs() | |
| 189 | boost::adaptors::filtered([this] (const NetworkInterfaceInfo& netif) { |
| 190 | return netif.isUp() && netif.isMulticastCapable() && |
| 191 | m_mcastConfig.netifPredicate(netif); |
| 192 | }); |
| 193 | |
| 194 | // create faces |
| 195 | for (const auto& netif : capableNetifs) { |
| 196 | shared_ptr<Face> face; |
| 197 | try { |
| 198 | face = this->createMulticastFace(netif, m_mcastConfig.group); |
| 199 | } |
| 200 | catch (const EthernetTransport::Error& e) { |
| 201 | NFD_LOG_ERROR("Cannot create Ethernet multicast face on " << netif.name << ": " << |
| 202 | e.what() << ", continuing"); |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | if (face->getId() == face::INVALID_FACEID) { |
| 207 | // new face: register with forwarding |
| 208 | context.addFace(face); |
| 209 | } |
| 210 | else { |
| 211 | // existing face: don't destroy |
| 212 | oldFaces.erase(face); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // destroy old faces that are not needed in new configuration |
| 218 | for (const auto& face : oldFaces) { |
| 219 | face->close(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | } // namespace face |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 224 | } // namespace nfd |