blob: e30a3e6384f44f3c98b4df7e9ae150fe36d8ffcf [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 constexpr
106 Options() noexcept
107 {
108 }
Eric Newberry86d31872015-09-23 16:24:59 -0700109
110 public:
Eric Newberry86d31872015-09-23 16:24:59 -0700111 /** \brief enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy
112 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400113 bool allowLocalFields = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700114
115 /** \brief enables fragmentation
116 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400117 bool allowFragmentation = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700118
119 /** \brief options for fragmentation
120 */
121 LpFragmenter::Options fragmenterOptions;
122
123 /** \brief enables reassembly
124 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400125 bool allowReassembly = false;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700126
127 /** \brief options for reassembly
128 */
129 LpReassembler::Options reassemblerOptions;
Eric Newberry185ab292017-03-28 06:45:39 +0000130
131 /** \brief options for reliability
132 */
133 LpReliability::Options reliabilityOptions;
Eric Newberryb49313d2017-12-24 20:22:27 -0700134
135 /** \brief enables send queue congestion detection and marking
136 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400137 bool allowCongestionMarking = false;
Eric Newberryb49313d2017-12-24 20:22:27 -0700138
139 /** \brief starting value for congestion marking interval
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400140 *
Klaus Schneider380668b2019-10-02 01:21:13 -0700141 * Packets are marked if the queue size stays above THRESHOLD for at least one INTERVAL.
142 *
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400143 * The default value (100 ms) is taken from RFC 8289 (CoDel).
Eric Newberryb49313d2017-12-24 20:22:27 -0700144 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400145 time::nanoseconds baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700146
147 /** \brief default congestion threshold in bytes
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400148 *
Klaus Schneider380668b2019-10-02 01:21:13 -0700149 * Packets are marked if the queue size stays above THRESHOLD for at least one INTERVAL.
150 *
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400151 * The default value (64 KiB) works well for a queue capacity of 200 KiB.
Eric Newberryb49313d2017-12-24 20:22:27 -0700152 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400153 size_t defaultCongestionThreshold = 65536;
Teng Liangfdcbb4d2018-01-27 16:01:35 -0700154
155 /** \brief enables self-learning forwarding support
156 */
Teng Liang39465c22018-11-12 19:43:04 -0700157 bool allowSelfLearning = true;
Eric Newberry86d31872015-09-23 16:24:59 -0700158 };
159
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700160 /** \brief counters provided by GenericLinkService
161 */
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000162 using Counters = GenericLinkServiceCounters;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700163
Eric Newberry86d31872015-09-23 16:24:59 -0700164 explicit
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400165 GenericLinkService(const Options& options = {});
Eric Newberry86d31872015-09-23 16:24:59 -0700166
167 /** \brief get Options used by GenericLinkService
168 */
169 const Options&
170 getOptions() const;
171
172 /** \brief sets Options used by GenericLinkService
173 */
174 void
175 setOptions(const Options& options);
176
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000177 const Counters&
Davide Pesavento16916ae2019-03-29 23:53:26 -0400178 getCounters() const OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700179
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000180PROTECTED_WITH_TESTS_ELSE_PRIVATE: // send path
Eric Newberry185ab292017-03-28 06:45:39 +0000181 /** \brief request an IDLE packet to transmit pending service fields
182 */
183 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000184 requestIdlePacket(const EndpointId& endpointId);
Eric Newberry185ab292017-03-28 06:45:39 +0000185
ashiqopu075bb7d2019-03-10 01:38:21 +0000186 /** \brief send an LpPacket to \p endpointId
Eric Newberry185ab292017-03-28 06:45:39 +0000187 */
188 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000189 sendLpPacket(lp::Packet&& pkt, const EndpointId& endpointId);
Eric Newberry185ab292017-03-28 06:45:39 +0000190
Junxiao Shi0de23a22015-12-03 20:07:02 +0000191 /** \brief send Interest
Eric Newberrya98bf932015-09-21 00:58:47 -0700192 */
193 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000194 doSendInterest(const Interest& interest, const EndpointId& endpointId) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700195
Junxiao Shi0de23a22015-12-03 20:07:02 +0000196 /** \brief send Data
Eric Newberrya98bf932015-09-21 00:58:47 -0700197 */
198 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000199 doSendData(const Data& data, const EndpointId& endpointId) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700200
Junxiao Shi0de23a22015-12-03 20:07:02 +0000201 /** \brief send Nack
Eric Newberrya98bf932015-09-21 00:58:47 -0700202 */
203 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000204 doSendNack(const ndn::lp::Nack& nack, const EndpointId& endpointId) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700205
Eric Newberry32f7eac2020-02-07 14:40:17 -0800206 /** \brief assign consecutive sequence numbers to LpPackets
207 */
208 void
209 assignSequences(std::vector<lp::Packet>& pkts);
210
Eric Newberry185ab292017-03-28 06:45:39 +0000211private: // send path
Eric Newberryee400b52016-11-24 14:12:48 +0000212 /** \brief encode link protocol fields from tags onto an outgoing LpPacket
213 * \param netPkt network-layer packet to extract tags from
214 * \param lpPacket LpPacket to add link protocol fields to
Eric Newberry86d31872015-09-23 16:24:59 -0700215 */
Eric Newberryee400b52016-11-24 14:12:48 +0000216 void
Eric Newberry41aba102017-11-01 16:42:13 -0700217 encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket);
Eric Newberry86d31872015-09-23 16:24:59 -0700218
Junxiao Shi0de23a22015-12-03 20:07:02 +0000219 /** \brief send a complete network layer packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700220 * \param pkt LpPacket containing a complete network layer packet
ashiqopu075bb7d2019-03-10 01:38:21 +0000221 * \param endpointId destination endpoint to which LpPacket will be sent
Eric Newberry41aba102017-11-01 16:42:13 -0700222 * \param isInterest whether the network layer packet is an Interest
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700223 */
224 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000225 sendNetPacket(lp::Packet&& pkt, const EndpointId& endpointId, bool isInterest);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700226
Eric Newberryb49313d2017-12-24 20:22:27 -0700227 /** \brief if the send queue is found to be congested, add a congestion mark to the packet
228 * according to CoDel
229 * \sa https://tools.ietf.org/html/rfc8289
230 */
231 void
232 checkCongestionLevel(lp::Packet& pkt);
233
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700234private: // receive path
235 /** \brief receive Packet from Transport
236 */
237 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400238 doReceivePacket(const Block& packet, const EndpointId& endpoint) OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700239
240 /** \brief decode incoming network-layer packet
241 * \param netPkt reassembled network-layer packet
242 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000243 * \param endpointId endpoint of peer who sent the packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700244 *
245 * If decoding is successful, a receive signal is emitted;
246 * otherwise, a warning is logged.
247 */
248 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000249 decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700250
Eric Newberry86d31872015-09-23 16:24:59 -0700251 /** \brief decode incoming Interest
252 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
253 * \param firstPkt LpPacket of first fragment; must not have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000254 * \param endpointId endpoint of peer who sent the Interest
Eric Newberry86d31872015-09-23 16:24:59 -0700255 *
256 * If decoding is successful, receiveInterest signal is emitted;
257 * otherwise, a warning is logged.
258 *
259 * \throw tlv::Error parse error in an LpHeader field
260 */
261 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000262 decodeInterest(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700263
264 /** \brief decode incoming Interest
265 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Data
266 * \param firstPkt LpPacket of first fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000267 * \param endpointId endpoint of peer who sent the Data
Eric Newberry86d31872015-09-23 16:24:59 -0700268 *
269 * If decoding is successful, receiveData signal is emitted;
270 * otherwise, a warning is logged.
271 *
272 * \throw tlv::Error parse error in an LpHeader field
273 */
274 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000275 decodeData(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700276
277 /** \brief decode incoming Interest
278 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
279 * \param firstPkt LpPacket of first fragment; must have Nack field
ashiqopu075bb7d2019-03-10 01:38:21 +0000280 * \param endpointId endpoint of peer who sent the Nack
Eric Newberry86d31872015-09-23 16:24:59 -0700281 *
282 * If decoding is successful, receiveNack signal is emitted;
283 * otherwise, a warning is logged.
284 *
285 * \throw tlv::Error parse error in an LpHeader field
286 */
287 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000288 decodeNack(const Block& netPkt, const lp::Packet& firstPkt, const EndpointId& endpointId);
Eric Newberry86d31872015-09-23 16:24:59 -0700289
Eric Newberry185ab292017-03-28 06:45:39 +0000290PROTECTED_WITH_TESTS_ELSE_PRIVATE:
Eric Newberry86d31872015-09-23 16:24:59 -0700291 Options m_options;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700292 LpFragmenter m_fragmenter;
293 LpReassembler m_reassembler;
Eric Newberry185ab292017-03-28 06:45:39 +0000294 LpReliability m_reliability;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700295 lp::Sequence m_lastSeqNo;
Eric Newberry185ab292017-03-28 06:45:39 +0000296
Eric Newberryb49313d2017-12-24 20:22:27 -0700297PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Eric Newberryb49313d2017-12-24 20:22:27 -0700298 /// Time to mark next packet due to send queue congestion
299 time::steady_clock::TimePoint m_nextMarkTime;
Eric Newberryb49313d2017-12-24 20:22:27 -0700300 /// number of marked packets in the current incident of congestion
301 size_t m_nMarkedSinceInMarkingState;
302
Eric Newberry185ab292017-03-28 06:45:39 +0000303 friend class LpReliability;
Eric Newberrya98bf932015-09-21 00:58:47 -0700304};
305
Eric Newberry86d31872015-09-23 16:24:59 -0700306inline const GenericLinkService::Options&
307GenericLinkService::getOptions() const
308{
309 return m_options;
310}
311
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700312inline const GenericLinkService::Counters&
313GenericLinkService::getCounters() const
314{
315 return *this;
316}
317
Eric Newberrya98bf932015-09-21 00:58:47 -0700318} // namespace face
319} // namespace nfd
320
Eric Newberry86d31872015-09-23 16:24:59 -0700321#endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP