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 | /* |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
Davide Pesavento | 3e7fc83 | 2018-09-08 14:07:29 -0400 | [diff] [blame] | 34 | const ssize_t Transport::MIN_MTU; |
| 35 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 36 | std::ostream& |
| 37 | operator<<(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 Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 55 | Transport::Transport() |
| 56 | : m_face(nullptr) |
| 57 | , m_service(nullptr) |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 58 | , 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 Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 62 | , m_sendQueueCapacity(QUEUE_UNSUPPORTED) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 63 | , m_state(TransportState::UP) |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 64 | , m_expirationTime(time::steady_clock::TimePoint::max()) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 65 | { |
| 66 | } |
| 67 | |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 68 | Transport::~Transport() = default; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 69 | |
| 70 | void |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 71 | Transport::setFaceAndLinkService(Face& face, LinkService& service) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 72 | { |
| 73 | BOOST_ASSERT(m_face == nullptr); |
| 74 | BOOST_ASSERT(m_service == nullptr); |
| 75 | |
| 76 | m_face = &face; |
| 77 | m_service = &service; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
| 81 | Transport::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 Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 89 | // warning: don't access any members after this: |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 90 | // the Transport may be deallocated if doClose changes state to CLOSED |
| 91 | } |
| 92 | |
| 93 | void |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 94 | Transport::send(const Block& packet, const EndpointId& endpoint) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 95 | { |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 96 | BOOST_ASSERT(packet.isValid()); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 97 | BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED || |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 98 | packet.size() <= static_cast<size_t>(this->getMtu())); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 99 | |
| 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 Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 106 | if (state == TransportState::UP) { |
| 107 | ++this->nOutPackets; |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 108 | this->nOutBytes += packet.size(); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 109 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 110 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 111 | this->doSend(packet, endpoint); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 115 | Transport::receive(const Block& packet, const EndpointId& endpoint) |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 116 | { |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 117 | BOOST_ASSERT(packet.isValid()); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 118 | BOOST_ASSERT(this->getMtu() == MTU_UNLIMITED || |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 119 | packet.size() <= static_cast<size_t>(this->getMtu())); |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 120 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 121 | ++this->nInPackets; |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 122 | this->nInBytes += packet.size(); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 123 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 124 | m_service->receivePacket(packet, endpoint); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 127 | bool |
| 128 | Transport::canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const |
| 129 | { |
| 130 | // not changing, or setting initial persistency in subclass constructor |
| 131 | if (m_persistency == newPersistency || m_persistency == ndn::nfd::FACE_PERSISTENCY_NONE) { |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | if (newPersistency == ndn::nfd::FACE_PERSISTENCY_NONE) { |
| 136 | NFD_LOG_FACE_TRACE("cannot change persistency to NONE"); |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | return this->canChangePersistencyToImpl(newPersistency); |
| 141 | } |
| 142 | |
| 143 | bool |
| 144 | Transport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const |
| 145 | { |
| 146 | return false; |
| 147 | } |
| 148 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 149 | void |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 150 | Transport::setPersistency(ndn::nfd::FacePersistency newPersistency) |
| 151 | { |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 152 | BOOST_ASSERT(canChangePersistencyTo(newPersistency)); |
| 153 | |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 154 | if (m_persistency == newPersistency) { |
| 155 | return; |
| 156 | } |
| 157 | |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 158 | auto oldPersistency = m_persistency; |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 159 | m_persistency = newPersistency; |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 160 | |
| 161 | if (oldPersistency != ndn::nfd::FACE_PERSISTENCY_NONE) { |
| 162 | NFD_LOG_FACE_INFO("setPersistency " << oldPersistency << " -> " << newPersistency); |
| 163 | this->afterChangePersistency(oldPersistency); |
| 164 | } |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void |
| 168 | Transport::afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) |
| 169 | { |
Davide Pesavento | 8728a25 | 2015-11-06 04:01:22 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 173 | Transport::setState(TransportState newState) |
| 174 | { |
| 175 | if (m_state == newState) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | bool isValid = false; |
| 180 | switch (m_state) { |
| 181 | case TransportState::UP: |
| 182 | isValid = newState == TransportState::DOWN || |
| 183 | newState == TransportState::CLOSING || |
| 184 | newState == TransportState::FAILED; |
| 185 | break; |
| 186 | case TransportState::DOWN: |
| 187 | isValid = newState == TransportState::UP || |
| 188 | newState == TransportState::CLOSING || |
| 189 | newState == TransportState::FAILED; |
| 190 | break; |
| 191 | case TransportState::CLOSING: |
| 192 | case TransportState::FAILED: |
| 193 | isValid = newState == TransportState::CLOSED; |
| 194 | break; |
| 195 | default: |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | if (!isValid) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 200 | NDN_THROW(std::runtime_error("Invalid state transition")); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState); |
| 204 | |
| 205 | TransportState oldState = m_state; |
| 206 | m_state = newState; |
| 207 | afterStateChange(oldState, newState); |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 208 | // warning: don't access any members after this: |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 209 | // the Transport may be deallocated in the signal handler if newState is CLOSED |
| 210 | } |
| 211 | |
| 212 | std::ostream& |
| 213 | operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh) |
| 214 | { |
| 215 | const Transport& transport = flh.obj; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 216 | const Face* face = transport.getFace(); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 217 | FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId(); |
| 218 | |
| 219 | os << "[id=" << faceId << ",local=" << transport.getLocalUri() |
| 220 | << ",remote=" << transport.getRemoteUri() << "] "; |
| 221 | return os; |
| 222 | } |
| 223 | |
| 224 | } // namespace face |
| 225 | } // namespace nfd |