blob: 7a1dd4f516521be948159b32f81d907666f27d13 [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/*
Eric Newberry32f7eac2020-02-07 14:40:17 -08003 * Copyright (c) 2014-2020, 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
34namespace nfd {
35namespace face {
36
Eric Newberry4c3e6b82015-11-10 16:48:42 -070037/** \brief counters provided by GenericLinkService
38 * \note The type name 'GenericLinkServiceCounters' is implementation detail.
39 * Use 'GenericLinkService::Counters' in public API.
40 */
41class GenericLinkServiceCounters : public virtual LinkService::Counters
42{
43public:
Eric Newberry4c3e6b82015-11-10 16:48:42 -070044 /** \brief count of failed fragmentations
45 */
46 PacketCounter nFragmentationErrors;
47
48 /** \brief count of outgoing LpPackets dropped due to exceeding MTU limit
49 *
50 * If this counter is non-zero, the operator should enable fragmentation.
51 */
52 PacketCounter nOutOverMtu;
53
54 /** \brief count of invalid LpPackets dropped before reassembly
55 */
56 PacketCounter nInLpInvalid;
57
58 /** \brief count of network-layer packets currently being reassembled
59 */
60 SizeCounter<LpReassembler> nReassembling;
61
62 /** \brief count of dropped partial network-layer packets due to reassembly timeout
63 */
64 PacketCounter nReassemblyTimeouts;
65
66 /** \brief count of invalid reassembled network-layer packets dropped
67 */
68 PacketCounter nInNetInvalid;
Eric Newberry185ab292017-03-28 06:45:39 +000069
70 /** \brief count of network-layer packets that did not require retransmission of a fragment
71 */
72 PacketCounter nAcknowledged;
73
74 /** \brief count of network-layer packets that had at least one fragment retransmitted, but were
75 * eventually received in full
76 */
77 PacketCounter nRetransmitted;
78
79 /** \brief count of network-layer packets dropped because a fragment reached the maximum number
80 * of retransmissions
81 */
82 PacketCounter nRetxExhausted;
Eric Newberryb49313d2017-12-24 20:22:27 -070083
Eric Newberry32f7eac2020-02-07 14:40:17 -080084 /** \brief count of LpPackets dropped due to duplicate Sequence numbers
85 */
86 PacketCounter nDuplicateSequence;
87
Eric Newberryb49313d2017-12-24 20:22:27 -070088 /** \brief count of outgoing LpPackets that were marked with congestion marks
89 */
90 PacketCounter nCongestionMarked;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070091};
92
Eric Newberry86d31872015-09-23 16:24:59 -070093/** \brief GenericLinkService is a LinkService that implements the NDNLPv2 protocol
Eric Newberry185ab292017-03-28 06:45:39 +000094 * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
Eric Newberrya98bf932015-09-21 00:58:47 -070095 */
Davide Pesavento16916ae2019-03-29 23:53:26 -040096class GenericLinkService 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:
100 /** \brief Options that control the behavior of GenericLinkService
101 */
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:
Eric Newberry86d31872015-09-23 16:24:59 -0700110 /** \brief enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy
111 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400112 bool allowLocalFields = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700113
114 /** \brief enables fragmentation
115 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400116 bool allowFragmentation = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700117
118 /** \brief options for fragmentation
119 */
120 LpFragmenter::Options fragmenterOptions;
121
122 /** \brief enables reassembly
123 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400124 bool allowReassembly = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700125
126 /** \brief options for reassembly
127 */
128 LpReassembler::Options reassemblerOptions;
Eric Newberry185ab292017-03-28 06:45:39 +0000129
130 /** \brief options for reliability
131 */
132 LpReliability::Options reliabilityOptions;
Eric Newberryb49313d2017-12-24 20:22:27 -0700133
134 /** \brief enables send queue congestion detection and marking
135 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400136 bool allowCongestionMarking = false;
Eric Newberryb49313d2017-12-24 20:22:27 -0700137
138 /** \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
146 /** \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
154 /** \brief enables self-learning forwarding support
155 */
Teng Liang39465c22018-11-12 19:43:04 -0700156 bool allowSelfLearning = true;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800157
158 /** \brief overrides MTU provided by Transport
159 *
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 *
163 * Acceptable values for the override MTU are values >= MIN_MTU, which can be validated before
164 * being set with canOverrideMtuTo().
165 */
166 ssize_t overrideMtu = std::numeric_limits<ssize_t>::max();
Eric Newberry86d31872015-09-23 16:24:59 -0700167 };
168
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700169 /** \brief counters provided by GenericLinkService
170 */
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
176 /** \brief get Options used by GenericLinkService
177 */
178 const Options&
179 getOptions() const;
180
181 /** \brief sets Options used by GenericLinkService
182 */
183 void
184 setOptions(const Options& options);
185
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000186 const Counters&
Davide Pesavento16916ae2019-03-29 23:53:26 -0400187 getCounters() const OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700188
Eric Newberrycb6551e2020-03-02 14:12:16 -0800189 ssize_t
190 getEffectiveMtu() const OVERRIDE_WITH_TESTS_ELSE_FINAL;
191
192 /** \brief Whether MTU can be overridden to the specified value
193 *
194 * If the transport MTU is unlimited, then this will always return false.
195 */
196 bool
197 canOverrideMtuTo(ssize_t mtu) const;
198
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000199PROTECTED_WITH_TESTS_ELSE_PRIVATE: // send path
Eric Newberry185ab292017-03-28 06:45:39 +0000200 /** \brief request an IDLE packet to transmit pending service fields
201 */
202 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700203 requestIdlePacket();
Eric Newberry185ab292017-03-28 06:45:39 +0000204
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700205 /** \brief send an LpPacket
Eric Newberry185ab292017-03-28 06:45:39 +0000206 */
207 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700208 sendLpPacket(lp::Packet&& pkt);
Eric Newberry185ab292017-03-28 06:45:39 +0000209
Eric Newberrya98bf932015-09-21 00:58:47 -0700210 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700211 doSendInterest(const Interest& interest) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700212
Eric Newberrya98bf932015-09-21 00:58:47 -0700213 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700214 doSendData(const Data& data) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700215
Eric Newberrya98bf932015-09-21 00:58:47 -0700216 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700217 doSendNack(const ndn::lp::Nack& nack) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700218
Eric Newberry32f7eac2020-02-07 14:40:17 -0800219 /** \brief assign consecutive sequence numbers to LpPackets
220 */
221 void
222 assignSequences(std::vector<lp::Packet>& pkts);
223
Eric Newberry185ab292017-03-28 06:45:39 +0000224private: // send path
Eric Newberryee400b52016-11-24 14:12:48 +0000225 /** \brief encode link protocol fields from tags onto an outgoing LpPacket
226 * \param netPkt network-layer packet to extract tags from
227 * \param lpPacket LpPacket to add link protocol fields to
Eric Newberry86d31872015-09-23 16:24:59 -0700228 */
Eric Newberryee400b52016-11-24 14:12:48 +0000229 void
Eric Newberry41aba102017-11-01 16:42:13 -0700230 encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket);
Eric Newberry86d31872015-09-23 16:24:59 -0700231
Junxiao Shi0de23a22015-12-03 20:07:02 +0000232 /** \brief send a complete network layer packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700233 * \param pkt LpPacket containing a complete network layer packet
Eric Newberry41aba102017-11-01 16:42:13 -0700234 * \param isInterest whether the network layer packet is an Interest
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700235 */
236 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700237 sendNetPacket(lp::Packet&& pkt, bool isInterest);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700238
Eric Newberryb49313d2017-12-24 20:22:27 -0700239 /** \brief if the send queue is found to be congested, add a congestion mark to the packet
240 * according to CoDel
241 * \sa https://tools.ietf.org/html/rfc8289
242 */
243 void
244 checkCongestionLevel(lp::Packet& pkt);
245
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700246private: // receive path
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700247 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400248 doReceivePacket(const Block& packet, const EndpointId& endpoint) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700249
250 /** \brief decode incoming network-layer packet
251 * \param netPkt reassembled network-layer packet
252 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000253 * \param endpointId endpoint of peer who sent the packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700254 *
255 * If decoding is successful, a receive signal is emitted;
256 * otherwise, a warning is logged.
257 */
258 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000259 decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700260
Eric Newberry86d31872015-09-23 16:24:59 -0700261 /** \brief decode incoming Interest
262 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
263 * \param firstPkt LpPacket of first fragment; must not have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000264 * \param endpointId endpoint of peer who sent the Interest
Eric Newberry86d31872015-09-23 16:24:59 -0700265 *
266 * If decoding is successful, receiveInterest signal is emitted;
267 * otherwise, a warning is logged.
268 *
269 * \throw tlv::Error parse error in an LpHeader field
270 */
271 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000272 decodeInterest(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700273
274 /** \brief decode incoming Interest
275 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Data
276 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000277 * \param endpointId endpoint of peer who sent the Data
Eric Newberry86d31872015-09-23 16:24:59 -0700278 *
279 * If decoding is successful, receiveData signal is emitted;
280 * otherwise, a warning is logged.
281 *
282 * \throw tlv::Error parse error in an LpHeader field
283 */
284 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000285 decodeData(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700286
287 /** \brief decode incoming Interest
288 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
289 * \param firstPkt LpPacket of first fragment; must have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000290 * \param endpointId endpoint of peer who sent the Nack
Eric Newberry86d31872015-09-23 16:24:59 -0700291 *
292 * If decoding is successful, receiveNack signal is emitted;
293 * otherwise, a warning is logged.
294 *
295 * \throw tlv::Error parse error in an LpHeader field
296 */
297 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000298 decodeNack(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700299
Eric Newberry185ab292017-03-28 06:45:39 +0000300PROTECTED_WITH_TESTS_ELSE_PRIVATE:
Eric Newberry86d31872015-09-23 16:24:59 -0700301 Options m_options;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700302 LpFragmenter m_fragmenter;
303 LpReassembler m_reassembler;
Eric Newberry185ab292017-03-28 06:45:39 +0000304 LpReliability m_reliability;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700305 lp::Sequence m_lastSeqNo;
Eric Newberry185ab292017-03-28 06:45:39 +0000306
Eric Newberryb49313d2017-12-24 20:22:27 -0700307PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Eric Newberryb49313d2017-12-24 20:22:27 -0700308 /// Time to mark next packet due to send queue congestion
309 time::steady_clock::TimePoint m_nextMarkTime;
Eric Newberryb49313d2017-12-24 20:22:27 -0700310 /// number of marked packets in the current incident of congestion
311 size_t m_nMarkedSinceInMarkingState;
312
Eric Newberry185ab292017-03-28 06:45:39 +0000313 friend class LpReliability;
Eric Newberrya98bf932015-09-21 00:58:47 -0700314};
315
Eric Newberry86d31872015-09-23 16:24:59 -0700316inline const GenericLinkService::Options&
317GenericLinkService::getOptions() const
318{
319 return m_options;
320}
321
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700322inline const GenericLinkService::Counters&
323GenericLinkService::getCounters() const
324{
325 return *this;
326}
327
Eric Newberrya98bf932015-09-21 00:58:47 -0700328} // namespace face
329} // namespace nfd
330
Eric Newberry86d31872015-09-23 16:24:59 -0700331#endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP