NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019, 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 {
38 namespace face {
39 
41 
42 UnicastUdpTransport::UnicastUdpTransport(protocol::socket&& socket,
43  ndn::nfd::FacePersistency persistency,
44  time::nanoseconds idleTimeout,
45  optional<ssize_t> overrideMtu)
46  : DatagramTransport(std::move(socket))
47  , m_idleTimeout(idleTimeout)
48 {
49  this->setLocalUri(FaceUri(m_socket.local_endpoint()));
50  this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
52  this->setPersistency(persistency);
54 
55  if (overrideMtu) {
56  this->setMtu(std::min(udp::computeMtu(m_socket.local_endpoint()), *overrideMtu));
57  }
58  else {
59  this->setMtu(udp::computeMtu(m_socket.local_endpoint()));
60  }
61  BOOST_ASSERT(this->getMtu() >= MIN_MTU);
62 
63  NFD_LOG_FACE_DEBUG("Creating transport");
64 
65 #ifdef __linux__
66  //
67  // By default, Linux does path MTU discovery on IPv4 sockets,
68  // and sets the DF (Don't Fragment) flag on datagrams smaller
69  // than the interface MTU. However this does not work for us,
70  // because we cannot properly respond to ICMP "packet too big"
71  // messages by fragmenting the packet at the application level,
72  // since we want to rely on IP for fragmentation and reassembly.
73  //
74  // Therefore, we disable PMTU discovery, which prevents the kernel
75  // from setting the DF flag on outgoing datagrams, and thus allows
76  // routers along the path to perform fragmentation as needed.
77  //
78  const int value = IP_PMTUDISC_DONT;
79  if (::setsockopt(m_socket.native_handle(), IPPROTO_IP,
80  IP_MTU_DISCOVER, &value, sizeof(value)) < 0) {
81  NFD_LOG_FACE_WARN("Failed to disable path MTU discovery: " << std::strerror(errno));
82  }
83 #endif
84 
86  m_idleTimeout > time::nanoseconds::zero()) {
87  scheduleClosureWhenIdle();
88  }
89 }
90 
91 bool
93 {
94  return true;
95 }
96 
97 void
99 {
101  m_idleTimeout > time::nanoseconds::zero()) {
102  scheduleClosureWhenIdle();
103  }
104  else {
105  m_closeIfIdleEvent.cancel();
106  setExpirationTime(time::steady_clock::TimePoint::max());
107  }
108 }
109 
110 void
111 UnicastUdpTransport::scheduleClosureWhenIdle()
112 {
113  m_closeIfIdleEvent = getScheduler().schedule(m_idleTimeout, [this] {
114  if (!hasRecentlyReceived()) {
115  NFD_LOG_FACE_INFO("Closing due to inactivity");
116  this->close();
117  }
118  else {
120  scheduleClosureWhenIdle();
121  }
122  });
123  setExpirationTime(time::steady_clock::now() + m_idleTimeout);
124 }
125 
126 } // namespace face
127 } // namespace nfd
global.hpp
ndn::detail::ScopedCancelHandle::cancel
void cancel()
Cancel the operation.
Definition: cancel-handle.hpp:109
ndn::nfd::FACE_SCOPE_NON_LOCAL
@ FACE_SCOPE_NON_LOCAL
face is non-local
Definition: nfd-constants.hpp:36
nfd::face::Transport::setMtu
void setMtu(ssize_t mtu)
Definition: transport.hpp:448
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
nfd::face::Transport::setRemoteUri
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:406
nfd::udp::computeMtu
ssize_t computeMtu(const Endpoint &localEndpoint)
computes maximum payload size in a UDP packet
Definition: udp-protocol.cpp:32
udp-protocol.hpp
ndn::nfd::LINK_TYPE_POINT_TO_POINT
@ LINK_TYPE_POINT_TO_POINT
link is point-to-point
Definition: nfd-constants.hpp:59
ndn::FaceUri
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:45
nfd::face::UnicastUdpTransport::UnicastUdpTransport
UnicastUdpTransport(protocol::socket &&socket, ndn::nfd::FacePersistency persistency, time::nanoseconds idleTimeout, optional< ssize_t > overrideMtu={})
Definition: unicast-udp-transport.cpp:42
nfd::face::Transport::setScope
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:418
ndn::time::steady_clock::now
static time_point now() noexcept
Definition: time.cpp:80
nfd::face::UnicastUdpTransport::canChangePersistencyToImpl
bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const final
invoked by canChangePersistencyTo to perform the check
Definition: unicast-udp-transport.cpp:92
nfd::face::NFD_LOG_MEMBER_INIT_SPECIALIZED
NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport< boost::asio::ip::udp, Multicast >), MulticastUdpTransport)
NFD_LOG_FACE_DEBUG
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
Definition: face-common.hpp:136
nfd::face::Transport::getPersistency
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:424
nfd::face::Transport::setExpirationTime
void setExpirationTime(const time::steady_clock::TimePoint &expirationTime)
Definition: transport.hpp:479
nfd::face::DatagramTransport< boost::asio::ip::udp, Unicast >::resetRecentlyReceived
void resetRecentlyReceived()
Definition: datagram-transport.hpp:255
ndn::nfd::FacePersistency
FacePersistency
Definition: nfd-constants.hpp:45
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::face::DatagramTransport< boost::asio::ip::udp, Unicast >::m_socket
protocol::socket m_socket
Definition: datagram-transport.hpp:93
nfd::getScheduler
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:70
nfd::face::DatagramTransport< boost::asio::ip::udp, Unicast >
nfd::face::Transport::setLinkType
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:436
ndn::nfd::FACE_PERSISTENCY_ON_DEMAND
@ FACE_PERSISTENCY_ON_DEMAND
face is on-demand
Definition: nfd-constants.hpp:48
unicast-udp-transport.hpp
nfd::face::Transport::setPersistency
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:150
nfd::face::UnicastUdpTransport::afterChangePersistency
void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
invoked after the persistency has been changed
Definition: unicast-udp-transport.cpp:98
NFD_LOG_FACE_INFO
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-common.hpp:139
nfd::face::UnicastUdpTransport
A Transport that communicates on a unicast UDP socket.
Definition: unicast-udp-transport.hpp:40
nfd::face::DatagramTransport< boost::asio::ip::udp, Unicast >::hasRecentlyReceived
bool hasRecentlyReceived() const
Definition: datagram-transport.hpp:248
nfd::face::Transport::MIN_MTU
static constexpr ssize_t MIN_MTU
minimum MTU that may be set on a transport
Definition: transport.hpp:347
nfd::face::Transport::close
void close()
Request the transport to be closed.
Definition: transport.cpp:81
NFD_LOG_FACE_WARN
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Definition: face-common.hpp:142
nfd::face::Transport::getMtu
ssize_t getMtu() const
Definition: transport.hpp:442
nfd::face::Transport::setLocalUri
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:394