NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019, 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 
33 
34 const ssize_t Transport::MIN_MTU;
35 
36 std::ostream&
37 operator<<(std::ostream& os, TransportState state)
38 {
39  switch (state) {
40  case TransportState::UP:
41  return os << "UP";
43  return os << "DOWN";
45  return os << "CLOSING";
47  return os << "FAILED";
49  return os << "CLOSED";
50  default:
51  return os << "NONE";
52  }
53 }
54 
56  : m_face(nullptr)
57  , m_service(nullptr)
58  , m_scope(ndn::nfd::FACE_SCOPE_NONE)
59  , m_persistency(ndn::nfd::FACE_PERSISTENCY_NONE)
60  , m_linkType(ndn::nfd::LINK_TYPE_NONE)
61  , m_mtu(MTU_INVALID)
62  , m_sendQueueCapacity(QUEUE_UNSUPPORTED)
63  , m_state(TransportState::UP)
64  , m_expirationTime(time::steady_clock::TimePoint::max())
65 {
66 }
67 
68 Transport::~Transport() = default;
69 
70 void
72 {
73  BOOST_ASSERT(m_face == nullptr);
74  BOOST_ASSERT(m_service == nullptr);
75 
76  m_face = &face;
77  m_service = &service;
78 }
79 
80 void
82 {
83  if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
84  return;
85  }
86 
88  this->doClose();
89  // warning: don't access any members after this:
90  // the Transport may be deallocated if doClose changes state to CLOSED
91 }
92 
93 void
94 Transport::send(const Block& packet, const EndpointId& endpoint)
95 {
96  BOOST_ASSERT(packet.isValid());
97  BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
98  packet.size() <= static_cast<size_t>(this->getMtu()));
99 
100  TransportState state = this->getState();
101  if (state != TransportState::UP && state != TransportState::DOWN) {
102  NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
103  return;
104  }
105 
106  if (state == TransportState::UP) {
107  ++this->nOutPackets;
108  this->nOutBytes += packet.size();
109  }
110 
111  this->doSend(packet, endpoint);
112 }
113 
114 void
115 Transport::receive(const Block& packet, const EndpointId& endpoint)
116 {
117  BOOST_ASSERT(packet.isValid());
118  BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
119  packet.size() <= static_cast<size_t>(this->getMtu()));
120 
121  ++this->nInPackets;
122  this->nInBytes += packet.size();
123 
124  m_service->receivePacket(packet, endpoint);
125 }
126 
127 bool
129 {
130  // not changing, or setting initial persistency in subclass constructor
131  if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
132  return true;
133  }
134 
135  if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
136  NFD_LOG_FACE_TRACE("cannot change persistency to NONE");
137  return false;
138  }
139 
140  return this->canChangePersistencyToImpl(newPersistency);
141 }
142 
143 bool
145 {
146  return false;
147 }
148 
149 void
151 {
152  BOOST_ASSERT(canChangePersistencyTo(newPersistency));
153 
154  if (m_persistency == newPersistency) {
155  return;
156  }
157 
158  auto oldPersistency = m_persistency;
159  m_persistency = newPersistency;
160 
161  if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
162  NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency);
163  this->afterChangePersistency(oldPersistency);
164  }
165 }
166 
167 void
169 {
170 }
171 
172 void
174 {
175  if (m_state == newState) {
176  return;
177  }
178 
179  bool isValid = false;
180  switch (m_state) {
181  case TransportState::UP:
182  isValid = newState == TransportState::DOWN ||
183  newState == TransportState::CLOSING ||
184  newState == TransportState::FAILED;
185  break;
187  isValid = newState == TransportState::UP ||
188  newState == TransportState::CLOSING ||
189  newState == TransportState::FAILED;
190  break;
193  isValid = newState == TransportState::CLOSED;
194  break;
195  default:
196  break;
197  }
198 
199  if (!isValid) {
200  NDN_THROW(std::runtime_error("Invalid state transition"));
201  }
202 
203  NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
204 
205  TransportState oldState = m_state;
206  m_state = newState;
207  afterStateChange(oldState, newState);
208  // warning: don't access any members after this:
209  // the Transport may be deallocated in the signal handler if newState is CLOSED
210 }
211 
212 std::ostream&
213 operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
214 {
215  const Transport& transport = flh.obj;
216  const Face* face = transport.getFace();
217  FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
218 
219  os << "[id=" << faceId << ",local=" << transport.getLocalUri()
220  << ",remote=" << transport.getRemoteUri() << "] ";
221  return os;
222 }
223 
224 } // namespace face
225 } // namespace nfd
nfd::face::TransportState
TransportState
Indicates the state of a transport.
Definition: transport.hpp:37
ndn::nfd::LINK_TYPE_NONE
@ LINK_TYPE_NONE
Definition: nfd-constants.hpp:58
nfd::face::MTU_INVALID
const ssize_t MTU_INVALID
(for internal use) indicates MTU field is unset
Definition: transport.hpp:95
nfd::face::TransportState::CLOSED
@ CLOSED
the transport is closed, and can be safely deallocated
nfd::face::Transport::canChangePersistencyTo
bool canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
check whether the face persistency can be changed to newPersistency
Definition: transport.cpp:128
nfd::face::MTU_UNLIMITED
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
Definition: transport.hpp:91
nfd::face::Transport::getRemoteUri
FaceUri getRemoteUri() const
Definition: transport.hpp:400
nfd::face::Transport::~Transport
virtual ~Transport()
nfd::face::Transport::doClose
virtual void doClose()=0
performs Transport specific operations to close the transport
nfd::face::Transport::setState
void setState(TransportState newState)
set transport state
Definition: transport.cpp:173
nfd::face::TransportState::DOWN
@ DOWN
the transport is temporarily down, and is being recovered
transport.hpp
ndn::Block::isValid
bool isValid() const noexcept
Check if the Block is valid.
Definition: block.hpp:188
ndn::nfd::FacePersistency
FacePersistency
Definition: nfd-constants.hpp:45
nfd::face::TransportState::CLOSING
@ CLOSING
the transport is being closed gracefully, either by the peer or by a call to close()
nfd::face::Transport::Transport
Transport()
Default constructor.
Definition: transport.cpp:55
nfd::face::Transport::setFaceAndLinkService
void setFaceAndLinkService(Face &face, LinkService &service)
set Face and LinkService for Transport
Definition: transport.cpp:71
nfd::face::operator<<
std::ostream & operator<<(std::ostream &os, const Face &face)
Definition: ndn-common.hpp:87
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::face::LinkService
the upper part of a Face
Definition: link-service.hpp:76
nfd::face::INVALID_FACEID
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face-common.hpp:47
nfd::face::TransportCounters::nOutPackets
PacketCounter nOutPackets
count of outgoing packets
Definition: transport.hpp:69
nfd::face::Transport::afterChangePersistency
virtual void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
invoked after the persistency has been changed
Definition: transport.cpp:168
ndn::nfd::FACE_SCOPE_NONE
@ FACE_SCOPE_NONE
Definition: nfd-constants.hpp:35
nfd::face::FaceId
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44
nfd::face::Face
generalization of a network interface
Definition: face.hpp:53
ndn::nfd::FACE_PERSISTENCY_NONE
@ FACE_PERSISTENCY_NONE
Definition: nfd-constants.hpp:46
ndn::time
Definition: time-custom-clock.hpp:28
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
NFD_LOG_FACE_TRACE
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
Definition: face-common.hpp:133
nfd::face::Transport::send
void send(const Block &packet, const EndpointId &endpoint=0)
Send a link-layer packet.
Definition: transport.cpp:94
nfd::face::TransportCounters::nOutBytes
ByteCounter nOutBytes
total outgoing bytes
Definition: transport.hpp:86
nfd::face::QUEUE_UNSUPPORTED
const ssize_t QUEUE_UNSUPPORTED
indicates that the transport does not support reading the queue capacity/length
Definition: transport.hpp:99
nfd::face::Face::getId
FaceId getId() const
Definition: face.hpp:214
nfd::face::Transport::getFace
const Face * getFace() const
Definition: transport.hpp:364
nfd::face::Transport::canChangePersistencyToImpl
virtual bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
invoked by canChangePersistencyTo to perform the check
Definition: transport.cpp:144
nfd::face::TransportState::UP
@ UP
the transport is up and can transmit packets
nfd::face::Transport::getLocalUri
FaceUri getLocalUri() const
Definition: transport.hpp:388
face.hpp
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
nfd::face::Transport::setPersistency
void setPersistency(ndn::nfd::FacePersistency newPersistency)
changes face persistency setting
Definition: transport.cpp:150
ndn::Block::size
size_t size() const
Return the size of the encoded wire, i.e.
Definition: block.cpp:290
NFD_LOG_FACE_INFO
#define NFD_LOG_FACE_INFO(msg)
Log a message at INFO level.
Definition: face-common.hpp:139
nfd::face::TransportCounters::nInBytes
ByteCounter nInBytes
total incoming bytes
Definition: transport.hpp:78
nfd::face::TransportState::FAILED
@ FAILED
the transport is being closed due to a failure
nfd::face::TransportCounters::nInPackets
PacketCounter nInPackets
count of incoming packets
Definition: transport.hpp:62
nfd::face::Transport::getState
TransportState getState() const
Definition: transport.hpp:467
nfd::face::Transport::MIN_MTU
static constexpr ssize_t MIN_MTU
minimum MTU that may be set on a transport
Definition: transport.hpp:347
nfd::face::EndpointId
uint64_t EndpointId
Identifies a remote endpoint on the link.
Definition: face-common.hpp:65
nfd::face::Transport::receive
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:115
nfd::face::Transport::close
void close()
Request the transport to be closed.
Definition: transport.cpp:81
nfd::face::LinkService::receivePacket
void receivePacket(const Block &packet, const EndpointId &endpoint)
performs LinkService specific operations to receive a lower-layer packet
Definition: link-service.hpp:241
nfd::face::FaceLogHelper
For internal use by FaceLogging macros.
Definition: face-common.hpp:93
nfd::face::Transport
The lower half of a Face.
Definition: transport.hpp:109
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
nfd::face::Transport::getMtu
ssize_t getMtu() const
Definition: transport.hpp:442
nfd::face::FaceLogHelper::obj
const T & obj
Definition: face-common.hpp:102
nfd::face::Transport::afterStateChange
signal::Signal< Transport, TransportState, TransportState > afterStateChange
signals when transport state changes
Definition: transport.hpp:244