blob: 35e78b1c8fba7abd3ce1361aef51c36964961ca5 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry7b0071e2017-07-03 17:33:31 +00002/*
Davide Pesavento412c9822021-07-02 00:21:05 -04003 * Copyright (c) 2014-2021, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -07004 * 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 Newberryb49313d2017-12-24 20:22:27 -070027
Junxiao Shi606d5dd2019-09-23 12:47:44 -060028#include <ndn-cxx/lp/pit-token.hpp>
Junxiao Shicbc8e942016-09-06 03:17:45 +000029#include <ndn-cxx/lp/tags.hpp>
Eric Newberrya98bf932015-09-21 00:58:47 -070030
Eric Newberryb49313d2017-12-24 20:22:27 -070031#include <cmath>
32
Eric Newberrya98bf932015-09-21 00:58:47 -070033namespace nfd {
34namespace face {
35
Davide Pesaventoa3148082018-04-12 18:21:54 -040036NFD_LOG_INIT(GenericLinkService);
Eric Newberrya98bf932015-09-21 00:58:47 -070037
Davide Pesaventoa8098582019-03-31 15:48:02 -040038constexpr 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 Newberry86d31872015-09-23 16:24:59 -070042GenericLinkService::GenericLinkService(const GenericLinkService::Options& options)
Eric Newberry73bcad32017-04-25 17:57:35 -070043 : m_options(options)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070044 , m_fragmenter(m_options.fragmenterOptions, this)
45 , m_reassembler(m_options.reassemblerOptions, this)
Eric Newberry185ab292017-03-28 06:45:39 +000046 , m_reliability(m_options.reliabilityOptions, this)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070047 , m_lastSeqNo(-2)
Davide Pesavento412c9822021-07-02 00:21:05 -040048 , m_nextMarkTime(time::steady_clock::time_point::max())
Eric Newberryb49313d2017-12-24 20:22:27 -070049 , m_nMarkedSinceInMarkingState(0)
Eric Newberry86d31872015-09-23 16:24:59 -070050{
Davide Pesavento412c9822021-07-02 00:21:05 -040051 m_reassembler.beforeTimeout.connect([this] (auto&&...) { ++nReassemblyTimeouts; });
52 m_reliability.onDroppedInterest.connect([this] (const auto& i) { notifyDroppedInterest(i); });
Eric Newberry73bcad32017-04-25 17:57:35 -070053 nReassembling.observe(&m_reassembler);
Eric Newberry86d31872015-09-23 16:24:59 -070054}
55
Eric Newberrya98bf932015-09-21 00:58:47 -070056void
Eric Newberry185ab292017-03-28 06:45:39 +000057GenericLinkService::setOptions(const GenericLinkService::Options& options)
58{
59 m_options = options;
60 m_fragmenter.setOptions(m_options.fragmenterOptions);
61 m_reassembler.setOptions(m_options.reassemblerOptions);
62 m_reliability.setOptions(m_options.reliabilityOptions);
63}
64
Eric Newberrycb6551e2020-03-02 14:12:16 -080065ssize_t
66GenericLinkService::getEffectiveMtu() const
67{
68 // Since MTU_UNLIMITED is negative, it will implicitly override any finite override MTU
69 return std::min(m_options.overrideMtu, getTransport()->getMtu());
70}
71
72bool
73GenericLinkService::canOverrideMtuTo(ssize_t mtu) const
74{
75 // Not allowed to override unlimited transport MTU
76 if (getTransport()->getMtu() == MTU_UNLIMITED) {
77 return false;
78 }
79
80 // Override MTU must be at least MIN_MTU (also implicitly forbids MTU_UNLIMITED and MTU_INVALID)
81 return mtu >= MIN_MTU;
82}
83
Eric Newberry185ab292017-03-28 06:45:39 +000084void
Teng Liangf3bc3ae2020-06-08 10:19:25 -070085GenericLinkService::requestIdlePacket()
Eric Newberry185ab292017-03-28 06:45:39 +000086{
87 // No need to request Acks to attach to this packet from LpReliability, as they are already
88 // attached in sendLpPacket
Eric Newberry5ab4cf82020-02-03 15:40:16 -080089 NFD_LOG_FACE_TRACE("IDLE packet requested");
Teng Liangf3bc3ae2020-06-08 10:19:25 -070090 this->sendLpPacket({});
Eric Newberry185ab292017-03-28 06:45:39 +000091}
92
93void
Teng Liangf3bc3ae2020-06-08 10:19:25 -070094GenericLinkService::sendLpPacket(lp::Packet&& pkt)
Eric Newberry185ab292017-03-28 06:45:39 +000095{
Eric Newberrycb6551e2020-03-02 14:12:16 -080096 const ssize_t mtu = getEffectiveMtu();
Eric Newberryb49313d2017-12-24 20:22:27 -070097
Eric Newberry185ab292017-03-28 06:45:39 +000098 if (m_options.reliabilityOptions.isEnabled) {
99 m_reliability.piggyback(pkt, mtu);
100 }
101
Eric Newberryb49313d2017-12-24 20:22:27 -0700102 if (m_options.allowCongestionMarking) {
103 checkCongestionLevel(pkt);
104 }
105
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400106 auto block = pkt.wireEncode();
107 if (mtu != MTU_UNLIMITED && block.size() > static_cast<size_t>(mtu)) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400108 ++nOutOverMtu;
Eric Newberry185ab292017-03-28 06:45:39 +0000109 NFD_LOG_FACE_WARN("attempted to send packet over MTU limit");
110 return;
111 }
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700112 this->sendPacket(block);
Eric Newberry185ab292017-03-28 06:45:39 +0000113}
114
115void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700116GenericLinkService::doSendInterest(const Interest& interest)
Eric Newberrya98bf932015-09-21 00:58:47 -0700117{
118 lp::Packet lpPacket(interest.wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000119
Eric Newberryee400b52016-11-24 14:12:48 +0000120 encodeLpFields(interest, lpPacket);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700121
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700122 this->sendNetPacket(std::move(lpPacket), true);
Eric Newberrya98bf932015-09-21 00:58:47 -0700123}
124
125void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700126GenericLinkService::doSendData(const Data& data)
Eric Newberrya98bf932015-09-21 00:58:47 -0700127{
128 lp::Packet lpPacket(data.wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000129
Eric Newberryee400b52016-11-24 14:12:48 +0000130 encodeLpFields(data, lpPacket);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700131
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700132 this->sendNetPacket(std::move(lpPacket), false);
Eric Newberrya98bf932015-09-21 00:58:47 -0700133}
134
135void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700136GenericLinkService::doSendNack(const lp::Nack& nack)
Eric Newberrya98bf932015-09-21 00:58:47 -0700137{
138 lp::Packet lpPacket(nack.getInterest().wireEncode());
139 lpPacket.add<lp::NackField>(nack.getHeader());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000140
Eric Newberryee400b52016-11-24 14:12:48 +0000141 encodeLpFields(nack, lpPacket);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700142
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700143 this->sendNetPacket(std::move(lpPacket), false);
Eric Newberrya98bf932015-09-21 00:58:47 -0700144}
145
Junxiao Shi0de23a22015-12-03 20:07:02 +0000146void
Eric Newberry32f7eac2020-02-07 14:40:17 -0800147GenericLinkService::assignSequences(std::vector<lp::Packet>& pkts)
148{
149 std::for_each(pkts.begin(), pkts.end(), [this] (lp::Packet& pkt) {
150 pkt.set<lp::SequenceField>(++m_lastSeqNo);
151 });
152}
153
154void
Eric Newberry41aba102017-11-01 16:42:13 -0700155GenericLinkService::encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket)
Eric Newberry86d31872015-09-23 16:24:59 -0700156{
Eric Newberryee400b52016-11-24 14:12:48 +0000157 if (m_options.allowLocalFields) {
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600158 auto incomingFaceIdTag = netPkt.getTag<lp::IncomingFaceIdTag>();
Eric Newberryee400b52016-11-24 14:12:48 +0000159 if (incomingFaceIdTag != nullptr) {
160 lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag);
161 }
162 }
163
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600164 auto congestionMarkTag = netPkt.getTag<lp::CongestionMarkTag>();
Eric Newberryee400b52016-11-24 14:12:48 +0000165 if (congestionMarkTag != nullptr) {
166 lpPacket.add<lp::CongestionMarkField>(*congestionMarkTag);
Eric Newberry86d31872015-09-23 16:24:59 -0700167 }
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700168
169 if (m_options.allowSelfLearning) {
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600170 auto nonDiscoveryTag = netPkt.getTag<lp::NonDiscoveryTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700171 if (nonDiscoveryTag != nullptr) {
172 lpPacket.add<lp::NonDiscoveryField>(*nonDiscoveryTag);
173 }
174
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600175 auto prefixAnnouncementTag = netPkt.getTag<lp::PrefixAnnouncementTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700176 if (prefixAnnouncementTag != nullptr) {
177 lpPacket.add<lp::PrefixAnnouncementField>(*prefixAnnouncementTag);
178 }
179 }
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600180
181 auto pitToken = netPkt.getTag<lp::PitToken>();
182 if (pitToken != nullptr) {
183 lpPacket.add<lp::PitTokenField>(*pitToken);
184 }
Spyridon Mastorakise6900392016-12-06 15:20:19 -0800185
186 shared_ptr<lp::HopCountTag> hopCountTag = netPkt.getTag<lp::HopCountTag>();
187 if (hopCountTag != nullptr) {
188 lpPacket.add<lp::HopCountTagField>(*hopCountTag);
189 }
190 else {
191 lpPacket.add<lp::HopCountTagField>(0);
192 }
Eric Newberry86d31872015-09-23 16:24:59 -0700193}
194
Eric Newberrya98bf932015-09-21 00:58:47 -0700195void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700196GenericLinkService::sendNetPacket(lp::Packet&& pkt, bool isInterest)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700197{
198 std::vector<lp::Packet> frags;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800199 ssize_t mtu = getEffectiveMtu();
Eric Newberry7b0071e2017-07-03 17:33:31 +0000200
201 // Make space for feature fields in fragments
202 if (m_options.reliabilityOptions.isEnabled && mtu != MTU_UNLIMITED) {
203 mtu -= LpReliability::RESERVED_HEADER_SPACE;
Eric Newberry7b0071e2017-07-03 17:33:31 +0000204 }
205
Eric Newberryb49313d2017-12-24 20:22:27 -0700206 if (m_options.allowCongestionMarking && mtu != MTU_UNLIMITED) {
207 mtu -= CONGESTION_MARK_SIZE;
208 }
209
Eric Newberryf3ee8082020-01-28 13:44:18 -0800210 // An MTU of 0 is allowed but will cause all packets to be dropped before transmission
211 BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu >= 0);
Eric Newberryb49313d2017-12-24 20:22:27 -0700212
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700213 if (m_options.allowFragmentation && mtu != MTU_UNLIMITED) {
214 bool isOk = false;
215 std::tie(isOk, frags) = m_fragmenter.fragmentPacket(pkt, mtu);
216 if (!isOk) {
217 // fragmentation failed (warning is logged by LpFragmenter)
Davide Pesavento412c9822021-07-02 00:21:05 -0400218 ++nFragmentationErrors;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700219 return;
220 }
221 }
222 else {
Eric Newberry41aba102017-11-01 16:42:13 -0700223 if (m_options.reliabilityOptions.isEnabled) {
224 frags.push_back(pkt);
225 }
226 else {
227 frags.push_back(std::move(pkt));
228 }
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700229 }
230
Eric Newberry185ab292017-03-28 06:45:39 +0000231 if (frags.size() == 1) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700232 // even if indexed fragmentation is enabled, the fragmenter should not
233 // fragment the packet if it can fit in MTU
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700234 BOOST_ASSERT(!frags.front().has<lp::FragIndexField>());
235 BOOST_ASSERT(!frags.front().has<lp::FragCountField>());
236 }
237
Eric Newberry32f7eac2020-02-07 14:40:17 -0800238 // Only assign sequences to fragments if reliability enabled or if packet contains >1 fragment
239 if (m_options.reliabilityOptions.isEnabled || frags.size() > 1) {
Eric Newberry185ab292017-03-28 06:45:39 +0000240 // Assign sequences to all fragments
241 this->assignSequences(frags);
242 }
243
244 if (m_options.reliabilityOptions.isEnabled && frags.front().has<lp::FragmentField>()) {
Eric Newberry41aba102017-11-01 16:42:13 -0700245 m_reliability.handleOutgoing(frags, std::move(pkt), isInterest);
Eric Newberry185ab292017-03-28 06:45:39 +0000246 }
247
248 for (lp::Packet& frag : frags) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700249 this->sendLpPacket(std::move(frag));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700250 }
251}
252
253void
Eric Newberryb49313d2017-12-24 20:22:27 -0700254GenericLinkService::checkCongestionLevel(lp::Packet& pkt)
255{
256 ssize_t sendQueueLength = getTransport()->getSendQueueLength();
Klaus Schneider380668b2019-10-02 01:21:13 -0700257 // The transport must support retrieving the current send queue length
Eric Newberryb49313d2017-12-24 20:22:27 -0700258 if (sendQueueLength < 0) {
259 return;
260 }
261
Eric Newberryb49313d2017-12-24 20:22:27 -0700262 if (sendQueueLength > 0) {
Klaus Schneider380668b2019-10-02 01:21:13 -0700263 NFD_LOG_FACE_TRACE("txqlen=" << sendQueueLength << " threshold=" <<
264 m_options.defaultCongestionThreshold << " capacity=" <<
265 getTransport()->getSendQueueCapacity());
Eric Newberryb49313d2017-12-24 20:22:27 -0700266 }
267
Klaus Schneider380668b2019-10-02 01:21:13 -0700268 // sendQueue is above target
269 if (static_cast<size_t>(sendQueueLength) > m_options.defaultCongestionThreshold) {
Eric Newberryb49313d2017-12-24 20:22:27 -0700270 const auto now = time::steady_clock::now();
Eric Newberryb49313d2017-12-24 20:22:27 -0700271
Davide Pesavento412c9822021-07-02 00:21:05 -0400272 if (m_nextMarkTime == time::steady_clock::time_point::max()) {
Klaus Schneider380668b2019-10-02 01:21:13 -0700273 m_nextMarkTime = now + m_options.baseCongestionMarkingInterval;
274 }
275 // Mark packet if sendQueue stays above target for one interval
276 else if (now >= m_nextMarkTime) {
Eric Newberryb49313d2017-12-24 20:22:27 -0700277 pkt.set<lp::CongestionMarkField>(1);
278 ++nCongestionMarked;
279 NFD_LOG_FACE_DEBUG("LpPacket was marked as congested");
280
281 ++m_nMarkedSinceInMarkingState;
282 // Decrease the marking interval by the inverse of the square root of the number of packets
283 // marked in this incident of congestion
Klaus Schneider380668b2019-10-02 01:21:13 -0700284 time::nanoseconds interval(static_cast<time::nanoseconds::rep>(
285 m_options.baseCongestionMarkingInterval.count() /
286 std::sqrt(m_nMarkedSinceInMarkingState + 1)));
287 m_nextMarkTime += interval;
Eric Newberryb49313d2017-12-24 20:22:27 -0700288 }
289 }
Davide Pesavento412c9822021-07-02 00:21:05 -0400290 else if (m_nextMarkTime != time::steady_clock::time_point::max()) {
Eric Newberryb49313d2017-12-24 20:22:27 -0700291 // Congestion incident has ended, so reset
292 NFD_LOG_FACE_DEBUG("Send queue length dropped below congestion threshold");
Davide Pesavento412c9822021-07-02 00:21:05 -0400293 m_nextMarkTime = time::steady_clock::time_point::max();
Eric Newberryb49313d2017-12-24 20:22:27 -0700294 m_nMarkedSinceInMarkingState = 0;
295 }
296}
297
298void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400299GenericLinkService::doReceivePacket(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -0700300{
Eric Newberry86d31872015-09-23 16:24:59 -0700301 try {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400302 lp::Packet pkt(packet);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700303
Eric Newberry185ab292017-03-28 06:45:39 +0000304 if (m_options.reliabilityOptions.isEnabled) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800305 if (!m_reliability.processIncomingPacket(pkt)) {
306 NFD_LOG_FACE_TRACE("received duplicate fragment: DROP");
Davide Pesavento412c9822021-07-02 00:21:05 -0400307 ++nDuplicateSequence;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800308 return;
309 }
Eric Newberry185ab292017-03-28 06:45:39 +0000310 }
311
Eric Newberrya1939ba2015-10-09 12:35:03 -0700312 if (!pkt.has<lp::FragmentField>()) {
313 NFD_LOG_FACE_TRACE("received IDLE packet: DROP");
314 return;
315 }
316
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700317 if ((pkt.has<lp::FragIndexField>() || pkt.has<lp::FragCountField>()) &&
318 !m_options.allowReassembly) {
319 NFD_LOG_FACE_WARN("received fragment, but reassembly disabled: DROP");
Eric Newberrya1939ba2015-10-09 12:35:03 -0700320 return;
321 }
322
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700323 bool isReassembled = false;
324 Block netPkt;
325 lp::Packet firstPkt;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400326 std::tie(isReassembled, netPkt, firstPkt) = m_reassembler.receiveFragment(endpoint, pkt);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700327 if (isReassembled) {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400328 this->decodeNetPacket(netPkt, firstPkt, endpoint);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700329 }
330 }
331 catch (const tlv::Error& e) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400332 ++nInLpInvalid;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700333 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
334 }
335}
Eric Newberry86d31872015-09-23 16:24:59 -0700336
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700337void
ashiqopu075bb7d2019-03-10 01:38:21 +0000338GenericLinkService::decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt,
339 const EndpointId& endpointId)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700340{
341 try {
Eric Newberry86d31872015-09-23 16:24:59 -0700342 switch (netPkt.type()) {
343 case tlv::Interest:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700344 if (firstPkt.has<lp::NackField>()) {
ashiqopu075bb7d2019-03-10 01:38:21 +0000345 this->decodeNack(netPkt, firstPkt, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700346 }
347 else {
ashiqopu075bb7d2019-03-10 01:38:21 +0000348 this->decodeInterest(netPkt, firstPkt, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700349 }
350 break;
351 case tlv::Data:
ashiqopu075bb7d2019-03-10 01:38:21 +0000352 this->decodeData(netPkt, firstPkt, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700353 break;
354 default:
Davide Pesavento412c9822021-07-02 00:21:05 -0400355 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700356 NFD_LOG_FACE_WARN("unrecognized network-layer packet TLV-TYPE " << netPkt.type() << ": DROP");
357 return;
Eric Newberrya98bf932015-09-21 00:58:47 -0700358 }
359 }
Eric Newberry86d31872015-09-23 16:24:59 -0700360 catch (const tlv::Error& e) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400361 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700362 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
363 }
364}
365
Eric Newberry86d31872015-09-23 16:24:59 -0700366void
ashiqopu075bb7d2019-03-10 01:38:21 +0000367GenericLinkService::decodeInterest(const Block& netPkt, const lp::Packet& firstPkt,
368 const EndpointId& endpointId)
Eric Newberry86d31872015-09-23 16:24:59 -0700369{
370 BOOST_ASSERT(netPkt.type() == tlv::Interest);
371 BOOST_ASSERT(!firstPkt.has<lp::NackField>());
372
373 // forwarding expects Interest to be created with make_shared
374 auto interest = make_shared<Interest>(netPkt);
375
Spyridon Mastorakise6900392016-12-06 15:20:19 -0800376 // Increment HopCount
377 if (firstPkt.has<lp::HopCountTagField>()) {
378 interest->setTag(make_shared<lp::HopCountTag>(firstPkt.get<lp::HopCountTagField>() + 1));
379 }
380
Eric Newberry86d31872015-09-23 16:24:59 -0700381 if (firstPkt.has<lp::NextHopFaceIdField>()) {
382 if (m_options.allowLocalFields) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000383 interest->setTag(make_shared<lp::NextHopFaceIdTag>(firstPkt.get<lp::NextHopFaceIdField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700384 }
385 else {
386 NFD_LOG_FACE_WARN("received NextHopFaceId, but local fields disabled: DROP");
387 return;
388 }
389 }
390
391 if (firstPkt.has<lp::CachePolicyField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400392 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700393 NFD_LOG_FACE_WARN("received CachePolicy with Interest: DROP");
394 return;
395 }
396
397 if (firstPkt.has<lp::IncomingFaceIdField>()) {
398 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
399 }
400
Eric Newberryee400b52016-11-24 14:12:48 +0000401 if (firstPkt.has<lp::CongestionMarkField>()) {
402 interest->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
403 }
404
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700405 if (firstPkt.has<lp::NonDiscoveryField>()) {
406 if (m_options.allowSelfLearning) {
407 interest->setTag(make_shared<lp::NonDiscoveryTag>(firstPkt.get<lp::NonDiscoveryField>()));
408 }
409 else {
410 NFD_LOG_FACE_WARN("received NonDiscovery, but self-learning disabled: IGNORE");
411 }
412 }
413
414 if (firstPkt.has<lp::PrefixAnnouncementField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400415 ++nInNetInvalid;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700416 NFD_LOG_FACE_WARN("received PrefixAnnouncement with Interest: DROP");
417 return;
418 }
419
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600420 if (firstPkt.has<lp::PitTokenField>()) {
421 interest->setTag(make_shared<lp::PitToken>(firstPkt.get<lp::PitTokenField>()));
422 }
423
ashiqopu075bb7d2019-03-10 01:38:21 +0000424 this->receiveInterest(*interest, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700425}
426
427void
ashiqopu075bb7d2019-03-10 01:38:21 +0000428GenericLinkService::decodeData(const Block& netPkt, const lp::Packet& firstPkt,
429 const EndpointId& endpointId)
Eric Newberry86d31872015-09-23 16:24:59 -0700430{
431 BOOST_ASSERT(netPkt.type() == tlv::Data);
432
433 // forwarding expects Data to be created with make_shared
434 auto data = make_shared<Data>(netPkt);
435
Spyridon Mastorakise6900392016-12-06 15:20:19 -0800436 if (firstPkt.has<lp::HopCountTagField>()) {
437 data->setTag(make_shared<lp::HopCountTag>(firstPkt.get<lp::HopCountTagField>() + 1));
438 }
439
Eric Newberry86d31872015-09-23 16:24:59 -0700440 if (firstPkt.has<lp::NackField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400441 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700442 NFD_LOG_FACE_WARN("received Nack with Data: DROP");
443 return;
444 }
445
446 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400447 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700448 NFD_LOG_FACE_WARN("received NextHopFaceId with Data: DROP");
449 return;
450 }
451
452 if (firstPkt.has<lp::CachePolicyField>()) {
Junxiao Shi6eb02712017-05-27 22:48:02 +0000453 // CachePolicy is unprivileged and does not require allowLocalFields option.
454 // In case of an invalid CachePolicyType, get<lp::CachePolicyField> will throw,
455 // so it's unnecessary to check here.
456 data->setTag(make_shared<lp::CachePolicyTag>(firstPkt.get<lp::CachePolicyField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700457 }
458
459 if (firstPkt.has<lp::IncomingFaceIdField>()) {
460 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
461 }
462
Eric Newberryee400b52016-11-24 14:12:48 +0000463 if (firstPkt.has<lp::CongestionMarkField>()) {
464 data->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
465 }
466
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700467 if (firstPkt.has<lp::NonDiscoveryField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400468 ++nInNetInvalid;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700469 NFD_LOG_FACE_WARN("received NonDiscovery with Data: DROP");
470 return;
471 }
472
473 if (firstPkt.has<lp::PrefixAnnouncementField>()) {
474 if (m_options.allowSelfLearning) {
475 data->setTag(make_shared<lp::PrefixAnnouncementTag>(firstPkt.get<lp::PrefixAnnouncementField>()));
476 }
477 else {
478 NFD_LOG_FACE_WARN("received PrefixAnnouncement, but self-learning disabled: IGNORE");
479 }
480 }
481
ashiqopu075bb7d2019-03-10 01:38:21 +0000482 this->receiveData(*data, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700483}
484
485void
ashiqopu075bb7d2019-03-10 01:38:21 +0000486GenericLinkService::decodeNack(const Block& netPkt, const lp::Packet& firstPkt,
487 const EndpointId& endpointId)
Eric Newberry86d31872015-09-23 16:24:59 -0700488{
489 BOOST_ASSERT(netPkt.type() == tlv::Interest);
490 BOOST_ASSERT(firstPkt.has<lp::NackField>());
491
492 lp::Nack nack((Interest(netPkt)));
493 nack.setHeader(firstPkt.get<lp::NackField>());
494
495 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400496 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700497 NFD_LOG_FACE_WARN("received NextHopFaceId with Nack: DROP");
498 return;
499 }
500
501 if (firstPkt.has<lp::CachePolicyField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400502 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700503 NFD_LOG_FACE_WARN("received CachePolicy with Nack: DROP");
504 return;
505 }
506
507 if (firstPkt.has<lp::IncomingFaceIdField>()) {
508 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
509 }
510
Eric Newberryee400b52016-11-24 14:12:48 +0000511 if (firstPkt.has<lp::CongestionMarkField>()) {
512 nack.setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
513 }
514
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700515 if (firstPkt.has<lp::NonDiscoveryField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400516 ++nInNetInvalid;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700517 NFD_LOG_FACE_WARN("received NonDiscovery with Nack: DROP");
518 return;
519 }
520
521 if (firstPkt.has<lp::PrefixAnnouncementField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400522 ++nInNetInvalid;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700523 NFD_LOG_FACE_WARN("received PrefixAnnouncement with Nack: DROP");
524 return;
525 }
526
ashiqopu075bb7d2019-03-10 01:38:21 +0000527 this->receiveNack(nack, endpointId);
Eric Newberrya98bf932015-09-21 00:58:47 -0700528}
529
530} // namespace face
531} // namespace nfd