blob: f6614f8190f2abb39c7a786532040a1614def69e [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 Pesaventoc0df94e2023-10-08 19:26:55 -04003 * Copyright (c) 2014-2023, 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 Pesaventoc0df94e2023-10-08 19:26:55 -040039namespace ip = boost::asio::ip;
Giulio Grassi624f6c62014-02-18 19:42:14 +010040
Davide Pesaventoc0df94e2023-10-08 19:26:55 -040041NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport<ip::udp, Unicast>), UnicastUdpTransport);
42
43UnicastUdpTransport::UnicastUdpTransport(ip::udp::socket&& socket,
Yukai Tu0a49d342015-09-13 12:54:22 +080044 ndn::nfd::FacePersistency persistency,
Eric Newberrycb6551e2020-03-02 14:12:16 -080045 time::nanoseconds idleTimeout)
Yukai Tu0a49d342015-09-13 12:54:22 +080046 : DatagramTransport(std::move(socket))
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070047 , m_idleTimeout(idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010048{
Yukai Tu0a49d342015-09-13 12:54:22 +080049 this->setLocalUri(FaceUri(m_socket.local_endpoint()));
50 this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
Junxiao Shi13546112015-10-14 19:33:07 -070051 this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
Yukai Tu375dcb02015-08-01 13:04:43 +080052 this->setPersistency(persistency);
Yukai Tu0a49d342015-09-13 12:54:22 +080053 this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Eric Newberrycb6551e2020-03-02 14:12:16 -080054 this->setMtu(udp::computeMtu(m_socket.local_endpoint()));
Yukai Tu0a49d342015-09-13 12:54:22 +080055
Davide Pesaventoa681a242019-03-29 23:48:27 -040056 NFD_LOG_FACE_DEBUG("Creating transport");
Davide Pesavento94279412015-02-27 01:29:32 +010057
Davide Pesavento0e76f622014-06-25 09:58:20 +020058#ifdef __linux__
59 //
60 // By default, Linux does path MTU discovery on IPv4 sockets,
61 // and sets the DF (Don't Fragment) flag on datagrams smaller
62 // than the interface MTU. However this does not work for us,
63 // because we cannot properly respond to ICMP "packet too big"
64 // messages by fragmenting the packet at the application level,
65 // since we want to rely on IP for fragmentation and reassembly.
66 //
67 // Therefore, we disable PMTU discovery, which prevents the kernel
68 // from setting the DF flag on outgoing datagrams, and thus allows
69 // routers along the path to perform fragmentation as needed.
70 //
71 const int value = IP_PMTUDISC_DONT;
Davide Pesavento292e5e12015-03-13 02:08:33 +010072 if (::setsockopt(m_socket.native_handle(), IPPROTO_IP,
73 IP_MTU_DISCOVER, &value, sizeof(value)) < 0) {
74 NFD_LOG_FACE_WARN("Failed to disable path MTU discovery: " << std::strerror(errno));
75 }
Davide Pesavento0e76f622014-06-25 09:58:20 +020076#endif
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070077
Yukai Tu0a49d342015-09-13 12:54:22 +080078 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
Davide Pesavento8728a252015-11-06 04:01:22 +010079 m_idleTimeout > time::nanoseconds::zero()) {
80 scheduleClosureWhenIdle();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070081 }
82}
83
Yanbiao Li32dab972016-11-27 12:26:09 +080084bool
85UnicastUdpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070086{
Yanbiao Li32dab972016-11-27 12:26:09 +080087 return true;
88}
89
90void
91UnicastUdpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
92{
93 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
Davide Pesavento8728a252015-11-06 04:01:22 +010094 m_idleTimeout > time::nanoseconds::zero()) {
95 scheduleClosureWhenIdle();
96 }
97 else {
98 m_closeIfIdleEvent.cancel();
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040099 setExpirationTime(time::steady_clock::time_point::max());
Davide Pesavento8728a252015-11-06 04:01:22 +0100100 }
101}
102
103void
104UnicastUdpTransport::scheduleClosureWhenIdle()
105{
Davide Pesavento3dade002019-03-19 11:29:56 -0600106 m_closeIfIdleEvent = getScheduler().schedule(m_idleTimeout, [this] {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400107 if (!hasRecentlyReceived()) {
Yukai Tu0a49d342015-09-13 12:54:22 +0800108 NFD_LOG_FACE_INFO("Closing due to inactivity");
109 this->close();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700110 }
111 else {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400112 resetRecentlyReceived();
Davide Pesavento8728a252015-11-06 04:01:22 +0100113 scheduleClosureWhenIdle();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700114 }
Davide Pesavento8728a252015-11-06 04:01:22 +0100115 });
Eric Newberryc64d30a2015-12-26 11:07:27 -0700116 setExpirationTime(time::steady_clock::now() + m_idleTimeout);
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700117}
118
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400119} // namespace nfd::face