Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 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. |
| 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 Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 29 | #include <boost/asio/defer.hpp> |
| 30 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 31 | #if defined(__linux__) |
| 32 | #include <linux/sockios.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #endif |
| 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::face { |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 37 | |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame^] | 38 | namespace ip = boost::asio::ip; |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 39 | |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame^] | 40 | NFD_LOG_MEMBER_INIT_SPECIALIZED(StreamTransport<ip::tcp>, TcpTransport); |
| 41 | |
| 42 | TcpTransport::TcpTransport(ip::tcp::socket&& socket, |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 43 | ndn::nfd::FacePersistency persistency, |
| 44 | ndn::nfd::FaceScope faceScope) |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 45 | : StreamTransport(std::move(socket)) |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 46 | , m_remoteEndpoint(m_socket.remote_endpoint()) |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 47 | , m_nextReconnectWait(INITIAL_RECONNECT_DELAY) |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 48 | { |
| 49 | this->setLocalUri(FaceUri(m_socket.local_endpoint())); |
| 50 | this->setRemoteUri(FaceUri(m_socket.remote_endpoint())); |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 51 | this->setScope(faceScope); |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 52 | this->setPersistency(persistency); |
| 53 | this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 54 | this->setMtu(MTU_UNLIMITED); |
| 55 | |
Davide Pesavento | a681a24 | 2019-03-29 23:48:27 -0400 | [diff] [blame] | 56 | NFD_LOG_FACE_DEBUG("Creating transport"); |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 59 | ssize_t |
| 60 | TcpTransport::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 Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 81 | bool |
| 82 | TcpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 83 | { |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | TcpTransport::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 Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 92 | 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 Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 100 | void |
| 101 | TcpTransport::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 Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 108 | boost::system::error_code ec; |
| 109 | m_socket.cancel(ec); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 110 | |
| 111 | // do this asynchronously because there could be some callbacks still pending |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 112 | boost::asio::defer(getGlobalIoService(), [this] { reconnect(); }); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 113 | } |
| 114 | else { |
| 115 | StreamTransport::handleError(error); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | TcpTransport::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 Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame^] | 135 | m_socket = ip::tcp::socket(m_socket.get_executor()); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 136 | this->resetReceiveBuffer(); |
| 137 | this->resetSendQueue(); |
| 138 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 139 | m_reconnectEvent = getScheduler().schedule(m_nextReconnectWait, |
| 140 | [this] { this->handleReconnectTimeout(); }); |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 141 | m_socket.async_connect(m_remoteEndpoint, [this] (const auto& e) { this->handleReconnect(e); }); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void |
| 145 | TcpTransport::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 Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 161 | m_nextReconnectWait = INITIAL_RECONNECT_DELAY; |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 162 | |
| 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 | |
| 169 | void |
| 170 | TcpTransport::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 Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 178 | std::min(time::duration_cast<time::milliseconds>(m_nextReconnectWait * RECONNECT_DELAY_MULTIPLIER), |
| 179 | MAX_RECONNECT_DELAY); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 180 | |
| 181 | // do this asynchronously because there could be some callbacks still pending |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame] | 182 | boost::asio::defer(getGlobalIoService(), [this] { reconnect(); }); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void |
| 186 | TcpTransport::doClose() |
| 187 | { |
| 188 | m_reconnectEvent.cancel(); |
| 189 | StreamTransport::doClose(); |
| 190 | } |
| 191 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 192 | } // namespace nfd::face |