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 | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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 | |
| 35 | NFD_LOG_INIT("GenericLinkService"); |
| 36 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 37 | constexpr uint32_t DEFAULT_CONGESTION_THRESHOLD_DIVISOR = 2; |
| 38 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 39 | GenericLinkService::Options::Options() |
| 40 | : allowLocalFields(false) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 41 | , allowFragmentation(false) |
| 42 | , allowReassembly(false) |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 43 | , allowCongestionMarking(false) |
| 44 | , baseCongestionMarkingInterval(time::milliseconds(100)) // Interval from RFC 8289 (CoDel) |
| 45 | , defaultCongestionThreshold(65536) // This default value works well for a queue capacity of 200KiB |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | GenericLinkService::GenericLinkService(const GenericLinkService::Options& options) |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 50 | : m_options(options) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 51 | , m_fragmenter(m_options.fragmenterOptions, this) |
| 52 | , m_reassembler(m_options.reassemblerOptions, this) |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 53 | , m_reliability(m_options.reliabilityOptions, this) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 54 | , m_lastSeqNo(-2) |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 55 | , m_nextMarkTime(time::steady_clock::TimePoint::max()) |
| 56 | , m_lastMarkTime(time::steady_clock::TimePoint::min()) |
| 57 | , m_nMarkedSinceInMarkingState(0) |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 58 | { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 59 | m_reassembler.beforeTimeout.connect(bind([this] { ++this->nReassemblyTimeouts; })); |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 60 | m_reliability.onDroppedInterest.connect([this] (const Interest& i) { this->notifyDroppedInterest(i); }); |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 61 | nReassembling.observe(&m_reassembler); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 64 | void |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 65 | GenericLinkService::setOptions(const GenericLinkService::Options& options) |
| 66 | { |
| 67 | m_options = options; |
| 68 | m_fragmenter.setOptions(m_options.fragmenterOptions); |
| 69 | m_reassembler.setOptions(m_options.reassemblerOptions); |
| 70 | m_reliability.setOptions(m_options.reliabilityOptions); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | GenericLinkService::requestIdlePacket() |
| 75 | { |
| 76 | // No need to request Acks to attach to this packet from LpReliability, as they are already |
| 77 | // attached in sendLpPacket |
| 78 | this->sendLpPacket({}); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | GenericLinkService::sendLpPacket(lp::Packet&& pkt) |
| 83 | { |
| 84 | const ssize_t mtu = this->getTransport()->getMtu(); |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 85 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 86 | if (m_options.reliabilityOptions.isEnabled) { |
| 87 | m_reliability.piggyback(pkt, mtu); |
| 88 | } |
| 89 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 90 | if (m_options.allowCongestionMarking) { |
| 91 | checkCongestionLevel(pkt); |
| 92 | } |
| 93 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 94 | Transport::Packet tp(pkt.wireEncode()); |
| 95 | if (mtu != MTU_UNLIMITED && tp.packet.size() > static_cast<size_t>(mtu)) { |
| 96 | ++this->nOutOverMtu; |
| 97 | NFD_LOG_FACE_WARN("attempted to send packet over MTU limit"); |
| 98 | return; |
| 99 | } |
| 100 | this->sendPacket(std::move(tp)); |
| 101 | } |
| 102 | |
| 103 | void |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 104 | GenericLinkService::doSendInterest(const Interest& interest) |
| 105 | { |
| 106 | lp::Packet lpPacket(interest.wireEncode()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 107 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 108 | encodeLpFields(interest, lpPacket); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 109 | |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 110 | this->sendNetPacket(std::move(lpPacket), true); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void |
| 114 | GenericLinkService::doSendData(const Data& data) |
| 115 | { |
| 116 | lp::Packet lpPacket(data.wireEncode()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 117 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 118 | encodeLpFields(data, lpPacket); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 119 | |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 120 | this->sendNetPacket(std::move(lpPacket), false); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void |
| 124 | GenericLinkService::doSendNack(const lp::Nack& nack) |
| 125 | { |
| 126 | lp::Packet lpPacket(nack.getInterest().wireEncode()); |
| 127 | lpPacket.add<lp::NackField>(nack.getHeader()); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 128 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 129 | encodeLpFields(nack, lpPacket); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 130 | |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 131 | this->sendNetPacket(std::move(lpPacket), false); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 134 | void |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 135 | GenericLinkService::encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket) |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 136 | { |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 137 | if (m_options.allowLocalFields) { |
| 138 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = netPkt.getTag<lp::IncomingFaceIdTag>(); |
| 139 | if (incomingFaceIdTag != nullptr) { |
| 140 | lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | shared_ptr<lp::CongestionMarkTag> congestionMarkTag = netPkt.getTag<lp::CongestionMarkTag>(); |
| 145 | if (congestionMarkTag != nullptr) { |
| 146 | lpPacket.add<lp::CongestionMarkField>(*congestionMarkTag); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 147 | } |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 150 | void |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 151 | GenericLinkService::sendNetPacket(lp::Packet&& pkt, bool isInterest) |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 152 | { |
| 153 | std::vector<lp::Packet> frags; |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 154 | ssize_t mtu = this->getTransport()->getMtu(); |
| 155 | |
| 156 | // Make space for feature fields in fragments |
| 157 | if (m_options.reliabilityOptions.isEnabled && mtu != MTU_UNLIMITED) { |
| 158 | mtu -= LpReliability::RESERVED_HEADER_SPACE; |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 161 | if (m_options.allowCongestionMarking && mtu != MTU_UNLIMITED) { |
| 162 | mtu -= CONGESTION_MARK_SIZE; |
| 163 | } |
| 164 | |
| 165 | BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu > 0); |
| 166 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 167 | if (m_options.allowFragmentation && mtu != MTU_UNLIMITED) { |
| 168 | bool isOk = false; |
| 169 | std::tie(isOk, frags) = m_fragmenter.fragmentPacket(pkt, mtu); |
| 170 | if (!isOk) { |
| 171 | // fragmentation failed (warning is logged by LpFragmenter) |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 172 | ++this->nFragmentationErrors; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | } |
| 176 | else { |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 177 | if (m_options.reliabilityOptions.isEnabled) { |
| 178 | frags.push_back(pkt); |
| 179 | } |
| 180 | else { |
| 181 | frags.push_back(std::move(pkt)); |
| 182 | } |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 185 | if (frags.size() == 1) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 186 | // even if indexed fragmentation is enabled, the fragmenter should not |
| 187 | // fragment the packet if it can fit in MTU |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 188 | BOOST_ASSERT(!frags.front().has<lp::FragIndexField>()); |
| 189 | BOOST_ASSERT(!frags.front().has<lp::FragCountField>()); |
| 190 | } |
| 191 | |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 192 | // Only assign sequences to fragments if packet contains more than 1 fragment |
| 193 | if (frags.size() > 1) { |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 194 | // Assign sequences to all fragments |
| 195 | this->assignSequences(frags); |
| 196 | } |
| 197 | |
| 198 | if (m_options.reliabilityOptions.isEnabled && frags.front().has<lp::FragmentField>()) { |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 199 | m_reliability.handleOutgoing(frags, std::move(pkt), isInterest); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | for (lp::Packet& frag : frags) { |
| 203 | this->sendLpPacket(std::move(frag)); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | GenericLinkService::assignSequence(lp::Packet& pkt) |
| 209 | { |
| 210 | pkt.set<lp::SequenceField>(++m_lastSeqNo); |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | GenericLinkService::assignSequences(std::vector<lp::Packet>& pkts) |
| 215 | { |
| 216 | std::for_each(pkts.begin(), pkts.end(), bind(&GenericLinkService::assignSequence, this, _1)); |
| 217 | } |
| 218 | |
| 219 | void |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 220 | GenericLinkService::checkCongestionLevel(lp::Packet& pkt) |
| 221 | { |
| 222 | ssize_t sendQueueLength = getTransport()->getSendQueueLength(); |
| 223 | // This operation requires that the transport supports retrieving current send queue length |
| 224 | if (sendQueueLength < 0) { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | // To avoid overflowing the queue, set the congestion threshold to at least half of the send |
| 229 | // queue capacity. |
| 230 | size_t congestionThreshold = m_options.defaultCongestionThreshold; |
| 231 | if (getTransport()->getSendQueueCapacity() >= 0) { |
| 232 | congestionThreshold = std::min(congestionThreshold, |
| 233 | static_cast<size_t>(getTransport()->getSendQueueCapacity()) / |
| 234 | DEFAULT_CONGESTION_THRESHOLD_DIVISOR); |
| 235 | } |
| 236 | |
| 237 | if (sendQueueLength > 0) { |
| 238 | NFD_LOG_FACE_TRACE("txqlen=" << sendQueueLength << " threshold=" << congestionThreshold << |
| 239 | " capacity=" << getTransport()->getSendQueueCapacity()); |
| 240 | } |
| 241 | |
| 242 | if (static_cast<size_t>(sendQueueLength) > congestionThreshold) { // Send queue is congested |
| 243 | const auto now = time::steady_clock::now(); |
| 244 | if (now >= m_nextMarkTime || now >= m_lastMarkTime + m_options.baseCongestionMarkingInterval) { |
| 245 | // Mark at most one initial packet per baseCongestionMarkingInterval |
| 246 | if (m_nMarkedSinceInMarkingState == 0) { |
| 247 | m_nextMarkTime = now; |
| 248 | } |
| 249 | |
| 250 | // Time to mark packet |
| 251 | pkt.set<lp::CongestionMarkField>(1); |
| 252 | ++nCongestionMarked; |
| 253 | NFD_LOG_FACE_DEBUG("LpPacket was marked as congested"); |
| 254 | |
| 255 | ++m_nMarkedSinceInMarkingState; |
| 256 | // Decrease the marking interval by the inverse of the square root of the number of packets |
| 257 | // marked in this incident of congestion |
| 258 | m_nextMarkTime += time::nanoseconds(static_cast<time::nanoseconds::rep>( |
| 259 | m_options.baseCongestionMarkingInterval.count() / |
| 260 | std::sqrt(m_nMarkedSinceInMarkingState))); |
| 261 | m_lastMarkTime = now; |
| 262 | } |
| 263 | } |
| 264 | else if (m_nextMarkTime != time::steady_clock::TimePoint::max()) { |
| 265 | // Congestion incident has ended, so reset |
| 266 | NFD_LOG_FACE_DEBUG("Send queue length dropped below congestion threshold"); |
| 267 | m_nextMarkTime = time::steady_clock::TimePoint::max(); |
| 268 | m_nMarkedSinceInMarkingState = 0; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 273 | GenericLinkService::doReceivePacket(Transport::Packet&& packet) |
| 274 | { |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 275 | try { |
Eric Newberry | a1939ba | 2015-10-09 12:35:03 -0700 | [diff] [blame] | 276 | lp::Packet pkt(packet.packet); |
| 277 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 278 | if (m_options.reliabilityOptions.isEnabled) { |
| 279 | m_reliability.processIncomingPacket(pkt); |
| 280 | } |
| 281 | |
Eric Newberry | a1939ba | 2015-10-09 12:35:03 -0700 | [diff] [blame] | 282 | if (!pkt.has<lp::FragmentField>()) { |
| 283 | NFD_LOG_FACE_TRACE("received IDLE packet: DROP"); |
| 284 | return; |
| 285 | } |
| 286 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 287 | if ((pkt.has<lp::FragIndexField>() || pkt.has<lp::FragCountField>()) && |
| 288 | !m_options.allowReassembly) { |
| 289 | NFD_LOG_FACE_WARN("received fragment, but reassembly disabled: DROP"); |
Eric Newberry | a1939ba | 2015-10-09 12:35:03 -0700 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 293 | bool isReassembled = false; |
| 294 | Block netPkt; |
| 295 | lp::Packet firstPkt; |
| 296 | std::tie(isReassembled, netPkt, firstPkt) = m_reassembler.receiveFragment(packet.remoteEndpoint, |
| 297 | pkt); |
| 298 | if (isReassembled) { |
| 299 | this->decodeNetPacket(netPkt, firstPkt); |
| 300 | } |
| 301 | } |
| 302 | catch (const tlv::Error& e) { |
| 303 | ++this->nInLpInvalid; |
| 304 | NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP"); |
| 305 | } |
| 306 | } |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 307 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 308 | void |
| 309 | GenericLinkService::decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt) |
| 310 | { |
| 311 | try { |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 312 | switch (netPkt.type()) { |
| 313 | case tlv::Interest: |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 314 | if (firstPkt.has<lp::NackField>()) { |
| 315 | this->decodeNack(netPkt, firstPkt); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 316 | } |
| 317 | else { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 318 | this->decodeInterest(netPkt, firstPkt); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 319 | } |
| 320 | break; |
| 321 | case tlv::Data: |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 322 | this->decodeData(netPkt, firstPkt); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 323 | break; |
| 324 | default: |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 325 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 326 | NFD_LOG_FACE_WARN("unrecognized network-layer packet TLV-TYPE " << netPkt.type() << ": DROP"); |
| 327 | return; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 330 | catch (const tlv::Error& e) { |
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("packet parse error (" << e.what() << "): DROP"); |
| 333 | } |
| 334 | } |
| 335 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 336 | void |
| 337 | GenericLinkService::decodeInterest(const Block& netPkt, const lp::Packet& firstPkt) |
| 338 | { |
| 339 | BOOST_ASSERT(netPkt.type() == tlv::Interest); |
| 340 | BOOST_ASSERT(!firstPkt.has<lp::NackField>()); |
| 341 | |
| 342 | // forwarding expects Interest to be created with make_shared |
| 343 | auto interest = make_shared<Interest>(netPkt); |
| 344 | |
| 345 | if (firstPkt.has<lp::NextHopFaceIdField>()) { |
| 346 | if (m_options.allowLocalFields) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 347 | interest->setTag(make_shared<lp::NextHopFaceIdTag>(firstPkt.get<lp::NextHopFaceIdField>())); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 348 | } |
| 349 | else { |
| 350 | NFD_LOG_FACE_WARN("received NextHopFaceId, but local fields disabled: DROP"); |
| 351 | return; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if (firstPkt.has<lp::CachePolicyField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 356 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 357 | NFD_LOG_FACE_WARN("received CachePolicy with Interest: DROP"); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | if (firstPkt.has<lp::IncomingFaceIdField>()) { |
| 362 | NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE"); |
| 363 | } |
| 364 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 365 | if (firstPkt.has<lp::CongestionMarkField>()) { |
| 366 | interest->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>())); |
| 367 | } |
| 368 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 369 | this->receiveInterest(*interest); |
| 370 | } |
| 371 | |
| 372 | void |
| 373 | GenericLinkService::decodeData(const Block& netPkt, const lp::Packet& firstPkt) |
| 374 | { |
| 375 | BOOST_ASSERT(netPkt.type() == tlv::Data); |
| 376 | |
| 377 | // forwarding expects Data to be created with make_shared |
| 378 | auto data = make_shared<Data>(netPkt); |
| 379 | |
| 380 | if (firstPkt.has<lp::NackField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 381 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 382 | NFD_LOG_FACE_WARN("received Nack with Data: DROP"); |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | if (firstPkt.has<lp::NextHopFaceIdField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 387 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 388 | NFD_LOG_FACE_WARN("received NextHopFaceId with Data: DROP"); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | if (firstPkt.has<lp::CachePolicyField>()) { |
Junxiao Shi | 6eb0271 | 2017-05-27 22:48:02 +0000 | [diff] [blame] | 393 | // CachePolicy is unprivileged and does not require allowLocalFields option. |
| 394 | // In case of an invalid CachePolicyType, get<lp::CachePolicyField> will throw, |
| 395 | // so it's unnecessary to check here. |
| 396 | data->setTag(make_shared<lp::CachePolicyTag>(firstPkt.get<lp::CachePolicyField>())); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | if (firstPkt.has<lp::IncomingFaceIdField>()) { |
| 400 | NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE"); |
| 401 | } |
| 402 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 403 | if (firstPkt.has<lp::CongestionMarkField>()) { |
| 404 | data->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>())); |
| 405 | } |
| 406 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 407 | this->receiveData(*data); |
| 408 | } |
| 409 | |
| 410 | void |
| 411 | GenericLinkService::decodeNack(const Block& netPkt, const lp::Packet& firstPkt) |
| 412 | { |
| 413 | BOOST_ASSERT(netPkt.type() == tlv::Interest); |
| 414 | BOOST_ASSERT(firstPkt.has<lp::NackField>()); |
| 415 | |
| 416 | lp::Nack nack((Interest(netPkt))); |
| 417 | nack.setHeader(firstPkt.get<lp::NackField>()); |
| 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 Nack: DROP"); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | if (firstPkt.has<lp::CachePolicyField>()) { |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 426 | ++this->nInNetInvalid; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 427 | NFD_LOG_FACE_WARN("received CachePolicy with Nack: DROP"); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | if (firstPkt.has<lp::IncomingFaceIdField>()) { |
| 432 | NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE"); |
| 433 | } |
| 434 | |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 435 | if (firstPkt.has<lp::CongestionMarkField>()) { |
| 436 | nack.setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>())); |
| 437 | } |
| 438 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 439 | this->receiveNack(nack); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | } // namespace face |
| 443 | } // namespace nfd |