blob: 0332c2f8ded43bf001b0a20b971585537ccb59e7 [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 Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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
Eric Newberryb49313d2017-12-24 20:22:27 -070029#if defined(__linux__)
30#include <linux/sockios.h>
31#include <sys/ioctl.h>
32#endif
33
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::face {
Yukai Tu16aabbc2015-10-06 05:08:42 -070035
Davide Pesaventoa3148082018-04-12 18:21:54 -040036NFD_LOG_MEMBER_INIT_SPECIALIZED(StreamTransport<boost::asio::ip::tcp>, TcpTransport);
Weiwei Liudcdf6212016-08-31 14:34:22 -070037
Davide Pesavento3dade002019-03-19 11:29:56 -060038TcpTransport::TcpTransport(protocol::socket&& socket,
39 ndn::nfd::FacePersistency persistency,
40 ndn::nfd::FaceScope faceScope)
Yukai Tu16aabbc2015-10-06 05:08:42 -070041 : StreamTransport(std::move(socket))
Weiwei Liudcdf6212016-08-31 14:34:22 -070042 , m_remoteEndpoint(m_socket.remote_endpoint())
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040043 , m_nextReconnectWait(INITIAL_RECONNECT_DELAY)
Yukai Tu16aabbc2015-10-06 05:08:42 -070044{
45 this->setLocalUri(FaceUri(m_socket.local_endpoint()));
46 this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
Alexander Afanasyevded17422018-04-03 19:00:23 -040047 this->setScope(faceScope);
Yukai Tu16aabbc2015-10-06 05:08:42 -070048 this->setPersistency(persistency);
49 this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
50 this->setMtu(MTU_UNLIMITED);
51
Davide Pesaventoa681a242019-03-29 23:48:27 -040052 NFD_LOG_FACE_DEBUG("Creating transport");
Yukai Tu16aabbc2015-10-06 05:08:42 -070053}
54
Eric Newberryb49313d2017-12-24 20:22:27 -070055ssize_t
56TcpTransport::getSendQueueLength()
57{
58 int queueLength = getSendQueueBytes();
59
60 // We want to obtain the amount of "not sent" bytes instead of the amount of "not sent" + "not
61 // acked" bytes. On Linux, we use SIOCOUTQNSD for this reason. However, macOS does not provide an
62 // efficient mechanism to obtain this value (SO_NWRITE includes both "not sent" and "not acked").
63#if defined(__linux__)
64 int nsd;
65 if (ioctl(m_socket.native_handle(), SIOCOUTQNSD, &nsd) < 0) {
66 NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno));
67 }
68 else if (nsd > 0) {
69 NFD_LOG_FACE_TRACE("SIOCOUTQNSD=" << nsd);
70 queueLength += nsd;
71 }
72#endif
73
74 return queueLength;
75}
76
Yanbiao Li32dab972016-11-27 12:26:09 +080077bool
78TcpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
Yukai Tu16aabbc2015-10-06 05:08:42 -070079{
Yanbiao Li32dab972016-11-27 12:26:09 +080080 return true;
81}
82
83void
84TcpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
85{
86 // if persistency was changed from permanent to any other value
87 if (oldPersistency == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
Weiwei Liudcdf6212016-08-31 14:34:22 -070088 if (this->getState() == TransportState::DOWN) {
89 // non-permanent transport cannot be in DOWN state, so fail hard
90 this->setState(TransportState::FAILED);
91 doClose();
92 }
Yukai Tu16aabbc2015-10-06 05:08:42 -070093 }
94}
95
Weiwei Liudcdf6212016-08-31 14:34:22 -070096void
97TcpTransport::handleError(const boost::system::error_code& error)
98{
99 if (this->getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
100 NFD_LOG_FACE_TRACE("TCP socket error: " << error.message());
101 this->setState(TransportState::DOWN);
102
103 // cancel all outstanding operations
Davide Pesavento3dade002019-03-19 11:29:56 -0600104 boost::system::error_code ec;
105 m_socket.cancel(ec);
Weiwei Liudcdf6212016-08-31 14:34:22 -0700106
107 // do this asynchronously because there could be some callbacks still pending
108 getGlobalIoService().post([this] { reconnect(); });
109 }
110 else {
111 StreamTransport::handleError(error);
112 }
113}
114
115void
116TcpTransport::reconnect()
117{
118 NFD_LOG_FACE_TRACE(__func__);
119
120 if (getState() == TransportState::CLOSING ||
121 getState() == TransportState::FAILED ||
122 getState() == TransportState::CLOSED) {
123 // transport is shutting down, don't attempt to reconnect
124 return;
125 }
126
127 BOOST_ASSERT(getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT);
128 BOOST_ASSERT(getState() == TransportState::DOWN);
129
130 // recreate the socket
Alexander Afanasyeva6bd7da2019-04-25 17:26:11 -0400131 m_socket = protocol::socket(
132#if BOOST_VERSION >= 107000
133 m_socket.get_executor()
134#else
135 m_socket.get_io_service()
136#endif // BOOST_VERSION >= 107000
137 );
Weiwei Liudcdf6212016-08-31 14:34:22 -0700138 this->resetReceiveBuffer();
139 this->resetSendQueue();
140
Davide Pesavento3dade002019-03-19 11:29:56 -0600141 m_reconnectEvent = getScheduler().schedule(m_nextReconnectWait,
142 [this] { this->handleReconnectTimeout(); });
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400143 m_socket.async_connect(m_remoteEndpoint, [this] (const auto& e) { this->handleReconnect(e); });
Weiwei Liudcdf6212016-08-31 14:34:22 -0700144}
145
146void
147TcpTransport::handleReconnect(const boost::system::error_code& error)
148{
149 if (getState() == TransportState::CLOSING ||
150 getState() == TransportState::FAILED ||
151 getState() == TransportState::CLOSED ||
152 error == boost::asio::error::operation_aborted) {
153 // transport is shutting down, abort the reconnection attempt and ignore any errors
154 return;
155 }
156
157 if (error) {
158 NFD_LOG_FACE_TRACE("Reconnection attempt failed: " << error.message());
159 return;
160 }
161
162 m_reconnectEvent.cancel();
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400163 m_nextReconnectWait = INITIAL_RECONNECT_DELAY;
Weiwei Liudcdf6212016-08-31 14:34:22 -0700164
165 this->setLocalUri(FaceUri(m_socket.local_endpoint()));
166 NFD_LOG_FACE_TRACE("TCP connection reestablished");
167 this->setState(TransportState::UP);
168 this->startReceive();
169}
170
171void
172TcpTransport::handleReconnectTimeout()
173{
174 // abort the reconnection attempt
175 boost::system::error_code error;
176 m_socket.close(error);
177
178 // exponentially back off the reconnection timer
179 m_nextReconnectWait =
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400180 std::min(time::duration_cast<time::milliseconds>(m_nextReconnectWait * RECONNECT_DELAY_MULTIPLIER),
181 MAX_RECONNECT_DELAY);
Weiwei Liudcdf6212016-08-31 14:34:22 -0700182
183 // do this asynchronously because there could be some callbacks still pending
184 getGlobalIoService().post([this] { reconnect(); });
185}
186
187void
188TcpTransport::doClose()
189{
190 m_reconnectEvent.cancel();
191 StreamTransport::doClose();
192}
193
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400194} // namespace nfd::face