blob: be3f87e63dbf5b620da6b6e6f9fe2c314dbc14d5 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2d491752017-07-14 21:32:05 +00002/*
Davide Pesaventod91fe6d2023-10-04 21:40:02 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010025
26#include "udp-factory.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080027#include "generic-link-service.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080028#include "multicast-udp-transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040029#include "common/global.hpp"
Yanbiao Liafb20592017-08-03 16:20:46 -070030
Davide Pesaventoc5a9f812023-10-17 18:01:52 -040031#include <boost/container/static_vector.hpp>
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040032#include <boost/lexical_cast.hpp>
Davide Pesaventobb734df2017-10-24 18:05:36 -040033#include <boost/range/adaptor/map.hpp>
Junxiao Shi64d99f22017-01-21 23:06:36 +000034#include <boost/range/algorithm/copy.hpp>
Giulio Grassi624f6c62014-02-18 19:42:14 +010035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010037
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020038namespace ip = boost::asio::ip;
Davide Pesaventobb734df2017-10-24 18:05:36 -040039namespace net = ndn::net;
Junxiao Shi79494162014-04-02 18:25:11 -070040
Davide Pesaventoa3148082018-04-12 18:21:54 -040041NFD_LOG_INIT(UdpFactory);
Junxiao Shib47247d2017-01-24 15:09:16 +000042NFD_REGISTER_PROTOCOL_FACTORY(UdpFactory);
43
44const std::string&
Davide Pesaventod27841b2018-11-13 00:22:24 -050045UdpFactory::getId() noexcept
Junxiao Shib47247d2017-01-24 15:09:16 +000046{
47 static std::string id("udp");
48 return id;
49}
50
Junxiao Shi0ba6d642017-07-17 00:53:22 +000051UdpFactory::UdpFactory(const CtorParams& params)
52 : ProtocolFactory(params)
53{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040054 m_netifAddConn = netmon->onInterfaceAdded.connect([this] (const auto& netif) {
Davide Pesavento0a05f7a2023-10-16 20:28:06 -040055 applyMcastConfigToNetif(netif);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040056 });
Junxiao Shi0ba6d642017-07-17 00:53:22 +000057}
Giulio Grassi624f6c62014-02-18 19:42:14 +010058
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060059void
Davide Pesaventod27841b2018-11-13 00:22:24 -050060UdpFactory::doProcessConfig(OptionalConfigSection configSection,
61 FaceSystem::ConfigContext& context)
Junxiao Shi64d99f22017-01-21 23:06:36 +000062{
63 // udp
64 // {
Davide Pesavento494a9552018-02-04 22:16:05 -050065 // listen yes
Junxiao Shi64d99f22017-01-21 23:06:36 +000066 // port 6363
67 // enable_v4 yes
68 // enable_v6 yes
69 // idle_timeout 600
Junxiao Shia6286a92021-02-23 06:43:52 -070070 // unicast_mtu 8800
Junxiao Shi64d99f22017-01-21 23:06:36 +000071 // mcast yes
72 // mcast_group 224.0.23.170
73 // mcast_port 56363
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050074 // mcast_group_v6 ff02::1234
75 // mcast_port_v6 56363
Teng Liangfe4fce32017-03-29 04:49:38 +000076 // mcast_ad_hoc no
Junxiao Shic31080d2017-01-24 15:10:12 +000077 // whitelist
78 // {
79 // *
80 // }
81 // blacklist
82 // {
83 // }
Junxiao Shi64d99f22017-01-21 23:06:36 +000084 // }
85
Eric Newberry0c841642018-01-17 15:01:00 -070086 m_wantCongestionMarking = context.generalConfig.wantCongestionMarking;
87
Davide Pesavento494a9552018-02-04 22:16:05 -050088 bool wantListen = true;
Junxiao Shi64d99f22017-01-21 23:06:36 +000089 uint16_t port = 6363;
90 bool enableV4 = false;
91 bool enableV6 = false;
92 uint32_t idleTimeout = 600;
Junxiao Shia6286a92021-02-23 06:43:52 -070093 size_t unicastMtu = ndn::MAX_NDN_PACKET_SIZE;
Junxiao Shi64d99f22017-01-21 23:06:36 +000094 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 Pesavento494a9552018-02-04 22:16:05 -0500104 if (key == "listen") {
105 wantListen = ConfigFile::parseYesNo(pair, "face_system.udp");
106 }
107 else if (key == "port") {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000108 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 Shia6286a92021-02-23 06:43:52 -0700119 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 Shi64d99f22017-01-21 23:06:36 +0000124 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 Pesaventod91fe6d2023-10-04 21:40:02 -0400133 mcastConfig.group.address(ip::make_address_v4(valueStr, ec));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000134 if (ec) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500135 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group: '" +
136 valueStr + "' cannot be parsed as an IPv4 address"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000137 }
138 else if (!mcastConfig.group.address().is_multicast()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500139 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group: '" +
140 valueStr + "' is not a multicast address"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000141 }
142 }
143 else if (key == "mcast_port") {
144 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
145 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500146 else if (key == "mcast_group_v6") {
147 const std::string& valueStr = value.get_value<std::string>();
148 boost::system::error_code ec;
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400149 mcastConfig.groupV6.address(ip::make_address_v6(valueStr, ec));
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500150 if (ec) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500151 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
152 valueStr + "' cannot be parsed as an IPv6 address"));
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500153 }
154 else if (!mcastConfig.groupV6.address().is_multicast()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500155 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
156 valueStr + "' is not a multicast address"));
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500157 }
158 }
159 else if (key == "mcast_port_v6") {
160 mcastConfig.groupV6.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
161 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000162 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 Shic31080d2017-01-24 15:10:12 +0000166 else if (key == "whitelist") {
167 mcastConfig.netifPredicate.parseWhitelist(value);
168 }
169 else if (key == "blacklist") {
170 mcastConfig.netifPredicate.parseBlacklist(value);
171 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000172 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500173 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.udp." + key));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000174 }
175 }
176
177 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500178 NDN_THROW(ConfigFile::Error(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000179 "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 Shi79a92082017-08-08 02:40:59 +0000184 if (context.isDryRun) {
185 return;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000186 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000187
Junxiao Shia6286a92021-02-23 06:43:52 -0700188 m_defaultUnicastMtu = unicastMtu;
189
Junxiao Shi79a92082017-08-08 02:40:59 +0000190 if (enableV4) {
191 udp::Endpoint endpoint(ip::udp::v4(), port);
192 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
Davide Pesavento494a9552018-02-04 22:16:05 -0500193 if (wantListen && !v4Channel->isListening()) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000194 v4Channel->listen(this->addFace, nullptr);
195 }
196 providedSchemes.insert("udp");
197 providedSchemes.insert("udp4");
198 }
199 else if (providedSchemes.count("udp4") > 0) {
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400200 NFD_LOG_WARN("Cannot close UDP channel after its creation");
Junxiao Shi79a92082017-08-08 02:40:59 +0000201 }
202
203 if (enableV6) {
204 udp::Endpoint endpoint(ip::udp::v6(), port);
205 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
Davide Pesavento494a9552018-02-04 22:16:05 -0500206 if (wantListen && !v6Channel->isListening()) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000207 v6Channel->listen(this->addFace, nullptr);
208 }
209 providedSchemes.insert("udp");
210 providedSchemes.insert("udp6");
211 }
212 else if (providedSchemes.count("udp6") > 0) {
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400213 NFD_LOG_WARN("Cannot close UDP channel after its creation");
Junxiao Shi79a92082017-08-08 02:40:59 +0000214 }
215
216 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
217 if (mcastConfig.isEnabled) {
218 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500219 NFD_LOG_INFO("enabling multicast on " << mcastConfig.groupV6);
Junxiao Shi79a92082017-08-08 02:40:59 +0000220 }
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 Rahman8ce09032018-01-14 22:43:13 -0500230 NFD_LOG_INFO("changing IPv4 multicast group from " << m_mcastConfig.group <<
Junxiao Shi79a92082017-08-08 02:40:59 +0000231 " to " << mcastConfig.group);
232 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500233 if (m_mcastConfig.groupV6 != mcastConfig.groupV6) {
234 NFD_LOG_INFO("changing IPv6 multicast group from " << m_mcastConfig.groupV6 <<
235 " to " << mcastConfig.groupV6);
236 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000237 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
238 NFD_LOG_INFO("changing whitelist/blacklist");
239 }
240 }
241
Davide Pesavento0a05f7a2023-10-16 20:28:06 -0400242 // 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 Shi64d99f22017-01-21 23:06:36 +0000246}
247
248void
Davide Pesaventod27841b2018-11-13 00:22:24 -0500249UdpFactory::doCreateFace(const CreateFaceRequest& req,
250 const FaceCreatedCallback& onCreated,
251 const FaceCreationFailedCallback& onFailure)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000252{
Davide Pesavento15b55052018-01-27 19:09:28 -0500253 if (req.localUri) {
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400254 NFD_LOG_TRACE("createFace: unsupported LocalUri");
Eric Newberry78e32b02017-04-01 14:34:44 +0000255 onFailure(406, "Unicast UDP faces cannot be created with a LocalUri");
256 return;
257 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000258
Davide Pesavento15b55052018-01-27 19:09:28 -0500259 if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400260 NFD_LOG_TRACE("createFace: unsupported FacePersistency");
Davide Pesavento46afec42017-05-28 14:28:47 -0400261 onFailure(406, "Outgoing UDP faces do not support on-demand persistency");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000262 return;
263 }
264
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400265 udp::Endpoint endpoint(ip::make_address(req.remoteUri.getHost()),
Davide Pesavento15b55052018-01-27 19:09:28 -0500266 boost::lexical_cast<uint16_t>(req.remoteUri.getPort()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000267
268 if (endpoint.address().is_multicast()) {
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400269 NFD_LOG_TRACE("createFace: unsupported multicast endpoint");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000270 onFailure(406, "Cannot create multicast UDP faces");
271 return;
272 }
273
Davide Pesavento15b55052018-01-27 19:09:28 -0500274 if (req.params.wantLocalFields) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000275 // UDP faces are never local
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400276 NFD_LOG_TRACE("createFace: cannot create non-local face with local fields enabled");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000277 onFailure(406, "Local fields can only be enabled on faces with local scope");
278 return;
279 }
280
Eric Newberrycb6551e2020-03-02 14:12:16 -0800281 if (req.params.mtu && *req.params.mtu < MIN_MTU) {
Eric Newberry812d6152018-06-06 15:06:01 -0700282 // The specified MTU must be greater than the minimum possible
Eric Newberrycb6551e2020-03-02 14:12:16 -0800283 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 Newberry812d6152018-06-06 15:06:01 -0700285 return;
286 }
287
Junxiao Shi64d99f22017-01-21 23:06:36 +0000288 // 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 Pesavento15b55052018-01-27 19:09:28 -0500292 i.second->connect(endpoint, req.params, onCreated, onFailure);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000293 return;
294 }
295 }
296
Davide Pesavento46afec42017-05-28 14:28:47 -0400297 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000298 onFailure(504, "No channels available to connect");
299}
300
Giulio Grassi624f6c62014-02-18 19:42:14 +0100301shared_ptr<UdpChannel>
Davide Pesavento46afec42017-05-28 14:28:47 -0400302UdpFactory::createChannel(const udp::Endpoint& localEndpoint,
303 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100304{
Davide Pesavento46afec42017-05-28 14:28:47 -0400305 auto it = m_channels.find(localEndpoint);
306 if (it != m_channels.end())
307 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100308
Yukai Tu0a49d342015-09-13 12:54:22 +0800309 // check if the endpoint is already used by a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400310 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500311 NDN_THROW(Error("Cannot create UDP channel on " + boost::lexical_cast<std::string>(localEndpoint) +
312 ", endpoint already allocated to a UDP multicast face"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800313 }
Junxiao Shi79494162014-04-02 18:25:11 -0700314
Junxiao Shia6286a92021-02-23 06:43:52 -0700315 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout,
316 m_wantCongestionMarking, m_defaultUnicastMtu);
Davide Pesavento46afec42017-05-28 14:28:47 -0400317 m_channels[localEndpoint] = channel;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100318 return channel;
319}
320
Junxiao Shi64d99f22017-01-21 23:06:36 +0000321std::vector<shared_ptr<const Channel>>
Davide Pesaventod27841b2018-11-13 00:22:24 -0500322UdpFactory::doGetChannels() const
Junxiao Shi64d99f22017-01-21 23:06:36 +0000323{
324 return getChannelsFromMap(m_channels);
325}
326
Junxiao Shicde37ad2015-12-24 01:02:05 -0700327shared_ptr<Face>
Davide Pesavento279af1c2022-08-29 20:18:32 -0400328UdpFactory::createMulticastFace(const net::NetworkInterface& netif,
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500329 const ip::address& localAddress,
330 const udp::Endpoint& multicastEndpoint)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100331{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400332 BOOST_ASSERT(multicastEndpoint.address().is_multicast());
Davide Pesaventobb734df2017-10-24 18:05:36 -0400333
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500334 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 Pesavento279af1c2022-08-29 20:18:32 -0400341 mcastAddress.scope_id(netif.getIndex());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500342 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 Pesavento46afec42017-05-28 14:28:47 -0400347 if (it != m_mcastFaces.end()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500348 if (it->second->getRemoteUri() == FaceUri(mcastEp))
Davide Pesavento46afec42017-05-28 14:28:47 -0400349 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100350 else
Davide Pesavento19779d82019-02-14 13:40:04 -0500351 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 Grassi624f6c62014-02-18 19:42:14 +0100353 }
Junxiao Shi79494162014-04-02 18:25:11 -0700354
Davide Pesaventobb734df2017-10-24 18:05:36 -0400355 // check if the local endpoint is already used by a unicast channel
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500356 if (m_channels.find(localEp) != m_channels.end()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500357 NDN_THROW(Error("Cannot create UDP multicast face on " + boost::lexical_cast<std::string>(localEp) +
358 ", endpoint already allocated to a UDP channel"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100359 }
Junxiao Shi79494162014-04-02 18:25:11 -0700360
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500361 ip::udp::socket rxSock(getGlobalIoService());
Davide Pesavento279af1c2022-08-29 20:18:32 -0400362 MulticastUdpTransport::openRxSocket(rxSock, mcastEp, localAddress, &netif);
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500363 ip::udp::socket txSock(getGlobalIoService());
Davide Pesavento279af1c2022-08-29 20:18:32 -0400364 MulticastUdpTransport::openTxSocket(txSock, udp::Endpoint(localAddress, 0), &netif);
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200365
Eric Newberry0c841642018-01-17 15:01:00 -0700366 GenericLinkService::Options options;
367 options.allowCongestionMarking = m_wantCongestionMarking;
368 auto linkService = make_unique<GenericLinkService>(options);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500369 auto transport = make_unique<MulticastUdpTransport>(mcastEp, std::move(rxSock), std::move(txSock),
Teng Liangfe4fce32017-03-29 04:49:38 +0000370 m_mcastConfig.linkType);
Davide Pesavento46afec42017-05-28 14:28:47 -0400371 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700372
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500373 m_mcastFaces[localEp] = face;
374 connectFaceClosedSignal(*face, [this, localEp] { m_mcastFaces.erase(localEp); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100375
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400376 // 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 Pesaventoc5a9f812023-10-17 18:01:52 -0400381 if (channelIt != m_channels.end()) {
382 face->setChannel(channelIt->second);
383 }
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400384
Davide Pesavento292e5e12015-03-13 02:08:33 +0100385 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100386}
387
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400388static std::optional<ip::address>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500389pickAddress(const net::NetworkInterface& netif, net::AddressFamily af)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100390{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400391 for (const auto& na : netif.getNetworkAddresses()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500392 if (na.getFamily() == af &&
393 (na.getScope() == net::AddressScope::LINK || na.getScope() == net::AddressScope::GLOBAL)) {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400394 return na.getIp();
Junxiao Shibbace1d2017-08-06 20:03:37 +0000395 }
396 }
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400397 return std::nullopt;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000398}
399
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500400std::vector<shared_ptr<Face>>
Davide Pesaventobb734df2017-10-24 18:05:36 -0400401UdpFactory::applyMcastConfigToNetif(const shared_ptr<const net::NetworkInterface>& netif)
Junxiao Shibbace1d2017-08-06 20:03:37 +0000402{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400403 BOOST_ASSERT(netif != nullptr);
404
Junxiao Shibbace1d2017-08-06 20:03:37 +0000405 if (!m_mcastConfig.isEnabled) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500406 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000407 }
408
409 if (!netif->isUp()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500410 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 Shibbace1d2017-08-06 20:03:37 +0000417 }
418
419 if (!netif->canMulticast()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500420 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif cannot multicast");
421 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000422 }
423
424 if (!m_mcastConfig.netifPredicate(*netif)) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500425 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": rejected by whitelist/blacklist");
426 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000427 }
428
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400429 boost::container::static_vector<ip::address, 2> addrs;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500430 for (auto af : {net::AddressFamily::V4, net::AddressFamily::V6}) {
431 auto addr = pickAddress(*netif, af);
432 if (addr)
433 addrs.push_back(*addr);
Junxiao Shibbace1d2017-08-06 20:03:37 +0000434 }
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500435
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500436 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 Pesavento0a05f7a2023-10-16 20:28:06 -0400440 netif->onAddressAdded.connect([=] (auto&&...) { applyMcastConfigToNetif(netif); });
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500441 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 Pesaventoc5a9f812023-10-17 18:01:52 -0400448 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 Rahman8ce09032018-01-14 22:43:13 -0500456 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 Shibbace1d2017-08-06 20:03:37 +0000464}
465
Junxiao Shi64d99f22017-01-21 23:06:36 +0000466void
Davide Pesaventod27841b2018-11-13 00:22:24 -0500467UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext&)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000468{
469 // collect old faces
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500470 std::set<shared_ptr<Face>> facesToClose;
471 boost::copy(m_mcastFaces | boost::adaptors::map_values,
472 std::inserter(facesToClose, facesToClose.end()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000473
Junxiao Shibbace1d2017-08-06 20:03:37 +0000474 // create faces if requested by config
475 for (const auto& netif : netmon->listNetworkInterfaces()) {
Davide Pesavento0a05f7a2023-10-16 20:28:06 -0400476 auto facesToKeep = applyMcastConfigToNetif(netif);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500477 for (const auto& face : facesToKeep) {
Junxiao Shibbace1d2017-08-06 20:03:37 +0000478 // don't destroy face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500479 facesToClose.erase(face);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000480 }
481 }
482
483 // destroy old faces that are not needed in new configuration
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500484 for (const auto& face : facesToClose) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000485 face->close();
486 }
487}
488
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400489} // namespace nfd::face