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