blob: a5fb3e71f19ed017255565d9da4c6aa9e185f05f [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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,
44 const time::seconds& idleTimeout)
45 : DatagramTransport(std::move(socket))
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070046 , m_idleTimeout(idleTimeout)
47 , m_lastIdleCheck(time::steady_clock::now())
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);
Junxiao Shi13546112015-10-14 19:33:07 -070054 this->setMtu(udp::computeMtu(m_socket.local_endpoint()));
Yukai Tu0a49d342015-09-13 12:54:22 +080055
56 NFD_LOG_FACE_INFO("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 &&
79 m_idleTimeout > time::seconds::zero()) {
80 m_closeIfIdleEvent = scheduler::schedule(m_idleTimeout, bind(&UnicastUdpTransport::closeIfIdle, this));
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070081 }
82}
83
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070084void
Yukai Tu0a49d342015-09-13 12:54:22 +080085UnicastUdpTransport::closeIfIdle()
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070086{
Yukai Tu0a49d342015-09-13 12:54:22 +080087 // transport can be switched from on-demand to non-on-demand mode
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070088 // (non-on-demand -> on-demand transition is not allowed)
Yukai Tu0a49d342015-09-13 12:54:22 +080089 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070090 if (!hasBeenUsedRecently()) {
Yukai Tu0a49d342015-09-13 12:54:22 +080091 NFD_LOG_FACE_INFO("Closing due to inactivity");
92 this->close();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070093 }
94 else {
95 resetRecentUsage();
96
97 m_lastIdleCheck = time::steady_clock::now();
Yukai Tu0a49d342015-09-13 12:54:22 +080098 m_closeIfIdleEvent = scheduler::schedule(m_idleTimeout, bind(&UnicastUdpTransport::closeIfIdle, this));
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070099 }
100 }
101 // else do nothing and do not reschedule the event
102}
103
Yukai Tu0a49d342015-09-13 12:54:22 +0800104} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100105} // namespace nfd