blob: b2faea39fadb0c33b7e74c3decbbf9cec222d096 [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 Pesavento264af772021-02-09 21:48:24 -05003 * 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#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
Alexander Afanasyevbaee72a2019-07-29 11:10:16 -040034#include <ndn-cxx/lp/tags.hpp>
35
Eric Newberrya98bf932015-09-21 00:58:47 -070036namespace nfd {
37namespace face {
38
Eric Newberry4c3e6b82015-11-10 16:48:42 -070039/** \brief counters provided by GenericLinkService
40 * \note The type name 'GenericLinkServiceCounters' is implementation detail.
41 * Use 'GenericLinkService::Counters' in public API.
42 */
43class GenericLinkServiceCounters : public virtual LinkService::Counters
44{
45public:
Eric Newberry4c3e6b82015-11-10 16:48:42 -070046 /** \brief count of failed fragmentations
47 */
48 PacketCounter nFragmentationErrors;
49
50 /** \brief count of outgoing LpPackets dropped due to exceeding MTU limit
51 *
52 * If this counter is non-zero, the operator should enable fragmentation.
53 */
54 PacketCounter nOutOverMtu;
55
56 /** \brief count of invalid LpPackets dropped before reassembly
57 */
58 PacketCounter nInLpInvalid;
59
60 /** \brief count of network-layer packets currently being reassembled
61 */
62 SizeCounter<LpReassembler> nReassembling;
63
64 /** \brief count of dropped partial network-layer packets due to reassembly timeout
65 */
66 PacketCounter nReassemblyTimeouts;
67
68 /** \brief count of invalid reassembled network-layer packets dropped
69 */
70 PacketCounter nInNetInvalid;
Eric Newberry185ab292017-03-28 06:45:39 +000071
72 /** \brief count of network-layer packets that did not require retransmission of a fragment
73 */
74 PacketCounter nAcknowledged;
75
76 /** \brief count of network-layer packets that had at least one fragment retransmitted, but were
77 * eventually received in full
78 */
79 PacketCounter nRetransmitted;
80
81 /** \brief count of network-layer packets dropped because a fragment reached the maximum number
82 * of retransmissions
83 */
84 PacketCounter nRetxExhausted;
Eric Newberryb49313d2017-12-24 20:22:27 -070085
Eric Newberry32f7eac2020-02-07 14:40:17 -080086 /** \brief count of LpPackets dropped due to duplicate Sequence numbers
87 */
88 PacketCounter nDuplicateSequence;
89
Eric Newberryb49313d2017-12-24 20:22:27 -070090 /** \brief count of outgoing LpPackets that were marked with congestion marks
91 */
92 PacketCounter nCongestionMarked;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070093};
94
Eric Newberry86d31872015-09-23 16:24:59 -070095/** \brief GenericLinkService is a LinkService that implements the NDNLPv2 protocol
Eric Newberry185ab292017-03-28 06:45:39 +000096 * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
Eric Newberrya98bf932015-09-21 00:58:47 -070097 */
Davide Pesavento264af772021-02-09 21:48:24 -050098class GenericLinkService NFD_FINAL_UNLESS_WITH_TESTS : public LinkService
99 , protected virtual GenericLinkServiceCounters
Eric Newberrya98bf932015-09-21 00:58:47 -0700100{
Eric Newberry86d31872015-09-23 16:24:59 -0700101public:
102 /** \brief Options that control the behavior of GenericLinkService
103 */
104 class Options
105 {
106 public:
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400107 Options() noexcept
108 {
109 }
Eric Newberry86d31872015-09-23 16:24:59 -0700110
111 public:
Eric Newberry86d31872015-09-23 16:24:59 -0700112 /** \brief enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy
113 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400114 bool allowLocalFields = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700115
116 /** \brief enables fragmentation
117 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400118 bool allowFragmentation = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700119
120 /** \brief options for fragmentation
121 */
122 LpFragmenter::Options fragmenterOptions;
123
124 /** \brief enables reassembly
125 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400126 bool allowReassembly = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700127
128 /** \brief options for reassembly
129 */
130 LpReassembler::Options reassemblerOptions;
Eric Newberry185ab292017-03-28 06:45:39 +0000131
132 /** \brief options for reliability
133 */
134 LpReliability::Options reliabilityOptions;
Eric Newberryb49313d2017-12-24 20:22:27 -0700135
136 /** \brief enables send queue congestion detection and marking
137 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400138 bool allowCongestionMarking = false;
Eric Newberryb49313d2017-12-24 20:22:27 -0700139
140 /** \brief starting value for congestion marking interval
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400141 *
Klaus Schneider380668b2019-10-02 01:21:13 -0700142 * Packets are marked if the queue size stays above THRESHOLD for at least one INTERVAL.
143 *
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400144 * The default value (100 ms) is taken from RFC 8289 (CoDel).
Eric Newberryb49313d2017-12-24 20:22:27 -0700145 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400146 time::nanoseconds baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700147
148 /** \brief default congestion threshold in bytes
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400149 *
Klaus Schneider380668b2019-10-02 01:21:13 -0700150 * Packets are marked if the queue size stays above THRESHOLD for at least one INTERVAL.
151 *
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400152 * The default value (64 KiB) works well for a queue capacity of 200 KiB.
Eric Newberryb49313d2017-12-24 20:22:27 -0700153 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400154 size_t defaultCongestionThreshold = 65536;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700155
156 /** \brief enables self-learning forwarding support
157 */
Teng Liang39465c22018-11-12 19:43:04 -0700158 bool allowSelfLearning = true;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800159
160 /** \brief overrides MTU provided by Transport
161 *
162 * This MTU value will be used instead of the MTU provided by the transport if it is less than
163 * the transport MTU. However, it will not be utilized when the transport MTU is unlimited.
164 *
165 * Acceptable values for the override MTU are values >= MIN_MTU, which can be validated before
166 * being set with canOverrideMtuTo().
167 */
168 ssize_t overrideMtu = std::numeric_limits<ssize_t>::max();
Alexander Afanasyevbaee72a2019-07-29 11:10:16 -0400169
170 /** \brief Enable encoding and decoding of GeoTags
171 *
172 * To enable, set value of enableGeoTags option to a function that generates `shared_ptr<GeoTag>`
173 */
174 std::function<std::shared_ptr<ndn::lp::GeoTag>()> enableGeoTags;
Eric Newberry86d31872015-09-23 16:24:59 -0700175 };
176
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700177 /** \brief counters provided by GenericLinkService
178 */
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000179 using Counters = GenericLinkServiceCounters;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700180
Eric Newberry86d31872015-09-23 16:24:59 -0700181 explicit
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400182 GenericLinkService(const Options& options = {});
Eric Newberry86d31872015-09-23 16:24:59 -0700183
184 /** \brief get Options used by GenericLinkService
185 */
186 const Options&
187 getOptions() const;
188
189 /** \brief sets Options used by GenericLinkService
190 */
191 void
192 setOptions(const Options& options);
193
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000194 const Counters&
Davide Pesavento264af772021-02-09 21:48:24 -0500195 getCounters() const NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700196
Eric Newberrycb6551e2020-03-02 14:12:16 -0800197 ssize_t
Davide Pesavento264af772021-02-09 21:48:24 -0500198 getEffectiveMtu() const NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800199
200 /** \brief Whether MTU can be overridden to the specified value
201 *
202 * If the transport MTU is unlimited, then this will always return false.
203 */
204 bool
205 canOverrideMtuTo(ssize_t mtu) const;
206
Davide Pesavento264af772021-02-09 21:48:24 -0500207NFD_PROTECTED_WITH_TESTS_ELSE_PRIVATE: // send path
Eric Newberry185ab292017-03-28 06:45:39 +0000208 /** \brief request an IDLE packet to transmit pending service fields
209 */
210 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700211 requestIdlePacket();
Eric Newberry185ab292017-03-28 06:45:39 +0000212
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700213 /** \brief send an LpPacket
Eric Newberry185ab292017-03-28 06:45:39 +0000214 */
215 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700216 sendLpPacket(lp::Packet&& pkt);
Eric Newberry185ab292017-03-28 06:45:39 +0000217
Eric Newberrya98bf932015-09-21 00:58:47 -0700218 void
Davide Pesavento264af772021-02-09 21:48:24 -0500219 doSendInterest(const Interest& interest) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700220
Eric Newberrya98bf932015-09-21 00:58:47 -0700221 void
Davide Pesavento264af772021-02-09 21:48:24 -0500222 doSendData(const Data& data) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700223
Eric Newberrya98bf932015-09-21 00:58:47 -0700224 void
Davide Pesavento264af772021-02-09 21:48:24 -0500225 doSendNack(const ndn::lp::Nack& nack) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700226
Eric Newberry32f7eac2020-02-07 14:40:17 -0800227 /** \brief assign consecutive sequence numbers to LpPackets
228 */
229 void
230 assignSequences(std::vector<lp::Packet>& pkts);
231
Eric Newberry185ab292017-03-28 06:45:39 +0000232private: // send path
Eric Newberryee400b52016-11-24 14:12:48 +0000233 /** \brief encode link protocol fields from tags onto an outgoing LpPacket
234 * \param netPkt network-layer packet to extract tags from
235 * \param lpPacket LpPacket to add link protocol fields to
Eric Newberry86d31872015-09-23 16:24:59 -0700236 */
Eric Newberryee400b52016-11-24 14:12:48 +0000237 void
Eric Newberry41aba102017-11-01 16:42:13 -0700238 encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket);
Eric Newberry86d31872015-09-23 16:24:59 -0700239
Junxiao Shi0de23a22015-12-03 20:07:02 +0000240 /** \brief send a complete network layer packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700241 * \param pkt LpPacket containing a complete network layer packet
Eric Newberry41aba102017-11-01 16:42:13 -0700242 * \param isInterest whether the network layer packet is an Interest
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700243 */
244 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700245 sendNetPacket(lp::Packet&& pkt, bool isInterest);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700246
Eric Newberryb49313d2017-12-24 20:22:27 -0700247 /** \brief if the send queue is found to be congested, add a congestion mark to the packet
248 * according to CoDel
249 * \sa https://tools.ietf.org/html/rfc8289
250 */
251 void
252 checkCongestionLevel(lp::Packet& pkt);
253
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700254private: // receive path
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700255 void
Davide Pesavento264af772021-02-09 21:48:24 -0500256 doReceivePacket(const Block& packet, const EndpointId& endpoint) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700257
258 /** \brief decode incoming network-layer packet
259 * \param netPkt reassembled network-layer packet
260 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000261 * \param endpointId endpoint of peer who sent the packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700262 *
263 * If decoding is successful, a receive signal is emitted;
264 * otherwise, a warning is logged.
265 */
266 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000267 decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700268
Eric Newberry86d31872015-09-23 16:24:59 -0700269 /** \brief decode incoming Interest
270 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
271 * \param firstPkt LpPacket of first fragment; must not have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000272 * \param endpointId endpoint of peer who sent the Interest
Eric Newberry86d31872015-09-23 16:24:59 -0700273 *
274 * If decoding is successful, receiveInterest signal is emitted;
275 * otherwise, a warning is logged.
276 *
277 * \throw tlv::Error parse error in an LpHeader field
278 */
279 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000280 decodeInterest(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700281
282 /** \brief decode incoming Interest
283 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Data
284 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000285 * \param endpointId endpoint of peer who sent the Data
Eric Newberry86d31872015-09-23 16:24:59 -0700286 *
287 * If decoding is successful, receiveData signal is emitted;
288 * otherwise, a warning is logged.
289 *
290 * \throw tlv::Error parse error in an LpHeader field
291 */
292 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000293 decodeData(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700294
295 /** \brief decode incoming Interest
296 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
297 * \param firstPkt LpPacket of first fragment; must have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000298 * \param endpointId endpoint of peer who sent the Nack
Eric Newberry86d31872015-09-23 16:24:59 -0700299 *
300 * If decoding is successful, receiveNack signal is emitted;
301 * otherwise, a warning is logged.
302 *
303 * \throw tlv::Error parse error in an LpHeader field
304 */
305 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000306 decodeNack(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700307
Davide Pesavento264af772021-02-09 21:48:24 -0500308NFD_PROTECTED_WITH_TESTS_ELSE_PRIVATE:
Eric Newberry86d31872015-09-23 16:24:59 -0700309 Options m_options;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700310 LpFragmenter m_fragmenter;
311 LpReassembler m_reassembler;
Eric Newberry185ab292017-03-28 06:45:39 +0000312 LpReliability m_reliability;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700313 lp::Sequence m_lastSeqNo;
Eric Newberry185ab292017-03-28 06:45:39 +0000314
Davide Pesavento264af772021-02-09 21:48:24 -0500315NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Eric Newberryb49313d2017-12-24 20:22:27 -0700316 /// Time to mark next packet due to send queue congestion
317 time::steady_clock::TimePoint m_nextMarkTime;
Eric Newberryb49313d2017-12-24 20:22:27 -0700318 /// number of marked packets in the current incident of congestion
319 size_t m_nMarkedSinceInMarkingState;
320
Eric Newberry185ab292017-03-28 06:45:39 +0000321 friend class LpReliability;
Eric Newberrya98bf932015-09-21 00:58:47 -0700322};
323
Eric Newberry86d31872015-09-23 16:24:59 -0700324inline const GenericLinkService::Options&
325GenericLinkService::getOptions() const
326{
327 return m_options;
328}
329
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700330inline const GenericLinkService::Counters&
331GenericLinkService::getCounters() const
332{
333 return *this;
334}
335
Eric Newberrya98bf932015-09-21 00:58:47 -0700336} // namespace face
337} // namespace nfd
338
Eric Newberry86d31872015-09-23 16:24:59 -0700339#endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP