blob: 467843e83cf1c3ecab7f0e13330f22b79fec1e55 [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/*
Eric Newberryf3ee8082020-01-28 13:44:18 -08003 * Copyright (c) 2014-2020, 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#include "transport.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070027#include "face.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070028
29namespace nfd {
30namespace face {
31
Davide Pesaventoa3148082018-04-12 18:21:54 -040032NFD_LOG_INIT(Transport);
Eric Newberrya98bf932015-09-21 00:58:47 -070033
34std::ostream&
35operator<<(std::ostream& os, TransportState state)
36{
37 switch (state) {
38 case TransportState::UP:
39 return os << "UP";
40 case TransportState::DOWN:
41 return os << "DOWN";
42 case TransportState::CLOSING:
43 return os << "CLOSING";
44 case TransportState::FAILED:
45 return os << "FAILED";
46 case TransportState::CLOSED:
47 return os << "CLOSED";
48 default:
49 return os << "NONE";
50 }
51}
52
Eric Newberrya98bf932015-09-21 00:58:47 -070053Transport::Transport()
54 : m_face(nullptr)
55 , m_service(nullptr)
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070056 , 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)
Eric Newberryb49313d2017-12-24 20:22:27 -070060 , m_sendQueueCapacity(QUEUE_UNSUPPORTED)
Eric Newberrya98bf932015-09-21 00:58:47 -070061 , m_state(TransportState::UP)
Eric Newberryc64d30a2015-12-26 11:07:27 -070062 , m_expirationTime(time::steady_clock::TimePoint::max())
Eric Newberrya98bf932015-09-21 00:58:47 -070063{
64}
65
Davide Pesavento32065652017-01-15 01:52:21 -050066Transport::~Transport() = default;
Eric Newberrya98bf932015-09-21 00:58:47 -070067
68void
Junxiao Shicde37ad2015-12-24 01:02:05 -070069Transport::setFaceAndLinkService(Face& face, LinkService& service)
Eric Newberrya98bf932015-09-21 00:58:47 -070070{
71 BOOST_ASSERT(m_face == nullptr);
72 BOOST_ASSERT(m_service == nullptr);
73
74 m_face = &face;
75 m_service = &service;
Eric Newberrya98bf932015-09-21 00:58:47 -070076}
77
78void
79Transport::close()
80{
81 if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
82 return;
83 }
84
85 this->setState(TransportState::CLOSING);
86 this->doClose();
Davide Pesavento32065652017-01-15 01:52:21 -050087 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -070088 // the Transport may be deallocated if doClose changes state to CLOSED
89}
90
91void
Teng Liang13d582a2020-07-21 20:23:11 -070092Transport::send(const Block& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -070093{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040094 BOOST_ASSERT(packet.isValid());
Junxiao Shi13546112015-10-14 19:33:07 -070095 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040096 packet.size() <= static_cast<size_t>(this->getMtu()));
Junxiao Shi13546112015-10-14 19:33:07 -070097
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
Junxiao Shi57df2882015-11-11 06:12:35 -0700104 if (state == TransportState::UP) {
105 ++this->nOutPackets;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400106 this->nOutBytes += packet.size();
Junxiao Shi57df2882015-11-11 06:12:35 -0700107 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700108
Teng Liang13d582a2020-07-21 20:23:11 -0700109 this->doSend(packet);
Eric Newberrya98bf932015-09-21 00:58:47 -0700110}
111
112void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400113Transport::receive(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -0700114{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400115 BOOST_ASSERT(packet.isValid());
Junxiao Shi13546112015-10-14 19:33:07 -0700116 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400117 packet.size() <= static_cast<size_t>(this->getMtu()));
Junxiao Shi13546112015-10-14 19:33:07 -0700118
Junxiao Shi57df2882015-11-11 06:12:35 -0700119 ++this->nInPackets;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400120 this->nInBytes += packet.size();
Eric Newberrya98bf932015-09-21 00:58:47 -0700121
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400122 m_service->receivePacket(packet, endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700123}
124
Eric Newberryf3ee8082020-01-28 13:44:18 -0800125void
126Transport::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
Yanbiao Li32dab972016-11-27 12:26:09 +0800141bool
142Transport::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
157bool
158Transport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
159{
160 return false;
161}
162
Eric Newberrya98bf932015-09-21 00:58:47 -0700163void
Davide Pesavento8728a252015-11-06 04:01:22 +0100164Transport::setPersistency(ndn::nfd::FacePersistency newPersistency)
165{
Yanbiao Li32dab972016-11-27 12:26:09 +0800166 BOOST_ASSERT(canChangePersistencyTo(newPersistency));
167
Davide Pesavento8728a252015-11-06 04:01:22 +0100168 if (m_persistency == newPersistency) {
169 return;
170 }
171
Yanbiao Li32dab972016-11-27 12:26:09 +0800172 auto oldPersistency = m_persistency;
Davide Pesavento8728a252015-11-06 04:01:22 +0100173 m_persistency = newPersistency;
Davide Pesavento32065652017-01-15 01:52:21 -0500174
175 if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
176 NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency);
177 this->afterChangePersistency(oldPersistency);
178 }
Yanbiao Li32dab972016-11-27 12:26:09 +0800179}
180
181void
182Transport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
183{
Davide Pesavento8728a252015-11-06 04:01:22 +0100184}
185
186void
Eric Newberrya98bf932015-09-21 00:58:47 -0700187Transport::setState(TransportState newState)
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;
200 case TransportState::DOWN:
201 isValid = newState == TransportState::UP ||
202 newState == TransportState::CLOSING ||
203 newState == TransportState::FAILED;
204 break;
205 case TransportState::CLOSING:
206 case TransportState::FAILED:
207 isValid = newState == TransportState::CLOSED;
208 break;
209 default:
210 break;
211 }
212
213 if (!isValid) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500214 NDN_THROW(std::runtime_error("Invalid state transition"));
Eric Newberrya98bf932015-09-21 00:58:47 -0700215 }
216
217 NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
218
219 TransportState oldState = m_state;
220 m_state = newState;
221 afterStateChange(oldState, newState);
Davide Pesavento32065652017-01-15 01:52:21 -0500222 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -0700223 // the Transport may be deallocated in the signal handler if newState is CLOSED
224}
225
226std::ostream&
227operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
228{
229 const Transport& transport = flh.obj;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700230 const Face* face = transport.getFace();
Eric Newberrya98bf932015-09-21 00:58:47 -0700231 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