blob: bdb69063669c9f03b6ed9ba675cc2ee86b53da06 [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 Pesaventoa8098582019-03-31 15:48:02 -04003 * Copyright (c) 2014-2019, 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)
Eric Newberryb49313d2017-12-24 20:22:27 -070048 , m_nextMarkTime(time::steady_clock::TimePoint::max())
Eric Newberryb49313d2017-12-24 20:22:27 -070049 , m_nMarkedSinceInMarkingState(0)
Eric Newberry86d31872015-09-23 16:24:59 -070050{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040051 m_reassembler.beforeTimeout.connect([this] (auto...) { ++this->nReassemblyTimeouts; });
52 m_reliability.onDroppedInterest.connect([this] (const auto& i) { this->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
65void
ashiqopu075bb7d2019-03-10 01:38:21 +000066GenericLinkService::requestIdlePacket(const EndpointId& endpointId)
Eric Newberry185ab292017-03-28 06:45:39 +000067{
68 // No need to request Acks to attach to this packet from LpReliability, as they are already
69 // attached in sendLpPacket
ashiqopu075bb7d2019-03-10 01:38:21 +000070 this->sendLpPacket({}, endpointId);
Eric Newberry185ab292017-03-28 06:45:39 +000071}
72
73void
ashiqopu075bb7d2019-03-10 01:38:21 +000074GenericLinkService::sendLpPacket(lp::Packet&& pkt, const EndpointId& endpointId)
Eric Newberry185ab292017-03-28 06:45:39 +000075{
76 const ssize_t mtu = this->getTransport()->getMtu();
Eric Newberryb49313d2017-12-24 20:22:27 -070077
Eric Newberry185ab292017-03-28 06:45:39 +000078 if (m_options.reliabilityOptions.isEnabled) {
79 m_reliability.piggyback(pkt, mtu);
80 }
81
Eric Newberryb49313d2017-12-24 20:22:27 -070082 if (m_options.allowCongestionMarking) {
83 checkCongestionLevel(pkt);
84 }
85
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040086 auto block = pkt.wireEncode();
87 if (mtu != MTU_UNLIMITED && block.size() > static_cast<size_t>(mtu)) {
Eric Newberry185ab292017-03-28 06:45:39 +000088 ++this->nOutOverMtu;
89 NFD_LOG_FACE_WARN("attempted to send packet over MTU limit");
90 return;
91 }
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040092 this->sendPacket(block, endpointId);
Eric Newberry185ab292017-03-28 06:45:39 +000093}
94
95void
ashiqopu075bb7d2019-03-10 01:38:21 +000096GenericLinkService::doSendInterest(const Interest& interest, const EndpointId& endpointId)
Eric Newberrya98bf932015-09-21 00:58:47 -070097{
98 lp::Packet lpPacket(interest.wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +000099
Eric Newberryee400b52016-11-24 14:12:48 +0000100 encodeLpFields(interest, lpPacket);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700101
ashiqopu075bb7d2019-03-10 01:38:21 +0000102 this->sendNetPacket(std::move(lpPacket), endpointId, true);
Eric Newberrya98bf932015-09-21 00:58:47 -0700103}
104
105void
ashiqopu075bb7d2019-03-10 01:38:21 +0000106GenericLinkService::doSendData(const Data& data, const EndpointId& endpointId)
Eric Newberrya98bf932015-09-21 00:58:47 -0700107{
108 lp::Packet lpPacket(data.wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000109
Eric Newberryee400b52016-11-24 14:12:48 +0000110 encodeLpFields(data, lpPacket);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700111
ashiqopu075bb7d2019-03-10 01:38:21 +0000112 this->sendNetPacket(std::move(lpPacket), endpointId, false);
Eric Newberrya98bf932015-09-21 00:58:47 -0700113}
114
115void
ashiqopu075bb7d2019-03-10 01:38:21 +0000116GenericLinkService::doSendNack(const lp::Nack& nack, const EndpointId& endpointId)
Eric Newberrya98bf932015-09-21 00:58:47 -0700117{
118 lp::Packet lpPacket(nack.getInterest().wireEncode());
119 lpPacket.add<lp::NackField>(nack.getHeader());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000120
Eric Newberryee400b52016-11-24 14:12:48 +0000121 encodeLpFields(nack, lpPacket);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700122
ashiqopu075bb7d2019-03-10 01:38:21 +0000123 this->sendNetPacket(std::move(lpPacket), endpointId, false);
Eric Newberrya98bf932015-09-21 00:58:47 -0700124}
125
Junxiao Shi0de23a22015-12-03 20:07:02 +0000126void
Eric Newberry41aba102017-11-01 16:42:13 -0700127GenericLinkService::encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket)
Eric Newberry86d31872015-09-23 16:24:59 -0700128{
Eric Newberryee400b52016-11-24 14:12:48 +0000129 if (m_options.allowLocalFields) {
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600130 auto incomingFaceIdTag = netPkt.getTag<lp::IncomingFaceIdTag>();
Eric Newberryee400b52016-11-24 14:12:48 +0000131 if (incomingFaceIdTag != nullptr) {
132 lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag);
133 }
134 }
135
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600136 auto congestionMarkTag = netPkt.getTag<lp::CongestionMarkTag>();
Eric Newberryee400b52016-11-24 14:12:48 +0000137 if (congestionMarkTag != nullptr) {
138 lpPacket.add<lp::CongestionMarkField>(*congestionMarkTag);
Eric Newberry86d31872015-09-23 16:24:59 -0700139 }
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700140
141 if (m_options.allowSelfLearning) {
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600142 auto nonDiscoveryTag = netPkt.getTag<lp::NonDiscoveryTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700143 if (nonDiscoveryTag != nullptr) {
144 lpPacket.add<lp::NonDiscoveryField>(*nonDiscoveryTag);
145 }
146
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600147 auto prefixAnnouncementTag = netPkt.getTag<lp::PrefixAnnouncementTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700148 if (prefixAnnouncementTag != nullptr) {
149 lpPacket.add<lp::PrefixAnnouncementField>(*prefixAnnouncementTag);
150 }
151 }
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600152
153 auto pitToken = netPkt.getTag<lp::PitToken>();
154 if (pitToken != nullptr) {
155 lpPacket.add<lp::PitTokenField>(*pitToken);
156 }
Spyridon Mastorakisfc5711a2016-12-06 15:20:19 -0800157
158 shared_ptr<lp::HopCountTag> hopCountTag = netPkt.getTag<lp::HopCountTag>();
159 if (hopCountTag != nullptr) {
160 lpPacket.add<lp::HopCountTagField>(*hopCountTag);
161 }
162 else {
163 lpPacket.add<lp::HopCountTagField>(0);
164 }
Eric Newberry86d31872015-09-23 16:24:59 -0700165}
166
Eric Newberrya98bf932015-09-21 00:58:47 -0700167void
ashiqopu075bb7d2019-03-10 01:38:21 +0000168GenericLinkService::sendNetPacket(lp::Packet&& pkt, const EndpointId& endpointId, bool isInterest)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700169{
170 std::vector<lp::Packet> frags;
Eric Newberry7b0071e2017-07-03 17:33:31 +0000171 ssize_t mtu = this->getTransport()->getMtu();
172
173 // Make space for feature fields in fragments
174 if (m_options.reliabilityOptions.isEnabled && mtu != MTU_UNLIMITED) {
175 mtu -= LpReliability::RESERVED_HEADER_SPACE;
Eric Newberry7b0071e2017-07-03 17:33:31 +0000176 }
177
Eric Newberryb49313d2017-12-24 20:22:27 -0700178 if (m_options.allowCongestionMarking && mtu != MTU_UNLIMITED) {
179 mtu -= CONGESTION_MARK_SIZE;
180 }
181
182 BOOST_ASSERT(mtu == MTU_UNLIMITED || mtu > 0);
183
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700184 if (m_options.allowFragmentation && mtu != MTU_UNLIMITED) {
185 bool isOk = false;
186 std::tie(isOk, frags) = m_fragmenter.fragmentPacket(pkt, mtu);
187 if (!isOk) {
188 // fragmentation failed (warning is logged by LpFragmenter)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000189 ++this->nFragmentationErrors;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700190 return;
191 }
192 }
193 else {
Eric Newberry41aba102017-11-01 16:42:13 -0700194 if (m_options.reliabilityOptions.isEnabled) {
195 frags.push_back(pkt);
196 }
197 else {
198 frags.push_back(std::move(pkt));
199 }
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700200 }
201
Eric Newberry185ab292017-03-28 06:45:39 +0000202 if (frags.size() == 1) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700203 // even if indexed fragmentation is enabled, the fragmenter should not
204 // fragment the packet if it can fit in MTU
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700205 BOOST_ASSERT(!frags.front().has<lp::FragIndexField>());
206 BOOST_ASSERT(!frags.front().has<lp::FragCountField>());
207 }
208
Eric Newberry7b0071e2017-07-03 17:33:31 +0000209 // Only assign sequences to fragments if packet contains more than 1 fragment
210 if (frags.size() > 1) {
Eric Newberry185ab292017-03-28 06:45:39 +0000211 // Assign sequences to all fragments
212 this->assignSequences(frags);
213 }
214
215 if (m_options.reliabilityOptions.isEnabled && frags.front().has<lp::FragmentField>()) {
Eric Newberry41aba102017-11-01 16:42:13 -0700216 m_reliability.handleOutgoing(frags, std::move(pkt), isInterest);
Eric Newberry185ab292017-03-28 06:45:39 +0000217 }
218
219 for (lp::Packet& frag : frags) {
ashiqopu075bb7d2019-03-10 01:38:21 +0000220 this->sendLpPacket(std::move(frag), endpointId);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700221 }
222}
223
224void
225GenericLinkService::assignSequence(lp::Packet& pkt)
226{
227 pkt.set<lp::SequenceField>(++m_lastSeqNo);
228}
229
230void
231GenericLinkService::assignSequences(std::vector<lp::Packet>& pkts)
232{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400233 std::for_each(pkts.begin(), pkts.end(), [this] (auto& pkt) { this->assignSequence(pkt); });
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700234}
235
236void
Eric Newberryb49313d2017-12-24 20:22:27 -0700237GenericLinkService::checkCongestionLevel(lp::Packet& pkt)
238{
239 ssize_t sendQueueLength = getTransport()->getSendQueueLength();
Klaus Schneider380668b2019-10-02 01:21:13 -0700240 // The transport must support retrieving the current send queue length
Eric Newberryb49313d2017-12-24 20:22:27 -0700241 if (sendQueueLength < 0) {
242 return;
243 }
244
Eric Newberryb49313d2017-12-24 20:22:27 -0700245 if (sendQueueLength > 0) {
Klaus Schneider380668b2019-10-02 01:21:13 -0700246 NFD_LOG_FACE_TRACE("txqlen=" << sendQueueLength << " threshold=" <<
247 m_options.defaultCongestionThreshold << " capacity=" <<
248 getTransport()->getSendQueueCapacity());
Eric Newberryb49313d2017-12-24 20:22:27 -0700249 }
250
Klaus Schneider380668b2019-10-02 01:21:13 -0700251 // sendQueue is above target
252 if (static_cast<size_t>(sendQueueLength) > m_options.defaultCongestionThreshold) {
Eric Newberryb49313d2017-12-24 20:22:27 -0700253 const auto now = time::steady_clock::now();
Eric Newberryb49313d2017-12-24 20:22:27 -0700254
Klaus Schneider380668b2019-10-02 01:21:13 -0700255 if (m_nextMarkTime == time::steady_clock::TimePoint::max()) {
256 m_nextMarkTime = now + m_options.baseCongestionMarkingInterval;
257 }
258 // Mark packet if sendQueue stays above target for one interval
259 else if (now >= m_nextMarkTime) {
Eric Newberryb49313d2017-12-24 20:22:27 -0700260 pkt.set<lp::CongestionMarkField>(1);
261 ++nCongestionMarked;
262 NFD_LOG_FACE_DEBUG("LpPacket was marked as congested");
263
264 ++m_nMarkedSinceInMarkingState;
265 // Decrease the marking interval by the inverse of the square root of the number of packets
266 // marked in this incident of congestion
Klaus Schneider380668b2019-10-02 01:21:13 -0700267 time::nanoseconds interval(static_cast<time::nanoseconds::rep>(
268 m_options.baseCongestionMarkingInterval.count() /
269 std::sqrt(m_nMarkedSinceInMarkingState + 1)));
270 m_nextMarkTime += interval;
Eric Newberryb49313d2017-12-24 20:22:27 -0700271 }
272 }
273 else if (m_nextMarkTime != time::steady_clock::TimePoint::max()) {
274 // Congestion incident has ended, so reset
275 NFD_LOG_FACE_DEBUG("Send queue length dropped below congestion threshold");
276 m_nextMarkTime = time::steady_clock::TimePoint::max();
277 m_nMarkedSinceInMarkingState = 0;
278 }
279}
280
281void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400282GenericLinkService::doReceivePacket(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -0700283{
Eric Newberry86d31872015-09-23 16:24:59 -0700284 try {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400285 lp::Packet pkt(packet);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700286
Eric Newberry185ab292017-03-28 06:45:39 +0000287 if (m_options.reliabilityOptions.isEnabled) {
288 m_reliability.processIncomingPacket(pkt);
289 }
290
Eric Newberrya1939ba2015-10-09 12:35:03 -0700291 if (!pkt.has<lp::FragmentField>()) {
292 NFD_LOG_FACE_TRACE("received IDLE packet: DROP");
293 return;
294 }
295
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700296 if ((pkt.has<lp::FragIndexField>() || pkt.has<lp::FragCountField>()) &&
297 !m_options.allowReassembly) {
298 NFD_LOG_FACE_WARN("received fragment, but reassembly disabled: DROP");
Eric Newberrya1939ba2015-10-09 12:35:03 -0700299 return;
300 }
301
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700302 bool isReassembled = false;
303 Block netPkt;
304 lp::Packet firstPkt;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400305 std::tie(isReassembled, netPkt, firstPkt) = m_reassembler.receiveFragment(endpoint, pkt);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700306 if (isReassembled) {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400307 this->decodeNetPacket(netPkt, firstPkt, endpoint);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700308 }
309 }
310 catch (const tlv::Error& e) {
311 ++this->nInLpInvalid;
312 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
313 }
314}
Eric Newberry86d31872015-09-23 16:24:59 -0700315
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700316void
ashiqopu075bb7d2019-03-10 01:38:21 +0000317GenericLinkService::decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt,
318 const EndpointId& endpointId)
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700319{
320 try {
Eric Newberry86d31872015-09-23 16:24:59 -0700321 switch (netPkt.type()) {
322 case tlv::Interest:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700323 if (firstPkt.has<lp::NackField>()) {
ashiqopu075bb7d2019-03-10 01:38:21 +0000324 this->decodeNack(netPkt, firstPkt, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700325 }
326 else {
ashiqopu075bb7d2019-03-10 01:38:21 +0000327 this->decodeInterest(netPkt, firstPkt, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700328 }
329 break;
330 case tlv::Data:
ashiqopu075bb7d2019-03-10 01:38:21 +0000331 this->decodeData(netPkt, firstPkt, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700332 break;
333 default:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700334 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700335 NFD_LOG_FACE_WARN("unrecognized network-layer packet TLV-TYPE " << netPkt.type() << ": DROP");
336 return;
Eric Newberrya98bf932015-09-21 00:58:47 -0700337 }
338 }
Eric Newberry86d31872015-09-23 16:24:59 -0700339 catch (const tlv::Error& e) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700340 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700341 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
342 }
343}
344
Eric Newberry86d31872015-09-23 16:24:59 -0700345void
ashiqopu075bb7d2019-03-10 01:38:21 +0000346GenericLinkService::decodeInterest(const Block& netPkt, const lp::Packet& firstPkt,
347 const EndpointId& endpointId)
Eric Newberry86d31872015-09-23 16:24:59 -0700348{
349 BOOST_ASSERT(netPkt.type() == tlv::Interest);
350 BOOST_ASSERT(!firstPkt.has<lp::NackField>());
351
352 // forwarding expects Interest to be created with make_shared
353 auto interest = make_shared<Interest>(netPkt);
354
Spyridon Mastorakisfc5711a2016-12-06 15:20:19 -0800355 // Increment HopCount
356 if (firstPkt.has<lp::HopCountTagField>()) {
357 interest->setTag(make_shared<lp::HopCountTag>(firstPkt.get<lp::HopCountTagField>() + 1));
358 }
359
Eric Newberry86d31872015-09-23 16:24:59 -0700360 if (firstPkt.has<lp::NextHopFaceIdField>()) {
361 if (m_options.allowLocalFields) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000362 interest->setTag(make_shared<lp::NextHopFaceIdTag>(firstPkt.get<lp::NextHopFaceIdField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700363 }
364 else {
365 NFD_LOG_FACE_WARN("received NextHopFaceId, but local fields disabled: DROP");
366 return;
367 }
368 }
369
370 if (firstPkt.has<lp::CachePolicyField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700371 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700372 NFD_LOG_FACE_WARN("received CachePolicy with Interest: DROP");
373 return;
374 }
375
376 if (firstPkt.has<lp::IncomingFaceIdField>()) {
377 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
378 }
379
Eric Newberryee400b52016-11-24 14:12:48 +0000380 if (firstPkt.has<lp::CongestionMarkField>()) {
381 interest->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
382 }
383
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700384 if (firstPkt.has<lp::NonDiscoveryField>()) {
385 if (m_options.allowSelfLearning) {
386 interest->setTag(make_shared<lp::NonDiscoveryTag>(firstPkt.get<lp::NonDiscoveryField>()));
387 }
388 else {
389 NFD_LOG_FACE_WARN("received NonDiscovery, but self-learning disabled: IGNORE");
390 }
391 }
392
393 if (firstPkt.has<lp::PrefixAnnouncementField>()) {
394 ++this->nInNetInvalid;
395 NFD_LOG_FACE_WARN("received PrefixAnnouncement with Interest: DROP");
396 return;
397 }
398
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600399 if (firstPkt.has<lp::PitTokenField>()) {
400 interest->setTag(make_shared<lp::PitToken>(firstPkt.get<lp::PitTokenField>()));
401 }
402
ashiqopu075bb7d2019-03-10 01:38:21 +0000403 this->receiveInterest(*interest, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700404}
405
406void
ashiqopu075bb7d2019-03-10 01:38:21 +0000407GenericLinkService::decodeData(const Block& netPkt, const lp::Packet& firstPkt,
408 const EndpointId& endpointId)
Eric Newberry86d31872015-09-23 16:24:59 -0700409{
410 BOOST_ASSERT(netPkt.type() == tlv::Data);
411
412 // forwarding expects Data to be created with make_shared
413 auto data = make_shared<Data>(netPkt);
414
Spyridon Mastorakisfc5711a2016-12-06 15:20:19 -0800415 if (firstPkt.has<lp::HopCountTagField>()) {
416 data->setTag(make_shared<lp::HopCountTag>(firstPkt.get<lp::HopCountTagField>() + 1));
417 }
418
Eric Newberry86d31872015-09-23 16:24:59 -0700419 if (firstPkt.has<lp::NackField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700420 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700421 NFD_LOG_FACE_WARN("received Nack with Data: DROP");
422 return;
423 }
424
425 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700426 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700427 NFD_LOG_FACE_WARN("received NextHopFaceId with Data: DROP");
428 return;
429 }
430
431 if (firstPkt.has<lp::CachePolicyField>()) {
Junxiao Shi6eb02712017-05-27 22:48:02 +0000432 // CachePolicy is unprivileged and does not require allowLocalFields option.
433 // In case of an invalid CachePolicyType, get<lp::CachePolicyField> will throw,
434 // so it's unnecessary to check here.
435 data->setTag(make_shared<lp::CachePolicyTag>(firstPkt.get<lp::CachePolicyField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700436 }
437
438 if (firstPkt.has<lp::IncomingFaceIdField>()) {
439 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
440 }
441
Eric Newberryee400b52016-11-24 14:12:48 +0000442 if (firstPkt.has<lp::CongestionMarkField>()) {
443 data->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
444 }
445
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700446 if (firstPkt.has<lp::NonDiscoveryField>()) {
447 ++this->nInNetInvalid;
448 NFD_LOG_FACE_WARN("received NonDiscovery with Data: DROP");
449 return;
450 }
451
452 if (firstPkt.has<lp::PrefixAnnouncementField>()) {
453 if (m_options.allowSelfLearning) {
454 data->setTag(make_shared<lp::PrefixAnnouncementTag>(firstPkt.get<lp::PrefixAnnouncementField>()));
455 }
456 else {
457 NFD_LOG_FACE_WARN("received PrefixAnnouncement, but self-learning disabled: IGNORE");
458 }
459 }
460
ashiqopu075bb7d2019-03-10 01:38:21 +0000461 this->receiveData(*data, endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700462}
463
464void
ashiqopu075bb7d2019-03-10 01:38:21 +0000465GenericLinkService::decodeNack(const Block& netPkt, const lp::Packet& firstPkt,
466 const EndpointId& endpointId)
Eric Newberry86d31872015-09-23 16:24:59 -0700467{
468 BOOST_ASSERT(netPkt.type() == tlv::Interest);
469 BOOST_ASSERT(firstPkt.has<lp::NackField>());
470
471 lp::Nack nack((Interest(netPkt)));
472 nack.setHeader(firstPkt.get<lp::NackField>());
473
474 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700475 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700476 NFD_LOG_FACE_WARN("received NextHopFaceId with Nack: DROP");
477 return;
478 }
479
480 if (firstPkt.has<lp::CachePolicyField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700481 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700482 NFD_LOG_FACE_WARN("received CachePolicy with Nack: DROP");
483 return;
484 }
485
486 if (firstPkt.has<lp::IncomingFaceIdField>()) {
487 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
488 }
489
Eric Newberryee400b52016-11-24 14:12:48 +0000490 if (firstPkt.has<lp::CongestionMarkField>()) {
491 nack.setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
492 }
493
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700494 if (firstPkt.has<lp::NonDiscoveryField>()) {
495 ++this->nInNetInvalid;
496 NFD_LOG_FACE_WARN("received NonDiscovery with Nack: DROP");
497 return;
498 }
499
500 if (firstPkt.has<lp::PrefixAnnouncementField>()) {
501 ++this->nInNetInvalid;
502 NFD_LOG_FACE_WARN("received PrefixAnnouncement with Nack: DROP");
503 return;
504 }
505
ashiqopu075bb7d2019-03-10 01:38:21 +0000506 this->receiveNack(nack, endpointId);
Eric Newberrya98bf932015-09-21 00:58:47 -0700507}
508
509} // namespace face
510} // namespace nfd