Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 24 | */ |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 25 | |
| 26 | #include "udp-channel.hpp" |
Davide Pesavento | cb425e8 | 2019-07-14 21:48:22 -0400 | [diff] [blame] | 27 | #include "face.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 28 | #include "generic-link-service.hpp" |
| 29 | #include "unicast-udp-transport.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 30 | #include "common/global.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 31 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 32 | #include <boost/asio/ip/v6_only.hpp> |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::face { |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 35 | |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 36 | namespace ip = boost::asio::ip; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 37 | |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 38 | NFD_LOG_INIT(UdpChannel); |
| 39 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 40 | UdpChannel::UdpChannel(const udp::Endpoint& localEndpoint, |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 41 | time::nanoseconds idleTimeout, |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 42 | bool wantCongestionMarking, |
| 43 | size_t defaultMtu) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 44 | : m_localEndpoint(localEndpoint) |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 45 | , m_socket(getGlobalIoService()) |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 46 | , m_idleFaceTimeout(idleTimeout) |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 47 | , m_wantCongestionMarking(wantCongestionMarking) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 48 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 49 | setUri(FaceUri(m_localEndpoint)); |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 50 | setDefaultMtu(defaultMtu); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 51 | NFD_LOG_CHAN_INFO("Creating channel"); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 54 | void |
| 55 | UdpChannel::connect(const udp::Endpoint& remoteEndpoint, |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 56 | const FaceParams& params, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 57 | const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 58 | const FaceCreationFailedCallback& onConnectFailed) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 59 | { |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 60 | shared_ptr<Face> face; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 61 | try { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 62 | face = createFace(remoteEndpoint, params).second; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 63 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 64 | catch (const boost::system::system_error& e) { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 65 | NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what()); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 66 | if (onConnectFailed) |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 67 | onConnectFailed(504, "Face creation failed: "s + e.what()); |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 68 | return; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 69 | } |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 70 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 71 | // Need to invoke the callback regardless of whether or not we had already |
| 72 | // created the face so that control responses and such can be sent |
| 73 | onFaceCreated(face); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 76 | void |
| 77 | UdpChannel::listen(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 78 | const FaceCreationFailedCallback& onFaceCreationFailed) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 79 | { |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 80 | if (isListening()) { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 81 | NFD_LOG_CHAN_WARN("Already listening"); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
| 85 | m_socket.open(m_localEndpoint.protocol()); |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 86 | m_socket.set_option(boost::asio::socket_base::reuse_address(true)); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 87 | if (m_localEndpoint.address().is_v6()) { |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 88 | m_socket.set_option(ip::v6_only(true)); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 89 | } |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 90 | m_socket.bind(m_localEndpoint); |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 91 | |
| 92 | waitForNewPeer(onFaceCreated, onFaceCreationFailed); |
| 93 | NFD_LOG_CHAN_DEBUG("Started listening"); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
| 97 | UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated, |
| 98 | const FaceCreationFailedCallback& onReceiveFailed) |
| 99 | { |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame^] | 100 | m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint, [=] (auto&&... args) { |
| 101 | handleNewPeer(std::forward<decltype(args)>(args)..., onFaceCreated, onReceiveFailed); |
| 102 | }); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void |
| 106 | UdpChannel::handleNewPeer(const boost::system::error_code& error, |
| 107 | size_t nBytesReceived, |
| 108 | const FaceCreatedCallback& onFaceCreated, |
| 109 | const FaceCreationFailedCallback& onReceiveFailed) |
| 110 | { |
| 111 | if (error) { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 112 | 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 Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 120 | NFD_LOG_CHAN_TRACE("New peer " << m_remoteEndpoint); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 121 | |
| 122 | bool isCreated = false; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 123 | shared_ptr<Face> face; |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 124 | try { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 125 | FaceParams params; |
| 126 | params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND; |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 127 | params.mtu = getDefaultMtu(); |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 128 | std::tie(isCreated, face) = createFace(m_remoteEndpoint, params); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 129 | } |
| 130 | catch (const boost::system::system_error& e) { |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 131 | NFD_LOG_CHAN_DEBUG("Face creation for " << m_remoteEndpoint << " failed: " << e.what()); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 132 | if (onReceiveFailed) |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 133 | onReceiveFailed(504, "Face creation failed: "s + e.what()); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
| 137 | if (isCreated) |
| 138 | onFaceCreated(face); |
Davide Pesavento | 43ff2a9 | 2017-05-18 19:50:57 -0400 | [diff] [blame] | 139 | else |
| 140 | NFD_LOG_CHAN_DEBUG("Received datagram for existing face"); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 141 | |
| 142 | // dispatch the datagram to the face for processing |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 143 | auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport()); |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 144 | transport->receiveDatagram(ndn::make_span(m_receiveBuffer).first(nBytesReceived), error); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 145 | |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 146 | waitForNewPeer(onFaceCreated, onReceiveFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 149 | std::pair<bool, shared_ptr<Face>> |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 150 | UdpChannel::createFace(const udp::Endpoint& remoteEndpoint, |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 151 | const FaceParams& params) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 152 | { |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 153 | auto it = m_channelFaces.find(remoteEndpoint); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 154 | if (it != m_channelFaces.end()) { |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 155 | // we already have a face for this endpoint, so reuse it |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 156 | NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 157 | return {false, it->second}; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 158 | } |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 159 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 160 | // else, create a new face |
| 161 | ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol()); |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 162 | socket.set_option(boost::asio::socket_base::reuse_address(true)); |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 163 | socket.bind(m_localEndpoint); |
| 164 | socket.connect(remoteEndpoint); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 165 | |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 166 | GenericLinkService::Options options; |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 167 | options.allowFragmentation = true; |
| 168 | options.allowReassembly = true; |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 169 | options.reliabilityOptions.isEnabled = params.wantLpReliability; |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 170 | |
| 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 Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 176 | options.allowCongestionMarking = bool(params.wantCongestionMarking); |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | if (params.baseCongestionMarkingInterval) { |
| 180 | options.baseCongestionMarkingInterval = *params.baseCongestionMarkingInterval; |
| 181 | } |
| 182 | if (params.defaultCongestionThreshold) { |
| 183 | options.defaultCongestionThreshold = *params.defaultCongestionThreshold; |
| 184 | } |
| 185 | |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 186 | options.overrideMtu = params.mtu.value_or(getDefaultMtu()); |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame] | 187 | |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 188 | auto linkService = make_unique<GenericLinkService>(options); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 189 | auto transport = make_unique<UnicastUdpTransport>(std::move(socket), params.persistency, |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame] | 190 | m_idleFaceTimeout); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 191 | auto face = make_shared<Face>(std::move(linkService), std::move(transport)); |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 192 | face->setChannel(weak_from_this()); |
Steve DiBenedetto | 2aa4eca | 2014-06-20 10:47:40 -0600 | [diff] [blame] | 193 | |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 194 | m_channelFaces[remoteEndpoint] = face; |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 195 | connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); }); |
Junxiao Shi | c540149 | 2015-12-23 16:33:21 -0700 | [diff] [blame] | 196 | |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 197 | return {true, face}; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 200 | } // namespace nfd::face |