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