blob: 44babbb9b52bc75a6d7e04fb84d88b4d61ed3386 [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/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040029namespace nfd::face {
Eric Newberrya98bf932015-09-21 00:58:47 -070030
Davide Pesaventoa3148082018-04-12 18:21:54 -040031NFD_LOG_INIT(Transport);
Eric Newberrya98bf932015-09-21 00:58:47 -070032
33std::ostream&
34operator<<(std::ostream& os, TransportState state)
35{
36 switch (state) {
37 case TransportState::UP:
38 return os << "UP";
39 case TransportState::DOWN:
40 return os << "DOWN";
41 case TransportState::CLOSING:
42 return os << "CLOSING";
43 case TransportState::FAILED:
44 return os << "FAILED";
45 case TransportState::CLOSED:
46 return os << "CLOSED";
47 default:
48 return os << "NONE";
49 }
50}
51
Eric Newberrya98bf932015-09-21 00:58:47 -070052Transport::Transport()
53 : m_face(nullptr)
54 , m_service(nullptr)
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070055 , m_scope(ndn::nfd::FACE_SCOPE_NONE)
56 , m_persistency(ndn::nfd::FACE_PERSISTENCY_NONE)
57 , m_linkType(ndn::nfd::LINK_TYPE_NONE)
58 , m_mtu(MTU_INVALID)
Eric Newberryb49313d2017-12-24 20:22:27 -070059 , m_sendQueueCapacity(QUEUE_UNSUPPORTED)
Eric Newberrya98bf932015-09-21 00:58:47 -070060 , m_state(TransportState::UP)
Eric Newberryc64d30a2015-12-26 11:07:27 -070061 , m_expirationTime(time::steady_clock::TimePoint::max())
Eric Newberrya98bf932015-09-21 00:58:47 -070062{
63}
64
Davide Pesavento32065652017-01-15 01:52:21 -050065Transport::~Transport() = default;
Eric Newberrya98bf932015-09-21 00:58:47 -070066
67void
Junxiao Shicde37ad2015-12-24 01:02:05 -070068Transport::setFaceAndLinkService(Face& face, LinkService& service)
Eric Newberrya98bf932015-09-21 00:58:47 -070069{
70 BOOST_ASSERT(m_face == nullptr);
71 BOOST_ASSERT(m_service == nullptr);
72
73 m_face = &face;
74 m_service = &service;
Eric Newberrya98bf932015-09-21 00:58:47 -070075}
76
77void
78Transport::close()
79{
80 if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
81 return;
82 }
83
84 this->setState(TransportState::CLOSING);
85 this->doClose();
Davide Pesavento32065652017-01-15 01:52:21 -050086 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -070087 // the Transport may be deallocated if doClose changes state to CLOSED
88}
89
90void
Teng Liang13d582a2020-07-21 20:23:11 -070091Transport::send(const Block& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -070092{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040093 BOOST_ASSERT(packet.isValid());
Junxiao Shi13546112015-10-14 19:33:07 -070094 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040095 packet.size() <= static_cast<size_t>(this->getMtu()));
Junxiao Shi13546112015-10-14 19:33:07 -070096
97 TransportState state = this->getState();
98 if (state != TransportState::UP && state != TransportState::DOWN) {
99 NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
100 return;
101 }
102
Junxiao Shi57df2882015-11-11 06:12:35 -0700103 if (state == TransportState::UP) {
104 ++this->nOutPackets;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400105 this->nOutBytes += packet.size();
Junxiao Shi57df2882015-11-11 06:12:35 -0700106 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700107
Teng Liang13d582a2020-07-21 20:23:11 -0700108 this->doSend(packet);
Eric Newberrya98bf932015-09-21 00:58:47 -0700109}
110
111void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400112Transport::receive(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -0700113{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400114 BOOST_ASSERT(packet.isValid());
Junxiao Shi13546112015-10-14 19:33:07 -0700115 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400116 packet.size() <= static_cast<size_t>(this->getMtu()));
Junxiao Shi13546112015-10-14 19:33:07 -0700117
Junxiao Shi57df2882015-11-11 06:12:35 -0700118 ++this->nInPackets;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400119 this->nInBytes += packet.size();
Eric Newberrya98bf932015-09-21 00:58:47 -0700120
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400121 m_service->receivePacket(packet, endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700122}
123
Eric Newberryf3ee8082020-01-28 13:44:18 -0800124void
125Transport::setMtu(ssize_t mtu)
126{
127 BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu >= 0);
128
129 if (mtu == m_mtu) {
130 return;
131 }
132
133 if (m_mtu != MTU_INVALID) {
134 NFD_LOG_FACE_INFO("setMtu " << m_mtu << " -> " << mtu);
135 }
136
137 m_mtu = mtu;
138}
139
Yanbiao Li32dab972016-11-27 12:26:09 +0800140bool
141Transport::canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
142{
143 // not changing, or setting initial persistency in subclass constructor
144 if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
145 return true;
146 }
147
148 if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
149 NFD_LOG_FACE_TRACE("cannot change persistency to NONE");
150 return false;
151 }
152
153 return this->canChangePersistencyToImpl(newPersistency);
154}
155
156bool
157Transport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
158{
159 return false;
160}
161
Eric Newberrya98bf932015-09-21 00:58:47 -0700162void
Davide Pesavento8728a252015-11-06 04:01:22 +0100163Transport::setPersistency(ndn::nfd::FacePersistency newPersistency)
164{
Yanbiao Li32dab972016-11-27 12:26:09 +0800165 BOOST_ASSERT(canChangePersistencyTo(newPersistency));
166
Davide Pesavento8728a252015-11-06 04:01:22 +0100167 if (m_persistency == newPersistency) {
168 return;
169 }
170
Yanbiao Li32dab972016-11-27 12:26:09 +0800171 auto oldPersistency = m_persistency;
Davide Pesavento8728a252015-11-06 04:01:22 +0100172 m_persistency = newPersistency;
Davide Pesavento32065652017-01-15 01:52:21 -0500173
174 if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
175 NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency);
176 this->afterChangePersistency(oldPersistency);
177 }
Yanbiao Li32dab972016-11-27 12:26:09 +0800178}
179
180void
181Transport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
182{
Davide Pesavento8728a252015-11-06 04:01:22 +0100183}
184
185void
Eric Newberrya98bf932015-09-21 00:58:47 -0700186Transport::setState(TransportState newState)
187{
188 if (m_state == newState) {
189 return;
190 }
191
192 bool isValid = false;
193 switch (m_state) {
194 case TransportState::UP:
195 isValid = newState == TransportState::DOWN ||
196 newState == TransportState::CLOSING ||
197 newState == TransportState::FAILED;
198 break;
199 case TransportState::DOWN:
200 isValid = newState == TransportState::UP ||
201 newState == TransportState::CLOSING ||
202 newState == TransportState::FAILED;
203 break;
204 case TransportState::CLOSING:
205 case TransportState::FAILED:
206 isValid = newState == TransportState::CLOSED;
207 break;
208 default:
209 break;
210 }
211
212 if (!isValid) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500213 NDN_THROW(std::runtime_error("Invalid state transition"));
Eric Newberrya98bf932015-09-21 00:58:47 -0700214 }
215
216 NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
217
218 TransportState oldState = m_state;
219 m_state = newState;
220 afterStateChange(oldState, newState);
Davide Pesavento32065652017-01-15 01:52:21 -0500221 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -0700222 // the Transport may be deallocated in the signal handler if newState is CLOSED
223}
224
225std::ostream&
226operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
227{
228 const Transport& transport = flh.obj;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700229 const Face* face = transport.getFace();
Eric Newberrya98bf932015-09-21 00:58:47 -0700230 FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
231
232 os << "[id=" << faceId << ",local=" << transport.getLocalUri()
233 << ",remote=" << transport.getRemoteUri() << "] ";
234 return os;
235}
236
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400237} // namespace nfd::face