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