unicast-udp-transport.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 
27 #include "udp-protocol.hpp"
28 #include "common/global.hpp"
29 
30 #ifdef __linux__
31 #include <cerrno> // for errno
32 #include <cstring> // for std::strerror()
33 #include <netinet/in.h> // for IP_MTU_DISCOVER and IP_PMTUDISC_DONT
34 #include <sys/socket.h> // for setsockopt()
35 #endif
36 
37 namespace nfd::face {
38 
39 NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport<boost::asio::ip::udp, Unicast>), UnicastUdpTransport);
40 
41 UnicastUdpTransport::UnicastUdpTransport(protocol::socket&& socket,
42  ndn::nfd::FacePersistency persistency,
43  time::nanoseconds idleTimeout)
44  : DatagramTransport(std::move(socket))
45  , m_idleTimeout(idleTimeout)
46 {
47  this->setLocalUri(FaceUri(m_socket.local_endpoint()));
48  this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
49  this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
50  this->setPersistency(persistency);
51  this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
52  this->setMtu(udp::computeMtu(m_socket.local_endpoint()));
53 
54  NFD_LOG_FACE_DEBUG("Creating transport");
55 
56 #ifdef __linux__
57  //
58  // By default, Linux does path MTU discovery on IPv4 sockets,
59  // and sets the DF (Don't Fragment) flag on datagrams smaller
60  // than the interface MTU. However this does not work for us,
61  // because we cannot properly respond to ICMP "packet too big"
62  // messages by fragmenting the packet at the application level,
63  // since we want to rely on IP for fragmentation and reassembly.
64  //
65  // Therefore, we disable PMTU discovery, which prevents the kernel
66  // from setting the DF flag on outgoing datagrams, and thus allows
67  // routers along the path to perform fragmentation as needed.
68  //
69  const int value = IP_PMTUDISC_DONT;
70  if (::setsockopt(m_socket.native_handle(), IPPROTO_IP,
71  IP_MTU_DISCOVER, &value, sizeof(value)) < 0) {
72  NFD_LOG_FACE_WARN("Failed to disable path MTU discovery: " << std::strerror(errno));
73  }
74 #endif
75 
76  if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
77  m_idleTimeout > time::nanoseconds::zero()) {
78  scheduleClosureWhenIdle();
79  }
80 }
81 
82 bool
83 UnicastUdpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
84 {
85  return true;
86 }
87 
88 void
89 UnicastUdpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
90 {
91  if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
92  m_idleTimeout > time::nanoseconds::zero()) {
93  scheduleClosureWhenIdle();
94  }
95  else {
96  m_closeIfIdleEvent.cancel();
97  setExpirationTime(time::steady_clock::time_point::max());
98  }
99 }
100 
101 void
102 UnicastUdpTransport::scheduleClosureWhenIdle()
103 {
104  m_closeIfIdleEvent = getScheduler().schedule(m_idleTimeout, [this] {
105  if (!hasRecentlyReceived()) {
106  NFD_LOG_FACE_INFO("Closing due to inactivity");
107  this->close();
108  }
109  else {
111  scheduleClosureWhenIdle();
112  }
113  });
114  setExpirationTime(time::steady_clock::now() + m_idleTimeout);
115 }
116 
117 } // namespace nfd::face
Implements Transport for datagram-based protocols.
void setScope(ndn::nfd::FaceScope scope) noexcept
Definition: transport.hpp:346
void setPersistency(ndn::nfd::FacePersistency newPersistency)
Changes the persistency setting of the transport.
Definition: transport.cpp:152
ndn::nfd::FacePersistency getPersistency() const noexcept
Returns the current persistency setting of the transport.
Definition: transport.hpp:227
void setMtu(ssize_t mtu) noexcept
Definition: transport.cpp:114
void setExpirationTime(const time::steady_clock::time_point &expirationTime) noexcept
Definition: transport.hpp:377
void setLocalUri(const FaceUri &uri) noexcept
Definition: transport.hpp:334
void setLinkType(ndn::nfd::LinkType linkType) noexcept
Definition: transport.hpp:352
void setRemoteUri(const FaceUri &uri) noexcept
Definition: transport.hpp:340
void close()
Request the transport to be closed.
Definition: transport.cpp:67
void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
Invoked after the persistency has been changed.
bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const final
Invoked by canChangePersistencyTo to perform the check.
UnicastUdpTransport(protocol::socket &&socket, ndn::nfd::FacePersistency persistency, time::nanoseconds idleTimeout)
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_MEMBER_INIT_SPECIALIZED(cls, name)
Definition: logger.hpp:35
ssize_t computeMtu(const Endpoint &localEndpoint)
Computes the maximum payload size in a UDP packet.
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:45