blob: 4e91045f1c263fbf8b6d4a79e0f6fa094c9fab1b [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry2642cd22017-07-13 21:34:53 -04002/*
Davide Pesaventoa599d2a2022-02-16 18:52:43 -05003 * 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/>.
Junxiao Shi5dd26c32014-07-20 23:15:14 -070024 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010025
26#include "udp-channel.hpp"
Davide Pesaventocb425e82019-07-14 21:48:22 -040027#include "face.hpp"
Yukai Tu0a49d342015-09-13 12:54:22 +080028#include "generic-link-service.hpp"
29#include "unicast-udp-transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040030#include "common/global.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010031
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010033
Davide Pesaventoa3148082018-04-12 18:21:54 -040034NFD_LOG_INIT(UdpChannel);
Giulio Grassi624f6c62014-02-18 19:42:14 +010035
Davide Pesavento8fd15e62017-04-06 19:58:54 -040036namespace ip = boost::asio::ip;
Giulio Grassi624f6c62014-02-18 19:42:14 +010037
38UdpChannel::UdpChannel(const udp::Endpoint& localEndpoint,
Eric Newberry0c841642018-01-17 15:01:00 -070039 time::nanoseconds idleTimeout,
Junxiao Shia6286a92021-02-23 06:43:52 -070040 bool wantCongestionMarking,
41 size_t defaultMtu)
Giulio Grassi624f6c62014-02-18 19:42:14 +010042 : m_localEndpoint(localEndpoint)
Davide Pesavento292e5e12015-03-13 02:08:33 +010043 , m_socket(getGlobalIoService())
Davide Pesavento43ff2a92017-05-18 19:50:57 -040044 , m_idleFaceTimeout(idleTimeout)
Eric Newberry0c841642018-01-17 15:01:00 -070045 , m_wantCongestionMarking(wantCongestionMarking)
Giulio Grassi624f6c62014-02-18 19:42:14 +010046{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010047 setUri(FaceUri(m_localEndpoint));
Junxiao Shia6286a92021-02-23 06:43:52 -070048 setDefaultMtu(defaultMtu);
Davide Pesavento77911cc2017-04-08 22:12:30 -040049 NFD_LOG_CHAN_INFO("Creating channel");
Giulio Grassi624f6c62014-02-18 19:42:14 +010050}
51
Giulio Grassi624f6c62014-02-18 19:42:14 +010052void
53UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050054 const FaceParams& params,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060055 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010056 const FaceCreationFailedCallback& onConnectFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010057{
Junxiao Shic5401492015-12-23 16:33:21 -070058 shared_ptr<Face> face;
Giulio Grassi624f6c62014-02-18 19:42:14 +010059 try {
Davide Pesavento15b55052018-01-27 19:09:28 -050060 face = createFace(remoteEndpoint, params).second;
Giulio Grassi624f6c62014-02-18 19:42:14 +010061 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010062 catch (const boost::system::system_error& e) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040063 NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
Davide Pesavento292e5e12015-03-13 02:08:33 +010064 if (onConnectFailed)
Davide Pesaventoe4b22382018-06-10 14:37:24 -040065 onConnectFailed(504, "Face creation failed: "s + e.what());
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060066 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +010067 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010068
Davide Pesavento292e5e12015-03-13 02:08:33 +010069 // Need to invoke the callback regardless of whether or not we had already
70 // created the face so that control responses and such can be sent
71 onFaceCreated(face);
Giulio Grassi624f6c62014-02-18 19:42:14 +010072}
73
Junxiao Shic5401492015-12-23 16:33:21 -070074void
75UdpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento77911cc2017-04-08 22:12:30 -040076 const FaceCreationFailedCallback& onFaceCreationFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010077{
Junxiao Shic5401492015-12-23 16:33:21 -070078 if (isListening()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040079 NFD_LOG_CHAN_WARN("Already listening");
Junxiao Shic5401492015-12-23 16:33:21 -070080 return;
81 }
82
83 m_socket.open(m_localEndpoint.protocol());
84 m_socket.set_option(ip::udp::socket::reuse_address(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040085 if (m_localEndpoint.address().is_v6()) {
Junxiao Shic5401492015-12-23 16:33:21 -070086 m_socket.set_option(ip::v6_only(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040087 }
Junxiao Shic5401492015-12-23 16:33:21 -070088 m_socket.bind(m_localEndpoint);
Davide Pesavento77911cc2017-04-08 22:12:30 -040089
90 waitForNewPeer(onFaceCreated, onFaceCreationFailed);
91 NFD_LOG_CHAN_DEBUG("Started listening");
Junxiao Shic5401492015-12-23 16:33:21 -070092}
93
94void
95UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
96 const FaceCreationFailedCallback& onReceiveFailed)
97{
Davide Pesaventoacca18a2017-04-09 13:23:16 -040098 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint,
Davide Pesaventoe4b22382018-06-10 14:37:24 -040099 [=] (auto&&... args) {
100 this->handleNewPeer(std::forward<decltype(args)>(args)..., onFaceCreated, onReceiveFailed);
101 });
Junxiao Shic5401492015-12-23 16:33:21 -0700102}
103
104void
105UdpChannel::handleNewPeer(const boost::system::error_code& error,
106 size_t nBytesReceived,
107 const FaceCreatedCallback& onFaceCreated,
108 const FaceCreationFailedCallback& onReceiveFailed)
109{
110 if (error) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400111 if (error != boost::asio::error::operation_aborted) {
112 NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message());
113 if (onReceiveFailed)
114 onReceiveFailed(500, "Receive failed: " + error.message());
115 }
Junxiao Shic5401492015-12-23 16:33:21 -0700116 return;
117 }
118
Davide Pesavento77911cc2017-04-08 22:12:30 -0400119 NFD_LOG_CHAN_TRACE("New peer " << m_remoteEndpoint);
Junxiao Shic5401492015-12-23 16:33:21 -0700120
121 bool isCreated = false;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700122 shared_ptr<Face> face;
Junxiao Shic5401492015-12-23 16:33:21 -0700123 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500124 FaceParams params;
125 params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
Junxiao Shia6286a92021-02-23 06:43:52 -0700126 params.mtu = getDefaultMtu();
Davide Pesavento15b55052018-01-27 19:09:28 -0500127 std::tie(isCreated, face) = createFace(m_remoteEndpoint, params);
Junxiao Shic5401492015-12-23 16:33:21 -0700128 }
129 catch (const boost::system::system_error& e) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400130 NFD_LOG_CHAN_DEBUG("Face creation for " << m_remoteEndpoint << " failed: " << e.what());
Junxiao Shic5401492015-12-23 16:33:21 -0700131 if (onReceiveFailed)
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400132 onReceiveFailed(504, "Face creation failed: "s + e.what());
Junxiao Shic5401492015-12-23 16:33:21 -0700133 return;
134 }
135
136 if (isCreated)
137 onFaceCreated(face);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400138 else
139 NFD_LOG_CHAN_DEBUG("Received datagram for existing face");
Junxiao Shic5401492015-12-23 16:33:21 -0700140
141 // dispatch the datagram to the face for processing
Davide Pesavento77911cc2017-04-08 22:12:30 -0400142 auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport());
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500143 transport->receiveDatagram(ndn::make_span(m_receiveBuffer).first(nBytesReceived), error);
Junxiao Shic5401492015-12-23 16:33:21 -0700144
Davide Pesavento77911cc2017-04-08 22:12:30 -0400145 waitForNewPeer(onFaceCreated, onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100146}
147
Junxiao Shicde37ad2015-12-24 01:02:05 -0700148std::pair<bool, shared_ptr<Face>>
Davide Pesavento77911cc2017-04-08 22:12:30 -0400149UdpChannel::createFace(const udp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -0500150 const FaceParams& params)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100151{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100152 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100153 if (it != m_channelFaces.end()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700154 // we already have a face for this endpoint, so reuse it
Davide Pesavento77911cc2017-04-08 22:12:30 -0400155 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000156 return {false, it->second};
Davide Pesavento292e5e12015-03-13 02:08:33 +0100157 }
Junxiao Shic099ddb2014-12-25 20:53:20 -0700158
Davide Pesavento292e5e12015-03-13 02:08:33 +0100159 // else, create a new face
160 ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol());
161 socket.set_option(ip::udp::socket::reuse_address(true));
162 socket.bind(m_localEndpoint);
163 socket.connect(remoteEndpoint);
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600164
Eric Newberry2642cd22017-07-13 21:34:53 -0400165 GenericLinkService::Options options;
Eric Newberry812d6152018-06-06 15:06:01 -0700166 options.allowFragmentation = true;
167 options.allowReassembly = true;
Davide Pesavento15b55052018-01-27 19:09:28 -0500168 options.reliabilityOptions.isEnabled = params.wantLpReliability;
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700169
170 if (boost::logic::indeterminate(params.wantCongestionMarking)) {
171 // Use default value for this channel if parameter is indeterminate
172 options.allowCongestionMarking = m_wantCongestionMarking;
173 }
174 else {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400175 options.allowCongestionMarking = bool(params.wantCongestionMarking);
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700176 }
177
178 if (params.baseCongestionMarkingInterval) {
179 options.baseCongestionMarkingInterval = *params.baseCongestionMarkingInterval;
180 }
181 if (params.defaultCongestionThreshold) {
182 options.defaultCongestionThreshold = *params.defaultCongestionThreshold;
183 }
184
Junxiao Shia6286a92021-02-23 06:43:52 -0700185 options.overrideMtu = params.mtu.value_or(getDefaultMtu());
Eric Newberrycb6551e2020-03-02 14:12:16 -0800186
Eric Newberry2642cd22017-07-13 21:34:53 -0400187 auto linkService = make_unique<GenericLinkService>(options);
Eric Newberry812d6152018-06-06 15:06:01 -0700188 auto transport = make_unique<UnicastUdpTransport>(std::move(socket), params.persistency,
Eric Newberrycb6551e2020-03-02 14:12:16 -0800189 m_idleFaceTimeout);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700190 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400191 face->setChannel(weak_from_this());
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600192
Yukai Tu0a49d342015-09-13 12:54:22 +0800193 m_channelFaces[remoteEndpoint] = face;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400194 connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
Junxiao Shic5401492015-12-23 16:33:21 -0700195
Davide Pesavento292e5e12015-03-13 02:08:33 +0100196 return {true, face};
Giulio Grassi624f6c62014-02-18 19:42:14 +0100197}
198
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400199} // namespace nfd::face