blob: 15c076997648df4192c00dcf3277fa2b45e76407 [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 Pesaventoa3148082018-04-12 18:21:54 -040038NFD_LOG_MEMBER_INIT_SPECIALIZED(StreamTransport<boost::asio::ip::tcp>, TcpTransport);
Weiwei Liudcdf6212016-08-31 14:34:22 -070039
Davide Pesavento3dade002019-03-19 11:29:56 -060040TcpTransport::TcpTransport(protocol::socket&& socket,
41 ndn::nfd::FacePersistency persistency,
42 ndn::nfd::FaceScope faceScope)
Yukai Tu16aabbc2015-10-06 05:08:42 -070043 : StreamTransport(std::move(socket))
Weiwei Liudcdf6212016-08-31 14:34:22 -070044 , m_remoteEndpoint(m_socket.remote_endpoint())
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040045 , m_nextReconnectWait(INITIAL_RECONNECT_DELAY)
Yukai Tu16aabbc2015-10-06 05:08:42 -070046{
47 this->setLocalUri(FaceUri(m_socket.local_endpoint()));
48 this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
Alexander Afanasyevded17422018-04-03 19:00:23 -040049 this->setScope(faceScope);
Yukai Tu16aabbc2015-10-06 05:08:42 -070050 this->setPersistency(persistency);
51 this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
52 this->setMtu(MTU_UNLIMITED);
53
Davide Pesaventoa681a242019-03-29 23:48:27 -040054 NFD_LOG_FACE_DEBUG("Creating transport");
Yukai Tu16aabbc2015-10-06 05:08:42 -070055}
56
Eric Newberryb49313d2017-12-24 20:22:27 -070057ssize_t
58TcpTransport::getSendQueueLength()
59{
60 int queueLength = getSendQueueBytes();
61
62 // We want to obtain the amount of "not sent" bytes instead of the amount of "not sent" + "not
63 // acked" bytes. On Linux, we use SIOCOUTQNSD for this reason. However, macOS does not provide an
64 // efficient mechanism to obtain this value (SO_NWRITE includes both "not sent" and "not acked").
65#if defined(__linux__)
66 int nsd;
67 if (ioctl(m_socket.native_handle(), SIOCOUTQNSD, &nsd) < 0) {
68 NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno));
69 }
70 else if (nsd > 0) {
71 NFD_LOG_FACE_TRACE("SIOCOUTQNSD=" << nsd);
72 queueLength += nsd;
73 }
74#endif
75
76 return queueLength;
77}
78
Yanbiao Li32dab972016-11-27 12:26:09 +080079bool
80TcpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
Yukai Tu16aabbc2015-10-06 05:08:42 -070081{
Yanbiao Li32dab972016-11-27 12:26:09 +080082 return true;
83}
84
85void
86TcpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
87{
88 // if persistency was changed from permanent to any other value
89 if (oldPersistency == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
Weiwei Liudcdf6212016-08-31 14:34:22 -070090 if (this->getState() == TransportState::DOWN) {
91 // non-permanent transport cannot be in DOWN state, so fail hard
92 this->setState(TransportState::FAILED);
93 doClose();
94 }
Yukai Tu16aabbc2015-10-06 05:08:42 -070095 }
96}
97
Weiwei Liudcdf6212016-08-31 14:34:22 -070098void
99TcpTransport::handleError(const boost::system::error_code& error)
100{
101 if (this->getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
102 NFD_LOG_FACE_TRACE("TCP socket error: " << error.message());
103 this->setState(TransportState::DOWN);
104
105 // cancel all outstanding operations
Davide Pesavento3dade002019-03-19 11:29:56 -0600106 boost::system::error_code ec;
107 m_socket.cancel(ec);
Weiwei Liudcdf6212016-08-31 14:34:22 -0700108
109 // do this asynchronously because there could be some callbacks still pending
Davide Pesavento5d642632023-10-03 00:36:08 -0400110 boost::asio::defer(getGlobalIoService(), [this] { reconnect(); });
Weiwei Liudcdf6212016-08-31 14:34:22 -0700111 }
112 else {
113 StreamTransport::handleError(error);
114 }
115}
116
117void
118TcpTransport::reconnect()
119{
120 NFD_LOG_FACE_TRACE(__func__);
121
122 if (getState() == TransportState::CLOSING ||
123 getState() == TransportState::FAILED ||
124 getState() == TransportState::CLOSED) {
125 // transport is shutting down, don't attempt to reconnect
126 return;
127 }
128
129 BOOST_ASSERT(getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT);
130 BOOST_ASSERT(getState() == TransportState::DOWN);
131
132 // recreate the socket
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400133 m_socket = protocol::socket(m_socket.get_executor());
Weiwei Liudcdf6212016-08-31 14:34:22 -0700134 this->resetReceiveBuffer();
135 this->resetSendQueue();
136
Davide Pesavento3dade002019-03-19 11:29:56 -0600137 m_reconnectEvent = getScheduler().schedule(m_nextReconnectWait,
138 [this] { this->handleReconnectTimeout(); });
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400139 m_socket.async_connect(m_remoteEndpoint, [this] (const auto& e) { this->handleReconnect(e); });
Weiwei Liudcdf6212016-08-31 14:34:22 -0700140}
141
142void
143TcpTransport::handleReconnect(const boost::system::error_code& error)
144{
145 if (getState() == TransportState::CLOSING ||
146 getState() == TransportState::FAILED ||
147 getState() == TransportState::CLOSED ||
148 error == boost::asio::error::operation_aborted) {
149 // transport is shutting down, abort the reconnection attempt and ignore any errors
150 return;
151 }
152
153 if (error) {
154 NFD_LOG_FACE_TRACE("Reconnection attempt failed: " << error.message());
155 return;
156 }
157
158 m_reconnectEvent.cancel();
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400159 m_nextReconnectWait = INITIAL_RECONNECT_DELAY;
Weiwei Liudcdf6212016-08-31 14:34:22 -0700160
161 this->setLocalUri(FaceUri(m_socket.local_endpoint()));
162 NFD_LOG_FACE_TRACE("TCP connection reestablished");
163 this->setState(TransportState::UP);
164 this->startReceive();
165}
166
167void
168TcpTransport::handleReconnectTimeout()
169{
170 // abort the reconnection attempt
171 boost::system::error_code error;
172 m_socket.close(error);
173
174 // exponentially back off the reconnection timer
175 m_nextReconnectWait =
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400176 std::min(time::duration_cast<time::milliseconds>(m_nextReconnectWait * RECONNECT_DELAY_MULTIPLIER),
177 MAX_RECONNECT_DELAY);
Weiwei Liudcdf6212016-08-31 14:34:22 -0700178
179 // do this asynchronously because there could be some callbacks still pending
Davide Pesavento5d642632023-10-03 00:36:08 -0400180 boost::asio::defer(getGlobalIoService(), [this] { reconnect(); });
Weiwei Liudcdf6212016-08-31 14:34:22 -0700181}
182
183void
184TcpTransport::doClose()
185{
186 m_reconnectEvent.cancel();
187 StreamTransport::doClose();
188}
189
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400190} // namespace nfd::face