blob: 4feec762602d961d06717710144986ac75b87903 [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 Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, 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 Pesaventobb734df2017-10-24 18:05:36 -040031#include <boost/range/adaptor/map.hpp>
Junxiao Shi64d99f22017-01-21 23:06:36 +000032#include <boost/range/algorithm/copy.hpp>
Giulio Grassi624f6c62014-02-18 19:42:14 +010033
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010035
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020036namespace ip = boost::asio::ip;
Davide Pesaventobb734df2017-10-24 18:05:36 -040037namespace net = ndn::net;
Junxiao Shi79494162014-04-02 18:25:11 -070038
Davide Pesaventoa3148082018-04-12 18:21:54 -040039NFD_LOG_INIT(UdpFactory);
Junxiao Shib47247d2017-01-24 15:09:16 +000040NFD_REGISTER_PROTOCOL_FACTORY(UdpFactory);
41
42const std::string&
Davide Pesaventod27841b2018-11-13 00:22:24 -050043UdpFactory::getId() noexcept
Junxiao Shib47247d2017-01-24 15:09:16 +000044{
45 static std::string id("udp");
46 return id;
47}
48
Junxiao Shi0ba6d642017-07-17 00:53:22 +000049UdpFactory::UdpFactory(const CtorParams& params)
50 : ProtocolFactory(params)
51{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040052 m_netifAddConn = netmon->onInterfaceAdded.connect([this] (const auto& netif) {
53 this->applyMcastConfigToNetif(netif);
54 });
Junxiao Shi0ba6d642017-07-17 00:53:22 +000055}
Giulio Grassi624f6c62014-02-18 19:42:14 +010056
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060057void
Davide Pesaventod27841b2018-11-13 00:22:24 -050058UdpFactory::doProcessConfig(OptionalConfigSection configSection,
59 FaceSystem::ConfigContext& context)
Junxiao Shi64d99f22017-01-21 23:06:36 +000060{
61 // udp
62 // {
Davide Pesavento494a9552018-02-04 22:16:05 -050063 // listen yes
Junxiao Shi64d99f22017-01-21 23:06:36 +000064 // port 6363
65 // enable_v4 yes
66 // enable_v6 yes
67 // idle_timeout 600
Junxiao Shia6286a92021-02-23 06:43:52 -070068 // unicast_mtu 8800
Junxiao Shi64d99f22017-01-21 23:06:36 +000069 // mcast yes
70 // mcast_group 224.0.23.170
71 // mcast_port 56363
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050072 // mcast_group_v6 ff02::1234
73 // mcast_port_v6 56363
Teng Liangfe4fce32017-03-29 04:49:38 +000074 // mcast_ad_hoc no
Junxiao Shic31080d2017-01-24 15:10:12 +000075 // whitelist
76 // {
77 // *
78 // }
79 // blacklist
80 // {
81 // }
Junxiao Shi64d99f22017-01-21 23:06:36 +000082 // }
83
Eric Newberry0c841642018-01-17 15:01:00 -070084 m_wantCongestionMarking = context.generalConfig.wantCongestionMarking;
85
Davide Pesavento494a9552018-02-04 22:16:05 -050086 bool wantListen = true;
Junxiao Shi64d99f22017-01-21 23:06:36 +000087 uint16_t port = 6363;
88 bool enableV4 = false;
89 bool enableV6 = false;
90 uint32_t idleTimeout = 600;
Junxiao Shia6286a92021-02-23 06:43:52 -070091 size_t unicastMtu = ndn::MAX_NDN_PACKET_SIZE;
Junxiao Shi64d99f22017-01-21 23:06:36 +000092 MulticastConfig mcastConfig;
93
94 if (configSection) {
95 // These default to 'yes' but only if face_system.udp section is present
96 enableV4 = enableV6 = mcastConfig.isEnabled = true;
97
98 for (const auto& pair : *configSection) {
99 const std::string& key = pair.first;
100 const ConfigSection& value = pair.second;
101
Davide Pesavento494a9552018-02-04 22:16:05 -0500102 if (key == "listen") {
103 wantListen = ConfigFile::parseYesNo(pair, "face_system.udp");
104 }
105 else if (key == "port") {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000106 port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp");
107 }
108 else if (key == "enable_v4") {
109 enableV4 = ConfigFile::parseYesNo(pair, "face_system.udp");
110 }
111 else if (key == "enable_v6") {
112 enableV6 = ConfigFile::parseYesNo(pair, "face_system.udp");
113 }
114 else if (key == "idle_timeout") {
115 idleTimeout = ConfigFile::parseNumber<uint32_t>(pair, "face_system.udp");
116 }
Junxiao Shia6286a92021-02-23 06:43:52 -0700117 else if (key == "unicast_mtu") {
118 unicastMtu = ConfigFile::parseNumber<size_t>(pair, "face_system.udp");
119 ConfigFile::checkRange(unicastMtu, static_cast<size_t>(MIN_MTU), ndn::MAX_NDN_PACKET_SIZE,
120 "unicast_mtu", "face_system.udp");
121 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000122 else if (key == "keep_alive_interval") {
123 // ignored
124 }
125 else if (key == "mcast") {
126 mcastConfig.isEnabled = ConfigFile::parseYesNo(pair, "face_system.udp");
127 }
128 else if (key == "mcast_group") {
129 const std::string& valueStr = value.get_value<std::string>();
130 boost::system::error_code ec;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400131 mcastConfig.group.address(ip::address_v4::from_string(valueStr, ec));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000132 if (ec) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500133 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group: '" +
134 valueStr + "' cannot be parsed as an IPv4 address"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000135 }
136 else if (!mcastConfig.group.address().is_multicast()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500137 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group: '" +
138 valueStr + "' is not a multicast address"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000139 }
140 }
141 else if (key == "mcast_port") {
142 mcastConfig.group.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
143 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500144 else if (key == "mcast_group_v6") {
145 const std::string& valueStr = value.get_value<std::string>();
146 boost::system::error_code ec;
Davide Pesavento9c33b902018-05-20 01:30:29 -0400147 mcastConfig.groupV6.address(ip::address_v6::from_string(valueStr, ec));
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500148 if (ec) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500149 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
150 valueStr + "' cannot be parsed as an IPv6 address"));
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500151 }
152 else if (!mcastConfig.groupV6.address().is_multicast()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500153 NDN_THROW(ConfigFile::Error("face_system.udp.mcast_group_v6: '" +
154 valueStr + "' is not a multicast address"));
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500155 }
156 }
157 else if (key == "mcast_port_v6") {
158 mcastConfig.groupV6.port(ConfigFile::parseNumber<uint16_t>(pair, "face_system.udp"));
159 }
Teng Liangfe4fce32017-03-29 04:49:38 +0000160 else if (key == "mcast_ad_hoc") {
161 bool wantAdHoc = ConfigFile::parseYesNo(pair, "face_system.udp");
162 mcastConfig.linkType = wantAdHoc ? ndn::nfd::LINK_TYPE_AD_HOC : ndn::nfd::LINK_TYPE_MULTI_ACCESS;
163 }
Junxiao Shic31080d2017-01-24 15:10:12 +0000164 else if (key == "whitelist") {
165 mcastConfig.netifPredicate.parseWhitelist(value);
166 }
167 else if (key == "blacklist") {
168 mcastConfig.netifPredicate.parseBlacklist(value);
169 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000170 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500171 NDN_THROW(ConfigFile::Error("Unrecognized option face_system.udp." + key));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000172 }
173 }
174
175 if (!enableV4 && !enableV6 && !mcastConfig.isEnabled) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500176 NDN_THROW(ConfigFile::Error(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000177 "IPv4 and IPv6 UDP channels and UDP multicast have been disabled. "
178 "Remove face_system.udp section to disable UDP channels or enable at least one of them."));
179 }
180 }
181
Junxiao Shi79a92082017-08-08 02:40:59 +0000182 if (context.isDryRun) {
183 return;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000184 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000185
Junxiao Shia6286a92021-02-23 06:43:52 -0700186 m_defaultUnicastMtu = unicastMtu;
187
Junxiao Shi79a92082017-08-08 02:40:59 +0000188 if (enableV4) {
189 udp::Endpoint endpoint(ip::udp::v4(), port);
190 shared_ptr<UdpChannel> v4Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
Davide Pesavento494a9552018-02-04 22:16:05 -0500191 if (wantListen && !v4Channel->isListening()) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000192 v4Channel->listen(this->addFace, nullptr);
193 }
194 providedSchemes.insert("udp");
195 providedSchemes.insert("udp4");
196 }
197 else if (providedSchemes.count("udp4") > 0) {
198 NFD_LOG_WARN("Cannot close udp4 channel after its creation");
199 }
200
201 if (enableV6) {
202 udp::Endpoint endpoint(ip::udp::v6(), port);
203 shared_ptr<UdpChannel> v6Channel = this->createChannel(endpoint, time::seconds(idleTimeout));
Davide Pesavento494a9552018-02-04 22:16:05 -0500204 if (wantListen && !v6Channel->isListening()) {
Junxiao Shi79a92082017-08-08 02:40:59 +0000205 v6Channel->listen(this->addFace, nullptr);
206 }
207 providedSchemes.insert("udp");
208 providedSchemes.insert("udp6");
209 }
210 else if (providedSchemes.count("udp6") > 0) {
211 NFD_LOG_WARN("Cannot close udp6 channel after its creation");
212 }
213
214 if (m_mcastConfig.isEnabled != mcastConfig.isEnabled) {
215 if (mcastConfig.isEnabled) {
216 NFD_LOG_INFO("enabling multicast on " << mcastConfig.group);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500217 NFD_LOG_INFO("enabling multicast on " << mcastConfig.groupV6);
Junxiao Shi79a92082017-08-08 02:40:59 +0000218 }
219 else {
220 NFD_LOG_INFO("disabling multicast");
221 }
222 }
223 else if (mcastConfig.isEnabled) {
224 if (m_mcastConfig.linkType != mcastConfig.linkType && !m_mcastFaces.empty()) {
225 NFD_LOG_WARN("Cannot change ad hoc setting on existing faces");
226 }
227 if (m_mcastConfig.group != mcastConfig.group) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500228 NFD_LOG_INFO("changing IPv4 multicast group from " << m_mcastConfig.group <<
Junxiao Shi79a92082017-08-08 02:40:59 +0000229 " to " << mcastConfig.group);
230 }
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500231 if (m_mcastConfig.groupV6 != mcastConfig.groupV6) {
232 NFD_LOG_INFO("changing IPv6 multicast group from " << m_mcastConfig.groupV6 <<
233 " to " << mcastConfig.groupV6);
234 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000235 if (m_mcastConfig.netifPredicate != mcastConfig.netifPredicate) {
236 NFD_LOG_INFO("changing whitelist/blacklist");
237 }
238 }
239
240 // Even if there's no configuration change, we still need to re-apply configuration because
241 // netifs may have changed.
242 m_mcastConfig = mcastConfig;
243 this->applyMcastConfig(context);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000244}
245
246void
Davide Pesaventod27841b2018-11-13 00:22:24 -0500247UdpFactory::doCreateFace(const CreateFaceRequest& req,
248 const FaceCreatedCallback& onCreated,
249 const FaceCreationFailedCallback& onFailure)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000250{
Davide Pesavento15b55052018-01-27 19:09:28 -0500251 if (req.localUri) {
Eric Newberry78e32b02017-04-01 14:34:44 +0000252 NFD_LOG_TRACE("Cannot create unicast UDP face with LocalUri");
253 onFailure(406, "Unicast UDP faces cannot be created with a LocalUri");
254 return;
255 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000256
Davide Pesavento15b55052018-01-27 19:09:28 -0500257 if (req.params.persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000258 NFD_LOG_TRACE("createFace does not support FACE_PERSISTENCY_ON_DEMAND");
Davide Pesavento46afec42017-05-28 14:28:47 -0400259 onFailure(406, "Outgoing UDP faces do not support on-demand persistency");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000260 return;
261 }
262
Davide Pesavento9c33b902018-05-20 01:30:29 -0400263 udp::Endpoint endpoint(ip::address::from_string(req.remoteUri.getHost()),
Davide Pesavento15b55052018-01-27 19:09:28 -0500264 boost::lexical_cast<uint16_t>(req.remoteUri.getPort()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000265
266 if (endpoint.address().is_multicast()) {
267 NFD_LOG_TRACE("createFace does not support multicast faces");
268 onFailure(406, "Cannot create multicast UDP faces");
269 return;
270 }
271
Davide Pesavento15b55052018-01-27 19:09:28 -0500272 if (req.params.wantLocalFields) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000273 // UDP faces are never local
274 NFD_LOG_TRACE("createFace cannot create non-local face with local fields enabled");
275 onFailure(406, "Local fields can only be enabled on faces with local scope");
276 return;
277 }
278
Eric Newberrycb6551e2020-03-02 14:12:16 -0800279 if (req.params.mtu && *req.params.mtu < MIN_MTU) {
Eric Newberry812d6152018-06-06 15:06:01 -0700280 // The specified MTU must be greater than the minimum possible
Eric Newberrycb6551e2020-03-02 14:12:16 -0800281 NFD_LOG_TRACE("createFace: override MTU cannot be less than " << MIN_MTU);
282 onFailure(406, "Override MTU cannot be less than " + to_string(MIN_MTU));
Eric Newberry812d6152018-06-06 15:06:01 -0700283 return;
284 }
285
Junxiao Shi64d99f22017-01-21 23:06:36 +0000286 // very simple logic for now
287 for (const auto& i : m_channels) {
288 if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
289 (i.first.address().is_v6() && endpoint.address().is_v6())) {
Davide Pesavento15b55052018-01-27 19:09:28 -0500290 i.second->connect(endpoint, req.params, onCreated, onFailure);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000291 return;
292 }
293 }
294
Davide Pesavento46afec42017-05-28 14:28:47 -0400295 NFD_LOG_TRACE("No channels available to connect to " << endpoint);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000296 onFailure(504, "No channels available to connect");
297}
298
Giulio Grassi624f6c62014-02-18 19:42:14 +0100299shared_ptr<UdpChannel>
Davide Pesavento46afec42017-05-28 14:28:47 -0400300UdpFactory::createChannel(const udp::Endpoint& localEndpoint,
301 time::nanoseconds idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100302{
Davide Pesavento46afec42017-05-28 14:28:47 -0400303 auto it = m_channels.find(localEndpoint);
304 if (it != m_channels.end())
305 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100306
Yukai Tu0a49d342015-09-13 12:54:22 +0800307 // check if the endpoint is already used by a multicast face
Davide Pesavento46afec42017-05-28 14:28:47 -0400308 if (m_mcastFaces.find(localEndpoint) != m_mcastFaces.end()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500309 NDN_THROW(Error("Cannot create UDP channel on " + boost::lexical_cast<std::string>(localEndpoint) +
310 ", endpoint already allocated to a UDP multicast face"));
Yukai Tu0a49d342015-09-13 12:54:22 +0800311 }
Junxiao Shi79494162014-04-02 18:25:11 -0700312
Junxiao Shia6286a92021-02-23 06:43:52 -0700313 auto channel = std::make_shared<UdpChannel>(localEndpoint, idleTimeout,
314 m_wantCongestionMarking, m_defaultUnicastMtu);
Davide Pesavento46afec42017-05-28 14:28:47 -0400315 m_channels[localEndpoint] = channel;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100316 return channel;
317}
318
Junxiao Shi64d99f22017-01-21 23:06:36 +0000319std::vector<shared_ptr<const Channel>>
Davide Pesaventod27841b2018-11-13 00:22:24 -0500320UdpFactory::doGetChannels() const
Junxiao Shi64d99f22017-01-21 23:06:36 +0000321{
322 return getChannelsFromMap(m_channels);
323}
324
Junxiao Shicde37ad2015-12-24 01:02:05 -0700325shared_ptr<Face>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500326UdpFactory::createMulticastFace(const shared_ptr<const net::NetworkInterface>& netif,
327 const ip::address& localAddress,
328 const udp::Endpoint& multicastEndpoint)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100329{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400330 BOOST_ASSERT(multicastEndpoint.address().is_multicast());
Davide Pesaventobb734df2017-10-24 18:05:36 -0400331
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500332 udp::Endpoint localEp(localAddress, multicastEndpoint.port());
333 BOOST_ASSERT(localEp.protocol() == multicastEndpoint.protocol());
334
335 auto mcastEp = multicastEndpoint;
336 if (mcastEp.address().is_v6()) {
337 // in IPv6, a scope id on the multicast address is always required
338 auto mcastAddress = mcastEp.address().to_v6();
339 mcastAddress.scope_id(netif->getIndex());
340 mcastEp.address(mcastAddress);
341 }
342
343 // check if the local endpoint is already used by another multicast face
344 auto it = m_mcastFaces.find(localEp);
Davide Pesavento46afec42017-05-28 14:28:47 -0400345 if (it != m_mcastFaces.end()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500346 if (it->second->getRemoteUri() == FaceUri(mcastEp))
Davide Pesavento46afec42017-05-28 14:28:47 -0400347 return it->second;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100348 else
Davide Pesavento19779d82019-02-14 13:40:04 -0500349 NDN_THROW(Error("Cannot create UDP multicast face on " + boost::lexical_cast<std::string>(localEp) +
350 ", endpoint already allocated to a different UDP multicast face"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100351 }
Junxiao Shi79494162014-04-02 18:25:11 -0700352
Davide Pesaventobb734df2017-10-24 18:05:36 -0400353 // check if the local endpoint is already used by a unicast channel
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500354 if (m_channels.find(localEp) != m_channels.end()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500355 NDN_THROW(Error("Cannot create UDP multicast face on " + boost::lexical_cast<std::string>(localEp) +
356 ", endpoint already allocated to a UDP channel"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100357 }
Junxiao Shi79494162014-04-02 18:25:11 -0700358
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500359 ip::udp::socket rxSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500360 MulticastUdpTransport::openRxSocket(rxSock, mcastEp, localAddress, netif);
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500361 ip::udp::socket txSock(getGlobalIoService());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500362 MulticastUdpTransport::openTxSocket(txSock, udp::Endpoint(localAddress, 0), netif);
Giulio Grassi6d7176d2014-04-16 16:08:48 +0200363
Eric Newberry0c841642018-01-17 15:01:00 -0700364 GenericLinkService::Options options;
365 options.allowCongestionMarking = m_wantCongestionMarking;
366 auto linkService = make_unique<GenericLinkService>(options);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500367 auto transport = make_unique<MulticastUdpTransport>(mcastEp, std::move(rxSock), std::move(txSock),
Teng Liangfe4fce32017-03-29 04:49:38 +0000368 m_mcastConfig.linkType);
Davide Pesavento46afec42017-05-28 14:28:47 -0400369 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Junxiao Shic099ddb2014-12-25 20:53:20 -0700370
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500371 m_mcastFaces[localEp] = face;
372 connectFaceClosedSignal(*face, [this, localEp] { m_mcastFaces.erase(localEp); });
Giulio Grassi624f6c62014-02-18 19:42:14 +0100373
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400374 // Associate with the first available channel of the same protocol family
375 auto channelIt = std::find_if(m_channels.begin(), m_channels.end(),
376 [isV4 = localEp.address().is_v4()] (const auto& it) {
377 return it.first.address().is_v4() == isV4;
378 });
379 face->setChannel(channelIt != m_channels.end() ? channelIt->second : nullptr);
380
Davide Pesavento292e5e12015-03-13 02:08:33 +0100381 return face;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100382}
383
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400384static std::optional<ip::address>
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500385pickAddress(const net::NetworkInterface& netif, net::AddressFamily af)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100386{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400387 for (const auto& na : netif.getNetworkAddresses()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500388 if (na.getFamily() == af &&
389 (na.getScope() == net::AddressScope::LINK || na.getScope() == net::AddressScope::GLOBAL)) {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400390 return na.getIp();
Junxiao Shibbace1d2017-08-06 20:03:37 +0000391 }
392 }
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400393 return std::nullopt;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000394}
395
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500396std::vector<shared_ptr<Face>>
Davide Pesaventobb734df2017-10-24 18:05:36 -0400397UdpFactory::applyMcastConfigToNetif(const shared_ptr<const net::NetworkInterface>& netif)
Junxiao Shibbace1d2017-08-06 20:03:37 +0000398{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400399 BOOST_ASSERT(netif != nullptr);
400
Junxiao Shibbace1d2017-08-06 20:03:37 +0000401 if (!m_mcastConfig.isEnabled) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500402 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000403 }
404
405 if (!netif->isUp()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500406 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is down");
407 return {};
408 }
409
410 if (netif->isLoopback()) {
411 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif is loopback");
412 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000413 }
414
415 if (!netif->canMulticast()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500416 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": netif cannot multicast");
417 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000418 }
419
420 if (!m_mcastConfig.netifPredicate(*netif)) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500421 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": rejected by whitelist/blacklist");
422 return {};
Junxiao Shibbace1d2017-08-06 20:03:37 +0000423 }
424
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500425 std::vector<ip::address> addrs;
426 for (auto af : {net::AddressFamily::V4, net::AddressFamily::V6}) {
427 auto addr = pickAddress(*netif, af);
428 if (addr)
429 addrs.push_back(*addr);
Junxiao Shibbace1d2017-08-06 20:03:37 +0000430 }
Davide Pesavento8215a3a2017-12-25 19:14:33 -0500431
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500432 if (addrs.empty()) {
433 NFD_LOG_DEBUG("Not creating multicast faces on " << netif->getName() << ": no viable IP address");
434 // keep an eye on new addresses
435 m_netifConns[netif->getIndex()].addrAddConn =
Davide Pesavento412c9822021-07-02 00:21:05 -0400436 netif->onAddressAdded.connect([=] (auto&&...) { this->applyMcastConfigToNetif(netif); });
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500437 return {};
438 }
439
440 NFD_LOG_DEBUG("Creating multicast faces on " << netif->getName());
441
442 std::vector<shared_ptr<Face>> faces;
443 for (const auto& addr : addrs) {
444 auto face = this->createMulticastFace(netif, addr,
445 addr.is_v4() ? m_mcastConfig.group : m_mcastConfig.groupV6);
446 if (face->getId() == INVALID_FACEID) {
447 // new face: register with forwarding
448 this->addFace(face);
449 }
450 faces.push_back(std::move(face));
451 }
452
453 return faces;
Junxiao Shibbace1d2017-08-06 20:03:37 +0000454}
455
Junxiao Shi64d99f22017-01-21 23:06:36 +0000456void
Davide Pesaventod27841b2018-11-13 00:22:24 -0500457UdpFactory::applyMcastConfig(const FaceSystem::ConfigContext&)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000458{
459 // collect old faces
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500460 std::set<shared_ptr<Face>> facesToClose;
461 boost::copy(m_mcastFaces | boost::adaptors::map_values,
462 std::inserter(facesToClose, facesToClose.end()));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000463
Junxiao Shibbace1d2017-08-06 20:03:37 +0000464 // create faces if requested by config
465 for (const auto& netif : netmon->listNetworkInterfaces()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500466 auto facesToKeep = this->applyMcastConfigToNetif(netif);
467 for (const auto& face : facesToKeep) {
Junxiao Shibbace1d2017-08-06 20:03:37 +0000468 // don't destroy face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500469 facesToClose.erase(face);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000470 }
471 }
472
473 // destroy old faces that are not needed in new configuration
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500474 for (const auto& face : facesToClose) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000475 face->close();
476 }
477}
478
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400479} // namespace nfd::face