NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
udp-channel.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
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.
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/>.
24  */
25 
26 #include "udp-channel.hpp"
27 #include "face.hpp"
28 #include "generic-link-service.hpp"
30 #include "common/global.hpp"
31 
32 namespace nfd {
33 namespace face {
34 
36 
37 namespace ip = boost::asio::ip;
38 
40  time::nanoseconds idleTimeout,
41  bool wantCongestionMarking,
42  size_t defaultMtu)
43  : m_localEndpoint(localEndpoint)
44  , m_socket(getGlobalIoService())
45  , m_idleFaceTimeout(idleTimeout)
46  , m_wantCongestionMarking(wantCongestionMarking)
47 {
48  setUri(FaceUri(m_localEndpoint));
49  setDefaultMtu(defaultMtu);
50  NFD_LOG_CHAN_INFO("Creating channel");
51 }
52 
53 void
54 UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
55  const FaceParams& params,
56  const FaceCreatedCallback& onFaceCreated,
57  const FaceCreationFailedCallback& onConnectFailed)
58 {
59  shared_ptr<Face> face;
60  try {
61  face = createFace(remoteEndpoint, params).second;
62  }
63  catch (const boost::system::system_error& e) {
64  NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
65  if (onConnectFailed)
66  onConnectFailed(504, "Face creation failed: "s + e.what());
67  return;
68  }
69 
70  // 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);
73 }
74 
75 void
77  const FaceCreationFailedCallback& onFaceCreationFailed)
78 {
79  if (isListening()) {
80  NFD_LOG_CHAN_WARN("Already listening");
81  return;
82  }
83 
84  m_socket.open(m_localEndpoint.protocol());
85  m_socket.set_option(ip::udp::socket::reuse_address(true));
86  if (m_localEndpoint.address().is_v6()) {
87  m_socket.set_option(ip::v6_only(true));
88  }
89  m_socket.bind(m_localEndpoint);
90 
91  waitForNewPeer(onFaceCreated, onFaceCreationFailed);
92  NFD_LOG_CHAN_DEBUG("Started listening");
93 }
94 
95 void
96 UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
97  const FaceCreationFailedCallback& onReceiveFailed)
98 {
99  m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint,
100  [=] (auto&&... args) {
101  this->handleNewPeer(std::forward<decltype(args)>(args)..., onFaceCreated, onReceiveFailed);
102  });
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) {
113  NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message());
114  if (onReceiveFailed)
115  onReceiveFailed(500, "Receive failed: " + error.message());
116  }
117  return;
118  }
119 
120  NFD_LOG_CHAN_TRACE("New peer " << m_remoteEndpoint);
121 
122  bool isCreated = false;
123  shared_ptr<Face> face;
124  try {
125  FaceParams params;
127  params.mtu = getDefaultMtu();
128  std::tie(isCreated, face) = createFace(m_remoteEndpoint, params);
129  }
130  catch (const boost::system::system_error& e) {
131  NFD_LOG_CHAN_DEBUG("Face creation for " << m_remoteEndpoint << " failed: " << e.what());
132  if (onReceiveFailed)
133  onReceiveFailed(504, "Face creation failed: "s + e.what());
134  return;
135  }
136 
137  if (isCreated)
138  onFaceCreated(face);
139  else
140  NFD_LOG_CHAN_DEBUG("Received datagram for existing face");
141 
142  // dispatch the datagram to the face for processing
143  auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport());
144  transport->receiveDatagram(ndn::make_span(m_receiveBuffer).first(nBytesReceived), error);
145 
146  waitForNewPeer(onFaceCreated, onReceiveFailed);
147 }
148 
149 std::pair<bool, shared_ptr<Face>>
150 UdpChannel::createFace(const udp::Endpoint& remoteEndpoint,
151  const FaceParams& params)
152 {
153  auto it = m_channelFaces.find(remoteEndpoint);
154  if (it != m_channelFaces.end()) {
155  // we already have a face for this endpoint, so reuse it
156  NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
157  return {false, it->second};
158  }
159 
160  // 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);
165 
166  GenericLinkService::Options options;
167  options.allowFragmentation = true;
168  options.allowReassembly = true;
169  options.reliabilityOptions.isEnabled = params.wantLpReliability;
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 {
176  options.allowCongestionMarking = bool(params.wantCongestionMarking);
177  }
178 
179  if (params.baseCongestionMarkingInterval) {
180  options.baseCongestionMarkingInterval = *params.baseCongestionMarkingInterval;
181  }
182  if (params.defaultCongestionThreshold) {
183  options.defaultCongestionThreshold = *params.defaultCongestionThreshold;
184  }
185 
186  options.overrideMtu = params.mtu.value_or(getDefaultMtu());
187 
188  auto linkService = make_unique<GenericLinkService>(options);
189  auto transport = make_unique<UnicastUdpTransport>(std::move(socket), params.persistency,
190  m_idleFaceTimeout);
191  auto face = make_shared<Face>(std::move(linkService), std::move(transport));
192  face->setChannel(shared_from_this()); // use weak_from_this() in C++17
193 
194  m_channelFaces[remoteEndpoint] = face;
195  connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
196 
197  return {true, face};
198 }
199 
200 } // namespace face
201 } // namespace nfd
optional< uint64_t > defaultCongestionThreshold
Definition: face-common.hpp:82
bool isListening() const final
Returns whether the channel is listening.
Definition: udp-channel.hpp:56
void connect(const udp::Endpoint &remoteEndpoint, const FaceParams &params, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a unicast UDP face toward remoteEndpoint.
Definition: udp-channel.cpp:54
void setUri(const FaceUri &uri)
Definition: channel.cpp:35
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
boost::logic::tribool wantCongestionMarking
Definition: face-common.hpp:86
ndn::nfd::FacePersistency persistency
Definition: face-common.hpp:80
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:49
std::function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
Definition: channel.hpp:90
size_t getDefaultMtu() const
Returns the default MTU for all faces created by this channel.
Definition: channel.hpp:56
A Transport that communicates on a unicast UDP socket.
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
Definition: channel.cpp:47
UdpChannel(const udp::Endpoint &localEndpoint, time::nanoseconds idleTimeout, bool wantCongestionMarking, size_t defaultMtu)
Create a UDP channel on the given localEndpoint.
Definition: udp-channel.cpp:39
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
Definition: channel-log.hpp:49
optional< ssize_t > mtu
Definition: face-common.hpp:83
optional< time::nanoseconds > baseCongestionMarkingInterval
Definition: face-common.hpp:81
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
Definition: channel-log.hpp:52
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
Definition: channel-log.hpp:46
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
Definition: channel-log.hpp:55
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
boost::asio::ip::udp::endpoint Endpoint
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onFaceCreationFailed)
Start listening.
Definition: udp-channel.cpp:76
void setDefaultMtu(size_t mtu)
Definition: channel.cpp:41
void receiveDatagram(span< const uint8_t > buffer, const boost::system::error_code &error)
Receive datagram, translate buffer into packet, deliver to parent class.
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:86
Catch-all error for socket component errors that don&#39;t fit in other categories.
Definition: base.hpp:83
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50
Parameters used to set Transport properties or LinkService options on a newly created face...
Definition: face-common.hpp:78
Class implementing UDP-based channel to create faces.
Definition: udp-channel.hpp:40