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 | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [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 "generic-link-service.hpp" |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 27 | |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 28 | #include <ndn-cxx/lp/tags.hpp> |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 29 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 30 | #include <cmath> |
| 31 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 32 | namespace nfd { |
| 33 | namespace face { |
| 34 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 35 | NFD_LOG_INIT(GenericLinkService); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [diff] [blame] | 37 | constexpr size_t CONGESTION_MARK_SIZE = tlv::sizeOfVarNumber(lp::tlv::CongestionMark) + // type |
| 38 | tlv::sizeOfVarNumber(sizeof(uint64_t)) + // length |
| 39 | tlv::sizeOfNonNegativeInteger(UINT64_MAX); // value |
| 40 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 41 | constexpr uint32_t DEFAULT_CONGESTION_THRESHOLD_DIVISOR = 2; |
| 42 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 43 | GenericLinkService::GenericLinkService(const GenericLinkService::Options& options) |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 44 | : m_options(options) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 45 | , m_fragmenter(m_options.fragmenterOptions, this) |
| 46 | , m_reassembler(m_options.reassemblerOptions, this) |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 47 | , m_reliability(m_options.reliabilityOptions, this) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 48 | , m_lastSeqNo(-2) |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 49 | , m_nextMarkTime(time::steady_clock::TimePoint::max()) |
| 50 | , m_lastMarkTime(time::steady_clock::TimePoint::min()) |
| 51 | , m_nMarkedSinceInMarkingState(0) |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 52 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 53 | m_reassembler.beforeTimeout.connect([this] (auto...) { ++this->nReassemblyTimeouts; }); |
| 54 | m_reliability.onDroppedInterest.connect([this] (const auto& i) { this->notifyDroppedInterest(i); }); |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 55 | nReassembling.observe(&m_reassembler); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 58 | void |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 59 | GenericLinkService::setOptions(const GenericLinkService::Options& options) |
| 60 | { |
| 61 | m_options = options; |
| 62 | m_fragmenter.setOptions(m_options.fragmenterOptions); |
| 63 | m_reassembler.setOptions(m_options.reassemblerOptions); |
| 64 | m_reliability.setOptions(m_options.reliabilityOptions); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | GenericLinkService::requestIdlePacket() |
| 69 | { |
| 70 | // No need to request Acks to attach to this packet from LpReliability, as they are already |
| 71 | // attached in sendLpPacket |
| 72 | this->sendLpPacket({}); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | GenericLinkService::sendLpPacket(lp::Packet&& pkt) |
| 77 | { |
| 78 | const ssize_t mtu = this->getTransport()->getMtu(); |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 79 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 80 | if (m_options.reliabilityOptions.isEnabled) { |
| 81 | m_reliability.piggyback(pkt, mtu); |
| 82 | } |
| 83 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 84 | if (m_options.allowCongestionMarking) { |
| 85 | checkCongestionLevel(pkt); |
| 86 | } |
| 87 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 88 | Transport::Packet tp(pkt.wireEncode()); |
| 89 | if (mtu != MTU_UNLIMITED && tp.packet.size() > static_cast<size_t>(mtu)) { |
| 90 | ++this->nOutOverMtu; |
| 91 | NFD_LOG_FACE_WARN("attempted to send packet over MTU limit"); |
| 92 | return; |
| 93 | } |
| 94 | this->sendPacket(std::move(tp)); |
| 95 | } |
| 96 | |
| 97 | void |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 98 | GenericLinkService::doSendInterest(const Interest& interest) |
| 99 | { |
| 100 | lp::Packet lpPacket(interest.wireEncode()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 101 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 102 | encodeLpFields(interest, lpPacket); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 103 | |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 104 | this->sendNetPacket(std::move(lpPacket), true); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void |
| 108 | GenericLinkService::doSendData(const Data& data) |
| 109 | { |
| 110 | lp::Packet lpPacket(data.wireEncode()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 111 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 112 | encodeLpFields(data, lpPacket); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 113 | |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 114 | this->sendNetPacket(std::move(lpPacket), false); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void |
| 118 | GenericLinkService::doSendNack(const lp::Nack& nack) |
| 119 | { |
| 120 | lp::Packet lpPacket(nack.getInterest().wireEncode()); |
| 121 | lpPacket.add<lp::NackField>(nack.getHeader()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 122 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 123 | encodeLpFields(nack, lpPacket); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 124 | |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 125 | this->sendNetPacket(std::move(lpPacket), false); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 128 | void |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 129 | GenericLinkService::encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket) |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 130 | { |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 131 | if (m_options.allowLocalFields) { |
| 132 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = netPkt.getTag<lp::IncomingFaceIdTag>(); |
| 133 | if (incomingFaceIdTag != nullptr) { |
| 134 | lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | shared_ptr<lp::CongestionMarkTag> congestionMarkTag = netPkt.getTag<lp::CongestionMarkTag>(); |
| 139 | if (congestionMarkTag != nullptr) { |
| 140 | lpPacket.add<lp::CongestionMarkField>(*congestionMarkTag); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 141 | } |
Teng Liang | fdcbb4d | 2018-01-27 16:01:35 -0700 | [diff] [blame] | 142 | |
| 143 | if (m_options.allowSelfLearning) { |
| 144 | shared_ptr<lp::NonDiscoveryTag> nonDiscoveryTag = netPkt.getTag<lp::NonDiscoveryTag>(); |
| 145 | if (nonDiscoveryTag != nullptr) { |
| 146 | lpPacket.add<lp::NonDiscoveryField>(*nonDiscoveryTag); |
| 147 | } |
| 148 | |
| 149 | shared_ptr<lp::PrefixAnnouncementTag> prefixAnnouncementTag = netPkt.getTag<lp::PrefixAnnouncementTag>(); |
| 150 | if (prefixAnnouncementTag != nullptr) { |
| 151 | lpPacket.add<lp::PrefixAnnouncementField>(*prefixAnnouncementTag); |
| 152 | } |
| 153 | } |
Spyridon Mastorakis | 75c03e3 | 2016-12-06 15:20:19 -0800 | [diff] [blame^] | 154 | |
| 155 | shared_ptr<lp::HopCountTag> hopCountTag = netPkt.getTag<lp::HopCountTag>(); |
| 156 | if (hopCountTag != nullptr) { |
| 157 | lpPacket.add<lp::HopCountTagField>(*hopCountTag); |
| 158 | } |
| 159 | else { |
| 160 | lpPacket.add<lp::HopCountTagField>(0); |
| 161 | } |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 164 | void |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 165 | GenericLinkService::sendNetPacket(lp::Packet&& pkt, bool isInterest) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 166 | { |
| 167 | std::vector<lp::Packet> frags; |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 168 | ssize_t mtu = this->getTransport()->getMtu(); |
| 169 | |
| 170 | // Make space for feature fields in fragments |
| 171 | if (m_options.reliabilityOptions.isEnabled && mtu != MTU_UNLIMITED) { |
| 172 | mtu -= LpReliability::RESERVED_HEADER_SPACE; |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 175 | if (m_options.allowCongestionMarking && mtu != MTU_UNLIMITED) { |
| 176 | mtu -= CONGESTION_MARK_SIZE; |
| 177 | } |
| 178 | |
| 179 | BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu > 0); |
| 180 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 181 | if (m_options.allowFragmentation && mtu != MTU_UNLIMITED) { |
| 182 | bool isOk = false; |
| 183 | std::tie(isOk, frags) = m_fragmenter.fragmentPacket(pkt, mtu); |
| 184 | if (!isOk) { |
| 185 | // fragmentation failed (warning is logged by LpFragmenter) |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 186 | ++this->nFragmentationErrors; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | } |
| 190 | else { |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 191 | if (m_options.reliabilityOptions.isEnabled) { |
| 192 | frags.push_back(pkt); |
| 193 | } |
| 194 | else { |
| 195 | frags.push_back(std::move(pkt)); |
| 196 | } |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 199 | if (frags.size() == 1) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 200 | // even if indexed fragmentation is enabled, the fragmenter should not |
| 201 | // fragment the packet if it can fit in MTU |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 202 | BOOST_ASSERT(!frags.front().has<lp::FragIndexField>()); |
| 203 | BOOST_ASSERT(!frags.front().has<lp::FragCountField>()); |
| 204 | } |
| 205 | |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 206 | // Only assign sequences to fragments if packet contains more than 1 fragment |
| 207 | if (frags.size() > 1) { |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 208 | // Assign sequences to all fragments |
| 209 | this->assignSequences(frags); |
| 210 | } |
| 211 | |
| 212 | if (m_options.reliabilityOptions.isEnabled && frags.front().has<lp::FragmentField>()) { |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 213 | m_reliability.handleOutgoing(frags, std::move(pkt), isInterest); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | for (lp::Packet& frag : frags) { |
| 217 | this->sendLpPacket(std::move(frag)); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | void |
| 222 | GenericLinkService::assignSequence(lp::Packet& pkt) |
| 223 | { |
| 224 | pkt.set<lp::SequenceField>(++m_lastSeqNo); |
| 225 | } |
| 226 | |
| 227 | void |
| 228 | GenericLinkService::assignSequences(std::vector<lp::Packet>& pkts) |
| 229 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 230 | std::for_each(pkts.begin(), pkts.end(), [this] (auto& pkt) { this->assignSequence(pkt); }); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 234 | GenericLinkService::checkCongestionLevel(lp::Packet& pkt) |
| 235 | { |
| 236 | ssize_t sendQueueLength = getTransport()->getSendQueueLength(); |
| 237 | // This operation requires that the transport supports retrieving current send queue length |
| 238 | if (sendQueueLength < 0) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | // To avoid overflowing the queue, set the congestion threshold to at least half of the send |
| 243 | // queue capacity. |
| 244 | size_t congestionThreshold = m_options.defaultCongestionThreshold; |
| 245 | if (getTransport()->getSendQueueCapacity() >= 0) { |
| 246 | congestionThreshold = std::min(congestionThreshold, |
| 247 | static_cast<size_t>(getTransport()->getSendQueueCapacity()) / |
| 248 | DEFAULT_CONGESTION_THRESHOLD_DIVISOR); |
| 249 | } |
| 250 | |
| 251 | if (sendQueueLength > 0) { |
| 252 | NFD_LOG_FACE_TRACE("txqlen=" << sendQueueLength << " threshold=" << congestionThreshold << |
| 253 | " capacity=" << getTransport()->getSendQueueCapacity()); |
| 254 | } |
| 255 | |
| 256 | if (static_cast<size_t>(sendQueueLength) > congestionThreshold) { // Send queue is congested |
| 257 | const auto now = time::steady_clock::now(); |
| 258 | if (now >= m_nextMarkTime || now >= m_lastMarkTime + m_options.baseCongestionMarkingInterval) { |
| 259 | // Mark at most one initial packet per baseCongestionMarkingInterval |
| 260 | if (m_nMarkedSinceInMarkingState == 0) { |
| 261 | m_nextMarkTime = now; |
| 262 | } |
| 263 | |
| 264 | // Time to mark packet |
| 265 | pkt.set<lp::CongestionMarkField>(1); |
| 266 | ++nCongestionMarked; |
| 267 | NFD_LOG_FACE_DEBUG("LpPacket was marked as congested"); |
| 268 | |
| 269 | ++m_nMarkedSinceInMarkingState; |
| 270 | // Decrease the marking interval by the inverse of the square root of the number of packets |
| 271 | // marked in this incident of congestion |
| 272 | m_nextMarkTime += time::nanoseconds(static_cast<time::nanoseconds::rep>( |
| 273 | m_options.baseCongestionMarkingInterval.count() / |
| 274 | std::sqrt(m_nMarkedSinceInMarkingState))); |
| 275 | m_lastMarkTime = now; |
| 276 | } |
| 277 | } |
| 278 | else if (m_nextMarkTime != time::steady_clock::TimePoint::max()) { |
| 279 | // Congestion incident has ended, so reset |
| 280 | NFD_LOG_FACE_DEBUG("Send queue length dropped below congestion threshold"); |
| 281 | m_nextMarkTime = time::steady_clock::TimePoint::max(); |
| 282 | m_nMarkedSinceInMarkingState = 0; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 287 | GenericLinkService::doReceivePacket(Transport::Packet&& packet) |
| 288 | { |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 289 | try { |
Eric Newberry | a1939ba | 2015-10-09 12:35:03 -0700 | [diff] [blame] | 290 | lp::Packet pkt(packet.packet); |
| 291 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 292 | if (m_options.reliabilityOptions.isEnabled) { |
| 293 | m_reliability.processIncomingPacket(pkt); |
| 294 | } |
| 295 | |
Eric Newberry | a1939ba | 2015-10-09 12:35:03 -0700 | [diff] [blame] | 296 | if (!pkt.has<lp::FragmentField>()) { |
| 297 | NFD_LOG_FACE_TRACE("received IDLE packet: DROP"); |
| 298 | return; |
| 299 | } |
| 300 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 301 | if ((pkt.has<lp::FragIndexField>() || pkt.has<lp::FragCountField>()) && |
| 302 | !m_options.allowReassembly) { |
| 303 | NFD_LOG_FACE_WARN("received fragment, but reassembly disabled: DROP"); |
Eric Newberry | a1939ba | 2015-10-09 12:35:03 -0700 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 307 | bool isReassembled = false; |
| 308 | Block netPkt; |
| 309 | lp::Packet firstPkt; |
| 310 | std::tie(isReassembled, netPkt, firstPkt) = m_reassembler.receiveFragment(packet.remoteEndpoint, |
| 311 | pkt); |
| 312 | if (isReassembled) { |
| 313 | this->decodeNetPacket(netPkt, firstPkt); |
| 314 | } |
| 315 | } |
| 316 | catch (const tlv::Error& e) { |
| 317 | ++this->nInLpInvalid; |
| 318 | NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP"); |
| 319 | } |
| 320 | } |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 321 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 322 | void |
| 323 | GenericLinkService::decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt) |
| 324 | { |
| 325 | try { |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 326 | switch (netPkt.type()) { |
| 327 | case tlv::Interest: |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 328 | if (firstPkt.has<lp::NackField>()) { |
| 329 | this->decodeNack(netPkt, firstPkt); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 330 | } |
| 331 | else { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 332 | this->decodeInterest(netPkt, firstPkt); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 333 | } |
| 334 | break; |
| 335 | case tlv::Data: |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 336 | this->decodeData(netPkt, firstPkt); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 337 | break; |
| 338 | default: |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 339 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 340 | NFD_LOG_FACE_WARN("unrecognized network-layer packet TLV-TYPE " << netPkt.type() << ": DROP"); |
| 341 | return; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 344 | catch (const tlv::Error& e) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 345 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 346 | NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP"); |
| 347 | } |
| 348 | } |
| 349 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 350 | void |
| 351 | GenericLinkService::decodeInterest(const Block& netPkt, const lp::Packet& firstPkt) |
| 352 | { |
| 353 | BOOST_ASSERT(netPkt.type() == tlv::Interest); |
| 354 | BOOST_ASSERT(!firstPkt.has<lp::NackField>()); |
| 355 | |
| 356 | // forwarding expects Interest to be created with make_shared |
| 357 | auto interest = make_shared<Interest>(netPkt); |
| 358 | |
Spyridon Mastorakis | 75c03e3 | 2016-12-06 15:20:19 -0800 | [diff] [blame^] | 359 | // Increment HopCount |
| 360 | if (firstPkt.has<lp::HopCountTagField>()) { |
| 361 | interest->setTag(make_shared<lp::HopCountTag>(firstPkt.get<lp::HopCountTagField>() + 1)); |
| 362 | } |
| 363 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 364 | if (firstPkt.has<lp::NextHopFaceIdField>()) { |
| 365 | if (m_options.allowLocalFields) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 366 | interest->setTag(make_shared<lp::NextHopFaceIdTag>(firstPkt.get<lp::NextHopFaceIdField>())); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 367 | } |
| 368 | else { |
| 369 | NFD_LOG_FACE_WARN("received NextHopFaceId, but local fields disabled: DROP"); |
| 370 | return; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (firstPkt.has<lp::CachePolicyField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 375 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 376 | NFD_LOG_FACE_WARN("received CachePolicy with Interest: DROP"); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | if (firstPkt.has<lp::IncomingFaceIdField>()) { |
| 381 | NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE"); |
| 382 | } |
| 383 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 384 | if (firstPkt.has<lp::CongestionMarkField>()) { |
| 385 | interest->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>())); |
| 386 | } |
| 387 | |
Teng Liang | fdcbb4d | 2018-01-27 16:01:35 -0700 | [diff] [blame] | 388 | if (firstPkt.has<lp::NonDiscoveryField>()) { |
| 389 | if (m_options.allowSelfLearning) { |
| 390 | interest->setTag(make_shared<lp::NonDiscoveryTag>(firstPkt.get<lp::NonDiscoveryField>())); |
| 391 | } |
| 392 | else { |
| 393 | NFD_LOG_FACE_WARN("received NonDiscovery, but self-learning disabled: IGNORE"); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (firstPkt.has<lp::PrefixAnnouncementField>()) { |
| 398 | ++this->nInNetInvalid; |
| 399 | NFD_LOG_FACE_WARN("received PrefixAnnouncement with Interest: DROP"); |
| 400 | return; |
| 401 | } |
| 402 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 403 | this->receiveInterest(*interest); |
| 404 | } |
| 405 | |
| 406 | void |
| 407 | GenericLinkService::decodeData(const Block& netPkt, const lp::Packet& firstPkt) |
| 408 | { |
| 409 | BOOST_ASSERT(netPkt.type() == tlv::Data); |
| 410 | |
| 411 | // forwarding expects Data to be created with make_shared |
| 412 | auto data = make_shared<Data>(netPkt); |
| 413 | |
Spyridon Mastorakis | 75c03e3 | 2016-12-06 15:20:19 -0800 | [diff] [blame^] | 414 | if (firstPkt.has<lp::HopCountTagField>()) { |
| 415 | data->setTag(make_shared<lp::HopCountTag>(firstPkt.get<lp::HopCountTagField>() + 1)); |
| 416 | } |
| 417 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 418 | if (firstPkt.has<lp::NackField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 419 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 420 | NFD_LOG_FACE_WARN("received Nack with Data: DROP"); |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | if (firstPkt.has<lp::NextHopFaceIdField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 425 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 426 | NFD_LOG_FACE_WARN("received NextHopFaceId with Data: DROP"); |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | if (firstPkt.has<lp::CachePolicyField>()) { |
Junxiao Shi | 6eb0271 | 2017-05-27 22:48:02 +0000 | [diff] [blame] | 431 | // CachePolicy is unprivileged and does not require allowLocalFields option. |
| 432 | // In case of an invalid CachePolicyType, get<lp::CachePolicyField> will throw, |
| 433 | // so it's unnecessary to check here. |
| 434 | data->setTag(make_shared<lp::CachePolicyTag>(firstPkt.get<lp::CachePolicyField>())); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | if (firstPkt.has<lp::IncomingFaceIdField>()) { |
| 438 | NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE"); |
| 439 | } |
| 440 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 441 | if (firstPkt.has<lp::CongestionMarkField>()) { |
| 442 | data->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>())); |
| 443 | } |
| 444 | |
Teng Liang | fdcbb4d | 2018-01-27 16:01:35 -0700 | [diff] [blame] | 445 | if (firstPkt.has<lp::NonDiscoveryField>()) { |
| 446 | ++this->nInNetInvalid; |
| 447 | NFD_LOG_FACE_WARN("received NonDiscovery with Data: DROP"); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | if (firstPkt.has<lp::PrefixAnnouncementField>()) { |
| 452 | if (m_options.allowSelfLearning) { |
| 453 | data->setTag(make_shared<lp::PrefixAnnouncementTag>(firstPkt.get<lp::PrefixAnnouncementField>())); |
| 454 | } |
| 455 | else { |
| 456 | NFD_LOG_FACE_WARN("received PrefixAnnouncement, but self-learning disabled: IGNORE"); |
| 457 | } |
| 458 | } |
| 459 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 460 | this->receiveData(*data); |
| 461 | } |
| 462 | |
| 463 | void |
| 464 | GenericLinkService::decodeNack(const Block& netPkt, const lp::Packet& firstPkt) |
| 465 | { |
| 466 | BOOST_ASSERT(netPkt.type() == tlv::Interest); |
| 467 | BOOST_ASSERT(firstPkt.has<lp::NackField>()); |
| 468 | |
| 469 | lp::Nack nack((Interest(netPkt))); |
| 470 | nack.setHeader(firstPkt.get<lp::NackField>()); |
| 471 | |
| 472 | if (firstPkt.has<lp::NextHopFaceIdField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 473 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 474 | NFD_LOG_FACE_WARN("received NextHopFaceId with Nack: DROP"); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | if (firstPkt.has<lp::CachePolicyField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 479 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 480 | NFD_LOG_FACE_WARN("received CachePolicy with Nack: DROP"); |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | if (firstPkt.has<lp::IncomingFaceIdField>()) { |
| 485 | NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE"); |
| 486 | } |
| 487 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 488 | if (firstPkt.has<lp::CongestionMarkField>()) { |
| 489 | nack.setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>())); |
| 490 | } |
| 491 | |
Teng Liang | fdcbb4d | 2018-01-27 16:01:35 -0700 | [diff] [blame] | 492 | if (firstPkt.has<lp::NonDiscoveryField>()) { |
| 493 | ++this->nInNetInvalid; |
| 494 | NFD_LOG_FACE_WARN("received NonDiscovery with Nack: DROP"); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | if (firstPkt.has<lp::PrefixAnnouncementField>()) { |
| 499 | ++this->nInNetInvalid; |
| 500 | NFD_LOG_FACE_WARN("received PrefixAnnouncement with Nack: DROP"); |
| 501 | return; |
| 502 | } |
| 503 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 504 | this->receiveNack(nack); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | } // namespace face |
| 508 | } // namespace nfd |