transport.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, Regents of the University of California,
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_TRANSPORT_HPP
27 #define NFD_DAEMON_FACE_TRANSPORT_HPP
28 
29 #include "face-common.hpp"
30 #include "common/counter.hpp"
31 
32 namespace nfd::face {
33 
37 enum class TransportState {
38  NONE,
39  UP,
40  DOWN,
41  CLOSING,
42  FAILED,
43  CLOSED
44 };
45 
46 std::ostream&
47 operator<<(std::ostream& os, TransportState state);
48 
55 {
56 public:
66 
75 
86 
96 };
97 
101 inline constexpr ssize_t MTU_UNLIMITED = -1;
102 
106 inline constexpr ssize_t MTU_INVALID = -2;
107 
111 inline constexpr ssize_t QUEUE_UNSUPPORTED = -1;
112 
116 inline constexpr ssize_t QUEUE_ERROR = -2;
117 
122 class Transport : protected virtual TransportCounters, noncopyable
123 {
124 public:
129 
139 
140  virtual
142 
143 public:
148  void
149  setFaceAndLinkService(Face& face, LinkService& service) noexcept;
150 
154  const Face*
155  getFace() const noexcept
156  {
157  return m_face;
158  }
159 
163  const LinkService*
164  getLinkService() const noexcept
165  {
166  return m_service;
167  }
168 
172  LinkService*
173  getLinkService() noexcept
174  {
175  return m_service;
176  }
177 
178  virtual const Counters&
179  getCounters() const
180  {
181  return *this;
182  }
183 
184 public: // upper interface
193  void
194  close();
195 
201  void
202  send(const Block& packet);
203 
204 public: // static properties
208  FaceUri
209  getLocalUri() const noexcept
210  {
211  return m_localUri;
212  }
213 
217  FaceUri
218  getRemoteUri() const noexcept
219  {
220  return m_remoteUri;
221  }
222 
226  ndn::nfd::FaceScope
227  getScope() const noexcept
228  {
229  return m_scope;
230  }
231 
235  ndn::nfd::FacePersistency
236  getPersistency() const noexcept
237  {
238  return m_persistency;
239  }
240 
250  bool
251  canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const;
252 
256  void
257  setPersistency(ndn::nfd::FacePersistency newPersistency);
258 
262  ndn::nfd::LinkType
263  getLinkType() const noexcept
264  {
265  return m_linkType;
266  }
267 
278  ssize_t
279  getMtu() const noexcept
280  {
281  return m_mtu;
282  }
283 
289  ssize_t
290  getSendQueueCapacity() const noexcept
291  {
292  return m_sendQueueCapacity;
293  }
294 
295 public: // dynamic properties
300  getState() const noexcept
301  {
302  return m_state;
303  }
304 
308  signal::Signal<Transport, TransportState /*old*/, TransportState /*new*/> afterStateChange;
309 
314  time::steady_clock::time_point
315  getExpirationTime() const noexcept
316  {
317  return m_expirationTime;
318  }
319 
325  virtual ssize_t
327  {
328  return QUEUE_UNSUPPORTED;
329  }
330 
331 protected: // upper interface to be invoked by subclass
338  void
339  receive(const Block& packet, const EndpointId& endpoint = {});
340 
341 protected: // properties to be set by subclass
342  void
343  setLocalUri(const FaceUri& uri) noexcept
344  {
345  m_localUri = uri;
346  }
347 
348  void
349  setRemoteUri(const FaceUri& uri) noexcept
350  {
351  m_remoteUri = uri;
352  }
353 
354  void
355  setScope(ndn::nfd::FaceScope scope) noexcept
356  {
357  m_scope = scope;
358  }
359 
360  void
361  setLinkType(ndn::nfd::LinkType linkType) noexcept
362  {
363  m_linkType = linkType;
364  }
365 
366  void
367  setMtu(ssize_t mtu) noexcept;
368 
369  void
370  setSendQueueCapacity(ssize_t sendQueueCapacity) noexcept
371  {
372  m_sendQueueCapacity = sendQueueCapacity;
373  }
374 
382  void
383  setState(TransportState newState);
384 
385  void
386  setExpirationTime(const time::steady_clock::time_point& expirationTime) noexcept
387  {
388  m_expirationTime = expirationTime;
389  }
390 
391 protected: // to be overridden by subclass
398  virtual bool
399  canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const;
400 
407  virtual void
408  afterChangePersistency(ndn::nfd::FacePersistency oldPersistency);
409 
418  virtual void
419  doClose() = 0;
420 
421 private: // to be overridden by subclass
426  virtual void
427  doSend(const Block& packet) = 0;
428 
429 private:
430  Face* m_face = nullptr;
431  LinkService* m_service = nullptr;
432  FaceUri m_localUri;
433  FaceUri m_remoteUri;
434  ndn::nfd::FaceScope m_scope = ndn::nfd::FACE_SCOPE_NONE;
435  ndn::nfd::FacePersistency m_persistency = ndn::nfd::FACE_PERSISTENCY_NONE;
436  ndn::nfd::LinkType m_linkType = ndn::nfd::LINK_TYPE_NONE;
437  ssize_t m_mtu = MTU_INVALID;
438  ssize_t m_sendQueueCapacity = QUEUE_UNSUPPORTED;
440  time::steady_clock::time_point m_expirationTime = time::steady_clock::time_point::max();
441 };
442 
443 std::ostream&
444 operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh);
445 
446 template<typename T>
447 std::enable_if_t<std::is_base_of_v<Transport, T> && !std::is_same_v<Transport, T>,
448  std::ostream&>
449 operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
450 {
451  return os << FaceLogHelper<Transport>(flh.obj);
452 }
453 
454 } // namespace nfd::face
455 
456 #endif // NFD_DAEMON_FACE_TRANSPORT_HPP
Represents a counter of number of bytes.
Definition: counter.hpp:90
Represents a counter of number of packets.
Definition: counter.hpp:71
Generalization of a network interface.
Definition: face.hpp:118
For internal use by FaceLogging macros.
Counters provided by a transport.
Definition: transport.hpp:55
ByteCounter nInBytes
Total bytes received.
Definition: transport.hpp:85
PacketCounter nInPackets
Count of incoming packets.
Definition: transport.hpp:65
PacketCounter nOutPackets
Count of outgoing packets.
Definition: transport.hpp:74
ByteCounter nOutBytes
Total bytes sent.
Definition: transport.hpp:95
The lower half of a Face.
Definition: transport.hpp:123
const LinkService * getLinkService() const noexcept
Returns the LinkService to which this transport is attached.
Definition: transport.hpp:164
void setScope(ndn::nfd::FaceScope scope) noexcept
Definition: transport.hpp:355
void receive(const Block &packet, const EndpointId &endpoint={})
Pass a received link-layer packet to the upper layer for further processing.
Definition: transport.cpp:101
void setPersistency(ndn::nfd::FacePersistency newPersistency)
Changes the persistency setting of the transport.
Definition: transport.cpp:152
ssize_t getMtu() const noexcept
Returns the maximum payload size.
Definition: transport.hpp:279
FaceUri getLocalUri() const noexcept
Returns a FaceUri representing the local endpoint.
Definition: transport.hpp:209
ndn::nfd::FacePersistency getPersistency() const noexcept
Returns the current persistency setting of the transport.
Definition: transport.hpp:236
virtual void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
Invoked after the persistency has been changed.
Definition: transport.cpp:170
ndn::nfd::FaceScope getScope() const noexcept
Returns whether the transport is local or non-local for scope control purposes.
Definition: transport.hpp:227
LinkService * getLinkService() noexcept
Returns the LinkService to which this transport is attached.
Definition: transport.hpp:173
void setMtu(ssize_t mtu) noexcept
Definition: transport.cpp:114
void send(const Block &packet)
Send a link-layer packet.
Definition: transport.cpp:80
void setFaceAndLinkService(Face &face, LinkService &service) noexcept
Set Face and LinkService for this transport.
Definition: transport.cpp:57
signal::Signal< Transport, TransportState, TransportState > afterStateChange
Called when the transport state changes.
Definition: transport.hpp:308
const Face * getFace() const noexcept
Returns the Face to which this transport is attached.
Definition: transport.hpp:155
TransportState getState() const noexcept
Returns the current transport state.
Definition: transport.hpp:300
virtual void doClose()=0
Performs Transport specific operations to close the transport.
bool canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
Check whether the persistency can be changed to newPersistency.
Definition: transport.cpp:130
virtual const Counters & getCounters() const
Definition: transport.hpp:179
void setExpirationTime(const time::steady_clock::time_point &expirationTime) noexcept
Definition: transport.hpp:386
TransportCounters Counters
Counters provided by a transport.
Definition: transport.hpp:128
FaceUri getRemoteUri() const noexcept
Returns a FaceUri representing the remote endpoint.
Definition: transport.hpp:218
Transport()
Default constructor.
virtual bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
Invoked by canChangePersistencyTo to perform the check.
Definition: transport.cpp:146
void setSendQueueCapacity(ssize_t sendQueueCapacity) noexcept
Definition: transport.hpp:370
void setState(TransportState newState)
Set transport state.
Definition: transport.cpp:175
void setLocalUri(const FaceUri &uri) noexcept
Definition: transport.hpp:343
time::steady_clock::time_point getExpirationTime() const noexcept
Returns the expiration time of the transport.
Definition: transport.hpp:315
ssize_t getSendQueueCapacity() const noexcept
Returns the capacity of the send queue (in bytes).
Definition: transport.hpp:290
void setLinkType(ndn::nfd::LinkType linkType) noexcept
Definition: transport.hpp:361
void setRemoteUri(const FaceUri &uri) noexcept
Definition: transport.hpp:349
ndn::nfd::LinkType getLinkType() const noexcept
Returns the link type of the transport.
Definition: transport.hpp:263
virtual ssize_t getSendQueueLength()
Returns the current send queue length of the transport (in octets).
Definition: transport.hpp:326
void close()
Request the transport to be closed.
Definition: transport.cpp:67
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
Definition: face.cpp:63
constexpr ssize_t QUEUE_UNSUPPORTED
Indicates that the transport does not support reading the queue capacity/length.
Definition: transport.hpp:111
TransportState
Indicates the state of a transport.
Definition: transport.hpp:37
@ CLOSED
the transport is closed, and can be safely deallocated
@ CLOSING
the transport is being closed gracefully, either by the peer or by a call to close()
@ FAILED
the transport is being closed due to a failure
@ DOWN
the transport is temporarily down, and is being recovered
@ UP
the transport is up and can transmit packets
constexpr ssize_t MTU_INVALID
(for internal use) Indicates that the MTU field is unset.
Definition: transport.hpp:106
std::variant< std::monostate, ethernet::Address, udp::Endpoint > EndpointId
Identifies a remote endpoint on the link.
Definition: face-common.hpp:78
constexpr ssize_t QUEUE_ERROR
Indicates that the transport was unable to retrieve the queue capacity/length.
Definition: transport.hpp:116
constexpr ssize_t MTU_UNLIMITED
Indicates that the transport has no limit on payload size.
Definition: transport.hpp:101