blob: a81e1d0143e3a9a96d5718076f70265316ffda6b [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 Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, 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
Giulio Grassi624f6c62014-02-18 19:42:14 +010037namespace nfd {
Yukai Tu0a49d342015-09-13 12:54:22 +080038namespace face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010039
Davide Pesaventoa3148082018-04-12 18:21:54 -040040NFD_LOG_MEMBER_INIT_SPECIALIZED((DatagramTransport<boost::asio::ip::udp, Unicast>), UnicastUdpTransport);
Giulio Grassi624f6c62014-02-18 19:42:14 +010041
Yukai Tu0a49d342015-09-13 12:54:22 +080042UnicastUdpTransport::UnicastUdpTransport(protocol::socket&& socket,
43 ndn::nfd::FacePersistency persistency,
Eric Newberry812d6152018-06-06 15:06:01 -070044 time::nanoseconds idleTimeout,
45 optional<ssize_t> overrideMtu)
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 Newberry812d6152018-06-06 15:06:01 -070054
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);
Yukai Tu0a49d342015-09-13 12:54:22 +080062
Davide Pesaventoa681a242019-03-29 23:48:27 -040063 NFD_LOG_FACE_DEBUG("Creating transport");
Davide Pesavento94279412015-02-27 01:29:32 +010064
Davide Pesavento0e76f622014-06-25 09:58:20 +020065#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;
Davide Pesavento292e5e12015-03-13 02:08:33 +010079 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 }
Davide Pesavento0e76f622014-06-25 09:58:20 +020083#endif
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070084
Yukai Tu0a49d342015-09-13 12:54:22 +080085 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
Davide Pesavento8728a252015-11-06 04:01:22 +010086 m_idleTimeout > time::nanoseconds::zero()) {
87 scheduleClosureWhenIdle();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070088 }
89}
90
Yanbiao Li32dab972016-11-27 12:26:09 +080091bool
92UnicastUdpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070093{
Yanbiao Li32dab972016-11-27 12:26:09 +080094 return true;
95}
96
97void
98UnicastUdpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
99{
100 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
Davide Pesavento8728a252015-11-06 04:01:22 +0100101 m_idleTimeout > time::nanoseconds::zero()) {
102 scheduleClosureWhenIdle();
103 }
104 else {
105 m_closeIfIdleEvent.cancel();
Eric Newberryc64d30a2015-12-26 11:07:27 -0700106 setExpirationTime(time::steady_clock::TimePoint::max());
Davide Pesavento8728a252015-11-06 04:01:22 +0100107 }
108}
109
110void
111UnicastUdpTransport::scheduleClosureWhenIdle()
112{
Davide Pesavento3dade002019-03-19 11:29:56 -0600113 m_closeIfIdleEvent = getScheduler().schedule(m_idleTimeout, [this] {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400114 if (!hasRecentlyReceived()) {
Yukai Tu0a49d342015-09-13 12:54:22 +0800115 NFD_LOG_FACE_INFO("Closing due to inactivity");
116 this->close();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700117 }
118 else {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400119 resetRecentlyReceived();
Davide Pesavento8728a252015-11-06 04:01:22 +0100120 scheduleClosureWhenIdle();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700121 }
Davide Pesavento8728a252015-11-06 04:01:22 +0100122 });
Eric Newberryc64d30a2015-12-26 11:07:27 -0700123 setExpirationTime(time::steady_clock::now() + m_idleTimeout);
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700124}
125
Yukai Tu0a49d342015-09-13 12:54:22 +0800126} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100127} // namespace nfd