Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -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 | */ |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 25 | |
| 26 | #include "udp-factory.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 27 | #include "generic-link-service.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 28 | #include "multicast-udp-transport.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 29 | #include "common/global.hpp" |
Yanbiao Li | afb2059 | 2017-08-03 16:20:46 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 31 | #include <boost/container/static_vector.hpp> |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 32 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 33 | #include <boost/range/adaptor/map.hpp> |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 34 | #include <boost/range/algorithm/copy.hpp> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::face { |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 37 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 38 | namespace ip = boost::asio::ip; |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 39 | namespace net = ndn::net; |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 40 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 41 | NFD_LOG_INIT(UdpFactory); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 42 | NFD_REGISTER_PROTOCOL_FACTORY(UdpFactory); |
| 43 | |
| 44 | const std::string& |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 45 | UdpFactory::getId() noexcept |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 46 | { |
| 47 | static std::string id("udp"); |
| 48 | return id; |
| 49 | } |
| 50 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 51 | UdpFactory::UdpFactory(const CtorParams& params) |
| 52 | : ProtocolFactory(params) |
| 53 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 54 | m_netifAddConn = netmon->onInterfaceAdded.connect([this] (const auto& netif) { |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 55 | applyMcastConfigToNetif(netif); |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 56 | }); |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 57 | } |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 58 | |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 59 | void |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 60 | UdpFactory::doProcessConfig(OptionalConfigSection configSection, |
| 61 | FaceSystem::ConfigContext& context) |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 62 | { |
| 63 | // udp |
| 64 | // { |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 65 | // listen yes |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 66 | // port 6363 |
| 67 | // enable_v4 yes |
| 68 | // enable_v6 yes |
| 69 | // idle_timeout 600 |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 70 | // unicast_mtu 8800 |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 71 | // mcast yes |
| 72 | // mcast_group 224.0.23.170 |
| 73 | // mcast_port 56363 |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 74 | // mcast_group_v6 ff02::1234 |
| 75 | // mcast_port_v6 56363 |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 76 | // mcast_ad_hoc no |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 77 | // whitelist |
| 78 | // { |
| 79 | // * |
| 80 | // } |
| 81 | // blacklist |
| 82 | // { |
| 83 | // } |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 84 | // } |
| 85 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 86 | m_wantCongestionMarking = context.generalConfig.wantCongestionMarking; |
| 87 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 88 | bool wantListen = true; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 89 | uint16_t port = 6363; |
| 90 | bool enableV4 = false; |
| 91 | bool enableV6 = false; |
| 92 | uint32_t idleTimeout = 600; |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 93 | size_t unicastMtu = ndn::MAX_NDN_PACKET_SIZE; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 94 | MulticastConfig mcastConfig; |
| 95 | |
| 96 | if (configSection) { |
| 97 | // These default to 'yes' but only if face_system.udp section is present |
| 98 | enableV4 = enableV6 = mcastConfig.isEnabled = true; |
| 99 | |
| 100 | for (const auto& pair : *configSection) { |
| 101 | const std::string& key = pair.first; |
| 102 | const ConfigSection& value = pair.second; |
| 103 | |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 104 | if (key == "listen") { |
| 105 | wantListen = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 106 | } |
| 107 | else if (key == "port") { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 108 | port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"); |
| 109 | } |
| 110 | else if (key == "enable_v4") { |
| 111 | enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 112 | } |
| 113 | else if (key == "enable_v6") { |
| 114 | enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 115 | } |
| 116 | else if (key == "idle_timeout") { |
| 117 | idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp"); |
| 118 | } |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 119 | else if (key == "unicast_mtu") { |
| 120 | unicastMtu = ConfigFile::parseNumber<size_t>(pair, "face_system.udp"); |
| 121 | ConfigFile::checkRange(unicastMtu, static_cast<size_t>(MIN_MTU), ndn::MAX_NDN_PACKET_SIZE, |
| 122 | "unicast_mtu", "face_system.udp"); |
| 123 | } |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 124 | else if (key == "keep_alive_interval") { |
| 125 | // ignored |
| 126 | } |
| 127 | else if (key == "mcast") { |
| 128 | mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 129 | } |
| 130 | else if (key == "mcast_group") { |
| 131 | const std::string& valueStr = value.get_value<std::string>(); |
| 132 | boost::system::error_code ec; |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 133 | mcastConfig.group.address(ip::make_address_v4(valueStr, ec)); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 134 | if (ec) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 135 | NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group: '" + |
| 136 | valueStr + "' cannot be parsed as an IPv4 address")); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 137 | } |
| 138 | else if (!mcastConfig.group.address().is_multicast()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 139 | NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group: '" + |
| 140 | valueStr + "' is not a multicast address")); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | else if (key == "mcast_port") { |
| 144 | mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp")); |
| 145 | } |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 146 | else if (key == "mcast_group_v6") { |
| 147 | const std::string& valueStr = value.get_value<std::string>(); |
| 148 | boost::system::error_code ec; |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 149 | mcastConfig.groupV6.address(ip::make_address_v6(valueStr, ec)); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 150 | if (ec) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 151 | NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group_v6: '" + |
| 152 | valueStr + "' cannot be parsed as an IPv6 address")); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 153 | } |
| 154 | else if (!mcastConfig.groupV6.address().is_multicast()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 155 | NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group_v6: '" + |
| 156 | valueStr + "' is not a multicast address")); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | else if (key == "mcast_port_v6") { |
| 160 | mcastConfig.groupV6.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp")); |
| 161 | } |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 162 | else if (key == "mcast_ad_hoc") { |
| 163 | bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp"); |
| 164 | mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS; |
| 165 | } |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 166 | else if (key == "whitelist") { |
| 167 | mcastConfig.netifPredicate.parseWhitelist(value); |
| 168 | } |
| 169 | else if (key == "blacklist") { |
| 170 | mcastConfig.netifPredicate.parseBlacklist(value); |
| 171 | } |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 172 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 173 | NDN_THROW(ConfigFile::Error("Unrecognized option face_system.udp." + key)); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 178 | NDN_THROW(ConfigFile::Error( |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 179 | "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. " |
| 180 | "Remove face_system.udp section to disable UDP channels or enable at least one of them.")); |
| 181 | } |
| 182 | } |
| 183 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 184 | if (context.isDryRun) { |
| 185 | return; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 186 | } |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 187 | |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 188 | m_defaultUnicastMtu = unicastMtu; |
| 189 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 190 | if (enableV4) { |
| 191 | udp::Endpoint endpoint(ip::udp::v4(), port); |
| 192 | shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout)); |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 193 | if (wantListen && !v4Channel->isListening()) { |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 194 | v4Channel->listen(this->addFace, nullptr); |
| 195 | } |
| 196 | providedSchemes.insert("udp"); |
| 197 | providedSchemes.insert("udp4"); |
| 198 | } |
| 199 | else if (providedSchemes.count("udp4") > 0) { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 200 | NFD_LOG_WARN("Cannot close UDP channel after its creation"); |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if (enableV6) { |
| 204 | udp::Endpoint endpoint(ip::udp::v6(), port); |
| 205 | shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout)); |
Davide Pesavento | 494a955 | 2018-02-04 22:16:05 -0500 | [diff] [blame] | 206 | if (wantListen && !v6Channel->isListening()) { |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 207 | v6Channel->listen(this->addFace, nullptr); |
| 208 | } |
| 209 | providedSchemes.insert("udp"); |
| 210 | providedSchemes.insert("udp6"); |
| 211 | } |
| 212 | else if (providedSchemes.count("udp6") > 0) { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 213 | NFD_LOG_WARN("Cannot close UDP channel after its creation"); |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) { |
| 217 | if (mcastConfig.isEnabled) { |
| 218 | NFD_LOG_INFO("enabling multicast on " << mcastConfig.group); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 219 | NFD_LOG_INFO("enabling multicast on " << mcastConfig.groupV6); |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 220 | } |
| 221 | else { |
| 222 | NFD_LOG_INFO("disabling multicast"); |
| 223 | } |
| 224 | } |
| 225 | else if (mcastConfig.isEnabled) { |
| 226 | if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) { |
| 227 | NFD_LOG_WARN("Cannot change ad hoc setting on existing faces"); |
| 228 | } |
| 229 | if (m_mcastConfig.group != mcastConfig.group) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 230 | NFD_LOG_INFO("changing IPv4 multicast group from " << m_mcastConfig.group << |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 231 | " to " << mcastConfig.group); |
| 232 | } |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 233 | if (m_mcastConfig.groupV6 != mcastConfig.groupV6) { |
| 234 | NFD_LOG_INFO("changing IPv6 multicast group from " << m_mcastConfig.groupV6 << |
| 235 | " to " << mcastConfig.groupV6); |
| 236 | } |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 237 | if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) { |
| 238 | NFD_LOG_INFO("changing whitelist/blacklist"); |
| 239 | } |
| 240 | } |
| 241 | |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 242 | // Even if there are no configuration changes, we still need to re-apply |
| 243 | // the configuration because netifs may have changed. |
| 244 | m_mcastConfig = std::move(mcastConfig); |
| 245 | applyMcastConfig(context); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 249 | UdpFactory::doCreateFace(const CreateFaceRequest& req, |
| 250 | const FaceCreatedCallback& onCreated, |
| 251 | const FaceCreationFailedCallback& onFailure) |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 252 | { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 253 | if (req.localUri) { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 254 | NFD_LOG_TRACE("createFace: unsupported LocalUri"); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 255 | onFailure(406, "Unicast UDP faces cannot be created with a LocalUri"); |
| 256 | return; |
| 257 | } |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 258 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 259 | if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 260 | NFD_LOG_TRACE("createFace: unsupported FacePersistency"); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 261 | onFailure(406, "Outgoing UDP faces do not support on-demand persistency"); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 262 | return; |
| 263 | } |
| 264 | |
Davide Pesavento | d91fe6d | 2023-10-04 21:40:02 -0400 | [diff] [blame] | 265 | udp::Endpoint endpoint(ip::make_address(req.remoteUri.getHost()), |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 266 | boost::lexical_cast<uint16_t>(req.remoteUri.getPort())); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 267 | |
| 268 | if (endpoint.address().is_multicast()) { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 269 | NFD_LOG_TRACE("createFace: unsupported multicast endpoint"); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 270 | onFailure(406, "Cannot create multicast UDP faces"); |
| 271 | return; |
| 272 | } |
| 273 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 274 | if (req.params.wantLocalFields) { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 275 | // UDP faces are never local |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 276 | NFD_LOG_TRACE("createFace: cannot create non-local face with local fields enabled"); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 277 | onFailure(406, "Local fields can only be enabled on faces with local scope"); |
| 278 | return; |
| 279 | } |
| 280 | |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame] | 281 | if (req.params.mtu && *req.params.mtu < MIN_MTU) { |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 282 | // The specified MTU must be greater than the minimum possible |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame] | 283 | NFD_LOG_TRACE("createFace: override MTU cannot be less than " << MIN_MTU); |
| 284 | onFailure(406, "Override MTU cannot be less than " + to_string(MIN_MTU)); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 288 | // very simple logic for now |
| 289 | for (const auto& i : m_channels) { |
| 290 | if ((i.first.address().is_v4() && endpoint.address().is_v4()) || |
| 291 | (i.first.address().is_v6() && endpoint.address().is_v6())) { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 292 | i.second->connect(endpoint, req.params, onCreated, onFailure); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | } |
| 296 | |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 297 | NFD_LOG_TRACE("No channels available to connect to " << endpoint); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 298 | onFailure(504, "No channels available to connect"); |
| 299 | } |
| 300 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 301 | shared_ptr<UdpChannel> |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 302 | UdpFactory::createChannel(const udp::Endpoint& localEndpoint, |
| 303 | time::nanoseconds idleTimeout) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 304 | { |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 305 | auto it = m_channels.find(localEndpoint); |
| 306 | if (it != m_channels.end()) |
| 307 | return it->second; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 308 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 309 | // check if the endpoint is already used by a multicast face |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 310 | if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 311 | NDN_THROW(Error("Cannot create UDP channel on " + boost::lexical_cast<std::string>(localEndpoint) + |
| 312 | ", endpoint already allocated to a UDP multicast face")); |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 313 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 314 | |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 315 | auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout, |
| 316 | m_wantCongestionMarking, m_defaultUnicastMtu); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 317 | m_channels[localEndpoint] = channel; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 318 | return channel; |
| 319 | } |
| 320 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 321 | std::vector<shared_ptr<const Channel>> |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 322 | UdpFactory::doGetChannels() const |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 323 | { |
| 324 | return getChannelsFromMap(m_channels); |
| 325 | } |
| 326 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 327 | shared_ptr<Face> |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 328 | UdpFactory::createMulticastFace(const net::NetworkInterface& netif, |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 329 | const ip::address& localAddress, |
| 330 | const udp::Endpoint& multicastEndpoint) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 331 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 332 | BOOST_ASSERT(multicastEndpoint.address().is_multicast()); |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 333 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 334 | udp::Endpoint localEp(localAddress, multicastEndpoint.port()); |
| 335 | BOOST_ASSERT(localEp.protocol() == multicastEndpoint.protocol()); |
| 336 | |
| 337 | auto mcastEp = multicastEndpoint; |
| 338 | if (mcastEp.address().is_v6()) { |
| 339 | // in IPv6, a scope id on the multicast address is always required |
| 340 | auto mcastAddress = mcastEp.address().to_v6(); |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 341 | mcastAddress.scope_id(netif.getIndex()); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 342 | mcastEp.address(mcastAddress); |
| 343 | } |
| 344 | |
| 345 | // check if the local endpoint is already used by another multicast face |
| 346 | auto it = m_mcastFaces.find(localEp); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 347 | if (it != m_mcastFaces.end()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 348 | if (it->second->getRemoteUri() == FaceUri(mcastEp)) |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 349 | return it->second; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 350 | else |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 351 | NDN_THROW(Error("Cannot create UDP multicast face on " + boost::lexical_cast<std::string>(localEp) + |
| 352 | ", endpoint already allocated to a different UDP multicast face")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 353 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 354 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 355 | // check if the local endpoint is already used by a unicast channel |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 356 | if (m_channels.find(localEp) != m_channels.end()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 357 | NDN_THROW(Error("Cannot create UDP multicast face on " + boost::lexical_cast<std::string>(localEp) + |
| 358 | ", endpoint already allocated to a UDP channel")); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 359 | } |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 360 | |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 361 | ip::udp::socket rxSock(getGlobalIoService()); |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 362 | MulticastUdpTransport::openRxSocket(rxSock, mcastEp, localAddress, &netif); |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 363 | ip::udp::socket txSock(getGlobalIoService()); |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 364 | MulticastUdpTransport::openTxSocket(txSock, udp::Endpoint(localAddress, 0), &netif); |
Giulio Grassi | 6d7176d | 2014-04-16 16:08:48 +0200 | [diff] [blame] | 365 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 366 | GenericLinkService::Options options; |
| 367 | options.allowCongestionMarking = m_wantCongestionMarking; |
| 368 | auto linkService = make_unique<GenericLinkService>(options); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 369 | auto transport = make_unique<MulticastUdpTransport>(mcastEp, std::move(rxSock), std::move(txSock), |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 370 | m_mcastConfig.linkType); |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 371 | auto face = make_shared<Face>(std::move(linkService), std::move(transport)); |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 372 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 373 | m_mcastFaces[localEp] = face; |
| 374 | connectFaceClosedSignal(*face, [this, localEp] { m_mcastFaces.erase(localEp); }); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 375 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 376 | // Associate with the first available channel of the same protocol family |
| 377 | auto channelIt = std::find_if(m_channels.begin(), m_channels.end(), |
| 378 | [isV4 = localEp.address().is_v4()] (const auto& it) { |
| 379 | return it.first.address().is_v4() == isV4; |
| 380 | }); |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 381 | if (channelIt != m_channels.end()) { |
| 382 | face->setChannel(channelIt->second); |
| 383 | } |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 384 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 385 | return face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 386 | } |
| 387 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 388 | static std::optional<ip::address> |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 389 | pickAddress(const net::NetworkInterface& netif, net::AddressFamily af) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 390 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 391 | for (const auto& na : netif.getNetworkAddresses()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 392 | if (na.getFamily() == af && |
| 393 | (na.getScope() == net::AddressScope::LINK || na.getScope() == net::AddressScope::GLOBAL)) { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 394 | return na.getIp(); |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 397 | return std::nullopt; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 400 | std::vector<shared_ptr<Face>> |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 401 | UdpFactory::applyMcastConfigToNetif(const shared_ptr<const net::NetworkInterface>& netif) |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 402 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 403 | BOOST_ASSERT(netif != nullptr); |
| 404 | |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 405 | if (!m_mcastConfig.isEnabled) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 406 | return {}; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (!netif->isUp()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 410 | NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is down"); |
| 411 | return {}; |
| 412 | } |
| 413 | |
| 414 | if (netif->isLoopback()) { |
| 415 | NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is loopback"); |
| 416 | return {}; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (!netif->canMulticast()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 420 | NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif cannot multicast"); |
| 421 | return {}; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (!m_mcastConfig.netifPredicate(*netif)) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 425 | NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": rejected by whitelist/blacklist"); |
| 426 | return {}; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 429 | boost::container::static_vector<ip::address, 2> addrs; |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 430 | for (auto af : {net::AddressFamily::V4, net::AddressFamily::V6}) { |
| 431 | auto addr = pickAddress(*netif, af); |
| 432 | if (addr) |
| 433 | addrs.push_back(*addr); |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 434 | } |
Davide Pesavento | 8215a3a | 2017-12-25 19:14:33 -0500 | [diff] [blame] | 435 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 436 | if (addrs.empty()) { |
| 437 | NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": no viable IP address"); |
| 438 | // keep an eye on new addresses |
| 439 | m_netifConns[netif->getIndex()].addrAddConn = |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 440 | netif->onAddressAdded.connect([=] (auto&&...) { applyMcastConfigToNetif(netif); }); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 441 | return {}; |
| 442 | } |
| 443 | |
| 444 | NFD_LOG_DEBUG("Creating multicast faces on " << netif->getName()); |
| 445 | |
| 446 | std::vector<shared_ptr<Face>> faces; |
| 447 | for (const auto& addr : addrs) { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 448 | shared_ptr<Face> face; |
| 449 | try { |
| 450 | face = createMulticastFace(*netif, addr, addr.is_v4() ? m_mcastConfig.group : m_mcastConfig.groupV6); |
| 451 | } |
| 452 | catch (const std::runtime_error& e) { |
| 453 | NFD_LOG_WARN("Cannot create multicast face on " << addr << ": " << e.what()); |
| 454 | continue; // not a fatal error |
| 455 | } |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 456 | if (face->getId() == INVALID_FACEID) { |
| 457 | // new face: register with forwarding |
| 458 | this->addFace(face); |
| 459 | } |
| 460 | faces.push_back(std::move(face)); |
| 461 | } |
| 462 | |
| 463 | return faces; |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 466 | void |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 467 | UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext&) |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 468 | { |
| 469 | // collect old faces |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 470 | std::set<shared_ptr<Face>> facesToClose; |
| 471 | boost::copy(m_mcastFaces | boost::adaptors::map_values, |
| 472 | std::inserter(facesToClose, facesToClose.end())); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 473 | |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 474 | // create faces if requested by config |
| 475 | for (const auto& netif : netmon->listNetworkInterfaces()) { |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 476 | auto facesToKeep = applyMcastConfigToNetif(netif); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 477 | for (const auto& face : facesToKeep) { |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 478 | // don't destroy face |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 479 | facesToClose.erase(face); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
| 483 | // destroy old faces that are not needed in new configuration |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 484 | for (const auto& face : facesToClose) { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 485 | face->close(); |
| 486 | } |
| 487 | } |
| 488 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 489 | } // namespace nfd::face |