Yukai Tu | 74c895d | 2015-09-21 01:11:51 -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 | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -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 | #ifndef NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP |
| 27 | #define NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP |
| 28 | |
| 29 | #include "transport.hpp" |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 30 | #include "socket-utils.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 31 | #include "common/global.hpp" |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 32 | |
| 33 | #include <queue> |
| 34 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 35 | #include <boost/asio/write.hpp> |
| 36 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 37 | namespace nfd::face { |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 38 | |
| 39 | /** \brief Implements Transport for stream-based protocols. |
| 40 | * |
| 41 | * \tparam Protocol a stream-based protocol in Boost.Asio |
| 42 | */ |
| 43 | template<class Protocol> |
| 44 | class StreamTransport : public Transport |
| 45 | { |
| 46 | public: |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 47 | using protocol = Protocol; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 48 | |
| 49 | /** \brief Construct stream transport. |
| 50 | * |
| 51 | * \param socket Protocol-specific socket for the created transport |
| 52 | */ |
| 53 | explicit |
| 54 | StreamTransport(typename protocol::socket&& socket); |
| 55 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 56 | ssize_t |
| 57 | getSendQueueLength() override; |
| 58 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 59 | protected: |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 60 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 61 | doClose() override; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 62 | |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 63 | void |
| 64 | deferredClose(); |
| 65 | |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 66 | void |
Teng Liang | 13d582a | 2020-07-21 20:23:11 -0700 | [diff] [blame] | 67 | doSend(const Block& packet) override; |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 68 | |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 69 | void |
| 70 | sendFromQueue(); |
| 71 | |
| 72 | void |
| 73 | handleSend(const boost::system::error_code& error, |
| 74 | size_t nBytesSent); |
| 75 | |
| 76 | void |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 77 | startReceive(); |
| 78 | |
| 79 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 80 | handleReceive(const boost::system::error_code& error, |
| 81 | size_t nBytesReceived); |
| 82 | |
| 83 | void |
| 84 | processErrorCode(const boost::system::error_code& error); |
| 85 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 86 | virtual void |
| 87 | handleError(const boost::system::error_code& error); |
| 88 | |
| 89 | void |
| 90 | resetReceiveBuffer(); |
| 91 | |
| 92 | void |
| 93 | resetSendQueue(); |
| 94 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 95 | size_t |
| 96 | getSendQueueBytes() const; |
| 97 | |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 98 | protected: |
| 99 | typename protocol::socket m_socket; |
| 100 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 101 | NFD_LOG_MEMBER_DECL(); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 102 | |
| 103 | private: |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 104 | uint8_t m_receiveBuffer[ndn::MAX_NDN_PACKET_SIZE]; |
| 105 | size_t m_receiveBufferSize; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 106 | std::queue<Block> m_sendQueue; |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 107 | size_t m_sendQueueBytes; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 110 | |
| 111 | template<class T> |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 112 | StreamTransport<T>::StreamTransport(typename StreamTransport::protocol::socket&& socket) |
| 113 | : m_socket(std::move(socket)) |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 114 | , m_receiveBufferSize(0) |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 115 | , m_sendQueueBytes(0) |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 116 | { |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 117 | // No queue capacity is set because there is no theoretical limit to the size of m_sendQueue. |
| 118 | // Therefore, protecting against send queue overflows is less critical than in other transport |
| 119 | // types. Instead, we use the default threshold specified in the GenericLinkService options. |
| 120 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 121 | startReceive(); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | template<class T> |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 125 | ssize_t |
| 126 | StreamTransport<T>::getSendQueueLength() |
| 127 | { |
| 128 | ssize_t queueLength = getTxQueueLength(m_socket.native_handle()); |
| 129 | if (queueLength == QUEUE_ERROR) { |
| 130 | NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno)); |
| 131 | } |
| 132 | return getSendQueueBytes() + std::max<ssize_t>(0, queueLength); |
| 133 | } |
| 134 | |
| 135 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 136 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 137 | StreamTransport<T>::doClose() |
| 138 | { |
| 139 | NFD_LOG_FACE_TRACE(__func__); |
| 140 | |
| 141 | if (m_socket.is_open()) { |
| 142 | // Cancel all outstanding operations and shutdown the socket |
| 143 | // so that no further sends or receives are possible. |
| 144 | // Use the non-throwing variants and ignore errors, if any. |
| 145 | boost::system::error_code error; |
| 146 | m_socket.cancel(error); |
| 147 | m_socket.shutdown(protocol::socket::shutdown_both, error); |
| 148 | } |
| 149 | |
| 150 | // Ensure that the Transport stays alive at least until |
| 151 | // all pending handlers are dispatched |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 152 | getGlobalIoService().post([this] { deferredClose(); }); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 153 | |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 154 | // Some bug or feature of Boost.Asio (see https://redmine.named-data.net/issues/1856): |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 155 | // |
| 156 | // When doClose is called from a socket event handler (e.g., from handleReceive), |
| 157 | // m_socket.shutdown() does not trigger the cancellation of the handleSend callback. |
| 158 | // Instead, handleSend is invoked as nothing bad happened. |
| 159 | // |
| 160 | // In order to prevent the assertion in handleSend from failing, we clear the queue |
| 161 | // and close the socket in deferredClose, i.e., after all callbacks scheduled up to |
| 162 | // this point have been executed. If more send operations are scheduled after this |
| 163 | // point, they will fail because the socket has been shutdown, and their callbacks |
| 164 | // will be invoked with error code == asio::error::shut_down. |
| 165 | } |
| 166 | |
| 167 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 168 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 169 | StreamTransport<T>::deferredClose() |
| 170 | { |
| 171 | NFD_LOG_FACE_TRACE(__func__); |
| 172 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 173 | resetSendQueue(); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 174 | |
| 175 | // use the non-throwing variant and ignore errors, if any |
| 176 | boost::system::error_code error; |
| 177 | m_socket.close(error); |
| 178 | |
| 179 | this->setState(TransportState::CLOSED); |
| 180 | } |
| 181 | |
| 182 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 183 | void |
Teng Liang | 13d582a | 2020-07-21 20:23:11 -0700 | [diff] [blame] | 184 | StreamTransport<T>::doSend(const Block& packet) |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 185 | { |
| 186 | NFD_LOG_FACE_TRACE(__func__); |
| 187 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 188 | if (getState() != TransportState::UP) |
| 189 | return; |
| 190 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 191 | bool wasQueueEmpty = m_sendQueue.empty(); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 192 | m_sendQueue.push(packet); |
| 193 | m_sendQueueBytes += packet.size(); |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 194 | |
| 195 | if (wasQueueEmpty) |
| 196 | sendFromQueue(); |
| 197 | } |
| 198 | |
| 199 | template<class T> |
| 200 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 201 | StreamTransport<T>::sendFromQueue() |
| 202 | { |
| 203 | boost::asio::async_write(m_socket, boost::asio::buffer(m_sendQueue.front()), |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 204 | [this] (auto&&... args) { this->handleSend(std::forward<decltype(args)>(args)...); }); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 208 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 209 | StreamTransport<T>::handleSend(const boost::system::error_code& error, |
| 210 | size_t nBytesSent) |
| 211 | { |
| 212 | if (error) |
| 213 | return processErrorCode(error); |
| 214 | |
| 215 | NFD_LOG_FACE_TRACE("Successfully sent: " << nBytesSent << " bytes"); |
| 216 | |
| 217 | BOOST_ASSERT(!m_sendQueue.empty()); |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 218 | BOOST_ASSERT(m_sendQueue.front().size() == nBytesSent); |
| 219 | m_sendQueueBytes -= nBytesSent; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 220 | m_sendQueue.pop(); |
| 221 | |
| 222 | if (!m_sendQueue.empty()) |
| 223 | sendFromQueue(); |
| 224 | } |
| 225 | |
| 226 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 227 | void |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 228 | StreamTransport<T>::startReceive() |
| 229 | { |
| 230 | BOOST_ASSERT(getState() == TransportState::UP); |
| 231 | |
| 232 | m_socket.async_receive(boost::asio::buffer(m_receiveBuffer + m_receiveBufferSize, |
| 233 | ndn::MAX_NDN_PACKET_SIZE - m_receiveBufferSize), |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 234 | [this] (auto&&... args) { this->handleReceive(std::forward<decltype(args)>(args)...); }); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | template<class T> |
| 238 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 239 | StreamTransport<T>::handleReceive(const boost::system::error_code& error, |
| 240 | size_t nBytesReceived) |
| 241 | { |
| 242 | if (error) |
| 243 | return processErrorCode(error); |
| 244 | |
| 245 | NFD_LOG_FACE_TRACE("Received: " << nBytesReceived << " bytes"); |
| 246 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 247 | m_receiveBufferSize += nBytesReceived; |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 248 | auto bufferView = ndn::make_span(m_receiveBuffer, m_receiveBufferSize); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 249 | size_t offset = 0; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 250 | bool isOk = true; |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 251 | while (offset < bufferView.size()) { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 252 | Block element; |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 253 | std::tie(isOk, element) = Block::fromBuffer(bufferView.subspan(offset)); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 254 | if (!isOk) |
| 255 | break; |
| 256 | |
| 257 | offset += element.size(); |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 258 | BOOST_ASSERT(offset <= bufferView.size()); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 259 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 260 | this->receive(element); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 263 | if (!isOk && m_receiveBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset == 0) { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 264 | NFD_LOG_FACE_ERROR("Failed to parse incoming packet or packet too large to process"); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 265 | this->setState(TransportState::FAILED); |
| 266 | doClose(); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | if (offset > 0) { |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 271 | if (offset != m_receiveBufferSize) { |
| 272 | std::copy(m_receiveBuffer + offset, m_receiveBuffer + m_receiveBufferSize, m_receiveBuffer); |
| 273 | m_receiveBufferSize -= offset; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 274 | } |
| 275 | else { |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 276 | m_receiveBufferSize = 0; |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 280 | startReceive(); |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | template<class T> |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 284 | void |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 285 | StreamTransport<T>::processErrorCode(const boost::system::error_code& error) |
| 286 | { |
| 287 | NFD_LOG_FACE_TRACE(__func__); |
| 288 | |
| 289 | if (getState() == TransportState::CLOSING || |
| 290 | getState() == TransportState::FAILED || |
| 291 | getState() == TransportState::CLOSED || |
| 292 | error == boost::asio::error::operation_aborted || // when cancel() is called |
| 293 | error == boost::asio::error::shut_down) // after shutdown() is called |
| 294 | // transport is shutting down, ignore any errors |
| 295 | return; |
| 296 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 297 | handleError(error); |
| 298 | } |
| 299 | |
| 300 | template<class T> |
| 301 | void |
| 302 | StreamTransport<T>::handleError(const boost::system::error_code& error) |
| 303 | { |
Davide Pesavento | 68ab43d | 2017-07-02 13:37:35 -0400 | [diff] [blame] | 304 | if (error == boost::asio::error::eof) { |
| 305 | this->setState(TransportState::CLOSING); |
| 306 | } |
| 307 | else { |
Davide Pesavento | 1816d4b | 2017-07-02 12:20:48 -0400 | [diff] [blame] | 308 | NFD_LOG_FACE_ERROR("Send or receive operation failed: " << error.message()); |
Davide Pesavento | 68ab43d | 2017-07-02 13:37:35 -0400 | [diff] [blame] | 309 | this->setState(TransportState::FAILED); |
| 310 | } |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 311 | doClose(); |
| 312 | } |
| 313 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 314 | template<class T> |
| 315 | void |
| 316 | StreamTransport<T>::resetReceiveBuffer() |
| 317 | { |
| 318 | m_receiveBufferSize = 0; |
| 319 | } |
| 320 | |
| 321 | template<class T> |
| 322 | void |
| 323 | StreamTransport<T>::resetSendQueue() |
| 324 | { |
| 325 | std::queue<Block> emptyQueue; |
| 326 | std::swap(emptyQueue, m_sendQueue); |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 327 | m_sendQueueBytes = 0; |
| 328 | } |
| 329 | |
| 330 | template<class T> |
| 331 | size_t |
| 332 | StreamTransport<T>::getSendQueueBytes() const |
| 333 | { |
| 334 | return m_sendQueueBytes; |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 337 | } // namespace nfd::face |
Yukai Tu | 74c895d | 2015-09-21 01:11:51 -0700 | [diff] [blame] | 338 | |
| 339 | #endif // NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP |