blob: 64a2d3c11ad60959e01d9575641300c75feea4d6 [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/*
Teng Liang39465c22018-11-12 19:43:04 -07003 * 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#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 Afanasyev53b29172019-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
86 /** \brief count of outgoing LpPackets that were marked with congestion marks
87 */
88 PacketCounter nCongestionMarked;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070089};
90
Eric Newberry86d31872015-09-23 16:24:59 -070091/** \brief GenericLinkService is a LinkService that implements the NDNLPv2 protocol
Eric Newberry185ab292017-03-28 06:45:39 +000092 * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
Eric Newberrya98bf932015-09-21 00:58:47 -070093 */
Davide Pesavento16916ae2019-03-29 23:53:26 -040094class GenericLinkService FINAL_UNLESS_WITH_TESTS : public LinkService
95 , protected virtual GenericLinkServiceCounters
Eric Newberrya98bf932015-09-21 00:58:47 -070096{
Eric Newberry86d31872015-09-23 16:24:59 -070097public:
98 /** \brief Options that control the behavior of GenericLinkService
99 */
100 class Options
101 {
102 public:
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400103 Options() noexcept
104 {
105 }
Eric Newberry86d31872015-09-23 16:24:59 -0700106
107 public:
Eric Newberry86d31872015-09-23 16:24:59 -0700108 /** \brief enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy
109 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400110 bool allowLocalFields = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700111
112 /** \brief enables fragmentation
113 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400114 bool allowFragmentation = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700115
116 /** \brief options for fragmentation
117 */
118 LpFragmenter::Options fragmenterOptions;
119
120 /** \brief enables reassembly
121 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400122 bool allowReassembly = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700123
124 /** \brief options for reassembly
125 */
126 LpReassembler::Options reassemblerOptions;
Eric Newberry185ab292017-03-28 06:45:39 +0000127
128 /** \brief options for reliability
129 */
130 LpReliability::Options reliabilityOptions;
Eric Newberryb49313d2017-12-24 20:22:27 -0700131
132 /** \brief enables send queue congestion detection and marking
133 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400134 bool allowCongestionMarking = false;
Eric Newberryb49313d2017-12-24 20:22:27 -0700135
136 /** \brief starting value for congestion marking interval
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400137 *
Klaus Schneider380668b2019-10-02 01:21:13 -0700138 * Packets are marked if the queue size stays above THRESHOLD for at least one INTERVAL.
139 *
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400140 * The default value (100 ms) is taken from RFC 8289 (CoDel).
Eric Newberryb49313d2017-12-24 20:22:27 -0700141 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400142 time::nanoseconds baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700143
144 /** \brief default congestion threshold in bytes
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400145 *
Klaus Schneider380668b2019-10-02 01:21:13 -0700146 * Packets are marked if the queue size stays above THRESHOLD for at least one INTERVAL.
147 *
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400148 * The default value (64 KiB) works well for a queue capacity of 200 KiB.
Eric Newberryb49313d2017-12-24 20:22:27 -0700149 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400150 size_t defaultCongestionThreshold = 65536;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700151
152 /** \brief enables self-learning forwarding support
153 */
Teng Liang39465c22018-11-12 19:43:04 -0700154 bool allowSelfLearning = true;
Alexander Afanasyev53b29172019-07-29 11:10:16 -0400155
156 /** \brief Enable encoding and decoding of GeoTags
157 *
158 * To enable, set value of enableGeoTags option to a function that generates `shared_ptr<GeoTag>`
159 */
160 std::function<std::shared_ptr<ndn::lp::GeoTag>()> enableGeoTags;
Eric Newberry86d31872015-09-23 16:24:59 -0700161 };
162
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700163 /** \brief counters provided by GenericLinkService
164 */
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000165 using Counters = GenericLinkServiceCounters;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700166
Eric Newberry86d31872015-09-23 16:24:59 -0700167 explicit
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400168 GenericLinkService(const Options& options = {});
Eric Newberry86d31872015-09-23 16:24:59 -0700169
170 /** \brief get Options used by GenericLinkService
171 */
172 const Options&
173 getOptions() const;
174
175 /** \brief sets Options used by GenericLinkService
176 */
177 void
178 setOptions(const Options& options);
179
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000180 const Counters&
Davide Pesavento16916ae2019-03-29 23:53:26 -0400181 getCounters() const OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700182
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000183PROTECTED_WITH_TESTS_ELSE_PRIVATE: // send path
Eric Newberry185ab292017-03-28 06:45:39 +0000184 /** \brief request an IDLE packet to transmit pending service fields
185 */
186 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000187 requestIdlePacket(const EndpointId& endpointId);
Eric Newberry185ab292017-03-28 06:45:39 +0000188
ashiqopu075bb7d2019-03-10 01:38:21 +0000189 /** \brief send an LpPacket to \p endpointId
Eric Newberry185ab292017-03-28 06:45:39 +0000190 */
191 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000192 sendLpPacket(lp::Packet&& pkt, const EndpointId& endpointId);
Eric Newberry185ab292017-03-28 06:45:39 +0000193
Junxiao Shi0de23a22015-12-03 20:07:02 +0000194 /** \brief send Interest
Eric Newberrya98bf932015-09-21 00:58:47 -0700195 */
196 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000197 doSendInterest(const Interest& interest, const EndpointId& endpointId) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700198
Junxiao Shi0de23a22015-12-03 20:07:02 +0000199 /** \brief send Data
Eric Newberrya98bf932015-09-21 00:58:47 -0700200 */
201 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000202 doSendData(const Data& data, const EndpointId& endpointId) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700203
Junxiao Shi0de23a22015-12-03 20:07:02 +0000204 /** \brief send Nack
Eric Newberrya98bf932015-09-21 00:58:47 -0700205 */
206 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000207 doSendNack(const ndn::lp::Nack& nack, const EndpointId& endpointId) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700208
Eric Newberry185ab292017-03-28 06:45:39 +0000209private: // send path
Eric Newberryee400b52016-11-24 14:12:48 +0000210 /** \brief encode link protocol fields from tags onto an outgoing LpPacket
211 * \param netPkt network-layer packet to extract tags from
212 * \param lpPacket LpPacket to add link protocol fields to
Eric Newberry86d31872015-09-23 16:24:59 -0700213 */
Eric Newberryee400b52016-11-24 14:12:48 +0000214 void
Eric Newberry41aba102017-11-01 16:42:13 -0700215 encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket);
Eric Newberry86d31872015-09-23 16:24:59 -0700216
Junxiao Shi0de23a22015-12-03 20:07:02 +0000217 /** \brief send a complete network layer packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700218 * \param pkt LpPacket containing a complete network layer packet
ashiqopu075bb7d2019-03-10 01:38:21 +0000219 * \param endpointId destination endpoint to which LpPacket will be sent
Eric Newberry41aba102017-11-01 16:42:13 -0700220 * \param isInterest whether the network layer packet is an Interest
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700221 */
222 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000223 sendNetPacket(lp::Packet&& pkt, const EndpointId& endpointId, bool isInterest);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700224
225 /** \brief assign a sequence number to an LpPacket
226 */
227 void
228 assignSequence(lp::Packet& pkt);
229
230 /** \brief assign consecutive sequence numbers to LpPackets
231 */
232 void
233 assignSequences(std::vector<lp::Packet>& pkts);
234
Eric Newberryb49313d2017-12-24 20:22:27 -0700235 /** \brief if the send queue is found to be congested, add a congestion mark to the packet
236 * according to CoDel
237 * \sa https://tools.ietf.org/html/rfc8289
238 */
239 void
240 checkCongestionLevel(lp::Packet& pkt);
241
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700242private: // receive path
243 /** \brief receive Packet from Transport
244 */
245 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400246 doReceivePacket(const Block& packet, const EndpointId& endpoint) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700247
248 /** \brief decode incoming network-layer packet
249 * \param netPkt reassembled network-layer packet
250 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000251 * \param endpointId endpoint of peer who sent the packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700252 *
253 * If decoding is successful, a receive signal is emitted;
254 * otherwise, a warning is logged.
255 */
256 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000257 decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700258
Eric Newberry86d31872015-09-23 16:24:59 -0700259 /** \brief decode incoming Interest
260 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
261 * \param firstPkt LpPacket of first fragment; must not have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000262 * \param endpointId endpoint of peer who sent the Interest
Eric Newberry86d31872015-09-23 16:24:59 -0700263 *
264 * If decoding is successful, receiveInterest signal is emitted;
265 * otherwise, a warning is logged.
266 *
267 * \throw tlv::Error parse error in an LpHeader field
268 */
269 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000270 decodeInterest(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700271
272 /** \brief decode incoming Interest
273 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Data
274 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000275 * \param endpointId endpoint of peer who sent the Data
Eric Newberry86d31872015-09-23 16:24:59 -0700276 *
277 * If decoding is successful, receiveData signal is emitted;
278 * otherwise, a warning is logged.
279 *
280 * \throw tlv::Error parse error in an LpHeader field
281 */
282 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000283 decodeData(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700284
285 /** \brief decode incoming Interest
286 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
287 * \param firstPkt LpPacket of first fragment; must have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000288 * \param endpointId endpoint of peer who sent the Nack
Eric Newberry86d31872015-09-23 16:24:59 -0700289 *
290 * If decoding is successful, receiveNack signal is emitted;
291 * otherwise, a warning is logged.
292 *
293 * \throw tlv::Error parse error in an LpHeader field
294 */
295 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000296 decodeNack(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700297
Eric Newberry185ab292017-03-28 06:45:39 +0000298PROTECTED_WITH_TESTS_ELSE_PRIVATE:
Eric Newberry86d31872015-09-23 16:24:59 -0700299 Options m_options;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700300 LpFragmenter m_fragmenter;
301 LpReassembler m_reassembler;
Eric Newberry185ab292017-03-28 06:45:39 +0000302 LpReliability m_reliability;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700303 lp::Sequence m_lastSeqNo;
Eric Newberry185ab292017-03-28 06:45:39 +0000304
Eric Newberryb49313d2017-12-24 20:22:27 -0700305PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Eric Newberryb49313d2017-12-24 20:22:27 -0700306 /// Time to mark next packet due to send queue congestion
307 time::steady_clock::TimePoint m_nextMarkTime;
Eric Newberryb49313d2017-12-24 20:22:27 -0700308 /// number of marked packets in the current incident of congestion
309 size_t m_nMarkedSinceInMarkingState;
310
Eric Newberry185ab292017-03-28 06:45:39 +0000311 friend class LpReliability;
Eric Newberrya98bf932015-09-21 00:58:47 -0700312};
313
Eric Newberry86d31872015-09-23 16:24:59 -0700314inline const GenericLinkService::Options&
315GenericLinkService::getOptions() const
316{
317 return m_options;
318}
319
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700320inline const GenericLinkService::Counters&
321GenericLinkService::getCounters() const
322{
323 return *this;
324}
325
Eric Newberrya98bf932015-09-21 00:58:47 -0700326} // namespace face
327} // namespace nfd
328
Eric Newberry86d31872015-09-23 16:24:59 -0700329#endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP