Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 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() | |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 121 | 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 Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 125 | |
| 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 Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 134 | NFD_LOG_WARN("Cannot listen on " << netif->getName() << ": " << e.what()); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | else if (!m_channels.empty()) { |
| 140 | NFD_LOG_WARN("Cannot disable dev channels after initialization"); |
| 141 | } |
| 142 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 143 | 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 Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 151 | 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 Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 164 | // Even if there's no configuration change, we still need to re-apply configuration because |
| 165 | // netifs may have changed. |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 166 | m_mcastConfig = mcastConfig; |
| 167 | this->applyConfig(context); |
| 168 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 171 | void |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 172 | EthernetFactory::createFace(const FaceUri& remoteUri, |
| 173 | const ndn::optional<FaceUri>& localUri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 174 | ndn::nfd::FacePersistency persistency, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 175 | bool wantLocalFieldsEnabled, |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 176 | const FaceCreatedCallback& onCreated, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 177 | const FaceCreationFailedCallback& onFailure) |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 178 | { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 179 | 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 | |
| 221 | shared_ptr<EthernetChannel> |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 222 | EthernetFactory::createChannel(const shared_ptr<const ndn::net::NetworkInterface>& localEndpoint, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 223 | time::nanoseconds idleTimeout) |
| 224 | { |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 225 | auto it = m_channels.find(localEndpoint->getName()); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 226 | if (it != m_channels.end()) |
| 227 | return it->second; |
| 228 | |
| 229 | auto channel = std::make_shared<EthernetChannel>(localEndpoint, idleTimeout); |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 230 | m_channels[localEndpoint->getName()] = channel; |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 231 | |
| 232 | return channel; |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 235 | std::vector<shared_ptr<const Channel>> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 236 | EthernetFactory::getChannels() const |
| 237 | { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 238 | return getChannelsFromMap(m_channels); |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 239 | } |
| 240 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 241 | shared_ptr<Face> |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 242 | EthernetFactory::createMulticastFace(const ndn::net::NetworkInterface& netif, |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 243 | const ethernet::Address& address) |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 244 | { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 245 | BOOST_ASSERT(address.isMulticast()); |
| 246 | |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 247 | auto key = std::make_pair(netif.getName(), address); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 248 | auto found = m_mcastFaces.find(key); |
| 249 | if (found != m_mcastFaces.end()) { |
| 250 | return found->second; |
| 251 | } |
| 252 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 253 | GenericLinkService::Options opts; |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 254 | opts.allowFragmentation = true; |
| 255 | opts.allowReassembly = true; |
| 256 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 257 | auto linkService = make_unique<GenericLinkService>(opts); |
| 258 | auto transport = make_unique<MulticastEthernetTransport>(netif, address, m_mcastConfig.linkType); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 259 | 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 Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 267 | void |
| 268 | EthernetFactory::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 Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 278 | 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 Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 282 | |
| 283 | // create faces |
| 284 | for (const auto& netif : capableNetifs) { |
| 285 | shared_ptr<Face> face; |
| 286 | try { |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 287 | face = this->createMulticastFace(*netif, m_mcastConfig.group); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 288 | } |
| 289 | catch (const EthernetTransport::Error& e) { |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 290 | NFD_LOG_WARN("Cannot create Ethernet multicast face on " << netif->getName() << ": " << e.what()); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 291 | 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 Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 312 | } // namespace nfd |