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