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-2018, 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 
29 #ifdef __linux__
30 #include <cerrno> // for errno
31 #include <cstring> // for std::strerror()
32 #include <netinet/in.h> // for IP_MTU_DISCOVER and IP_PMTUDISC_DONT
33 #include <sys/socket.h> // for setsockopt()
34 #endif
35 
36 namespace nfd {
37 namespace 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  optional<ssize_t> overrideMtu)
45  : DatagramTransport(std::move(socket))
46  , m_idleTimeout(idleTimeout)
47 {
48  this->setLocalUri(FaceUri(m_socket.local_endpoint()));
49  this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
51  this->setPersistency(persistency);
53 
54  if (overrideMtu) {
55  this->setMtu(std::min(udp::computeMtu(m_socket.local_endpoint()), *overrideMtu));
56  }
57  else {
58  this->setMtu(udp::computeMtu(m_socket.local_endpoint()));
59  }
60  BOOST_ASSERT(this->getMtu() >= MIN_MTU);
61 
62  NFD_LOG_FACE_INFO("Creating transport");
63 
64 #ifdef __linux__
65  //
66  // By default, Linux does path MTU discovery on IPv4 sockets,
67  // and sets the DF (Don't Fragment) flag on datagrams smaller
68  // than the interface MTU. However this does not work for us,
69  // because we cannot properly respond to ICMP "packet too big"
70  // messages by fragmenting the packet at the application level,
71  // since we want to rely on IP for fragmentation and reassembly.
72  //
73  // Therefore, we disable PMTU discovery, which prevents the kernel
74  // from setting the DF flag on outgoing datagrams, and thus allows
75  // routers along the path to perform fragmentation as needed.
76  //
77  const int value = IP_PMTUDISC_DONT;
78  if (::setsockopt(m_socket.native_handle(), IPPROTO_IP,
79  IP_MTU_DISCOVER, &value, sizeof(value)) < 0) {
80  NFD_LOG_FACE_WARN("Failed to disable path MTU discovery: " << std::strerror(errno));
81  }
82 #endif
83 
85  m_idleTimeout > time::nanoseconds::zero()) {
86  scheduleClosureWhenIdle();
87  }
88 }
89 
90 bool
92 {
93  return true;
94 }
95 
96 void
98 {
100  m_idleTimeout > time::nanoseconds::zero()) {
101  scheduleClosureWhenIdle();
102  }
103  else {
104  m_closeIfIdleEvent.cancel();
105  setExpirationTime(time::steady_clock::TimePoint::max());
106  }
107 }
108 
109 void
110 UnicastUdpTransport::scheduleClosureWhenIdle()
111 {
112  m_closeIfIdleEvent = scheduler::schedule(m_idleTimeout, [this] {
113  if (!hasRecentlyReceived()) {
114  NFD_LOG_FACE_INFO("Closing due to inactivity");
115  this->close();
116  }
117  else {
119  scheduleClosureWhenIdle();
120  }
121  });
122  setExpirationTime(time::steady_clock::now() + m_idleTimeout);
123 }
124 
125 } // namespace face
126 } // namespace nfd
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:154
void setExpirationTime(const time::steady_clock::TimePoint &expirationTime)
Definition: transport.hpp:506
NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport< boost::asio::ip::udp, Multicast >), MulticastUdpTransport)
bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const final
invoked by canChangePersistencyTo to perform the check
void setRemoteUri(const FaceUri &uri)
Definition: transport.hpp:433
static time_point now() noexcept
Definition: time.cpp:80
static constexpr ssize_t MIN_MTU
minimum MTU that may be set on a transport
Definition: transport.hpp:374
void close()
request the transport to be closed
Definition: transport.cpp:87
void setLinkType(ndn::nfd::LinkType linkType)
Definition: transport.hpp:463
Implements Transport for datagram-based protocols.
void setMtu(ssize_t mtu)
Definition: transport.hpp:475
ssize_t computeMtu(const Endpoint &localEndpoint)
computes maximum payload size in a UDP packet
void setScope(ndn::nfd::FaceScope scope)
Definition: transport.hpp:445
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-log.hpp:85
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
void cancel()
Cancel the operation.
ssize_t getMtu() const
Definition: transport.hpp:469
void setLocalUri(const FaceUri &uri)
Definition: transport.hpp:421
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
invoked after the persistency has been changed
ndn::nfd::FacePersistency getPersistency() const
Definition: transport.hpp:451
UnicastUdpTransport(protocol::socket &&socket, ndn::nfd::FacePersistency persistency, time::nanoseconds idleTimeout, optional< ssize_t > overrideMtu={})
EventId schedule(time::nanoseconds after, const EventCallback &event)
Schedule an event.
Definition: scheduler.cpp:48
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Definition: face-log.hpp:88