blob: 000245e1c85c53023a24d9c7d446a5cb4c7f2a71 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry7b0071e2017-07-03 17:33:31 +00002/*
Eric Newberry185ab292017-03-28 06:45:39 +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#include "generic-link-service.hpp"
Junxiao Shicbc8e942016-09-06 03:17:45 +000027#include <ndn-cxx/lp/tags.hpp>
Eric Newberrya98bf932015-09-21 00:58:47 -070028
29namespace nfd {
30namespace face {
31
32NFD_LOG_INIT("GenericLinkService");
33
Eric Newberry86d31872015-09-23 16:24:59 -070034GenericLinkService::Options::Options()
35 : allowLocalFields(false)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070036 , allowFragmentation(false)
37 , allowReassembly(false)
Eric Newberry86d31872015-09-23 16:24:59 -070038{
39}
40
41GenericLinkService::GenericLinkService(const GenericLinkService::Options& options)
Eric Newberry73bcad32017-04-25 17:57:35 -070042 : m_options(options)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070043 , m_fragmenter(m_options.fragmenterOptions, this)
44 , m_reassembler(m_options.reassemblerOptions, this)
Eric Newberry185ab292017-03-28 06:45:39 +000045 , m_reliability(m_options.reliabilityOptions, this)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070046 , m_lastSeqNo(-2)
Eric Newberry86d31872015-09-23 16:24:59 -070047{
Junxiao Shi0de23a22015-12-03 20:07:02 +000048 m_reassembler.beforeTimeout.connect(bind([this] { ++this->nReassemblyTimeouts; }));
Eric Newberry73bcad32017-04-25 17:57:35 -070049 nReassembling.observe(&m_reassembler);
Eric Newberry86d31872015-09-23 16:24:59 -070050}
51
Eric Newberrya98bf932015-09-21 00:58:47 -070052void
Eric Newberry185ab292017-03-28 06:45:39 +000053GenericLinkService::setOptions(const GenericLinkService::Options& options)
54{
55 m_options = options;
56 m_fragmenter.setOptions(m_options.fragmenterOptions);
57 m_reassembler.setOptions(m_options.reassemblerOptions);
58 m_reliability.setOptions(m_options.reliabilityOptions);
59}
60
61void
62GenericLinkService::requestIdlePacket()
63{
64 // No need to request Acks to attach to this packet from LpReliability, as they are already
65 // attached in sendLpPacket
66 this->sendLpPacket({});
67}
68
69void
70GenericLinkService::sendLpPacket(lp::Packet&& pkt)
71{
72 const ssize_t mtu = this->getTransport()->getMtu();
73 if (m_options.reliabilityOptions.isEnabled) {
74 m_reliability.piggyback(pkt, mtu);
75 }
76
77 Transport::Packet tp(pkt.wireEncode());
78 if (mtu != MTU_UNLIMITED && tp.packet.size() > static_cast<size_t>(mtu)) {
79 ++this->nOutOverMtu;
80 NFD_LOG_FACE_WARN("attempted to send packet over MTU limit");
81 return;
82 }
83 this->sendPacket(std::move(tp));
84}
85
86void
Eric Newberrya98bf932015-09-21 00:58:47 -070087GenericLinkService::doSendInterest(const Interest& interest)
88{
89 lp::Packet lpPacket(interest.wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +000090
Eric Newberryee400b52016-11-24 14:12:48 +000091 encodeLpFields(interest, lpPacket);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070092
Junxiao Shi0de23a22015-12-03 20:07:02 +000093 this->sendNetPacket(std::move(lpPacket));
Eric Newberrya98bf932015-09-21 00:58:47 -070094}
95
96void
97GenericLinkService::doSendData(const Data& data)
98{
99 lp::Packet lpPacket(data.wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000100
Eric Newberryee400b52016-11-24 14:12:48 +0000101 encodeLpFields(data, lpPacket);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700102
Junxiao Shi0de23a22015-12-03 20:07:02 +0000103 this->sendNetPacket(std::move(lpPacket));
Eric Newberrya98bf932015-09-21 00:58:47 -0700104}
105
106void
107GenericLinkService::doSendNack(const lp::Nack& nack)
108{
109 lp::Packet lpPacket(nack.getInterest().wireEncode());
110 lpPacket.add<lp::NackField>(nack.getHeader());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000111
Eric Newberryee400b52016-11-24 14:12:48 +0000112 encodeLpFields(nack, lpPacket);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700113
Junxiao Shi0de23a22015-12-03 20:07:02 +0000114 this->sendNetPacket(std::move(lpPacket));
Eric Newberrya98bf932015-09-21 00:58:47 -0700115}
116
Junxiao Shi0de23a22015-12-03 20:07:02 +0000117void
Eric Newberryee400b52016-11-24 14:12:48 +0000118GenericLinkService::encodeLpFields(const ndn::TagHost& netPkt, lp::Packet& lpPacket)
Eric Newberry86d31872015-09-23 16:24:59 -0700119{
Eric Newberryee400b52016-11-24 14:12:48 +0000120 if (m_options.allowLocalFields) {
121 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = netPkt.getTag<lp::IncomingFaceIdTag>();
122 if (incomingFaceIdTag != nullptr) {
123 lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag);
124 }
125 }
126
127 shared_ptr<lp::CongestionMarkTag> congestionMarkTag = netPkt.getTag<lp::CongestionMarkTag>();
128 if (congestionMarkTag != nullptr) {
129 lpPacket.add<lp::CongestionMarkField>(*congestionMarkTag);
Eric Newberry86d31872015-09-23 16:24:59 -0700130 }
Eric Newberry86d31872015-09-23 16:24:59 -0700131}
132
Eric Newberrya98bf932015-09-21 00:58:47 -0700133void
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700134GenericLinkService::sendNetPacket(lp::Packet&& pkt)
135{
136 std::vector<lp::Packet> frags;
Eric Newberry7b0071e2017-07-03 17:33:31 +0000137 ssize_t mtu = this->getTransport()->getMtu();
138
139 // Make space for feature fields in fragments
140 if (m_options.reliabilityOptions.isEnabled && mtu != MTU_UNLIMITED) {
141 mtu -= LpReliability::RESERVED_HEADER_SPACE;
142 BOOST_ASSERT(mtu > 0);
143 }
144
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700145 if (m_options.allowFragmentation && mtu != MTU_UNLIMITED) {
146 bool isOk = false;
147 std::tie(isOk, frags) = m_fragmenter.fragmentPacket(pkt, mtu);
148 if (!isOk) {
149 // fragmentation failed (warning is logged by LpFragmenter)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000150 ++this->nFragmentationErrors;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700151 return;
152 }
153 }
154 else {
Eric Newberry185ab292017-03-28 06:45:39 +0000155 frags.push_back(std::move(pkt));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700156 }
157
Eric Newberry185ab292017-03-28 06:45:39 +0000158 if (frags.size() == 1) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700159 // even if indexed fragmentation is enabled, the fragmenter should not
160 // fragment the packet if it can fit in MTU
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700161 BOOST_ASSERT(!frags.front().has<lp::FragIndexField>());
162 BOOST_ASSERT(!frags.front().has<lp::FragCountField>());
163 }
164
Eric Newberry7b0071e2017-07-03 17:33:31 +0000165 // Only assign sequences to fragments if packet contains more than 1 fragment
166 if (frags.size() > 1) {
Eric Newberry185ab292017-03-28 06:45:39 +0000167 // Assign sequences to all fragments
168 this->assignSequences(frags);
169 }
170
171 if (m_options.reliabilityOptions.isEnabled && frags.front().has<lp::FragmentField>()) {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000172 m_reliability.handleOutgoing(frags);
Eric Newberry185ab292017-03-28 06:45:39 +0000173 }
174
175 for (lp::Packet& frag : frags) {
176 this->sendLpPacket(std::move(frag));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700177 }
178}
179
180void
181GenericLinkService::assignSequence(lp::Packet& pkt)
182{
183 pkt.set<lp::SequenceField>(++m_lastSeqNo);
184}
185
186void
187GenericLinkService::assignSequences(std::vector<lp::Packet>& pkts)
188{
189 std::for_each(pkts.begin(), pkts.end(), bind(&GenericLinkService::assignSequence, this, _1));
190}
191
192void
Eric Newberrya98bf932015-09-21 00:58:47 -0700193GenericLinkService::doReceivePacket(Transport::Packet&& packet)
194{
Eric Newberry86d31872015-09-23 16:24:59 -0700195 try {
Eric Newberrya1939ba2015-10-09 12:35:03 -0700196 lp::Packet pkt(packet.packet);
197
Eric Newberry185ab292017-03-28 06:45:39 +0000198 if (m_options.reliabilityOptions.isEnabled) {
199 m_reliability.processIncomingPacket(pkt);
200 }
201
Eric Newberrya1939ba2015-10-09 12:35:03 -0700202 if (!pkt.has<lp::FragmentField>()) {
203 NFD_LOG_FACE_TRACE("received IDLE packet: DROP");
204 return;
205 }
206
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700207 if ((pkt.has<lp::FragIndexField>() || pkt.has<lp::FragCountField>()) &&
208 !m_options.allowReassembly) {
209 NFD_LOG_FACE_WARN("received fragment, but reassembly disabled: DROP");
Eric Newberrya1939ba2015-10-09 12:35:03 -0700210 return;
211 }
212
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700213 bool isReassembled = false;
214 Block netPkt;
215 lp::Packet firstPkt;
216 std::tie(isReassembled, netPkt, firstPkt) = m_reassembler.receiveFragment(packet.remoteEndpoint,
217 pkt);
218 if (isReassembled) {
219 this->decodeNetPacket(netPkt, firstPkt);
220 }
221 }
222 catch (const tlv::Error& e) {
223 ++this->nInLpInvalid;
224 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
225 }
226}
Eric Newberry86d31872015-09-23 16:24:59 -0700227
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700228void
229GenericLinkService::decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt)
230{
231 try {
Eric Newberry86d31872015-09-23 16:24:59 -0700232 switch (netPkt.type()) {
233 case tlv::Interest:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700234 if (firstPkt.has<lp::NackField>()) {
235 this->decodeNack(netPkt, firstPkt);
Eric Newberry86d31872015-09-23 16:24:59 -0700236 }
237 else {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700238 this->decodeInterest(netPkt, firstPkt);
Eric Newberry86d31872015-09-23 16:24:59 -0700239 }
240 break;
241 case tlv::Data:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700242 this->decodeData(netPkt, firstPkt);
Eric Newberry86d31872015-09-23 16:24:59 -0700243 break;
244 default:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700245 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700246 NFD_LOG_FACE_WARN("unrecognized network-layer packet TLV-TYPE " << netPkt.type() << ": DROP");
247 return;
Eric Newberrya98bf932015-09-21 00:58:47 -0700248 }
249 }
Eric Newberry86d31872015-09-23 16:24:59 -0700250 catch (const tlv::Error& e) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700251 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700252 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
253 }
254}
255
Eric Newberry86d31872015-09-23 16:24:59 -0700256void
257GenericLinkService::decodeInterest(const Block& netPkt, const lp::Packet& firstPkt)
258{
259 BOOST_ASSERT(netPkt.type() == tlv::Interest);
260 BOOST_ASSERT(!firstPkt.has<lp::NackField>());
261
262 // forwarding expects Interest to be created with make_shared
263 auto interest = make_shared<Interest>(netPkt);
264
265 if (firstPkt.has<lp::NextHopFaceIdField>()) {
266 if (m_options.allowLocalFields) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000267 interest->setTag(make_shared<lp::NextHopFaceIdTag>(firstPkt.get<lp::NextHopFaceIdField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700268 }
269 else {
270 NFD_LOG_FACE_WARN("received NextHopFaceId, but local fields disabled: DROP");
271 return;
272 }
273 }
274
275 if (firstPkt.has<lp::CachePolicyField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700276 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700277 NFD_LOG_FACE_WARN("received CachePolicy with Interest: DROP");
278 return;
279 }
280
281 if (firstPkt.has<lp::IncomingFaceIdField>()) {
282 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
283 }
284
Eric Newberryee400b52016-11-24 14:12:48 +0000285 if (firstPkt.has<lp::CongestionMarkField>()) {
286 interest->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
287 }
288
Eric Newberry86d31872015-09-23 16:24:59 -0700289 this->receiveInterest(*interest);
290}
291
292void
293GenericLinkService::decodeData(const Block& netPkt, const lp::Packet& firstPkt)
294{
295 BOOST_ASSERT(netPkt.type() == tlv::Data);
296
297 // forwarding expects Data to be created with make_shared
298 auto data = make_shared<Data>(netPkt);
299
300 if (firstPkt.has<lp::NackField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700301 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700302 NFD_LOG_FACE_WARN("received Nack with Data: DROP");
303 return;
304 }
305
306 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700307 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700308 NFD_LOG_FACE_WARN("received NextHopFaceId with Data: DROP");
309 return;
310 }
311
312 if (firstPkt.has<lp::CachePolicyField>()) {
Junxiao Shi6eb02712017-05-27 22:48:02 +0000313 // CachePolicy is unprivileged and does not require allowLocalFields option.
314 // In case of an invalid CachePolicyType, get<lp::CachePolicyField> will throw,
315 // so it's unnecessary to check here.
316 data->setTag(make_shared<lp::CachePolicyTag>(firstPkt.get<lp::CachePolicyField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700317 }
318
319 if (firstPkt.has<lp::IncomingFaceIdField>()) {
320 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
321 }
322
Eric Newberryee400b52016-11-24 14:12:48 +0000323 if (firstPkt.has<lp::CongestionMarkField>()) {
324 data->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
325 }
326
Eric Newberry86d31872015-09-23 16:24:59 -0700327 this->receiveData(*data);
328}
329
330void
331GenericLinkService::decodeNack(const Block& netPkt, const lp::Packet& firstPkt)
332{
333 BOOST_ASSERT(netPkt.type() == tlv::Interest);
334 BOOST_ASSERT(firstPkt.has<lp::NackField>());
335
336 lp::Nack nack((Interest(netPkt)));
337 nack.setHeader(firstPkt.get<lp::NackField>());
338
339 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700340 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700341 NFD_LOG_FACE_WARN("received NextHopFaceId with Nack: DROP");
342 return;
343 }
344
345 if (firstPkt.has<lp::CachePolicyField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700346 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700347 NFD_LOG_FACE_WARN("received CachePolicy with Nack: DROP");
348 return;
349 }
350
351 if (firstPkt.has<lp::IncomingFaceIdField>()) {
352 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
353 }
354
Eric Newberryee400b52016-11-24 14:12:48 +0000355 if (firstPkt.has<lp::CongestionMarkField>()) {
356 nack.setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
357 }
358
Eric Newberry86d31872015-09-23 16:24:59 -0700359 this->receiveNack(nack);
Eric Newberrya98bf932015-09-21 00:58:47 -0700360}
361
362} // namespace face
363} // namespace nfd