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 <boost/range/adaptors.hpp> |
| 30 | #include <boost/range/algorithm/copy.hpp> |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 31 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 32 | namespace nfd { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 33 | namespace face { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 34 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 35 | NFD_LOG_INIT("EthernetFactory"); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 36 | NFD_REGISTER_PROTOCOL_FACTORY(EthernetFactory); |
| 37 | |
| 38 | const std::string& |
| 39 | EthernetFactory::getId() |
| 40 | { |
| 41 | static std::string id("ether"); |
| 42 | return id; |
| 43 | } |
| 44 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 45 | |
| 46 | void |
| 47 | EthernetFactory::processConfig(OptionalConfigSection configSection, |
| 48 | FaceSystem::ConfigContext& context) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 49 | { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 50 | // ether |
| 51 | // { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 52 | // listen yes |
| 53 | // idle_timeout 600 |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 54 | // mcast yes |
| 55 | // mcast_group 01:00:5E:00:17:AA |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 56 | // mcast_ad_hoc no |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 57 | // whitelist |
| 58 | // { |
| 59 | // * |
| 60 | // } |
| 61 | // blacklist |
| 62 | // { |
| 63 | // } |
| 64 | // } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 65 | |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 66 | bool wantListen = true; |
| 67 | uint32_t idleTimeout = 600; |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 68 | MulticastConfig mcastConfig; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 69 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 70 | if (configSection) { |
| 71 | // face_system.ether.mcast defaults to 'yes' but only if face_system.ether section is present |
| 72 | mcastConfig.isEnabled = true; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 73 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 74 | for (const auto& pair : *configSection) { |
| 75 | const std::string& key = pair.first; |
| 76 | const ConfigSection& value = pair.second; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 77 | |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 78 | 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 Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 86 | } |
| 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 Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 99 | else if (key == "mcast_ad_hoc") { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 100 | bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.ether"); |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 101 | mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS; |
| 102 | } |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 103 | 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 Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 114 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 115 | if (!context.isDryRun) { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 116 | 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 Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 142 | 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 Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 150 | 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 Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 163 | // Even if there's no configuration change, we still need to re-apply configuration because |
| 164 | // netifs may have changed. |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 165 | m_mcastConfig = mcastConfig; |
| 166 | this->applyConfig(context); |
| 167 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 170 | void |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 171 | EthernetFactory::createFace(const FaceUri& remoteUri, |
| 172 | const ndn::optional<FaceUri>& localUri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 173 | ndn::nfd::FacePersistency persistency, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 174 | bool wantLocalFieldsEnabled, |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 175 | const FaceCreatedCallback& onCreated, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 176 | const FaceCreationFailedCallback& onFailure) |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 177 | { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 178 | 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 | |
| 220 | shared_ptr<EthernetChannel> |
| 221 | EthernetFactory::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 Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 234 | std::vector<shared_ptr<const Channel>> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 235 | EthernetFactory::getChannels() const |
| 236 | { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 237 | return getChannelsFromMap(m_channels); |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 238 | } |
| 239 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 240 | shared_ptr<Face> |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 241 | EthernetFactory::createMulticastFace(const NetworkInterfaceInfo& netif, |
| 242 | const ethernet::Address& address) |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 243 | { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 244 | 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 Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 252 | GenericLinkService::Options opts; |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 253 | opts.allowFragmentation = true; |
| 254 | opts.allowReassembly = true; |
| 255 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 256 | auto linkService = make_unique<GenericLinkService>(opts); |
| 257 | auto transport = make_unique<MulticastEthernetTransport>(netif, address, m_mcastConfig.linkType); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 258 | 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 Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 266 | void |
| 267 | EthernetFactory::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 Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 289 | NFD_LOG_WARN("Cannot create Ethernet multicast face on " << netif.name << ": " << e.what()); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 290 | 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 Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 311 | } // namespace nfd |