blob: dc3682e975c815c5b20d7e83442f6a4977198953 [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
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040052Transport::Transport() = default;
Eric Newberrya98bf932015-09-21 00:58:47 -070053
Davide Pesavento32065652017-01-15 01:52:21 -050054Transport::~Transport() = default;
Eric Newberrya98bf932015-09-21 00:58:47 -070055
56void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040057Transport::setFaceAndLinkService(Face& face, LinkService& service) noexcept
Eric Newberrya98bf932015-09-21 00:58:47 -070058{
59 BOOST_ASSERT(m_face == nullptr);
60 BOOST_ASSERT(m_service == nullptr);
61
62 m_face = &face;
63 m_service = &service;
Eric Newberrya98bf932015-09-21 00:58:47 -070064}
65
66void
67Transport::close()
68{
69 if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
70 return;
71 }
72
73 this->setState(TransportState::CLOSING);
74 this->doClose();
Davide Pesavento32065652017-01-15 01:52:21 -050075 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -070076 // the Transport may be deallocated if doClose changes state to CLOSED
77}
78
79void
Teng Liang13d582a2020-07-21 20:23:11 -070080Transport::send(const Block& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -070081{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040082 BOOST_ASSERT(packet.isValid());
Junxiao Shi13546112015-10-14 19:33:07 -070083 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040084 packet.size() <= static_cast<size_t>(this->getMtu()));
Junxiao Shi13546112015-10-14 19:33:07 -070085
86 TransportState state = this->getState();
87 if (state != TransportState::UP && state != TransportState::DOWN) {
88 NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
89 return;
90 }
91
Junxiao Shi57df2882015-11-11 06:12:35 -070092 if (state == TransportState::UP) {
93 ++this->nOutPackets;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040094 this->nOutBytes += packet.size();
Junxiao Shi57df2882015-11-11 06:12:35 -070095 }
Eric Newberrya98bf932015-09-21 00:58:47 -070096
Teng Liang13d582a2020-07-21 20:23:11 -070097 this->doSend(packet);
Eric Newberrya98bf932015-09-21 00:58:47 -070098}
99
100void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400101Transport::receive(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -0700102{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400103 BOOST_ASSERT(packet.isValid());
Junxiao Shi13546112015-10-14 19:33:07 -0700104 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400105 packet.size() <= static_cast<size_t>(this->getMtu()));
Junxiao Shi13546112015-10-14 19:33:07 -0700106
Junxiao Shi57df2882015-11-11 06:12:35 -0700107 ++this->nInPackets;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400108 this->nInBytes += packet.size();
Eric Newberrya98bf932015-09-21 00:58:47 -0700109
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400110 m_service->receivePacket(packet, endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700111}
112
Eric Newberryf3ee8082020-01-28 13:44:18 -0800113void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400114Transport::setMtu(ssize_t mtu) noexcept
Eric Newberryf3ee8082020-01-28 13:44:18 -0800115{
116 BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu >= 0);
117
118 if (mtu == m_mtu) {
119 return;
120 }
121
122 if (m_mtu != MTU_INVALID) {
123 NFD_LOG_FACE_INFO("setMtu " << m_mtu << " -> " << mtu);
124 }
125
126 m_mtu = mtu;
127}
128
Yanbiao Li32dab972016-11-27 12:26:09 +0800129bool
130Transport::canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
131{
132 // not changing, or setting initial persistency in subclass constructor
133 if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
134 return true;
135 }
136
137 if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
138 NFD_LOG_FACE_TRACE("cannot change persistency to NONE");
139 return false;
140 }
141
142 return this->canChangePersistencyToImpl(newPersistency);
143}
144
145bool
146Transport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
147{
148 return false;
149}
150
Eric Newberrya98bf932015-09-21 00:58:47 -0700151void
Davide Pesavento8728a252015-11-06 04:01:22 +0100152Transport::setPersistency(ndn::nfd::FacePersistency newPersistency)
153{
Yanbiao Li32dab972016-11-27 12:26:09 +0800154 BOOST_ASSERT(canChangePersistencyTo(newPersistency));
155
Davide Pesavento8728a252015-11-06 04:01:22 +0100156 if (m_persistency == newPersistency) {
157 return;
158 }
159
Yanbiao Li32dab972016-11-27 12:26:09 +0800160 auto oldPersistency = m_persistency;
Davide Pesavento8728a252015-11-06 04:01:22 +0100161 m_persistency = newPersistency;
Davide Pesavento32065652017-01-15 01:52:21 -0500162
163 if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
164 NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency);
165 this->afterChangePersistency(oldPersistency);
166 }
Yanbiao Li32dab972016-11-27 12:26:09 +0800167}
168
169void
170Transport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
171{
Davide Pesavento8728a252015-11-06 04:01:22 +0100172}
173
174void
Eric Newberrya98bf932015-09-21 00:58:47 -0700175Transport::setState(TransportState newState)
176{
177 if (m_state == newState) {
178 return;
179 }
180
181 bool isValid = false;
182 switch (m_state) {
183 case TransportState::UP:
184 isValid = newState == TransportState::DOWN ||
185 newState == TransportState::CLOSING ||
186 newState == TransportState::FAILED;
187 break;
188 case TransportState::DOWN:
189 isValid = newState == TransportState::UP ||
190 newState == TransportState::CLOSING ||
191 newState == TransportState::FAILED;
192 break;
193 case TransportState::CLOSING:
194 case TransportState::FAILED:
195 isValid = newState == TransportState::CLOSED;
196 break;
197 default:
198 break;
199 }
200
201 if (!isValid) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500202 NDN_THROW(std::runtime_error("Invalid state transition"));
Eric Newberrya98bf932015-09-21 00:58:47 -0700203 }
204
205 NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
206
207 TransportState oldState = m_state;
208 m_state = newState;
209 afterStateChange(oldState, newState);
Davide Pesavento32065652017-01-15 01:52:21 -0500210 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -0700211 // the Transport may be deallocated in the signal handler if newState is CLOSED
212}
213
214std::ostream&
215operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
216{
217 const Transport& transport = flh.obj;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700218 const Face* face = transport.getFace();
Eric Newberrya98bf932015-09-21 00:58:47 -0700219 FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
220
221 os << "[id=" << faceId << ",local=" << transport.getLocalUri()
222 << ",remote=" << transport.getRemoteUri() << "] ";
223 return os;
224}
225
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400226} // namespace nfd::face