blob: 1d321e2d7cf7d3a3b0d379790ebb926406456635 [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 Pesavento91c15c82024-01-15 17:14:23 -05003 * Copyright (c) 2014-2024, 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
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050034#include <limits>
35
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::face {
Eric Newberrya98bf932015-09-21 00:58:47 -070037
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050038/**
39 * \brief Counters provided by GenericLinkService.
40 * \note The type name GenericLinkServiceCounters is an implementation detail.
41 * Use GenericLinkService::Counters in public API.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070042 */
43class GenericLinkServiceCounters : public virtual LinkService::Counters
44{
45public:
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050046 /// Count of failed fragmentations.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070047 PacketCounter nFragmentationErrors;
48
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050049 /**
50 * \brief Count of outgoing LpPackets dropped due to exceeding MTU limit.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070051 *
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050052 * If this counter is non-zero, the operator should enable fragmentation.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070053 */
54 PacketCounter nOutOverMtu;
55
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050056 /// Count of invalid LpPackets dropped before reassembly.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070057 PacketCounter nInLpInvalid;
58
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050059 /// Count of network-layer packets currently being reassembled.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070060 SizeCounter<LpReassembler> nReassembling;
61
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050062 /// Count of dropped partial network-layer packets due to reassembly timeout.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070063 PacketCounter nReassemblyTimeouts;
64
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050065 /// Count of invalid reassembled network-layer packets dropped.
Eric Newberry4c3e6b82015-11-10 16:48:42 -070066 PacketCounter nInNetInvalid;
Eric Newberry185ab292017-03-28 06:45:39 +000067
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050068 /// Count of network-layer packets that did not require retransmission of a fragment.
Eric Newberry185ab292017-03-28 06:45:39 +000069 PacketCounter nAcknowledged;
70
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050071 /**
72 * \brief Count of network-layer packets that had at least one fragment retransmitted,
73 * but were eventually received in full.
Eric Newberry185ab292017-03-28 06:45:39 +000074 */
75 PacketCounter nRetransmitted;
76
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050077 /**
78 * \brief Count of network-layer packets dropped because a fragment reached the maximum
79 * number of retransmissions.
Eric Newberry185ab292017-03-28 06:45:39 +000080 */
81 PacketCounter nRetxExhausted;
Eric Newberryb49313d2017-12-24 20:22:27 -070082
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050083 /// Count of LpPackets dropped due to duplicate Sequence numbers.
Eric Newberry32f7eac2020-02-07 14:40:17 -080084 PacketCounter nDuplicateSequence;
85
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050086 /// Count of outgoing LpPackets that were marked with congestion marks.
Eric Newberryb49313d2017-12-24 20:22:27 -070087 PacketCounter nCongestionMarked;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070088};
89
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040090/**
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050091 * \brief Options that control the behavior of GenericLinkService.
92 * \note The type name GenericLinkServiceOptions is an implementation detail.
93 * Use GenericLinkService::Options in public API.
94 */
95struct GenericLinkServiceOptions
96{
97 /** \brief Enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy.
98 */
99 bool allowLocalFields = false;
100
101 /** \brief Enables fragmentation.
102 */
103 bool allowFragmentation = false;
104
105 /** \brief Options for fragmentation.
106 */
107 LpFragmenter::Options fragmenterOptions;
108
109 /** \brief Enables reassembly.
110 */
111 bool allowReassembly = false;
112
113 /** \brief Options for reassembly.
114 */
115 LpReassembler::Options reassemblerOptions;
116
117 /** \brief Options for reliability.
118 */
119 LpReliability::Options reliabilityOptions;
120
121 /** \brief Enables send queue congestion detection and marking.
122 */
123 bool allowCongestionMarking = false;
124
125 /** \brief Starting value for congestion marking interval.
126 *
127 * Packets are marked if the queue size stays above THRESHOLD for at least one INTERVAL.
128 *
129 * The default value (100 ms) is taken from RFC 8289 (CoDel).
130 */
131 time::nanoseconds baseCongestionMarkingInterval = 100_ms;
132
133 /** \brief Default congestion threshold in bytes.
134 *
135 * Packets are marked if the queue size stays above THRESHOLD for at least one INTERVAL.
136 *
137 * The default value (64 KiB) works well for a queue capacity of 200 KiB.
138 */
139 size_t defaultCongestionThreshold = 65536;
140
141 /** \brief Enables self-learning forwarding support.
142 */
143 bool allowSelfLearning = true;
144
145 /** \brief Overrides the MTU provided by Transport.
146 *
147 * This MTU value will be used instead of the MTU provided by the transport if it is less than
148 * the transport MTU. However, it will not be utilized when the transport MTU is unlimited.
149 *
150 * Acceptable values for this option are values >= #MIN_MTU, which can be validated before
151 * being set with canOverrideMtuTo().
152 */
153 ssize_t overrideMtu = std::numeric_limits<ssize_t>::max();
154};
155
156/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400157 * \brief GenericLinkService is a LinkService that implements the NDNLPv2 protocol.
158 * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
Eric Newberrya98bf932015-09-21 00:58:47 -0700159 */
Davide Pesavento264af772021-02-09 21:48:24 -0500160class GenericLinkService NFD_FINAL_UNLESS_WITH_TESTS : public LinkService
161 , protected virtual GenericLinkServiceCounters
Eric Newberrya98bf932015-09-21 00:58:47 -0700162{
Eric Newberry86d31872015-09-23 16:24:59 -0700163public:
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500164 /**
165 * \brief %Counters provided by GenericLinkService.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700166 */
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000167 using Counters = GenericLinkServiceCounters;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700168
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500169 /**
170 * \brief %Options for GenericLinkService.
171 */
172 using Options = GenericLinkServiceOptions;
173
Eric Newberry86d31872015-09-23 16:24:59 -0700174 explicit
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400175 GenericLinkService(const Options& options = {});
Eric Newberry86d31872015-09-23 16:24:59 -0700176
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500177 /**
178 * \brief Get the options used by GenericLinkService.
Eric Newberry86d31872015-09-23 16:24:59 -0700179 */
180 const Options&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400181 getOptions() const
182 {
183 return m_options;
184 }
Eric Newberry86d31872015-09-23 16:24:59 -0700185
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500186 /**
187 * \brief Sets the options used by GenericLinkService.
Eric Newberry86d31872015-09-23 16:24:59 -0700188 */
189 void
190 setOptions(const Options& options);
191
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000192 const Counters&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400193 getCounters() const NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL
194 {
195 return *this;
196 }
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700197
Eric Newberrycb6551e2020-03-02 14:12:16 -0800198 ssize_t
Davide Pesavento264af772021-02-09 21:48:24 -0500199 getEffectiveMtu() const NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrycb6551e2020-03-02 14:12:16 -0800200
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400201 /** \brief Whether MTU can be overridden to the specified value.
Eric Newberrycb6551e2020-03-02 14:12:16 -0800202 *
203 * If the transport MTU is unlimited, then this will always return false.
204 */
205 bool
206 canOverrideMtuTo(ssize_t mtu) const;
207
Davide Pesavento264af772021-02-09 21:48:24 -0500208NFD_PROTECTED_WITH_TESTS_ELSE_PRIVATE: // send path
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400209 /** \brief Request an IDLE packet to transmit pending service fields.
Eric Newberry185ab292017-03-28 06:45:39 +0000210 */
211 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700212 requestIdlePacket();
Eric Newberry185ab292017-03-28 06:45:39 +0000213
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400214 /** \brief Send an LpPacket.
Eric Newberry185ab292017-03-28 06:45:39 +0000215 */
216 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700217 sendLpPacket(lp::Packet&& pkt);
Eric Newberry185ab292017-03-28 06:45:39 +0000218
Eric Newberrya98bf932015-09-21 00:58:47 -0700219 void
Davide Pesavento264af772021-02-09 21:48:24 -0500220 doSendInterest(const Interest& interest) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700221
Eric Newberrya98bf932015-09-21 00:58:47 -0700222 void
Davide Pesavento264af772021-02-09 21:48:24 -0500223 doSendData(const Data& data) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700224
Eric Newberrya98bf932015-09-21 00:58:47 -0700225 void
Davide Pesavento264af772021-02-09 21:48:24 -0500226 doSendNack(const ndn::lp::Nack& nack) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberrya98bf932015-09-21 00:58:47 -0700227
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400228 /** \brief Assign consecutive sequence numbers to LpPackets.
Eric Newberry32f7eac2020-02-07 14:40:17 -0800229 */
230 void
231 assignSequences(std::vector<lp::Packet>& pkts);
232
Eric Newberry185ab292017-03-28 06:45:39 +0000233private: // send path
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400234 /** \brief Encode link protocol fields from tags onto an outgoing LpPacket.
Eric Newberryee400b52016-11-24 14:12:48 +0000235 * \param netPkt network-layer packet to extract tags from
236 * \param lpPacket LpPacket to add link protocol fields to
Eric Newberry86d31872015-09-23 16:24:59 -0700237 */
Eric Newberryee400b52016-11-24 14:12:48 +0000238 void
Eric Newberry41aba102017-11-01 16:42:13 -0700239 encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket);
Eric Newberry86d31872015-09-23 16:24:59 -0700240
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400241 /** \brief Send a complete network layer packet.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700242 * \param pkt LpPacket containing a complete network layer packet
Eric Newberry41aba102017-11-01 16:42:13 -0700243 * \param isInterest whether the network layer packet is an Interest
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700244 */
245 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700246 sendNetPacket(lp::Packet&& pkt, bool isInterest);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700247
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400248 /** \brief If the send queue is found to be congested, add a congestion mark to the packet
249 * according to CoDel.
Eric Newberryb49313d2017-12-24 20:22:27 -0700250 * \sa https://tools.ietf.org/html/rfc8289
251 */
252 void
253 checkCongestionLevel(lp::Packet& pkt);
254
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700255private: // receive path
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700256 void
Davide Pesavento264af772021-02-09 21:48:24 -0500257 doReceivePacket(const Block& packet, const EndpointId& endpoint) NFD_OVERRIDE_WITH_TESTS_ELSE_FINAL;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700258
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400259 /** \brief Decode incoming network-layer packet.
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700260 * \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
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400270 /** \brief Decode incoming Interest.
Eric Newberry86d31872015-09-23 16:24:59 -0700271 * \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
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400283 /** \brief Decode incoming Interest.
Eric Newberry86d31872015-09-23 16:24:59 -0700284 * \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
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400296 /** \brief Decode incoming Interest.
Eric Newberry86d31872015-09-23 16:24:59 -0700297 * \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
Davide Pesavento264af772021-02-09 21:48:24 -0500309NFD_PROTECTED_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;
Davide Pesavento91c15c82024-01-15 17:14:23 -0500314 lp::Sequence m_lastSeqNo = static_cast<lp::Sequence>(-2);
Eric Newberry185ab292017-03-28 06:45:39 +0000315
Davide Pesavento264af772021-02-09 21:48:24 -0500316NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Eric Newberryb49313d2017-12-24 20:22:27 -0700317 /// Time to mark next packet due to send queue congestion
Davide Pesavento91c15c82024-01-15 17:14:23 -0500318 time::steady_clock::time_point m_nextMarkTime = time::steady_clock::time_point::max();
319 /// Number of marked packets in the current incident of congestion
320 size_t m_nMarkedSinceInMarkingState = 0;
Eric Newberryb49313d2017-12-24 20:22:27 -0700321
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400322 friend LpReliability;
Eric Newberrya98bf932015-09-21 00:58:47 -0700323};
324
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400325} // namespace nfd::face
Eric Newberrya98bf932015-09-21 00:58:47 -0700326
Eric Newberry86d31872015-09-23 16:24:59 -0700327#endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP