blob: 7b5a427f9115a61acea4696b9e66fa72489e13a8 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry41aba102017-11-01 16:42:13 -07002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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#ifndef NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP
27#define NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP
28
Eric Newberrya98bf932015-09-21 00:58:47 -070029#include "link-service.hpp"
Eric Newberry4c3e6b82015-11-10 16:48:42 -070030#include "lp-fragmenter.hpp"
31#include "lp-reassembler.hpp"
Eric Newberry185ab292017-03-28 06:45:39 +000032#include "lp-reliability.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070033
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::face {
Eric Newberrya98bf932015-09-21 00:58:47 -070035
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040036/** \brief Counters provided by GenericLinkService.
37 * \note The type name GenericLinkServiceCounters is an implementation detail.
38 * Use GenericLinkService::Counters in public API.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070039 */
40class GenericLinkServiceCounters : public virtual LinkService::Counters
41{
42public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040043 /** \brief Count of failed fragmentations.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070044 */
45 PacketCounter nFragmentationErrors;
46
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040047 /** \brief Count of outgoing LpPackets dropped due to exceeding MTU limit.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070048 *
49 * If this counter is non-zero, the operator should enable fragmentation.
50 */
51 PacketCounter nOutOverMtu;
52
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040053 /** \brief Count of invalid LpPackets dropped before reassembly.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070054 */
55 PacketCounter nInLpInvalid;
56
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040057 /** \brief Count of network-layer packets currently being reassembled.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070058 */
59 SizeCounter<LpReassembler> nReassembling;
60
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040061 /** \brief Count of dropped partial network-layer packets due to reassembly timeout.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070062 */
63 PacketCounter nReassemblyTimeouts;
64
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040065 /** \brief Count of invalid reassembled network-layer packets dropped.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070066 */
67 PacketCounter nInNetInvalid;
Eric Newberry185ab292017-03-28 06:45:39 +000068
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040069 /** \brief Count of network-layer packets that did not require retransmission of a fragment.
Eric Newberry185ab292017-03-28 06:45:39 +000070 */
71 PacketCounter nAcknowledged;
72
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040073 /** \brief Count of network-layer packets that had at least one fragment retransmitted, but were
74 * eventually received in full.
Eric Newberry185ab292017-03-28 06:45:39 +000075 */
76 PacketCounter nRetransmitted;
77
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040078 /** \brief Count of network-layer packets dropped because a fragment reached the maximum number
79 * of retransmissions.
Eric Newberry185ab292017-03-28 06:45:39 +000080 */
81 PacketCounter nRetxExhausted;
Eric Newberryb49313d2017-12-24 20:22:27 -070082
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040083 /** \brief Count of LpPackets dropped due to duplicate Sequence numbers.
Eric Newberry32f7eac2020-02-07 14:40:17 -080084 */
85 PacketCounter nDuplicateSequence;
86
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040087 /** \brief Count of outgoing LpPackets that were marked with congestion marks.
Eric Newberryb49313d2017-12-24 20:22:27 -070088 */
89 PacketCounter nCongestionMarked;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070090};
91
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040092/**
93 * \brief GenericLinkService is a LinkService that implements the NDNLPv2 protocol.
94 * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
Eric Newberrya98bf932015-09-21 00:58:47 -070095 */
Davide Pesavento264af772021-02-09 21:48:24 -050096class GenericLinkService NFD_FINAL_UNLESS_WITH_TESTS : public LinkService
97 , protected virtual GenericLinkServiceCounters
Eric Newberrya98bf932015-09-21 00:58:47 -070098{
Eric Newberry86d31872015-09-23 16:24:59 -070099public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400100 /** \brief %Options that control the behavior of GenericLinkService.
Eric Newberry86d31872015-09-23 16:24:59 -0700101 */
102 class Options
103 {
104 public:
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400105 Options() noexcept
106 {
107 }
Eric Newberry86d31872015-09-23 16:24:59 -0700108
109 public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400110 /** \brief Enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy.
Eric Newberry86d31872015-09-23 16:24:59 -0700111 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400112 bool allowLocalFields = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700113
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400114 /** \brief Enables fragmentation.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700115 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400116 bool allowFragmentation = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700117
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400118 /** \brief Options for fragmentation.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700119 */
120 LpFragmenter::Options fragmenterOptions;
121
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400122 /** \brief Enables reassembly.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700123 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400124 bool allowReassembly = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700125
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400126 /** \brief Options for reassembly.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700127 */
128 LpReassembler::Options reassemblerOptions;
Eric Newberry185ab292017-03-28 06:45:39 +0000129
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400130 /** \brief Options for reliability.
Eric Newberry185ab292017-03-28 06:45:39 +0000131 */
132 LpReliability::Options reliabilityOptions;
Eric Newberryb49313d2017-12-24 20:22:27 -0700133
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400134 /** \brief Enables send queue congestion detection and marking.
Eric Newberryb49313d2017-12-24 20:22:27 -0700135 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400136 bool allowCongestionMarking = false;
Eric Newberryb49313d2017-12-24 20:22:27 -0700137
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400138 /** \brief Starting value for congestion marking interval.
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400139 *
Klaus Schneider380668b2019-10-02 01:21:13 -0700140 * Packets are marked if the queue size stays above THRESHOLD for at least one INTERVAL.
141 *
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400142 * The default value (100 ms) is taken from RFC 8289 (CoDel).
Eric Newberryb49313d2017-12-24 20:22:27 -0700143 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400144 time::nanoseconds baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700145
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400146 /** \brief Default congestion threshold in bytes.
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400147 *
Klaus Schneider380668b2019-10-02 01:21:13 -0700148 * Packets are marked if the queue size stays above THRESHOLD for at least one INTERVAL.
149 *
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400150 * The default value (64 KiB) works well for a queue capacity of 200 KiB.
Eric Newberryb49313d2017-12-24 20:22:27 -0700151 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400152 size_t defaultCongestionThreshold = 65536;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700153
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400154 /** \brief Enables self-learning forwarding support.
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700155 */
Teng Liang39465c22018-11-12 19:43:04 -0700156 bool allowSelfLearning = true;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800157
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400158 /** \brief Overrides the MTU provided by Transport.
Eric Newberrycb6551e2020-03-02 14:12:16 -0800159 *
160 * This MTU value will be used instead of the MTU provided by the transport if it is less than
161 * the transport MTU. However, it will not be utilized when the transport MTU is unlimited.
162 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400163 * Acceptable values for this option are values >= #MIN_MTU, which can be validated before
Eric Newberrycb6551e2020-03-02 14:12:16 -0800164 * being set with canOverrideMtuTo().
165 */
166 ssize_t overrideMtu = std::numeric_limits<ssize_t>::max();
Eric Newberry86d31872015-09-23 16:24:59 -0700167 };
168
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400169 /** \brief %Counters provided by GenericLinkService.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700170 */
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000171 using Counters = GenericLinkServiceCounters;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700172
Eric Newberry86d31872015-09-23 16:24:59 -0700173 explicit
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400174 GenericLinkService(const Options& options = {});
Eric Newberry86d31872015-09-23 16:24:59 -0700175
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400176 /** \brief Get the options used by GenericLinkService.
Eric Newberry86d31872015-09-23 16:24:59 -0700177 */
178 const Options&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400179 getOptions() const
180 {
181 return m_options;
182 }
Eric Newberry86d31872015-09-23 16:24:59 -0700183
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400184 /** \brief Sets the options used by GenericLinkService.
Eric Newberry86d31872015-09-23 16:24:59 -0700185 */
186 void
187 setOptions(const Options& options);
188
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000189 const Counters&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400190 getCounters() const NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL
191 {
192 return *this;
193 }
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700194
Eric Newberrycb6551e2020-03-02 14:12:16 -0800195 ssize_t
Davide Pesavento264af772021-02-09 21:48:24 -0500196 getEffectiveMtu() const NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800197
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400198 /** \brief Whether MTU can be overridden to the specified value.
Eric Newberrycb6551e2020-03-02 14:12:16 -0800199 *
200 * If the transport MTU is unlimited, then this will always return false.
201 */
202 bool
203 canOverrideMtuTo(ssize_t mtu) const;
204
Davide Pesavento264af772021-02-09 21:48:24 -0500205NFD_PROTECTED_WITH_TESTS_ELSE_PRIVATE: // send path
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400206 /** \brief Request an IDLE packet to transmit pending service fields.
Eric Newberry185ab292017-03-28 06:45:39 +0000207 */
208 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700209 requestIdlePacket();
Eric Newberry185ab292017-03-28 06:45:39 +0000210
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400211 /** \brief Send an LpPacket.
Eric Newberry185ab292017-03-28 06:45:39 +0000212 */
213 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700214 sendLpPacket(lp::Packet&& pkt);
Eric Newberry185ab292017-03-28 06:45:39 +0000215
Eric Newberrya98bf932015-09-21 00:58:47 -0700216 void
Davide Pesavento264af772021-02-09 21:48:24 -0500217 doSendInterest(const Interest& interest) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700218
Eric Newberrya98bf932015-09-21 00:58:47 -0700219 void
Davide Pesavento264af772021-02-09 21:48:24 -0500220 doSendData(const Data& data) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700221
Eric Newberrya98bf932015-09-21 00:58:47 -0700222 void
Davide Pesavento264af772021-02-09 21:48:24 -0500223 doSendNack(const ndn::lp::Nack& nack) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700224
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400225 /** \brief Assign consecutive sequence numbers to LpPackets.
Eric Newberry32f7eac2020-02-07 14:40:17 -0800226 */
227 void
228 assignSequences(std::vector<lp::Packet>& pkts);
229
Eric Newberry185ab292017-03-28 06:45:39 +0000230private: // send path
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400231 /** \brief Encode link protocol fields from tags onto an outgoing LpPacket.
Eric Newberryee400b52016-11-24 14:12:48 +0000232 * \param netPkt network-layer packet to extract tags from
233 * \param lpPacket LpPacket to add link protocol fields to
Eric Newberry86d31872015-09-23 16:24:59 -0700234 */
Eric Newberryee400b52016-11-24 14:12:48 +0000235 void
Eric Newberry41aba102017-11-01 16:42:13 -0700236 encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket);
Eric Newberry86d31872015-09-23 16:24:59 -0700237
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400238 /** \brief Send a complete network layer packet.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700239 * \param pkt LpPacket containing a complete network layer packet
Eric Newberry41aba102017-11-01 16:42:13 -0700240 * \param isInterest whether the network layer packet is an Interest
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700241 */
242 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700243 sendNetPacket(lp::Packet&& pkt, bool isInterest);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700244
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400245 /** \brief If the send queue is found to be congested, add a congestion mark to the packet
246 * according to CoDel.
Eric Newberryb49313d2017-12-24 20:22:27 -0700247 * \sa https://tools.ietf.org/html/rfc8289
248 */
249 void
250 checkCongestionLevel(lp::Packet& pkt);
251
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700252private: // receive path
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700253 void
Davide Pesavento264af772021-02-09 21:48:24 -0500254 doReceivePacket(const Block& packet, const EndpointId& endpoint) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700255
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400256 /** \brief Decode incoming network-layer packet.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700257 * \param netPkt reassembled network-layer packet
258 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000259 * \param endpointId endpoint of peer who sent the packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700260 *
261 * If decoding is successful, a receive signal is emitted;
262 * otherwise, a warning is logged.
263 */
264 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000265 decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700266
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400267 /** \brief Decode incoming Interest.
Eric Newberry86d31872015-09-23 16:24:59 -0700268 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
269 * \param firstPkt LpPacket of first fragment; must not have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000270 * \param endpointId endpoint of peer who sent the Interest
Eric Newberry86d31872015-09-23 16:24:59 -0700271 *
272 * If decoding is successful, receiveInterest signal is emitted;
273 * otherwise, a warning is logged.
274 *
275 * \throw tlv::Error parse error in an LpHeader field
276 */
277 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000278 decodeInterest(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700279
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400280 /** \brief Decode incoming Interest.
Eric Newberry86d31872015-09-23 16:24:59 -0700281 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Data
282 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000283 * \param endpointId endpoint of peer who sent the Data
Eric Newberry86d31872015-09-23 16:24:59 -0700284 *
285 * If decoding is successful, receiveData signal is emitted;
286 * otherwise, a warning is logged.
287 *
288 * \throw tlv::Error parse error in an LpHeader field
289 */
290 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000291 decodeData(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700292
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400293 /** \brief Decode incoming Interest.
Eric Newberry86d31872015-09-23 16:24:59 -0700294 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
295 * \param firstPkt LpPacket of first fragment; must have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000296 * \param endpointId endpoint of peer who sent the Nack
Eric Newberry86d31872015-09-23 16:24:59 -0700297 *
298 * If decoding is successful, receiveNack signal is emitted;
299 * otherwise, a warning is logged.
300 *
301 * \throw tlv::Error parse error in an LpHeader field
302 */
303 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000304 decodeNack(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700305
Davide Pesavento264af772021-02-09 21:48:24 -0500306NFD_PROTECTED_WITH_TESTS_ELSE_PRIVATE:
Eric Newberry86d31872015-09-23 16:24:59 -0700307 Options m_options;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700308 LpFragmenter m_fragmenter;
309 LpReassembler m_reassembler;
Eric Newberry185ab292017-03-28 06:45:39 +0000310 LpReliability m_reliability;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700311 lp::Sequence m_lastSeqNo;
Eric Newberry185ab292017-03-28 06:45:39 +0000312
Davide Pesavento264af772021-02-09 21:48:24 -0500313NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Eric Newberryb49313d2017-12-24 20:22:27 -0700314 /// Time to mark next packet due to send queue congestion
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400315 time::steady_clock::time_point m_nextMarkTime;
Eric Newberryb49313d2017-12-24 20:22:27 -0700316 /// number of marked packets in the current incident of congestion
317 size_t m_nMarkedSinceInMarkingState;
318
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400319 friend LpReliability;
Eric Newberrya98bf932015-09-21 00:58:47 -0700320};
321
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400322} // namespace nfd::face
Eric Newberrya98bf932015-09-21 00:58:47 -0700323
Eric Newberry86d31872015-09-23 16:24:59 -0700324#endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP