blob: a9768e5c480a4786fb64787bc7265eea0e82923e [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberryb49313d2017-12-24 20:22:27 -07002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -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_TRANSPORT_HPP
27#define NFD_DAEMON_FACE_TRANSPORT_HPP
28
Junxiao Shida93f1f2015-11-11 06:13:16 -070029#include "core/counter.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070030#include "face-log.hpp"
Eric Newberryb49313d2017-12-24 20:22:27 -070031
Junxiao Shi0de23a22015-12-03 20:07:02 +000032#include <ndn-cxx/encoding/nfd-constants.hpp>
Eric Newberrya98bf932015-09-21 00:58:47 -070033
34namespace nfd {
35namespace face {
36
Junxiao Shicde37ad2015-12-24 01:02:05 -070037class Face;
Eric Newberrya98bf932015-09-21 00:58:47 -070038class LinkService;
39
40/** \brief indicates the state of a transport
41 */
42enum class TransportState {
43 NONE,
Davide Pesavento68ab43d2017-07-02 13:37:35 -040044 UP, ///< the transport is up and can transmit packets
45 DOWN, ///< the transport is temporarily down, and is being recovered
46 CLOSING, ///< the transport is being closed gracefully, either by the peer or by a call to close()
Eric Newberrya98bf932015-09-21 00:58:47 -070047 FAILED, ///< the transport is being closed due to a failure
48 CLOSED ///< the transport is closed, and can be safely deallocated
49};
50
51std::ostream&
52operator<<(std::ostream& os, TransportState state);
53
Junxiao Shi57df2882015-11-11 06:12:35 -070054/** \brief counters provided by Transport
55 * \note The type name 'TransportCounters' is implementation detail.
56 * Use 'Transport::Counters' in public API.
57 */
58class TransportCounters
59{
60public:
61 /** \brief count of incoming packets
62 *
63 * A 'packet' typically means a top-level TLV block.
64 * For a datagram-based transport, an incoming packet that cannot be parsed as TLV
65 * would not be counted.
66 */
67 PacketCounter nInPackets;
68
69 /** \brief count of outgoing packets
70 *
71 * A 'packet' typically means a top-level TLV block.
72 * This counter is incremented only if transport is UP.
73 */
74 PacketCounter nOutPackets;
75
76 /** \brief total incoming bytes
77 *
78 * This counter includes headers imposed by NFD (such as NDNLP),
79 * but excludes overhead of underlying protocol (such as IP header).
80 * For a datagram-based transport, an incoming packet that cannot be parsed as TLV
81 * would not be counted.
82 */
83 ByteCounter nInBytes;
84
85 /** \brief total outgoing bytes
86 *
87 * This counter includes headers imposed by NFD (such as NDNLP),
88 * but excludes overhead of underlying protocol (such as IP header).
89 * This counter is increased only if transport is UP.
90 */
91 ByteCounter nOutBytes;
92};
93
Junxiao Shi13546112015-10-14 19:33:07 -070094/** \brief indicates the transport has no limit on payload size
95 */
96const ssize_t MTU_UNLIMITED = -1;
97
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070098/** \brief (for internal use) indicates MTU field is unset
99 */
100const ssize_t MTU_INVALID = -2;
101
Eric Newberryb49313d2017-12-24 20:22:27 -0700102/** \brief indicates that the transport does not support reading the queue capacity/length
103 */
104const ssize_t QUEUE_UNSUPPORTED = -1;
105
106/** \brief indicates that the transport was unable to retrieve the queue capacity/length
107 */
108const ssize_t QUEUE_ERROR = -2;
109
Junxiao Shicde37ad2015-12-24 01:02:05 -0700110/** \brief the lower part of a Face
111 * \sa Face
Eric Newberrya98bf932015-09-21 00:58:47 -0700112 */
Junxiao Shi57df2882015-11-11 06:12:35 -0700113class Transport : protected virtual TransportCounters, noncopyable
Eric Newberrya98bf932015-09-21 00:58:47 -0700114{
115public:
116 /** \brief identifies an endpoint on the link
117 */
118 typedef uint64_t EndpointId;
119
120 /** \brief stores a packet along with the remote endpoint
121 */
122 class Packet
123 {
124 public:
125 Packet() = default;
126
127 explicit
128 Packet(Block&& packet);
129
130 public:
131 /** \brief the packet as a TLV block
132 */
133 Block packet;
134
135 /** \brief identifies the remote endpoint
136 *
137 * This ID is only meaningful in the context of the same Transport.
138 * Incoming packets from the same remote endpoint have the same EndpointId,
139 * and incoming packets from different remote endpoints have different EndpointIds.
140 */
141 EndpointId remoteEndpoint;
142 };
143
Junxiao Shi57df2882015-11-11 06:12:35 -0700144 /** \brief counters provided by Transport
145 */
146 typedef TransportCounters Counters;
147
Junxiao Shi5b8a2b22015-10-22 17:20:44 -0700148 /** \brief constructor
149 *
150 * Transport constructor initializes static properties to invalid values.
151 * Subclass constructor must explicitly set every static property.
152 *
153 * This constructor initializes TransportState to UP;
154 * subclass constructor can rely on this default value.
155 */
Eric Newberrya98bf932015-09-21 00:58:47 -0700156 Transport();
157
158 virtual
159 ~Transport();
160
161public:
162 /** \brief set Face and LinkService for Transport
163 * \pre setFaceAndLinkService has not been called
164 */
165 void
Junxiao Shicde37ad2015-12-24 01:02:05 -0700166 setFaceAndLinkService(Face& face, LinkService& service);
Eric Newberrya98bf932015-09-21 00:58:47 -0700167
168 /** \return Face to which this Transport is attached
169 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700170 const Face*
Eric Newberrya98bf932015-09-21 00:58:47 -0700171 getFace() const;
172
173 /** \return LinkService to which this Transport is attached
174 */
175 const LinkService*
176 getLinkService() const;
177
178 /** \return LinkService to which this Transport is attached
179 */
180 LinkService*
181 getLinkService();
182
Junxiao Shi57df2882015-11-11 06:12:35 -0700183 virtual const Counters&
184 getCounters() const;
185
Eric Newberrya98bf932015-09-21 00:58:47 -0700186public: // upper interface
187 /** \brief request the transport to be closed
188 *
189 * This operation is effective only if transport is in UP or DOWN state,
190 * otherwise it has no effect.
191 * The transport changes state to CLOSING, and performs cleanup procedure.
192 * The state will be changed to CLOSED when cleanup is complete, which may
193 * happen synchronously or asynchronously.
194 */
195 void
196 close();
197
198 /** \brief send a link-layer packet
Junxiao Shi13546112015-10-14 19:33:07 -0700199 * \note This operation has no effect if \p getState() is neither UP nor DOWN
200 * \warning undefined behavior if packet size exceeds MTU limit
Eric Newberrya98bf932015-09-21 00:58:47 -0700201 */
202 void
203 send(Packet&& packet);
204
205protected: // upper interface to be invoked by subclass
206 /** \brief receive a link-layer packet
Junxiao Shi13546112015-10-14 19:33:07 -0700207 * \warning undefined behavior if packet size exceeds MTU limit
Eric Newberrya98bf932015-09-21 00:58:47 -0700208 */
209 void
210 receive(Packet&& packet);
211
212public: // static properties
213 /** \return a FaceUri representing local endpoint
214 */
215 FaceUri
216 getLocalUri() const;
217
218 /** \return a FaceUri representing remote endpoint
219 */
220 FaceUri
221 getRemoteUri() const;
222
223 /** \return whether face is local or non-local for scope control purpose
224 */
225 ndn::nfd::FaceScope
226 getScope() const;
227
228 /** \return face persistency setting
229 */
230 ndn::nfd::FacePersistency
231 getPersistency() const;
232
Davide Pesavento32065652017-01-15 01:52:21 -0500233 /** \brief check whether the face persistency can be changed to \p newPersistency
Yanbiao Li32dab972016-11-27 12:26:09 +0800234 *
Davide Pesavento32065652017-01-15 01:52:21 -0500235 * This function serves as the external API, and invokes the protected function
236 * canChangePersistencyToImpl to perform further checks if \p newPersistency differs
237 * from the current persistency.
Yanbiao Li32dab972016-11-27 12:26:09 +0800238 *
Davide Pesavento32065652017-01-15 01:52:21 -0500239 * \return true if the change can be performed, false otherwise
Yanbiao Li32dab972016-11-27 12:26:09 +0800240 */
241 bool
242 canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const;
243
Eric Newberrya98bf932015-09-21 00:58:47 -0700244 /** \brief changes face persistency setting
245 */
246 void
Davide Pesavento32065652017-01-15 01:52:21 -0500247 setPersistency(ndn::nfd::FacePersistency newPersistency);
Eric Newberrya98bf932015-09-21 00:58:47 -0700248
249 /** \return whether face is point-to-point or multi-access
250 */
251 ndn::nfd::LinkType
252 getLinkType() const;
253
Junxiao Shi13546112015-10-14 19:33:07 -0700254 /** \return maximum payload size
255 * \retval MTU_UNLIMITED transport has no limit on payload size
256 *
257 * This size is the maximum packet size that can be sent or received through this transport.
258 *
259 * For a datagram-based transport, this is typically the Maximum Transmission Unit (MTU),
260 * after the overhead of headers introduced by the transport has been accounted for.
261 * For a stream-based transport, this is typically unlimited (MTU_UNLIMITED).
262 */
263 ssize_t
264 getMtu() const;
265
Eric Newberryb49313d2017-12-24 20:22:27 -0700266 /** \return capacity of the send queue (in bytes)
267 * \retval QUEUE_UNSUPPORTED transport does not support queue capacity retrieval
268 * \retval QUEUE_ERROR transport was unable to retrieve the queue capacity
269 */
270 ssize_t
271 getSendQueueCapacity() const;
272
Eric Newberrya98bf932015-09-21 00:58:47 -0700273public: // dynamic properties
274 /** \return transport state
275 */
276 TransportState
277 getState() const;
278
279 /** \brief signals when transport state changes
280 */
281 signal::Signal<Transport, TransportState/*old*/, TransportState/*new*/> afterStateChange;
282
Eric Newberryc64d30a2015-12-26 11:07:27 -0700283 /** \return expiration time of the transport
284 * \retval time::steady_clock::TimePoint::max() the transport has indefinite lifetime
285 */
286 time::steady_clock::TimePoint
287 getExpirationTime() const;
288
Eric Newberryb49313d2017-12-24 20:22:27 -0700289 /** \return current send queue length of the transport (in octets)
290 * \retval QUEUE_UNSUPPORTED transport does not support queue length retrieval
291 * \retval QUEUE_ERROR transport was unable to retrieve the queue length
292 */
293 virtual ssize_t
294 getSendQueueLength();
295
Eric Newberrya98bf932015-09-21 00:58:47 -0700296protected: // properties to be set by subclass
297 void
298 setLocalUri(const FaceUri& uri);
299
300 void
301 setRemoteUri(const FaceUri& uri);
302
303 void
304 setScope(ndn::nfd::FaceScope scope);
305
306 void
307 setLinkType(ndn::nfd::LinkType linkType);
308
Junxiao Shi13546112015-10-14 19:33:07 -0700309 void
310 setMtu(ssize_t mtu);
311
Eric Newberryb49313d2017-12-24 20:22:27 -0700312 void
313 setSendQueueCapacity(ssize_t sendQueueCapacity);
314
Eric Newberrya98bf932015-09-21 00:58:47 -0700315 /** \brief set transport state
316 *
317 * Only the following transitions are valid:
318 * UP->DOWN, DOWN->UP, UP/DOWN->CLOSING/FAILED, CLOSING/FAILED->CLOSED
319 *
320 * \throw std::runtime_error transition is invalid.
321 */
322 void
323 setState(TransportState newState);
324
Eric Newberryc64d30a2015-12-26 11:07:27 -0700325 void
326 setExpirationTime(const time::steady_clock::TimePoint& expirationTime);
327
Eric Newberrya98bf932015-09-21 00:58:47 -0700328protected: // to be overridden by subclass
Yanbiao Li32dab972016-11-27 12:26:09 +0800329 /** \brief invoked by canChangePersistencyTo to perform the check
330 *
331 * Base class implementation returns false.
332 *
333 * \param newPersistency the new persistency, guaranteed to be different from current persistency
334 */
335 virtual bool
336 canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const;
337
338 /** \brief invoked after the persistency has been changed
339 *
340 * The base class implementation does nothing.
341 * When overridden in a subclass, the function should update internal states
342 * after persistency setting has been changed.
Eric Newberrya98bf932015-09-21 00:58:47 -0700343 */
344 virtual void
Yanbiao Li32dab972016-11-27 12:26:09 +0800345 afterChangePersistency(ndn::nfd::FacePersistency oldPersistency);
Eric Newberrya98bf932015-09-21 00:58:47 -0700346
347 /** \brief performs Transport specific operations to close the transport
348 *
Junxiao Shi13546112015-10-14 19:33:07 -0700349 * This is invoked once by \p close() after changing state to CLOSING.
350 * It will not be invoked by Transport class if the transport is already CLOSING or CLOSED.
351 *
Eric Newberrya98bf932015-09-21 00:58:47 -0700352 * When the cleanup procedure is complete, this method should change state to CLOSED.
Junxiao Shi13546112015-10-14 19:33:07 -0700353 * This transition can happen synchronously or asynchronously.
Eric Newberrya98bf932015-09-21 00:58:47 -0700354 */
355 virtual void
356 doClose() = 0;
357
358private: // to be overridden by subclass
359 /** \brief performs Transport specific operations to send a packet
360 * \param packet the packet, which must be a well-formed TLV block
Junxiao Shi13546112015-10-14 19:33:07 -0700361 * \pre state is either UP or DOWN
Eric Newberrya98bf932015-09-21 00:58:47 -0700362 */
363 virtual void
364 doSend(Packet&& packet) = 0;
365
366private:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700367 Face* m_face;
Eric Newberrya98bf932015-09-21 00:58:47 -0700368 LinkService* m_service;
369 FaceUri m_localUri;
370 FaceUri m_remoteUri;
371 ndn::nfd::FaceScope m_scope;
372 ndn::nfd::FacePersistency m_persistency;
373 ndn::nfd::LinkType m_linkType;
Junxiao Shi13546112015-10-14 19:33:07 -0700374 ssize_t m_mtu;
Eric Newberryb49313d2017-12-24 20:22:27 -0700375 size_t m_sendQueueCapacity;
Eric Newberrya98bf932015-09-21 00:58:47 -0700376 TransportState m_state;
Eric Newberryc64d30a2015-12-26 11:07:27 -0700377 time::steady_clock::TimePoint m_expirationTime;
Eric Newberrya98bf932015-09-21 00:58:47 -0700378};
379
Junxiao Shicde37ad2015-12-24 01:02:05 -0700380inline const Face*
Eric Newberrya98bf932015-09-21 00:58:47 -0700381Transport::getFace() const
382{
383 return m_face;
384}
385
386inline const LinkService*
387Transport::getLinkService() const
388{
389 return m_service;
390}
391
392inline LinkService*
393Transport::getLinkService()
394{
395 return m_service;
396}
397
Junxiao Shi57df2882015-11-11 06:12:35 -0700398inline const Transport::Counters&
399Transport::getCounters() const
400{
401 return *this;
402}
403
Eric Newberrya98bf932015-09-21 00:58:47 -0700404inline FaceUri
405Transport::getLocalUri() const
406{
407 return m_localUri;
408}
409
Junxiao Shi13546112015-10-14 19:33:07 -0700410inline void
411Transport::setLocalUri(const FaceUri& uri)
412{
413 m_localUri = uri;
414}
415
Eric Newberrya98bf932015-09-21 00:58:47 -0700416inline FaceUri
417Transport::getRemoteUri() const
418{
419 return m_remoteUri;
420}
421
Junxiao Shi13546112015-10-14 19:33:07 -0700422inline void
423Transport::setRemoteUri(const FaceUri& uri)
424{
425 m_remoteUri = uri;
426}
427
Eric Newberrya98bf932015-09-21 00:58:47 -0700428inline ndn::nfd::FaceScope
429Transport::getScope() const
430{
431 return m_scope;
432}
433
Junxiao Shi13546112015-10-14 19:33:07 -0700434inline void
435Transport::setScope(ndn::nfd::FaceScope scope)
436{
437 m_scope = scope;
438}
439
Eric Newberrya98bf932015-09-21 00:58:47 -0700440inline ndn::nfd::FacePersistency
441Transport::getPersistency() const
442{
443 return m_persistency;
444}
445
Eric Newberrya98bf932015-09-21 00:58:47 -0700446inline ndn::nfd::LinkType
447Transport::getLinkType() const
448{
449 return m_linkType;
450}
451
Eric Newberrya98bf932015-09-21 00:58:47 -0700452inline void
453Transport::setLinkType(ndn::nfd::LinkType linkType)
454{
455 m_linkType = linkType;
456}
457
Junxiao Shi13546112015-10-14 19:33:07 -0700458inline ssize_t
459Transport::getMtu() const
460{
461 return m_mtu;
462}
463
464inline void
465Transport::setMtu(ssize_t mtu)
466{
467 BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu > 0);
468 m_mtu = mtu;
469}
470
Eric Newberryb49313d2017-12-24 20:22:27 -0700471inline ssize_t
472Transport::getSendQueueCapacity() const
473{
474 return m_sendQueueCapacity;
475}
476
477inline void
478Transport::setSendQueueCapacity(ssize_t sendQueueCapacity)
479{
480 m_sendQueueCapacity = sendQueueCapacity;
481}
482
Junxiao Shi13546112015-10-14 19:33:07 -0700483inline TransportState
484Transport::getState() const
485{
486 return m_state;
487}
488
Eric Newberryc64d30a2015-12-26 11:07:27 -0700489inline time::steady_clock::TimePoint
490Transport::getExpirationTime() const
491{
492 return m_expirationTime;
493}
494
495inline void
496Transport::setExpirationTime(const time::steady_clock::TimePoint& expirationTime)
497{
498 m_expirationTime = expirationTime;
499}
500
Eric Newberrya98bf932015-09-21 00:58:47 -0700501std::ostream&
502operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh);
503
504template<typename T>
505typename std::enable_if<std::is_base_of<Transport, T>::value &&
506 !std::is_same<Transport, T>::value, std::ostream&>::type
507operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
508{
509 return os << FaceLogHelper<Transport>(flh.obj);
510}
511
512} // namespace face
513} // namespace nfd
514
515#endif // NFD_DAEMON_FACE_TRANSPORT_HPP