blob: 3d6e6b88dcc3dd7529ec7f12c63dac42bc7b478b [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/*
Eric Newberry0c841642018-01-17 15:01:00 -07003 * Copyright (c) 2014-2018, 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"
Yukai Tu0a49d342015-09-13 12:54:22 +080027#include "generic-link-service.hpp"
28#include "unicast-udp-transport.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010029#include "core/global-io.hpp"
30
31namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040032namespace 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,
40 bool wantCongestionMarking)
Giulio Grassi624f6c62014-02-18 19:42:14 +010041 : m_localEndpoint(localEndpoint)
Davide Pesavento292e5e12015-03-13 02:08:33 +010042 , m_socket(getGlobalIoService())
Davide Pesavento43ff2a92017-05-18 19:50:57 -040043 , m_idleFaceTimeout(idleTimeout)
Eric Newberry0c841642018-01-17 15:01:00 -070044 , m_wantCongestionMarking(wantCongestionMarking)
Giulio Grassi624f6c62014-02-18 19:42:14 +010045{
Davide Pesavento6ad890a2015-03-09 03:43:17 +010046 setUri(FaceUri(m_localEndpoint));
Davide Pesavento77911cc2017-04-08 22:12:30 -040047 NFD_LOG_CHAN_INFO("Creating channel");
Giulio Grassi624f6c62014-02-18 19:42:14 +010048}
49
Giulio Grassi624f6c62014-02-18 19:42:14 +010050void
51UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -050052 const FaceParams& params,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060053 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010054 const FaceCreationFailedCallback& onConnectFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010055{
Junxiao Shic5401492015-12-23 16:33:21 -070056 shared_ptr<Face> face;
Giulio Grassi624f6c62014-02-18 19:42:14 +010057 try {
Davide Pesavento15b55052018-01-27 19:09:28 -050058 face = createFace(remoteEndpoint, params).second;
Giulio Grassi624f6c62014-02-18 19:42:14 +010059 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010060 catch (const boost::system::system_error& e) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040061 NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
Davide Pesavento292e5e12015-03-13 02:08:33 +010062 if (onConnectFailed)
Davide Pesavento77911cc2017-04-08 22:12:30 -040063 onConnectFailed(504, std::string("Face creation failed: ") + e.what());
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060064 return;
Giulio Grassi624f6c62014-02-18 19:42:14 +010065 }
Davide Pesavento6ad890a2015-03-09 03:43:17 +010066
Davide Pesavento292e5e12015-03-13 02:08:33 +010067 // Need to invoke the callback regardless of whether or not we had already
68 // created the face so that control responses and such can be sent
69 onFaceCreated(face);
Giulio Grassi624f6c62014-02-18 19:42:14 +010070}
71
Junxiao Shic5401492015-12-23 16:33:21 -070072void
73UdpChannel::listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento77911cc2017-04-08 22:12:30 -040074 const FaceCreationFailedCallback& onFaceCreationFailed)
Giulio Grassi624f6c62014-02-18 19:42:14 +010075{
Junxiao Shic5401492015-12-23 16:33:21 -070076 if (isListening()) {
Davide Pesavento77911cc2017-04-08 22:12:30 -040077 NFD_LOG_CHAN_WARN("Already listening");
Junxiao Shic5401492015-12-23 16:33:21 -070078 return;
79 }
80
81 m_socket.open(m_localEndpoint.protocol());
82 m_socket.set_option(ip::udp::socket::reuse_address(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040083 if (m_localEndpoint.address().is_v6()) {
Junxiao Shic5401492015-12-23 16:33:21 -070084 m_socket.set_option(ip::v6_only(true));
Davide Pesavento77911cc2017-04-08 22:12:30 -040085 }
Junxiao Shic5401492015-12-23 16:33:21 -070086 m_socket.bind(m_localEndpoint);
Davide Pesavento77911cc2017-04-08 22:12:30 -040087
88 waitForNewPeer(onFaceCreated, onFaceCreationFailed);
89 NFD_LOG_CHAN_DEBUG("Started listening");
Junxiao Shic5401492015-12-23 16:33:21 -070090}
91
92void
93UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
94 const FaceCreationFailedCallback& onReceiveFailed)
95{
Davide Pesaventoacca18a2017-04-09 13:23:16 -040096 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint,
Junxiao Shic5401492015-12-23 16:33:21 -070097 bind(&UdpChannel::handleNewPeer, this,
98 boost::asio::placeholders::error,
99 boost::asio::placeholders::bytes_transferred,
100 onFaceCreated, onReceiveFailed));
101}
102
103void
104UdpChannel::handleNewPeer(const boost::system::error_code& error,
105 size_t nBytesReceived,
106 const FaceCreatedCallback& onFaceCreated,
107 const FaceCreationFailedCallback& onReceiveFailed)
108{
109 if (error) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400110 if (error != boost::asio::error::operation_aborted) {
111 NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message());
112 if (onReceiveFailed)
113 onReceiveFailed(500, "Receive failed: " + error.message());
114 }
Junxiao Shic5401492015-12-23 16:33:21 -0700115 return;
116 }
117
Davide Pesavento77911cc2017-04-08 22:12:30 -0400118 NFD_LOG_CHAN_TRACE("New peer " << m_remoteEndpoint);
Junxiao Shic5401492015-12-23 16:33:21 -0700119
120 bool isCreated = false;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700121 shared_ptr<Face> face;
Junxiao Shic5401492015-12-23 16:33:21 -0700122 try {
Davide Pesavento15b55052018-01-27 19:09:28 -0500123 FaceParams params;
124 params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
125 std::tie(isCreated, face) = createFace(m_remoteEndpoint, params);
Junxiao Shic5401492015-12-23 16:33:21 -0700126 }
127 catch (const boost::system::system_error& e) {
Davide Pesavento77911cc2017-04-08 22:12:30 -0400128 NFD_LOG_CHAN_DEBUG("Face creation for " << m_remoteEndpoint << " failed: " << e.what());
Junxiao Shic5401492015-12-23 16:33:21 -0700129 if (onReceiveFailed)
Davide Pesavento77911cc2017-04-08 22:12:30 -0400130 onReceiveFailed(504, std::string("Face creation failed: ") + e.what());
Junxiao Shic5401492015-12-23 16:33:21 -0700131 return;
132 }
133
134 if (isCreated)
135 onFaceCreated(face);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400136 else
137 NFD_LOG_CHAN_DEBUG("Received datagram for existing face");
Junxiao Shic5401492015-12-23 16:33:21 -0700138
139 // dispatch the datagram to the face for processing
Davide Pesavento77911cc2017-04-08 22:12:30 -0400140 auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport());
Davide Pesaventoacca18a2017-04-09 13:23:16 -0400141 transport->receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error);
Junxiao Shic5401492015-12-23 16:33:21 -0700142
Davide Pesavento77911cc2017-04-08 22:12:30 -0400143 waitForNewPeer(onFaceCreated, onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100144}
145
Junxiao Shicde37ad2015-12-24 01:02:05 -0700146std::pair<bool, shared_ptr<Face>>
Davide Pesavento77911cc2017-04-08 22:12:30 -0400147UdpChannel::createFace(const udp::Endpoint& remoteEndpoint,
Davide Pesavento15b55052018-01-27 19:09:28 -0500148 const FaceParams& params)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100149{
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100150 auto it = m_channelFaces.find(remoteEndpoint);
Davide Pesavento292e5e12015-03-13 02:08:33 +0100151 if (it != m_channelFaces.end()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700152 // we already have a face for this endpoint, so reuse it
Davide Pesavento77911cc2017-04-08 22:12:30 -0400153 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000154 return {false, it->second};
Davide Pesavento292e5e12015-03-13 02:08:33 +0100155 }
Junxiao Shic099ddb2014-12-25 20:53:20 -0700156
Davide Pesavento292e5e12015-03-13 02:08:33 +0100157 // else, create a new face
158 ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol());
159 socket.set_option(ip::udp::socket::reuse_address(true));
160 socket.bind(m_localEndpoint);
161 socket.connect(remoteEndpoint);
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600162
Eric Newberry2642cd22017-07-13 21:34:53 -0400163 GenericLinkService::Options options;
Davide Pesavento15b55052018-01-27 19:09:28 -0500164 options.reliabilityOptions.isEnabled = params.wantLpReliability;
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700165
166 if (boost::logic::indeterminate(params.wantCongestionMarking)) {
167 // Use default value for this channel if parameter is indeterminate
168 options.allowCongestionMarking = m_wantCongestionMarking;
169 }
170 else {
171 options.allowCongestionMarking = params.wantCongestionMarking;
172 }
173
174 if (params.baseCongestionMarkingInterval) {
175 options.baseCongestionMarkingInterval = *params.baseCongestionMarkingInterval;
176 }
177 if (params.defaultCongestionThreshold) {
178 options.defaultCongestionThreshold = *params.defaultCongestionThreshold;
179 }
180
Eric Newberry2642cd22017-07-13 21:34:53 -0400181 auto linkService = make_unique<GenericLinkService>(options);
Davide Pesavento15b55052018-01-27 19:09:28 -0500182 auto transport = make_unique<UnicastUdpTransport>(std::move(socket), params.persistency, m_idleFaceTimeout);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700183 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
Steve DiBenedetto2aa4eca2014-06-20 10:47:40 -0600184
Yukai Tu0a49d342015-09-13 12:54:22 +0800185 m_channelFaces[remoteEndpoint] = face;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400186 connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
Junxiao Shic5401492015-12-23 16:33:21 -0700187
Davide Pesavento292e5e12015-03-13 02:08:33 +0100188 return {true, face};
Giulio Grassi624f6c62014-02-18 19:42:14 +0100189}
190
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400191} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100192} // namespace nfd