blob: b675ae2203fe09a30c8e66f4d47c561b0bf47d0e [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shifab9e0d2017-02-02 06:04:59 +00003 * Copyright (c) 2014-2017, 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
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000029#include "core/common.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070030#include "core/logger.hpp"
31
32#include "link-service.hpp"
Eric Newberry4c3e6b82015-11-10 16:48:42 -070033#include "lp-fragmenter.hpp"
34#include "lp-reassembler.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070035
36namespace 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:
46 explicit
47 GenericLinkServiceCounters(const LpReassembler& reassembler);
48
49 /** \brief count of failed fragmentations
50 */
51 PacketCounter nFragmentationErrors;
52
53 /** \brief count of outgoing LpPackets dropped due to exceeding MTU limit
54 *
55 * If this counter is non-zero, the operator should enable fragmentation.
56 */
57 PacketCounter nOutOverMtu;
58
59 /** \brief count of invalid LpPackets dropped before reassembly
60 */
61 PacketCounter nInLpInvalid;
62
63 /** \brief count of network-layer packets currently being reassembled
64 */
65 SizeCounter<LpReassembler> nReassembling;
66
67 /** \brief count of dropped partial network-layer packets due to reassembly timeout
68 */
69 PacketCounter nReassemblyTimeouts;
70
71 /** \brief count of invalid reassembled network-layer packets dropped
72 */
73 PacketCounter nInNetInvalid;
74};
75
Eric Newberry86d31872015-09-23 16:24:59 -070076/** \brief GenericLinkService is a LinkService that implements the NDNLPv2 protocol
77 * \sa http://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
Junxiao Shifab9e0d2017-02-02 06:04:59 +000078 * \todo #3941 declare GenericLinkServiceCounters as virtual inheritance
Eric Newberrya98bf932015-09-21 00:58:47 -070079 */
80class GenericLinkService : public LinkService
Junxiao Shifab9e0d2017-02-02 06:04:59 +000081 , protected GenericLinkServiceCounters
Eric Newberrya98bf932015-09-21 00:58:47 -070082{
Eric Newberry86d31872015-09-23 16:24:59 -070083public:
84 /** \brief Options that control the behavior of GenericLinkService
85 */
86 class Options
87 {
88 public:
89 Options();
90
91 public:
Eric Newberry86d31872015-09-23 16:24:59 -070092 /** \brief enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy
93 */
94 bool allowLocalFields;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070095
96 /** \brief enables fragmentation
97 */
98 bool allowFragmentation;
99
100 /** \brief options for fragmentation
101 */
102 LpFragmenter::Options fragmenterOptions;
103
104 /** \brief enables reassembly
105 */
106 bool allowReassembly;
107
108 /** \brief options for reassembly
109 */
110 LpReassembler::Options reassemblerOptions;
Eric Newberry86d31872015-09-23 16:24:59 -0700111 };
112
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700113 /** \brief counters provided by GenericLinkService
114 */
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000115 using Counters = GenericLinkServiceCounters;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700116
Eric Newberry86d31872015-09-23 16:24:59 -0700117 explicit
118 GenericLinkService(const Options& options = Options());
119
120 /** \brief get Options used by GenericLinkService
121 */
122 const Options&
123 getOptions() const;
124
125 /** \brief sets Options used by GenericLinkService
126 */
127 void
128 setOptions(const Options& options);
129
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000130 const Counters&
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200131 getCounters() const override;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700132
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000133PROTECTED_WITH_TESTS_ELSE_PRIVATE: // send path
Junxiao Shi0de23a22015-12-03 20:07:02 +0000134 /** \brief send Interest
Eric Newberrya98bf932015-09-21 00:58:47 -0700135 */
136 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200137 doSendInterest(const Interest& interest) override;
Eric Newberrya98bf932015-09-21 00:58:47 -0700138
Junxiao Shi0de23a22015-12-03 20:07:02 +0000139 /** \brief send Data
Eric Newberrya98bf932015-09-21 00:58:47 -0700140 */
141 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200142 doSendData(const Data& data) override;
Eric Newberrya98bf932015-09-21 00:58:47 -0700143
Junxiao Shi0de23a22015-12-03 20:07:02 +0000144 /** \brief send Nack
Eric Newberrya98bf932015-09-21 00:58:47 -0700145 */
146 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200147 doSendNack(const ndn::lp::Nack& nack) override;
Eric Newberrya98bf932015-09-21 00:58:47 -0700148
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000149private:
Eric Newberryee400b52016-11-24 14:12:48 +0000150 /** \brief encode link protocol fields from tags onto an outgoing LpPacket
151 * \param netPkt network-layer packet to extract tags from
152 * \param lpPacket LpPacket to add link protocol fields to
Eric Newberry86d31872015-09-23 16:24:59 -0700153 */
Eric Newberryee400b52016-11-24 14:12:48 +0000154 void
155 encodeLpFields(const ndn::TagHost& netPkt, lp::Packet& lpPacket);
Eric Newberry86d31872015-09-23 16:24:59 -0700156
Junxiao Shi0de23a22015-12-03 20:07:02 +0000157 /** \brief send a complete network layer packet
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700158 * \param pkt LpPacket containing a complete network layer packet
159 */
160 void
161 sendNetPacket(lp::Packet&& pkt);
162
163 /** \brief assign a sequence number to an LpPacket
164 */
165 void
166 assignSequence(lp::Packet& pkt);
167
168 /** \brief assign consecutive sequence numbers to LpPackets
169 */
170 void
171 assignSequences(std::vector<lp::Packet>& pkts);
172
173private: // receive path
174 /** \brief receive Packet from Transport
175 */
176 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200177 doReceivePacket(Transport::Packet&& packet) override;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700178
179 /** \brief decode incoming network-layer packet
180 * \param netPkt reassembled network-layer packet
181 * \param firstPkt LpPacket of first fragment
182 *
183 * If decoding is successful, a receive signal is emitted;
184 * otherwise, a warning is logged.
185 */
186 void
187 decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt);
188
Eric Newberry86d31872015-09-23 16:24:59 -0700189 /** \brief decode incoming Interest
190 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
191 * \param firstPkt LpPacket of first fragment; must not have Nack field
192 *
193 * If decoding is successful, receiveInterest signal is emitted;
194 * otherwise, a warning is logged.
195 *
196 * \throw tlv::Error parse error in an LpHeader field
197 */
198 void
199 decodeInterest(const Block& netPkt, const lp::Packet& firstPkt);
200
201 /** \brief decode incoming Interest
202 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Data
203 * \param firstPkt LpPacket of first fragment
204 *
205 * If decoding is successful, receiveData signal is emitted;
206 * otherwise, a warning is logged.
207 *
208 * \throw tlv::Error parse error in an LpHeader field
209 */
210 void
211 decodeData(const Block& netPkt, const lp::Packet& firstPkt);
212
213 /** \brief decode incoming Interest
214 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
215 * \param firstPkt LpPacket of first fragment; must have Nack field
216 *
217 * If decoding is successful, receiveNack signal is emitted;
218 * otherwise, a warning is logged.
219 *
220 * \throw tlv::Error parse error in an LpHeader field
221 */
222 void
223 decodeNack(const Block& netPkt, const lp::Packet& firstPkt);
224
225private:
226 Options m_options;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700227 LpFragmenter m_fragmenter;
228 LpReassembler m_reassembler;
229 lp::Sequence m_lastSeqNo;
Eric Newberrya98bf932015-09-21 00:58:47 -0700230};
231
Eric Newberry86d31872015-09-23 16:24:59 -0700232inline const GenericLinkService::Options&
233GenericLinkService::getOptions() const
234{
235 return m_options;
236}
237
238inline void
239GenericLinkService::setOptions(const GenericLinkService::Options& options)
240{
241 m_options = options;
242}
243
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700244inline const GenericLinkService::Counters&
245GenericLinkService::getCounters() const
246{
247 return *this;
248}
249
Eric Newberrya98bf932015-09-21 00:58:47 -0700250} // namespace face
251} // namespace nfd
252
Eric Newberry86d31872015-09-23 16:24:59 -0700253#endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP