blob: 8e3d7d8ee212aae1943414eb0d730450992e30f9 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Alexander Afanasyev29d1fab2014-07-07 19:27:16 -070024 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010025
Yukai Tu0a49d342015-09-13 12:54:22 +080026#include "unicast-udp-transport.hpp"
Junxiao Shi13546112015-10-14 19:33:07 -070027#include "udp-protocol.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010028
Davide Pesavento0e76f622014-06-25 09:58:20 +020029#ifdef __linux__
Davide Pesavento66ff0982015-01-29 22:39:00 +010030#include <cerrno> // for errno
31#include <cstring> // for std::strerror()
Davide Pesavento0e76f622014-06-25 09:58:20 +020032#include <netinet/in.h> // for IP_MTU_DISCOVER and IP_PMTUDISC_DONT
33#include <sys/socket.h> // for setsockopt()
34#endif
35
Giulio Grassi624f6c62014-02-18 19:42:14 +010036namespace nfd {
Yukai Tu0a49d342015-09-13 12:54:22 +080037namespace face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010038
Davide Pesaventoa3148082018-04-12 18:21:54 -040039NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport<boost::asio::ip::udp, Unicast>), UnicastUdpTransport);
Giulio Grassi624f6c62014-02-18 19:42:14 +010040
Yukai Tu0a49d342015-09-13 12:54:22 +080041UnicastUdpTransport::UnicastUdpTransport(protocol::socket&& socket,
42 ndn::nfd::FacePersistency persistency,
Davide Pesavento8728a252015-11-06 04:01:22 +010043 time::nanoseconds idleTimeout)
Yukai Tu0a49d342015-09-13 12:54:22 +080044 : DatagramTransport(std::move(socket))
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070045 , m_idleTimeout(idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010046{
Yukai Tu0a49d342015-09-13 12:54:22 +080047 this->setLocalUri(FaceUri(m_socket.local_endpoint()));
48 this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
Junxiao Shi13546112015-10-14 19:33:07 -070049 this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
Yukai Tu375dcb02015-08-01 13:04:43 +080050 this->setPersistency(persistency);
Yukai Tu0a49d342015-09-13 12:54:22 +080051 this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Junxiao Shi13546112015-10-14 19:33:07 -070052 this->setMtu(udp::computeMtu(m_socket.local_endpoint()));
Yukai Tu0a49d342015-09-13 12:54:22 +080053
54 NFD_LOG_FACE_INFO("Creating transport");
Davide Pesavento94279412015-02-27 01:29:32 +010055
Davide Pesavento0e76f622014-06-25 09:58:20 +020056#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;
Davide Pesavento292e5e12015-03-13 02:08:33 +010070 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 }
Davide Pesavento0e76f622014-06-25 09:58:20 +020074#endif
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070075
Yukai Tu0a49d342015-09-13 12:54:22 +080076 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
Davide Pesavento8728a252015-11-06 04:01:22 +010077 m_idleTimeout > time::nanoseconds::zero()) {
78 scheduleClosureWhenIdle();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070079 }
80}
81
Yanbiao Li32dab972016-11-27 12:26:09 +080082bool
83UnicastUdpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070084{
Yanbiao Li32dab972016-11-27 12:26:09 +080085 return true;
86}
87
88void
89UnicastUdpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
90{
91 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
Davide Pesavento8728a252015-11-06 04:01:22 +010092 m_idleTimeout > time::nanoseconds::zero()) {
93 scheduleClosureWhenIdle();
94 }
95 else {
96 m_closeIfIdleEvent.cancel();
Eric Newberryc64d30a2015-12-26 11:07:27 -070097 setExpirationTime(time::steady_clock::TimePoint::max());
Davide Pesavento8728a252015-11-06 04:01:22 +010098 }
99}
100
101void
102UnicastUdpTransport::scheduleClosureWhenIdle()
103{
104 m_closeIfIdleEvent = scheduler::schedule(m_idleTimeout, [this] {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400105 if (!hasRecentlyReceived()) {
Yukai Tu0a49d342015-09-13 12:54:22 +0800106 NFD_LOG_FACE_INFO("Closing due to inactivity");
107 this->close();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700108 }
109 else {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400110 resetRecentlyReceived();
Davide Pesavento8728a252015-11-06 04:01:22 +0100111 scheduleClosureWhenIdle();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700112 }
Davide Pesavento8728a252015-11-06 04:01:22 +0100113 });
Eric Newberryc64d30a2015-12-26 11:07:27 -0700114 setExpirationTime(time::steady_clock::now() + m_idleTimeout);
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700115}
116
Yukai Tu0a49d342015-09-13 12:54:22 +0800117} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100118} // namespace nfd