Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 2 | /* |
Eric Newberry | f3ee808 | 2020-01-28 13:44:18 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, Regents of the University of California, |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 4 | * 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 Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 27 | #include "face.hpp" |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | namespace face { |
| 31 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 32 | NFD_LOG_INIT(Transport); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 33 | |
| 34 | std::ostream& |
| 35 | operator<<(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 Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 53 | Transport::Transport() |
| 54 | : m_face(nullptr) |
| 55 | , m_service(nullptr) |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 56 | , 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 Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 60 | , m_sendQueueCapacity(QUEUE_UNSUPPORTED) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 61 | , m_state(TransportState::UP) |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 62 | , m_expirationTime(time::steady_clock::TimePoint::max()) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 63 | { |
| 64 | } |
| 65 | |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 66 | Transport::~Transport() = default; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 67 | |
| 68 | void |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 69 | Transport::setFaceAndLinkService(Face& face, LinkService& service) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 70 | { |
| 71 | BOOST_ASSERT(m_face == nullptr); |
| 72 | BOOST_ASSERT(m_service == nullptr); |
| 73 | |
| 74 | m_face = &face; |
| 75 | m_service = &service; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void |
| 79 | Transport::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 Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 87 | // warning: don't access any members after this: |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 88 | // the Transport may be deallocated if doClose changes state to CLOSED |
| 89 | } |
| 90 | |
| 91 | void |
Teng Liang | 13d582a | 2020-07-21 20:23:11 -0700 | [diff] [blame^] | 92 | Transport::send(const Block& packet) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 93 | { |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 94 | BOOST_ASSERT(packet.isValid()); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 95 | BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED || |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 96 | packet.size() <= static_cast<size_t>(this->getMtu())); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 97 | |
| 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 Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 104 | if (state == TransportState::UP) { |
| 105 | ++this->nOutPackets; |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 106 | this->nOutBytes += packet.size(); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 107 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 108 | |
Teng Liang | 13d582a | 2020-07-21 20:23:11 -0700 | [diff] [blame^] | 109 | this->doSend(packet); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 113 | Transport::receive(const Block& packet, const EndpointId& endpoint) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 114 | { |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 115 | BOOST_ASSERT(packet.isValid()); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 116 | BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED || |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 117 | packet.size() <= static_cast<size_t>(this->getMtu())); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 119 | ++this->nInPackets; |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 120 | this->nInBytes += packet.size(); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 121 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 122 | m_service->receivePacket(packet, endpoint); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Eric Newberry | f3ee808 | 2020-01-28 13:44:18 -0800 | [diff] [blame] | 125 | void |
| 126 | Transport::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 Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 141 | bool |
| 142 | Transport::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 | |
| 157 | bool |
| 158 | Transport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const |
| 159 | { |
| 160 | return false; |
| 161 | } |
| 162 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 163 | void |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 164 | Transport::setPersistency(ndn::nfd::FacePersistency newPersistency) |
| 165 | { |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 166 | BOOST_ASSERT(canChangePersistencyTo(newPersistency)); |
| 167 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 168 | if (m_persistency == newPersistency) { |
| 169 | return; |
| 170 | } |
| 171 | |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 172 | auto oldPersistency = m_persistency; |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 173 | m_persistency = newPersistency; |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 174 | |
| 175 | if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) { |
| 176 | NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency); |
| 177 | this->afterChangePersistency(oldPersistency); |
| 178 | } |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void |
| 182 | Transport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) |
| 183 | { |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 187 | Transport::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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 214 | NDN_THROW(std::runtime_error("Invalid state transition")); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 215 | } |
| 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 Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 222 | // warning: don't access any members after this: |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 223 | // the Transport may be deallocated in the signal handler if newState is CLOSED |
| 224 | } |
| 225 | |
| 226 | std::ostream& |
| 227 | operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh) |
| 228 | { |
| 229 | const Transport& transport = flh.obj; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 230 | const Face* face = transport.getFace(); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 231 | 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 |