blob: d74b1f2a121414a58c406caacb326324e16e4d65 [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 Newberryb49313d2017-12-24 20:22:27 -07003 * Copyright (c) 2014-2018, 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
84 /** \brief count of outgoing LpPackets that were marked with congestion marks
85 */
86 PacketCounter nCongestionMarked;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070087};
88
Eric Newberry86d31872015-09-23 16:24:59 -070089/** \brief GenericLinkService is a LinkService that implements the NDNLPv2 protocol
Eric Newberry185ab292017-03-28 06:45:39 +000090 * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
Eric Newberrya98bf932015-09-21 00:58:47 -070091 */
92class GenericLinkService : public LinkService
Eric Newberry73bcad32017-04-25 17:57:35 -070093 , protected virtual GenericLinkServiceCounters
Eric Newberrya98bf932015-09-21 00:58:47 -070094{
Eric Newberry86d31872015-09-23 16:24:59 -070095public:
96 /** \brief Options that control the behavior of GenericLinkService
97 */
98 class Options
99 {
100 public:
101 Options();
102
103 public:
Eric Newberry86d31872015-09-23 16:24:59 -0700104 /** \brief enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy
105 */
106 bool allowLocalFields;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700107
108 /** \brief enables fragmentation
109 */
110 bool allowFragmentation;
111
112 /** \brief options for fragmentation
113 */
114 LpFragmenter::Options fragmenterOptions;
115
116 /** \brief enables reassembly
117 */
118 bool allowReassembly;
119
120 /** \brief options for reassembly
121 */
122 LpReassembler::Options reassemblerOptions;
Eric Newberry185ab292017-03-28 06:45:39 +0000123
124 /** \brief options for reliability
125 */
126 LpReliability::Options reliabilityOptions;
Eric Newberryb49313d2017-12-24 20:22:27 -0700127
128 /** \brief enables send queue congestion detection and marking
129 */
130 bool allowCongestionMarking;
131
132 /** \brief starting value for congestion marking interval
133 */
134 time::nanoseconds baseCongestionMarkingInterval;
135
136 /** \brief default congestion threshold in bytes
137 */
138 size_t defaultCongestionThreshold;
Eric Newberry86d31872015-09-23 16:24:59 -0700139 };
140
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700141 /** \brief counters provided by GenericLinkService
142 */
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000143 using Counters = GenericLinkServiceCounters;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700144
Eric Newberry86d31872015-09-23 16:24:59 -0700145 explicit
146 GenericLinkService(const Options& options = Options());
147
148 /** \brief get Options used by GenericLinkService
149 */
150 const Options&
151 getOptions() const;
152
153 /** \brief sets Options used by GenericLinkService
154 */
155 void
156 setOptions(const Options& options);
157
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000158 const Counters&
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200159 getCounters() const override;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700160
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000161PROTECTED_WITH_TESTS_ELSE_PRIVATE: // send path
Eric Newberry185ab292017-03-28 06:45:39 +0000162 /** \brief request an IDLE packet to transmit pending service fields
163 */
164 void
165 requestIdlePacket();
166
167 /** \brief send an LpPacket fragment
168 * \param pkt LpPacket to send
169 */
170 void
171 sendLpPacket(lp::Packet&& pkt);
172
Junxiao Shi0de23a22015-12-03 20:07:02 +0000173 /** \brief send Interest
Eric Newberrya98bf932015-09-21 00:58:47 -0700174 */
175 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200176 doSendInterest(const Interest& interest) override;
Eric Newberrya98bf932015-09-21 00:58:47 -0700177
Junxiao Shi0de23a22015-12-03 20:07:02 +0000178 /** \brief send Data
Eric Newberrya98bf932015-09-21 00:58:47 -0700179 */
180 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200181 doSendData(const Data& data) override;
Eric Newberrya98bf932015-09-21 00:58:47 -0700182
Junxiao Shi0de23a22015-12-03 20:07:02 +0000183 /** \brief send Nack
Eric Newberrya98bf932015-09-21 00:58:47 -0700184 */
185 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200186 doSendNack(const ndn::lp::Nack& nack) override;
Eric Newberrya98bf932015-09-21 00:58:47 -0700187
Eric Newberry185ab292017-03-28 06:45:39 +0000188private: // send path
Eric Newberryee400b52016-11-24 14:12:48 +0000189 /** \brief encode link protocol fields from tags onto an outgoing LpPacket
190 * \param netPkt network-layer packet to extract tags from
191 * \param lpPacket LpPacket to add link protocol fields to
Eric Newberry86d31872015-09-23 16:24:59 -0700192 */
Eric Newberryee400b52016-11-24 14:12:48 +0000193 void
Eric Newberry41aba102017-11-01 16:42:13 -0700194 encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket);
Eric Newberry86d31872015-09-23 16:24:59 -0700195
Junxiao Shi0de23a22015-12-03 20:07:02 +0000196 /** \brief send a complete network layer packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700197 * \param pkt LpPacket containing a complete network layer packet
Eric Newberry41aba102017-11-01 16:42:13 -0700198 * \param isInterest whether the network layer packet is an Interest
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700199 */
200 void
Eric Newberry41aba102017-11-01 16:42:13 -0700201 sendNetPacket(lp::Packet&& pkt, bool isInterest);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700202
203 /** \brief assign a sequence number to an LpPacket
204 */
205 void
206 assignSequence(lp::Packet& pkt);
207
208 /** \brief assign consecutive sequence numbers to LpPackets
209 */
210 void
211 assignSequences(std::vector<lp::Packet>& pkts);
212
Eric Newberryb49313d2017-12-24 20:22:27 -0700213 /** \brief if the send queue is found to be congested, add a congestion mark to the packet
214 * according to CoDel
215 * \sa https://tools.ietf.org/html/rfc8289
216 */
217 void
218 checkCongestionLevel(lp::Packet& pkt);
219
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700220private: // receive path
221 /** \brief receive Packet from Transport
222 */
223 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200224 doReceivePacket(Transport::Packet&& packet) override;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700225
226 /** \brief decode incoming network-layer packet
227 * \param netPkt reassembled network-layer packet
228 * \param firstPkt LpPacket of first fragment
229 *
230 * If decoding is successful, a receive signal is emitted;
231 * otherwise, a warning is logged.
232 */
233 void
234 decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt);
235
Eric Newberry86d31872015-09-23 16:24:59 -0700236 /** \brief decode incoming Interest
237 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
238 * \param firstPkt LpPacket of first fragment; must not have Nack field
239 *
240 * If decoding is successful, receiveInterest signal is emitted;
241 * otherwise, a warning is logged.
242 *
243 * \throw tlv::Error parse error in an LpHeader field
244 */
245 void
246 decodeInterest(const Block& netPkt, const lp::Packet& firstPkt);
247
248 /** \brief decode incoming Interest
249 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Data
250 * \param firstPkt LpPacket of first fragment
251 *
252 * If decoding is successful, receiveData signal is emitted;
253 * otherwise, a warning is logged.
254 *
255 * \throw tlv::Error parse error in an LpHeader field
256 */
257 void
258 decodeData(const Block& netPkt, const lp::Packet& firstPkt);
259
260 /** \brief decode incoming Interest
261 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
262 * \param firstPkt LpPacket of first fragment; must have Nack field
263 *
264 * If decoding is successful, receiveNack signal is emitted;
265 * otherwise, a warning is logged.
266 *
267 * \throw tlv::Error parse error in an LpHeader field
268 */
269 void
270 decodeNack(const Block& netPkt, const lp::Packet& firstPkt);
271
Eric Newberry185ab292017-03-28 06:45:39 +0000272PROTECTED_WITH_TESTS_ELSE_PRIVATE:
Eric Newberry86d31872015-09-23 16:24:59 -0700273 Options m_options;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700274 LpFragmenter m_fragmenter;
275 LpReassembler m_reassembler;
Eric Newberry185ab292017-03-28 06:45:39 +0000276 LpReliability m_reliability;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700277 lp::Sequence m_lastSeqNo;
Eric Newberry185ab292017-03-28 06:45:39 +0000278
Eric Newberryb49313d2017-12-24 20:22:27 -0700279PUBLIC_WITH_TESTS_ELSE_PRIVATE:
280 /// CongestionMark TLV-TYPE (3 octets) + CongestionMark TLV-LENGTH (1 octet) + sizeof(uint64_t)
281 static constexpr size_t CONGESTION_MARK_SIZE = 3 + 1 + sizeof(uint64_t);
282 /// Time to mark next packet due to send queue congestion
283 time::steady_clock::TimePoint m_nextMarkTime;
284 /// Time last packet was marked
285 time::steady_clock::TimePoint m_lastMarkTime;
286 /// number of marked packets in the current incident of congestion
287 size_t m_nMarkedSinceInMarkingState;
288
Eric Newberry185ab292017-03-28 06:45:39 +0000289 friend class LpReliability;
Eric Newberrya98bf932015-09-21 00:58:47 -0700290};
291
Eric Newberry86d31872015-09-23 16:24:59 -0700292inline const GenericLinkService::Options&
293GenericLinkService::getOptions() const
294{
295 return m_options;
296}
297
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700298inline const GenericLinkService::Counters&
299GenericLinkService::getCounters() const
300{
301 return *this;
302}
303
Eric Newberrya98bf932015-09-21 00:58:47 -0700304} // namespace face
305} // namespace nfd
306
Eric Newberry86d31872015-09-23 16:24:59 -0700307#endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP