blob: eb8ab66bc280495a8eea04b68f98a213eed1dbd8 [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"
Giulio Grassi624f6c62014-02-18 19:42:14 +010027
Davide Pesavento0e76f622014-06-25 09:58:20 +020028#ifdef __linux__
Davide Pesavento66ff0982015-01-29 22:39:00 +010029#include <cerrno> // for errno
30#include <cstring> // for std::strerror()
Davide Pesavento0e76f622014-06-25 09:58:20 +020031#include <netinet/in.h> // for IP_MTU_DISCOVER and IP_PMTUDISC_DONT
32#include <sys/socket.h> // for setsockopt()
33#endif
34
Giulio Grassi624f6c62014-02-18 19:42:14 +010035namespace nfd {
Yukai Tu0a49d342015-09-13 12:54:22 +080036namespace face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010037
Yukai Tu0a49d342015-09-13 12:54:22 +080038NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(DatagramTransport, UnicastUdpTransport::protocol,
39 "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,
43 const time::seconds& idleTimeout)
44 : DatagramTransport(std::move(socket))
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070045 , m_idleTimeout(idleTimeout)
46 , m_lastIdleCheck(time::steady_clock::now())
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()));
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);
52
53 NFD_LOG_FACE_INFO("Creating transport");
Davide Pesavento94279412015-02-27 01:29:32 +010054
Davide Pesavento0e76f622014-06-25 09:58:20 +020055#ifdef __linux__
56 //
57 // By default, Linux does path MTU discovery on IPv4 sockets,
58 // and sets the DF (Don't Fragment) flag on datagrams smaller
59 // than the interface MTU. However this does not work for us,
60 // because we cannot properly respond to ICMP "packet too big"
61 // messages by fragmenting the packet at the application level,
62 // since we want to rely on IP for fragmentation and reassembly.
63 //
64 // Therefore, we disable PMTU discovery, which prevents the kernel
65 // from setting the DF flag on outgoing datagrams, and thus allows
66 // routers along the path to perform fragmentation as needed.
67 //
68 const int value = IP_PMTUDISC_DONT;
Davide Pesavento292e5e12015-03-13 02:08:33 +010069 if (::setsockopt(m_socket.native_handle(), IPPROTO_IP,
70 IP_MTU_DISCOVER, &value, sizeof(value)) < 0) {
71 NFD_LOG_FACE_WARN("Failed to disable path MTU discovery: " << std::strerror(errno));
72 }
Davide Pesavento0e76f622014-06-25 09:58:20 +020073#endif
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070074
Yukai Tu0a49d342015-09-13 12:54:22 +080075 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
76 m_idleTimeout > time::seconds::zero()) {
77 m_closeIfIdleEvent = scheduler::schedule(m_idleTimeout, bind(&UnicastUdpTransport::closeIfIdle, this));
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070078 }
79}
80
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070081void
Yukai Tu0a49d342015-09-13 12:54:22 +080082UnicastUdpTransport::closeIfIdle()
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070083{
Yukai Tu0a49d342015-09-13 12:54:22 +080084 // transport can be switched from on-demand to non-on-demand mode
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070085 // (non-on-demand -> on-demand transition is not allowed)
Yukai Tu0a49d342015-09-13 12:54:22 +080086 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070087 if (!hasBeenUsedRecently()) {
Yukai Tu0a49d342015-09-13 12:54:22 +080088 NFD_LOG_FACE_INFO("Closing due to inactivity");
89 this->close();
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070090 }
91 else {
92 resetRecentUsage();
93
94 m_lastIdleCheck = time::steady_clock::now();
Yukai Tu0a49d342015-09-13 12:54:22 +080095 m_closeIfIdleEvent = scheduler::schedule(m_idleTimeout, bind(&UnicastUdpTransport::closeIfIdle, this));
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070096 }
97 }
98 // else do nothing and do not reschedule the event
99}
100
Yukai Tu0a49d342015-09-13 12:54:22 +0800101} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100102} // namespace nfd