blob: a5d9dee15fd096516e607018f03cab5795657fdc [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/*
Junxiao Shia6286a92021-02-23 06:43:52 -07003 * Copyright (c) 2014-2021, 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
32namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040033namespace face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010034
Davide Pesaventoa3148082018-04-12 18:21:54 -040035NFD_LOG_INIT(UdpChannel);
Giulio Grassi624f6c62014-02-18 19:42:14 +010036
Davide Pesavento8fd15e62017-04-06 19:58:54 -040037namespace ip = boost::asio::ip;
Giulio Grassi624f6c62014-02-18 19:42:14 +010038
39UdpChannel::UdpChannel(const udp::Endpoint& localEndpoint,
Eric Newberry0c841642018-01-17 15:01:00 -070040 time::nanoseconds idleTimeout,
Junxiao Shia6286a92021-02-23 06:43:52 -070041 bool wantCongestionMarking,
42 size_t defaultMtu)
Giulio Grassi624f6c62014-02-18 19:42:14 +010043 : m_localEndpoint(localEndpoint)
Davide Pesavento292e5e12015-03-13 02:08:33 +010044 , m_socket(getGlobalIoService())
Davide Pesavento43ff2a92017-05-18 19:50:57 -040045 , m_idleFaceTimeout(idleTimeout)
Eric Newberry0c841642018-01-17 15:01:00 -070046 , m_wantCongestionMarking(wantCongestionMarking)
Giulio Grassi624f6c62014-02-18 19:42:14 +010047{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010048 setUri(FaceUri(m_localEndpoint));
Junxiao Shia6286a92021-02-23 06:43:52 -070049 setDefaultMtu(defaultMtu);
Davide Pesavento77911cc2017-04-08 22:12:30 -040050 NFD_LOG_CHAN_INFO("Creating channel");
Giulio Grassi624f6c62014-02-18 19:42:14 +010051}
52
Giulio Grassi624f6c62014-02-18 19:42:14 +010053void
54UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050055 const FaceParams& params,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060056 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010057 const FaceCreationFailedCallback& onConnectFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010058{
Junxiao Shic5401492015-12-23 16:33:21 -070059 shared_ptr<Face> face;
Giulio Grassi624f6c62014-02-18 19:42:14 +010060 try {
Davide Pesavento15b55052018-01-27 19:09:28 -050061 face = createFace(remoteEndpoint, params).second;
Giulio Grassi624f6c62014-02-18 19:42:14 +010062 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010063 catch (const boost::system::system_error& e) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040064 NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
Davide Pesavento292e5e12015-03-13 02:08:33 +010065 if (onConnectFailed)
Davide Pesaventoe4b22382018-06-10 14:37:24 -040066 onConnectFailed(504, "Face creation failed: "s + e.what());
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060067 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +010068 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010069
Davide Pesavento292e5e12015-03-13 02:08:33 +010070 // Need to invoke the callback regardless of whether or not we had already
71 // created the face so that control responses and such can be sent
72 onFaceCreated(face);
Giulio Grassi624f6c62014-02-18 19:42:14 +010073}
74
Junxiao Shic5401492015-12-23 16:33:21 -070075void
76UdpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento77911cc2017-04-08 22:12:30 -040077 const FaceCreationFailedCallback& onFaceCreationFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010078{
Junxiao Shic5401492015-12-23 16:33:21 -070079 if (isListening()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040080 NFD_LOG_CHAN_WARN("Already listening");
Junxiao Shic5401492015-12-23 16:33:21 -070081 return;
82 }
83
84 m_socket.open(m_localEndpoint.protocol());
85 m_socket.set_option(ip::udp::socket::reuse_address(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040086 if (m_localEndpoint.address().is_v6()) {
Junxiao Shic5401492015-12-23 16:33:21 -070087 m_socket.set_option(ip::v6_only(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040088 }
Junxiao Shic5401492015-12-23 16:33:21 -070089 m_socket.bind(m_localEndpoint);
Davide Pesavento77911cc2017-04-08 22:12:30 -040090
91 waitForNewPeer(onFaceCreated, onFaceCreationFailed);
92 NFD_LOG_CHAN_DEBUG("Started listening");
Junxiao Shic5401492015-12-23 16:33:21 -070093}
94
95void
96UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
97 const FaceCreationFailedCallback& onReceiveFailed)
98{
Davide Pesaventoacca18a2017-04-09 13:23:16 -040099 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint,
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400100 [=] (auto&&... args) {
101 this->handleNewPeer(std::forward<decltype(args)>(args)..., onFaceCreated, onReceiveFailed);
102 });
Junxiao Shic5401492015-12-23 16:33:21 -0700103}
104
105void
106UdpChannel::handleNewPeer(const boost::system::error_code& error,
107 size_t nBytesReceived,
108 const FaceCreatedCallback& onFaceCreated,
109 const FaceCreationFailedCallback& onReceiveFailed)
110{
111 if (error) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400112 if (error != boost::asio::error::operation_aborted) {
113 NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message());
114 if (onReceiveFailed)
115 onReceiveFailed(500, "Receive failed: " + error.message());
116 }
Junxiao Shic5401492015-12-23 16:33:21 -0700117 return;
118 }
119
Davide Pesavento77911cc2017-04-08 22:12:30 -0400120 NFD_LOG_CHAN_TRACE("New peer " << m_remoteEndpoint);
Junxiao Shic5401492015-12-23 16:33:21 -0700121
122 bool isCreated = false;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700123 shared_ptr<Face> face;
Junxiao Shic5401492015-12-23 16:33:21 -0700124 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500125 FaceParams params;
126 params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
Junxiao Shia6286a92021-02-23 06:43:52 -0700127 params.mtu = getDefaultMtu();
Davide Pesavento15b55052018-01-27 19:09:28 -0500128 std::tie(isCreated, face) = createFace(m_remoteEndpoint, params);
Junxiao Shic5401492015-12-23 16:33:21 -0700129 }
130 catch (const boost::system::system_error& e) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400131 NFD_LOG_CHAN_DEBUG("Face creation for " << m_remoteEndpoint << " failed: " << e.what());
Junxiao Shic5401492015-12-23 16:33:21 -0700132 if (onReceiveFailed)
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400133 onReceiveFailed(504, "Face creation failed: "s + e.what());
Junxiao Shic5401492015-12-23 16:33:21 -0700134 return;
135 }
136
137 if (isCreated)
138 onFaceCreated(face);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400139 else
140 NFD_LOG_CHAN_DEBUG("Received datagram for existing face");
Junxiao Shic5401492015-12-23 16:33:21 -0700141
142 // dispatch the datagram to the face for processing
Davide Pesavento77911cc2017-04-08 22:12:30 -0400143 auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport());
Davide Pesaventoacca18a2017-04-09 13:23:16 -0400144 transport->receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error);
Junxiao Shic5401492015-12-23 16:33:21 -0700145
Davide Pesavento77911cc2017-04-08 22:12:30 -0400146 waitForNewPeer(onFaceCreated, onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100147}
148
Junxiao Shicde37ad2015-12-24 01:02:05 -0700149std::pair<bool, shared_ptr<Face>>
Davide Pesavento77911cc2017-04-08 22:12:30 -0400150UdpChannel::createFace(const udp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -0500151 const FaceParams& params)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100152{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100153 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100154 if (it != m_channelFaces.end()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700155 // we already have a face for this endpoint, so reuse it
Davide Pesavento77911cc2017-04-08 22:12:30 -0400156 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000157 return {false, it->second};
Davide Pesavento292e5e12015-03-13 02:08:33 +0100158 }
Junxiao Shic099ddb2014-12-25 20:53:20 -0700159
Davide Pesavento292e5e12015-03-13 02:08:33 +0100160 // else, create a new face
161 ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol());
162 socket.set_option(ip::udp::socket::reuse_address(true));
163 socket.bind(m_localEndpoint);
164 socket.connect(remoteEndpoint);
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600165
Eric Newberry2642cd22017-07-13 21:34:53 -0400166 GenericLinkService::Options options;
Eric Newberry812d6152018-06-06 15:06:01 -0700167 options.allowFragmentation = true;
168 options.allowReassembly = true;
Davide Pesavento15b55052018-01-27 19:09:28 -0500169 options.reliabilityOptions.isEnabled = params.wantLpReliability;
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700170
171 if (boost::logic::indeterminate(params.wantCongestionMarking)) {
172 // Use default value for this channel if parameter is indeterminate
173 options.allowCongestionMarking = m_wantCongestionMarking;
174 }
175 else {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400176 options.allowCongestionMarking = bool(params.wantCongestionMarking);
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700177 }
178
179 if (params.baseCongestionMarkingInterval) {
180 options.baseCongestionMarkingInterval = *params.baseCongestionMarkingInterval;
181 }
182 if (params.defaultCongestionThreshold) {
183 options.defaultCongestionThreshold = *params.defaultCongestionThreshold;
184 }
185
Junxiao Shia6286a92021-02-23 06:43:52 -0700186 options.overrideMtu = params.mtu.value_or(getDefaultMtu());
Eric Newberrycb6551e2020-03-02 14:12:16 -0800187
Eric Newberry2642cd22017-07-13 21:34:53 -0400188 auto linkService = make_unique<GenericLinkService>(options);
Eric Newberry812d6152018-06-06 15:06:01 -0700189 auto transport = make_unique<UnicastUdpTransport>(std::move(socket), params.persistency,
Eric Newberrycb6551e2020-03-02 14:12:16 -0800190 m_idleFaceTimeout);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700191 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400192 face->setChannel(shared_from_this()); // use weak_from_this() in C++17
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600193
Yukai Tu0a49d342015-09-13 12:54:22 +0800194 m_channelFaces[remoteEndpoint] = face;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400195 connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
Junxiao Shic5401492015-12-23 16:33:21 -0700196
Davide Pesavento292e5e12015-03-13 02:08:33 +0100197 return {true, face};
Giulio Grassi624f6c62014-02-18 19:42:14 +0100198}
199
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400200} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100201} // namespace nfd