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 | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 29 | #if defined(__linux__) |
| 30 | #include <linux/sockios.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #endif |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::face { |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 36 | NFD_LOG_MEMBER_INIT_SPECIALIZED(StreamTransport<boost::asio::ip::tcp>, TcpTransport); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 37 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 38 | TcpTransport::TcpTransport(protocol::socket&& socket, |
| 39 | ndn::nfd::FacePersistency persistency, |
| 40 | ndn::nfd::FaceScope faceScope) |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 41 | : StreamTransport(std::move(socket)) |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 42 | , m_remoteEndpoint(m_socket.remote_endpoint()) |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 43 | , m_nextReconnectWait(INITIAL_RECONNECT_DELAY) |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 44 | { |
| 45 | this->setLocalUri(FaceUri(m_socket.local_endpoint())); |
| 46 | this->setRemoteUri(FaceUri(m_socket.remote_endpoint())); |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 47 | this->setScope(faceScope); |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 48 | this->setPersistency(persistency); |
| 49 | this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 50 | this->setMtu(MTU_UNLIMITED); |
| 51 | |
Davide Pesavento | a681a24 | 2019-03-29 23:48:27 -0400 | [diff] [blame] | 52 | NFD_LOG_FACE_DEBUG("Creating transport"); |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 55 | ssize_t |
| 56 | TcpTransport::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 Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 77 | bool |
| 78 | TcpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 79 | { |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 80 | return true; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | TcpTransport::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 Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 88 | 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 Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 96 | void |
| 97 | TcpTransport::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 Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 104 | boost::system::error_code ec; |
| 105 | m_socket.cancel(ec); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 106 | |
| 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 | |
| 115 | void |
| 116 | TcpTransport::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 Afanasyev | a6bd7da | 2019-04-25 17:26:11 -0400 | [diff] [blame] | 131 | 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 Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 138 | this->resetReceiveBuffer(); |
| 139 | this->resetSendQueue(); |
| 140 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 141 | m_reconnectEvent = getScheduler().schedule(m_nextReconnectWait, |
| 142 | [this] { this->handleReconnectTimeout(); }); |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 143 | 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] | 144 | } |
| 145 | |
| 146 | void |
| 147 | TcpTransport::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 Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 163 | m_nextReconnectWait = INITIAL_RECONNECT_DELAY; |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 164 | |
| 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 | |
| 171 | void |
| 172 | TcpTransport::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 Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 180 | std::min(time::duration_cast<time::milliseconds>(m_nextReconnectWait * RECONNECT_DELAY_MULTIPLIER), |
| 181 | MAX_RECONNECT_DELAY); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 182 | |
| 183 | // do this asynchronously because there could be some callbacks still pending |
| 184 | getGlobalIoService().post([this] { reconnect(); }); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | TcpTransport::doClose() |
| 189 | { |
| 190 | m_reconnectEvent.cancel(); |
| 191 | StreamTransport::doClose(); |
| 192 | } |
| 193 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 194 | } // namespace nfd::face |