blob: e93480fc6f1f73a5d298836a7df38b8bf8417b4e [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 Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, 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
55Transport::Packet::Packet(Block&& packet1)
56 : packet(std::move(packet1))
Junxiao Shi13546112015-10-14 19:33:07 -070057 , remoteEndpoint(0)
Eric Newberrya98bf932015-09-21 00:58:47 -070058{
59}
60
61Transport::Transport()
62 : m_face(nullptr)
63 , m_service(nullptr)
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070064 , m_scope(ndn::nfd::FACE_SCOPE_NONE)
65 , m_persistency(ndn::nfd::FACE_PERSISTENCY_NONE)
66 , m_linkType(ndn::nfd::LINK_TYPE_NONE)
67 , m_mtu(MTU_INVALID)
Eric Newberryb49313d2017-12-24 20:22:27 -070068 , m_sendQueueCapacity(QUEUE_UNSUPPORTED)
Eric Newberrya98bf932015-09-21 00:58:47 -070069 , m_state(TransportState::UP)
Eric Newberryc64d30a2015-12-26 11:07:27 -070070 , m_expirationTime(time::steady_clock::TimePoint::max())
Eric Newberrya98bf932015-09-21 00:58:47 -070071{
72}
73
Davide Pesavento32065652017-01-15 01:52:21 -050074Transport::~Transport() = default;
Eric Newberrya98bf932015-09-21 00:58:47 -070075
76void
Junxiao Shicde37ad2015-12-24 01:02:05 -070077Transport::setFaceAndLinkService(Face& face, LinkService& service)
Eric Newberrya98bf932015-09-21 00:58:47 -070078{
79 BOOST_ASSERT(m_face == nullptr);
80 BOOST_ASSERT(m_service == nullptr);
81
82 m_face = &face;
83 m_service = &service;
Eric Newberrya98bf932015-09-21 00:58:47 -070084}
85
86void
87Transport::close()
88{
89 if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
90 return;
91 }
92
93 this->setState(TransportState::CLOSING);
94 this->doClose();
Davide Pesavento32065652017-01-15 01:52:21 -050095 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -070096 // the Transport may be deallocated if doClose changes state to CLOSED
97}
98
99void
Junxiao Shi13546112015-10-14 19:33:07 -0700100Transport::send(Packet&& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -0700101{
Junxiao Shi13546112015-10-14 19:33:07 -0700102 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
103 packet.packet.size() <= static_cast<size_t>(this->getMtu()));
104
105 TransportState state = this->getState();
106 if (state != TransportState::UP && state != TransportState::DOWN) {
107 NFD_LOG_FACE_TRACE("send ignored in " << state << " state");
108 return;
109 }
110
Junxiao Shi57df2882015-11-11 06:12:35 -0700111 if (state == TransportState::UP) {
112 ++this->nOutPackets;
113 this->nOutBytes += packet.packet.size();
Junxiao Shi57df2882015-11-11 06:12:35 -0700114 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700115
116 this->doSend(std::move(packet));
117}
118
119void
Junxiao Shi13546112015-10-14 19:33:07 -0700120Transport::receive(Packet&& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -0700121{
Junxiao Shi13546112015-10-14 19:33:07 -0700122 BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED ||
123 packet.packet.size() <= static_cast<size_t>(this->getMtu()));
124
Junxiao Shi57df2882015-11-11 06:12:35 -0700125 ++this->nInPackets;
126 this->nInBytes += packet.packet.size();
Eric Newberrya98bf932015-09-21 00:58:47 -0700127
128 m_service->receivePacket(std::move(packet));
129}
130
Yanbiao Li32dab972016-11-27 12:26:09 +0800131bool
132Transport::canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
133{
134 // not changing, or setting initial persistency in subclass constructor
135 if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
136 return true;
137 }
138
139 if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) {
140 NFD_LOG_FACE_TRACE("cannot change persistency to NONE");
141 return false;
142 }
143
144 return this->canChangePersistencyToImpl(newPersistency);
145}
146
147bool
148Transport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
149{
150 return false;
151}
152
Eric Newberrya98bf932015-09-21 00:58:47 -0700153void
Davide Pesavento8728a252015-11-06 04:01:22 +0100154Transport::setPersistency(ndn::nfd::FacePersistency newPersistency)
155{
Yanbiao Li32dab972016-11-27 12:26:09 +0800156 BOOST_ASSERT(canChangePersistencyTo(newPersistency));
157
Davide Pesavento8728a252015-11-06 04:01:22 +0100158 if (m_persistency == newPersistency) {
159 return;
160 }
161
Yanbiao Li32dab972016-11-27 12:26:09 +0800162 auto oldPersistency = m_persistency;
Davide Pesavento8728a252015-11-06 04:01:22 +0100163 m_persistency = newPersistency;
Davide Pesavento32065652017-01-15 01:52:21 -0500164
165 if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) {
166 NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency);
167 this->afterChangePersistency(oldPersistency);
168 }
Yanbiao Li32dab972016-11-27 12:26:09 +0800169}
170
171void
172Transport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
173{
Davide Pesavento8728a252015-11-06 04:01:22 +0100174}
175
176void
Eric Newberrya98bf932015-09-21 00:58:47 -0700177Transport::setState(TransportState newState)
178{
179 if (m_state == newState) {
180 return;
181 }
182
183 bool isValid = false;
184 switch (m_state) {
185 case TransportState::UP:
186 isValid = newState == TransportState::DOWN ||
187 newState == TransportState::CLOSING ||
188 newState == TransportState::FAILED;
189 break;
190 case TransportState::DOWN:
191 isValid = newState == TransportState::UP ||
192 newState == TransportState::CLOSING ||
193 newState == TransportState::FAILED;
194 break;
195 case TransportState::CLOSING:
196 case TransportState::FAILED:
197 isValid = newState == TransportState::CLOSED;
198 break;
199 default:
200 break;
201 }
202
203 if (!isValid) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500204 NDN_THROW(std::runtime_error("Invalid state transition"));
Eric Newberrya98bf932015-09-21 00:58:47 -0700205 }
206
207 NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
208
209 TransportState oldState = m_state;
210 m_state = newState;
211 afterStateChange(oldState, newState);
Davide Pesavento32065652017-01-15 01:52:21 -0500212 // warning: don't access any members after this:
Eric Newberrya98bf932015-09-21 00:58:47 -0700213 // the Transport may be deallocated in the signal handler if newState is CLOSED
214}
215
216std::ostream&
217operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
218{
219 const Transport& transport = flh.obj;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700220 const Face* face = transport.getFace();
Eric Newberrya98bf932015-09-21 00:58:47 -0700221 FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
222
223 os << "[id=" << faceId << ",local=" << transport.getLocalUri()
224 << ",remote=" << transport.getRemoteUri() << "] ";
225 return os;
226}
227
228} // namespace face
229} // namespace nfd