blob: 074e53a693f7abc0a1917aea978ce3d72064b7a2 [file] [log] [blame]
Yukai Tu16aabbc2015-10-06 05:08:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberryb49313d2017-12-24 20:22:27 -07002/*
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Yukai Tu16aabbc2015-10-06 05:08:42 -07004 * 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.
10 *
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/>.
24 */
25
26#include "tcp-transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/global.hpp"
Yukai Tu16aabbc2015-10-06 05:08:42 -070028
Davide Pesavento5d642632023-10-03 00:36:08 -040029#include <boost/asio/defer.hpp>
30
Eric Newberryb49313d2017-12-24 20:22:27 -070031#if defined(__linux__)
32#include <linux/sockios.h>
33#include <sys/ioctl.h>
34#endif
35
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::face {
Yukai Tu16aabbc2015-10-06 05:08:42 -070037
Davide Pesaventoc0df94e2023-10-08 19:26:55 -040038namespace ip = boost::asio::ip;
Weiwei Liudcdf6212016-08-31 14:34:22 -070039
Davide Pesaventoc0df94e2023-10-08 19:26:55 -040040NFD_LOG_MEMBER_INIT_SPECIALIZED(StreamTransport<ip::tcp>, TcpTransport);
41
42TcpTransport::TcpTransport(ip::tcp::socket&& socket,
Davide Pesavento3dade002019-03-19 11:29:56 -060043 ndn::nfd::FacePersistency persistency,
44 ndn::nfd::FaceScope faceScope)
Yukai Tu16aabbc2015-10-06 05:08:42 -070045 : StreamTransport(std::move(socket))
Weiwei Liudcdf6212016-08-31 14:34:22 -070046 , m_remoteEndpoint(m_socket.remote_endpoint())
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040047 , m_nextReconnectWait(INITIAL_RECONNECT_DELAY)
Yukai Tu16aabbc2015-10-06 05:08:42 -070048{
49 this->setLocalUri(FaceUri(m_socket.local_endpoint()));
50 this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
Alexander Afanasyevded17422018-04-03 19:00:23 -040051 this->setScope(faceScope);
Yukai Tu16aabbc2015-10-06 05:08:42 -070052 this->setPersistency(persistency);
53 this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
54 this->setMtu(MTU_UNLIMITED);
55
Davide Pesaventoa681a242019-03-29 23:48:27 -040056 NFD_LOG_FACE_DEBUG("Creating transport");
Yukai Tu16aabbc2015-10-06 05:08:42 -070057}
58
Eric Newberryb49313d2017-12-24 20:22:27 -070059ssize_t
60TcpTransport::getSendQueueLength()
61{
62 int queueLength = getSendQueueBytes();
63
64 // We want to obtain the amount of "not sent" bytes instead of the amount of "not sent" + "not
65 // acked" bytes. On Linux, we use SIOCOUTQNSD for this reason. However, macOS does not provide an
66 // efficient mechanism to obtain this value (SO_NWRITE includes both "not sent" and "not acked").
67#if defined(__linux__)
68 int nsd;
69 if (ioctl(m_socket.native_handle(), SIOCOUTQNSD, &nsd) < 0) {
70 NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno));
71 }
72 else if (nsd > 0) {
73 NFD_LOG_FACE_TRACE("SIOCOUTQNSD=" << nsd);
74 queueLength += nsd;
75 }
76#endif
77
78 return queueLength;
79}
80
Yanbiao Li32dab972016-11-27 12:26:09 +080081bool
82TcpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
Yukai Tu16aabbc2015-10-06 05:08:42 -070083{
Yanbiao Li32dab972016-11-27 12:26:09 +080084 return true;
85}
86
87void
88TcpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
89{
90 // if persistency was changed from permanent to any other value
91 if (oldPersistency == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
Weiwei Liudcdf6212016-08-31 14:34:22 -070092 if (this->getState() == TransportState::DOWN) {
93 // non-permanent transport cannot be in DOWN state, so fail hard
94 this->setState(TransportState::FAILED);
95 doClose();
96 }
Yukai Tu16aabbc2015-10-06 05:08:42 -070097 }
98}
99
Weiwei Liudcdf6212016-08-31 14:34:22 -0700100void
101TcpTransport::handleError(const boost::system::error_code& error)
102{
103 if (this->getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
104 NFD_LOG_FACE_TRACE("TCP socket error: " << error.message());
105 this->setState(TransportState::DOWN);
106
107 // cancel all outstanding operations
Davide Pesavento3dade002019-03-19 11:29:56 -0600108 boost::system::error_code ec;
109 m_socket.cancel(ec);
Weiwei Liudcdf6212016-08-31 14:34:22 -0700110
111 // do this asynchronously because there could be some callbacks still pending
Davide Pesavento5d642632023-10-03 00:36:08 -0400112 boost::asio::defer(getGlobalIoService(), [this] { reconnect(); });
Weiwei Liudcdf6212016-08-31 14:34:22 -0700113 }
114 else {
115 StreamTransport::handleError(error);
116 }
117}
118
119void
120TcpTransport::reconnect()
121{
122 NFD_LOG_FACE_TRACE(__func__);
123
124 if (getState() == TransportState::CLOSING ||
125 getState() == TransportState::FAILED ||
126 getState() == TransportState::CLOSED) {
127 // transport is shutting down, don't attempt to reconnect
128 return;
129 }
130
131 BOOST_ASSERT(getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT);
132 BOOST_ASSERT(getState() == TransportState::DOWN);
133
134 // recreate the socket
Davide Pesaventoc0df94e2023-10-08 19:26:55 -0400135 m_socket = ip::tcp::socket(m_socket.get_executor());
Weiwei Liudcdf6212016-08-31 14:34:22 -0700136 this->resetReceiveBuffer();
137 this->resetSendQueue();
138
Davide Pesavento3dade002019-03-19 11:29:56 -0600139 m_reconnectEvent = getScheduler().schedule(m_nextReconnectWait,
140 [this] { this->handleReconnectTimeout(); });
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400141 m_socket.async_connect(m_remoteEndpoint, [this] (const auto& e) { this->handleReconnect(e); });
Weiwei Liudcdf6212016-08-31 14:34:22 -0700142}
143
144void
145TcpTransport::handleReconnect(const boost::system::error_code& error)
146{
147 if (getState() == TransportState::CLOSING ||
148 getState() == TransportState::FAILED ||
149 getState() == TransportState::CLOSED ||
150 error == boost::asio::error::operation_aborted) {
151 // transport is shutting down, abort the reconnection attempt and ignore any errors
152 return;
153 }
154
155 if (error) {
156 NFD_LOG_FACE_TRACE("Reconnection attempt failed: " << error.message());
157 return;
158 }
159
160 m_reconnectEvent.cancel();
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400161 m_nextReconnectWait = INITIAL_RECONNECT_DELAY;
Weiwei Liudcdf6212016-08-31 14:34:22 -0700162
163 this->setLocalUri(FaceUri(m_socket.local_endpoint()));
164 NFD_LOG_FACE_TRACE("TCP connection reestablished");
165 this->setState(TransportState::UP);
166 this->startReceive();
167}
168
169void
170TcpTransport::handleReconnectTimeout()
171{
172 // abort the reconnection attempt
173 boost::system::error_code error;
174 m_socket.close(error);
175
176 // exponentially back off the reconnection timer
177 m_nextReconnectWait =
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400178 std::min(time::duration_cast<time::milliseconds>(m_nextReconnectWait * RECONNECT_DELAY_MULTIPLIER),
179 MAX_RECONNECT_DELAY);
Weiwei Liudcdf6212016-08-31 14:34:22 -0700180
181 // do this asynchronously because there could be some callbacks still pending
Davide Pesavento5d642632023-10-03 00:36:08 -0400182 boost::asio::defer(getGlobalIoService(), [this] { reconnect(); });
Weiwei Liudcdf6212016-08-31 14:34:22 -0700183}
184
185void
186TcpTransport::doClose()
187{
188 m_reconnectEvent.cancel();
189 StreamTransport::doClose();
190}
191
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400192} // namespace nfd::face