transport.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2020, 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 #include "transport.hpp"
27 #include "face.hpp"
28 
29 namespace nfd {
30 namespace face {
31 
32 NFD_LOG_INIT(Transport);
33 
34 std::ostream&
35 operator<<(std::ostream& os, TransportState state)
36 {
37  switch (state) {
38  case TransportState::UP:
39  return os << "UP";
41  return os << "DOWN";
43  return os << "CLOSING";
45  return os << "FAILED";
47  return os << "CLOSED";
48  default:
49  return os << "NONE";
50  }
51 }
52 
54  : m_face(nullptr)
55  , m_service(nullptr)
56  , m_scope(ndn::nfd::FACE_SCOPE_NONE)
57  , m_persistency(ndn::nfd::FACE_PERSISTENCY_NONE)
58  , m_linkType(ndn::nfd::LINK_TYPE_NONE)
59  , m_mtu(MTU_INVALID)
60  , m_sendQueueCapacity(QUEUE_UNSUPPORTED)
61  , m_state(TransportState::UP)
62  , m_expirationTime(time::steady_clock::TimePoint::max())
63 {
64 }
65 
66 Transport::~Transport() = default;
67 
68 void
70 {
71  BOOST_ASSERT(m_face == nullptr);
72  BOOST_ASSERT(m_service == nullptr);
73 
74  m_face = &face;
75  m_service = &service;
76 }
77 
78 void
80 {
81  if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
82  return;
83  }
84 
86  this->doClose();
87  // warning: don't access any members after this:
88  // the Transport may be deallocated if doClose changes state to CLOSED
89 }
90 
91 void
92 Transport::send(const Block& packet)
93 {
94  BOOST_ASSERT(packet.isValid());
95  BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
96  packet.size() <= static_cast<size_t>(this->getMtu()));
97 
98  TransportState state = this->getState();
99  if (state != TransportState::UP && state != TransportState::DOWN) {
100  NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
101  return;
102  }
103 
104  if (state == TransportState::UP) {
105  ++this->nOutPackets;
106  this->nOutBytes += packet.size();
107  }
108 
109  this->doSend(packet);
110 }
111 
112 void
113 Transport::receive(const Block& packet, const EndpointId& endpoint)
114 {
115  BOOST_ASSERT(packet.isValid());
116  BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
117  packet.size() <= static_cast<size_t>(this->getMtu()));
118 
119  ++this->nInPackets;
120  this->nInBytes += packet.size();
121 
122  m_service->receivePacket(packet, endpoint);
123 }
124 
125 void
126 Transport::setMtu(ssize_t mtu)
127 {
128  BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu >= 0);
129 
130  if (mtu == m_mtu) {
131  return;
132  }
133 
134  if (m_mtu != MTU_INVALID) {
135  NFD_LOG_FACE_INFO("setMtu " << m_mtu << " -> " << mtu);
136  }
137 
138  m_mtu = mtu;
139 }
140 
141 bool
142 Transport::canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
143 {
144  // not changing, or setting initial persistency in subclass constructor
145  if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
146  return true;
147  }
148 
149  if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
150  NFD_LOG_FACE_TRACE("cannot change persistency to NONE");
151  return false;
152  }
153 
154  return this->canChangePersistencyToImpl(newPersistency);
155 }
156 
157 bool
158 Transport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
159 {
160  return false;
161 }
162 
163 void
164 Transport::setPersistency(ndn::nfd::FacePersistency newPersistency)
165 {
166  BOOST_ASSERT(canChangePersistencyTo(newPersistency));
167 
168  if (m_persistency == newPersistency) {
169  return;
170  }
171 
172  auto oldPersistency = m_persistency;
173  m_persistency = newPersistency;
174 
175  if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
176  NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency);
177  this->afterChangePersistency(oldPersistency);
178  }
179 }
180 
181 void
182 Transport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
183 {
184 }
185 
186 void
188 {
189  if (m_state == newState) {
190  return;
191  }
192 
193  bool isValid = false;
194  switch (m_state) {
195  case TransportState::UP:
196  isValid = newState == TransportState::DOWN ||
197  newState == TransportState::CLOSING ||
198  newState == TransportState::FAILED;
199  break;
201  isValid = newState == TransportState::UP ||
202  newState == TransportState::CLOSING ||
203  newState == TransportState::FAILED;
204  break;
207  isValid = newState == TransportState::CLOSED;
208  break;
209  default:
210  break;
211  }
212 
213  if (!isValid) {
214  NDN_THROW(std::runtime_error("Invalid state transition"));
215  }
216 
217  NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
218 
219  TransportState oldState = m_state;
220  m_state = newState;
221  afterStateChange(oldState, newState);
222  // warning: don't access any members after this:
223  // the Transport may be deallocated in the signal handler if newState is CLOSED
224 }
225 
226 std::ostream&
227 operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
228 {
229  const Transport& transport = flh.obj;
230  const Face* face = transport.getFace();
231  FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
232 
233  os << "[id=" << faceId << ",local=" << transport.getLocalUri()
234  << ",remote=" << transport.getRemoteUri() << "] ";
235  return os;
236 }
237 
238 } // namespace face
239 } // namespace nfd
const ssize_t QUEUE_UNSUPPORTED
indicates that the transport does not support reading the queue capacity/length
Definition: transport.hpp:99
virtual void doClose()=0
performs Transport specific operations to close the transport
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...
Definition: dns-srv.cpp:41
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
void send(const Block &packet)
Send a link-layer packet.
Definition: transport.cpp:92
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:91
TransportState
Indicates the state of a transport.
Definition: transport.hpp:37
void setMtu(ssize_t mtu)
Definition: transport.cpp:126
const ssize_t MTU_INVALID
(for internal use) indicates MTU field is unset
Definition: transport.hpp:95
bool canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
check whether the face persistency can be changed to newPersistency
Definition: transport.cpp:142
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
Definition: face.cpp:47
signal::Signal< Transport, TransportState, TransportState > afterStateChange
signals when transport state changes
Definition: transport.hpp:243
The lower half of a Face.
Definition: transport.hpp:108
FaceUri getRemoteUri() const
Definition: transport.hpp:391
uint64_t EndpointId
Identifies a remote endpoint on the link.
Definition: face-common.hpp:71
ByteCounter nInBytes
total incoming bytes
Definition: transport.hpp:78
the transport is being closed due to a failure
TransportState getState() const
Definition: transport.hpp:451
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
the transport is closed, and can be safely deallocated
virtual void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
invoked after the persistency has been changed
Definition: transport.cpp:182
PacketCounter nInPackets
count of incoming packets
Definition: transport.hpp:62
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
const Face * getFace() const
Definition: transport.hpp:355
ssize_t getMtu() const
Definition: transport.hpp:433
generalization of a network interface
Definition: face.hpp:54
the transport is being closed gracefully, either by the peer or by a call to close() ...
PacketCounter nOutPackets
count of outgoing packets
Definition: transport.hpp:69
FaceId getId() const
Definition: face.hpp:239
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:164
void close()
Request the transport to be closed.
Definition: transport.cpp:79
FaceUri getLocalUri() const
Definition: transport.hpp:379
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
void receive(const Block &packet, const EndpointId &endpoint=0)
Pass a received link-layer packet to the upper layer for further processing.
Definition: transport.cpp:113
void setState(TransportState newState)
set transport state
Definition: transport.cpp:187
the transport is up and can transmit packets
ByteCounter nOutBytes
total outgoing bytes
Definition: transport.hpp:86
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face-common.hpp:47
void setFaceAndLinkService(Face &face, LinkService &service)
set Face and LinkService for Transport
Definition: transport.cpp:69
Transport()
Default constructor.
Definition: transport.cpp:53
the transport is temporarily down, and is being recovered
virtual bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
invoked by canChangePersistencyTo to perform the check
Definition: transport.cpp:158