blob: 5ed4ac25f16c34e015bfa690306bcdfd1cf67ffc [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 }
Eric Newberry86d31872015-09-23 16:24:59 -0700185}
186
Eric Newberrya98bf932015-09-21 00:58:47 -0700187void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700188GenericLinkService::sendNetPacket(lp::Packet&& pkt, bool isInterest)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700189{
190 std::vector<lp::Packet> frags;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800191 ssize_t mtu = getEffectiveMtu();
Eric Newberry7b0071e2017-07-03 17:33:31 +0000192
193 // Make space for feature fields in fragments
194 if (m_options.reliabilityOptions.isEnabled && mtu != MTU_UNLIMITED) {
195 mtu -= LpReliability::RESERVED_HEADER_SPACE;
Eric Newberry7b0071e2017-07-03 17:33:31 +0000196 }
197
Eric Newberryb49313d2017-12-24 20:22:27 -0700198 if (m_options.allowCongestionMarking && mtu != MTU_UNLIMITED) {
199 mtu -= CONGESTION_MARK_SIZE;
200 }
201
Eric Newberryf3ee8082020-01-28 13:44:18 -0800202 // An MTU of 0 is allowed but will cause all packets to be dropped before transmission
203 BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu >= 0);
Eric Newberryb49313d2017-12-24 20:22:27 -0700204
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700205 if (m_options.allowFragmentation && mtu != MTU_UNLIMITED) {
206 bool isOk = false;
207 std::tie(isOk, frags) = m_fragmenter.fragmentPacket(pkt, mtu);
208 if (!isOk) {
209 // fragmentation failed (warning is logged by LpFragmenter)
Davide Pesavento412c9822021-07-02 00:21:05 -0400210 ++nFragmentationErrors;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700211 return;
212 }
213 }
214 else {
Eric Newberry41aba102017-11-01 16:42:13 -0700215 if (m_options.reliabilityOptions.isEnabled) {
216 frags.push_back(pkt);
217 }
218 else {
219 frags.push_back(std::move(pkt));
220 }
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700221 }
222
Eric Newberry185ab292017-03-28 06:45:39 +0000223 if (frags.size() == 1) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700224 // even if indexed fragmentation is enabled, the fragmenter should not
225 // fragment the packet if it can fit in MTU
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700226 BOOST_ASSERT(!frags.front().has<lp::FragIndexField>());
227 BOOST_ASSERT(!frags.front().has<lp::FragCountField>());
228 }
229
Eric Newberry32f7eac2020-02-07 14:40:17 -0800230 // Only assign sequences to fragments if reliability enabled or if packet contains >1 fragment
231 if (m_options.reliabilityOptions.isEnabled || frags.size() > 1) {
Eric Newberry185ab292017-03-28 06:45:39 +0000232 // Assign sequences to all fragments
233 this->assignSequences(frags);
234 }
235
236 if (m_options.reliabilityOptions.isEnabled && frags.front().has<lp::FragmentField>()) {
Eric Newberry41aba102017-11-01 16:42:13 -0700237 m_reliability.handleOutgoing(frags, std::move(pkt), isInterest);
Eric Newberry185ab292017-03-28 06:45:39 +0000238 }
239
240 for (lp::Packet& frag : frags) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700241 this->sendLpPacket(std::move(frag));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700242 }
243}
244
245void
Eric Newberryb49313d2017-12-24 20:22:27 -0700246GenericLinkService::checkCongestionLevel(lp::Packet& pkt)
247{
248 ssize_t sendQueueLength = getTransport()->getSendQueueLength();
Klaus Schneider380668b2019-10-02 01:21:13 -0700249 // The transport must support retrieving the current send queue length
Eric Newberryb49313d2017-12-24 20:22:27 -0700250 if (sendQueueLength < 0) {
251 return;
252 }
253
Eric Newberryb49313d2017-12-24 20:22:27 -0700254 if (sendQueueLength > 0) {
Klaus Schneider380668b2019-10-02 01:21:13 -0700255 NFD_LOG_FACE_TRACE("txqlen=" << sendQueueLength << " threshold=" <<
256 m_options.defaultCongestionThreshold << " capacity=" <<
257 getTransport()->getSendQueueCapacity());
Eric Newberryb49313d2017-12-24 20:22:27 -0700258 }
259
Klaus Schneider380668b2019-10-02 01:21:13 -0700260 // sendQueue is above target
261 if (static_cast<size_t>(sendQueueLength) > m_options.defaultCongestionThreshold) {
Eric Newberryb49313d2017-12-24 20:22:27 -0700262 const auto now = time::steady_clock::now();
Eric Newberryb49313d2017-12-24 20:22:27 -0700263
Davide Pesavento412c9822021-07-02 00:21:05 -0400264 if (m_nextMarkTime == time::steady_clock::time_point::max()) {
Klaus Schneider380668b2019-10-02 01:21:13 -0700265 m_nextMarkTime = now + m_options.baseCongestionMarkingInterval;
266 }
267 // Mark packet if sendQueue stays above target for one interval
268 else if (now >= m_nextMarkTime) {
Eric Newberryb49313d2017-12-24 20:22:27 -0700269 pkt.set<lp::CongestionMarkField>(1);
270 ++nCongestionMarked;
271 NFD_LOG_FACE_DEBUG("LpPacket was marked as congested");
272
273 ++m_nMarkedSinceInMarkingState;
274 // Decrease the marking interval by the inverse of the square root of the number of packets
275 // marked in this incident of congestion
Klaus Schneider380668b2019-10-02 01:21:13 -0700276 time::nanoseconds interval(static_cast<time::nanoseconds::rep>(
277 m_options.baseCongestionMarkingInterval.count() /
278 std::sqrt(m_nMarkedSinceInMarkingState + 1)));
279 m_nextMarkTime += interval;
Eric Newberryb49313d2017-12-24 20:22:27 -0700280 }
281 }
Davide Pesavento412c9822021-07-02 00:21:05 -0400282 else if (m_nextMarkTime != time::steady_clock::time_point::max()) {
Eric Newberryb49313d2017-12-24 20:22:27 -0700283 // Congestion incident has ended, so reset
284 NFD_LOG_FACE_DEBUG("Send queue length dropped below congestion threshold");
Davide Pesavento412c9822021-07-02 00:21:05 -0400285 m_nextMarkTime = time::steady_clock::time_point::max();
Eric Newberryb49313d2017-12-24 20:22:27 -0700286 m_nMarkedSinceInMarkingState = 0;
287 }
288}
289
290void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400291GenericLinkService::doReceivePacket(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -0700292{
Eric Newberry86d31872015-09-23 16:24:59 -0700293 try {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400294 lp::Packet pkt(packet);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700295
Eric Newberry185ab292017-03-28 06:45:39 +0000296 if (m_options.reliabilityOptions.isEnabled) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800297 if (!m_reliability.processIncomingPacket(pkt)) {
298 NFD_LOG_FACE_TRACE("received duplicate fragment: DROP");
Davide Pesavento412c9822021-07-02 00:21:05 -0400299 ++nDuplicateSequence;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800300 return;
301 }
Eric Newberry185ab292017-03-28 06:45:39 +0000302 }
303
Eric Newberrya1939ba2015-10-09 12:35:03 -0700304 if (!pkt.has<lp::FragmentField>()) {
305 NFD_LOG_FACE_TRACE("received IDLE packet: DROP");
306 return;
307 }
308
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700309 if ((pkt.has<lp::FragIndexField>() || pkt.has<lp::FragCountField>()) &&
310 !m_options.allowReassembly) {
311 NFD_LOG_FACE_WARN("received fragment, but reassembly disabled: DROP");
Eric Newberrya1939ba2015-10-09 12:35:03 -0700312 return;
313 }
314
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700315 bool isReassembled = false;
316 Block netPkt;
317 lp::Packet firstPkt;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400318 std::tie(isReassembled, netPkt, firstPkt) = m_reassembler.receiveFragment(endpoint, pkt);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700319 if (isReassembled) {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400320 this->decodeNetPacket(netPkt, firstPkt, endpoint);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700321 }
322 }
323 catch (const tlv::Error& e) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400324 ++nInLpInvalid;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700325 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
326 }
327}
Eric Newberry86d31872015-09-23 16:24:59 -0700328
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700329void
ashiqopu075bb7d2019-03-10 01:38:21 +0000330GenericLinkService::decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt,
331 const EndpointId& endpointId)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700332{
333 try {
Eric Newberry86d31872015-09-23 16:24:59 -0700334 switch (netPkt.type()) {
335 case tlv::Interest:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700336 if (firstPkt.has<lp::NackField>()) {
ashiqopu075bb7d2019-03-10 01:38:21 +0000337 this->decodeNack(netPkt, firstPkt, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700338 }
339 else {
ashiqopu075bb7d2019-03-10 01:38:21 +0000340 this->decodeInterest(netPkt, firstPkt, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700341 }
342 break;
343 case tlv::Data:
ashiqopu075bb7d2019-03-10 01:38:21 +0000344 this->decodeData(netPkt, firstPkt, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700345 break;
346 default:
Davide Pesavento412c9822021-07-02 00:21:05 -0400347 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700348 NFD_LOG_FACE_WARN("unrecognized network-layer packet TLV-TYPE " << netPkt.type() << ": DROP");
349 return;
Eric Newberrya98bf932015-09-21 00:58:47 -0700350 }
351 }
Eric Newberry86d31872015-09-23 16:24:59 -0700352 catch (const tlv::Error& e) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400353 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700354 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
355 }
356}
357
Eric Newberry86d31872015-09-23 16:24:59 -0700358void
ashiqopu075bb7d2019-03-10 01:38:21 +0000359GenericLinkService::decodeInterest(const Block& netPkt, const lp::Packet& firstPkt,
360 const EndpointId& endpointId)
Eric Newberry86d31872015-09-23 16:24:59 -0700361{
362 BOOST_ASSERT(netPkt.type() == tlv::Interest);
363 BOOST_ASSERT(!firstPkt.has<lp::NackField>());
364
365 // forwarding expects Interest to be created with make_shared
366 auto interest = make_shared<Interest>(netPkt);
367
368 if (firstPkt.has<lp::NextHopFaceIdField>()) {
369 if (m_options.allowLocalFields) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000370 interest->setTag(make_shared<lp::NextHopFaceIdTag>(firstPkt.get<lp::NextHopFaceIdField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700371 }
372 else {
373 NFD_LOG_FACE_WARN("received NextHopFaceId, but local fields disabled: DROP");
374 return;
375 }
376 }
377
378 if (firstPkt.has<lp::CachePolicyField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400379 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700380 NFD_LOG_FACE_WARN("received CachePolicy with Interest: DROP");
381 return;
382 }
383
384 if (firstPkt.has<lp::IncomingFaceIdField>()) {
385 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
386 }
387
Eric Newberryee400b52016-11-24 14:12:48 +0000388 if (firstPkt.has<lp::CongestionMarkField>()) {
389 interest->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
390 }
391
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700392 if (firstPkt.has<lp::NonDiscoveryField>()) {
393 if (m_options.allowSelfLearning) {
394 interest->setTag(make_shared<lp::NonDiscoveryTag>(firstPkt.get<lp::NonDiscoveryField>()));
395 }
396 else {
397 NFD_LOG_FACE_WARN("received NonDiscovery, but self-learning disabled: IGNORE");
398 }
399 }
400
401 if (firstPkt.has<lp::PrefixAnnouncementField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400402 ++nInNetInvalid;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700403 NFD_LOG_FACE_WARN("received PrefixAnnouncement with Interest: DROP");
404 return;
405 }
406
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600407 if (firstPkt.has<lp::PitTokenField>()) {
408 interest->setTag(make_shared<lp::PitToken>(firstPkt.get<lp::PitTokenField>()));
409 }
410
ashiqopu075bb7d2019-03-10 01:38:21 +0000411 this->receiveInterest(*interest, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700412}
413
414void
ashiqopu075bb7d2019-03-10 01:38:21 +0000415GenericLinkService::decodeData(const Block& netPkt, const lp::Packet& firstPkt,
416 const EndpointId& endpointId)
Eric Newberry86d31872015-09-23 16:24:59 -0700417{
418 BOOST_ASSERT(netPkt.type() == tlv::Data);
419
420 // forwarding expects Data to be created with make_shared
421 auto data = make_shared<Data>(netPkt);
422
423 if (firstPkt.has<lp::NackField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400424 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700425 NFD_LOG_FACE_WARN("received Nack with Data: DROP");
426 return;
427 }
428
429 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400430 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700431 NFD_LOG_FACE_WARN("received NextHopFaceId with Data: DROP");
432 return;
433 }
434
435 if (firstPkt.has<lp::CachePolicyField>()) {
Junxiao Shi6eb02712017-05-27 22:48:02 +0000436 // CachePolicy is unprivileged and does not require allowLocalFields option.
437 // In case of an invalid CachePolicyType, get<lp::CachePolicyField> will throw,
438 // so it's unnecessary to check here.
439 data->setTag(make_shared<lp::CachePolicyTag>(firstPkt.get<lp::CachePolicyField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700440 }
441
442 if (firstPkt.has<lp::IncomingFaceIdField>()) {
443 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
444 }
445
Eric Newberryee400b52016-11-24 14:12:48 +0000446 if (firstPkt.has<lp::CongestionMarkField>()) {
447 data->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
448 }
449
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700450 if (firstPkt.has<lp::NonDiscoveryField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400451 ++nInNetInvalid;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700452 NFD_LOG_FACE_WARN("received NonDiscovery with Data: DROP");
453 return;
454 }
455
456 if (firstPkt.has<lp::PrefixAnnouncementField>()) {
457 if (m_options.allowSelfLearning) {
458 data->setTag(make_shared<lp::PrefixAnnouncementTag>(firstPkt.get<lp::PrefixAnnouncementField>()));
459 }
460 else {
461 NFD_LOG_FACE_WARN("received PrefixAnnouncement, but self-learning disabled: IGNORE");
462 }
463 }
464
ashiqopu075bb7d2019-03-10 01:38:21 +0000465 this->receiveData(*data, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700466}
467
468void
ashiqopu075bb7d2019-03-10 01:38:21 +0000469GenericLinkService::decodeNack(const Block& netPkt, const lp::Packet& firstPkt,
470 const EndpointId& endpointId)
Eric Newberry86d31872015-09-23 16:24:59 -0700471{
472 BOOST_ASSERT(netPkt.type() == tlv::Interest);
473 BOOST_ASSERT(firstPkt.has<lp::NackField>());
474
475 lp::Nack nack((Interest(netPkt)));
476 nack.setHeader(firstPkt.get<lp::NackField>());
477
478 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400479 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700480 NFD_LOG_FACE_WARN("received NextHopFaceId with Nack: DROP");
481 return;
482 }
483
484 if (firstPkt.has<lp::CachePolicyField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400485 ++nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700486 NFD_LOG_FACE_WARN("received CachePolicy with Nack: DROP");
487 return;
488 }
489
490 if (firstPkt.has<lp::IncomingFaceIdField>()) {
491 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
492 }
493
Eric Newberryee400b52016-11-24 14:12:48 +0000494 if (firstPkt.has<lp::CongestionMarkField>()) {
495 nack.setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
496 }
497
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700498 if (firstPkt.has<lp::NonDiscoveryField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400499 ++nInNetInvalid;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700500 NFD_LOG_FACE_WARN("received NonDiscovery with Nack: DROP");
501 return;
502 }
503
504 if (firstPkt.has<lp::PrefixAnnouncementField>()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400505 ++nInNetInvalid;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700506 NFD_LOG_FACE_WARN("received PrefixAnnouncement with Nack: DROP");
507 return;
508 }
509
ashiqopu075bb7d2019-03-10 01:38:21 +0000510 this->receiveNack(nack, endpointId);
Eric Newberrya98bf932015-09-21 00:58:47 -0700511}
512
513} // namespace face
514} // namespace nfd