blob: c8a208e2d0fd07bf79d38c248077328d4834e6e1 [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/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/global.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010029
Davide Pesavento0e76f622014-06-25 09:58:20 +020030#ifdef __linux__
Davide Pesavento66ff0982015-01-29 22:39:00 +010031#include <cerrno> // for errno
32#include <cstring> // for std::strerror()
Davide Pesavento0e76f622014-06-25 09:58:20 +020033#include <netinet/in.h> // for IP_MTU_DISCOVER and IP_PMTUDISC_DONT
34#include <sys/socket.h> // for setsockopt()
35#endif
36
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::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,
Eric Newberrycb6551e2020-03-02 14:12:16 -080043 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);
Eric Newberrycb6551e2020-03-02 14:12:16 -080052 this->setMtu(udp::computeMtu(m_socket.local_endpoint()));
Yukai Tu0a49d342015-09-13 12:54:22 +080053
Davide Pesaventoa681a242019-03-29 23:48:27 -040054 NFD_LOG_FACE_DEBUG("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();
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040097 setExpirationTime(time::steady_clock::time_point::max());
Davide Pesavento8728a252015-11-06 04:01:22 +010098 }
99}
100
101void
102UnicastUdpTransport::scheduleClosureWhenIdle()
103{
Davide Pesavento3dade002019-03-19 11:29:56 -0600104 m_closeIfIdleEvent = getScheduler().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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400117} // namespace nfd::face