blob: 38473f5b1f612fac4a2dc08723c16a0e420e05df [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
ashiqopu075bb7d2019-03-10 01:38:21 +0000203 requestIdlePacket(const EndpointId& endpointId);
Eric Newberry185ab292017-03-28 06:45:39 +0000204
ashiqopu075bb7d2019-03-10 01:38:21 +0000205 /** \brief send an LpPacket to \p endpointId
Eric Newberry185ab292017-03-28 06:45:39 +0000206 */
207 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000208 sendLpPacket(lp::Packet&& pkt, const EndpointId& endpointId);
Eric Newberry185ab292017-03-28 06:45:39 +0000209
Junxiao Shi0de23a22015-12-03 20:07:02 +0000210 /** \brief send Interest
Eric Newberrya98bf932015-09-21 00:58:47 -0700211 */
212 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000213 doSendInterest(const Interest& interest, const EndpointId& endpointId) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700214
Junxiao Shi0de23a22015-12-03 20:07:02 +0000215 /** \brief send Data
Eric Newberrya98bf932015-09-21 00:58:47 -0700216 */
217 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000218 doSendData(const Data& data, const EndpointId& endpointId) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700219
Junxiao Shi0de23a22015-12-03 20:07:02 +0000220 /** \brief send Nack
Eric Newberrya98bf932015-09-21 00:58:47 -0700221 */
222 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000223 doSendNack(const ndn::lp::Nack& nack, const EndpointId& endpointId) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700224
Eric Newberry32f7eac2020-02-07 14:40:17 -0800225 /** \brief assign consecutive sequence numbers to LpPackets
226 */
227 void
228 assignSequences(std::vector<lp::Packet>& pkts);
229
Eric Newberry185ab292017-03-28 06:45:39 +0000230private: // send path
Eric Newberryee400b52016-11-24 14:12:48 +0000231 /** \brief encode link protocol fields from tags onto an outgoing LpPacket
232 * \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
Junxiao Shi0de23a22015-12-03 20:07:02 +0000238 /** \brief send a complete network layer packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700239 * \param pkt LpPacket containing a complete network layer packet
ashiqopu075bb7d2019-03-10 01:38:21 +0000240 * \param endpointId destination endpoint to which LpPacket will be sent
Eric Newberry41aba102017-11-01 16:42:13 -0700241 * \param isInterest whether the network layer packet is an Interest
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700242 */
243 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000244 sendNetPacket(lp::Packet&& pkt, const EndpointId& endpointId, bool isInterest);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700245
Eric Newberryb49313d2017-12-24 20:22:27 -0700246 /** \brief if the send queue is found to be congested, add a congestion mark to the packet
247 * according to CoDel
248 * \sa https://tools.ietf.org/html/rfc8289
249 */
250 void
251 checkCongestionLevel(lp::Packet& pkt);
252
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700253private: // receive path
254 /** \brief receive Packet from Transport
255 */
256 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400257 doReceivePacket(const Block& packet, const EndpointId& endpoint) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700258
259 /** \brief decode incoming network-layer packet
260 * \param netPkt reassembled network-layer packet
261 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000262 * \param endpointId endpoint of peer who sent the packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700263 *
264 * If decoding is successful, a receive signal is emitted;
265 * otherwise, a warning is logged.
266 */
267 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000268 decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700269
Eric Newberry86d31872015-09-23 16:24:59 -0700270 /** \brief decode incoming Interest
271 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
272 * \param firstPkt LpPacket of first fragment; must not have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000273 * \param endpointId endpoint of peer who sent the Interest
Eric Newberry86d31872015-09-23 16:24:59 -0700274 *
275 * If decoding is successful, receiveInterest signal is emitted;
276 * otherwise, a warning is logged.
277 *
278 * \throw tlv::Error parse error in an LpHeader field
279 */
280 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000281 decodeInterest(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700282
283 /** \brief decode incoming Interest
284 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Data
285 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000286 * \param endpointId endpoint of peer who sent the Data
Eric Newberry86d31872015-09-23 16:24:59 -0700287 *
288 * If decoding is successful, receiveData signal is emitted;
289 * otherwise, a warning is logged.
290 *
291 * \throw tlv::Error parse error in an LpHeader field
292 */
293 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000294 decodeData(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700295
296 /** \brief decode incoming Interest
297 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
298 * \param firstPkt LpPacket of first fragment; must have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000299 * \param endpointId endpoint of peer who sent the Nack
Eric Newberry86d31872015-09-23 16:24:59 -0700300 *
301 * If decoding is successful, receiveNack signal is emitted;
302 * otherwise, a warning is logged.
303 *
304 * \throw tlv::Error parse error in an LpHeader field
305 */
306 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000307 decodeNack(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700308
Eric Newberry185ab292017-03-28 06:45:39 +0000309PROTECTED_WITH_TESTS_ELSE_PRIVATE:
Eric Newberry86d31872015-09-23 16:24:59 -0700310 Options m_options;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700311 LpFragmenter m_fragmenter;
312 LpReassembler m_reassembler;
Eric Newberry185ab292017-03-28 06:45:39 +0000313 LpReliability m_reliability;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700314 lp::Sequence m_lastSeqNo;
Eric Newberry185ab292017-03-28 06:45:39 +0000315
Eric Newberryb49313d2017-12-24 20:22:27 -0700316PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Eric Newberryb49313d2017-12-24 20:22:27 -0700317 /// Time to mark next packet due to send queue congestion
318 time::steady_clock::TimePoint m_nextMarkTime;
Eric Newberryb49313d2017-12-24 20:22:27 -0700319 /// number of marked packets in the current incident of congestion
320 size_t m_nMarkedSinceInMarkingState;
321
Eric Newberry185ab292017-03-28 06:45:39 +0000322 friend class LpReliability;
Eric Newberrya98bf932015-09-21 00:58:47 -0700323};
324
Eric Newberry86d31872015-09-23 16:24:59 -0700325inline const GenericLinkService::Options&
326GenericLinkService::getOptions() const
327{
328 return m_options;
329}
330
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700331inline const GenericLinkService::Counters&
332GenericLinkService::getCounters() const
333{
334 return *this;
335}
336
Eric Newberrya98bf932015-09-21 00:58:47 -0700337} // namespace face
338} // namespace nfd
339
Eric Newberry86d31872015-09-23 16:24:59 -0700340#endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP