blob: 466e2c6c94628baae0fbafe0d8dbc5408a25c24e [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
Yukai Tu16aabbc2015-10-06 05:08:42 -070034namespace nfd {
35namespace face {
36
Davide Pesaventoa3148082018-04-12 18:21:54 -040037NFD_LOG_MEMBER_INIT_SPECIALIZED(StreamTransport<boost::asio::ip::tcp>, TcpTransport);
Weiwei Liudcdf6212016-08-31 14:34:22 -070038
Davide Pesavento3dade002019-03-19 11:29:56 -060039TcpTransport::TcpTransport(protocol::socket&& socket,
40 ndn::nfd::FacePersistency persistency,
41 ndn::nfd::FaceScope faceScope)
Yukai Tu16aabbc2015-10-06 05:08:42 -070042 : StreamTransport(std::move(socket))
Weiwei Liudcdf6212016-08-31 14:34:22 -070043 , m_remoteEndpoint(m_socket.remote_endpoint())
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040044 , m_nextReconnectWait(INITIAL_RECONNECT_DELAY)
Yukai Tu16aabbc2015-10-06 05:08:42 -070045{
46 this->setLocalUri(FaceUri(m_socket.local_endpoint()));
47 this->setRemoteUri(FaceUri(m_socket.remote_endpoint()));
Alexander Afanasyevded17422018-04-03 19:00:23 -040048 this->setScope(faceScope);
Yukai Tu16aabbc2015-10-06 05:08:42 -070049 this->setPersistency(persistency);
50 this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT);
51 this->setMtu(MTU_UNLIMITED);
52
Davide Pesaventoa681a242019-03-29 23:48:27 -040053 NFD_LOG_FACE_DEBUG("Creating transport");
Yukai Tu16aabbc2015-10-06 05:08:42 -070054}
55
Eric Newberryb49313d2017-12-24 20:22:27 -070056ssize_t
57TcpTransport::getSendQueueLength()
58{
59 int queueLength = getSendQueueBytes();
60
61 // We want to obtain the amount of "not sent" bytes instead of the amount of "not sent" + "not
62 // acked" bytes. On Linux, we use SIOCOUTQNSD for this reason. However, macOS does not provide an
63 // efficient mechanism to obtain this value (SO_NWRITE includes both "not sent" and "not acked").
64#if defined(__linux__)
65 int nsd;
66 if (ioctl(m_socket.native_handle(), SIOCOUTQNSD, &nsd) < 0) {
67 NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno));
68 }
69 else if (nsd > 0) {
70 NFD_LOG_FACE_TRACE("SIOCOUTQNSD=" << nsd);
71 queueLength += nsd;
72 }
73#endif
74
75 return queueLength;
76}
77
Yanbiao Li32dab972016-11-27 12:26:09 +080078bool
79TcpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
Yukai Tu16aabbc2015-10-06 05:08:42 -070080{
Yanbiao Li32dab972016-11-27 12:26:09 +080081 return true;
82}
83
84void
85TcpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
86{
87 // if persistency was changed from permanent to any other value
88 if (oldPersistency == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
Weiwei Liudcdf6212016-08-31 14:34:22 -070089 if (this->getState() == TransportState::DOWN) {
90 // non-permanent transport cannot be in DOWN state, so fail hard
91 this->setState(TransportState::FAILED);
92 doClose();
93 }
Yukai Tu16aabbc2015-10-06 05:08:42 -070094 }
95}
96
Weiwei Liudcdf6212016-08-31 14:34:22 -070097void
98TcpTransport::handleError(const boost::system::error_code& error)
99{
100 if (this->getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
101 NFD_LOG_FACE_TRACE("TCP socket error: " << error.message());
102 this->setState(TransportState::DOWN);
103
104 // cancel all outstanding operations
Davide Pesavento3dade002019-03-19 11:29:56 -0600105 boost::system::error_code ec;
106 m_socket.cancel(ec);
Weiwei Liudcdf6212016-08-31 14:34:22 -0700107
108 // do this asynchronously because there could be some callbacks still pending
109 getGlobalIoService().post([this] { reconnect(); });
110 }
111 else {
112 StreamTransport::handleError(error);
113 }
114}
115
116void
117TcpTransport::reconnect()
118{
119 NFD_LOG_FACE_TRACE(__func__);
120
121 if (getState() == TransportState::CLOSING ||
122 getState() == TransportState::FAILED ||
123 getState() == TransportState::CLOSED) {
124 // transport is shutting down, don't attempt to reconnect
125 return;
126 }
127
128 BOOST_ASSERT(getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT);
129 BOOST_ASSERT(getState() == TransportState::DOWN);
130
131 // recreate the socket
Alexander Afanasyeva6bd7da2019-04-25 17:26:11 -0400132 m_socket = protocol::socket(
133#if BOOST_VERSION >= 107000
134 m_socket.get_executor()
135#else
136 m_socket.get_io_service()
137#endif // BOOST_VERSION >= 107000
138 );
Weiwei Liudcdf6212016-08-31 14:34:22 -0700139 this->resetReceiveBuffer();
140 this->resetSendQueue();
141
Davide Pesavento3dade002019-03-19 11:29:56 -0600142 m_reconnectEvent = getScheduler().schedule(m_nextReconnectWait,
143 [this] { this->handleReconnectTimeout(); });
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400144 m_socket.async_connect(m_remoteEndpoint, [this] (const auto& e) { this->handleReconnect(e); });
Weiwei Liudcdf6212016-08-31 14:34:22 -0700145}
146
147void
148TcpTransport::handleReconnect(const boost::system::error_code& error)
149{
150 if (getState() == TransportState::CLOSING ||
151 getState() == TransportState::FAILED ||
152 getState() == TransportState::CLOSED ||
153 error == boost::asio::error::operation_aborted) {
154 // transport is shutting down, abort the reconnection attempt and ignore any errors
155 return;
156 }
157
158 if (error) {
159 NFD_LOG_FACE_TRACE("Reconnection attempt failed: " << error.message());
160 return;
161 }
162
163 m_reconnectEvent.cancel();
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400164 m_nextReconnectWait = INITIAL_RECONNECT_DELAY;
Weiwei Liudcdf6212016-08-31 14:34:22 -0700165
166 this->setLocalUri(FaceUri(m_socket.local_endpoint()));
167 NFD_LOG_FACE_TRACE("TCP connection reestablished");
168 this->setState(TransportState::UP);
169 this->startReceive();
170}
171
172void
173TcpTransport::handleReconnectTimeout()
174{
175 // abort the reconnection attempt
176 boost::system::error_code error;
177 m_socket.close(error);
178
179 // exponentially back off the reconnection timer
180 m_nextReconnectWait =
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400181 std::min(time::duration_cast<time::milliseconds>(m_nextReconnectWait * RECONNECT_DELAY_MULTIPLIER),
182 MAX_RECONNECT_DELAY);
Weiwei Liudcdf6212016-08-31 14:34:22 -0700183
184 // do this asynchronously because there could be some callbacks still pending
185 getGlobalIoService().post([this] { reconnect(); });
186}
187
188void
189TcpTransport::doClose()
190{
191 m_reconnectEvent.cancel();
192 StreamTransport::doClose();
193}
194
Yukai Tu16aabbc2015-10-06 05:08:42 -0700195} // namespace face
196} // namespace nfd