blob: d7cc22f9708e39b11ce986ee2072fba74c7c0fd0 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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
29#include "common.hpp"
30#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
Eric Newberrya98bf932015-09-21 00:58:47 -070078 */
79class GenericLinkService : public LinkService
Eric Newberry4c3e6b82015-11-10 16:48:42 -070080 , protected virtual GenericLinkServiceCounters
Eric Newberrya98bf932015-09-21 00:58:47 -070081{
Eric Newberry86d31872015-09-23 16:24:59 -070082public:
83 /** \brief Options that control the behavior of GenericLinkService
84 */
85 class Options
86 {
87 public:
88 Options();
89
90 public:
Eric Newberry86d31872015-09-23 16:24:59 -070091 /** \brief enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy
92 */
93 bool allowLocalFields;
Eric Newberry4c3e6b82015-11-10 16:48:42 -070094
95 /** \brief enables fragmentation
96 */
97 bool allowFragmentation;
98
99 /** \brief options for fragmentation
100 */
101 LpFragmenter::Options fragmenterOptions;
102
103 /** \brief enables reassembly
104 */
105 bool allowReassembly;
106
107 /** \brief options for reassembly
108 */
109 LpReassembler::Options reassemblerOptions;
Eric Newberry86d31872015-09-23 16:24:59 -0700110 };
111
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700112 /** \brief counters provided by GenericLinkService
113 */
114 typedef GenericLinkServiceCounters Counters;
115
Eric Newberry86d31872015-09-23 16:24:59 -0700116 explicit
117 GenericLinkService(const Options& options = Options());
118
119 /** \brief get Options used by GenericLinkService
120 */
121 const Options&
122 getOptions() const;
123
124 /** \brief sets Options used by GenericLinkService
125 */
126 void
127 setOptions(const Options& options);
128
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700129 virtual const Counters&
130 getCounters() const DECL_OVERRIDE;
131
132private: // send path
Eric Newberrya98bf932015-09-21 00:58:47 -0700133 /** \brief sends Interest
134 */
135 void
136 doSendInterest(const Interest& interest) DECL_OVERRIDE;
137
138 /** \brief sends Data
139 */
140 void
141 doSendData(const Data& data) DECL_OVERRIDE;
142
143 /** \brief sends Nack
144 * This class does not send out a Nack.
145 */
146 void
147 doSendNack(const ndn::lp::Nack& nack) DECL_OVERRIDE;
148
Eric Newberry86d31872015-09-23 16:24:59 -0700149 /** \brief encode IncomingFaceId into LpPacket and verify local fields
150 */
151 static bool
152 encodeLocalFields(const Interest& interest, lp::Packet& lpPacket);
153
154 /** \brief encode CachingPolicy and IncomingFaceId into LpPacket and verify local fields
155 */
156 static bool
157 encodeLocalFields(const Data& data, lp::Packet& lpPacket);
158
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700159 /** \brief encode and send a complete network layer packet
160 * \param pkt LpPacket containing a complete network layer packet
161 */
162 void
163 sendNetPacket(lp::Packet&& pkt);
164
165 /** \brief assign a sequence number to an LpPacket
166 */
167 void
168 assignSequence(lp::Packet& pkt);
169
170 /** \brief assign consecutive sequence numbers to LpPackets
171 */
172 void
173 assignSequences(std::vector<lp::Packet>& pkts);
174
175private: // receive path
176 /** \brief receive Packet from Transport
177 */
178 void
179 doReceivePacket(Transport::Packet&& packet) DECL_OVERRIDE;
180
181 /** \brief decode incoming network-layer packet
182 * \param netPkt reassembled network-layer packet
183 * \param firstPkt LpPacket of first fragment
184 *
185 * If decoding is successful, a receive signal is emitted;
186 * otherwise, a warning is logged.
187 */
188 void
189 decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt);
190
Eric Newberry86d31872015-09-23 16:24:59 -0700191 /** \brief decode incoming Interest
192 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
193 * \param firstPkt LpPacket of first fragment; must not have Nack field
194 *
195 * If decoding is successful, receiveInterest signal is emitted;
196 * otherwise, a warning is logged.
197 *
198 * \throw tlv::Error parse error in an LpHeader field
199 */
200 void
201 decodeInterest(const Block& netPkt, const lp::Packet& firstPkt);
202
203 /** \brief decode incoming Interest
204 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Data
205 * \param firstPkt LpPacket of first fragment
206 *
207 * If decoding is successful, receiveData signal is emitted;
208 * otherwise, a warning is logged.
209 *
210 * \throw tlv::Error parse error in an LpHeader field
211 */
212 void
213 decodeData(const Block& netPkt, const lp::Packet& firstPkt);
214
215 /** \brief decode incoming Interest
216 * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest
217 * \param firstPkt LpPacket of first fragment; must have Nack field
218 *
219 * If decoding is successful, receiveNack signal is emitted;
220 * otherwise, a warning is logged.
221 *
222 * \throw tlv::Error parse error in an LpHeader field
223 */
224 void
225 decodeNack(const Block& netPkt, const lp::Packet& firstPkt);
226
227private:
228 Options m_options;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700229 LpFragmenter m_fragmenter;
230 LpReassembler m_reassembler;
231 lp::Sequence m_lastSeqNo;
Eric Newberrya98bf932015-09-21 00:58:47 -0700232};
233
Eric Newberry86d31872015-09-23 16:24:59 -0700234inline const GenericLinkService::Options&
235GenericLinkService::getOptions() const
236{
237 return m_options;
238}
239
240inline void
241GenericLinkService::setOptions(const GenericLinkService::Options& options)
242{
243 m_options = options;
244}
245
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700246inline const GenericLinkService::Counters&
247GenericLinkService::getCounters() const
248{
249 return *this;
250}
251
Eric Newberrya98bf932015-09-21 00:58:47 -0700252} // namespace face
253} // namespace nfd
254
Eric Newberry86d31872015-09-23 16:24:59 -0700255#endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP