blob: eb7de92305f7c78098f37e20c0190ee990abb6c8 [file] [log] [blame]
Yukai Tu74c895d2015-09-21 01:11:51 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberryb49313d2017-12-24 20:22:27 -07002/*
Davide Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, Regents of the University of California,
Yukai Tu74c895d2015-09-21 01:11:51 -07004 * 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 Newberryb49313d2017-12-24 20:22:27 -070030#include "socket-utils.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060031#include "daemon/global.hpp"
Yukai Tu74c895d2015-09-21 01:11:51 -070032
33#include <queue>
34
35namespace nfd {
36namespace face {
37
38/** \brief Implements Transport for stream-based protocols.
39 *
40 * \tparam Protocol a stream-based protocol in Boost.Asio
41 */
42template<class Protocol>
43class StreamTransport : public Transport
44{
45public:
46 typedef Protocol protocol;
47
48 /** \brief Construct stream transport.
49 *
50 * \param socket Protocol-specific socket for the created transport
51 */
52 explicit
53 StreamTransport(typename protocol::socket&& socket);
54
Eric Newberryb49313d2017-12-24 20:22:27 -070055 ssize_t
56 getSendQueueLength() override;
57
Davide Pesavento8728a252015-11-06 04:01:22 +010058protected:
Davide Pesavento1816d4b2017-07-02 12:20:48 -040059 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020060 doClose() override;
Yukai Tu74c895d2015-09-21 01:11:51 -070061
Yukai Tu74c895d2015-09-21 01:11:51 -070062 void
63 deferredClose();
64
Davide Pesavento1816d4b2017-07-02 12:20:48 -040065 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020066 doSend(Transport::Packet&& packet) override;
Davide Pesavento8728a252015-11-06 04:01:22 +010067
Yukai Tu74c895d2015-09-21 01:11:51 -070068 void
69 sendFromQueue();
70
71 void
72 handleSend(const boost::system::error_code& error,
73 size_t nBytesSent);
74
75 void
Weiwei Liudcdf6212016-08-31 14:34:22 -070076 startReceive();
77
78 void
Yukai Tu74c895d2015-09-21 01:11:51 -070079 handleReceive(const boost::system::error_code& error,
80 size_t nBytesReceived);
81
82 void
83 processErrorCode(const boost::system::error_code& error);
84
Weiwei Liudcdf6212016-08-31 14:34:22 -070085 virtual void
86 handleError(const boost::system::error_code& error);
87
88 void
89 resetReceiveBuffer();
90
91 void
92 resetSendQueue();
93
Eric Newberryb49313d2017-12-24 20:22:27 -070094 size_t
95 getSendQueueBytes() const;
96
Yukai Tu74c895d2015-09-21 01:11:51 -070097protected:
98 typename protocol::socket m_socket;
99
Davide Pesaventoa3148082018-04-12 18:21:54 -0400100 NFD_LOG_MEMBER_DECL();
Yukai Tu74c895d2015-09-21 01:11:51 -0700101
102private:
Davide Pesavento8728a252015-11-06 04:01:22 +0100103 uint8_t m_receiveBuffer[ndn::MAX_NDN_PACKET_SIZE];
104 size_t m_receiveBufferSize;
Yukai Tu74c895d2015-09-21 01:11:51 -0700105 std::queue<Block> m_sendQueue;
Eric Newberryb49313d2017-12-24 20:22:27 -0700106 size_t m_sendQueueBytes;
Yukai Tu74c895d2015-09-21 01:11:51 -0700107};
108
Yukai Tu74c895d2015-09-21 01:11:51 -0700109
110template<class T>
Yukai Tu74c895d2015-09-21 01:11:51 -0700111StreamTransport<T>::StreamTransport(typename StreamTransport::protocol::socket&& socket)
112 : m_socket(std::move(socket))
Davide Pesavento8728a252015-11-06 04:01:22 +0100113 , m_receiveBufferSize(0)
Eric Newberryb49313d2017-12-24 20:22:27 -0700114 , m_sendQueueBytes(0)
Yukai Tu74c895d2015-09-21 01:11:51 -0700115{
Eric Newberryb49313d2017-12-24 20:22:27 -0700116 // No queue capacity is set because there is no theoretical limit to the size of m_sendQueue.
117 // Therefore, protecting against send queue overflows is less critical than in other transport
118 // types. Instead, we use the default threshold specified in the GenericLinkService options.
119
Weiwei Liudcdf6212016-08-31 14:34:22 -0700120 startReceive();
Yukai Tu74c895d2015-09-21 01:11:51 -0700121}
122
123template<class T>
Eric Newberryb49313d2017-12-24 20:22:27 -0700124ssize_t
125StreamTransport<T>::getSendQueueLength()
126{
127 ssize_t queueLength = getTxQueueLength(m_socket.native_handle());
128 if (queueLength == QUEUE_ERROR) {
129 NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno));
130 }
131 return getSendQueueBytes() + std::max<ssize_t>(0, queueLength);
132}
133
134template<class T>
Davide Pesavento8728a252015-11-06 04:01:22 +0100135void
Yukai Tu74c895d2015-09-21 01:11:51 -0700136StreamTransport<T>::doClose()
137{
138 NFD_LOG_FACE_TRACE(__func__);
139
140 if (m_socket.is_open()) {
141 // Cancel all outstanding operations and shutdown the socket
142 // so that no further sends or receives are possible.
143 // Use the non-throwing variants and ignore errors, if any.
144 boost::system::error_code error;
145 m_socket.cancel(error);
146 m_socket.shutdown(protocol::socket::shutdown_both, error);
147 }
148
149 // Ensure that the Transport stays alive at least until
150 // all pending handlers are dispatched
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400151 getGlobalIoService().post([this] { deferredClose(); });
Yukai Tu74c895d2015-09-21 01:11:51 -0700152
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400153 // Some bug or feature of Boost.Asio (see https://redmine.named-data.net/issues/1856):
Yukai Tu74c895d2015-09-21 01:11:51 -0700154 //
155 // When doClose is called from a socket event handler (e.g., from handleReceive),
156 // m_socket.shutdown() does not trigger the cancellation of the handleSend callback.
157 // Instead, handleSend is invoked as nothing bad happened.
158 //
159 // In order to prevent the assertion in handleSend from failing, we clear the queue
160 // and close the socket in deferredClose, i.e., after all callbacks scheduled up to
161 // this point have been executed. If more send operations are scheduled after this
162 // point, they will fail because the socket has been shutdown, and their callbacks
163 // will be invoked with error code == asio::error::shut_down.
164}
165
166template<class T>
Davide Pesavento8728a252015-11-06 04:01:22 +0100167void
Yukai Tu74c895d2015-09-21 01:11:51 -0700168StreamTransport<T>::deferredClose()
169{
170 NFD_LOG_FACE_TRACE(__func__);
171
Weiwei Liudcdf6212016-08-31 14:34:22 -0700172 resetSendQueue();
Yukai Tu74c895d2015-09-21 01:11:51 -0700173
174 // use the non-throwing variant and ignore errors, if any
175 boost::system::error_code error;
176 m_socket.close(error);
177
178 this->setState(TransportState::CLOSED);
179}
180
181template<class T>
Davide Pesavento8728a252015-11-06 04:01:22 +0100182void
183StreamTransport<T>::doSend(Transport::Packet&& packet)
184{
185 NFD_LOG_FACE_TRACE(__func__);
186
Weiwei Liudcdf6212016-08-31 14:34:22 -0700187 if (getState() != TransportState::UP)
188 return;
189
Davide Pesavento8728a252015-11-06 04:01:22 +0100190 bool wasQueueEmpty = m_sendQueue.empty();
191 m_sendQueue.push(packet.packet);
Eric Newberryb49313d2017-12-24 20:22:27 -0700192 m_sendQueueBytes += packet.packet.size();
Davide Pesavento8728a252015-11-06 04:01:22 +0100193
194 if (wasQueueEmpty)
195 sendFromQueue();
196}
197
198template<class T>
199void
Yukai Tu74c895d2015-09-21 01:11:51 -0700200StreamTransport<T>::sendFromQueue()
201{
202 boost::asio::async_write(m_socket, boost::asio::buffer(m_sendQueue.front()),
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400203 [this] (auto&&... args) { this->handleSend(std::forward<decltype(args)>(args)...); });
Yukai Tu74c895d2015-09-21 01:11:51 -0700204}
205
206template<class T>
Davide Pesavento8728a252015-11-06 04:01:22 +0100207void
Yukai Tu74c895d2015-09-21 01:11:51 -0700208StreamTransport<T>::handleSend(const boost::system::error_code& error,
209 size_t nBytesSent)
210{
211 if (error)
212 return processErrorCode(error);
213
214 NFD_LOG_FACE_TRACE("Successfully sent: " << nBytesSent << " bytes");
215
216 BOOST_ASSERT(!m_sendQueue.empty());
Eric Newberryb49313d2017-12-24 20:22:27 -0700217 BOOST_ASSERT(m_sendQueue.front().size() == nBytesSent);
218 m_sendQueueBytes -= nBytesSent;
Yukai Tu74c895d2015-09-21 01:11:51 -0700219 m_sendQueue.pop();
220
221 if (!m_sendQueue.empty())
222 sendFromQueue();
223}
224
225template<class T>
Davide Pesavento8728a252015-11-06 04:01:22 +0100226void
Weiwei Liudcdf6212016-08-31 14:34:22 -0700227StreamTransport<T>::startReceive()
228{
229 BOOST_ASSERT(getState() == TransportState::UP);
230
231 m_socket.async_receive(boost::asio::buffer(m_receiveBuffer + m_receiveBufferSize,
232 ndn::MAX_NDN_PACKET_SIZE - m_receiveBufferSize),
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400233 [this] (auto&&... args) { this->handleReceive(std::forward<decltype(args)>(args)...); });
Weiwei Liudcdf6212016-08-31 14:34:22 -0700234}
235
236template<class T>
237void
Yukai Tu74c895d2015-09-21 01:11:51 -0700238StreamTransport<T>::handleReceive(const boost::system::error_code& error,
239 size_t nBytesReceived)
240{
241 if (error)
242 return processErrorCode(error);
243
244 NFD_LOG_FACE_TRACE("Received: " << nBytesReceived << " bytes");
245
Davide Pesavento8728a252015-11-06 04:01:22 +0100246 m_receiveBufferSize += nBytesReceived;
Yukai Tu74c895d2015-09-21 01:11:51 -0700247 size_t offset = 0;
Yukai Tu74c895d2015-09-21 01:11:51 -0700248 bool isOk = true;
Davide Pesavento8728a252015-11-06 04:01:22 +0100249 while (m_receiveBufferSize - offset > 0) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400250 Block element;
Davide Pesavento8728a252015-11-06 04:01:22 +0100251 std::tie(isOk, element) = Block::fromBuffer(m_receiveBuffer + offset, m_receiveBufferSize - offset);
Yukai Tu74c895d2015-09-21 01:11:51 -0700252 if (!isOk)
253 break;
254
255 offset += element.size();
Davide Pesavento8728a252015-11-06 04:01:22 +0100256 BOOST_ASSERT(offset <= m_receiveBufferSize);
Yukai Tu74c895d2015-09-21 01:11:51 -0700257
Davide Pesavento8728a252015-11-06 04:01:22 +0100258 this->receive(Transport::Packet(std::move(element)));
Yukai Tu74c895d2015-09-21 01:11:51 -0700259 }
260
Davide Pesavento8728a252015-11-06 04:01:22 +0100261 if (!isOk && m_receiveBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset == 0) {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400262 NFD_LOG_FACE_ERROR("Failed to parse incoming packet or packet too large to process");
Yukai Tu74c895d2015-09-21 01:11:51 -0700263 this->setState(TransportState::FAILED);
264 doClose();
265 return;
266 }
267
268 if (offset > 0) {
Davide Pesavento8728a252015-11-06 04:01:22 +0100269 if (offset != m_receiveBufferSize) {
270 std::copy(m_receiveBuffer + offset, m_receiveBuffer + m_receiveBufferSize, m_receiveBuffer);
271 m_receiveBufferSize -= offset;
Yukai Tu74c895d2015-09-21 01:11:51 -0700272 }
273 else {
Davide Pesavento8728a252015-11-06 04:01:22 +0100274 m_receiveBufferSize = 0;
Yukai Tu74c895d2015-09-21 01:11:51 -0700275 }
276 }
277
Weiwei Liudcdf6212016-08-31 14:34:22 -0700278 startReceive();
Yukai Tu74c895d2015-09-21 01:11:51 -0700279}
280
281template<class T>
Davide Pesavento8728a252015-11-06 04:01:22 +0100282void
Yukai Tu74c895d2015-09-21 01:11:51 -0700283StreamTransport<T>::processErrorCode(const boost::system::error_code& error)
284{
285 NFD_LOG_FACE_TRACE(__func__);
286
287 if (getState() == TransportState::CLOSING ||
288 getState() == TransportState::FAILED ||
289 getState() == TransportState::CLOSED ||
290 error == boost::asio::error::operation_aborted || // when cancel() is called
291 error == boost::asio::error::shut_down) // after shutdown() is called
292 // transport is shutting down, ignore any errors
293 return;
294
Weiwei Liudcdf6212016-08-31 14:34:22 -0700295 handleError(error);
296}
297
298template<class T>
299void
300StreamTransport<T>::handleError(const boost::system::error_code& error)
301{
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400302 if (error == boost::asio::error::eof) {
303 this->setState(TransportState::CLOSING);
304 }
305 else {
Davide Pesavento1816d4b2017-07-02 12:20:48 -0400306 NFD_LOG_FACE_ERROR("Send or receive operation failed: " << error.message());
Davide Pesavento68ab43d2017-07-02 13:37:35 -0400307 this->setState(TransportState::FAILED);
308 }
Yukai Tu74c895d2015-09-21 01:11:51 -0700309 doClose();
310}
311
Weiwei Liudcdf6212016-08-31 14:34:22 -0700312template<class T>
313void
314StreamTransport<T>::resetReceiveBuffer()
315{
316 m_receiveBufferSize = 0;
317}
318
319template<class T>
320void
321StreamTransport<T>::resetSendQueue()
322{
323 std::queue<Block> emptyQueue;
324 std::swap(emptyQueue, m_sendQueue);
Eric Newberryb49313d2017-12-24 20:22:27 -0700325 m_sendQueueBytes = 0;
326}
327
328template<class T>
329size_t
330StreamTransport<T>::getSendQueueBytes() const
331{
332 return m_sendQueueBytes;
Weiwei Liudcdf6212016-08-31 14:34:22 -0700333}
334
Yukai Tu74c895d2015-09-21 01:11:51 -0700335} // namespace face
336} // namespace nfd
337
338#endif // NFD_DAEMON_FACE_STREAM_TRANSPORT_HPP