blob: 18d860896f4d98b631a1d99533742e738c8abded [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
Davide Pesavento3e7fc832018-09-08 14:07:29 -040034const ssize_t Transport::MIN_MTU;
35
Eric Newberrya98bf932015-09-21 00:58:47 -070036std::ostream&
37operator<<(std::ostream& os, TransportState state)
38{
39 switch (state) {
40 case TransportState::UP:
41 return os << "UP";
42 case TransportState::DOWN:
43 return os << "DOWN";
44 case TransportState::CLOSING:
45 return os << "CLOSING";
46 case TransportState::FAILED:
47 return os << "FAILED";
48 case TransportState::CLOSED:
49 return os << "CLOSED";
50 default:
51 return os << "NONE";
52 }
53}
54
Eric Newberrya98bf932015-09-21 00:58:47 -070055Transport::Transport()
56 : m_face(nullptr)
57 , m_service(nullptr)
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070058 , 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)
Eric Newberryb49313d2017-12-24 20:22:27 -070062 , m_sendQueueCapacity(QUEUE_UNSUPPORTED)
Eric Newberrya98bf932015-09-21 00:58:47 -070063 , m_state(TransportState::UP)
Eric Newberryc64d30a2015-12-26 11:07:27 -070064 , m_expirationTime(time::steady_clock::TimePoint::max())
Eric Newberrya98bf932015-09-21 00:58:47 -070065{
66}
67
Davide Pesavento32065652017-01-15 01:52:21 -050068Transport::~Transport() = default;
Eric Newberrya98bf932015-09-21 00:58:47 -070069
70void
Junxiao Shicde37ad2015-12-24 01:02:05 -070071Transport::setFaceAndLinkService(Face& face, LinkService& service)
Eric Newberrya98bf932015-09-21 00:58:47 -070072{
73 BOOST_ASSERT(m_face == nullptr);
74 BOOST_ASSERT(m_service == nullptr);
75
76 m_face = &face;
77 m_service = &service;
Eric Newberrya98bf932015-09-21 00:58:47 -070078}
79
80void
81Transport::close()
82{
83 if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
84 return;
85 }
86
87 this->setState(TransportState::CLOSING);
88 this->doClose();
Davide Pesavento32065652017-01-15 01:52:21 -050089 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -070090 // the Transport may be deallocated if doClose changes state to CLOSED
91}
92
93void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040094Transport::send(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -070095{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040096 BOOST_ASSERT(packet.isValid());
Junxiao Shi13546112015-10-14 19:33:07 -070097 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040098 packet.size() <= static_cast<size_t>(this->getMtu()));
Junxiao Shi13546112015-10-14 19:33:07 -070099
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
Junxiao Shi57df2882015-11-11 06:12:35 -0700106 if (state == TransportState::UP) {
107 ++this->nOutPackets;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400108 this->nOutBytes += packet.size();
Junxiao Shi57df2882015-11-11 06:12:35 -0700109 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700110
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400111 this->doSend(packet, endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700112}
113
114void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400115Transport::receive(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -0700116{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400117 BOOST_ASSERT(packet.isValid());
Junxiao Shi13546112015-10-14 19:33:07 -0700118 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400119 packet.size() <= static_cast<size_t>(this->getMtu()));
Junxiao Shi13546112015-10-14 19:33:07 -0700120
Junxiao Shi57df2882015-11-11 06:12:35 -0700121 ++this->nInPackets;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400122 this->nInBytes += packet.size();
Eric Newberrya98bf932015-09-21 00:58:47 -0700123
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400124 m_service->receivePacket(packet, endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700125}
126
Eric Newberryf3ee8082020-01-28 13:44:18 -0800127void
128Transport::setMtu(ssize_t mtu)
129{
130 BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu >= 0);
131
132 if (mtu == m_mtu) {
133 return;
134 }
135
136 if (m_mtu != MTU_INVALID) {
137 NFD_LOG_FACE_INFO("setMtu " << m_mtu << " -> " << mtu);
138 }
139
140 m_mtu = mtu;
141}
142
Yanbiao Li32dab972016-11-27 12:26:09 +0800143bool
144Transport::canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
145{
146 // not changing, or setting initial persistency in subclass constructor
147 if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
148 return true;
149 }
150
151 if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
152 NFD_LOG_FACE_TRACE("cannot change persistency to NONE");
153 return false;
154 }
155
156 return this->canChangePersistencyToImpl(newPersistency);
157}
158
159bool
160Transport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
161{
162 return false;
163}
164
Eric Newberrya98bf932015-09-21 00:58:47 -0700165void
Davide Pesavento8728a252015-11-06 04:01:22 +0100166Transport::setPersistency(ndn::nfd::FacePersistency newPersistency)
167{
Yanbiao Li32dab972016-11-27 12:26:09 +0800168 BOOST_ASSERT(canChangePersistencyTo(newPersistency));
169
Davide Pesavento8728a252015-11-06 04:01:22 +0100170 if (m_persistency == newPersistency) {
171 return;
172 }
173
Yanbiao Li32dab972016-11-27 12:26:09 +0800174 auto oldPersistency = m_persistency;
Davide Pesavento8728a252015-11-06 04:01:22 +0100175 m_persistency = newPersistency;
Davide Pesavento32065652017-01-15 01:52:21 -0500176
177 if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
178 NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency);
179 this->afterChangePersistency(oldPersistency);
180 }
Yanbiao Li32dab972016-11-27 12:26:09 +0800181}
182
183void
184Transport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
185{
Davide Pesavento8728a252015-11-06 04:01:22 +0100186}
187
188void
Eric Newberrya98bf932015-09-21 00:58:47 -0700189Transport::setState(TransportState newState)
190{
191 if (m_state == newState) {
192 return;
193 }
194
195 bool isValid = false;
196 switch (m_state) {
197 case TransportState::UP:
198 isValid = newState == TransportState::DOWN ||
199 newState == TransportState::CLOSING ||
200 newState == TransportState::FAILED;
201 break;
202 case TransportState::DOWN:
203 isValid = newState == TransportState::UP ||
204 newState == TransportState::CLOSING ||
205 newState == TransportState::FAILED;
206 break;
207 case TransportState::CLOSING:
208 case TransportState::FAILED:
209 isValid = newState == TransportState::CLOSED;
210 break;
211 default:
212 break;
213 }
214
215 if (!isValid) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500216 NDN_THROW(std::runtime_error("Invalid state transition"));
Eric Newberrya98bf932015-09-21 00:58:47 -0700217 }
218
219 NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
220
221 TransportState oldState = m_state;
222 m_state = newState;
223 afterStateChange(oldState, newState);
Davide Pesavento32065652017-01-15 01:52:21 -0500224 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -0700225 // the Transport may be deallocated in the signal handler if newState is CLOSED
226}
227
228std::ostream&
229operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
230{
231 const Transport& transport = flh.obj;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700232 const Face* face = transport.getFace();
Eric Newberrya98bf932015-09-21 00:58:47 -0700233 FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
234
235 os << "[id=" << faceId << ",local=" << transport.getLocalUri()
236 << ",remote=" << transport.getRemoteUri() << "] ";
237 return os;
238}
239
240} // namespace face
241} // namespace nfd