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