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