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 | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 34 | namespace nfd { |
| 35 | namespace face { |
| 36 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 37 | NFD_LOG_MEMBER_INIT_SPECIALIZED(StreamTransport<boost::asio::ip::tcp>, TcpTransport); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 39 | time::milliseconds TcpTransport::s_initialReconnectWait = 1_s; |
| 40 | time::milliseconds TcpTransport::s_maxReconnectWait = 5_min; |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 41 | float TcpTransport::s_reconnectWaitMultiplier = 2.0f; |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 42 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 43 | TcpTransport::TcpTransport(protocol::socket&& socket, |
| 44 | ndn::nfd::FacePersistency persistency, |
| 45 | ndn::nfd::FaceScope faceScope) |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 46 | : StreamTransport(std::move(socket)) |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 47 | , m_remoteEndpoint(m_socket.remote_endpoint()) |
| 48 | , m_nextReconnectWait(s_initialReconnectWait) |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 49 | { |
| 50 | this->setLocalUri(FaceUri(m_socket.local_endpoint())); |
| 51 | this->setRemoteUri(FaceUri(m_socket.remote_endpoint())); |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 52 | this->setScope(faceScope); |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 53 | this->setPersistency(persistency); |
| 54 | this->setLinkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 55 | this->setMtu(MTU_UNLIMITED); |
| 56 | |
Davide Pesavento | a681a24 | 2019-03-29 23:48:27 -0400 | [diff] [blame] | 57 | NFD_LOG_FACE_DEBUG("Creating transport"); |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 60 | ssize_t |
| 61 | TcpTransport::getSendQueueLength() |
| 62 | { |
| 63 | int queueLength = getSendQueueBytes(); |
| 64 | |
| 65 | // We want to obtain the amount of "not sent" bytes instead of the amount of "not sent" + "not |
| 66 | // acked" bytes. On Linux, we use SIOCOUTQNSD for this reason. However, macOS does not provide an |
| 67 | // efficient mechanism to obtain this value (SO_NWRITE includes both "not sent" and "not acked"). |
| 68 | #if defined(__linux__) |
| 69 | int nsd; |
| 70 | if (ioctl(m_socket.native_handle(), SIOCOUTQNSD, &nsd) < 0) { |
| 71 | NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno)); |
| 72 | } |
| 73 | else if (nsd > 0) { |
| 74 | NFD_LOG_FACE_TRACE("SIOCOUTQNSD=" << nsd); |
| 75 | queueLength += nsd; |
| 76 | } |
| 77 | #endif |
| 78 | |
| 79 | return queueLength; |
| 80 | } |
| 81 | |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 82 | bool |
| 83 | TcpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 84 | { |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | TcpTransport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) |
| 90 | { |
| 91 | // if persistency was changed from permanent to any other value |
| 92 | if (oldPersistency == ndn::nfd::FACE_PERSISTENCY_PERMANENT) { |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 93 | if (this->getState() == TransportState::DOWN) { |
| 94 | // non-permanent transport cannot be in DOWN state, so fail hard |
| 95 | this->setState(TransportState::FAILED); |
| 96 | doClose(); |
| 97 | } |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 101 | void |
| 102 | TcpTransport::handleError(const boost::system::error_code& error) |
| 103 | { |
| 104 | if (this->getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) { |
| 105 | NFD_LOG_FACE_TRACE("TCP socket error: " << error.message()); |
| 106 | this->setState(TransportState::DOWN); |
| 107 | |
| 108 | // cancel all outstanding operations |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 109 | boost::system::error_code ec; |
| 110 | m_socket.cancel(ec); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 111 | |
| 112 | // do this asynchronously because there could be some callbacks still pending |
| 113 | getGlobalIoService().post([this] { reconnect(); }); |
| 114 | } |
| 115 | else { |
| 116 | StreamTransport::handleError(error); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | TcpTransport::reconnect() |
| 122 | { |
| 123 | NFD_LOG_FACE_TRACE(__func__); |
| 124 | |
| 125 | if (getState() == TransportState::CLOSING || |
| 126 | getState() == TransportState::FAILED || |
| 127 | getState() == TransportState::CLOSED) { |
| 128 | // transport is shutting down, don't attempt to reconnect |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | BOOST_ASSERT(getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 133 | BOOST_ASSERT(getState() == TransportState::DOWN); |
| 134 | |
| 135 | // recreate the socket |
Alexander Afanasyev | a6bd7da | 2019-04-25 17:26:11 -0400 | [diff] [blame] | 136 | m_socket = protocol::socket( |
| 137 | #if BOOST_VERSION >= 107000 |
| 138 | m_socket.get_executor() |
| 139 | #else |
| 140 | m_socket.get_io_service() |
| 141 | #endif // BOOST_VERSION >= 107000 |
| 142 | ); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 143 | this->resetReceiveBuffer(); |
| 144 | this->resetSendQueue(); |
| 145 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 146 | m_reconnectEvent = getScheduler().schedule(m_nextReconnectWait, |
| 147 | [this] { this->handleReconnectTimeout(); }); |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 148 | 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] | 149 | } |
| 150 | |
| 151 | void |
| 152 | TcpTransport::handleReconnect(const boost::system::error_code& error) |
| 153 | { |
| 154 | if (getState() == TransportState::CLOSING || |
| 155 | getState() == TransportState::FAILED || |
| 156 | getState() == TransportState::CLOSED || |
| 157 | error == boost::asio::error::operation_aborted) { |
| 158 | // transport is shutting down, abort the reconnection attempt and ignore any errors |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | if (error) { |
| 163 | NFD_LOG_FACE_TRACE("Reconnection attempt failed: " << error.message()); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | m_reconnectEvent.cancel(); |
| 168 | m_nextReconnectWait = s_initialReconnectWait; |
| 169 | |
| 170 | this->setLocalUri(FaceUri(m_socket.local_endpoint())); |
| 171 | NFD_LOG_FACE_TRACE("TCP connection reestablished"); |
| 172 | this->setState(TransportState::UP); |
| 173 | this->startReceive(); |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | TcpTransport::handleReconnectTimeout() |
| 178 | { |
| 179 | // abort the reconnection attempt |
| 180 | boost::system::error_code error; |
| 181 | m_socket.close(error); |
| 182 | |
| 183 | // exponentially back off the reconnection timer |
| 184 | m_nextReconnectWait = |
| 185 | std::min(time::duration_cast<time::milliseconds>(m_nextReconnectWait * s_reconnectWaitMultiplier), |
| 186 | s_maxReconnectWait); |
| 187 | |
| 188 | // do this asynchronously because there could be some callbacks still pending |
| 189 | getGlobalIoService().post([this] { reconnect(); }); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | TcpTransport::doClose() |
| 194 | { |
| 195 | m_reconnectEvent.cancel(); |
| 196 | StreamTransport::doClose(); |
| 197 | } |
| 198 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 199 | } // namespace face |
| 200 | } // namespace nfd |