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