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 | /* |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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" |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 29 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 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 | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 46 | EthernetFactory::EthernetFactory(const CtorParams& params) |
| 47 | : ProtocolFactory(params) |
| 48 | { |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 49 | m_netifAddConn = netmon->onInterfaceAdded.connect( |
| 50 | [this] (const shared_ptr<const ndn::net::NetworkInterface>& netif) { |
| 51 | this->applyUnicastConfigToNetif(netif); |
| 52 | this->applyMcastConfigToNetif(*netif); |
| 53 | }); |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 54 | } |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 55 | |
| 56 | void |
| 57 | EthernetFactory::processConfig(OptionalConfigSection configSection, |
| 58 | FaceSystem::ConfigContext& context) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 59 | { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 60 | // ether |
| 61 | // { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 62 | // listen yes |
| 63 | // idle_timeout 600 |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 64 | // mcast yes |
| 65 | // mcast_group 01:00:5E:00:17:AA |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 66 | // mcast_ad_hoc no |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 67 | // whitelist |
| 68 | // { |
| 69 | // * |
| 70 | // } |
| 71 | // blacklist |
| 72 | // { |
| 73 | // } |
| 74 | // } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 75 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 76 | UnicastConfig unicastConfig; |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 77 | MulticastConfig mcastConfig; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 78 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 79 | if (configSection) { |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 80 | // listen and mcast default to 'yes' but only if face_system.ether section is present |
| 81 | unicastConfig.isEnabled = unicastConfig.wantListen = mcastConfig.isEnabled = true; |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 82 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 83 | for (const auto& pair : *configSection) { |
| 84 | const std::string& key = pair.first; |
| 85 | const ConfigSection& value = pair.second; |
Davide Pesavento | 7726ae5 | 2014-11-23 21:01:05 +0100 | [diff] [blame] | 86 | |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 87 | if (key == "listen") { |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 88 | unicastConfig.wantListen = ConfigFile::parseYesNo(pair, "face_system.ether"); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 89 | } |
| 90 | else if (key == "idle_timeout") { |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 91 | unicastConfig.idleTimeout = time::seconds(ConfigFile::parseNumber<uint32_t>(pair, "face_system.ether")); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 92 | } |
| 93 | else if (key == "mcast") { |
| 94 | mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.ether"); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 95 | } |
| 96 | else if (key == "mcast_group") { |
| 97 | const std::string& valueStr = value.get_value<std::string>(); |
| 98 | mcastConfig.group = ethernet::Address::fromString(valueStr); |
| 99 | if (mcastConfig.group.isNull()) { |
| 100 | BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" + |
| 101 | valueStr + "' cannot be parsed as an Ethernet address")); |
| 102 | } |
| 103 | else if (!mcastConfig.group.isMulticast()) { |
| 104 | BOOST_THROW_EXCEPTION(ConfigFile::Error("face_system.ether.mcast_group: '" + |
| 105 | valueStr + "' is not a multicast address")); |
| 106 | } |
| 107 | } |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 108 | else if (key == "mcast_ad_hoc") { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 109 | bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.ether"); |
Teng Liang | bfea575 | 2017-03-29 04:51:10 +0000 | [diff] [blame] | 110 | mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS; |
| 111 | } |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 112 | else if (key == "whitelist") { |
| 113 | mcastConfig.netifPredicate.parseWhitelist(value); |
| 114 | } |
| 115 | else if (key == "blacklist") { |
| 116 | mcastConfig.netifPredicate.parseBlacklist(value); |
| 117 | } |
| 118 | else { |
| 119 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.ether." + key)); |
| 120 | } |
| 121 | } |
| 122 | } |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 123 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 124 | if (context.isDryRun) { |
| 125 | return; |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 126 | } |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 127 | |
| 128 | if (unicastConfig.isEnabled) { |
| 129 | if (m_unicastConfig.wantListen && !unicastConfig.wantListen && !m_channels.empty()) { |
| 130 | NFD_LOG_WARN("Cannot stop listening on Ethernet channels"); |
| 131 | } |
| 132 | if (m_unicastConfig.idleTimeout != unicastConfig.idleTimeout && !m_channels.empty()) { |
| 133 | NFD_LOG_WARN("Idle timeout setting applies to new Ethernet channels only"); |
| 134 | } |
| 135 | } |
| 136 | else if (m_unicastConfig.isEnabled && !m_channels.empty()) { |
| 137 | NFD_LOG_WARN("Cannot disable Ethernet channels after initialization"); |
| 138 | } |
| 139 | |
| 140 | if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) { |
| 141 | if (mcastConfig.isEnabled) { |
| 142 | NFD_LOG_INFO("enabling multicast on " << mcastConfig.group); |
| 143 | } |
| 144 | else { |
| 145 | NFD_LOG_INFO("disabling multicast"); |
| 146 | } |
| 147 | } |
| 148 | else if (mcastConfig.isEnabled) { |
| 149 | if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) { |
| 150 | NFD_LOG_WARN("Cannot change ad hoc setting on existing faces"); |
| 151 | } |
| 152 | if (m_mcastConfig.group != mcastConfig.group) { |
| 153 | NFD_LOG_INFO("changing multicast group from " << m_mcastConfig.group << |
| 154 | " to " << mcastConfig.group); |
| 155 | } |
| 156 | if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) { |
| 157 | NFD_LOG_INFO("changing whitelist/blacklist"); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Even if there's no configuration change, we still need to re-apply configuration because |
| 162 | // netifs may have changed. |
| 163 | m_unicastConfig = unicastConfig; |
| 164 | m_mcastConfig = mcastConfig; |
| 165 | this->applyConfig(context); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 168 | void |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 169 | EthernetFactory::createFace(const CreateFaceRequest& req, |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 170 | const FaceCreatedCallback& onCreated, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 171 | const FaceCreationFailedCallback& onFailure) |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 172 | { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 173 | BOOST_ASSERT(req.remoteUri.isCanonical()); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 174 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 175 | if (!req.localUri || req.localUri->getScheme() != "dev") { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 176 | NFD_LOG_TRACE("Cannot create unicast Ethernet face without dev:// LocalUri"); |
| 177 | onFailure(406, "Creation of unicast Ethernet faces requires a LocalUri with dev:// scheme"); |
| 178 | return; |
| 179 | } |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 180 | BOOST_ASSERT(req.localUri->isCanonical()); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 181 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 182 | if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 183 | NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND"); |
| 184 | onFailure(406, "Outgoing Ethernet faces do not support on-demand persistency"); |
| 185 | return; |
| 186 | } |
| 187 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 188 | ethernet::Address remoteEndpoint(ethernet::Address::fromString(req.remoteUri.getHost())); |
| 189 | std::string localEndpoint(req.localUri->getHost()); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 190 | |
| 191 | if (remoteEndpoint.isMulticast()) { |
| 192 | NFD_LOG_TRACE("createFace does not support multicast faces"); |
| 193 | onFailure(406, "Cannot create multicast Ethernet faces"); |
| 194 | return; |
| 195 | } |
| 196 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 197 | if (req.params.wantLocalFields) { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 198 | // Ethernet faces are never local |
| 199 | NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled"); |
| 200 | onFailure(406, "Local fields can only be enabled on faces with local scope"); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | for (const auto& i : m_channels) { |
| 205 | if (i.first == localEndpoint) { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 206 | i.second->connect(remoteEndpoint, req.params, onCreated, onFailure); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | NFD_LOG_TRACE("No channels available to connect to " << remoteEndpoint); |
| 212 | onFailure(504, "No channels available to connect"); |
| 213 | } |
| 214 | |
| 215 | shared_ptr<EthernetChannel> |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 216 | EthernetFactory::createChannel(const shared_ptr<const ndn::net::NetworkInterface>& localEndpoint, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 217 | time::nanoseconds idleTimeout) |
| 218 | { |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 219 | auto it = m_channels.find(localEndpoint->getName()); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 220 | if (it != m_channels.end()) |
| 221 | return it->second; |
| 222 | |
| 223 | auto channel = std::make_shared<EthernetChannel>(localEndpoint, idleTimeout); |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 224 | m_channels[localEndpoint->getName()] = channel; |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 225 | |
| 226 | return channel; |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 229 | std::vector<shared_ptr<const Channel>> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 230 | EthernetFactory::getChannels() const |
| 231 | { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 232 | return getChannelsFromMap(m_channels); |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 233 | } |
| 234 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 235 | shared_ptr<Face> |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 236 | EthernetFactory::createMulticastFace(const ndn::net::NetworkInterface& netif, |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 237 | const ethernet::Address& address) |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 238 | { |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 239 | BOOST_ASSERT(address.isMulticast()); |
| 240 | |
Junxiao Shi | 0d82d04 | 2017-07-07 06:15:27 +0000 | [diff] [blame] | 241 | auto key = std::make_pair(netif.getName(), address); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 242 | auto found = m_mcastFaces.find(key); |
| 243 | if (found != m_mcastFaces.end()) { |
| 244 | return found->second; |
| 245 | } |
| 246 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 247 | GenericLinkService::Options opts; |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 248 | opts.allowFragmentation = true; |
| 249 | opts.allowReassembly = true; |
| 250 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 251 | auto linkService = make_unique<GenericLinkService>(opts); |
| 252 | auto transport = make_unique<MulticastEthernetTransport>(netif, address, m_mcastConfig.linkType); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 253 | auto face = make_shared<Face>(std::move(linkService), std::move(transport)); |
| 254 | |
| 255 | m_mcastFaces[key] = face; |
| 256 | connectFaceClosedSignal(*face, [this, key] { m_mcastFaces.erase(key); }); |
| 257 | |
| 258 | return face; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 261 | shared_ptr<EthernetChannel> |
| 262 | EthernetFactory::applyUnicastConfigToNetif(const shared_ptr<const ndn::net::NetworkInterface>& netif) |
| 263 | { |
| 264 | if (!m_unicastConfig.isEnabled) { |
| 265 | return nullptr; |
| 266 | } |
| 267 | |
Davide Pesavento | 6126547 | 2017-08-08 15:48:10 -0400 | [diff] [blame] | 268 | if (netif->getType() != ndn::net::InterfaceType::ETHERNET) { |
| 269 | NFD_LOG_DEBUG("Not creating channel on " << netif->getName() << ": incompatible netif type"); |
| 270 | return nullptr; |
| 271 | } |
| 272 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 273 | if (!netif->isUp()) { |
| 274 | NFD_LOG_DEBUG("Not creating channel on " << netif->getName() << ": netif is down"); |
| 275 | return nullptr; |
| 276 | } |
| 277 | |
Davide Pesavento | 6126547 | 2017-08-08 15:48:10 -0400 | [diff] [blame] | 278 | if (netif->getEthernetAddress().isNull()) { |
| 279 | NFD_LOG_DEBUG("Not creating channel on " << netif->getName() << ": invalid Ethernet address"); |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 280 | return nullptr; |
| 281 | } |
| 282 | |
| 283 | auto channel = this->createChannel(netif, m_unicastConfig.idleTimeout); |
| 284 | if (m_unicastConfig.wantListen && !channel->isListening()) { |
| 285 | try { |
| 286 | channel->listen(this->addFace, nullptr); |
| 287 | } |
| 288 | catch (const EthernetChannel::Error& e) { |
| 289 | NFD_LOG_WARN("Cannot listen on " << netif->getName() << ": " << e.what()); |
| 290 | } |
| 291 | } |
| 292 | return channel; |
| 293 | } |
| 294 | |
| 295 | shared_ptr<Face> |
| 296 | EthernetFactory::applyMcastConfigToNetif(const ndn::net::NetworkInterface& netif) |
| 297 | { |
| 298 | if (!m_mcastConfig.isEnabled) { |
| 299 | return nullptr; |
| 300 | } |
| 301 | |
Davide Pesavento | 6126547 | 2017-08-08 15:48:10 -0400 | [diff] [blame] | 302 | if (netif.getType() != ndn::net::InterfaceType::ETHERNET) { |
| 303 | NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": incompatible netif type"); |
| 304 | return nullptr; |
| 305 | } |
| 306 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 307 | if (!netif.isUp()) { |
| 308 | NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": netif is down"); |
| 309 | return nullptr; |
| 310 | } |
| 311 | |
| 312 | if (!netif.canMulticast()) { |
| 313 | NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": netif cannot multicast"); |
| 314 | return nullptr; |
| 315 | } |
| 316 | |
Davide Pesavento | 6126547 | 2017-08-08 15:48:10 -0400 | [diff] [blame] | 317 | if (netif.getEthernetAddress().isNull()) { |
| 318 | NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": invalid Ethernet address"); |
| 319 | return nullptr; |
| 320 | } |
| 321 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 322 | if (!m_mcastConfig.netifPredicate(netif)) { |
| 323 | NFD_LOG_DEBUG("Not creating multicast face on " << netif.getName() << ": rejected by whitelist/blacklist"); |
| 324 | return nullptr; |
| 325 | } |
| 326 | |
| 327 | NFD_LOG_DEBUG("Creating multicast face on " << netif.getName()); |
| 328 | shared_ptr<Face> face; |
| 329 | try { |
| 330 | face = this->createMulticastFace(netif, m_mcastConfig.group); |
| 331 | } |
| 332 | catch (const EthernetTransport::Error& e) { |
| 333 | NFD_LOG_WARN("Cannot create multicast face on " << netif.getName() << ": " << e.what()); |
| 334 | return nullptr; |
| 335 | } |
| 336 | |
| 337 | if (face->getId() == face::INVALID_FACEID) { |
| 338 | // new face: register with forwarding |
| 339 | this->addFace(face); |
| 340 | } |
| 341 | return face; |
| 342 | } |
| 343 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 344 | void |
| 345 | EthernetFactory::applyConfig(const FaceSystem::ConfigContext& context) |
| 346 | { |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 347 | if (m_unicastConfig.isEnabled) { |
| 348 | providedSchemes.insert("ether"); |
| 349 | } |
| 350 | else { |
| 351 | providedSchemes.erase("ether"); |
| 352 | } |
| 353 | |
| 354 | // collect old multicast faces |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 355 | std::set<shared_ptr<Face>> oldFaces; |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 356 | boost::copy(m_mcastFaces | boost::adaptors::map_values, std::inserter(oldFaces, oldFaces.end())); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 357 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 358 | // create channels and multicast faces if requested by config |
| 359 | for (const auto& netif : netmon->listNetworkInterfaces()) { |
| 360 | this->applyUnicastConfigToNetif(netif); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 361 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 362 | auto face = this->applyMcastConfigToNetif(*netif); |
| 363 | if (face != nullptr) { |
| 364 | // don't destroy face |
| 365 | oldFaces.erase(face); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 369 | // destroy old multicast faces that are not needed in new configuration |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 370 | for (const auto& face : oldFaces) { |
| 371 | face->close(); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | } // namespace face |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 376 | } // namespace nfd |