blob: 20c1cb80a887b8c89ca533fbe43ac7bd43220312 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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;
137 const ssize_t mtu = this->getTransport()->getMtu();
138 if (m_options.allowFragmentation && mtu != MTU_UNLIMITED) {
139 bool isOk = false;
140 std::tie(isOk, frags) = m_fragmenter.fragmentPacket(pkt, mtu);
141 if (!isOk) {
142 // fragmentation failed (warning is logged by LpFragmenter)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000143 ++this->nFragmentationErrors;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700144 return;
145 }
146 }
147 else {
Eric Newberry185ab292017-03-28 06:45:39 +0000148 frags.push_back(std::move(pkt));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700149 }
150
Eric Newberry185ab292017-03-28 06:45:39 +0000151 if (frags.size() == 1) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700152 // even if indexed fragmentation is enabled, the fragmenter should not
153 // fragment the packet if it can fit in MTU
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700154 BOOST_ASSERT(!frags.front().has<lp::FragIndexField>());
155 BOOST_ASSERT(!frags.front().has<lp::FragCountField>());
156 }
157
Eric Newberry185ab292017-03-28 06:45:39 +0000158 // Only assign sequences to fragments if reliability enabled and packet contains a fragment,
159 // or there is more than 1 fragment
160 if ((m_options.reliabilityOptions.isEnabled && frags.front().has<lp::FragmentField>()) ||
161 frags.size() > 1) {
162 // Assign sequences to all fragments
163 this->assignSequences(frags);
164 }
165
166 if (m_options.reliabilityOptions.isEnabled && frags.front().has<lp::FragmentField>()) {
167 m_reliability.observeOutgoing(frags);
168 }
169
170 for (lp::Packet& frag : frags) {
171 this->sendLpPacket(std::move(frag));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700172 }
173}
174
175void
176GenericLinkService::assignSequence(lp::Packet& pkt)
177{
178 pkt.set<lp::SequenceField>(++m_lastSeqNo);
179}
180
181void
182GenericLinkService::assignSequences(std::vector<lp::Packet>& pkts)
183{
184 std::for_each(pkts.begin(), pkts.end(), bind(&GenericLinkService::assignSequence, this, _1));
185}
186
187void
Eric Newberrya98bf932015-09-21 00:58:47 -0700188GenericLinkService::doReceivePacket(Transport::Packet&& packet)
189{
Eric Newberry86d31872015-09-23 16:24:59 -0700190 try {
Eric Newberrya1939ba2015-10-09 12:35:03 -0700191 lp::Packet pkt(packet.packet);
192
Eric Newberry185ab292017-03-28 06:45:39 +0000193 if (m_options.reliabilityOptions.isEnabled) {
194 m_reliability.processIncomingPacket(pkt);
195 }
196
Eric Newberrya1939ba2015-10-09 12:35:03 -0700197 if (!pkt.has<lp::FragmentField>()) {
198 NFD_LOG_FACE_TRACE("received IDLE packet: DROP");
199 return;
200 }
201
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700202 if ((pkt.has<lp::FragIndexField>() || pkt.has<lp::FragCountField>()) &&
203 !m_options.allowReassembly) {
204 NFD_LOG_FACE_WARN("received fragment, but reassembly disabled: DROP");
Eric Newberrya1939ba2015-10-09 12:35:03 -0700205 return;
206 }
207
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700208 bool isReassembled = false;
209 Block netPkt;
210 lp::Packet firstPkt;
211 std::tie(isReassembled, netPkt, firstPkt) = m_reassembler.receiveFragment(packet.remoteEndpoint,
212 pkt);
213 if (isReassembled) {
214 this->decodeNetPacket(netPkt, firstPkt);
215 }
216 }
217 catch (const tlv::Error& e) {
218 ++this->nInLpInvalid;
219 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
220 }
221}
Eric Newberry86d31872015-09-23 16:24:59 -0700222
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700223void
224GenericLinkService::decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt)
225{
226 try {
Eric Newberry86d31872015-09-23 16:24:59 -0700227 switch (netPkt.type()) {
228 case tlv::Interest:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700229 if (firstPkt.has<lp::NackField>()) {
230 this->decodeNack(netPkt, firstPkt);
Eric Newberry86d31872015-09-23 16:24:59 -0700231 }
232 else {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700233 this->decodeInterest(netPkt, firstPkt);
Eric Newberry86d31872015-09-23 16:24:59 -0700234 }
235 break;
236 case tlv::Data:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700237 this->decodeData(netPkt, firstPkt);
Eric Newberry86d31872015-09-23 16:24:59 -0700238 break;
239 default:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700240 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700241 NFD_LOG_FACE_WARN("unrecognized network-layer packet TLV-TYPE " << netPkt.type() << ": DROP");
242 return;
Eric Newberrya98bf932015-09-21 00:58:47 -0700243 }
244 }
Eric Newberry86d31872015-09-23 16:24:59 -0700245 catch (const tlv::Error& e) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700246 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700247 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
248 }
249}
250
Eric Newberry86d31872015-09-23 16:24:59 -0700251void
252GenericLinkService::decodeInterest(const Block& netPkt, const lp::Packet& firstPkt)
253{
254 BOOST_ASSERT(netPkt.type() == tlv::Interest);
255 BOOST_ASSERT(!firstPkt.has<lp::NackField>());
256
257 // forwarding expects Interest to be created with make_shared
258 auto interest = make_shared<Interest>(netPkt);
259
260 if (firstPkt.has<lp::NextHopFaceIdField>()) {
261 if (m_options.allowLocalFields) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000262 interest->setTag(make_shared<lp::NextHopFaceIdTag>(firstPkt.get<lp::NextHopFaceIdField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700263 }
264 else {
265 NFD_LOG_FACE_WARN("received NextHopFaceId, but local fields disabled: DROP");
266 return;
267 }
268 }
269
270 if (firstPkt.has<lp::CachePolicyField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700271 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700272 NFD_LOG_FACE_WARN("received CachePolicy with Interest: DROP");
273 return;
274 }
275
276 if (firstPkt.has<lp::IncomingFaceIdField>()) {
277 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
278 }
279
Eric Newberryee400b52016-11-24 14:12:48 +0000280 if (firstPkt.has<lp::CongestionMarkField>()) {
281 interest->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
282 }
283
Eric Newberry86d31872015-09-23 16:24:59 -0700284 this->receiveInterest(*interest);
285}
286
287void
288GenericLinkService::decodeData(const Block& netPkt, const lp::Packet& firstPkt)
289{
290 BOOST_ASSERT(netPkt.type() == tlv::Data);
291
292 // forwarding expects Data to be created with make_shared
293 auto data = make_shared<Data>(netPkt);
294
295 if (firstPkt.has<lp::NackField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700296 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700297 NFD_LOG_FACE_WARN("received Nack with Data: DROP");
298 return;
299 }
300
301 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700302 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700303 NFD_LOG_FACE_WARN("received NextHopFaceId with Data: DROP");
304 return;
305 }
306
307 if (firstPkt.has<lp::CachePolicyField>()) {
308 if (m_options.allowLocalFields) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000309 // In case of an invalid CachePolicyType, get<lp::CachePolicyField> will throw,
310 // so it's unnecessary to check here.
311 data->setTag(make_shared<lp::CachePolicyTag>(firstPkt.get<lp::CachePolicyField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700312 }
313 else {
314 NFD_LOG_FACE_WARN("received CachePolicy, but local fields disabled: IGNORE");
315 }
316 }
317
318 if (firstPkt.has<lp::IncomingFaceIdField>()) {
319 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
320 }
321
Eric Newberryee400b52016-11-24 14:12:48 +0000322 if (firstPkt.has<lp::CongestionMarkField>()) {
323 data->setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
324 }
325
Eric Newberry86d31872015-09-23 16:24:59 -0700326 this->receiveData(*data);
327}
328
329void
330GenericLinkService::decodeNack(const Block& netPkt, const lp::Packet& firstPkt)
331{
332 BOOST_ASSERT(netPkt.type() == tlv::Interest);
333 BOOST_ASSERT(firstPkt.has<lp::NackField>());
334
335 lp::Nack nack((Interest(netPkt)));
336 nack.setHeader(firstPkt.get<lp::NackField>());
337
338 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700339 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700340 NFD_LOG_FACE_WARN("received NextHopFaceId with Nack: DROP");
341 return;
342 }
343
344 if (firstPkt.has<lp::CachePolicyField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700345 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700346 NFD_LOG_FACE_WARN("received CachePolicy with Nack: DROP");
347 return;
348 }
349
350 if (firstPkt.has<lp::IncomingFaceIdField>()) {
351 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
352 }
353
Eric Newberryee400b52016-11-24 14:12:48 +0000354 if (firstPkt.has<lp::CongestionMarkField>()) {
355 nack.setTag(make_shared<lp::CongestionMarkTag>(firstPkt.get<lp::CongestionMarkField>()));
356 }
357
Eric Newberry86d31872015-09-23 16:24:59 -0700358 this->receiveNack(nack);
Eric Newberrya98bf932015-09-21 00:58:47 -0700359}
360
361} // namespace face
362} // namespace nfd