blob: 4f8a8f773000e848990f6f6563694fd385fbfc2b [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Li32dab972016-11-27 12:26:09 +08003 * Copyright (c) 2014-2017, 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
Yukai Tu0a49d342015-09-13 12:54:22 +080039NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(DatagramTransport, UnicastUdpTransport::protocol,
40 "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,
Davide Pesavento8728a252015-11-06 04:01:22 +010044 time::nanoseconds idleTimeout)
Yukai Tu0a49d342015-09-13 12:54:22 +080045 : DatagramTransport(std::move(socket))
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070046 , m_idleTimeout(idleTimeout)
Giulio Grassi624f6c62014-02-18 19:42:14 +010047{
Yukai Tu0a49d342015-09-13 12:54:22 +080048 this->setLocalUri(FaceUri(m_socket.local_endpoint()));
49 this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
Junxiao Shi13546112015-10-14 19:33:07 -070050 this->setScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
Yukai Tu375dcb02015-08-01 13:04:43 +080051 this->setPersistency(persistency);
Yukai Tu0a49d342015-09-13 12:54:22 +080052 this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Junxiao Shi13546112015-10-14 19:33:07 -070053 this->setMtu(udp::computeMtu(m_socket.local_endpoint()));
Yukai Tu0a49d342015-09-13 12:54:22 +080054
55 NFD_LOG_FACE_INFO("Creating transport");
Davide Pesavento94279412015-02-27 01:29:32 +010056
Davide Pesavento0e76f622014-06-25 09:58:20 +020057#ifdef __linux__
58 //
59 // By default, Linux does path MTU discovery on IPv4 sockets,
60 // and sets the DF (Don't Fragment) flag on datagrams smaller
61 // than the interface MTU. However this does not work for us,
62 // because we cannot properly respond to ICMP "packet too big"
63 // messages by fragmenting the packet at the application level,
64 // since we want to rely on IP for fragmentation and reassembly.
65 //
66 // Therefore, we disable PMTU discovery, which prevents the kernel
67 // from setting the DF flag on outgoing datagrams, and thus allows
68 // routers along the path to perform fragmentation as needed.
69 //
70 const int value = IP_PMTUDISC_DONT;
Davide Pesavento292e5e12015-03-13 02:08:33 +010071 if (::setsockopt(m_socket.native_handle(), IPPROTO_IP,
72 IP_MTU_DISCOVER, &value, sizeof(value)) < 0) {
73 NFD_LOG_FACE_WARN("Failed to disable path MTU discovery: " << std::strerror(errno));
74 }
Davide Pesavento0e76f622014-06-25 09:58:20 +020075#endif
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070076
Yukai Tu0a49d342015-09-13 12:54:22 +080077 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
Davide Pesavento8728a252015-11-06 04:01:22 +010078 m_idleTimeout > time::nanoseconds::zero()) {
79 scheduleClosureWhenIdle();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070080 }
81}
82
Yanbiao Li32dab972016-11-27 12:26:09 +080083bool
84UnicastUdpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070085{
Yanbiao Li32dab972016-11-27 12:26:09 +080086 return true;
87}
88
89void
90UnicastUdpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
91{
92 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
Davide Pesavento8728a252015-11-06 04:01:22 +010093 m_idleTimeout > time::nanoseconds::zero()) {
94 scheduleClosureWhenIdle();
95 }
96 else {
97 m_closeIfIdleEvent.cancel();
Eric Newberryc64d30a2015-12-26 11:07:27 -070098 setExpirationTime(time::steady_clock::TimePoint::max());
Davide Pesavento8728a252015-11-06 04:01:22 +010099 }
100}
101
102void
103UnicastUdpTransport::scheduleClosureWhenIdle()
104{
105 m_closeIfIdleEvent = scheduler::schedule(m_idleTimeout, [this] {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400106 if (!hasRecentlyReceived()) {
Yukai Tu0a49d342015-09-13 12:54:22 +0800107 NFD_LOG_FACE_INFO("Closing due to inactivity");
108 this->close();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700109 }
110 else {
Davide Pesavento0b0a71d2017-07-02 13:57:36 -0400111 resetRecentlyReceived();
Davide Pesavento8728a252015-11-06 04:01:22 +0100112 scheduleClosureWhenIdle();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700113 }
Davide Pesavento8728a252015-11-06 04:01:22 +0100114 });
Eric Newberryc64d30a2015-12-26 11:07:27 -0700115 setExpirationTime(time::steady_clock::now() + m_idleTimeout);
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700116}
117
Yukai Tu0a49d342015-09-13 12:54:22 +0800118} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100119} // namespace nfd